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
) {
49 disassembler
.get()->moveToThread(&disassemblerThread
);
50 connect(&disassemblerThread
, &QThread::started
,
51 disassembler
.get(), &Disassembler::start
);
52 disassemblerThread
.start();
54 disassembler
.get()->start();
59 void InformationManager::load(const std::string
& filename
) {
60 QuaZip
zip(filename
.c_str());
61 QuaZipFile
file(&zip
);
64 if (!zip
.open(QuaZip::mdUnzip
)) {
65 LOG4CXX_ERROR(logger
, "Failed to open archive " << filename
);
68 tmpfile
.reset(new QTemporaryFile());
71 LOG4CXX_INFO(logger
, "Loading binary from archive");
72 zip
.setCurrentFile("binary");
74 file
.open(QIODevice::ReadOnly
);
76 while (!file
.atEnd()) {
77 buffer
= file
.read(4096);
78 tmpfile
->write(buffer
);
82 disassembler
.reset(createLLVMDisassembler(tmpfile
->fileName().toStdString(), this));
85 for (bool more
= zip
.goToFirstFile(); more
; more
= zip
.goToNextFile()) {
86 zip
.getCurrentFileInfo(&info
);
87 file
.open(QIODevice::ReadOnly
);
89 if(info
.name
!= "binary") {
90 if (info
.name
.startsWith("comment:")) {
91 QXmlStreamReader
reader(&file
);
92 auto starttoken
= reader
.readNext();
93 auto elementtoken
= reader
.readNext();
94 assert(QXmlStreamReader::StartDocument
== starttoken
);
95 assert(QXmlStreamReader::StartElement
== elementtoken
);
96 Comment::deserialize(reader
, this);
98 QXmlStreamReader
reader(&file
);
99 auto starttoken
= reader
.readNext();
100 auto elementtoken
= reader
.readNext();
101 assert(QXmlStreamReader::StartDocument
== starttoken
);
102 assert(QXmlStreamReader::StartElement
== elementtoken
);
103 Function::deserialize(reader
, this);
110 void InformationManager::save(const std::string
& filename
) {
111 QuaZip
zip(filename
.c_str());
112 zip
.open(QuaZip::mdCreate
);
113 zip
.setComment("FRIDA 0.0");
114 QuaZipFile
outZipFile(&zip
);
117 QFile
binary(this->filename
.c_str());
118 binary
.open(QIODevice::ReadOnly
);
119 QuaZipNewInfo
zipinfo("binary");
120 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
121 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
123 while (!binary
.atEnd()) {
124 buffer
= binary
.read(4096);
125 outZipFile
.write(buffer
);
130 for (auto funpair
: functions
) {
131 Function
* fun
= funpair
.second
;
132 QuaZipNewInfo
zipinfo(fun
->getName().c_str());
133 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
134 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
135 QXmlStreamWriter
stream(&outZipFile
);
136 stream
.setAutoFormatting(true);
137 stream
.setAutoFormattingIndent(-1);
138 stream
.writeStartDocument();
140 fun
->serialize(stream
);
142 stream
.writeEndDocument();
145 for (auto commentpair
: comments
) {
146 Comment
* comment
= commentpair
.second
;
147 if (!comment
->isLocal()) {
148 QuaZipNewInfo
zipinfo(QString("comment:%1").arg(comment
->getAddress(), 0, 16));
149 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
150 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
151 QXmlStreamWriter
stream(&outZipFile
);
152 stream
.setAutoFormatting(true);
153 stream
.setAutoFormattingIndent(-1);
154 stream
.writeStartDocument();
156 comment
->serialize(stream
);
158 stream
.writeEndDocument();
167 /* *******************************
168 * Accessors for the Functions map
171 Function
* InformationManager::getFunction(uint64_t address
) {
172 auto it
= functions
.find(address
);
173 if (it
!= functions
.end())
179 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::beginFunctions() {
180 return functions
.begin();
182 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::endFunctions() {
183 return functions
.end();
187 /* *********************************
188 * Accessors for the BasicBlocks map
191 BasicBlock
* InformationManager::getBasicBlock(uint64_t address
) {
192 auto it
= blocks
.find(address
);
193 if (it
!= blocks
.end())
199 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::beginBasicBlocks() {
200 return blocks
.begin();
202 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::endBasicBlocks() {
206 /* *********************************
207 * Accessors for the Comments map
210 std::multimap
<uint64_t, Comment
*>::const_iterator
,
211 std::multimap
<uint64_t, Comment
*>::const_iterator
>
212 InformationManager::getComments(uint64_t address
) {
213 return comments
.equal_range(address
);
216 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::beginComments() {
217 return comments
.begin();
219 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::endComments() {
220 return comments
.end();
223 /* *********************************
224 * Accessors for the Interpreter map
227 Interpreter
* InformationManager::getInterpreter(const std::string
& name
) {
228 auto it
= interpreters
.find(name
);
229 if (it
!= interpreters
.end())
235 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::beginInterpreters() {
236 return interpreters
.begin();
238 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::endInterpreters() {
239 return interpreters
.end();
243 /* ********************************
244 * Factory methods for data classes
247 Function
* InformationManager::newFunction(uint64_t address
) {
248 Function
* fun
= new Function(address
, false, this);
249 functions
.insert(std::make_pair(address
, fun
));
253 Function
* InformationManager::newDynamicFunction(uint64_t address
) {
254 Function
* fun
= new Function(address
, true, this);
255 functions
.insert(std::make_pair(address
, fun
));
259 BasicBlock
* InformationManager::newBasicBlock(uint64_t address
) {
260 BasicBlock
* block
= new BasicBlock(address
, this);
261 blocks
.insert(std::make_pair(address
, block
));
265 Comment
* InformationManager::newGlobalComment(uint64_t address
) {
266 Comment
* comment
= new Comment(address
, this);
267 comments
.insert(std::make_pair(address
, comment
));
271 Comment
* InformationManager::newLocalComment(uint64_t address
, Function
* f
) {
272 Comment
* comment
= new Comment(address
, f
, this);
273 comments
.insert(std::make_pair(address
, comment
));
277 void InformationManager::finishFunction(Function
* fun
) {
278 LOG4CXX_DEBUG(logger
, "Finishing function " << fun
->getName());
279 for (auto b
: fun
->blocks()) {
280 BasicBlock
* bl
= b
.second
;
281 blocks
.insert(std::make_pair(bl
->getStartAddress(), bl
));
283 NewFunctionEvent
event(fun
->getStartAddress(), fun
);
284 emit
newFunctionEvent(event
);
287 void InformationManager::finishBasicBlock(BasicBlock
*) {
290 void InformationManager::finishComment(Comment
* c
) {
291 LOG4CXX_DEBUG(logger
, "Finishing comment " << c
->getAddress());
292 ChangeCommentEvent
event(c
->getAddress(), c
->getLocation(), c
);
293 emit
changeCommentEvent(&event
);
296 void InformationManager::deleteFunction(Function
* f
) {
297 functions
.erase(f
->getStartAddress());
301 void InformationManager::deleteBasicBlock(BasicBlock
* b
) {
302 blocks
.erase(b
->getStartAddress());
306 void InformationManager::deleteComment(Comment
* c
) {
307 auto range
= comments
.equal_range(c
->getAddress());
308 for (auto it
= range
.first
; it
!= range
.second
; ++it
) {
309 if (it
->second
== c
) {