src/platform/qt/GDBController.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 "GDBController.h"
7
8#include "GameController.h"
9
10using namespace QGBA;
11
12GDBController::GDBController(GameController* controller, QObject* parent)
13 : QObject(parent)
14 , m_gameController(controller)
15 , m_port(2345)
16 , m_bindAddress({ IPV4, 0 })
17{
18 GDBStubCreate(&m_gdbStub);
19}
20
21ushort GDBController::port() {
22 return m_port;
23}
24
25bool GDBController::isAttached() {
26 return m_gameController->debugger() == &m_gdbStub.d;
27}
28
29void GDBController::setPort(ushort port) {
30 m_port = port;
31}
32
33void GDBController::setBindAddress(uint32_t bindAddress) {
34 m_bindAddress.version = IPV4;
35 m_bindAddress.ipv4 = htonl(bindAddress);
36}
37
38void GDBController::attach() {
39 if (isAttached()) {
40 return;
41 }
42 m_gameController->setDebugger(&m_gdbStub.d);
43 if (m_gameController->isLoaded()) {
44 DebuggerEnter(&m_gdbStub.d, DEBUGGER_ENTER_ATTACHED, 0);
45 } else {
46 QObject::disconnect(m_autoattach);
47 m_autoattach = connect(m_gameController, &GameController::gameStarted, [this]() {
48 QObject::disconnect(m_autoattach);
49 DebuggerEnter(&m_gdbStub.d, DEBUGGER_ENTER_ATTACHED, 0);
50 });
51 }
52}
53
54void GDBController::detach() {
55 QObject::disconnect(m_autoattach);
56 if (!isAttached()) {
57 return;
58 }
59 m_gameController->threadInterrupt();
60 GDBStubShutdown(&m_gdbStub);
61 m_gameController->setDebugger(nullptr);
62 m_gameController->threadContinue();
63}
64
65void GDBController::listen() {
66 m_gameController->threadInterrupt();
67 if (!isAttached()) {
68 attach();
69 }
70 if (GDBStubListen(&m_gdbStub, m_port, &m_bindAddress)) {
71 emit listening();
72 } else {
73 detach();
74 emit listenFailed();
75 }
76 m_gameController->threadContinue();
77}
78
79void GDBController::breakInto() {
80 if (!isAttached()) {
81 return;
82 }
83 m_gameController->threadInterrupt();
84 DebuggerEnter(&m_gdbStub.d, DEBUGGER_ENTER_MANUAL, 0);
85 m_gameController->threadContinue();
86}