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