diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index a3d240cc..ad3b4057 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -972,6 +972,6 @@ void MainWindow::on_actionRefresh_contents_triggered() void MainWindow::on_actionAsmOptions_triggered() { - auto dialog = new AsmOptionsDialog(core, this); + auto dialog = new AsmOptionsDialog(this); dialog->show(); } diff --git a/src/cutter.pro b/src/cutter.pro index 332a5ef0..aeba96d7 100644 --- a/src/cutter.pro +++ b/src/cutter.pro @@ -72,7 +72,8 @@ SOURCES += \ widgets/SectionsWidget.cpp \ widgets/Sidebar.cpp \ widgets/StringsWidget.cpp \ - widgets/SymbolsWidget.cpp + widgets/SymbolsWidget.cpp \ + menus/DisassemblyContextMenu.cpp HEADERS += \ cutter.h \ @@ -117,7 +118,8 @@ HEADERS += \ widgets/SectionsWidget.h \ widgets/Sidebar.h \ widgets/StringsWidget.h \ - widgets/SymbolsWidget.h + widgets/SymbolsWidget.h \ + menus/DisassemblyContextMenu.h FORMS += \ widgets/MemoryWidget.ui \ diff --git a/src/dialogs/AsmOptionsDialog.cpp b/src/dialogs/AsmOptionsDialog.cpp index 72cccb2b..13ac2b74 100644 --- a/src/dialogs/AsmOptionsDialog.cpp +++ b/src/dialogs/AsmOptionsDialog.cpp @@ -5,10 +5,10 @@ #include "utils/Helpers.h" -AsmOptionsDialog::AsmOptionsDialog(CutterCore *core, QWidget *parent) +AsmOptionsDialog::AsmOptionsDialog(QWidget *parent) : QDialog(parent), - core(core), - ui(new Ui::AsmOptionsDialog) + ui(new Ui::AsmOptionsDialog), + core(CutterCore::getInstance()) { ui->setupUi(this); diff --git a/src/dialogs/AsmOptionsDialog.h b/src/dialogs/AsmOptionsDialog.h index c82f0a2a..7355b8c8 100644 --- a/src/dialogs/AsmOptionsDialog.h +++ b/src/dialogs/AsmOptionsDialog.h @@ -18,16 +18,14 @@ class AsmOptionsDialog : public QDialog Q_OBJECT public: - explicit AsmOptionsDialog(CutterCore *core, QWidget *parent = nullptr); + explicit AsmOptionsDialog(QWidget *parent = nullptr); ~AsmOptionsDialog(); private: CutterCore *core; - std::unique_ptr ui; void updateFromVars(); - void saveAsDefault(); void resetToDefault(); diff --git a/src/dialogs/FlagDialog.cpp b/src/dialogs/FlagDialog.cpp index c1866884..eddaf0e4 100644 --- a/src/dialogs/FlagDialog.cpp +++ b/src/dialogs/FlagDialog.cpp @@ -1,11 +1,11 @@ #include "FlagDialog.h" #include "ui_FlagDialog.h" -FlagDialog::FlagDialog(CutterCore *core, RVA offset, QWidget *parent) : +FlagDialog::FlagDialog(RVA offset, QWidget *parent) : QDialog(parent), ui(new Ui::FlagDialog), - core(core), - offset(offset) + offset(offset), + core(CutterCore::getInstance()) { ui->setupUi(this); setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint)); diff --git a/src/dialogs/FlagDialog.h b/src/dialogs/FlagDialog.h index 41d81583..ddbeabee 100644 --- a/src/dialogs/FlagDialog.h +++ b/src/dialogs/FlagDialog.h @@ -15,7 +15,7 @@ class FlagDialog : public QDialog Q_OBJECT public: - explicit FlagDialog(CutterCore *core, RVA offset, QWidget *parent = 0); + explicit FlagDialog(RVA offset, QWidget *parent = 0); ~FlagDialog(); private slots: @@ -24,9 +24,8 @@ private slots: private: std::unique_ptr ui; - - CutterCore *core; RVA offset; + CutterCore *core; }; #endif // FLAGDIALOG_H diff --git a/src/dialogs/XrefsDialog.cpp b/src/dialogs/XrefsDialog.cpp index 6ca7a3c6..de76e5a2 100644 --- a/src/dialogs/XrefsDialog.cpp +++ b/src/dialogs/XrefsDialog.cpp @@ -5,12 +5,11 @@ #include -XrefsDialog::XrefsDialog(MainWindow *main, QWidget *parent) : +XrefsDialog::XrefsDialog(QWidget *parent) : QDialog(parent), addr(0), func_name(QString::null), ui(new Ui::XrefsDialog), - main(main), core(CutterCore::getInstance()) { ui->setupUi(this); diff --git a/src/dialogs/XrefsDialog.h b/src/dialogs/XrefsDialog.h index 5eb4198d..d7024d0d 100644 --- a/src/dialogs/XrefsDialog.h +++ b/src/dialogs/XrefsDialog.h @@ -19,7 +19,7 @@ class XrefsDialog : public QDialog Q_OBJECT public: - explicit XrefsDialog(MainWindow *main, QWidget *parent = 0); + explicit XrefsDialog(QWidget *parent = 0); ~XrefsDialog(); void fillRefsForAddress(RVA addr, QString name, bool whole_function); @@ -42,7 +42,6 @@ private: QString func_name; std::unique_ptr ui; - MainWindow *main; CutterCore *core; Highlighter *highlighter; diff --git a/src/menus/DisassemblyContextMenu.cpp b/src/menus/DisassemblyContextMenu.cpp new file mode 100644 index 00000000..f3009fc9 --- /dev/null +++ b/src/menus/DisassemblyContextMenu.cpp @@ -0,0 +1,125 @@ +#include "DisassemblyContextMenu.h" +#include "dialogs/AsmOptionsDialog.h" +#include "dialogs/CommentsDialog.h" +#include "dialogs/FlagDialog.h" +#include "dialogs/RenameDialog.h" +#include "dialogs/XrefsDialog.h" +#include + +DisassemblyContextMenu::DisassemblyContextMenu(RVA offset, QWidget *parent) : + QMenu(parent), + offset(offset) +{ + actionAddComment.setText("Add comment"); + this->addAction(&actionAddComment); + actionAddFlag.setText("Add flag"); + this->addAction(&actionAddFlag); + actionRename.setText("Rename"); + this->addAction(&actionRename); + actionXRefs.setText("Show xrefs"); + this->addAction(&actionXRefs); + this->addSeparator(); + actionDisplayOptions.setText("Show options"); + this->addAction(&actionDisplayOptions); + + connect(&actionAddComment, SIGNAL(triggered(bool)), this, SLOT(on_actionAddComment_triggered())); + connect(&actionAddFlag, SIGNAL(triggered(bool)), this, SLOT(on_actionAddFlag_triggered())); + connect(&actionRename, SIGNAL(triggered(bool)), this, SLOT(on_actionRename_triggered())); + connect(&actionXRefs, SIGNAL(triggered(bool)), this, SLOT(on_actionXRefs_triggered())); + connect(&actionDisplayOptions, SIGNAL(triggered()), this, SLOT(on_actionDisplayOptions_triggered())); + + /* + * // Set Disas popup menu + QMenu *menu = ui->disasTextEdit_2->createStandardContextMenu(); + QTextCursor cur = ui->disasTextEdit_2->textCursor(); + + // Move cursor to mouse position to get proper function data + cur.setPosition(ui->disasTextEdit_2->cursorForPosition(pt).position(), QTextCursor::MoveAnchor); + ui->disasTextEdit_2->setTextCursor(cur); + + if (cur.hasSelection()) + { + menu->addSeparator(); + menu->addAction(ui->actionSend_to_Notepad); + ui->disasTextEdit_2->setContextMenuPolicy(Qt::DefaultContextMenu); + } + else + { + // Add menu actions + menu->clear(); + menu->addAction(ui->actionDisasAdd_comment); + menu->addAction(ui->actionAddFlag); + menu->addAction(ui->actionFunctionsRename); + menu->addAction(ui->actionFunctionsUndefine); + menu->addSeparator(); + menu->addAction(ui->actionXRefs); + menu->addSeparator(); + menu->addAction(ui->actionDisasCopy_All); + menu->addAction(ui->actionDisasCopy_Bytes); + menu->addAction(ui->actionDisasCopy_Disasm); + menu->addSeparator(); + menu->addAction(ui->actionDisplayOptions); + + ui->disasTextEdit_2->setContextMenuPolicy(Qt::CustomContextMenu); + } + menu->exec(ui->disasTextEdit_2->mapToGlobal(pt)); + delete menu; + ui->disasTextEdit_2->setContextMenuPolicy(Qt::CustomContextMenu); + */ +} + +DisassemblyContextMenu::~DisassemblyContextMenu() +{ +} + +void DisassemblyContextMenu::on_actionAddComment_triggered() +{ + RAnalFunction *fcn = CutterCore::getInstance()->functionAt(offset); + CommentsDialog *c = new CommentsDialog(this); + if (c->exec()) + { + QString comment = c->getComment(); + CutterCore::getInstance()->setComment(offset, comment); + if (fcn) + { + CutterCore::getInstance()->seek(fcn->addr); + } + } +} + +void DisassemblyContextMenu::on_actionAddFlag_triggered() +{ + FlagDialog *dialog = new FlagDialog(offset, this->parentWidget()); + dialog->exec(); +} + +void DisassemblyContextMenu::on_actionRename_triggered() +{ + // Get function for clicked offset + RAnalFunction *fcn = CutterCore::getInstance()->functionAt(offset); + RenameDialog *dialog = new RenameDialog(this); + // Get function based on click position + dialog->setFunctionName(fcn->name); + if (dialog->exec()) + { + // Get new function name + QString new_name = dialog->getFunctionName(); + // Rename function in r2 core + CutterCore::getInstance()->renameFunction(fcn->name, new_name); + // Seek to new renamed function + CutterCore::getInstance()->seek(fcn->addr); + } +} + +void DisassemblyContextMenu::on_actionXRefs_triggered() +{ + XrefsDialog *dialog = new XrefsDialog(this); + dialog->fillRefsForAddress(offset, RAddressString(offset), false); + dialog->exec(); +} + +void DisassemblyContextMenu::on_actionDisplayOptions_triggered() +{ + AsmOptionsDialog *dialog = new AsmOptionsDialog(this->parentWidget()); + dialog->show(); +} diff --git a/src/menus/DisassemblyContextMenu.h b/src/menus/DisassemblyContextMenu.h new file mode 100644 index 00000000..5e189a49 --- /dev/null +++ b/src/menus/DisassemblyContextMenu.h @@ -0,0 +1,28 @@ +#ifndef DISASSEMBLYCONTEXTMENU_H +#define DISASSEMBLYCONTEXTMENU_H + +#include "cutter.h" +#include + +class DisassemblyContextMenu : public QMenu { +Q_OBJECT +public: + DisassemblyContextMenu(RVA offset, QWidget *parent = nullptr); + ~DisassemblyContextMenu(); + +private: + RVA offset; + QAction actionAddComment; + QAction actionAddFlag; + QAction actionRename; + QAction actionXRefs; + QAction actionDisplayOptions; + +private slots: + void on_actionAddComment_triggered(); + void on_actionAddFlag_triggered(); + void on_actionRename_triggered(); + void on_actionXRefs_triggered(); + void on_actionDisplayOptions_triggered(); +}; +#endif // DISASSEMBLYCONTEXTMENU_H diff --git a/src/widgets/DisassemblerGraphView.cpp b/src/widgets/DisassemblerGraphView.cpp index 334f98f2..32ce8998 100644 --- a/src/widgets/DisassemblerGraphView.cpp +++ b/src/widgets/DisassemblerGraphView.cpp @@ -1,5 +1,6 @@ /* x64dbg DisassemblerGraphView */ #include "DisassemblerGraphView.h" +#include "menus/DisassemblyContextMenu.h" #include #include #include @@ -543,7 +544,6 @@ duint DisassemblerGraphView::getInstrForMouseEvent(QMouseEvent* event) bool DisassemblerGraphView::getTokenForMouseEvent(QMouseEvent* event, Token & tokenOut) { Q_UNUSED(event); - Q_UNUSED(tokenOut); /* TODO //Convert coordinates to system used in blocks int xofs = this->horizontalScrollBar()->value(); @@ -662,12 +662,11 @@ void DisassemblerGraphView::mousePressEvent(QMouseEvent* event) this->viewport()->update(); - /*if(event->button() == Qt::RightButton) + if(event->button() == Qt::RightButton) { - QMenu wMenu(this); - mMenuBuilder->build(&wMenu); - wMenu.exec(event->globalPos()); //execute context menu - }*/ + DisassemblyContextMenu cMenu(instr, this); + cMenu.exec(event->globalPos()); //execute context menu + } } else if(event->button() == Qt::LeftButton) { @@ -735,6 +734,7 @@ void DisassemblerGraphView::mouseDoubleClickEvent(QMouseEvent* event) { duint instr = this->getInstrForMouseEvent(event); //DbgCmdExec(QString("graph dis.branchdest(%1), silent").arg(ToPtrString(instr)).toUtf8().constData()); + CutterCore::getInstance()->seek(instr); } } @@ -1372,7 +1372,7 @@ void DisassemblerGraphView::renderFunction(Function & func) //Adjust scroll bars for new size auto areaSize = this->viewport()->size(); this->adjustSize(areaSize.width(), areaSize.height()); - puts("Adjust scroll bars for new size"); + //puts("Adjust scroll bars for new size"); if(this->desired_pos) { diff --git a/src/widgets/FunctionsWidget.cpp b/src/widgets/FunctionsWidget.cpp index 852ce6c0..2e52cdfd 100644 --- a/src/widgets/FunctionsWidget.cpp +++ b/src/widgets/FunctionsWidget.cpp @@ -503,7 +503,7 @@ void FunctionsWidget::on_action_References_triggered() // Get selected item in functions tree view QTreeView *treeView = getCurrentTreeView(); FunctionDescription function = treeView->selectionModel()->currentIndex().data(FunctionModel::FunctionDescriptionRole).value(); - XrefsDialog *x = new XrefsDialog(this->main, this); + XrefsDialog *x = new XrefsDialog(this); x->fillRefsForAddress(function.offset, function.name, true); x->exec(); } diff --git a/src/widgets/MemoryWidget.cpp b/src/widgets/MemoryWidget.cpp index 952deb52..ef099048 100644 --- a/src/widgets/MemoryWidget.cpp +++ b/src/widgets/MemoryWidget.cpp @@ -5,10 +5,7 @@ #include "MainWindow.h" #include "utils/Helpers.h" #include "dialogs/XrefsDialog.h" -#include "dialogs/RenameDialog.h" -#include "dialogs/CommentsDialog.h" -#include "dialogs/AsmOptionsDialog.h" -#include "dialogs/FlagDialog.h" +#include "menus/DisassemblyContextMenu.h" #include #include @@ -983,50 +980,10 @@ void MemoryWidget::showHexASCIIContextMenu(const QPoint &pt) delete menu; } -void MemoryWidget::on_actionDisplayOptions_triggered() -{ - auto dialog = new AsmOptionsDialog(core, this); - dialog->show(); -} - void MemoryWidget::showDisasContextMenu(const QPoint &pt) { - // Set Disas popup menu - QMenu *menu = ui->disasTextEdit_2->createStandardContextMenu(); - QTextCursor cur = ui->disasTextEdit_2->textCursor(); - - // Move cursor to mouse position to get proper function data - cur.setPosition(ui->disasTextEdit_2->cursorForPosition(pt).position(), QTextCursor::MoveAnchor); - ui->disasTextEdit_2->setTextCursor(cur); - - if (cur.hasSelection()) - { - menu->addSeparator(); - menu->addAction(ui->actionSend_to_Notepad); - ui->disasTextEdit_2->setContextMenuPolicy(Qt::DefaultContextMenu); - } - else - { - // Add menu actions - menu->clear(); - menu->addAction(ui->actionDisasAdd_comment); - menu->addAction(ui->actionAddFlag); - menu->addAction(ui->actionFunctionsRename); - menu->addAction(ui->actionFunctionsUndefine); - menu->addSeparator(); - menu->addAction(ui->actionXRefs); - menu->addSeparator(); - menu->addAction(ui->actionDisasCopy_All); - menu->addAction(ui->actionDisasCopy_Bytes); - menu->addAction(ui->actionDisasCopy_Disasm); - menu->addSeparator(); - menu->addAction(ui->actionDisplayOptions); - - ui->disasTextEdit_2->setContextMenuPolicy(Qt::CustomContextMenu); - } - menu->exec(ui->disasTextEdit_2->mapToGlobal(pt)); - delete menu; - ui->disasTextEdit_2->setContextMenuPolicy(Qt::CustomContextMenu); + DisassemblyContextMenu menu(this->readCurrentDisassemblyOffset(), ui->disasTextEdit_2); + menu.exec(ui->disasTextEdit_2->mapToGlobal(pt)); } void MemoryWidget::on_showInfoButton_2_clicked() @@ -1067,12 +1024,10 @@ void MemoryWidget::showXrefsDialog() QString ele = lastline.split(" ", QString::SkipEmptyParts)[0]; if (ele.contains("0x")) { - /*TODO FIXME - * RVA addr = ele.toLongLong(0, 16); - XrefsDialog *x = new XrefsDialog(this->main, this); + RVA addr = ele.toLongLong(0, 16); + XrefsDialog *x = new XrefsDialog(this); x->fillRefsForAddress(addr, RAddressString(addr), false); x->exec(); - */ } } @@ -1199,77 +1154,6 @@ void MemoryWidget::on_actionSend_to_Notepad_triggered() // this->main->sendToNotepad(text); } -void MemoryWidget::on_actionDisasAdd_comment_triggered() -{ - RVA offset = readCurrentDisassemblyOffset(); - - // Get function for clicked offset - RAnalFunction *fcn = this->core->functionAt(offset); - CommentsDialog *c = new CommentsDialog(this); - if (c->exec()) - { - // Get new function name - QString comment = c->getComment(); - //this->main->add_debug_output("Comment: " + comment + " at: " + ele); - // Rename function in r2 core - this->core->setComment(offset, comment); - // Seek to new renamed function - if (fcn) - { - core->seek(fcn->addr); - } - // TODO: Refresh functions tree widget - } - - // this->main->refreshComments(); -} - - -void MemoryWidget::on_actionAddFlag_triggered() -{ - RVA offset = readCurrentDisassemblyOffset(); - - FlagDialog *dialog = new FlagDialog(core, offset, this); - if (dialog->exec()) - { - //QString comment = dialog->getFlagName(); - // Rename function in r2 core - - //this->core->setComment(offset, comment); - } - - // FIXME? - // this->main->refreshComments(); -} - -void MemoryWidget::on_actionFunctionsRename_triggered() -{ - // Get current offset - QTextCursor tc = this->disasTextEdit->textCursor(); - tc.select(QTextCursor::LineUnderCursor); - QString lastline = tc.selectedText(); - QString ele = lastline.split(" ", QString::SkipEmptyParts)[0]; - if (ele.contains("0x")) - { - // Get function for clicked offset - RAnalFunction *fcn = this->core->functionAt(ele.toLongLong(0, 16)); - RenameDialog *r = new RenameDialog(this); - // Get function based on click position - r->setFunctionName(fcn->name); - if (r->exec()) - { - // Get new function name - QString new_name = r->getFunctionName(); - // Rename function in r2 core - this->core->renameFunction(fcn->name, new_name); - // Seek to new renamed function - this->core->seek(fcn->addr); - } - } - // FIXME? - // this->main->refreshFunctions(); -} - void MemoryWidget::on_action8columns_triggered() { this->core->setConfig("hex.cols", 8); @@ -1777,11 +1661,6 @@ void MemoryWidget::setScrollMode() qhelpers::setVerticalScrollMode(ui->xrefToTreeWidget_2); } -void MemoryWidget::on_actionXRefs_triggered() -{ - showXrefsDialog(); -} - void MemoryWidget::on_copyMD5_clicked() { diff --git a/src/widgets/MemoryWidget.h b/src/widgets/MemoryWidget.h index 313e0f30..ee995a5b 100644 --- a/src/widgets/MemoryWidget.h +++ b/src/widgets/MemoryWidget.h @@ -131,7 +131,6 @@ private slots: void on_actionHideDisasm_side_panel_triggered(); void on_actionHideHexdump_side_panel_triggered(); void on_actionHideGraph_side_panel_triggered(); - void on_actionDisplayOptions_triggered(); void on_disasButton_clicked(); void on_graphButton_clicked(); @@ -140,9 +139,6 @@ private slots: void showHexdumpContextMenu(const QPoint &pt); void showHexASCIIContextMenu(const QPoint &pt); void on_actionSend_to_Notepad_triggered(); - void on_actionDisasAdd_comment_triggered(); - void on_actionAddFlag_triggered(); - void on_actionFunctionsRename_triggered(); void on_hexHexText_2_selectionChanged(); void on_hexArchComboBox_2_currentTextChanged(const QString &arg1); @@ -178,7 +174,6 @@ private slots: void on_previewToolButton_clicked(); void on_decoToolButton_clicked(); void on_previewToolButton_2_clicked(); - void on_actionXRefs_triggered(); void on_copyMD5_clicked(); void on_copySHA1_clicked(); void on_simpleGrapgToolButton_clicked(); diff --git a/src/widgets/MemoryWidget.ui b/src/widgets/MemoryWidget.ui index 40ab567f..f1f9f1a7 100644 --- a/src/widgets/MemoryWidget.ui +++ b/src/widgets/MemoryWidget.ui @@ -1220,7 +1220,7 @@ p, li { white-space: pre-wrap; } - + @@ -2739,7 +2739,7 @@ QToolTip { - + @@ -2783,22 +2783,6 @@ QToolTip { Send to notepad - - - Add comment - - - Add comment - - - - - Rename - - - Rename - - Undefine @@ -2823,11 +2807,6 @@ QToolTip { Copy bytes - - - Display Options - - Copy disasm @@ -2948,19 +2927,6 @@ QToolTip { Insert String - - - XRefs - - - XRefs - - - - - Add flag - - @@ -2974,8 +2940,8 @@ QToolTip { + -