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