]> git.siccegge.de Git - frida/frida.git/blob - src/core/InformationManager.cxx
Complete accessors for InformationManager managed objects
[frida/frida.git] / src / core / InformationManager.cxx
1 #include "InformationManager.hxx"
2 #include "disassembler/llvm/LLVMDisassembler.hxx"
3 #include "core/Function.hxx"
4 #include "core/BasicBlock.hxx"
5 #include "core/Comment.hxx"
6
7 #include "gui/qt.hxx"
8 #include <quazip/quazip.h>
9 #include <quazip/quazipfile.h>
10
11 #include <QTemporaryFile>
12
13 InformationManager* current_information_manager;
14
15 InformationManager::InformationManager()
16 : logger(log4cxx::Logger::getLogger("core.InformationManager"))
17 {
18 current_information_manager = this;
19 }
20
21 InformationManager::~InformationManager() {
22 for (auto b : blocks)
23 delete b.second;
24
25 for (auto f : functions)
26 delete f.second;
27 }
28
29 void InformationManager::reset(const std::string& filename) {
30 this->filename = filename;
31 disassembler.reset(createLLVMDisassembler(filename, this));
32 if (disassembler.get() != NULL)
33 disassembler.get()->start();
34 }
35
36 void InformationManager::load(const std::string& filename) {
37 QuaZip zip(filename.c_str());
38 QuaZipFile file(&zip);
39 QuaZipFileInfo info;
40
41 zip.open(QuaZip::mdUnzip);
42 tmpfile.reset(new QTemporaryFile());
43
44 {
45 LOG4CXX_INFO(logger, "Loading binary from archive");
46 zip.setCurrentFile("binary");
47 tmpfile->open();
48 file.open(QIODevice::ReadOnly);
49 QByteArray buffer;
50 while (!file.atEnd()) {
51 buffer = file.read(4096);
52 tmpfile->write(buffer);
53 }
54 tmpfile->flush();
55 file.close();
56 disassembler.reset(createLLVMDisassembler(tmpfile->fileName().toStdString(), this));
57 }
58
59 for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
60 zip.getCurrentFileInfo(&info);
61 file.open(QIODevice::ReadOnly);
62
63 if(info.name != "binary") {
64 QXmlStreamReader reader(&file);
65 assert(QXmlStreamReader::StartDocument == reader.readNext());
66 assert(QXmlStreamReader::StartElement == reader.readNext());
67 Function * fun = Function::deserialize(reader, this);
68 }
69 file.close();
70 }
71 }
72
73 void InformationManager::save(const std::string& filename) {
74 QuaZip zip(filename.c_str());
75 zip.open(QuaZip::mdCreate);
76 zip.setComment("FRIDA 0.0");
77 QuaZipFile outZipFile(&zip);
78
79 {
80 QFile binary(this->filename.c_str());
81 binary.open(QIODevice::ReadOnly);
82 QuaZipNewInfo zipinfo("binary");
83 zipinfo.setPermissions(static_cast<QFile::Permissions>(0x6444));
84 outZipFile.open(QIODevice::WriteOnly, zipinfo);
85 QByteArray buffer;
86 while (!binary.atEnd()) {
87 buffer = binary.read(4096);
88 outZipFile.write(buffer);
89 }
90 outZipFile.close();
91 }
92
93 for (auto funpair : functions) {
94 Function* fun = funpair.second;
95 QuaZipNewInfo zipinfo(fun->getName().c_str());
96 zipinfo.setPermissions(static_cast<QFile::Permissions>(0x6444));
97 outZipFile.open(QIODevice::WriteOnly, zipinfo);
98 QXmlStreamWriter stream(&outZipFile);
99 stream.setAutoFormatting(true);
100 stream.setAutoFormattingIndent(-1);
101 stream.writeStartDocument();
102
103 fun->serialize(stream);
104
105 stream.writeEndDocument();
106 outZipFile.close();
107 }
108
109 zip.close();
110 }
111
112 void InformationManager::signal_new_function(Function* fun) {
113 }
114
115
116
117 /* *******************************
118 * Accessors for the Functions map
119 */
120
121 Function* InformationManager::getFunction(uint64_t address) {
122 auto it = functions.find(address);
123 if (it != functions.end())
124 return it->second;
125 else
126 return NULL;
127 }
128
129 std::map<uint64_t, Function*>::const_iterator InformationManager::beginFunctions() {
130 return functions.begin();
131 }
132 std::map<uint64_t, Function*>::const_iterator InformationManager::endFunctions() {
133 return functions.end();
134 }
135
136
137 /* *********************************
138 * Accessors for the BasicBlocks map
139 */
140
141 BasicBlock* InformationManager::getBasicBlock(uint64_t address) {
142 auto it = blocks.find(address);
143 if (it != blocks.end())
144 return it->second;
145 else
146 return NULL;
147 }
148
149 std::map<uint64_t, BasicBlock*>::const_iterator InformationManager::beginBasicBlocks() {
150 return blocks.begin();
151 }
152 std::map<uint64_t, BasicBlock*>::const_iterator InformationManager::endBasicBlocks() {
153 return blocks.end();
154 }
155
156
157
158 /* ********************************
159 * Factory methods for data classes
160 */
161
162 Function* InformationManager::newFunction(uint64_t address) {
163 Function* fun = new Function(address, this);
164 functions.insert(std::make_pair(address, fun));
165 return fun;
166 }
167
168 BasicBlock* InformationManager::newBasicBlock(uint64_t address) {
169 BasicBlock* block = new BasicBlock(address, this);
170 blocks.insert(std::make_pair(address, block));
171 return block;
172 }
173
174 Comment* InformationManager::newGlobalComment(uint64_t address) {
175 return NULL;
176 }
177
178 Comment* InformationManager::newLocalComment(uint64_t address, Function* f) {
179 return NULL;
180 }
181
182 void InformationManager::finishFunction(Function* fun) {
183 LOG4CXX_DEBUG(logger, "Finishing function " << fun->getName());
184 for (auto b : fun->blocks()) {
185 BasicBlock* bl = b.second;
186 blocks.insert(std::make_pair(bl->getStartAddress(), bl));
187 }
188 new_function_signal(fun);
189 }
190
191 void InformationManager::finishBasicBlock(BasicBlock* b) {
192 }
193
194 void InformationManager::finnishComment(Comment* c) {
195 }
196
197 void InformationManager::deleteFunction(Function* f) {
198 functions.erase(f->getStartAddress());
199 delete f;
200 }
201
202 void InformationManager::deleteBasicBlock(BasicBlock* b) {
203 blocks.erase(b->getStartAddress());
204 delete b;
205 }
206
207 void InformationManager::deleteComment(Comment* c) {
208 delete c;
209 }