Reoganize Function/BasicBlock creation
[frida/frida.git] / src / gui / Mainwindow.cxx
1 #include "Mainwindow.hxx"
2 #include "qt.hxx"
3 #include "disassembler/llvm/LLVMDisassembler.hxx"
4 #include "core/Function.hxx"
5 #include "core/BasicBlock.hxx"
6 #include "core/InformationManager.hxx"
7 #include "widgets/ScriptingDock.hxx"
8 #include "widgets/CFGScene.hxx"
9 #include "widgets/FunctionWidget.hxx"
10 #include "dialogs/NewFunctionDialog.hxx"
11 #include "dialogs/SimpleStringDialog.hxx"
12
13 #include <sstream>
14
15 namespace {
16 BasicBlockWidget *
17 local__add_basic_block(BasicBlock * block,
18 Mainwindow * mainwindow, InformationManager * manager,
19 std::map<uint64_t, BasicBlockWidget*>& known_blocks,
20 CFGScene * scene, uint64_t starty, uint64_t startx);
21 }
22
23 Mainwindow::Mainwindow(InformationManager* mgr)
24 : manager(mgr)
25 , logger(log4cxx::Logger::getLogger("Mainwindow")) {
26 openAction = new QAction(tr("&Open"), this);
27 saveAction = new QAction(tr("&Save"), this);
28 exitAction = new QAction(tr("E&xit"), this);
29
30 connect(openAction, SIGNAL(triggered()),
31 this, SLOT(open()));
32 connect(saveAction, SIGNAL(triggered()),
33 this, SLOT(save()));
34 connect(exitAction, SIGNAL(triggered()),
35 qApp, SLOT(quit()));
36
37 fileMenu = menuBar()->addMenu(tr("&File"));
38 fileMenu->addAction(openAction);
39 fileMenu->addAction(saveAction);
40 fileMenu->addSeparator();
41 fileMenu->addAction(exitAction);
42
43 scripting = new ScriptingDock(tr("Scripting"), this);
44 scripting->setAllowedAreas(Qt::BottomDockWidgetArea);
45 addDockWidget(Qt::BottomDockWidgetArea, scripting);
46
47 listWidget = new QListWidget();
48 listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
49 connect(listWidget, SIGNAL(customContextMenuRequested(const QPoint&)),
50 this, SLOT(showListContextMenu(const QPoint&)));
51
52 stackedWidget = new QStackedWidget();
53 dockWidget = new QDockWidget(tr("Functions"), this);
54 dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
55 Qt::RightDockWidgetArea);
56 dockWidget->setWidget(listWidget);
57 addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
58 setCentralWidget(stackedWidget);
59
60 connect(listWidget, SIGNAL(currentRowChanged(int)),
61 this, SLOT(switchMainPlane(int)));
62
63 setWindowTitle(tr("FRIDA"));
64
65 mgr->connect_new_function_signal([&] (Function* fun) {addFunction(fun);});
66 mgr->connect_new_dyn_symbol_signal([&] (const std::string& name) {
67 auto item = new QListWidgetItem(name.c_str(), listWidget);
68 item->setBackground(QBrush(QColor(0xff, 0xdd, 0xdd)));
69 });
70 setGlobalHotkeys();
71 }
72
73 void Mainwindow::setGlobalHotkeys() {
74 QShortcut *shortcut = new QShortcut(QKeySequence("f"), this);
75 connect(shortcut, &QShortcut::activated, this, &Mainwindow::requestNewFunction);
76
77 shortcut = new QShortcut(QKeySequence("r"), listWidget);
78 connect(shortcut, &QShortcut::activated, [=]() {
79 QListWidgetItem * item = listWidget->currentItem();
80 if (item) renameFunction(objects_list[item]->getFunction());
81 });
82 }
83
84 void Mainwindow::quit()
85 {
86 QMessageBox messageBox;
87 messageBox.setWindowTitle(tr("Frida"));
88 messageBox.setText(tr("Do you really want to quit?"));
89 messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
90 messageBox.setDefaultButton(QMessageBox::No);
91 if (messageBox.exec() == QMessageBox::Yes)
92 qApp->quit();
93 }
94
95 void Mainwindow::open() {
96 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
97 tr("Binaries (*)"));
98
99 manager->reset(fileName.toStdString());
100 }
101
102 void Mainwindow::save() {
103 QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Frida Archives (*.frida)"));
104 manager->save(filename);
105 }
106
107 void Mainwindow::switchMainPlaneToAddress(uint64_t address) {
108 if (objects_list_by_address.find(address) != objects_list_by_address.end()) {
109 LOG4CXX_DEBUG(logger, "Switching to function " << std::hex << address);
110 QListWidgetItem * item = objects_list_by_address[address];
111 listWidget->setCurrentItem(item);
112 stackedWidget->setCurrentWidget(objects_list[item]);
113 } else {
114 LOG4CXX_DEBUG(logger, "No function at " << std::hex << address
115 << " -- it's probably an imported Symbol");
116 }
117 }
118
119 void Mainwindow::switchMainPlane(int index) {
120 stackedWidget->setCurrentWidget(objects_list[listWidget->currentItem()]);
121 }
122
123 void Mainwindow::showListContextMenu(const QPoint& point) {
124 QListWidgetItem * item = listWidget->itemAt(point);
125 QMenu menu(this);
126 if (item) {
127 QAction * act = menu.addAction("Rename Function");
128 connect(act, &QAction::triggered, [=]() {this->renameFunction(objects_list[item]->getFunction());});
129 } else {
130 QAction * act = menu.addAction("AddFunction");
131 connect(act, SIGNAL(triggered()), this, SLOT(requestNewFunction()));
132 }
133 menu.exec(listWidget->mapToGlobal(point));
134 }
135
136 void Mainwindow::requestNewFunction() {
137 NewFunctionDialog dialog;
138 int result = dialog.exec();
139 if (QDialog::Accepted == result) {
140 requestNewFunctionByAddress(dialog.result());
141 } else {
142 LOG4CXX_DEBUG(logger, "requestNewFunction aborted");
143 }
144 }
145
146 void Mainwindow::requestNewFunctionByAddress(uint64_t address) {
147 LOG4CXX_DEBUG(logger, "requesting Function at " << std::hex << address);
148 manager->getDisassembler()->disassembleFunctionAt(address);
149 switchMainPlaneToAddress(address);
150 }
151
152 void Mainwindow::renameFunction(Function* function) {
153 SimpleStringDialog dialog("New name");
154 int result = dialog.exec();
155 if (QDialog::Accepted == result) {
156 LOG4CXX_DEBUG(logger, "renaming Function " << function->getName()
157 << " to " << dialog.result().toStdString());
158 function->setName(dialog.result().toStdString());
159 objects_list_by_address[function->getStartAddress()]->setText(dialog.result());
160 } else {
161 LOG4CXX_DEBUG(logger, "renameFunction aborted");
162 }
163 }
164
165 void Mainwindow::addFunction(Function* fun) {
166 if (functions.find(fun->getStartAddress()) != functions.end())
167 return;
168
169 functions.insert(std::make_pair(fun->getStartAddress(), fun));
170
171 FunctionWidget * w = new FunctionWidget(fun);
172
173 // CFG
174 CFGScene * scene = new CFGScene;
175
176 BasicBlock * block = manager->getBasicBlock(fun->getStartAddress());
177
178 uint64_t start_address(std::numeric_limits<uint64_t>::max());
179 for (auto b : fun->blocks()) {
180 if (b.first < start_address)
181 start_address = b.first;
182 }
183
184 local__add_basic_block(block, this,
185 manager, blocks, scene, start_address, 100);
186
187 QGraphicsView * view = new QGraphicsView(scene);
188 w->addTab(view, "CFG");
189
190 // Listing
191 QTableWidget * t = new QTableWidget();
192 t->setColumnCount(3);
193 t->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
194
195 w->addTab(t, "Listing");
196
197 QListWidgetItem * item = new QListWidgetItem(fun->getName().c_str(), listWidget);
198 stackedWidget->addWidget(w);
199 objects_list.insert(std::make_pair(item, w));
200 LOG4CXX_DEBUG(logger, "Adding function widget at " << std::hex
201 << fun->getStartAddress());
202 objects_list_by_address.insert(std::make_pair(fun->getStartAddress(), item));
203 }
204
205 namespace {
206 BasicBlockWidget *
207 local__add_basic_block(BasicBlock * block,
208 Mainwindow * mainwindow, InformationManager * manager,
209 std::map<uint64_t, BasicBlockWidget*>& known_blocks,
210 CFGScene * scene, uint64_t starty, uint64_t startx) {
211
212 decltype(known_blocks.begin()) old;
213 if ((old = known_blocks.find(block->getStartAddress())) != known_blocks.end())
214 return old->second;
215
216 std::stringstream s;
217 s << "BLOCK_" << std::hex << block->getStartAddress()
218 << "_" << block->getEndAddress();
219 BasicBlockWidget * widget = new BasicBlockWidget(s.str().c_str(),
220 block, mainwindow);
221
222 known_blocks.insert(std::make_pair(block->getStartAddress(), widget));
223
224 scene->addItem(widget);
225 widget->setFlag(QGraphicsItem::ItemIsMovable, true);
226 widget->moveBy(100*startx, block->getStartAddress() - starty);
227
228 manager->getDisassembler()
229 ->printEachInstruction(block->getStartAddress(),
230 block->getEndAddress(),
231 [&](uint8_t* bytes,
232 size_t byte_count,
233 const std::string& line,
234 const std::string& ref) {
235 widget->addItem(bytes, byte_count,
236 line.c_str() + 1, // remove \t
237 ref.c_str());
238 });
239
240 BasicBlockWidget *tmp, *nextl(NULL), *nextr(NULL);
241 BasicBlock * tmpblock;
242 if (block->getNextBlock(0) != 0) {
243 int xshift = 0;
244 if (block->getNextBlock(1) != 0)
245 xshift = 1;
246 tmpblock = manager->getBasicBlock(block->getNextBlock(0));
247 tmp = local__add_basic_block(tmpblock, mainwindow, manager,
248 known_blocks,
249 scene, starty, startx+xshift);
250 nextl = tmp;
251 tmp->addPrevious(widget);
252 }
253 if (block->getNextBlock(1) != 0) {
254 tmpblock = manager->getBasicBlock(block->getNextBlock(1));
255 tmp = local__add_basic_block(tmpblock, mainwindow, manager,
256 known_blocks,
257 scene, starty, startx-1);
258 nextr = tmp;
259 tmp->addPrevious(widget);
260 }
261 widget->addNext(nextl, nextr);
262 return widget;
263 }
264 }