]> git.siccegge.de Git - frida/frida.git/blob - src/gui/Mainwindow.cxx
Properly handle abort of open dialog
[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("Binaries") + " (*." +
60 manager->getInterpreter("GUILE")->fileExtension().c_str() + ")");
61 std::stringstream a, b;
62 std::string c;
63 manager->getInterpreter("GUILE")->loadFile(fileName.toStdString(), a, b, c);
64 });
65
66 listWidget = new QTreeWidget();
67 listWidget->setColumnCount(1);
68 listWidget->setDragDropMode(QAbstractItemView::InternalMove);
69 listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
70 connect(listWidget, SIGNAL(customContextMenuRequested(const QPoint&)),
71 this, SLOT(showListContextMenu(const QPoint&)));
72
73 stackedWidget = new QStackedWidget();
74 dockWidget = new QDockWidget(tr("Functions"), this);
75 dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
76 Qt::RightDockWidgetArea);
77 dockWidget->setWidget(listWidget);
78 addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
79 setCentralWidget(stackedWidget);
80
81 connect(listWidget, &QTreeWidget::currentItemChanged,
82 [=] (QTreeWidgetItem* current, QTreeWidgetItem*) {
83 switchMainPlane(current);
84 });
85
86 setWindowTitle(tr("FRIDA"));
87
88 QTreeWidgetItem * external = new QTreeWidgetItem(listWidget, QStringList("External Functions"));
89 external->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
90 external->setBackground(0, QBrush(QColor(0xff, 0xdd, 0xdd)));
91 mgr->registerNewFunctionEvent([=] (NewFunctionEvent* event) {
92 std::string name = event->function->getName();
93 if (event->function->isDynamic()) {
94 auto item = new QTreeWidgetItem(external, QStringList(name.c_str()));
95 item->setBackground(0, QBrush(QColor(0xff, 0xdd, 0xdd)));
96 } else {
97 addFunction(event->function);
98 }
99 });
100 mgr->registerRenameFunctionEvent([&](RenameFunctionEvent* event) {
101 if (objects_list_by_address.find(event->address) == objects_list_by_address.end())
102 return;
103 auto item = objects_list_by_address[event->address];
104 if (item) item->setText(0, event->new_name.c_str());
105 });
106 setGlobalHotkeys();
107 }
108
109 void Mainwindow::setGlobalHotkeys() {
110 QShortcut *shortcut = new QShortcut(QKeySequence("f"), this);
111 connect(shortcut, &QShortcut::activated, this, &Mainwindow::requestNewFunction);
112
113 shortcut = new QShortcut(QKeySequence("r"), listWidget);
114 connect(shortcut, &QShortcut::activated, [=]() {
115 QTreeWidgetItem * item = listWidget->currentItem();
116 if (item) renameFunction(objects_list[item]->getFunction());
117 });
118 }
119
120 void Mainwindow::quit()
121 {
122 QMessageBox messageBox;
123 messageBox.setWindowTitle(tr("Frida"));
124 messageBox.setText(tr("Do you really want to quit?"));
125 messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
126 messageBox.setDefaultButton(QMessageBox::No);
127 if (messageBox.exec() == QMessageBox::Yes)
128 qApp->quit();
129 }
130
131 void Mainwindow::open() {
132 QFileDialog dialog(this, tr("Open bianry"), "", tr("Binaries (*)"));
133
134 if (dialog.exec()) {
135 QStringList files = dialog.selectedFiles();
136 if(1 != files.size()) {
137 LOG4CXX_ERROR(logger, "Needs exactly one file name")
138 } else {
139 manager->reset(files[0].toStdString());
140 }
141 }
142 }
143
144 void Mainwindow::load() {
145 QFileDialog dialog(this, tr("Open saved FrIDa file"), "", tr("Frida Archives (*.frida)"));
146
147 if (dialog.exec()) {
148 QStringList files = dialog.selectedFiles();
149 if(1 != files.size()) {
150 LOG4CXX_ERROR(logger, "Needs exactly one file name")
151 } else {
152 manager->load(files[0].toStdString());
153 }
154 }
155 }
156
157 void Mainwindow::save() {
158 QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Frida Archives (*.frida)"));
159 manager->save(filename.toStdString());
160 }
161
162 void Mainwindow::switchMainPlaneToAddress(uint64_t address) {
163 if (objects_list_by_address.find(address) != objects_list_by_address.end()) {
164 LOG4CXX_DEBUG(logger, "Switching to function " << std::hex << address);
165 QTreeWidgetItem * item = objects_list_by_address[address];
166 listWidget->setCurrentItem(item);
167 stackedWidget->setCurrentWidget(objects_list[item]);
168 } else {
169 LOG4CXX_DEBUG(logger, "No function at " << std::hex << address
170 << " -- it's probably an imported Symbol");
171 }
172 }
173
174 void Mainwindow::switchMainPlane(QTreeWidgetItem* to) {
175 if (objects_list.end() != objects_list.find(to))
176 stackedWidget->setCurrentWidget(objects_list[to]);
177 }
178
179 void Mainwindow::showListContextMenu(const QPoint& point) {
180 QAction * act;
181 QTreeWidgetItem * item = listWidget->itemAt(point);
182 QMenu menu(this);
183
184 act = menu.addAction("Add Function");
185 connect(act, &QAction::triggered, this, &Mainwindow::requestNewFunction);
186
187 act = menu.addAction("Add Group");
188 connect(act, &QAction::triggered, this, &Mainwindow::requestNewGroup);
189
190 if (item) {
191 if (objects_list.find(item) != objects_list.end()) {
192 act = menu.addAction("Rename Function");
193 connect(act, &QAction::triggered, [=]() {this->renameFunction(objects_list[item]->getFunction());});
194 } else {
195 act = menu.addAction("Rename Group");
196 connect(act, &QAction::triggered, [=]() {renameGroup(item);});
197 }
198
199
200 QMenu* submenu = menu.addMenu("Move to group");
201
202 for (QTreeWidgetItem* groupitem : group_list) {
203 act = submenu->addAction(groupitem->text(0));
204 connect(act, &QAction::triggered,
205 [=] () {
206 listWidget->invisibleRootItem()->removeChild(item);
207 groupitem->addChild(item);
208 });
209 }
210 }
211
212 menu.exec(listWidget->mapToGlobal(point));
213 }
214
215 void Mainwindow::requestNewFunction() {
216 NewFunctionDialog dialog;
217 int result = dialog.exec();
218 if (QDialog::Accepted == result) {
219 requestNewFunctionByAddress(dialog.result());
220 } else {
221 LOG4CXX_DEBUG(logger, "requestNewFunction aborted");
222 }
223 }
224
225 void Mainwindow::requestNewGroup() {
226 SimpleStringDialog dialog("New Group");
227 int result = dialog.exec();
228 if (QDialog::Accepted == result) {
229 QTreeWidgetItem * external = new QTreeWidgetItem(listWidget, QStringList(dialog.result()));
230 external->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
231 group_list.push_back(external);
232 } else {
233 LOG4CXX_DEBUG(logger, "requestNewGroup aborted");
234 }
235 }
236
237 void Mainwindow::requestNewFunctionByAddress(uint64_t address) {
238 LOG4CXX_DEBUG(logger, "requesting Function at " << std::hex << address);
239 manager->getDisassembler()->disassembleFunctionAt(address);
240 switchMainPlaneToAddress(address);
241 }
242
243 void Mainwindow::renameFunction(Function* function) {
244 SimpleStringDialog dialog("New name");
245 int result = dialog.exec();
246 if (QDialog::Accepted == result) {
247 LOG4CXX_DEBUG(logger, "renaming Function " << function->getName()
248 << " to " << dialog.result().toStdString());
249 function->setName(dialog.result().toStdString());
250 } else {
251 LOG4CXX_DEBUG(logger, "renameFunction aborted");
252 }
253 }
254
255 void Mainwindow::renameGroup(QTreeWidgetItem* item) {
256 SimpleStringDialog dialog("New name");
257 int result = dialog.exec();
258 if (QDialog::Accepted == result) {
259 LOG4CXX_DEBUG(logger, "renaming group " << item->text(0).toStdString()
260 << " to " << dialog.result().toStdString());
261 item->setText(0, dialog.result());
262 } else {
263 LOG4CXX_DEBUG(logger, "renameFunction aborted");
264 }
265 }
266
267 void Mainwindow::addFunction(Function* fun) {
268 if (functions.find(fun->getStartAddress()) != functions.end())
269 return;
270
271 functions.insert(std::make_pair(fun->getStartAddress(), fun));
272
273 FunctionWidget * w = new FunctionWidget(fun, this);
274
275 QTreeWidgetItem * item = new QTreeWidgetItem(listWidget, QStringList(fun->getName().c_str()));
276 stackedWidget->addWidget(w);
277 objects_list.insert(std::make_pair(item, w));
278 LOG4CXX_DEBUG(logger, "Adding function widget at " << std::hex
279 << fun->getStartAddress());
280 objects_list_by_address.insert(std::make_pair(fun->getStartAddress(), item));
281 }