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