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