cutter/src/widgets/DebugToolbar.cpp

199 lines
8.0 KiB
C++
Raw Normal View History

2018-06-12 08:43:14 +00:00
#include "DebugToolbar.h"
#include "MainWindow.h"
#include "dialogs/AttachProcDialog.h"
2018-06-12 08:43:14 +00:00
#include <QAction>
#include <QPainter>
#include <QMenu>
2018-10-03 11:30:12 +00:00
#include <QList>
2018-07-25 08:59:40 +00:00
#include <QFileInfo>
2018-06-12 08:43:14 +00:00
DebugToolbar::DebugToolbar(MainWindow *main, QWidget *parent) :
QToolBar(parent),
main(main)
{
setObjectName("debugToolbar");
2018-10-03 11:30:12 +00:00
setIconSize(QSize(20, 20));
// define icons
QIcon startDebugIcon = QIcon(":/img/icons/play_light_debug.svg");
QIcon startEmulIcon = QIcon(":/img/icons/play_light_emul.svg");
QIcon startAttachIcon = QIcon(":/img/icons/play_light_attach.svg");
QIcon stopIcon = QIcon(":/img/icons/media-stop_light.svg");
2018-06-12 08:43:14 +00:00
QIcon continueIcon = QIcon(":/img/icons/media-skip-forward_light.svg");
QIcon continueUntilMainIcon = QIcon(":/img/icons/continue_until_main.svg");
QIcon continueUntilCallIcon = QIcon(":/img/icons/continue_until_call.svg");
QIcon continueUntilSyscallIcon = QIcon(":/img/icons/continue_until_syscall.svg");
2018-06-12 08:43:14 +00:00
QIcon stepIcon = QIcon(":/img/icons/step_light.svg");
QIcon stepOverIcon = QIcon(":/img/icons/step_over_light.svg");
QIcon stepOutIcon = QIcon(":/img/icons/step_out_light.svg");
2018-10-03 11:30:12 +00:00
QIcon restartIcon = QIcon(":/img/icons/spin_light.svg");
2018-06-12 08:43:14 +00:00
2018-10-03 11:30:12 +00:00
// define actions
actionStart = new QAction(startDebugIcon, tr("Start debug"), parent);
actionStart->setShortcut(QKeySequence(Qt::Key_F9));
actionStartEmul = new QAction(startEmulIcon, tr("Start emulation"), parent);
actionAttach = new QAction(startAttachIcon, tr("Attach to process"), parent);
actionStop = new QAction(stopIcon, tr("Stop debug"), parent);
actionContinue = new QAction(continueIcon, tr("Continue"), parent);
actionContinue->setShortcut(QKeySequence(Qt::Key_F5));
actionContinueUntilMain = new QAction(continueUntilMainIcon, tr("Continue until main"), parent);
actionContinueUntilCall = new QAction(continueUntilCallIcon, tr("Continue until call"), parent);
2018-09-30 20:00:53 +00:00
actionContinueUntilSyscall = new QAction(continueUntilSyscallIcon, tr("Continue until syscall"),
parent);
actionStep = new QAction(stepIcon, tr("Step"), parent);
actionStep->setShortcut(QKeySequence(Qt::Key_F7));
actionStepOver = new QAction(stepOverIcon, tr("Step over"), parent);
actionStepOver->setShortcut(QKeySequence(Qt::Key_F8));
actionStepOut = new QAction(stepOutIcon, tr("Step out"), parent);
actionStepOut->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_F8));
QToolButton *startButton = new QToolButton;
startButton->setPopupMode(QToolButton::MenuButtonPopup);
connect(startButton, &QToolButton::triggered, startButton, &QToolButton::setDefaultAction);
QMenu *startMenu = new QMenu;
2018-10-03 11:30:12 +00:00
// only emulation is currently allowed
2018-07-26 09:46:17 +00:00
// startMenu->addAction(actionStart);
// startMenu->addAction(actionAttach);
// startButton->setDefaultAction(actionStart);
2018-10-03 11:30:12 +00:00
startMenu->addAction(actionStartEmul);
2018-07-26 09:46:17 +00:00
startButton->setDefaultAction(actionStartEmul);
startButton->setMenu(startMenu);
QToolButton *continueUntilButton = new QToolButton;
continueUntilButton->setPopupMode(QToolButton::MenuButtonPopup);
2018-09-30 20:00:53 +00:00
connect(continueUntilButton, &QToolButton::triggered, continueUntilButton,
&QToolButton::setDefaultAction);
QMenu *continueUntilMenu = new QMenu;
continueUntilMenu->addAction(actionContinueUntilMain);
continueUntilMenu->addAction(actionContinueUntilCall);
continueUntilMenu->addAction(actionContinueUntilSyscall);
continueUntilButton->setMenu(continueUntilMenu);
continueUntilButton->setDefaultAction(actionContinueUntilMain);
2018-10-03 11:30:12 +00:00
// define toolbar widgets and actions
addWidget(startButton);
addAction(actionStop);
2018-06-12 08:43:14 +00:00
addAction(actionContinue);
2018-10-03 11:30:12 +00:00
actionAllContinues = addWidget(continueUntilButton);
2018-06-12 08:43:14 +00:00
addAction(actionStep);
addAction(actionStepOver);
addAction(actionStepOut);
2018-06-12 08:43:14 +00:00
2018-10-03 11:30:12 +00:00
allActions = {actionStop, actionAllContinues, actionContinue, actionContinueUntilCall, actionContinueUntilMain, actionContinueUntilSyscall, actionStep, actionStepOut, actionStepOver};
// hide allactions
setAllActionsVisible(false);
connect(actionStop, &QAction::triggered, Core(), &CutterCore::stopDebug);
2018-09-30 20:00:53 +00:00
connect(actionStop, &QAction::triggered, [ = ]() {
actionContinue->setVisible(true);
actionStart->setVisible(true);
actionStartEmul->setVisible(true);
actionAttach->setVisible(true);
actionContinueUntilMain->setVisible(true);
actionStepOut->setVisible(true);
this->colorToolbar(false);
2018-10-03 11:30:12 +00:00
actionStop->setText("Stop session");
actionStart->setText("Start debug");
actionStartEmul->setText("Start emulation");
actionStart->setIcon(startDebugIcon);
actionStartEmul->setIcon(startEmulIcon);
colorToolbar(false);
2018-10-03 11:30:12 +00:00
setAllActionsVisible(false);
});
connect(actionStep, &QAction::triggered, Core(), &CutterCore::stepDebug);
2018-10-03 11:30:12 +00:00
connect(actionStart, &QAction::triggered, [ = ]() {
setAllActionsVisible(true);
QString filename = Core()->getConfig("file.path").split(" ").first();
2018-07-25 08:59:40 +00:00
QFileInfo info(filename);
if (!Core()->currentlyDebugging && !info.isExecutable()) {
2018-07-25 08:59:40 +00:00
QMessageBox msgBox;
msgBox.setText(QString("File '%1' does not have executable permissions.").arg(filename));
msgBox.exec();
return;
}
colorToolbar(true);
actionAttach->setVisible(false);
actionStartEmul->setVisible(false);
2018-10-03 11:30:12 +00:00
actionStart->setText("Restart program");
actionStart->setIcon(restartIcon);
2018-07-25 08:59:40 +00:00
Core()->startDebug();
});
2018-10-03 11:30:12 +00:00
connect(actionAttach, &QAction::triggered, this, &DebugToolbar::attachProcessDialog);
connect(actionStartEmul, &QAction::triggered, Core(), &CutterCore::startEmulation);
2018-09-30 20:00:53 +00:00
connect(actionStartEmul, &QAction::triggered, [ = ]() {
2018-10-03 11:30:12 +00:00
setAllActionsVisible(true);
actionStart->setVisible(false);
actionAttach->setVisible(false);
actionContinueUntilMain->setVisible(false);
actionStepOut->setVisible(false);
continueUntilButton->setDefaultAction(actionContinueUntilSyscall);
2018-10-03 11:30:12 +00:00
actionStartEmul->setText("Restart emulation");
actionStartEmul->setIcon(restartIcon);
actionStop->setText("Stop emulation");
colorToolbar(true);
});
connect(actionStepOver, &QAction::triggered, Core(), &CutterCore::stepOverDebug);
connect(actionStepOut, &QAction::triggered, Core(), &CutterCore::stepOutDebug);
connect(actionContinue, &QAction::triggered, Core(), &CutterCore::continueDebug);
connect(actionContinueUntilMain, &QAction::triggered, this, &DebugToolbar::continueUntilMain);
connect(actionContinueUntilCall, &QAction::triggered, Core(), &CutterCore::continueUntilCall);
connect(actionContinueUntilSyscall, &QAction::triggered, Core(), &CutterCore::continueUntilSyscall);
2018-06-12 08:43:14 +00:00
}
void DebugToolbar::continueUntilMain()
{
Core()->continueUntilDebug(tr("main"));
}
void DebugToolbar::colorToolbar(bool p)
{
if (p) {
setStyleSheet("QToolBar {background: green;}");
} else {
setStyleSheet("");
}
}
void DebugToolbar::attachProcessDialog()
{
AttachProcDialog *dialog = new AttachProcDialog(this);
bool success = false;
while (!success) {
success = true;
if (dialog->exec()) {
int pid = dialog->getPID();
if (pid >= 0) {
attachProcess(pid);
} else {
success = false;
QMessageBox msgBox;
msgBox.setText("Error attaching. No process selected!");
msgBox.exec();
}
}
}
delete dialog;
}
void DebugToolbar::attachProcess(int pid)
{
// hide unwanted buttons
2018-10-03 11:30:12 +00:00
setAllActionsVisible(true);
colorToolbar(true);
actionStart->setVisible(false);
actionStartEmul->setVisible(false);
actionStop->setText("Detach from process");
// attach
Core()->attachDebug(pid);
2018-10-03 11:30:12 +00:00
}
void DebugToolbar::setAllActionsVisible(bool visible)
{
for (QAction *action : allActions) {
action->setVisible(visible);
}
2018-06-12 08:43:14 +00:00
}