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