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"
11 #include <quazip/quazip.h>
12 #include <quazip/quazipfile.h>
14 #include <QTemporaryFile>
16 InformationManager
* current_information_manager
;
18 InformationManager::InformationManager()
19 : logger(log4cxx::Logger::getLogger("core.InformationManager"))
21 current_information_manager
= this;
23 QPluginLoader
* loader
= new QPluginLoader("libguilePlugin", NULL
);
25 LOG4CXX_ERROR(logger
, "Loading plugin failed: " << loader
->errorString().toStdString());
26 interpreters
["GUILE"] = qobject_cast
<Interpreter
*>(loader
->instance());
27 plugins
.push_back(loader
);
30 InformationManager::~InformationManager() {
34 for (auto f
: functions
)
37 for (auto i
: plugins
)
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();
48 void InformationManager::load(const std::string
& filename
) {
49 QuaZip
zip(filename
.c_str());
50 QuaZipFile
file(&zip
);
53 zip
.open(QuaZip::mdUnzip
);
54 tmpfile
.reset(new QTemporaryFile());
57 LOG4CXX_INFO(logger
, "Loading binary from archive");
58 zip
.setCurrentFile("binary");
60 file
.open(QIODevice::ReadOnly
);
62 while (!file
.atEnd()) {
63 buffer
= file
.read(4096);
64 tmpfile
->write(buffer
);
68 disassembler
.reset(createLLVMDisassembler(tmpfile
->fileName().toStdString(), this));
71 for (bool more
= zip
.goToFirstFile(); more
; more
= zip
.goToNextFile()) {
72 zip
.getCurrentFileInfo(&info
);
73 file
.open(QIODevice::ReadOnly
);
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);
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);
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
);
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
);
109 while (!binary
.atEnd()) {
110 buffer
= binary
.read(4096);
111 outZipFile
.write(buffer
);
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();
126 fun
->serialize(stream
);
128 stream
.writeEndDocument();
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();
142 comment
->serialize(stream
);
144 stream
.writeEndDocument();
153 /* *******************************
154 * Accessors for the Functions map
157 Function
* InformationManager::getFunction(uint64_t address
) {
158 auto it
= functions
.find(address
);
159 if (it
!= functions
.end())
165 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::beginFunctions() {
166 return functions
.begin();
168 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::endFunctions() {
169 return functions
.end();
173 /* *********************************
174 * Accessors for the BasicBlocks map
177 BasicBlock
* InformationManager::getBasicBlock(uint64_t address
) {
178 auto it
= blocks
.find(address
);
179 if (it
!= blocks
.end())
185 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::beginBasicBlocks() {
186 return blocks
.begin();
188 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::endBasicBlocks() {
192 /* *********************************
193 * Accessors for the Comments map
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
);
202 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::beginComments() {
203 return comments
.begin();
205 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::endComments() {
206 return comments
.end();
209 /* *********************************
210 * Accessors for the Interpreter map
213 Interpreter
* InformationManager::getInterpreter(const std::string
& name
) {
214 auto it
= interpreters
.find(name
);
215 if (it
!= interpreters
.end())
221 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::beginInterpreters() {
222 return interpreters
.begin();
224 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::endInterpreters() {
225 return interpreters
.end();
229 /* ********************************
230 * Factory methods for data classes
233 Function
* InformationManager::newFunction(uint64_t address
) {
234 Function
* fun
= new Function(address
, false, this);
235 functions
.insert(std::make_pair(address
, fun
));
239 Function
* InformationManager::newDynamicFunction(uint64_t address
) {
240 Function
* fun
= new Function(address
, true, this);
241 functions
.insert(std::make_pair(address
, fun
));
245 BasicBlock
* InformationManager::newBasicBlock(uint64_t address
) {
246 BasicBlock
* block
= new BasicBlock(address
, this);
247 blocks
.insert(std::make_pair(address
, block
));
251 Comment
* InformationManager::newGlobalComment(uint64_t address
) {
252 Comment
* comment
= new Comment(address
, this);
253 comments
.insert(std::make_pair(address
, comment
));
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
));
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
));
269 NewFunctionEvent
event(fun
->getStartAddress(), fun
);
273 void InformationManager::finishBasicBlock(BasicBlock
*) {
276 void InformationManager::finishComment(Comment
* c
) {
277 LOG4CXX_DEBUG(logger
, "Finishing comment " << c
->getAddress());
278 ChangeCommentEvent
event(c
->getAddress(), c
->getLocation(), c
);
282 void InformationManager::deleteFunction(Function
* f
) {
283 functions
.erase(f
->getStartAddress());
287 void InformationManager::deleteBasicBlock(BasicBlock
* b
) {
288 blocks
.erase(b
->getStartAddress());
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
) {