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/ChangeCommentEvent.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;
21 qRegisterMetaType
<NewFunctionEvent
>("NewFunctionEvent");
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() {
31 disassemblerThread
.quit();
35 for (auto f
: functions
)
38 for (auto i
: plugins
)
40 disassemblerThread
.wait();
43 void InformationManager::reset(const std::string
& filename
) {
44 this->filename
= filename
;
45 disassembler
.reset(createLLVMDisassembler(filename
, this));
46 if (disassembler
.get() != NULL
) {
48 disassembler
.get()->moveToThread(&disassemblerThread
);
49 connect(&disassemblerThread
, &QThread::started
,
50 disassembler
.get(), &Disassembler::start
);
51 disassemblerThread
.start();
55 void InformationManager::load(const std::string
& filename
) {
56 QuaZip
zip(filename
.c_str());
57 QuaZipFile
file(&zip
);
60 if (!zip
.open(QuaZip::mdUnzip
)) {
61 LOG4CXX_ERROR(logger
, "Failed to open archive " << filename
);
64 tmpfile
.reset(new QTemporaryFile());
67 LOG4CXX_INFO(logger
, "Loading binary from archive");
68 zip
.setCurrentFile("binary");
70 file
.open(QIODevice::ReadOnly
);
72 while (!file
.atEnd()) {
73 buffer
= file
.read(4096);
74 tmpfile
->write(buffer
);
78 disassembler
.reset(createLLVMDisassembler(tmpfile
->fileName().toStdString(), this));
81 for (bool more
= zip
.goToFirstFile(); more
; more
= zip
.goToNextFile()) {
82 zip
.getCurrentFileInfo(&info
);
83 file
.open(QIODevice::ReadOnly
);
85 if(info
.name
!= "binary") {
86 if (info
.name
.startsWith("comment:")) {
87 QXmlStreamReader
reader(&file
);
88 auto starttoken
= reader
.readNext();
89 auto elementtoken
= reader
.readNext();
90 assert(QXmlStreamReader::StartDocument
== starttoken
);
91 assert(QXmlStreamReader::StartElement
== elementtoken
);
92 Comment::deserialize(reader
, this);
94 QXmlStreamReader
reader(&file
);
95 auto starttoken
= reader
.readNext();
96 auto elementtoken
= reader
.readNext();
97 assert(QXmlStreamReader::StartDocument
== starttoken
);
98 assert(QXmlStreamReader::StartElement
== elementtoken
);
99 Function::deserialize(reader
, this);
106 void InformationManager::save(const std::string
& filename
) {
107 QuaZip
zip(filename
.c_str());
108 zip
.open(QuaZip::mdCreate
);
109 zip
.setComment("FRIDA 0.0");
110 QuaZipFile
outZipFile(&zip
);
113 QFile
binary(this->filename
.c_str());
114 binary
.open(QIODevice::ReadOnly
);
115 QuaZipNewInfo
zipinfo("binary");
116 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
117 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
119 while (!binary
.atEnd()) {
120 buffer
= binary
.read(4096);
121 outZipFile
.write(buffer
);
126 for (auto funpair
: functions
) {
127 Function
* fun
= funpair
.second
;
128 QuaZipNewInfo
zipinfo(fun
->getName().c_str());
129 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
130 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
131 QXmlStreamWriter
stream(&outZipFile
);
132 stream
.setAutoFormatting(true);
133 stream
.setAutoFormattingIndent(-1);
134 stream
.writeStartDocument();
136 fun
->serialize(stream
);
138 stream
.writeEndDocument();
141 for (auto commentpair
: comments
) {
142 Comment
* comment
= commentpair
.second
;
143 if (!comment
->isLocal()) {
144 QuaZipNewInfo
zipinfo(QString("comment:%1").arg(comment
->getAddress(), 0, 16));
145 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
146 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
147 QXmlStreamWriter
stream(&outZipFile
);
148 stream
.setAutoFormatting(true);
149 stream
.setAutoFormattingIndent(-1);
150 stream
.writeStartDocument();
152 comment
->serialize(stream
);
154 stream
.writeEndDocument();
163 /* *******************************
164 * Accessors for the Functions map
167 Function
* InformationManager::getFunction(uint64_t address
) {
168 auto it
= functions
.find(address
);
169 if (it
!= functions
.end())
175 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::beginFunctions() {
176 return functions
.begin();
178 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::endFunctions() {
179 return functions
.end();
183 /* *********************************
184 * Accessors for the BasicBlocks map
187 BasicBlock
* InformationManager::getBasicBlock(uint64_t address
) {
188 auto it
= blocks
.find(address
);
189 if (it
!= blocks
.end())
195 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::beginBasicBlocks() {
196 return blocks
.begin();
198 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::endBasicBlocks() {
202 /* *********************************
203 * Accessors for the Comments map
206 std::multimap
<uint64_t, Comment
*>::const_iterator
,
207 std::multimap
<uint64_t, Comment
*>::const_iterator
>
208 InformationManager::getComments(uint64_t address
) {
209 return comments
.equal_range(address
);
212 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::beginComments() {
213 return comments
.begin();
215 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::endComments() {
216 return comments
.end();
219 /* *********************************
220 * Accessors for the Interpreter map
223 Interpreter
* InformationManager::getInterpreter(const std::string
& name
) {
224 auto it
= interpreters
.find(name
);
225 if (it
!= interpreters
.end())
231 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::beginInterpreters() {
232 return interpreters
.begin();
234 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::endInterpreters() {
235 return interpreters
.end();
239 /* ********************************
240 * Factory methods for data classes
243 Function
* InformationManager::newFunction(uint64_t address
) {
244 Function
* fun
= new Function(address
, false, this);
245 functions
.insert(std::make_pair(address
, fun
));
249 Function
* InformationManager::newDynamicFunction(uint64_t address
) {
250 Function
* fun
= new Function(address
, true, this);
251 functions
.insert(std::make_pair(address
, fun
));
255 BasicBlock
* InformationManager::newBasicBlock(uint64_t address
) {
256 BasicBlock
* block
= new BasicBlock(address
, this);
257 blocks
.insert(std::make_pair(address
, block
));
261 Comment
* InformationManager::newGlobalComment(uint64_t address
) {
262 Comment
* comment
= new Comment(address
, this);
263 comments
.insert(std::make_pair(address
, comment
));
267 Comment
* InformationManager::newLocalComment(uint64_t address
, Function
* f
) {
268 Comment
* comment
= new Comment(address
, f
, this);
269 comments
.insert(std::make_pair(address
, comment
));
273 void InformationManager::finishFunction(Function
* fun
) {
274 LOG4CXX_DEBUG(logger
, "Finishing function " << fun
->getName());
275 for (auto b
: fun
->blocks()) {
276 BasicBlock
* bl
= b
.second
;
277 blocks
.insert(std::make_pair(bl
->getStartAddress(), bl
));
279 NewFunctionEvent
event(fun
->getStartAddress(), fun
);
280 emit
newFunctionEvent(event
);
283 void InformationManager::finishBasicBlock(BasicBlock
*) {
286 void InformationManager::finishComment(Comment
* c
) {
287 LOG4CXX_DEBUG(logger
, "Finishing comment " << c
->getAddress());
288 ChangeCommentEvent
event(c
->getAddress(), c
->getLocation(), c
);
289 emit
changeCommentEvent(&event
);
292 void InformationManager::deleteFunction(Function
* f
) {
293 functions
.erase(f
->getStartAddress());
297 void InformationManager::deleteBasicBlock(BasicBlock
* b
) {
298 blocks
.erase(b
->getStartAddress());
302 void InformationManager::deleteComment(Comment
* c
) {
303 auto range
= comments
.equal_range(c
->getAddress());
304 for (auto it
= range
.first
; it
!= range
.second
; ++it
) {
305 if (it
->second
== c
) {