src/platform/qt/GDBWindow.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "GDBWindow.h"
7
8#include <QGridLayout>
9#include <QGroupBox>
10#include <QLabel>
11#include <QLineEdit>
12#include <QMessageBox>
13#include <QPushButton>
14#include <QHBoxLayout>
15#include <QVBoxLayout>
16
17#include "GDBController.h"
18#include "utils.h"
19
20using namespace QGBA;
21
22GDBWindow::GDBWindow(GDBController* controller, QWidget* parent)
23 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
24 , m_gdbController(controller)
25{
26 setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
27 QVBoxLayout* mainSegment = new QVBoxLayout;
28 setLayout(mainSegment);
29 QGroupBox* settings = new QGroupBox(tr("Server settings"));
30 mainSegment->addWidget(settings);
31
32 QGridLayout* settingsGrid = new QGridLayout;
33 settings->setLayout(settingsGrid);
34
35 QLabel* portLabel = new QLabel(tr("Local port"));
36 settingsGrid->addWidget(portLabel, 0, 0, Qt::AlignRight);
37 QLabel* bindAddressLabel = new QLabel(tr("Bind address"));
38 settingsGrid->addWidget(bindAddressLabel, 1, 0, Qt::AlignRight);
39
40 m_portEdit = new QLineEdit("2345");
41 m_portEdit->setMaxLength(5);
42 connect(m_portEdit, &QLineEdit::textChanged, this, &GDBWindow::portChanged);
43 settingsGrid->addWidget(m_portEdit, 0, 1, Qt::AlignLeft);
44
45 m_bindAddressEdit = new QLineEdit("0.0.0.0");
46 m_bindAddressEdit->setMaxLength(15);
47 connect(m_bindAddressEdit, &QLineEdit::textChanged, this, &GDBWindow::bindAddressChanged);
48 settingsGrid->addWidget(m_bindAddressEdit, 1, 1, Qt::AlignLeft);
49
50 QHBoxLayout* buttons = new QHBoxLayout;
51
52 m_startStopButton = new QPushButton;
53 buttons->addWidget(m_startStopButton);
54
55 m_breakButton = new QPushButton;
56 m_breakButton->setText(tr("Break"));
57 buttons->addWidget(m_breakButton);
58
59 mainSegment->addLayout(buttons);
60
61 connect(m_gdbController, &GDBController::listening, this, &GDBWindow::started);
62 connect(m_gdbController, &GDBController::listenFailed, this, &GDBWindow::failed);
63 connect(m_breakButton, &QAbstractButton::clicked, controller, &DebuggerController::breakInto);
64
65 if (m_gdbController->isAttached()) {
66 started();
67 } else {
68 stopped();
69 }
70}
71
72void GDBWindow::portChanged(const QString& portString) {
73 bool ok = false;
74 ushort port = portString.toUShort(&ok);
75 if (ok) {
76 m_gdbController->setPort(port);
77 }
78}
79
80void GDBWindow::bindAddressChanged(const QString& bindAddressString) {
81 QHostAddress qaddress;
82 Address address;
83
84 if (!qaddress.setAddress(bindAddressString)) {
85 return;
86 }
87
88 convertAddress(&qaddress, &address);
89 m_gdbController->setBindAddress(address);
90}
91
92void GDBWindow::started() {
93 m_portEdit->setEnabled(false);
94 m_bindAddressEdit->setEnabled(false);
95 m_startStopButton->setText(tr("Stop"));
96 m_breakButton->setEnabled(true);
97 disconnect(m_startStopButton, &QAbstractButton::clicked, m_gdbController, &GDBController::listen);
98 connect(m_startStopButton, &QAbstractButton::clicked, m_gdbController, &DebuggerController::detach);
99 connect(m_startStopButton, &QAbstractButton::clicked, this, &GDBWindow::stopped);
100}
101
102void GDBWindow::stopped() {
103 m_portEdit->setEnabled(true);
104 m_bindAddressEdit->setEnabled(true);
105 m_startStopButton->setText(tr("Start"));
106 m_breakButton->setEnabled(false);
107 disconnect(m_startStopButton, &QAbstractButton::clicked, m_gdbController, &DebuggerController::detach);
108 disconnect(m_startStopButton, &QAbstractButton::clicked, this, &GDBWindow::stopped);
109 connect(m_startStopButton, &QAbstractButton::clicked, m_gdbController, &GDBController::listen);
110}
111
112void GDBWindow::failed() {
113 QMessageBox* failure = new QMessageBox(QMessageBox::Warning, tr("Crash"), tr("Could not start GDB server"),
114 QMessageBox::Ok, this, Qt::Sheet);
115 failure->setAttribute(Qt::WA_DeleteOnClose);
116 failure->show();
117}