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;
22 qRegisterMetaType
<NewFunctionEvent
>("NewFunctionEvent");
24 QPluginLoader
* loader
= new QPluginLoader("libguilePlugin", NULL
);
26 LOG4CXX_ERROR(logger
, "Loading plugin failed: " << loader
->errorString().toStdString());
27 interpreters
["GUILE"] = qobject_cast
<Interpreter
*>(loader
->instance());
28 plugins
.push_back(loader
);
31 InformationManager::~InformationManager() {
35 for (auto f
: functions
)
38 for (auto i
: plugins
)
42 void InformationManager::reset(const std::string
& filename
) {
43 this->filename
= filename
;
44 disassembler
.reset(createLLVMDisassembler(filename
, this));
46 if (disassembler
.get() != NULL
)
47 disassembler
.get()->start();
50 void InformationManager::load(const std::string
& filename
) {
51 QuaZip
zip(filename
.c_str());
52 QuaZipFile
file(&zip
);
55 if (!zip
.open(QuaZip::mdUnzip
)) {
56 LOG4CXX_ERROR(logger
, "Failed to open archive " << filename
);
59 tmpfile
.reset(new QTemporaryFile());
62 LOG4CXX_INFO(logger
, "Loading binary from archive");
63 zip
.setCurrentFile("binary");
65 file
.open(QIODevice::ReadOnly
);
67 while (!file
.atEnd()) {
68 buffer
= file
.read(4096);
69 tmpfile
->write(buffer
);
73 disassembler
.reset(createLLVMDisassembler(tmpfile
->fileName().toStdString(), this));
76 for (bool more
= zip
.goToFirstFile(); more
; more
= zip
.goToNextFile()) {
77 zip
.getCurrentFileInfo(&info
);
78 file
.open(QIODevice::ReadOnly
);
80 if(info
.name
!= "binary") {
81 if (info
.name
.startsWith("comment:")) {
82 QXmlStreamReader
reader(&file
);
83 auto starttoken
= reader
.readNext();
84 auto elementtoken
= reader
.readNext();
85 assert(QXmlStreamReader::StartDocument
== starttoken
);
86 assert(QXmlStreamReader::StartElement
== elementtoken
);
87 Comment::deserialize(reader
, this);
89 QXmlStreamReader
reader(&file
);
90 auto starttoken
= reader
.readNext();
91 auto elementtoken
= reader
.readNext();
92 assert(QXmlStreamReader::StartDocument
== starttoken
);
93 assert(QXmlStreamReader::StartElement
== elementtoken
);
94 Function::deserialize(reader
, this);
101 void InformationManager::save(const std::string
& filename
) {
102 QuaZip
zip(filename
.c_str());
103 zip
.open(QuaZip::mdCreate
);
104 zip
.setComment("FRIDA 0.0");
105 QuaZipFile
outZipFile(&zip
);
108 QFile
binary(this->filename
.c_str());
109 binary
.open(QIODevice::ReadOnly
);
110 QuaZipNewInfo
zipinfo("binary");
111 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
112 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
114 while (!binary
.atEnd()) {
115 buffer
= binary
.read(4096);
116 outZipFile
.write(buffer
);
121 for (auto funpair
: functions
) {
122 Function
* fun
= funpair
.second
;
123 QuaZipNewInfo
zipinfo(fun
->getName().c_str());
124 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
125 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
126 QXmlStreamWriter
stream(&outZipFile
);
127 stream
.setAutoFormatting(true);
128 stream
.setAutoFormattingIndent(-1);
129 stream
.writeStartDocument();
131 fun
->serialize(stream
);
133 stream
.writeEndDocument();
136 for (auto commentpair
: comments
) {
137 Comment
* comment
= commentpair
.second
;
138 if (!comment
->isLocal()) {
139 QuaZipNewInfo
zipinfo(QString("comment:%1").arg(comment
->getAddress(), 0, 16));
140 zipinfo
.setPermissions(static_cast<QFile::Permissions
>(0x6444));
141 outZipFile
.open(QIODevice::WriteOnly
, zipinfo
);
142 QXmlStreamWriter
stream(&outZipFile
);
143 stream
.setAutoFormatting(true);
144 stream
.setAutoFormattingIndent(-1);
145 stream
.writeStartDocument();
147 comment
->serialize(stream
);
149 stream
.writeEndDocument();
158 /* *******************************
159 * Accessors for the Functions map
162 Function
* InformationManager::getFunction(uint64_t address
) {
163 auto it
= functions
.find(address
);
164 if (it
!= functions
.end())
170 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::beginFunctions() {
171 return functions
.begin();
173 std::map
<uint64_t, Function
*>::const_iterator
InformationManager::endFunctions() {
174 return functions
.end();
178 /* *********************************
179 * Accessors for the BasicBlocks map
182 BasicBlock
* InformationManager::getBasicBlock(uint64_t address
) {
183 auto it
= blocks
.find(address
);
184 if (it
!= blocks
.end())
190 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::beginBasicBlocks() {
191 return blocks
.begin();
193 std::map
<uint64_t, BasicBlock
*>::const_iterator
InformationManager::endBasicBlocks() {
197 /* *********************************
198 * Accessors for the Comments map
201 std::multimap
<uint64_t, Comment
*>::const_iterator
,
202 std::multimap
<uint64_t, Comment
*>::const_iterator
>
203 InformationManager::getComments(uint64_t address
) {
204 return comments
.equal_range(address
);
207 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::beginComments() {
208 return comments
.begin();
210 std::multimap
<uint64_t, Comment
*>::const_iterator
InformationManager::endComments() {
211 return comments
.end();
214 /* *********************************
215 * Accessors for the Interpreter map
218 Interpreter
* InformationManager::getInterpreter(const std::string
& name
) {
219 auto it
= interpreters
.find(name
);
220 if (it
!= interpreters
.end())
226 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::beginInterpreters() {
227 return interpreters
.begin();
229 std::map
<std::string
, Interpreter
*>::const_iterator
InformationManager::endInterpreters() {
230 return interpreters
.end();
234 /* ********************************
235 * Factory methods for data classes
238 Function
* InformationManager::newFunction(uint64_t address
) {
239 Function
* fun
= new Function(address
, false, this);
240 functions
.insert(std::make_pair(address
, fun
));
244 Function
* InformationManager::newDynamicFunction(uint64_t address
) {
245 Function
* fun
= new Function(address
, true, this);
246 functions
.insert(std::make_pair(address
, fun
));
250 BasicBlock
* InformationManager::newBasicBlock(uint64_t address
) {
251 BasicBlock
* block
= new BasicBlock(address
, this);
252 blocks
.insert(std::make_pair(address
, block
));
256 Comment
* InformationManager::newGlobalComment(uint64_t address
) {
257 Comment
* comment
= new Comment(address
, this);
258 comments
.insert(std::make_pair(address
, comment
));
262 Comment
* InformationManager::newLocalComment(uint64_t address
, Function
* f
) {
263 Comment
* comment
= new Comment(address
, f
, this);
264 comments
.insert(std::make_pair(address
, comment
));
268 void InformationManager::finishFunction(Function
* fun
) {
269 LOG4CXX_DEBUG(logger
, "Finishing function " << fun
->getName());
270 for (auto b
: fun
->blocks()) {
271 BasicBlock
* bl
= b
.second
;
272 blocks
.insert(std::make_pair(bl
->getStartAddress(), bl
));
274 NewFunctionEvent
event(fun
->getStartAddress(), fun
);
275 emit
newFunctionEvent(&event
);
278 void InformationManager::finishBasicBlock(BasicBlock
*) {
281 void InformationManager::finishComment(Comment
* c
) {
282 LOG4CXX_DEBUG(logger
, "Finishing comment " << c
->getAddress());
283 ChangeCommentEvent
event(c
->getAddress(), c
->getLocation(), c
);
284 emit
changeCommentEvent(&event
);
287 void InformationManager::deleteFunction(Function
* f
) {
288 functions
.erase(f
->getStartAddress());
292 void InformationManager::deleteBasicBlock(BasicBlock
* b
) {
293 blocks
.erase(b
->getStartAddress());
297 void InformationManager::deleteComment(Comment
* c
) {
298 auto range
= comments
.equal_range(c
->getAddress());
299 for (auto it
= range
.first
; it
!= range
.second
; ++it
) {
300 if (it
->second
== c
) {