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"
10 #include <quazip/quazip.h>
11 #include <quazip/quazipfile.h>
13 #include <QTemporaryFile>
15 InformationManager
* current_information_manager
;
17 InformationManager::InformationManager()
18 : logger(log4cxx::Logger::getLogger("core.InformationManager"))
20 current_information_manager
= this;
22 QPluginLoader
* loader
= new QPluginLoader("libguilePlugin", NULL
);
24 LOG4CXX_ERROR(logger
, "Loading plugin failed: " << loader
->errorString().toStdString());
25 interpreters
["GUILE"] = qobject_cast
<Interpreter
*>(loader
->instance());
26 plugins
.push_back(loader
);
29 InformationManager::~InformationManager() {
33 for (auto f
: functions
)
36 for (auto i
: plugins
)
40 void InformationManager::reset(const std::string
& filename
) {
41 this->filename
= filename
;
42 disassembler
.reset(createLLVMDisassembler(filename
, this));
43 if (disassembler
.get() != NULL
)
44 disassembler
.get()->start();
47 void InformationManager::load(const std::string
& filename
) {
48 QuaZip
zip(filename
.c_str());
49 QuaZipFile
file(&zip
);
52 zip
.open(QuaZip::mdUnzip
);
53 tmpfile
.reset(new QTemporaryFile());
56 LOG4CXX_INFO(logger
, "Loading binary from archive");
57 zip
.setCurrentFile("binary");
59 file
.open(QIODevice::ReadOnly
);
61 while (!file
.atEnd()) {
62 buffer
= file
.read(4096);
63 tmpfile
->write(buffer
);
67 disassembler
.reset(createLLVMDisassembler(tmpfile
->fileName().toStdString(), this));
70 for (bool more
= zip
.goToFirstFile(); more
; more
= zip
.goToNextFile()) {
71 zip
.getCurrentFileInfo(&info
);
72 file
.open(QIODevice::ReadOnly
);
74 if(info
.name
!= "binary") {
75 QXmlStreamReader
reader(&file
);
76 assert(QXmlStreamReader::StartDocument
== reader
.readNext());
77 assert(QXmlStreamReader::StartElement
== reader
.readNext());
78 Function
* fun
= Function::deserialize(reader
, this);
84 void InformationManager::save(const std::string
& filename
) {
85 QuaZip
zip(filename
.c_str());
86 zip
.open(QuaZip::mdCreate
);
87 zip
.setComment("FRIDA 0.0");
88 QuaZipFile
outZipFile(&zip
);
91 QFile
binary(this->filename
.c_str());
92 binary
.open(QIODevice::ReadOnly
);
93 QuaZipNewInfo
zipinfo("binary");
94 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
95 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
97 while (!binary
.atEnd()) {
98 buffer
= binary
.read(4096);
99 outZipFile
.write(buffer
);
104 for (auto funpair
: functions
) {
105 Function
* fun
= funpair
.second
;
106 QuaZipNewInfo
zipinfo(fun
->getName().c_str());
107 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
108 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
109 QXmlStreamWriter
stream(&outZipFile
);
110 stream
.setAutoFormatting(true);
111 stream
.setAutoFormattingIndent(-1);
112 stream
.writeStartDocument();
114 fun
->serialize(stream
);
116 stream
.writeEndDocument();
124 /* *******************************
125 * Accessors for the Functions map
128 Function
* InformationManager::getFunction(uint64_t address
) {
129 auto it
= functions
.find(address
);
130 if (it
!= functions
.end())
136 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::beginFunctions() {
137 return functions
.begin();
139 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::endFunctions() {
140 return functions
.end();
144 /* *********************************
145 * Accessors for the BasicBlocks map
148 BasicBlock
* InformationManager::getBasicBlock(uint64_t address
) {
149 auto it
= blocks
.find(address
);
150 if (it
!= blocks
.end())
156 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::beginBasicBlocks() {
157 return blocks
.begin();
159 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::endBasicBlocks() {
164 /* *********************************
165 * Accessors for the Interpreter map
168 Interpreter
* InformationManager::getInterpreter(const std::string
& name
) {
169 auto it
= interpreters
.find(name
);
170 if (it
!= interpreters
.end())
176 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::beginInterpreters() {
177 return interpreters
.begin();
179 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::endInterpreters() {
180 return interpreters
.end();
184 /* ********************************
185 * Factory methods for data classes
188 Function
* InformationManager::newFunction(uint64_t address
) {
189 Function
* fun
= new Function(address
, false, this);
190 functions
.insert(std::make_pair(address
, fun
));
194 Function
* InformationManager::newDynamicFunction(uint64_t address
) {
195 Function
* fun
= new Function(address
, true, this);
196 functions
.insert(std::make_pair(address
, fun
));
200 BasicBlock
* InformationManager::newBasicBlock(uint64_t address
) {
201 BasicBlock
* block
= new BasicBlock(address
, this);
202 blocks
.insert(std::make_pair(address
, block
));
206 Comment
* InformationManager::newGlobalComment(uint64_t address
) {
207 Comment
* comment
= new Comment(address
, this);
208 comments
.insert(std::make_pair(address
, comment
));
212 Comment
* InformationManager::newLocalComment(uint64_t address
, Function
* f
) {
213 Comment
* comment
= new Comment(address
, this);
214 comments
.insert(std::make_pair(address
, comment
));
218 void InformationManager::finishFunction(Function
* fun
) {
219 LOG4CXX_DEBUG(logger
, "Finishing function " << fun
->getName());
220 for (auto b
: fun
->blocks()) {
221 BasicBlock
* bl
= b
.second
;
222 blocks
.insert(std::make_pair(bl
->getStartAddress(), bl
));
224 NewFunctionEvent
event(fun
->getStartAddress(), fun
);
228 void InformationManager::finishBasicBlock(BasicBlock
*) {
231 void InformationManager::finnishComment(Comment
* c
) {
234 void InformationManager::deleteFunction(Function
* f
) {
235 functions
.erase(f
->getStartAddress());
239 void InformationManager::deleteBasicBlock(BasicBlock
* b
) {
240 blocks
.erase(b
->getStartAddress());
244 void InformationManager::deleteComment(Comment
* c
) {
245 auto range
= comments
.equal_range(c
->getAddress());
246 for (auto it
= range
.first
; it
!= range
.second
; ++it
) {
247 if (it
->second
== c
) {