]> git.siccegge.de Git - frida/frida.git/blob - src/gui/Mainwindow.cxx
850c0364b61ba1c4d5fc845586ba8913fe48f9d9
[frida/frida.git] / src / gui / Mainwindow.cxx
1 #include "Mainwindow.hxx"
2 #include "qt.hxx"
3 #include "bindings/Guile.hxx"
4 #include "disassembler/llvm/LLVMDisassembler.hxx"
5 #include "core/Function.hxx"
6 #include "core/BasicBlock.hxx"
7 #include "core/InformationManager.hxx"
8 #include "core/events/RenameFunctionEvent.hxx"
9 #include "core/events/NewFunctionEvent.hxx"
10
11 #include "widgets/FridaDock.hxx"
12 #include "widgets/LogDock.hxx"
13 #include "widgets/ScriptingDock.hxx"
14 #include "widgets/CFGScene.hxx"
15 #include "widgets/FunctionWidget.hxx"
16 #include "dialogs/NewFunctionDialog.hxx"
17 #include "dialogs/SimpleStringDialog.hxx"
18
19 #include <sstream>
20
21 Mainwindow::Mainwindow(InformationManager* mgr)
22 : manager(mgr)
23 , logger(log4cxx::Logger::getLogger("gui.Mainwindow")) {
24 openAction = new QAction(tr("&Open"), this);
25 loadAction = new QAction(tr("&Load"), this);
26 saveAction = new QAction(tr("&Save"), this);
27 exitAction = new QAction(tr("E&xit"), this);
28
29 connect(openAction, &QAction::triggered,
30 this, &Mainwindow::open);
31 connect(loadAction, &QAction::triggered,
32 this, &Mainwindow::load);
33 connect(saveAction, &QAction::triggered,
34 this, &Mainwindow::save);
35 connect(exitAction, &QAction::triggered,
36 qApp, &QApplication::quit);
37
38 fileMenu = menuBar()->addMenu(tr("&File"));
39 fileMenu->addAction(openAction);
40 fileMenu->addAction(loadAction);
41 fileMenu->addAction(saveAction);
42 fileMenu->addSeparator();
43 fileMenu->addAction(exitAction);
44
45 QMenu* interpretermenu = menuBar()->addMenu(tr("&Interpreter"));
46
47 fdock = new FridaDock(tr("Frida Dock"), this);
48
49 fdock->addTab(new LogDock(fdock), "Log");
50
51 fdock->addTab(new ScriptingDock(manager->getInterpreter("GUILE"), fdock), "guile");
52 fdock->setAllowedAreas(Qt::BottomDockWidgetArea);
53 addDockWidget(Qt::BottomDockWidgetArea, fdock);
54 QAction* guileLoad = new QAction(tr("&GUILE"), this);
55 interpretermenu->addAction(guileLoad);
56 connect(guileLoad, &QAction::triggered,
57 [&]() {
58 QString fileName = QFileDialog::getOpenFileName(this, tr("Open Script"), "",
59 tr("Scripts") + " (*." +
60 manager->getInterpreter("GUILE")->fileExtension().c_str() + ")");
61 if(! fileName.isNull()) {
62 std::stringstream a, b;
63 std::string c;
64 manager->getInterpreter("GUILE")->loadFile(fileName.toStdString(), a, b, c);
65 }
66 });
67
68 listWidget = new QTreeWidget();
69 listWidget->setColumnCount(1);
70 listWidget->setDragDropMode(QAbstractItemView::InternalMove);
71 listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
72 connect(listWidget, SIGNAL(customContextMenuRequested(const QPoint&)),
73 this, SLOT(showListContextMenu(const QPoint&)));
74
75 stackedWidget = new QStackedWidget();
76 dockWidget = new QDockWidget(tr("Functions"), this);
77 dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
78 Qt::RightDockWidgetArea);
79 dockWidget->setWidget(listWidget);
80 addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
81 setCentralWidget(stackedWidget);
82
83 connect(listWidget, &QTreeWidget::currentItemChanged,
84 [=] (QTreeWidgetItem* current, QTreeWidgetItem*) {
85 switchMainPlane(current);
86 });
87
88 setWindowTitle(tr("FRIDA"));
89
90 QTreeWidgetItem * external = new QTreeWidgetItem(listWidget, QStringList("External Functions"));
91 external->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
92 external->setBackground(0, QBrush(QColor(0xff, 0xdd, 0xdd)));
93 connect(mgr, &InformationManager::resetEvent,
94 [this,mgr]() {
95 connect(this, SIGNAL(requestNewFunctionByAddress(uint64_t)),
96 mgr->getDisassembler(), SLOT(disassembleFunctionAt(uint64_t)));
97 });
98 connect(mgr, &InformationManager::newFunctionEvent,
99 [=] (NewFunctionEvent* event) {
100 std::string name = event->function->getName();
101 if (event->function->isDynamic()) {
102 auto item = new QTreeWidgetItem(external, QStringList(name.c_str()));
103 item->setBackground(0, QBrush(QColor(0xff, 0xdd, 0xdd)));
104 } else {
105 addFunction(event->function);
106 }
107 });
108 connect(mgr, &InformationManager::renameFunctionEvent,
109 [&](RenameFunctionEvent* event) {
110 if (objects_list_by_address.find(event->address) == objects_list_by_address.end())
111 return;
112 auto item = objects_list_by_address[event->address];
113 if (item) item->setText(0, event->new_name.c_str());
114 });
115 setGlobalHotkeys();
116 }
117
118 void Mainwindow::setGlobalHotkeys() {
119 QShortcut *shortcut = new QShortcut(QKeySequence("f"), this);
120 connect(shortcut, &QShortcut::activated, this, &Mainwindow::requestNewFunction);
121
122 shortcut = new QShortcut(QKeySequence("r"), listWidget);
123 connect(shortcut, &QShortcut::activated, [=]() {
124 QTreeWidgetItem * item = listWidget->currentItem();
125 if (item) renameFunction(objects_list[item]->getFunction());
126 });
127 }
128
129 void Mainwindow::quit()
130 {
131 QMessageBox messageBox;
132 messageBox.setWindowTitle(tr("Frida"));
133 messageBox.setText(tr("Do you really want to quit?"));
134 messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
135 messageBox.setDefaultButton(QMessageBox::No);
136 if (messageBox.exec() == QMessageBox::Yes)
137 qApp->quit();
138 }
139
140 void Mainwindow::open() {
141 QFileDialog dialog(this, tr("Open bianry"), "", tr("Binaries (*)"));
142
143 if (dialog.exec()) {
144 QStringList files = dialog.selectedFiles();
145 if(1 != files.size()) {
146 LOG4CXX_ERROR(logger, "Needs exactly one file name")
147 } else {
148 manager->reset(files[0].toStdString());
149 }
150 }
151 }
152
153 void Mainwindow::load() {
154 QFileDialog dialog(this, tr("Open saved FrIDa file"), "", tr("Frida Archives (*.frida)"));
155
156 if (dialog.exec()) {
157 QStringList files = dialog.selectedFiles();
158 if(1 != files.size()) {
159 LOG4CXX_ERROR(logger, "Needs exactly one file name")
160 } else {
161 manager->load(files[0].toStdString());
162 }
163 }
164 }
165
166 void Mainwindow::save() {
167 QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Frida Archives (*.frida)"));
168 manager->save(filename.toStdString());
169 }
170
171 void Mainwindow::switchMainPlaneToAddress(uint64_t address) {
172 if (objects_list_by_address.find(address) != objects_list_by_address.end()) {
173 LOG4CXX_DEBUG(logger, "Switching to function " << std::hex << address);
174 QTreeWidgetItem * item = objects_list_by_address[address];
175 listWidget->setCurrentItem(item);
176 stackedWidget->setCurrentWidget(objects_list[item]);
177 } else {
178 LOG4CXX_DEBUG(logger, "No function at " << std::hex << address
179 << " -- it's probably an imported Symbol");
180 }
181 }
182
183 void Mainwindow::switchMainPlane(QTreeWidgetItem* to) {
184 if (objects_list.end() != objects_list.find(to))
185 stackedWidget->setCurrentWidget(objects_list[to]);
186 }
187
188 void Mainwindow::showListContextMenu(const QPoint& point) {
189 QAction * act;
190 QTreeWidgetItem * item = listWidget->itemAt(point);
191 QMenu menu(this);
192
193 act = menu.addAction("Add Function");
194 connect(act, &QAction::triggered, this, &Mainwindow::requestNewFunction);
195
196 act = menu.addAction("Add Group");
197 connect(act, &QAction::triggered, this, &Mainwindow::requestNewGroup);
198
199 if (item) {
200 if (objects_list.find(item) != objects_list.end()) {
201 act = menu.addAction("Rename Function");
202 connect(act, &QAction::triggered, [=]() {this->renameFunction(objects_list[item]->getFunction());});
203 } else {
204 act = menu.addAction("Rename Group");
205 connect(act, &QAction::triggered, [=]() {renameGroup(item);});
206 }
207
208
209 QMenu* submenu = menu.addMenu("Move to group");
210
211 for (QTreeWidgetItem* groupitem : group_list) {
212 act = submenu->addAction(groupitem->text(0));
213 connect(act, &QAction::triggered,
214 [=] () {
215 listWidget->invisibleRootItem()->removeChild(item);
216 groupitem->addChild(item);
217 });
218 }
219 }
220
221 menu.exec(listWidget->mapToGlobal(point));
222 }
223
224 void Mainwindow::requestNewFunction() {
225 NewFunctionDialog dialog;
226 int result = dialog.exec();
227 if (QDialog::Accepted == result) {
228 emit requestNewFunctionByAddress(dialog.result());
229 } else {
230 LOG4CXX_DEBUG(logger, "requestNewFunction aborted");
231 }
232 }
233
234 void Mainwindow::requestNewGroup() {
235 SimpleStringDialog dialog("New Group");
236 int result = dialog.exec();
237 if (QDialog::Accepted == result) {
238 QTreeWidgetItem * external = new QTreeWidgetItem(listWidget, QStringList(dialog.result()));
239 external->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
240 group_list.push_back(external);
241 } else {
242 LOG4CXX_DEBUG(logger, "requestNewGroup aborted");
243 }
244 }
245
246 void Mainwindow::renameFunction(Function* function) {
247 SimpleStringDialog dialog("New name");
248 int result = dialog.exec();
249 if (QDialog::Accepted == result) {
250 LOG4CXX_DEBUG(logger, "renaming Function " << function->getName()
251 << " to " << dialog.result().toStdString());
252 function->setName(dialog.result().toStdString());
253 } else {
254 LOG4CXX_DEBUG(logger, "renameFunction aborted");
255 }
256 }
257
258 void Mainwindow::renameGroup(QTreeWidgetItem* item) {
259 SimpleStringDialog dialog("New name");
260 int result = dialog.exec();
261 if (QDialog::Accepted == result) {
262 LOG4CXX_DEBUG(logger, "renaming group " << item->text(0).toStdString()
263 << " to " << dialog.result().toStdString());
264 item->setText(0, dialog.result());
265 } else {
266 LOG4CXX_DEBUG(logger, "renameFunction aborted");
267 }
268 }
269
270 void Mainwindow::addFunction(Function* fun) {
271 if (functions.find(fun->getStartAddress()) != functions.end())
272 return;
273
274 functions.insert(std::make_pair(fun->getStartAddress(), fun));
275
276 FunctionWidget * w = new FunctionWidget(fun, this);
277
278 QTreeWidgetItem * item = new QTreeWidgetItem(listWidget, QStringList(fun->getName().c_str()));
279 stackedWidget->addWidget(w);
280 objects_list.insert(std::make_pair(item, w));
281 LOG4CXX_DEBUG(logger, "Adding function widget at " << std::hex
282 << fun->getStartAddress());
283 objects_list_by_address.insert(std::make_pair(fun->getStartAddress(), item));
284 }