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