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