]>
git.siccegge.de Git - frida/frida.git/blob - src/gui/Mainwindow.cxx
1 #include "Mainwindow.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"
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"
20 Mainwindow::Mainwindow(InformationManager
* 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);
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
);
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
);
44 QMenu
* interpretermenu
= menuBar()->addMenu(tr("&Interpreter"));
46 QPluginLoader
* loader
= new QPluginLoader("libguilePlugin", this);
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);
52 fdock
->addTab(new LogDock(fdock
), "Log");
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
,
61 QString fileName
= QFileDialog::getOpenFileName(this, tr("Open Script"), "",
62 tr("Binaries") + " (*." +
63 interpreter
["GUILE"]->fileExtension().c_str() + ")");
64 std::stringstream a
, b
;
66 interpreter
["GUILE"]->loadFile(fileName
.toStdString(), a
, b
, c
);
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
&)));
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
);
84 connect(listWidget
, &QTreeWidget::currentItemChanged
,
85 [=] (QTreeWidgetItem
* current
, QTreeWidgetItem
*) {
86 switchMainPlane(current
);
89 setWindowTitle(tr("FRIDA"));
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)));
99 mgr
->connect_rename_function_signal([&](RenameFunctionEvent
* event
) {
100 if (objects_list_by_address
.find(event
->address
) == objects_list_by_address
.end())
102 auto item
= objects_list_by_address
[event
->address
];
103 if (item
) item
->setText(0, event
->new_name
.c_str());
108 void Mainwindow::setGlobalHotkeys() {
109 QShortcut
*shortcut
= new QShortcut(QKeySequence("f"), this);
110 connect(shortcut
, &QShortcut::activated
, this, &Mainwindow::requestNewFunction
);
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());
119 void Mainwindow::quit()
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
)
130 void Mainwindow::open() {
131 QString fileName
= QFileDialog::getOpenFileName(this, tr("Open File"), "",
133 manager
->reset(fileName
.toStdString());
136 void Mainwindow::load() {
137 QString fileName
= QFileDialog::getOpenFileName(this, tr("Open File"), "",
138 tr("Frida Archives (*.frida)"));
139 manager
->load(fileName
.toStdString());
142 void Mainwindow::save() {
143 QString filename
= QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Frida Archives (*.frida)"));
144 manager
->save(filename
.toStdString());
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
]);
154 LOG4CXX_DEBUG(logger
, "No function at " << std::hex
<< address
155 << " -- it's probably an imported Symbol");
159 void Mainwindow::switchMainPlane(QTreeWidgetItem
* to
) {
160 if (objects_list
.end() != objects_list
.find(to
))
161 stackedWidget
->setCurrentWidget(objects_list
[to
]);
164 void Mainwindow::showListContextMenu(const QPoint
& point
) {
166 QTreeWidgetItem
* item
= listWidget
->itemAt(point
);
169 act
= menu
.addAction("Add Function");
170 connect(act
, &QAction::triggered
, this, &Mainwindow::requestNewFunction
);
172 act
= menu
.addAction("Add Group");
173 connect(act
, &QAction::triggered
, this, &Mainwindow::requestNewGroup
);
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());});
180 act
= menu
.addAction("Rename Group");
181 connect(act
, &QAction::triggered
, [=]() {renameGroup(item
);});
185 QMenu
* submenu
= menu
.addMenu("Move to group");
187 for (QTreeWidgetItem
* groupitem
: group_list
) {
188 act
= submenu
->addAction(groupitem
->text(0));
189 connect(act
, &QAction::triggered
,
191 listWidget
->invisibleRootItem()->removeChild(item
);
192 groupitem
->addChild(item
);
197 menu
.exec(listWidget
->mapToGlobal(point
));
200 void Mainwindow::requestNewFunction() {
201 NewFunctionDialog dialog
;
202 int result
= dialog
.exec();
203 if (QDialog::Accepted
== result
) {
204 requestNewFunctionByAddress(dialog
.result());
206 LOG4CXX_DEBUG(logger
, "requestNewFunction aborted");
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
);
218 LOG4CXX_DEBUG(logger
, "requestNewGroup aborted");
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
);
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());
236 LOG4CXX_DEBUG(logger
, "renameFunction aborted");
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());
248 LOG4CXX_DEBUG(logger
, "renameFunction aborted");
252 void Mainwindow::addFunction(Function
* fun
) {
253 if (functions
.find(fun
->getStartAddress()) != functions
.end())
256 functions
.insert(std::make_pair(fun
->getStartAddress(), fun
));
258 FunctionWidget
* w
= new FunctionWidget(fun
, this);
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
));