Reoganize Function/BasicBlock creation
[frida/frida.git] / src / core / InformationManager.cxx
1 #include "InformationManager.hxx"
2 #include "disassembler/llvm/LLVMDisassembler.hxx"
3 #include "core/Function.hxx"
4 #include "core/BasicBlock.hxx"
5 #include "core/Comment.hxx"
6
7 #include "gui/qt.hxx"
8 #include <quazip/quazip.h>
9 #include <quazip/quazipfile.h>
10
11 InformationManager::~InformationManager() {
12 for (auto b : blocks)
13 delete b.second;
14
15 for (auto f : functions)
16 delete f.second;
17 }
18
19 void InformationManager::reset(const std::string& filename) {
20 disassembler.reset(createLLVMDisassembler(filename, this));
21 if (disassembler.get() != NULL)
22 disassembler.get()->start();
23 }
24
25 void InformationManager::save(const QString& filename) {
26 QuaZip zip(filename);
27 zip.open(QuaZip::mdCreate);
28 zip.setComment("FRIDA 0.0");
29 QuaZipFile outZipFile(&zip);
30
31 for (auto funpair : functions) {
32 Function* fun = funpair.second;
33 QuaZipNewInfo zipinfo(fun->getName().c_str());
34 zipinfo.setPermissions(static_cast<QFile::Permissions>(0x6444));
35 outZipFile.open(QIODevice::WriteOnly, zipinfo);
36 QXmlStreamWriter stream(&outZipFile);
37 stream.setAutoFormatting(true);
38 stream.setAutoFormattingIndent(-1);
39 stream.writeStartDocument();
40 stream.writeStartElement("function");
41 stream.writeAttribute("name", fun->getName().c_str());
42 stream.writeAttribute("entry", QString::number(fun->getStartAddress(), 16));
43
44 for (auto& blockentry : fun->blocks()) {
45 stream.writeStartElement("block");
46 stream.writeAttribute("id", blockentry.second->getName().c_str());
47 stream.writeTextElement("start", QString::number(blockentry.second->getStartAddress(), 16));
48 stream.writeTextElement("end", QString::number(blockentry.second->getEndAddress(), 16));
49 if (0 != blockentry.second->getNextBlock(0))
50 stream.writeTextElement("next", QString::number(blockentry.second->getNextBlock(0), 16));
51 if (0 != blockentry.second->getNextBlock(1))
52 stream.writeTextElement("next", QString::number(blockentry.second->getNextBlock(1), 16));
53 stream.writeEndElement(); // "block"
54 }
55
56 stream.writeEndElement(); // "function"
57 stream.writeEndDocument();
58 outZipFile.close();
59 }
60
61 zip.close();
62 }
63
64 void InformationManager::signal_new_function(Function* fun) {
65 }
66
67 Function* InformationManager::getFunction(uint64_t address) {
68 auto it = functions.find(address);
69 if (it != functions.end())
70 return it->second;
71 else
72 return NULL;
73 }
74
75 BasicBlock* InformationManager::getBasicBlock(uint64_t address) {
76 auto it = blocks.find(address);
77 if (it != blocks.end())
78 return it->second;
79 else
80 return NULL;
81 }
82
83 Function* InformationManager::newFunction(uint64_t address) {
84 Function* fun = new Function(address, this);
85 functions.insert(std::make_pair(address, fun));
86 return fun;
87 }
88
89 BasicBlock* InformationManager::newBasicBlock(uint64_t address) {
90 BasicBlock* block = new BasicBlock(address, this);
91 blocks.insert(std::make_pair(address, block));
92 return block;
93 }
94
95 Comment* InformationManager::newGlobalComment(uint64_t address) {
96 return NULL;
97 }
98
99 Comment* InformationManager::newLocalComment(uint64_t address, Function* f) {
100 return NULL;
101 }
102
103 void InformationManager::finishFunction(Function* fun) {
104 for (auto b : fun->blocks()) {
105 BasicBlock* bl = b.second;
106 blocks.insert(std::make_pair(bl->getStartAddress(), bl));
107 }
108 new_function_signal(fun);
109 }
110
111 void InformationManager::finishBasicBlock(BasicBlock* b) {
112 }
113
114 void InformationManager::finnishComment(Comment* c) {
115 }
116
117 void InformationManager::deleteFunction(Function* f) {
118 functions.erase(f->getStartAddress());
119 delete f;
120 }
121
122 void InformationManager::deleteBasicBlock(BasicBlock* b) {
123 blocks.erase(b->getStartAddress());
124 delete b;
125 }
126
127 void InformationManager::deleteComment(Comment* c) {
128 delete c;
129 }