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