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