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 if (!zip
.open(QuaZip::mdUnzip
)) {
54 LOG4CXX_ERROR(logger
, "Failed to open archive " << filename
);
57 tmpfile
.reset(new QTemporaryFile());
60 LOG4CXX_INFO(logger
, "Loading binary from archive");
61 zip
.setCurrentFile("binary");
63 file
.open(QIODevice::ReadOnly
);
65 while (!file
.atEnd()) {
66 buffer
= file
.read(4096);
67 tmpfile
->write(buffer
);
71 disassembler
.reset(createLLVMDisassembler(tmpfile
->fileName().toStdString(), this));
74 for (bool more
= zip
.goToFirstFile(); more
; more
= zip
.goToNextFile()) {
75 zip
.getCurrentFileInfo(&info
);
76 file
.open(QIODevice::ReadOnly
);
78 if(info
.name
!= "binary") {
79 if (info
.name
.startsWith("comment:")) {
80 QXmlStreamReader
reader(&file
);
81 auto starttoken
= reader
.readNext();
82 auto elementtoken
= reader
.readNext();
83 assert(QXmlStreamReader::StartDocument
== starttoken
);
84 assert(QXmlStreamReader::StartElement
== elementtoken
);
85 Comment::deserialize(reader
, this);
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 Function::deserialize(reader
, this);
99 void InformationManager::save(const std::string
& filename
) {
100 QuaZip
zip(filename
.c_str());
101 zip
.open(QuaZip::mdCreate
);
102 zip
.setComment("FRIDA 0.0");
103 QuaZipFile
outZipFile(&zip
);
106 QFile
binary(this->filename
.c_str());
107 binary
.open(QIODevice::ReadOnly
);
108 QuaZipNewInfo
zipinfo("binary");
109 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
110 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
112 while (!binary
.atEnd()) {
113 buffer
= binary
.read(4096);
114 outZipFile
.write(buffer
);
119 for (auto funpair
: functions
) {
120 Function
* fun
= funpair
.second
;
121 QuaZipNewInfo
zipinfo(fun
->getName().c_str());
122 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
123 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
124 QXmlStreamWriter
stream(&outZipFile
);
125 stream
.setAutoFormatting(true);
126 stream
.setAutoFormattingIndent(-1);
127 stream
.writeStartDocument();
129 fun
->serialize(stream
);
131 stream
.writeEndDocument();
134 for (auto commentpair
: comments
) {
135 Comment
* comment
= commentpair
.second
;
136 if (!comment
->isLocal()) {
137 QuaZipNewInfo
zipinfo(QString("comment:%1").arg(comment
->getAddress(), 0, 16));
138 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
139 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
140 QXmlStreamWriter
stream(&outZipFile
);
141 stream
.setAutoFormatting(true);
142 stream
.setAutoFormattingIndent(-1);
143 stream
.writeStartDocument();
145 comment
->serialize(stream
);
147 stream
.writeEndDocument();
156 /* *******************************
157 * Accessors for the Functions map
160 Function
* InformationManager::getFunction(uint64_t address
) {
161 auto it
= functions
.find(address
);
162 if (it
!= functions
.end())
168 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::beginFunctions() {
169 return functions
.begin();
171 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::endFunctions() {
172 return functions
.end();
176 /* *********************************
177 * Accessors for the BasicBlocks map
180 BasicBlock
* InformationManager::getBasicBlock(uint64_t address
) {
181 auto it
= blocks
.find(address
);
182 if (it
!= blocks
.end())
188 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::beginBasicBlocks() {
189 return blocks
.begin();
191 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::endBasicBlocks() {
195 /* *********************************
196 * Accessors for the Comments map
199 std::multimap
<uint64_t, Comment
*>::const_iterator
,
200 std::multimap
<uint64_t, Comment
*>::const_iterator
>
201 InformationManager::getComments(uint64_t address
) {
202 return comments
.equal_range(address
);
205 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::beginComments() {
206 return comments
.begin();
208 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::endComments() {
209 return comments
.end();
212 /* *********************************
213 * Accessors for the Interpreter map
216 Interpreter
* InformationManager::getInterpreter(const std::string
& name
) {
217 auto it
= interpreters
.find(name
);
218 if (it
!= interpreters
.end())
224 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::beginInterpreters() {
225 return interpreters
.begin();
227 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::endInterpreters() {
228 return interpreters
.end();
232 /* ********************************
233 * Factory methods for data classes
236 Function
* InformationManager::newFunction(uint64_t address
) {
237 Function
* fun
= new Function(address
, false, this);
238 functions
.insert(std::make_pair(address
, fun
));
242 Function
* InformationManager::newDynamicFunction(uint64_t address
) {
243 Function
* fun
= new Function(address
, true, this);
244 functions
.insert(std::make_pair(address
, fun
));
248 BasicBlock
* InformationManager::newBasicBlock(uint64_t address
) {
249 BasicBlock
* block
= new BasicBlock(address
, this);
250 blocks
.insert(std::make_pair(address
, block
));
254 Comment
* InformationManager::newGlobalComment(uint64_t address
) {
255 Comment
* comment
= new Comment(address
, this);
256 comments
.insert(std::make_pair(address
, comment
));
260 Comment
* InformationManager::newLocalComment(uint64_t address
, Function
* f
) {
261 Comment
* comment
= new Comment(address
, f
, this);
262 comments
.insert(std::make_pair(address
, comment
));
266 void InformationManager::finishFunction(Function
* fun
) {
267 LOG4CXX_DEBUG(logger
, "Finishing function " << fun
->getName());
268 for (auto b
: fun
->blocks()) {
269 BasicBlock
* bl
= b
.second
;
270 blocks
.insert(std::make_pair(bl
->getStartAddress(), bl
));
272 NewFunctionEvent
event(fun
->getStartAddress(), fun
);
273 emit
newFunctionEvent(&event
);
276 void InformationManager::finishBasicBlock(BasicBlock
*) {
279 void InformationManager::finishComment(Comment
* c
) {
280 LOG4CXX_DEBUG(logger
, "Finishing comment " << c
->getAddress());
281 ChangeCommentEvent
event(c
->getAddress(), c
->getLocation(), c
);
282 emit
changeCommentEvent(&event
);
285 void InformationManager::deleteFunction(Function
* f
) {
286 functions
.erase(f
->getStartAddress());
290 void InformationManager::deleteBasicBlock(BasicBlock
* b
) {
291 blocks
.erase(b
->getStartAddress());
295 void InformationManager::deleteComment(Comment
* c
) {
296 auto range
= comments
.equal_range(c
->getAddress());
297 for (auto it
= range
.first
; it
!= range
.second
; ++it
) {
298 if (it
->second
== c
) {