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"
9 #include <quazip/quazip.h>
10 #include <quazip/quazipfile.h>
12 #include <QTemporaryFile>
14 InformationManager
* current_information_manager
;
16 InformationManager::InformationManager()
17 : logger(log4cxx::Logger::getLogger("core.InformationManager"))
19 current_information_manager
= this;
21 QPluginLoader
* loader
= new QPluginLoader("libguilePlugin", NULL
);
23 LOG4CXX_ERROR(logger
, "Loading plugin failed: " << loader
->errorString().toStdString());
24 interpreters
["GUILE"] = qobject_cast
<Interpreter
*>(loader
->instance());
25 plugins
.push_back(loader
);
28 InformationManager::~InformationManager() {
32 for (auto f
: functions
)
35 for (auto i
: plugins
)
39 void InformationManager::reset(const std::string
& filename
) {
40 this->filename
= filename
;
41 disassembler
.reset(createLLVMDisassembler(filename
, this));
42 if (disassembler
.get() != NULL
)
43 disassembler
.get()->start();
46 void InformationManager::load(const std::string
& filename
) {
47 QuaZip
zip(filename
.c_str());
48 QuaZipFile
file(&zip
);
51 zip
.open(QuaZip::mdUnzip
);
52 tmpfile
.reset(new QTemporaryFile());
55 LOG4CXX_INFO(logger
, "Loading binary from archive");
56 zip
.setCurrentFile("binary");
58 file
.open(QIODevice::ReadOnly
);
60 while (!file
.atEnd()) {
61 buffer
= file
.read(4096);
62 tmpfile
->write(buffer
);
66 disassembler
.reset(createLLVMDisassembler(tmpfile
->fileName().toStdString(), this));
69 for (bool more
= zip
.goToFirstFile(); more
; more
= zip
.goToNextFile()) {
70 zip
.getCurrentFileInfo(&info
);
71 file
.open(QIODevice::ReadOnly
);
73 if(info
.name
!= "binary") {
74 QXmlStreamReader
reader(&file
);
75 assert(QXmlStreamReader::StartDocument
== reader
.readNext());
76 assert(QXmlStreamReader::StartElement
== reader
.readNext());
77 Function
* fun
= Function::deserialize(reader
, this);
83 void InformationManager::save(const std::string
& filename
) {
84 QuaZip
zip(filename
.c_str());
85 zip
.open(QuaZip::mdCreate
);
86 zip
.setComment("FRIDA 0.0");
87 QuaZipFile
outZipFile(&zip
);
90 QFile
binary(this->filename
.c_str());
91 binary
.open(QIODevice::ReadOnly
);
92 QuaZipNewInfo
zipinfo("binary");
93 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
94 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
96 while (!binary
.atEnd()) {
97 buffer
= binary
.read(4096);
98 outZipFile
.write(buffer
);
103 for (auto funpair
: functions
) {
104 Function
* fun
= funpair
.second
;
105 QuaZipNewInfo
zipinfo(fun
->getName().c_str());
106 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
107 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
108 QXmlStreamWriter
stream(&outZipFile
);
109 stream
.setAutoFormatting(true);
110 stream
.setAutoFormattingIndent(-1);
111 stream
.writeStartDocument();
113 fun
->serialize(stream
);
115 stream
.writeEndDocument();
122 void InformationManager::signal_new_function(Function
* fun
) {
127 /* *******************************
128 * Accessors for the Functions map
131 Function
* InformationManager::getFunction(uint64_t address
) {
132 auto it
= functions
.find(address
);
133 if (it
!= functions
.end())
139 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::beginFunctions() {
140 return functions
.begin();
142 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::endFunctions() {
143 return functions
.end();
147 /* *********************************
148 * Accessors for the BasicBlocks map
151 BasicBlock
* InformationManager::getBasicBlock(uint64_t address
) {
152 auto it
= blocks
.find(address
);
153 if (it
!= blocks
.end())
159 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::beginBasicBlocks() {
160 return blocks
.begin();
162 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::endBasicBlocks() {
167 /* *********************************
168 * Accessors for the Interpreter map
171 Interpreter
* InformationManager::getInterpreter(const std::string
& name
) {
172 auto it
= interpreters
.find(name
);
173 if (it
!= interpreters
.end())
179 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::beginInterpreters() {
180 return interpreters
.begin();
182 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::endInterpreters() {
183 return interpreters
.end();
187 /* ********************************
188 * Factory methods for data classes
191 Function
* InformationManager::newFunction(uint64_t address
) {
192 Function
* fun
= new Function(address
, this);
193 functions
.insert(std::make_pair(address
, fun
));
197 BasicBlock
* InformationManager::newBasicBlock(uint64_t address
) {
198 BasicBlock
* block
= new BasicBlock(address
, this);
199 blocks
.insert(std::make_pair(address
, block
));
203 Comment
* InformationManager::newGlobalComment(uint64_t address
) {
207 Comment
* InformationManager::newLocalComment(uint64_t address
, Function
* f
) {
211 void InformationManager::finishFunction(Function
* fun
) {
212 LOG4CXX_DEBUG(logger
, "Finishing function " << fun
->getName());
213 for (auto b
: fun
->blocks()) {
214 BasicBlock
* bl
= b
.second
;
215 blocks
.insert(std::make_pair(bl
->getStartAddress(), bl
));
217 new_function_signal(fun
);
220 void InformationManager::finishBasicBlock(BasicBlock
* b
) {
223 void InformationManager::finnishComment(Comment
* c
) {
226 void InformationManager::deleteFunction(Function
* f
) {
227 functions
.erase(f
->getStartAddress());
231 void InformationManager::deleteBasicBlock(BasicBlock
* b
) {
232 blocks
.erase(b
->getStartAddress());
236 void InformationManager::deleteComment(Comment
* c
) {