Add doc repo as submodule
[frida/frida.git] / src / core / InformationManager.cxx
1 #include "InformationManager.hxx"
2 #include "bindings/Interpreter.hxx"
3 #include "disassembler/llvm/LLVMDisassembler.hxx"
4 #include "core/Function.hxx"
5 #include "core/BasicBlock.hxx"
6 #include "core/Comment.hxx"
7 #include "core/events/ChangeCommentEvent.hxx"
8
9 #include "qt.hxx"
10 #include <quazip/quazip.h>
11 #include <quazip/quazipfile.h>
12
13 #include <QTemporaryFile>
14
15 InformationManager* current_information_manager;
16
17 InformationManager::InformationManager()
18 : logger(log4cxx::Logger::getLogger("core.InformationManager"))
19 {
20 current_information_manager = this;
21 qRegisterMetaType<NewFunctionEvent>("NewFunctionEvent");
22
23 QPluginLoader* loader = new QPluginLoader("libguilePlugin", NULL);
24 if (!loader->load())
25 LOG4CXX_ERROR(logger, "Loading plugin failed: " << loader->errorString().toStdString());
26 interpreters["GUILE"] = qobject_cast<Interpreter*>(loader->instance());
27 plugins.push_back(loader);
28 }
29
30 InformationManager::~InformationManager() {
31 disassemblerThread.quit();
32 for (auto b : blocks)
33 delete b.second;
34
35 for (auto f : functions)
36 delete f.second;
37
38 for (auto i : plugins)
39 delete i;
40 disassemblerThread.wait();
41 }
42
43 void InformationManager::reset(const std::string& filename) {
44 this->filename = filename;
45 disassembler.reset(createLLVMDisassembler(filename, this));
46 if (disassembler.get() != NULL) {
47 emit resetEvent();
48 #ifndef CONFIG_TEST
49 disassembler.get()->moveToThread(&disassemblerThread);
50 connect(&disassemblerThread, &QThread::started,
51 disassembler.get(), &Disassembler::start);
52 disassemblerThread.start();
53 #else
54 disassembler.get()->start();
55 #endif
56 }
57 }
58
59 void InformationManager::load(const std::string& filename) {
60 QuaZip zip(filename.c_str());
61 QuaZipFile file(&zip);
62 QuaZipFileInfo info;
63
64 if (!zip.open(QuaZip::mdUnzip)) {
65 LOG4CXX_ERROR(logger, "Failed to open archive " << filename);
66 return;
67 }
68 tmpfile.reset(new QTemporaryFile());
69
70 {
71 LOG4CXX_INFO(logger, "Loading binary from archive");
72 zip.setCurrentFile("binary");
73 tmpfile->open();
74 file.open(QIODevice::ReadOnly);
75 QByteArray buffer;
76 while (!file.atEnd()) {
77 buffer = file.read(4096);
78 tmpfile->write(buffer);
79 }
80 tmpfile->flush();
81 file.close();
82 disassembler.reset(createLLVMDisassembler(tmpfile->fileName().toStdString(), this));
83 }
84
85 for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
86 zip.getCurrentFileInfo(&info);
87 file.open(QIODevice::ReadOnly);
88
89 if(info.name != "binary") {
90 if (info.name.startsWith("comment:")) {
91 QXmlStreamReader reader(&file);
92 auto starttoken = reader.readNext();
93 auto elementtoken = reader.readNext();
94 assert(QXmlStreamReader::StartDocument == starttoken);
95 assert(QXmlStreamReader::StartElement == elementtoken);
96 Comment::deserialize(reader, this);
97 } else {
98 QXmlStreamReader reader(&file);
99 auto starttoken = reader.readNext();
100 auto elementtoken = reader.readNext();
101 assert(QXmlStreamReader::StartDocument == starttoken);
102 assert(QXmlStreamReader::StartElement == elementtoken);
103 Function::deserialize(reader, this);
104 }
105 }
106 file.close();
107 }
108 }
109
110 void InformationManager::save(const std::string& filename) {
111 QuaZip zip(filename.c_str());
112 zip.open(QuaZip::mdCreate);
113 zip.setComment("FRIDA 0.0");
114 QuaZipFile outZipFile(&zip);
115
116 {
117 QFile binary(this->filename.c_str());
118 binary.open(QIODevice::ReadOnly);
119 QuaZipNewInfo zipinfo("binary");
120 zipinfo.setPermissions(static_cast<QFile::Permissions>(0x6444));
121 outZipFile.open(QIODevice::WriteOnly, zipinfo);
122 QByteArray buffer;
123 while (!binary.atEnd()) {
124 buffer = binary.read(4096);
125 outZipFile.write(buffer);
126 }
127 outZipFile.close();
128 }
129
130 for (auto funpair : functions) {
131 Function* fun = funpair.second;
132 QuaZipNewInfo zipinfo(fun->getName().c_str());
133 zipinfo.setPermissions(static_cast<QFile::Permissions>(0x6444));
134 outZipFile.open(QIODevice::WriteOnly, zipinfo);
135 QXmlStreamWriter stream(&outZipFile);
136 stream.setAutoFormatting(true);
137 stream.setAutoFormattingIndent(-1);
138 stream.writeStartDocument();
139
140 fun->serialize(stream);
141
142 stream.writeEndDocument();
143 outZipFile.close();
144 }
145 for (auto commentpair : comments) {
146 Comment* comment = commentpair.second;
147 if (!comment->isLocal()) {
148 QuaZipNewInfo zipinfo(QString("comment:%1").arg(comment->getAddress(), 0, 16));
149 zipinfo.setPermissions(static_cast<QFile::Permissions>(0x6444));
150 outZipFile.open(QIODevice::WriteOnly, zipinfo);
151 QXmlStreamWriter stream(&outZipFile);
152 stream.setAutoFormatting(true);
153 stream.setAutoFormattingIndent(-1);
154 stream.writeStartDocument();
155
156 comment->serialize(stream);
157
158 stream.writeEndDocument();
159 outZipFile.close();
160 }
161 }
162
163 zip.close();
164 }
165
166
167 /* *******************************
168 * Accessors for the Functions map
169 */
170
171 Function* InformationManager::getFunction(uint64_t address) {
172 auto it = functions.find(address);
173 if (it != functions.end())
174 return it->second;
175 else
176 return NULL;
177 }
178
179 std::map<uint64_t, Function*>::const_iterator InformationManager::beginFunctions() {
180 return functions.begin();
181 }
182 std::map<uint64_t, Function*>::const_iterator InformationManager::endFunctions() {
183 return functions.end();
184 }
185
186
187 /* *********************************
188 * Accessors for the BasicBlocks map
189 */
190
191 BasicBlock* InformationManager::getBasicBlock(uint64_t address) {
192 auto it = blocks.find(address);
193 if (it != blocks.end())
194 return it->second;
195 else
196 return NULL;
197 }
198
199 std::map<uint64_t, BasicBlock*>::const_iterator InformationManager::beginBasicBlocks() {
200 return blocks.begin();
201 }
202 std::map<uint64_t, BasicBlock*>::const_iterator InformationManager::endBasicBlocks() {
203 return blocks.end();
204 }
205
206 /* *********************************
207 * Accessors for the Comments map
208 */
209 std::pair<
210 std::multimap<uint64_t, Comment*>::const_iterator,
211 std::multimap<uint64_t, Comment*>::const_iterator>
212 InformationManager::getComments(uint64_t address) {
213 return comments.equal_range(address);
214 }
215
216 std::multimap<uint64_t, Comment*>::const_iterator InformationManager::beginComments() {
217 return comments.begin();
218 }
219 std::multimap<uint64_t, Comment*>::const_iterator InformationManager::endComments() {
220 return comments.end();
221 }
222
223 /* *********************************
224 * Accessors for the Interpreter map
225 */
226
227 Interpreter* InformationManager::getInterpreter(const std::string& name) {
228 auto it = interpreters.find(name);
229 if (it != interpreters.end())
230 return it->second;
231 else
232 return NULL;
233 }
234
235 std::map<std::string, Interpreter*>::const_iterator InformationManager::beginInterpreters() {
236 return interpreters.begin();
237 }
238 std::map<std::string, Interpreter*>::const_iterator InformationManager::endInterpreters() {
239 return interpreters.end();
240 }
241
242
243 /* ********************************
244 * Factory methods for data classes
245 */
246
247 Function* InformationManager::newFunction(uint64_t address) {
248 Function* fun = new Function(address, false, this);
249 functions.insert(std::make_pair(address, fun));
250 return fun;
251 }
252
253 Function* InformationManager::newDynamicFunction(uint64_t address) {
254 Function* fun = new Function(address, true, this);
255 functions.insert(std::make_pair(address, fun));
256 return fun;
257 }
258
259 BasicBlock* InformationManager::newBasicBlock(uint64_t address) {
260 BasicBlock* block = new BasicBlock(address, this);
261 blocks.insert(std::make_pair(address, block));
262 return block;
263 }
264
265 Comment* InformationManager::newGlobalComment(uint64_t address) {
266 Comment* comment = new Comment(address, this);
267 comments.insert(std::make_pair(address, comment));
268 return comment;
269 }
270
271 Comment* InformationManager::newLocalComment(uint64_t address, Function* f) {
272 Comment* comment = new Comment(address, f, this);
273 comments.insert(std::make_pair(address, comment));
274 return comment;
275 }
276
277 void InformationManager::finishFunction(Function* fun) {
278 LOG4CXX_DEBUG(logger, "Finishing function " << fun->getName());
279 for (auto b : fun->blocks()) {
280 BasicBlock* bl = b.second;
281 blocks.insert(std::make_pair(bl->getStartAddress(), bl));
282 }
283 NewFunctionEvent event(fun->getStartAddress(), fun);
284 emit newFunctionEvent(event);
285 }
286
287 void InformationManager::finishBasicBlock(BasicBlock*) {
288 }
289
290 void InformationManager::finishComment(Comment* c) {
291 LOG4CXX_DEBUG(logger, "Finishing comment " << c->getAddress());
292 ChangeCommentEvent event(c->getAddress(), c->getLocation(), c);
293 emit changeCommentEvent(&event);
294 }
295
296 void InformationManager::deleteFunction(Function* f) {
297 functions.erase(f->getStartAddress());
298 delete f;
299 }
300
301 void InformationManager::deleteBasicBlock(BasicBlock* b) {
302 blocks.erase(b->getStartAddress());
303 delete b;
304 }
305
306 void InformationManager::deleteComment(Comment* c) {
307 auto range = comments.equal_range(c->getAddress());
308 for (auto it = range.first; it != range.second; ++it) {
309 if (it->second == c) {
310 comments.erase(it);
311 break;
312 }
313 }
314 delete c;
315 }