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