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 QXmlStreamReader
reader(&file
);
77 assert(QXmlStreamReader::StartDocument
== reader
.readNext());
78 assert(QXmlStreamReader::StartElement
== reader
.readNext());
79 Function::deserialize(reader
, this);
85 void InformationManager::save(const std::string
& filename
) {
86 QuaZip
zip(filename
.c_str());
87 zip
.open(QuaZip::mdCreate
);
88 zip
.setComment("FRIDA 0.0");
89 QuaZipFile
outZipFile(&zip
);
92 QFile
binary(this->filename
.c_str());
93 binary
.open(QIODevice::ReadOnly
);
94 QuaZipNewInfo
zipinfo("binary");
95 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
96 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
98 while (!binary
.atEnd()) {
99 buffer
= binary
.read(4096);
100 outZipFile
.write(buffer
);
105 for (auto funpair
: functions
) {
106 Function
* fun
= funpair
.second
;
107 QuaZipNewInfo
zipinfo(fun
->getName().c_str());
108 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
109 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
110 QXmlStreamWriter
stream(&outZipFile
);
111 stream
.setAutoFormatting(true);
112 stream
.setAutoFormattingIndent(-1);
113 stream
.writeStartDocument();
115 fun
->serialize(stream
);
117 stream
.writeEndDocument();
125 /* *******************************
126 * Accessors for the Functions map
129 Function
* InformationManager::getFunction(uint64_t address
) {
130 auto it
= functions
.find(address
);
131 if (it
!= functions
.end())
137 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::beginFunctions() {
138 return functions
.begin();
140 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::endFunctions() {
141 return functions
.end();
145 /* *********************************
146 * Accessors for the BasicBlocks map
149 BasicBlock
* InformationManager::getBasicBlock(uint64_t address
) {
150 auto it
= blocks
.find(address
);
151 if (it
!= blocks
.end())
157 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::beginBasicBlocks() {
158 return blocks
.begin();
160 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::endBasicBlocks() {
164 /* *********************************
165 * Accessors for the Comments map
168 std::multimap
<uint64_t, Comment
*>::const_iterator
,
169 std::multimap
<uint64_t, Comment
*>::const_iterator
>
170 InformationManager::getComments(uint64_t address
) {
171 return comments
.equal_range(address
);
174 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::beginComments() {
175 return comments
.begin();
177 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::endComments() {
178 return comments
.end();
181 /* *********************************
182 * Accessors for the Interpreter map
185 Interpreter
* InformationManager::getInterpreter(const std::string
& name
) {
186 auto it
= interpreters
.find(name
);
187 if (it
!= interpreters
.end())
193 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::beginInterpreters() {
194 return interpreters
.begin();
196 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::endInterpreters() {
197 return interpreters
.end();
201 /* ********************************
202 * Factory methods for data classes
205 Function
* InformationManager::newFunction(uint64_t address
) {
206 Function
* fun
= new Function(address
, false, this);
207 functions
.insert(std::make_pair(address
, fun
));
211 Function
* InformationManager::newDynamicFunction(uint64_t address
) {
212 Function
* fun
= new Function(address
, true, this);
213 functions
.insert(std::make_pair(address
, fun
));
217 BasicBlock
* InformationManager::newBasicBlock(uint64_t address
) {
218 BasicBlock
* block
= new BasicBlock(address
, this);
219 blocks
.insert(std::make_pair(address
, block
));
223 Comment
* InformationManager::newGlobalComment(uint64_t address
) {
224 Comment
* comment
= new Comment(address
, this);
225 comments
.insert(std::make_pair(address
, comment
));
229 Comment
* InformationManager::newLocalComment(uint64_t address
, Function
* f
) {
230 Comment
* comment
= new Comment(address
, f
, this);
231 comments
.insert(std::make_pair(address
, comment
));
235 void InformationManager::finishFunction(Function
* fun
) {
236 LOG4CXX_DEBUG(logger
, "Finishing function " << fun
->getName());
237 for (auto b
: fun
->blocks()) {
238 BasicBlock
* bl
= b
.second
;
239 blocks
.insert(std::make_pair(bl
->getStartAddress(), bl
));
241 NewFunctionEvent
event(fun
->getStartAddress(), fun
);
245 void InformationManager::finishBasicBlock(BasicBlock
*) {
248 void InformationManager::finishComment(Comment
* c
) {
249 LOG4CXX_DEBUG(logger
, "Finishing comment " << c
->getAddress());
250 ChangeCommentEvent
event(c
->getAddress(), c
->getLocation(), c
);
254 void InformationManager::deleteFunction(Function
* f
) {
255 functions
.erase(f
->getStartAddress());
259 void InformationManager::deleteBasicBlock(BasicBlock
* b
) {
260 blocks
.erase(b
->getStartAddress());
264 void InformationManager::deleteComment(Comment
* c
) {
265 auto range
= comments
.equal_range(c
->getAddress());
266 for (auto it
= range
.first
; it
!= range
.second
; ++it
) {
267 if (it
->second
== c
) {