-#include "Mainwindow.h++"
+#include "Mainwindow.hxx"
+#include "qt.hxx"
+#include "disassembler/llvm/LLVMDisassembler.hxx"
+#include "core/Function.hxx"
+#include "core/BasicBlock.hxx"
+#include "core/InformationManager.hxx"
+#include "widgets/ScriptingDock.hxx"
+#include "widgets/CFGScene.hxx"
+#include "dialogs/NewFunctionDialog.hxx"
+#include "dialogs/SimpleStringDialog.hxx"
-#include <QtGui>
+#include <sstream>
-Mainwindow::Mainwindow()
-{
- // openAction = new QAction(tr("&Open"), this);
- // saveAction = new QAction(tr("&Save"), this);
- exitAction = new QAction(tr("E&xit"), this);
+namespace {
+ BasicBlockWidget *
+ local__add_basic_block(BasicBlock * block, Disassembler * dis,
+ Mainwindow * mainwindow,
+ std::map<uint64_t, BasicBlockWidget*>& known_blocks,
+ CFGScene * scene, uint64_t starty, uint64_t startx);
+}
+
+Mainwindow::Mainwindow(InformationManager* mgr)
+ : manager(mgr)
+ , logger(log4cxx::Logger::getLogger("Mainwindow")) {
+ openAction = new QAction(tr("&Open"), this);
+ saveAction = new QAction(tr("&Save"), this);
+ exitAction = new QAction(tr("E&xit"), this);
- // connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
- // connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
- connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
+ connect(openAction, SIGNAL(triggered()),
+ this, SLOT(open()));
+ connect(saveAction, SIGNAL(triggered()),
+ this, SLOT(save()));
+ connect(exitAction, SIGNAL(triggered()),
+ qApp, SLOT(quit()));
- fileMenu = menuBar()->addMenu(tr("&File"));
- // fileMenu->addAction(openAction);
- // fileMenu->addAction(saveAction);
- // fileMenu->addSeparator();
- fileMenu->addAction(exitAction);
+ fileMenu = menuBar()->addMenu(tr("&File"));
+ fileMenu->addAction(openAction);
+ fileMenu->addAction(saveAction);
+ fileMenu->addSeparator();
+ fileMenu->addAction(exitAction);
- //
- // setCentralWidget(textEdit);
+ scripting = new ScriptingDock(tr("Scripting"), this);
+ scripting->setAllowedAreas(Qt::BottomDockWidgetArea);
+ addDockWidget(Qt::BottomDockWidgetArea, scripting);
- tabwidget = new QTabWidget;
- setCentralWidget(tabwidget);
+ listWidget = new QListWidget();
+ listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
+ connect(listWidget, SIGNAL(customContextMenuRequested(const QPoint&)),
+ this, SLOT(showListContextMenu(const QPoint&)));
- textEdit = new QTextEdit;
- tabwidget->addTab(textEdit, "edit");
+ stackedWidget = new QStackedWidget();
+ dockWidget = new QDockWidget(tr("Functions"), this);
+ dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
+ Qt::RightDockWidgetArea);
+ dockWidget->setWidget(listWidget);
+ addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
+ setCentralWidget(stackedWidget);
- setWindowTitle(tr("Notepad"));
+ connect(listWidget, SIGNAL(currentRowChanged(int)),
+ this, SLOT(switchMainPlane(int)));
+
+ setWindowTitle(tr("FRIDA"));
+
+ mgr->connect_new_function_signal([&] (Function* fun) {addFunction(fun);});
+ mgr->connect_new_dyn_symbol_signal([&] (const std::string& name) {
+ auto item = new QListWidgetItem(name.c_str(), listWidget);
+ item->setBackground(QBrush(QColor(0xff, 0xdd, 0xdd)));
+ });
+ setGlobalHotkeys();
+}
+
+void Mainwindow::setGlobalHotkeys() {
+ QShortcut *shortcut = new QShortcut(QKeySequence("f"), this);
+ connect(shortcut, &QShortcut::activated, this, &Mainwindow::requestNewFunction);
+
+ shortcut = new QShortcut(QKeySequence("r"), listWidget);
+ connect(shortcut, &QShortcut::activated, [=]() {
+ QListWidgetItem * item = listWidget->currentItem();
+ if (item) renameFunction(item);
+ });
}
void Mainwindow::quit()
{
- QMessageBox messageBox;
- messageBox.setWindowTitle(tr("Notepad"));
- messageBox.setText(tr("Do you really want to quit?"));
- messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
- messageBox.setDefaultButton(QMessageBox::No);
- if (messageBox.exec() == QMessageBox::Yes)
- qApp->quit();
+ QMessageBox messageBox;
+ messageBox.setWindowTitle(tr("Frida"));
+ messageBox.setText(tr("Do you really want to quit?"));
+ messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
+ messageBox.setDefaultButton(QMessageBox::No);
+ if (messageBox.exec() == QMessageBox::Yes)
+ qApp->quit();
}
void Mainwindow::open() {
+ QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
+ tr("Binaries (*)"));
+
+ manager->reset(fileName.toStdString());
+}
+
+void Mainwindow::save() {
+ QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Frida Archives (*.frida)"));
+ manager->save(filename);
+}
+
+void Mainwindow::switchMainPlaneToAddress(uint64_t address) {
+ if (objects_list_by_address.find(address) != objects_list_by_address.end()) {
+ LOG4CXX_DEBUG(logger, "Switching to function " << std::hex << address);
+ QListWidgetItem * item = objects_list_by_address[address];
+ listWidget->setCurrentItem(item);
+ stackedWidget->setCurrentWidget(objects_list[item]);
+ } else {
+ LOG4CXX_DEBUG(logger, "No function at " << std::hex << address
+ << " -- it's probably an imported Symbol");
+ }
+}
+
+void Mainwindow::switchMainPlane(int index) {
+ stackedWidget->setCurrentWidget(objects_list[listWidget->currentItem()]);
+}
+
+void Mainwindow::showListContextMenu(const QPoint& point) {
+ QListWidgetItem * item = listWidget->itemAt(point);
+ QMenu menu(this);
+ if (item) {
+ QAction * act = menu.addAction("Rename Function");
+ connect(act, &QAction::triggered, [=]() {this->renameFunction(item);});
+ } else {
+ QAction * act = menu.addAction("AddFunction");
+ connect(act, SIGNAL(triggered()), this, SLOT(requestNewFunction()));
+ }
+ menu.exec(listWidget->mapToGlobal(point));
+}
+
+void Mainwindow::requestNewFunction() {
+ NewFunctionDialog dialog;
+ int result = dialog.exec();
+ if (QDialog::Accepted == result) {
+ requestNewFunctionByAddress(dialog.result());
+ } else {
+ LOG4CXX_DEBUG(logger, "requestNewFunction aborted");
+ }
+}
+
+void Mainwindow::requestNewFunctionByAddress(uint64_t address) {
+ LOG4CXX_DEBUG(logger, "requesting Function at " << std::hex << address);
+ manager->getDisassembler()->disassembleFunctionAt(address);
+ switchMainPlaneToAddress(address);
+}
+
+void Mainwindow::renameFunction(QListWidgetItem * item) {
+ SimpleStringDialog dialog("New name");
+ int result = dialog.exec();
+ if (QDialog::Accepted == result) {
+ LOG4CXX_DEBUG(logger, "renaming Function " << item->text().toStdString()
+ << " to " << dialog.result().toStdString());
+ item->setText(dialog.result());
+ } else {
+ LOG4CXX_DEBUG(logger, "renameFunction aborted");
+ }
+}
+
+void Mainwindow::addFunction(Function* fun) {
+ if (functions.find(fun) != functions.end())
+ return;
+
+ functions.insert(fun);
+
+ QTabWidget * w = new QTabWidget();
+
+ // CFG
+ CFGScene * scene = new CFGScene;
+
+ Disassembler * dis = manager->getDisassembler();
+ BasicBlock * block = dis->getBasicBlock(fun->getStartAddress());
+
+ uint64_t start_address(std::numeric_limits<uint64_t>::max());
+ for (auto b : fun->blocks()) {
+ if (b.first < start_address)
+ start_address = b.first;
+ }
+
+ local__add_basic_block(block, manager->getDisassembler(), this,
+ blocks, scene, start_address, 100);
+
+ QGraphicsView * view = new QGraphicsView(scene);
+ w->addTab(view, "CFG");
+
+ // Listing
+ QTableWidget * t = new QTableWidget();
+ t->setColumnCount(3);
+ t->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
+
+ w->addTab(t, "Listing");
+
+ QListWidgetItem * item = new QListWidgetItem(fun->getName().c_str(), listWidget);
+ stackedWidget->addWidget(w);
+ objects_list.insert(std::make_pair(item, w));
+ LOG4CXX_DEBUG(logger, "Adding function widget at " << std::hex
+ << fun->getStartAddress());
+ objects_list_by_address.insert(std::make_pair(fun->getStartAddress(), item));
+}
+
+namespace {
+ BasicBlockWidget *
+ local__add_basic_block(BasicBlock * block, Disassembler * dis,
+ Mainwindow * mainwindow,
+ std::map<uint64_t, BasicBlockWidget*>& known_blocks,
+ CFGScene * scene, uint64_t starty, uint64_t startx) {
+
+ decltype(known_blocks.begin()) old;
+ if ((old = known_blocks.find(block->getStartAddress())) != known_blocks.end())
+ return old->second;
+
+ std::stringstream s;
+ s << "BLOCK_" << std::hex << block->getStartAddress()
+ << "_" << block->getEndAddress();
+ BasicBlockWidget * widget = new BasicBlockWidget(s.str().c_str(),
+ block, mainwindow);
+
+ known_blocks.insert(std::make_pair(block->getStartAddress(), widget));
+
+ scene->addItem(widget);
+ widget->setFlag(QGraphicsItem::ItemIsMovable, true);
+ widget->moveBy(100*startx, block->getStartAddress() - starty);
+
+ dis->printEachInstruction(block->getStartAddress(),
+ block->getEndAddress(),
+ [&](uint8_t* bytes,
+ size_t byte_count,
+ const std::string& line,
+ const std::string& ref) {
+ widget->addItem(bytes, byte_count,
+ line.c_str() + 1, // remove \t
+ ref.c_str());
+ });
+ BasicBlockWidget *tmp, *nextl(NULL), *nextr(NULL);
+ BasicBlock * tmpblock;
+ if (block->getNextBlock(0) != 0) {
+ int xshift = 0;
+ if (block->getNextBlock(1) != 0)
+ xshift = 1;
+ tmpblock = dis->getBasicBlock(block->getNextBlock(0));
+ tmp = local__add_basic_block(tmpblock, dis,
+ mainwindow,
+ known_blocks,
+ scene, starty, startx+xshift);
+ nextl = tmp;
+ tmp->addPrevious(widget);
+ }
+ if (block->getNextBlock(1) != 0) {
+ tmpblock = dis->getBasicBlock(block->getNextBlock(1));
+ tmp = local__add_basic_block(tmpblock, dis,
+ mainwindow,
+ known_blocks,
+ scene, starty, startx-1);
+ nextr = tmp;
+ tmp->addPrevious(widget);
+ }
+ widget->addNext(nextl, nextr);
+ return widget;
+ }
}