]> git.siccegge.de Git - frida/frida.git/blob - src/core/InformationManager.cxx
Add SWIG bindings for the guile interpreter
[frida/frida.git] / src / core / InformationManager.cxx
1 #include "InformationManager.hxx"
2 #include "disassembler/llvm/LLVMDisassembler.hxx"
3 #include "core/Function.hxx"
4 #include "core/BasicBlock.hxx"
5 #include "core/Comment.hxx"
6
7 #include "gui/qt.hxx"
8 #include <quazip/quazip.h>
9 #include <quazip/quazipfile.h>
10
11 #include <QTemporaryFile>
12
13 InformationManager* current_information_manager;
14
15 InformationManager::InformationManager()
16 : logger(log4cxx::Logger::getLogger("InformationManager"))
17 {
18 current_information_manager = this;
19 }
20
21 InformationManager::~InformationManager() {
22 for (auto b : blocks)
23 delete b.second;
24
25 for (auto f : functions)
26 delete f.second;
27 }
28
29 void InformationManager::reset(const std::string& filename) {
30 this->filename = filename;
31 disassembler.reset(createLLVMDisassembler(filename, this));
32 if (disassembler.get() != NULL)
33 disassembler.get()->start();
34 }
35
36 void InformationManager::load(const std::string& filename) {
37 QuaZip zip(filename.c_str());
38 QuaZipFile file(&zip);
39 QuaZipFileInfo info;
40
41 zip.open(QuaZip::mdUnzip);
42 tmpfile.reset(new QTemporaryFile());
43
44 {
45 LOG4CXX_INFO(logger, "Loading binary from archive");
46 zip.setCurrentFile("binary");
47 tmpfile->open();
48 file.open(QIODevice::ReadOnly);
49 QByteArray buffer;
50 while (!file.atEnd()) {
51 buffer = file.read(4096);
52 tmpfile->write(buffer);
53 }
54 tmpfile->flush();
55 file.close();
56 disassembler.reset(createLLVMDisassembler(tmpfile->fileName().toStdString(), this));
57 }
58
59 for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
60 zip.getCurrentFileInfo(&info);
61 file.open(QIODevice::ReadOnly);
62
63 if(info.name != "binary") {
64 QXmlStreamReader reader(&file);
65 assert(QXmlStreamReader::StartDocument == reader.readNext());
66 assert(QXmlStreamReader::StartElement == reader.readNext());
67 Function * fun = Function::deserialize(reader, this);
68 }
69 file.close();
70 }
71 }
72
73 void InformationManager::save(const std::string& filename) {
74 QuaZip zip(filename.c_str());
75 zip.open(QuaZip::mdCreate);
76 zip.setComment("FRIDA 0.0");
77 QuaZipFile outZipFile(&zip);
78
79 {
80 QFile binary(this->filename.c_str());
81 binary.open(QIODevice::ReadOnly);
82 QuaZipNewInfo zipinfo("binary");
83 zipinfo.setPermissions(static_cast<QFile::Permissions>(0x6444));
84 outZipFile.open(QIODevice::WriteOnly, zipinfo);
85 QByteArray buffer;
86 while (!binary.atEnd()) {
87 buffer = binary.read(4096);
88 outZipFile.write(buffer);
89 }
90 outZipFile.close();
91 }
92
93 for (auto funpair : functions) {
94 Function* fun = funpair.second;
95 QuaZipNewInfo zipinfo(fun->getName().c_str());
96 zipinfo.setPermissions(static_cast<QFile::Permissions>(0x6444));
97 outZipFile.open(QIODevice::WriteOnly, zipinfo);
98 QXmlStreamWriter stream(&outZipFile);
99 stream.setAutoFormatting(true);
100 stream.setAutoFormattingIndent(-1);
101 stream.writeStartDocument();
102
103 fun->serialize(stream);
104
105 stream.writeEndDocument();
106 outZipFile.close();
107 }
108
109 zip.close();
110 }
111
112 void InformationManager::signal_new_function(Function* fun) {
113 }
114
115 Function* InformationManager::getFunction(uint64_t address) {
116 auto it = functions.find(address);
117 if (it != functions.end())
118 return it->second;
119 else
120 return NULL;
121 }
122
123 BasicBlock* InformationManager::getBasicBlock(uint64_t address) {
124 auto it = blocks.find(address);
125 if (it != blocks.end())
126 return it->second;
127 else
128 return NULL;
129 }
130
131 Function* InformationManager::newFunction(uint64_t address) {
132 Function* fun = new Function(address, this);
133 functions.insert(std::make_pair(address, fun));
134 return fun;
135 }
136
137 BasicBlock* InformationManager::newBasicBlock(uint64_t address) {
138 BasicBlock* block = new BasicBlock(address, this);
139 blocks.insert(std::make_pair(address, block));
140 return block;
141 }
142
143 Comment* InformationManager::newGlobalComment(uint64_t address) {
144 return NULL;
145 }
146
147 Comment* InformationManager::newLocalComment(uint64_t address, Function* f) {
148 return NULL;
149 }
150
151 void InformationManager::finishFunction(Function* fun) {
152 LOG4CXX_DEBUG(logger, "Finishing function " << fun->getName());
153 for (auto b : fun->blocks()) {
154 BasicBlock* bl = b.second;
155 blocks.insert(std::make_pair(bl->getStartAddress(), bl));
156 }
157 new_function_signal(fun);
158 }
159
160 void InformationManager::finishBasicBlock(BasicBlock* b) {
161 }
162
163 void InformationManager::finnishComment(Comment* c) {
164 }
165
166 void InformationManager::deleteFunction(Function* f) {
167 functions.erase(f->getStartAddress());
168 delete f;
169 }
170
171 void InformationManager::deleteBasicBlock(BasicBlock* b) {
172 blocks.erase(b->getStartAddress());
173 delete b;
174 }
175
176 void InformationManager::deleteComment(Comment* c) {
177 delete c;
178 }