From 5114d756470ff70b0ce8c6f3ff98000462aaef35 Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Wed, 2 Sep 2015 08:56:38 -0400
Subject: [PATCH 01/14] Implement gdbstub

---
 src/citra/citra.cpp                           |   3 +
 src/citra/config.cpp                          |   4 +
 src/citra/default_ini.h                       |   5 +
 src/citra_qt/config.cpp                       |  10 +
 src/citra_qt/main.cpp                         |  20 +
 src/citra_qt/main.h                           |   1 +
 src/citra_qt/main.ui                          |   9 +
 src/common/logging/backend.cpp                |   1 +
 src/common/logging/log.h                      |   1 +
 src/core/CMakeLists.txt                       |   2 +
 .../arm/dyncom/arm_dyncom_interpreter.cpp     |  41 +-
 src/core/arm/skyeye_common/armstate.cpp       |  35 +
 src/core/arm/skyeye_common/armstate.h         |   2 +
 src/core/core.cpp                             |  17 +
 src/core/gdbstub/gdbstub.cpp                  | 940 ++++++++++++++++++
 src/core/gdbstub/gdbstub.h                    |  89 ++
 src/core/settings.h                           |   5 +
 src/core/system.cpp                           |   6 +
 18 files changed, 1182 insertions(+), 9 deletions(-)
 create mode 100644 src/core/gdbstub/gdbstub.cpp
 create mode 100644 src/core/gdbstub/gdbstub.h

diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index 46f4a07c94..b368653950 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -30,6 +30,8 @@
 
 #include "video_core/video_core.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 
 static void PrintHelp()
 {
@@ -72,6 +74,7 @@ int main(int argc, char **argv) {
     Config config;
     log_filter.ParseFilterString(Settings::values.log_filter);
 
+    GDBStub::SetServerPort(static_cast<u32>(Settings::values.gdbstub_port));
 
     EmuWindow_GLFW* emu_window = new EmuWindow_GLFW;
 
diff --git a/src/citra/config.cpp b/src/citra/config.cpp
index 8a98bda877..af343e9fe3 100644
--- a/src/citra/config.cpp
+++ b/src/citra/config.cpp
@@ -75,6 +75,10 @@ void Config::ReadValues() {
 
     // Miscellaneous
     Settings::values.log_filter = glfw_config->Get("Miscellaneous", "log_filter", "*:Info");
+
+    // GDBStubebugging
+    Settings::values.use_gdbstub = glfw_config->GetBoolean("Debugging", "use_gdbstub", false);
+    Settings::values.gdbstub_port = glfw_config->GetInteger("Debugging", "gdbstub_port", 24689);
 }
 
 void Config::Reload() {
diff --git a/src/citra/default_ini.h b/src/citra/default_ini.h
index 7e5d497298..5ba40a8edb 100644
--- a/src/citra/default_ini.h
+++ b/src/citra/default_ini.h
@@ -66,6 +66,11 @@ region_value =
 # A filter which removes logs below a certain logging level.
 # Examples: *:Debug Kernel.SVC:Trace Service.*:Critical
 log_filter = *:Info
+
+[Debugging]
+# Port for listening to GDB connections.
+use_gdbstub=false
+gdbstub_port=24689
 )";
 
 }
diff --git a/src/citra_qt/config.cpp b/src/citra_qt/config.cpp
index 1f4981ce1d..8e247ff5c0 100644
--- a/src/citra_qt/config.cpp
+++ b/src/citra_qt/config.cpp
@@ -62,6 +62,11 @@ void Config::ReadValues() {
     qt_config->beginGroup("Miscellaneous");
     Settings::values.log_filter = qt_config->value("log_filter", "*:Info").toString().toStdString();
     qt_config->endGroup();
+
+    qt_config->beginGroup("Debugging");
+    Settings::values.use_gdbstub = qt_config->value("use_gdbstub", false).toBool();
+    Settings::values.gdbstub_port = qt_config->value("gdbstub_port", 24689).toInt();
+    qt_config->endGroup();
 }
 
 void Config::SaveValues() {
@@ -97,6 +102,11 @@ void Config::SaveValues() {
     qt_config->beginGroup("Miscellaneous");
     qt_config->setValue("log_filter", QString::fromStdString(Settings::values.log_filter));
     qt_config->endGroup();
+
+    qt_config->beginGroup("Debugging");
+    qt_config->setValue("use_gdbstub", Settings::values.use_gdbstub);
+    qt_config->setValue("gdbstub_port", Settings::values.gdbstub_port);
+    qt_config->endGroup();
 }
 
 void Config::Reload() {
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index 01841b33c2..e032cf6397 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -47,6 +47,12 @@
 
 #include "video_core/video_core.h"
 
+#ifdef USE_GDBSTUB
+#include "core/gdbstub/gdbstub.h"
+#endif
+
+#include "core/gdbstub/gdbstub.h"
+
 GMainWindow::GMainWindow() : emu_thread(nullptr)
 {
     Pica::g_debug_context = Pica::DebugContext::Construct();
@@ -137,6 +143,15 @@ GMainWindow::GMainWindow() : emu_thread(nullptr)
     microProfileDialog->setVisible(settings.value("microProfileDialogVisible").toBool());
     settings.endGroup();
 
+#ifdef USE_GDBSTUB
+    Gdbstub::SetServerPort(static_cast<u32>(Settings::values.gdbstub_port));
+#endif
+
+    ui.action_Use_Gdbstub->setChecked(Settings::values.use_gdbstub);
+    SetGdbstubEnabled(ui.action_Use_Gdbstub->isChecked());
+
+    GDBStub::SetServerPort(static_cast<u32>(Settings::values.gdbstub_port));
+
     ui.action_Use_Hardware_Renderer->setChecked(Settings::values.use_hw_renderer);
     SetHardwareRendererEnabled(ui.action_Use_Hardware_Renderer->isChecked());
 
@@ -167,6 +182,7 @@ GMainWindow::GMainWindow() : emu_thread(nullptr)
     connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame()));
     connect(ui.action_Use_Hardware_Renderer, SIGNAL(triggered(bool)), this, SLOT(SetHardwareRendererEnabled(bool)));
     connect(ui.action_Use_Shader_JIT, SIGNAL(triggered(bool)), this, SLOT(SetShaderJITEnabled(bool)));
+    connect(ui.action_Use_Gdbstub, SIGNAL(triggered(bool)), this, SLOT(SetGdbstubEnabled(bool)));
     connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode()));
     connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog()));
 
@@ -414,6 +430,10 @@ void GMainWindow::SetHardwareRendererEnabled(bool enabled) {
     VideoCore::g_hw_renderer_enabled = enabled;
 }
 
+void GMainWindow::SetGdbstubEnabled(bool enabled) {
+    GDBStub::ToggleServer(enabled);
+}
+
 void GMainWindow::SetShaderJITEnabled(bool enabled) {
     VideoCore::g_shader_jit_enabled = enabled;
 }
diff --git a/src/citra_qt/main.h b/src/citra_qt/main.h
index 32523fded6..daa3d1c95e 100644
--- a/src/citra_qt/main.h
+++ b/src/citra_qt/main.h
@@ -94,6 +94,7 @@ private slots:
     void OnConfigure();
     void OnDisplayTitleBars(bool);
     void SetHardwareRendererEnabled(bool);
+    void SetGdbstubEnabled(bool);
     void SetShaderJITEnabled(bool);
     void ToggleWindowMode();
 
diff --git a/src/citra_qt/main.ui b/src/citra_qt/main.ui
index 1ba700a3a6..88d04439a0 100644
--- a/src/citra_qt/main.ui
+++ b/src/citra_qt/main.ui
@@ -74,6 +74,7 @@
     <addaction name="separator"/>
     <addaction name="action_Use_Hardware_Renderer"/>
     <addaction name="action_Use_Shader_JIT"/>
+    <addaction name="action_Use_Gdbstub"/>
     <addaction name="action_Configure"/>
    </widget>
    <widget class="QMenu" name="menu_View">
@@ -169,6 +170,14 @@
     <string>Use Shader JIT</string>
    </property>
   </action>
+  <action name="action_Use_Gdbstub">
+    <property name="checkable">
+      <bool>true</bool>
+    </property>
+    <property name="text">
+      <string>Use Gdbstub</string>
+    </property>
+  </action>
   <action name="action_Configure">
    <property name="text">
     <string>Configure ...</string>
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 92e8e742d8..21a9ae8d0d 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -29,6 +29,7 @@ namespace Log {
         SUB(Debug, Emulated) \
         SUB(Debug, GPU) \
         SUB(Debug, Breakpoint) \
+        SUB(Debug, GDBStub) \
         CLS(Kernel) \
         SUB(Kernel, SVC) \
         CLS(Service) \
diff --git a/src/common/logging/log.h b/src/common/logging/log.h
index 5fd3bd7f53..43f0c59e47 100644
--- a/src/common/logging/log.h
+++ b/src/common/logging/log.h
@@ -43,6 +43,7 @@ enum class Class : ClassType {
     Debug_Emulated,             ///< Debug messages from the emulated programs
     Debug_GPU,                  ///< GPU debugging tools
     Debug_Breakpoint,           ///< Logging breakpoints and watchpoints
+    Debug_GDBStub,              ///< GDB Stub
     Kernel,                     ///< The HLE implementation of the CTR kernel
     Kernel_SVC,                 ///< Kernel system calls
     Service,                    ///< HLE implementation of system services. Each major service
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index c17290b9bb..861b711c7a 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -22,6 +22,7 @@ set(SRCS
             file_sys/archive_systemsavedata.cpp
             file_sys/disk_archive.cpp
             file_sys/ivfc_archive.cpp
+            gdbstub/gdbstub.cpp
             hle/config_mem.cpp
             hle/hle.cpp
             hle/applets/applet.cpp
@@ -149,6 +150,7 @@ set(HEADERS
             file_sys/disk_archive.h
             file_sys/file_backend.h
             file_sys/ivfc_archive.h
+            gdbstub/gdbstub.h
             hle/config_mem.h
             hle/function_wrappers.h
             hle/hle.h
diff --git a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
index fbd6f94f93..8293f4c601 100644
--- a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
@@ -23,6 +23,8 @@
 #include "core/arm/skyeye_common/armsupp.h"
 #include "core/arm/skyeye_common/vfp/vfp.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 Common::Profiling::TimingCategory profile_execute("DynCom::Execute");
 Common::Profiling::TimingCategory profile_decode("DynCom::Decode");
 
@@ -3548,6 +3550,7 @@ static int InterpreterTranslate(ARMul_State* cpu, int& bb_start, u32 addr) {
             CITRA_IGNORE_EXIT(-1);
         }
         inst_base = arm_instruction_trans[idx](inst, idx);
+
 translated:
         phys_addr += inst_size;
 
@@ -3580,6 +3583,8 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     Common::Profiling::ScopeTimer timer_execute(profile_execute);
     MICROPROFILE_SCOPE(DynCom_Execute);
 
+    int breakpoint_offset = -1;
+
     #undef RM
     #undef RS
 
@@ -3604,15 +3609,27 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     #define INC_PC(l)   ptr += sizeof(arm_inst) + l
     #define INC_PC_STUB ptr += sizeof(arm_inst)
 
+#define GDB_BP_CHECK \
+    cpu->Cpsr &= ~(1 << 5); \
+    cpu->Cpsr |= cpu->TFlag << 5; \
+    if (GDBStub::g_server_enabled) { \
+        if (GDBStub::IsMemoryBreak() || PC == breakpoint_offset) { \
+            GDBStub::Break(); \
+            goto END; \
+        } \
+    }
+
 // GCC and Clang have a C++ extension to support a lookup table of labels. Otherwise, fallback to a
 // clunky switch statement.
 #if defined __GNUC__ || defined __clang__
 #define GOTO_NEXT_INST \
+    GDB_BP_CHECK; \
     if (num_instrs >= cpu->NumInstrsToExecute) goto END; \
     num_instrs++; \
     goto *InstLabel[inst_base->idx]
 #else
 #define GOTO_NEXT_INST \
+    GDB_BP_CHECK; \
     if (num_instrs >= cpu->NumInstrsToExecute) goto END; \
     num_instrs++; \
     switch(inst_base->idx) { \
@@ -3878,6 +3895,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     unsigned int addr;
     unsigned int num_instrs = 0;
 
+
     int ptr;
 
     LOAD_NZCVT;
@@ -3903,6 +3921,11 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
                 goto END;
         }
 
+        // Find breakpoint if one exists within the block
+        if (GDBStub::g_server_enabled && GDBStub::IsConnected()) {
+            breakpoint_offset = GDBStub::GetNextBreakpointFromAddress(cpu->Reg[15], GDBStub::BreakpointType::Execute);
+        }
+
         inst_base = (arm_inst *)&inst_buf[ptr];
         GOTO_NEXT_INST;
     }
@@ -4454,7 +4477,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
 
-            cpu->Reg[BITS(inst_cream->inst, 12, 15)] = Memory::Read8(addr);
+            cpu->Reg[BITS(inst_cream->inst, 12, 15)] = cpu->ReadMemory8(addr);
 
             if (BITS(inst_cream->inst, 12, 15) == 15) {
                 INC_PC(sizeof(ldst_inst));
@@ -4472,7 +4495,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
 
-            cpu->Reg[BITS(inst_cream->inst, 12, 15)] = Memory::Read8(addr);
+            cpu->Reg[BITS(inst_cream->inst, 12, 15)] = cpu->ReadMemory8(addr);
 
             if (BITS(inst_cream->inst, 12, 15) == 15) {
                 INC_PC(sizeof(ldst_inst));
@@ -4531,7 +4554,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
 
             cpu->SetExclusiveMemoryAddress(read_addr);
 
-            RD = Memory::Read8(read_addr);
+            RD = cpu->ReadMemory8(read_addr);
             if (inst_cream->Rd == 15) {
                 INC_PC(sizeof(generic_arm_inst));
                 goto DISPATCH;
@@ -4604,7 +4627,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
         if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
-            unsigned int value = Memory::Read8(addr);
+            unsigned int value = cpu->ReadMemory8(addr);
             if (BIT(value, 7)) {
                 value |= 0xffffff00;
             }
@@ -6027,7 +6050,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
             unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
-            Memory::Write8(addr, value);
+            cpu->WriteMemory8(addr, value);
         }
         cpu->Reg[15] += cpu->GetInstructionSize();
         INC_PC(sizeof(ldst_inst));
@@ -6040,7 +6063,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
             unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
-            Memory::Write8(addr, value);
+            cpu->WriteMemory8(addr, value);
         }
         cpu->Reg[15] += cpu->GetInstructionSize();
         INC_PC(sizeof(ldst_inst));
@@ -6091,7 +6114,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
 
             if (cpu->IsExclusiveMemoryAccess(write_addr)) {
                 cpu->UnsetExclusiveMemoryAddress();
-                Memory::Write8(write_addr, cpu->Reg[inst_cream->Rm]);
+                cpu->WriteMemory8(write_addr, cpu->Reg[inst_cream->Rm]);
                 RD = 0;
             } else {
                 // Failed to write due to mutex access
@@ -6250,8 +6273,8 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
         if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
             swp_inst* inst_cream = (swp_inst*)inst_base->component;
             addr = RN;
-            unsigned int value = Memory::Read8(addr);
-            Memory::Write8(addr, (RM & 0xFF));
+            unsigned int value = cpu->ReadMemory8(addr);
+            cpu->WriteMemory8(addr, (RM & 0xFF));
             RD = value;
         }
         cpu->Reg[15] += cpu->GetInstructionSize();
diff --git a/src/core/arm/skyeye_common/armstate.cpp b/src/core/arm/skyeye_common/armstate.cpp
index 0491717dc2..2d814345ad 100644
--- a/src/core/arm/skyeye_common/armstate.cpp
+++ b/src/core/arm/skyeye_common/armstate.cpp
@@ -7,6 +7,7 @@
 #include "core/memory.h"
 #include "core/arm/skyeye_common/armstate.h"
 #include "core/arm/skyeye_common/vfp/vfp.h"
+#include "core/gdbstub/gdbstub.h"
 
 ARMul_State::ARMul_State(PrivilegeMode initial_mode)
 {
@@ -185,8 +186,25 @@ void ARMul_State::ResetMPCoreCP15Registers()
     CP15[CP15_TLB_DEBUG_CONTROL] = 0x00000000;
 }
 
+static void CheckMemoryBreakpoint(u32 address, GDBStub::BreakpointType type)
+{
+    if (GDBStub::g_server_enabled && GDBStub::CheckBreakpoint(address, type)) {
+        LOG_DEBUG(Debug, "Found memory breakpoint @ %08x", address);
+        GDBStub::Break(true);
+    }
+}
+
+u8 ARMul_State::ReadMemory8(u32 address) const
+{
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
+
+    return Memory::Read8(address);
+}
+
 u16 ARMul_State::ReadMemory16(u32 address) const
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
+
     u16 data = Memory::Read16(address);
 
     if (InBigEndianMode())
@@ -197,6 +215,8 @@ u16 ARMul_State::ReadMemory16(u32 address) const
 
 u32 ARMul_State::ReadMemory32(u32 address) const
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
+
     u32 data = Memory::Read32(address);
 
     if (InBigEndianMode())
@@ -207,6 +227,8 @@ u32 ARMul_State::ReadMemory32(u32 address) const
 
 u64 ARMul_State::ReadMemory64(u32 address) const
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
+
     u64 data = Memory::Read64(address);
 
     if (InBigEndianMode())
@@ -215,8 +237,17 @@ u64 ARMul_State::ReadMemory64(u32 address) const
     return data;
 }
 
+void ARMul_State::WriteMemory8(u32 address, u8 data)
+{
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
+
+    Memory::Write8(address, data);
+}
+
 void ARMul_State::WriteMemory16(u32 address, u16 data)
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
+
     if (InBigEndianMode())
         data = Common::swap16(data);
 
@@ -225,6 +256,8 @@ void ARMul_State::WriteMemory16(u32 address, u16 data)
 
 void ARMul_State::WriteMemory32(u32 address, u32 data)
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
+
     if (InBigEndianMode())
         data = Common::swap32(data);
 
@@ -233,6 +266,8 @@ void ARMul_State::WriteMemory32(u32 address, u32 data)
 
 void ARMul_State::WriteMemory64(u32 address, u64 data)
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
+
     if (InBigEndianMode())
         data = Common::swap64(data);
 
diff --git a/src/core/arm/skyeye_common/armstate.h b/src/core/arm/skyeye_common/armstate.h
index b364e26210..c0536c02f5 100644
--- a/src/core/arm/skyeye_common/armstate.h
+++ b/src/core/arm/skyeye_common/armstate.h
@@ -153,9 +153,11 @@ public:
 
     // Reads/writes data in big/little endian format based on the
     // state of the E (endian) bit in the APSR.
+    u8 ReadMemory8(u32 address) const;
     u16 ReadMemory16(u32 address) const;
     u32 ReadMemory32(u32 address) const;
     u64 ReadMemory64(u32 address) const;
+    void WriteMemory8(u32 address, u8 data);
     void WriteMemory16(u32 address, u16 data);
     void WriteMemory32(u32 address, u32 data);
     void WriteMemory64(u32 address, u64 data);
diff --git a/src/core/core.cpp b/src/core/core.cpp
index dddc16708e..219b03af49 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -13,6 +13,8 @@
 #include "core/hle/kernel/thread.h"
 #include "core/hw/hw.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 namespace Core {
 
 ARM_Interface*     g_app_core = nullptr;  ///< ARM11 application core
@@ -20,6 +22,21 @@ ARM_Interface*     g_sys_core = nullptr;  ///< ARM11 system (OS) core
 
 /// Run the core CPU loop
 void RunLoop(int tight_loop) {
+    if (GDBStub::g_server_enabled) {
+        GDBStub::HandlePacket();
+
+        // If the loop is halted and we want to step, use a tiny (1) number of instructions to execute.
+        // Otherwise get out of the loop function.
+        if (GDBStub::GetCpuHaltFlag()) {
+            if (GDBStub::GetCpuStepFlag()) {
+                GDBStub::SetCpuStepFlag(false);
+                tight_loop = 1;
+            } else {
+                return;
+            }
+        }
+    }
+
     // If we don't have a currently active thread then don't execute instructions,
     // instead advance to the next event and try to yield to the next thread
     if (Kernel::GetCurrentThread() == nullptr) {
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
new file mode 100644
index 0000000000..ced1c54f56
--- /dev/null
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -0,0 +1,940 @@
+// Copyright 2013 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
+
+#include <csignal>
+#include <cstdarg>
+#include <cstdio>
+#include <cstring>
+#include <fcntl.h>
+#include <map>
+#include <numeric>
+
+#ifdef _MSC_VER
+#include <WinSock2.h>
+#include <ws2tcpip.h>
+#include <common/x64/abi.h>
+#include <io.h>
+#include <iphlpapi.h>
+#define SHUT_RDWR 2
+#else
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#endif
+
+#include "common/logging/log.h"
+#include "common/string_util.h"
+#include <core/arm/arm_interface.h>
+#include "core/core.h"
+#include "core/memory.h"
+#include "gdbstub.h"
+
+const int GDB_BUFFER_SIZE = 10000;
+
+const char GDB_STUB_START = '$';
+const char GDB_STUB_END = '#';
+const char GDB_STUB_ACK = '+';
+const char GDB_STUB_NACK = '-';
+
+#ifndef SIGTRAP
+const u32 SIGTRAP = 5;
+#endif
+
+#ifndef SIGTERM
+const u32 SIGTERM = 15;
+#endif
+
+#ifndef MSG_WAITALL
+const u32 MSG_WAITALL = 8;
+#endif
+
+const u32 R0_REGISTER = 0;
+const u32 R15_REGISTER = 15;
+const u32 CSPR_REGISTER = 25;
+
+namespace GDBStub {
+
+static int gdbserver_socket = -1;
+
+static u8 command_buffer[GDB_BUFFER_SIZE];
+static u32 command_length;
+
+static u32 latest_signal = 0;
+static u32 send_signal = 0;
+static u32 step_break = 0;
+static bool memory_break = false;
+
+// Binding to a port within the reserved ports range (0-1023) requires root permissions,
+// so default to a port outside of that range.
+static u16 gdbstub_port = 24689;
+
+static bool halt_loop = true;
+static bool step_loop = false;
+std::atomic<bool> g_server_enabled(false);
+
+#ifdef _WIN32
+WSADATA InitData;
+#endif
+
+struct Breakpoint {
+    bool active;
+    PAddr addr;
+    u32 len;
+};
+
+static std::map<u32, Breakpoint> breakpoints_execute;
+static std::map<u32, Breakpoint> breakpoints_read;
+static std::map<u32, Breakpoint> breakpoints_write;
+
+/**
+ * Turns hex string character into the equivalent byte.
+ *
+ * @param hex Input hex character to be turned into byte.
+ */
+static u8 HexCharToValue(u8 hex) {
+    if (hex >= '0' && hex <= '9') {
+        return hex - '0';
+    } else if (hex >= 'a' && hex <= 'f') {
+        return hex - 'a' + 0xA;
+    } else if (hex >= 'A' && hex <= 'F') {
+        return hex - 'A' + 0xA;
+    }
+
+    LOG_ERROR(Debug_GDBStub, "Invalid nibble: %c (%02x)\n", hex, hex);
+    return 0;
+}
+
+/**
+ * Turn nibble of byte into hex string character.
+ *
+ * @param n Nibble to be turned into hex character.
+ */
+static u8 NibbleToHex(u8 n) {
+    n &= 0xF;
+    if (n < 0xA) {
+        return '0' + n;
+    } else {
+        return 'A' + n - 0xA;
+    }
+}
+
+/**
+ * Converts input array of u8 bytes into their equivalent hex string characters.
+ *
+ * @param dest Pointer to buffer to store output hex string characters.
+ * @param src Pointer to array of u8 bytes.
+ * @param len Length of src array.
+ */
+static void MemToHex(u8* dest, u8* src, u32 len) {
+    while (len-- > 0) {
+        u8 tmp = *src++;
+        *dest++ = NibbleToHex(tmp >> 4);
+        *dest++ = NibbleToHex(tmp);
+    }
+}
+
+/**
+ * Converts input hex string characters into an array of equivalent of u8 bytes.
+ *
+ * @param dest Pointer to buffer to store u8 bytes.
+ * @param src Pointer to array of output hex string characters.
+ * @param len Length of src array.
+ */
+static void HexToMem(u8* dest, u8* src, u32 len) {
+    while (len-- > 0) {
+        *dest++ = (HexCharToValue(src[0]) << 4) | HexCharToValue(src[1]);
+        src += 2;
+    }
+}
+
+/**
+ * Convert a u32 into a hex string.
+ *
+ * @param dest Pointer to buffer to store output hex string characters.
+ */
+static void IntToHex(u8* dest, u32 v) {
+    for (int i = 0; i < 8; i += 2) {
+        dest[i + 1] = NibbleToHex(v >> (4 * i));
+        dest[i] = NibbleToHex(v >> (4 * (i + 1)));
+    }
+}
+
+/**
+ * Convert a hex string into a u32.
+ *
+ * @param src Pointer to hex string.
+ */
+static u32 HexToInt(u8* src) {
+    u32 output = 0;
+
+    for (int i = 0; i < 8; i += 2) {
+        output = (output << 4) | HexCharToValue(src[7 - i - 1]);
+        output = (output << 4) | HexCharToValue(src[7 - i]);
+    }
+
+    return output;
+}
+
+/// Read a byte from the gdb client.
+static u8 ReadByte() {
+    u8 c;
+    size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL);
+    if (received_size != 1) {
+        LOG_ERROR(Debug_GDBStub, "recv failed : %ld", received_size);
+        Deinit();
+    }
+
+    return c;
+}
+
+/// Calculate the checksum of the current command buffer.
+static u8 CalculateChecksum(u8 *buffer, u32 length) {
+    return static_cast<u8>(std::accumulate(buffer, buffer + length, 0, std::plus<u8>()));
+}
+
+/**
+ * Get the list of breakpoints for a given breakpoint type.
+ *
+ * @param type Type of breakpoint list.
+ */
+static std::map<u32, Breakpoint>& GetBreakpointList(BreakpointType type) {
+    switch (type) {
+    case BreakpointType::Execute:
+        return breakpoints_execute;
+    case BreakpointType::Read:
+        return breakpoints_read;
+    case BreakpointType::Write:
+        return breakpoints_write;
+    default:
+        return breakpoints_read;
+    }
+}
+
+/**
+ * Remove the breakpoint from the given address of the specified type.
+ *
+ * @param type Type of breakpoint.
+ * @param addr Address of breakpoint.
+ */
+static void RemoveBreakpoint(BreakpointType type, PAddr addr) {
+    std::map<u32, Breakpoint>& p = GetBreakpointList(type);
+
+    auto bp = p.find(addr);
+    if (bp != p.end()) {
+        LOG_DEBUG(Debug_GDBStub, "gdb: removed a breakpoint: %08x bytes at %08x of type %d\n", bp->second.len, bp->second.addr, type);
+        p.erase(addr);
+    }
+}
+
+PAddr GetNextBreakpointFromAddress(PAddr addr, BreakpointType type) {
+    std::map<u32, Breakpoint>& p = GetBreakpointList(type);
+    auto next_breakpoint = p.lower_bound(addr);
+    u32 breakpoint = -1;
+
+    if (next_breakpoint != p.end())
+        breakpoint = next_breakpoint->first;
+
+    return breakpoint;
+}
+
+bool CheckBreakpoint(PAddr addr, BreakpointType type) {
+    if (!IsConnected()) {
+        return false;
+    }
+
+    std::map<u32, Breakpoint>& p = GetBreakpointList(type);
+
+    auto bp = p.find(addr);
+    if (bp != p.end()) {
+        u32 len = bp->second.len;
+
+        // IDA Pro defaults to 4-byte breakpoints for all non-hardware breakpoints
+        // no matter if it's a 4-byte or 2-byte instruction. When you execute a
+        // Thumb instruction with a 4-byte breakpoint set, it will set a breakpoint on
+        // two instructions instead of the single instruction you placed the breakpoint
+        // on. So, as a way to make sure that execution breakpoints are only breaking
+        // on the instruction that was specified, set the length of an execution
+        // breakpoint to 1. This should be fine since the CPU should never begin executing
+        // an instruction anywhere except the beginning of the instruction.
+        if (type == BreakpointType::Execute) {
+            len = 1;
+        }
+
+        if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) {
+            LOG_DEBUG(Debug_GDBStub, "Found breakpoint type %d @ %08x, range: %08x - %08x (%d bytes)\n", type, addr, bp->second.addr, bp->second.addr + len, len);
+            return true;
+        }
+    }
+
+    return false;
+}
+
+/**
+ * Send packet to gdb client.
+ *
+ * @param packet Packet to be sent to client.
+ */
+static void SendPacket(const char packet) {
+    size_t sent_size = send(gdbserver_socket, &packet, 1, 0);
+    if (sent_size != 1) {
+        LOG_ERROR(Debug_GDBStub, "send failed");
+    }
+}
+
+/**
+ * Send reply to gdb client.
+ *
+ * @param reply Reply to be sent to client.
+ */
+static void SendReply(const char* reply) {
+    if (!IsConnected()) {
+        return;
+    }
+
+    memset(command_buffer, 0, sizeof(command_buffer));
+
+    command_length = strlen(reply);
+    if (command_length + 4 > sizeof(command_buffer)) {
+        LOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply");
+    }
+
+    memcpy(command_buffer + 1, reply, command_length);
+
+    u8 checksum = CalculateChecksum(command_buffer, command_length + 1);
+    command_buffer[0] = GDB_STUB_START;
+    command_buffer[command_length + 1] = GDB_STUB_END;
+    command_buffer[command_length + 2] = NibbleToHex(checksum >> 4);
+    command_buffer[command_length + 3] = NibbleToHex(checksum);
+
+    u8* ptr = command_buffer;
+    u32 left = command_length + 4;
+    while (left > 0) {
+        int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0);
+        if (sent_size < 0) {
+            LOG_ERROR(Debug_GDBStub, "gdb: send failed");
+            return Deinit();
+        }
+
+        left -= sent_size;
+        ptr += sent_size;
+    }
+}
+
+/// Handle query command from gdb client.
+static void HandleQuery() {
+    LOG_DEBUG(Debug_GDBStub, "gdb: query '%s'\n", command_buffer + 1);
+
+    if (!strcmp(reinterpret_cast<const char*>(command_buffer + 1), "TStatus")) {
+        SendReply("T0");
+    } else {
+        SendReply("");
+    }
+}
+
+/// Handle set thread command from gdb client.
+static void HandleSetThread() {
+    if (memcmp(command_buffer, "Hg0", 3) == 0 ||
+        memcmp(command_buffer, "Hc-1", 4) == 0 ||
+        memcmp(command_buffer, "Hc0", 4) == 0 ||
+        memcmp(command_buffer, "Hc1", 4) == 0) {
+        return SendReply("OK");
+    }
+
+    SendReply("E01");
+}
+
+/// Create and send signal packet.
+static void HandleSignal() {
+    std::string buffer = Common::StringFromFormat("T%02x%02x:%08x;%02x:%08x;", latest_signal, 15, htonl(Core::g_app_core->GetPC()), 13, htonl(Core::g_app_core->GetReg(13)));
+
+    LOG_DEBUG(Debug_GDBStub, "Response: %s", buffer.c_str());
+
+    SendReply(buffer.c_str());
+}
+
+/**
+ * Set signal and send packet to client through HandleSignal if signal flag is set using SendSignal.
+ *
+ * @param signal Signal to be sent to client.
+ */
+int SendSignal(u32 signal) {
+    if (gdbserver_socket == -1) {
+        return 1;
+    }
+
+    latest_signal = signal;
+
+    if (send_signal) {
+        HandleSignal();
+        send_signal = 0;
+    }
+
+    return 0;
+}
+
+/// Read command from gdb client.
+static void ReadCommand() {
+    command_length = 0;
+    memset(command_buffer, 0, sizeof(command_buffer));
+
+    u8 c = ReadByte();
+    if (c == '+') {
+        //ignore ack
+        return;
+    } else if (c == 0x03) {
+        LOG_INFO(Debug_GDBStub, "gdb: found break command\n");
+        halt_loop = true;
+        send_signal = 1;
+        SendSignal(SIGTRAP);
+        return;
+    } else if (c != GDB_STUB_START) {
+        LOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte %02x\n", c);
+        return;
+    }
+
+    while ((c = ReadByte()) != GDB_STUB_END) {
+        command_buffer[command_length++] = c;
+        if (command_length == sizeof(command_buffer)) {
+            LOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow\n");
+            SendPacket(GDB_STUB_NACK);
+            return;
+        }
+    }
+
+    u8 checksum_received = HexCharToValue(ReadByte()) << 4;
+    checksum_received |= HexCharToValue(ReadByte());
+
+    u8 checksum_calculated = CalculateChecksum(command_buffer, command_length);
+
+    if (checksum_received != checksum_calculated) {
+        LOG_ERROR(Debug_GDBStub, "gdb: invalid checksum: calculated %02x and read %02x for $%s# (length: %d)\n",
+            checksum_calculated, checksum_received, command_buffer, command_length);
+
+        command_length = 0;
+
+        SendPacket(GDB_STUB_NACK);
+        return;
+    }
+
+    SendPacket(GDB_STUB_ACK);
+}
+
+/// Check if there is data to be read from the gdb client.
+static bool IsDataAvailable() {
+    if (!IsConnected()) {
+        return false;
+    }
+
+    fd_set fd_socket;
+
+    FD_ZERO(&fd_socket);
+    FD_SET(gdbserver_socket, &fd_socket);
+
+    struct timeval t;
+    t.tv_sec = 0;
+    t.tv_usec = 0;
+
+    if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) {
+        LOG_ERROR(Debug_GDBStub, "select failed");
+        return false;
+    }
+
+    return FD_ISSET(gdbserver_socket, &fd_socket);
+}
+
+/// Send requested register to gdb client.
+static void ReadRegister() {
+    static u8 reply[64];
+    memset(reply, 0, sizeof(reply));
+
+    u32 id = HexCharToValue(command_buffer[1]);
+    if (command_buffer[2] != '\0') {
+        id <<= 4;
+        id |= HexCharToValue(command_buffer[2]);
+    }
+
+    if (id >= R0_REGISTER && id <= R15_REGISTER) {
+        IntToHex(reply, Core::g_app_core->GetReg(id));
+    } else if (id == CSPR_REGISTER) {
+        IntToHex(reply, Core::g_app_core->GetCPSR());
+    } else {
+        return SendReply("E01");
+    }
+
+    SendReply(reinterpret_cast<char*>(reply));
+}
+
+/// Send all registers to the gdb client.
+static void ReadRegisters() {
+    static u8 buffer[GDB_BUFFER_SIZE - 4];
+    memset(buffer, 0, sizeof(buffer));
+
+    u8* bufptr = buffer;
+    for (int i = 0; i <= CSPR_REGISTER; i++) {
+        if (i <= R15_REGISTER) {
+            IntToHex(bufptr + i * 8, Core::g_app_core->GetReg(i));
+        } else if (i == CSPR_REGISTER) {
+            IntToHex(bufptr + i * 8, Core::g_app_core->GetCPSR());
+        } else {
+            IntToHex(bufptr + i * 8, 0);
+            IntToHex(bufptr + (i + 1) * 8, 0);
+            i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
+        }
+    }
+
+    SendReply(reinterpret_cast<char*>(buffer));
+}
+
+/// Modify data of register specified by gdb client.
+static void WriteRegister() {
+    u8* buffer_ptr = command_buffer + 3;
+
+    u32 id = HexCharToValue(command_buffer[1]);
+    if (command_buffer[2] != '=') {
+        ++buffer_ptr;
+        id <<= 4;
+        id |= HexCharToValue(command_buffer[2]);
+    }
+
+    if (id >= R0_REGISTER && id <= R15_REGISTER) {
+        Core::g_app_core->SetReg(id, HexToInt(buffer_ptr));
+    } else if (id == CSPR_REGISTER) {
+        Core::g_app_core->SetCPSR(HexToInt(buffer_ptr));
+    } else {
+        return SendReply("E01");
+    }
+
+    SendReply("OK");
+}
+
+/// Modify all registers with data received from the client.
+static void WriteRegisters() {
+    u8* buffer_ptr = command_buffer + 1;
+
+    if (command_buffer[0] != 'G')
+        return SendReply("E01");
+
+    for (int i = 0; i <= CSPR_REGISTER; i++) {
+        if (i <= R15_REGISTER) {
+            Core::g_app_core->SetReg(i, HexToInt(buffer_ptr + i * 8));
+        } else if (i == CSPR_REGISTER) {
+            Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * 8));
+        } else {
+            i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
+        }
+    }
+
+    SendReply("OK");
+}
+
+/// Read location in memory specified by gdb client.
+static void ReadMemory() {
+    static u8 reply[GDB_BUFFER_SIZE - 4];
+
+    int i = 1;
+    PAddr addr = 0;
+    while (command_buffer[i] != ',') {
+        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
+    }
+    i++;
+
+    u32 len = 0;
+    while (i < command_length) {
+        len = (len << 4) | HexCharToValue(command_buffer[i++]);
+    }
+
+    if (len * 2 > sizeof(reply)) {
+        SendReply("E01");
+    }
+
+    u8* data = Memory::GetPointer(addr);
+    if (!data) {
+        return SendReply("E0");
+    }
+
+    MemToHex(reply, data, len);
+    reply[len * 2] = '\0';
+    SendReply(reinterpret_cast<char*>(reply));
+}
+
+/// Modify location in memory with data received from the gdb client.
+static void WriteMemory() {
+    int i = 1;
+    PAddr addr = 0;
+    while (command_buffer[i] != ',') {
+        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
+    }
+    i++;
+
+    u32 len = 0;
+    while (command_buffer[i] != ':') {
+        len = (len << 4) | HexCharToValue(command_buffer[i++]);
+    }
+
+    u8* dst = Memory::GetPointer(addr);
+    if (!dst) {
+        return SendReply("E00");
+    }
+
+    HexToMem(dst, command_buffer + i + 1, len);
+    SendReply("OK");
+}
+
+void Break(bool is_memory_break) {
+    if (!halt_loop) {
+        halt_loop = true;
+        send_signal = 1;
+        SendSignal(SIGTRAP);
+    }
+
+    memory_break = is_memory_break;
+}
+
+/// Tell the CPU that it should perform a single step.
+static void Step() {
+    step_loop = true;
+    halt_loop = true;
+    send_signal = 1;
+    step_break = 1;
+    SendSignal(SIGTRAP);
+}
+
+bool IsMemoryBreak() {
+    if (IsConnected()) {
+        return false;
+    }
+
+    return memory_break;
+}
+
+/// Tell the CPU to continue executing.
+static void Continue() {
+    memory_break = false;
+    step_break = 0;
+    step_loop = false;
+    halt_loop = false;
+}
+
+/**
+ * Commit breakpoint to list of breakpoints.
+ *
+ * @param type Type of breakpoint.
+ * @param addr Address of breakpoint.
+ * @param len Length of breakpoint.
+ */
+bool CommitBreakpoint(BreakpointType type, PAddr addr, u32 len) {
+    std::map<u32, Breakpoint>& p = GetBreakpointList(type);
+
+    Breakpoint breakpoint;
+    breakpoint.active = true;
+    breakpoint.addr = addr;
+    breakpoint.len = len;
+    p.insert({ addr, breakpoint });
+
+    LOG_DEBUG(Debug_GDBStub, "gdb: added %d breakpoint: %08x bytes at %08x\n", type, breakpoint.len, breakpoint.addr);
+
+    return true;
+}
+
+/// Handle add breakpoint command from gdb client.
+static void AddBreakpoint() {
+    BreakpointType type;
+
+    u8 type_id = HexCharToValue(command_buffer[1]);
+    switch (type_id) {
+    case 0:
+    case 1:
+        type = BreakpointType::Execute;
+        break;
+    case 2:
+        type = BreakpointType::Write;
+        break;
+    case 3:
+        type = BreakpointType::Read;
+        break;
+    case 4:
+        type = BreakpointType::Access;
+        break;
+    default:
+        return SendReply("E01");
+    }
+
+    int i = 3;
+    PAddr addr = 0;
+    while (command_buffer[i] != ',') {
+        addr = addr << 4 | HexCharToValue(command_buffer[i++]);
+    }
+    i++;
+
+    u32 len = 0;
+    while (i < command_length) {
+        len = len << 4 | HexCharToValue(command_buffer[i++]);
+    }
+
+    if (type == BreakpointType::Access) {
+        // Access is made up of Read and Write types, so add both breakpoints
+        type = BreakpointType::Read;
+
+        if (!CommitBreakpoint(type, addr, len)) {
+            return SendReply("E02");
+        }
+
+        type = BreakpointType::Write;
+    }
+
+    if (!CommitBreakpoint(type, addr, len)) {
+        return SendReply("E02");
+    }
+
+    SendReply("OK");
+}
+
+/// Handle remove breakpoint command from gdb client.
+static void RemoveBreakpoint() {
+    BreakpointType type;
+
+    u8 type_id = HexCharToValue(command_buffer[1]);
+    switch (type_id) {
+    case 0:
+    case 1:
+        type = BreakpointType::Execute;
+        break;
+    case 2:
+        type = BreakpointType::Write;
+        break;
+    case 3:
+        type = BreakpointType::Read;
+        break;
+    case 4:
+        type = BreakpointType::Access;
+        break;
+    default:
+        return SendReply("E01");
+    }
+
+    int i = 3;
+    PAddr addr = 0;
+    while (command_buffer[i] != ',') {
+        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
+    }
+    i++;
+
+    u32 len = 0;
+    while (i < command_length) {
+        len = (len << 4) | HexCharToValue(command_buffer[i++]);
+    }
+
+    if (type == BreakpointType::Access) {
+        // Access is made up of Read and Write types, so add both breakpoints
+        type = BreakpointType::Read;
+        RemoveBreakpoint(type, addr);
+
+        type = BreakpointType::Write;
+    }
+
+    RemoveBreakpoint(type, addr);
+    SendReply("OK");
+}
+
+void HandlePacket() {
+    if (!IsConnected()) {
+        return;
+    }
+
+    if (!IsDataAvailable()) {
+        return;
+    }
+
+    ReadCommand();
+    if (command_length == 0) {
+        return;
+    }
+
+    LOG_DEBUG(Debug_GDBStub, "Packet: %s", command_buffer);
+
+    switch (command_buffer[0]) {
+    case 'q':
+        HandleQuery();
+        break;
+    case 'H':
+        HandleSetThread();
+        break;
+    case '?':
+        HandleSignal();
+        break;
+    case 'k':
+        Deinit();
+        LOG_INFO(Debug_GDBStub, "killed by gdb");
+        return;
+    case 'g':
+        ReadRegisters();
+        break;
+    case 'G':
+        WriteRegisters();
+        break;
+    case 'p':
+        ReadRegister();
+        break;
+    case 'P':
+        WriteRegister();
+        break;
+    case 'm':
+        ReadMemory();
+        break;
+    case 'M':
+        WriteMemory();
+        break;
+    case 's':
+        Step();
+        return;
+    case 'C':
+    case 'c':
+        Continue();
+        return;
+    case 'z':
+        RemoveBreakpoint();
+        break;
+    case 'Z':
+        AddBreakpoint();
+        break;
+    default:
+        SendReply("");
+        break;
+    }
+}
+
+void SetServerPort(u16 port) {
+    gdbstub_port = port;
+}
+
+void ToggleServer(bool status) {
+    if (status) {
+        g_server_enabled = status;
+
+        // Start server
+        if (!IsConnected() && Core::g_sys_core != nullptr) {
+            Init();
+        }
+    }
+    else {
+        // Stop server
+        if (IsConnected()) {
+            Deinit();
+        }
+
+        g_server_enabled = status;
+    }
+}
+
+void Init(u16 port) {
+    if (!g_server_enabled) {
+        // Set the halt loop to false in case the user enabled the gdbstub mid-execution.
+        // This way the CPU can still execute normally.
+        halt_loop = false;
+        step_loop = false;
+        return;
+    }
+
+    // Setup initial gdbstub status
+    halt_loop = true;
+    step_loop = false;
+
+    breakpoints_execute.clear();
+    breakpoints_read.clear();
+    breakpoints_write.clear();
+
+    // Start gdb server
+    LOG_INFO(Debug_GDBStub, "Starting GDB server on port %d...", port);
+
+    sockaddr_in saddr_server = {};
+    saddr_server.sin_family = AF_INET;
+    saddr_server.sin_port = htons(port);
+    saddr_server.sin_addr.s_addr = INADDR_ANY;
+
+#ifdef _WIN32
+    WSAStartup(MAKEWORD(2, 2), &InitData);
+#endif
+
+    int tmpsock = socket(PF_INET, SOCK_STREAM, 0);
+    if (tmpsock == -1) {
+        LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket");
+    }
+
+    const sockaddr* server_addr = reinterpret_cast<const sockaddr*>(&saddr_server);
+    socklen_t server_addrlen = sizeof(saddr_server);
+    if (bind(tmpsock, server_addr, server_addrlen) < 0) {
+        LOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket");
+    }
+
+    if (listen(tmpsock, 1) < 0) {
+        LOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket");
+    }
+
+    // Wait for gdb to connect
+    LOG_INFO(Debug_GDBStub, "Waiting for gdb to connect...\n");
+    sockaddr_in saddr_client;
+    sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client);
+    socklen_t client_addrlen = sizeof(saddr_client);
+    gdbserver_socket = accept(tmpsock, client_addr, &client_addrlen);
+    if (gdbserver_socket < 0) {
+        // In the case that we couldn't start the server for whatever reason, just start CPU execution like normal.
+        halt_loop = false;
+        step_loop = false;
+
+        LOG_ERROR(Debug_GDBStub, "Failed to accept gdb client");
+    }
+    else {
+        LOG_INFO(Debug_GDBStub, "Client connected.\n");
+        saddr_client.sin_addr.s_addr = ntohl(saddr_client.sin_addr.s_addr);
+    }
+
+    // Clean up temporary socket if it's still alive at this point.
+    if (tmpsock != -1) {
+        shutdown(tmpsock, SHUT_RDWR);
+    }
+}
+
+void Init() {
+    Init(gdbstub_port);
+}
+
+void Deinit() {
+    if (!g_server_enabled) {
+        return;
+    }
+
+    LOG_INFO(Debug_GDBStub, "Stopping GDB ...");
+    if (gdbserver_socket != -1) {
+        shutdown(gdbserver_socket, SHUT_RDWR);
+        gdbserver_socket = -1;
+    }
+
+#ifdef _WIN32
+    WSACleanup();
+#endif
+
+    LOG_INFO(Debug_GDBStub, "GDB stopped.");
+}
+
+bool IsConnected() {
+    return g_server_enabled && gdbserver_socket != -1;
+}
+
+bool GetCpuHaltFlag() {
+    return halt_loop;
+}
+
+bool GetCpuStepFlag() {
+    return step_loop;
+}
+
+void SetCpuStepFlag(bool is_step) {
+    step_loop = is_step;
+}
+
+};
diff --git a/src/core/gdbstub/gdbstub.h b/src/core/gdbstub/gdbstub.h
new file mode 100644
index 0000000000..11ff823c3d
--- /dev/null
+++ b/src/core/gdbstub/gdbstub.h
@@ -0,0 +1,89 @@
+// Copyright 2013 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
+
+#pragma once
+#include <atomic>
+
+namespace GDBStub {
+
+/// Breakpoint Method
+enum class BreakpointType {
+    None,     ///< None
+    Execute,  ///< Execution Breakpoint
+    Read,     ///< Read Breakpoint
+    Write,    ///< Write Breakpoint
+    Access    ///< Access (R/W) Breakpoint
+};
+
+/// If set to false, the server will never be started and no gdbstub-related functions will be executed.
+extern std::atomic<bool> g_server_enabled;
+
+/**
+ * Set the port the gdbstub should use to listen for connections.
+ *
+ * @param port Port to listen for connection
+ */
+void SetServerPort(u16 port);
+
+/**
+ * Set the g_server_enabled flag and start or stop the server if possible.
+ *
+ * @param status Set the server to enabled or disabled.
+ */
+void ToggleServer(bool status);
+
+/// Start the gdbstub server.
+void Init();
+
+/// Stop gdbstub server.
+void Deinit();
+
+/// Returns true if there is an active socket connection.
+bool IsConnected();
+
+/**
+ * Signal to the gdbstub server that it should halt CPU execution.
+ *
+ * @param is_memory_break If true, the break resulted from a memory breakpoint.
+ */
+void Break(bool is_memory_break = false);
+
+/// Determine if there was a memory breakpoint.
+bool IsMemoryBreak();
+
+/// Read and handle packet from gdb client.
+void HandlePacket();
+
+/**
+ * Get the nearest breakpoint of the specified type at the given address.
+ *
+ * @param addr Address to search from.
+ * @param type Type of breakpoint.
+ */
+PAddr GetNextBreakpointFromAddress(u32 addr, GDBStub::BreakpointType type);
+
+/**
+ * Check if a breakpoint of the specified type exists at the given address.
+ *
+ * @param addr Address of breakpoint.
+ * @param type Type of breakpoint.
+ */
+bool CheckBreakpoint(u32 addr, GDBStub::BreakpointType type);
+
+// If set to true, the CPU will halt at the beginning of the next CPU loop.
+bool GetCpuHaltFlag();
+
+// If set to true and the CPU is halted, the CPU will step one instruction.
+bool GetCpuStepFlag();
+
+/**
+ * When set to true, the CPU will step one instruction when the CPU is halted next.
+ *
+ * @param is_step
+ */
+void SetCpuStepFlag(bool is_step);
+
+}
diff --git a/src/core/settings.h b/src/core/settings.h
index 6ca0e1afce..b6b395a79c 100644
--- a/src/core/settings.h
+++ b/src/core/settings.h
@@ -6,6 +6,7 @@
 
 #include <string>
 #include <array>
+#include <common/file_util.h>
 
 namespace Settings {
 
@@ -60,6 +61,10 @@ struct Values {
     float bg_blue;
 
     std::string log_filter;
+
+    // Debugging
+    bool use_gdbstub;
+    u16 gdbstub_port;
 } extern values;
 
 }
diff --git a/src/core/system.cpp b/src/core/system.cpp
index 3cd84bf5e0..421fc48a78 100644
--- a/src/core/system.cpp
+++ b/src/core/system.cpp
@@ -12,6 +12,8 @@
 
 #include "video_core/video_core.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 namespace System {
 
 void Init(EmuWindow* emu_window) {
@@ -22,9 +24,13 @@ void Init(EmuWindow* emu_window) {
     Kernel::Init();
     HLE::Init();
     VideoCore::Init(emu_window);
+
+    GDBStub::Init();
 }
 
 void Shutdown() {
+    GDBStub::Deinit();
+
     VideoCore::Shutdown();
     HLE::Shutdown();
     Kernel::Shutdown();

From 31dee93e849d79a91f280faf16941806e3cb3c6b Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Wed, 2 Sep 2015 08:56:38 -0400
Subject: [PATCH 02/14] Implement gdbstub

---
 src/citra/citra.cpp                           |   3 +
 src/citra/config.cpp                          |   4 +
 src/citra/default_ini.h                       |   5 +
 src/citra_qt/config.cpp                       |  10 +
 src/citra_qt/main.cpp                         |  12 +
 src/citra_qt/main.h                           |   1 +
 src/citra_qt/main.ui                          |   9 +
 src/common/logging/backend.cpp                |   1 +
 src/common/logging/log.h                      |   1 +
 src/core/CMakeLists.txt                       |   2 +
 .../arm/dyncom/arm_dyncom_interpreter.cpp     |  41 +-
 src/core/arm/skyeye_common/armstate.cpp       |  35 +
 src/core/arm/skyeye_common/armstate.h         |   2 +
 src/core/core.cpp                             |  17 +
 src/core/gdbstub/gdbstub.cpp                  | 940 ++++++++++++++++++
 src/core/gdbstub/gdbstub.h                    |  89 ++
 src/core/settings.h                           |   5 +
 src/core/system.cpp                           |   6 +
 18 files changed, 1174 insertions(+), 9 deletions(-)
 create mode 100644 src/core/gdbstub/gdbstub.cpp
 create mode 100644 src/core/gdbstub/gdbstub.h

diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index 46f4a07c94..b368653950 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -30,6 +30,8 @@
 
 #include "video_core/video_core.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 
 static void PrintHelp()
 {
@@ -72,6 +74,7 @@ int main(int argc, char **argv) {
     Config config;
     log_filter.ParseFilterString(Settings::values.log_filter);
 
+    GDBStub::SetServerPort(static_cast<u32>(Settings::values.gdbstub_port));
 
     EmuWindow_GLFW* emu_window = new EmuWindow_GLFW;
 
diff --git a/src/citra/config.cpp b/src/citra/config.cpp
index 8a98bda877..af343e9fe3 100644
--- a/src/citra/config.cpp
+++ b/src/citra/config.cpp
@@ -75,6 +75,10 @@ void Config::ReadValues() {
 
     // Miscellaneous
     Settings::values.log_filter = glfw_config->Get("Miscellaneous", "log_filter", "*:Info");
+
+    // GDBStubebugging
+    Settings::values.use_gdbstub = glfw_config->GetBoolean("Debugging", "use_gdbstub", false);
+    Settings::values.gdbstub_port = glfw_config->GetInteger("Debugging", "gdbstub_port", 24689);
 }
 
 void Config::Reload() {
diff --git a/src/citra/default_ini.h b/src/citra/default_ini.h
index 7e5d497298..5ba40a8edb 100644
--- a/src/citra/default_ini.h
+++ b/src/citra/default_ini.h
@@ -66,6 +66,11 @@ region_value =
 # A filter which removes logs below a certain logging level.
 # Examples: *:Debug Kernel.SVC:Trace Service.*:Critical
 log_filter = *:Info
+
+[Debugging]
+# Port for listening to GDB connections.
+use_gdbstub=false
+gdbstub_port=24689
 )";
 
 }
diff --git a/src/citra_qt/config.cpp b/src/citra_qt/config.cpp
index 1f4981ce1d..8e247ff5c0 100644
--- a/src/citra_qt/config.cpp
+++ b/src/citra_qt/config.cpp
@@ -62,6 +62,11 @@ void Config::ReadValues() {
     qt_config->beginGroup("Miscellaneous");
     Settings::values.log_filter = qt_config->value("log_filter", "*:Info").toString().toStdString();
     qt_config->endGroup();
+
+    qt_config->beginGroup("Debugging");
+    Settings::values.use_gdbstub = qt_config->value("use_gdbstub", false).toBool();
+    Settings::values.gdbstub_port = qt_config->value("gdbstub_port", 24689).toInt();
+    qt_config->endGroup();
 }
 
 void Config::SaveValues() {
@@ -97,6 +102,11 @@ void Config::SaveValues() {
     qt_config->beginGroup("Miscellaneous");
     qt_config->setValue("log_filter", QString::fromStdString(Settings::values.log_filter));
     qt_config->endGroup();
+
+    qt_config->beginGroup("Debugging");
+    qt_config->setValue("use_gdbstub", Settings::values.use_gdbstub);
+    qt_config->setValue("gdbstub_port", Settings::values.gdbstub_port);
+    qt_config->endGroup();
 }
 
 void Config::Reload() {
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index 298649aaf6..d8d17f466e 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -48,6 +48,8 @@
 
 #include "video_core/video_core.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 GMainWindow::GMainWindow() : emu_thread(nullptr)
 {
     Pica::g_debug_context = Pica::DebugContext::Construct();
@@ -143,6 +145,11 @@ GMainWindow::GMainWindow() : emu_thread(nullptr)
 
     game_list->LoadInterfaceLayout(settings);
 
+    ui.action_Use_Gdbstub->setChecked(Settings::values.use_gdbstub);
+    SetGdbstubEnabled(ui.action_Use_Gdbstub->isChecked());
+
+    GDBStub::SetServerPort(static_cast<u32>(Settings::values.gdbstub_port));
+
     ui.action_Use_Hardware_Renderer->setChecked(Settings::values.use_hw_renderer);
     SetHardwareRendererEnabled(ui.action_Use_Hardware_Renderer->isChecked());
 
@@ -175,6 +182,7 @@ GMainWindow::GMainWindow() : emu_thread(nullptr)
     connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame()));
     connect(ui.action_Use_Hardware_Renderer, SIGNAL(triggered(bool)), this, SLOT(SetHardwareRendererEnabled(bool)));
     connect(ui.action_Use_Shader_JIT, SIGNAL(triggered(bool)), this, SLOT(SetShaderJITEnabled(bool)));
+    connect(ui.action_Use_Gdbstub, SIGNAL(triggered(bool)), this, SLOT(SetGdbstubEnabled(bool)));
     connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode()));
     connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog()));
 
@@ -445,6 +453,10 @@ void GMainWindow::SetHardwareRendererEnabled(bool enabled) {
     VideoCore::g_hw_renderer_enabled = enabled;
 }
 
+void GMainWindow::SetGdbstubEnabled(bool enabled) {
+    GDBStub::ToggleServer(enabled);
+}
+
 void GMainWindow::SetShaderJITEnabled(bool enabled) {
     VideoCore::g_shader_jit_enabled = enabled;
 }
diff --git a/src/citra_qt/main.h b/src/citra_qt/main.h
index 6d27ce6a97..f6d429cd98 100644
--- a/src/citra_qt/main.h
+++ b/src/citra_qt/main.h
@@ -99,6 +99,7 @@ private slots:
     void OnConfigure();
     void OnDisplayTitleBars(bool);
     void SetHardwareRendererEnabled(bool);
+    void SetGdbstubEnabled(bool);
     void SetShaderJITEnabled(bool);
     void ToggleWindowMode();
 
diff --git a/src/citra_qt/main.ui b/src/citra_qt/main.ui
index 997597642b..1e8a07cfb4 100644
--- a/src/citra_qt/main.ui
+++ b/src/citra_qt/main.ui
@@ -75,6 +75,7 @@
     <addaction name="separator"/>
     <addaction name="action_Use_Hardware_Renderer"/>
     <addaction name="action_Use_Shader_JIT"/>
+    <addaction name="action_Use_Gdbstub"/>
     <addaction name="action_Configure"/>
    </widget>
    <widget class="QMenu" name="menu_View">
@@ -170,6 +171,14 @@
     <string>Use Shader JIT</string>
    </property>
   </action>
+  <action name="action_Use_Gdbstub">
+    <property name="checkable">
+      <bool>true</bool>
+    </property>
+    <property name="text">
+      <string>Use Gdbstub</string>
+    </property>
+  </action>
   <action name="action_Configure">
    <property name="text">
     <string>Configure ...</string>
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 92e8e742d8..21a9ae8d0d 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -29,6 +29,7 @@ namespace Log {
         SUB(Debug, Emulated) \
         SUB(Debug, GPU) \
         SUB(Debug, Breakpoint) \
+        SUB(Debug, GDBStub) \
         CLS(Kernel) \
         SUB(Kernel, SVC) \
         CLS(Service) \
diff --git a/src/common/logging/log.h b/src/common/logging/log.h
index 5fd3bd7f53..43f0c59e47 100644
--- a/src/common/logging/log.h
+++ b/src/common/logging/log.h
@@ -43,6 +43,7 @@ enum class Class : ClassType {
     Debug_Emulated,             ///< Debug messages from the emulated programs
     Debug_GPU,                  ///< GPU debugging tools
     Debug_Breakpoint,           ///< Logging breakpoints and watchpoints
+    Debug_GDBStub,              ///< GDB Stub
     Kernel,                     ///< The HLE implementation of the CTR kernel
     Kernel_SVC,                 ///< Kernel system calls
     Service,                    ///< HLE implementation of system services. Each major service
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index c17290b9bb..861b711c7a 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -22,6 +22,7 @@ set(SRCS
             file_sys/archive_systemsavedata.cpp
             file_sys/disk_archive.cpp
             file_sys/ivfc_archive.cpp
+            gdbstub/gdbstub.cpp
             hle/config_mem.cpp
             hle/hle.cpp
             hle/applets/applet.cpp
@@ -149,6 +150,7 @@ set(HEADERS
             file_sys/disk_archive.h
             file_sys/file_backend.h
             file_sys/ivfc_archive.h
+            gdbstub/gdbstub.h
             hle/config_mem.h
             hle/function_wrappers.h
             hle/hle.h
diff --git a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
index fbd6f94f93..8293f4c601 100644
--- a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
@@ -23,6 +23,8 @@
 #include "core/arm/skyeye_common/armsupp.h"
 #include "core/arm/skyeye_common/vfp/vfp.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 Common::Profiling::TimingCategory profile_execute("DynCom::Execute");
 Common::Profiling::TimingCategory profile_decode("DynCom::Decode");
 
@@ -3548,6 +3550,7 @@ static int InterpreterTranslate(ARMul_State* cpu, int& bb_start, u32 addr) {
             CITRA_IGNORE_EXIT(-1);
         }
         inst_base = arm_instruction_trans[idx](inst, idx);
+
 translated:
         phys_addr += inst_size;
 
@@ -3580,6 +3583,8 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     Common::Profiling::ScopeTimer timer_execute(profile_execute);
     MICROPROFILE_SCOPE(DynCom_Execute);
 
+    int breakpoint_offset = -1;
+
     #undef RM
     #undef RS
 
@@ -3604,15 +3609,27 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     #define INC_PC(l)   ptr += sizeof(arm_inst) + l
     #define INC_PC_STUB ptr += sizeof(arm_inst)
 
+#define GDB_BP_CHECK \
+    cpu->Cpsr &= ~(1 << 5); \
+    cpu->Cpsr |= cpu->TFlag << 5; \
+    if (GDBStub::g_server_enabled) { \
+        if (GDBStub::IsMemoryBreak() || PC == breakpoint_offset) { \
+            GDBStub::Break(); \
+            goto END; \
+        } \
+    }
+
 // GCC and Clang have a C++ extension to support a lookup table of labels. Otherwise, fallback to a
 // clunky switch statement.
 #if defined __GNUC__ || defined __clang__
 #define GOTO_NEXT_INST \
+    GDB_BP_CHECK; \
     if (num_instrs >= cpu->NumInstrsToExecute) goto END; \
     num_instrs++; \
     goto *InstLabel[inst_base->idx]
 #else
 #define GOTO_NEXT_INST \
+    GDB_BP_CHECK; \
     if (num_instrs >= cpu->NumInstrsToExecute) goto END; \
     num_instrs++; \
     switch(inst_base->idx) { \
@@ -3878,6 +3895,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     unsigned int addr;
     unsigned int num_instrs = 0;
 
+
     int ptr;
 
     LOAD_NZCVT;
@@ -3903,6 +3921,11 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
                 goto END;
         }
 
+        // Find breakpoint if one exists within the block
+        if (GDBStub::g_server_enabled && GDBStub::IsConnected()) {
+            breakpoint_offset = GDBStub::GetNextBreakpointFromAddress(cpu->Reg[15], GDBStub::BreakpointType::Execute);
+        }
+
         inst_base = (arm_inst *)&inst_buf[ptr];
         GOTO_NEXT_INST;
     }
@@ -4454,7 +4477,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
 
-            cpu->Reg[BITS(inst_cream->inst, 12, 15)] = Memory::Read8(addr);
+            cpu->Reg[BITS(inst_cream->inst, 12, 15)] = cpu->ReadMemory8(addr);
 
             if (BITS(inst_cream->inst, 12, 15) == 15) {
                 INC_PC(sizeof(ldst_inst));
@@ -4472,7 +4495,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
 
-            cpu->Reg[BITS(inst_cream->inst, 12, 15)] = Memory::Read8(addr);
+            cpu->Reg[BITS(inst_cream->inst, 12, 15)] = cpu->ReadMemory8(addr);
 
             if (BITS(inst_cream->inst, 12, 15) == 15) {
                 INC_PC(sizeof(ldst_inst));
@@ -4531,7 +4554,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
 
             cpu->SetExclusiveMemoryAddress(read_addr);
 
-            RD = Memory::Read8(read_addr);
+            RD = cpu->ReadMemory8(read_addr);
             if (inst_cream->Rd == 15) {
                 INC_PC(sizeof(generic_arm_inst));
                 goto DISPATCH;
@@ -4604,7 +4627,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
         if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
-            unsigned int value = Memory::Read8(addr);
+            unsigned int value = cpu->ReadMemory8(addr);
             if (BIT(value, 7)) {
                 value |= 0xffffff00;
             }
@@ -6027,7 +6050,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
             unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
-            Memory::Write8(addr, value);
+            cpu->WriteMemory8(addr, value);
         }
         cpu->Reg[15] += cpu->GetInstructionSize();
         INC_PC(sizeof(ldst_inst));
@@ -6040,7 +6063,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
             ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
             inst_cream->get_addr(cpu, inst_cream->inst, addr);
             unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
-            Memory::Write8(addr, value);
+            cpu->WriteMemory8(addr, value);
         }
         cpu->Reg[15] += cpu->GetInstructionSize();
         INC_PC(sizeof(ldst_inst));
@@ -6091,7 +6114,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
 
             if (cpu->IsExclusiveMemoryAccess(write_addr)) {
                 cpu->UnsetExclusiveMemoryAddress();
-                Memory::Write8(write_addr, cpu->Reg[inst_cream->Rm]);
+                cpu->WriteMemory8(write_addr, cpu->Reg[inst_cream->Rm]);
                 RD = 0;
             } else {
                 // Failed to write due to mutex access
@@ -6250,8 +6273,8 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
         if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
             swp_inst* inst_cream = (swp_inst*)inst_base->component;
             addr = RN;
-            unsigned int value = Memory::Read8(addr);
-            Memory::Write8(addr, (RM & 0xFF));
+            unsigned int value = cpu->ReadMemory8(addr);
+            cpu->WriteMemory8(addr, (RM & 0xFF));
             RD = value;
         }
         cpu->Reg[15] += cpu->GetInstructionSize();
diff --git a/src/core/arm/skyeye_common/armstate.cpp b/src/core/arm/skyeye_common/armstate.cpp
index 0491717dc2..2d814345ad 100644
--- a/src/core/arm/skyeye_common/armstate.cpp
+++ b/src/core/arm/skyeye_common/armstate.cpp
@@ -7,6 +7,7 @@
 #include "core/memory.h"
 #include "core/arm/skyeye_common/armstate.h"
 #include "core/arm/skyeye_common/vfp/vfp.h"
+#include "core/gdbstub/gdbstub.h"
 
 ARMul_State::ARMul_State(PrivilegeMode initial_mode)
 {
@@ -185,8 +186,25 @@ void ARMul_State::ResetMPCoreCP15Registers()
     CP15[CP15_TLB_DEBUG_CONTROL] = 0x00000000;
 }
 
+static void CheckMemoryBreakpoint(u32 address, GDBStub::BreakpointType type)
+{
+    if (GDBStub::g_server_enabled && GDBStub::CheckBreakpoint(address, type)) {
+        LOG_DEBUG(Debug, "Found memory breakpoint @ %08x", address);
+        GDBStub::Break(true);
+    }
+}
+
+u8 ARMul_State::ReadMemory8(u32 address) const
+{
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
+
+    return Memory::Read8(address);
+}
+
 u16 ARMul_State::ReadMemory16(u32 address) const
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
+
     u16 data = Memory::Read16(address);
 
     if (InBigEndianMode())
@@ -197,6 +215,8 @@ u16 ARMul_State::ReadMemory16(u32 address) const
 
 u32 ARMul_State::ReadMemory32(u32 address) const
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
+
     u32 data = Memory::Read32(address);
 
     if (InBigEndianMode())
@@ -207,6 +227,8 @@ u32 ARMul_State::ReadMemory32(u32 address) const
 
 u64 ARMul_State::ReadMemory64(u32 address) const
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
+
     u64 data = Memory::Read64(address);
 
     if (InBigEndianMode())
@@ -215,8 +237,17 @@ u64 ARMul_State::ReadMemory64(u32 address) const
     return data;
 }
 
+void ARMul_State::WriteMemory8(u32 address, u8 data)
+{
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
+
+    Memory::Write8(address, data);
+}
+
 void ARMul_State::WriteMemory16(u32 address, u16 data)
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
+
     if (InBigEndianMode())
         data = Common::swap16(data);
 
@@ -225,6 +256,8 @@ void ARMul_State::WriteMemory16(u32 address, u16 data)
 
 void ARMul_State::WriteMemory32(u32 address, u32 data)
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
+
     if (InBigEndianMode())
         data = Common::swap32(data);
 
@@ -233,6 +266,8 @@ void ARMul_State::WriteMemory32(u32 address, u32 data)
 
 void ARMul_State::WriteMemory64(u32 address, u64 data)
 {
+    CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
+
     if (InBigEndianMode())
         data = Common::swap64(data);
 
diff --git a/src/core/arm/skyeye_common/armstate.h b/src/core/arm/skyeye_common/armstate.h
index ceb159d144..98dad9b1f9 100644
--- a/src/core/arm/skyeye_common/armstate.h
+++ b/src/core/arm/skyeye_common/armstate.h
@@ -153,9 +153,11 @@ public:
 
     // Reads/writes data in big/little endian format based on the
     // state of the E (endian) bit in the APSR.
+    u8 ReadMemory8(u32 address) const;
     u16 ReadMemory16(u32 address) const;
     u32 ReadMemory32(u32 address) const;
     u64 ReadMemory64(u32 address) const;
+    void WriteMemory8(u32 address, u8 data);
     void WriteMemory16(u32 address, u16 data);
     void WriteMemory32(u32 address, u32 data);
     void WriteMemory64(u32 address, u64 data);
diff --git a/src/core/core.cpp b/src/core/core.cpp
index dddc16708e..219b03af49 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -13,6 +13,8 @@
 #include "core/hle/kernel/thread.h"
 #include "core/hw/hw.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 namespace Core {
 
 ARM_Interface*     g_app_core = nullptr;  ///< ARM11 application core
@@ -20,6 +22,21 @@ ARM_Interface*     g_sys_core = nullptr;  ///< ARM11 system (OS) core
 
 /// Run the core CPU loop
 void RunLoop(int tight_loop) {
+    if (GDBStub::g_server_enabled) {
+        GDBStub::HandlePacket();
+
+        // If the loop is halted and we want to step, use a tiny (1) number of instructions to execute.
+        // Otherwise get out of the loop function.
+        if (GDBStub::GetCpuHaltFlag()) {
+            if (GDBStub::GetCpuStepFlag()) {
+                GDBStub::SetCpuStepFlag(false);
+                tight_loop = 1;
+            } else {
+                return;
+            }
+        }
+    }
+
     // If we don't have a currently active thread then don't execute instructions,
     // instead advance to the next event and try to yield to the next thread
     if (Kernel::GetCurrentThread() == nullptr) {
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
new file mode 100644
index 0000000000..ced1c54f56
--- /dev/null
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -0,0 +1,940 @@
+// Copyright 2013 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
+
+#include <csignal>
+#include <cstdarg>
+#include <cstdio>
+#include <cstring>
+#include <fcntl.h>
+#include <map>
+#include <numeric>
+
+#ifdef _MSC_VER
+#include <WinSock2.h>
+#include <ws2tcpip.h>
+#include <common/x64/abi.h>
+#include <io.h>
+#include <iphlpapi.h>
+#define SHUT_RDWR 2
+#else
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#endif
+
+#include "common/logging/log.h"
+#include "common/string_util.h"
+#include <core/arm/arm_interface.h>
+#include "core/core.h"
+#include "core/memory.h"
+#include "gdbstub.h"
+
+const int GDB_BUFFER_SIZE = 10000;
+
+const char GDB_STUB_START = '$';
+const char GDB_STUB_END = '#';
+const char GDB_STUB_ACK = '+';
+const char GDB_STUB_NACK = '-';
+
+#ifndef SIGTRAP
+const u32 SIGTRAP = 5;
+#endif
+
+#ifndef SIGTERM
+const u32 SIGTERM = 15;
+#endif
+
+#ifndef MSG_WAITALL
+const u32 MSG_WAITALL = 8;
+#endif
+
+const u32 R0_REGISTER = 0;
+const u32 R15_REGISTER = 15;
+const u32 CSPR_REGISTER = 25;
+
+namespace GDBStub {
+
+static int gdbserver_socket = -1;
+
+static u8 command_buffer[GDB_BUFFER_SIZE];
+static u32 command_length;
+
+static u32 latest_signal = 0;
+static u32 send_signal = 0;
+static u32 step_break = 0;
+static bool memory_break = false;
+
+// Binding to a port within the reserved ports range (0-1023) requires root permissions,
+// so default to a port outside of that range.
+static u16 gdbstub_port = 24689;
+
+static bool halt_loop = true;
+static bool step_loop = false;
+std::atomic<bool> g_server_enabled(false);
+
+#ifdef _WIN32
+WSADATA InitData;
+#endif
+
+struct Breakpoint {
+    bool active;
+    PAddr addr;
+    u32 len;
+};
+
+static std::map<u32, Breakpoint> breakpoints_execute;
+static std::map<u32, Breakpoint> breakpoints_read;
+static std::map<u32, Breakpoint> breakpoints_write;
+
+/**
+ * Turns hex string character into the equivalent byte.
+ *
+ * @param hex Input hex character to be turned into byte.
+ */
+static u8 HexCharToValue(u8 hex) {
+    if (hex >= '0' && hex <= '9') {
+        return hex - '0';
+    } else if (hex >= 'a' && hex <= 'f') {
+        return hex - 'a' + 0xA;
+    } else if (hex >= 'A' && hex <= 'F') {
+        return hex - 'A' + 0xA;
+    }
+
+    LOG_ERROR(Debug_GDBStub, "Invalid nibble: %c (%02x)\n", hex, hex);
+    return 0;
+}
+
+/**
+ * Turn nibble of byte into hex string character.
+ *
+ * @param n Nibble to be turned into hex character.
+ */
+static u8 NibbleToHex(u8 n) {
+    n &= 0xF;
+    if (n < 0xA) {
+        return '0' + n;
+    } else {
+        return 'A' + n - 0xA;
+    }
+}
+
+/**
+ * Converts input array of u8 bytes into their equivalent hex string characters.
+ *
+ * @param dest Pointer to buffer to store output hex string characters.
+ * @param src Pointer to array of u8 bytes.
+ * @param len Length of src array.
+ */
+static void MemToHex(u8* dest, u8* src, u32 len) {
+    while (len-- > 0) {
+        u8 tmp = *src++;
+        *dest++ = NibbleToHex(tmp >> 4);
+        *dest++ = NibbleToHex(tmp);
+    }
+}
+
+/**
+ * Converts input hex string characters into an array of equivalent of u8 bytes.
+ *
+ * @param dest Pointer to buffer to store u8 bytes.
+ * @param src Pointer to array of output hex string characters.
+ * @param len Length of src array.
+ */
+static void HexToMem(u8* dest, u8* src, u32 len) {
+    while (len-- > 0) {
+        *dest++ = (HexCharToValue(src[0]) << 4) | HexCharToValue(src[1]);
+        src += 2;
+    }
+}
+
+/**
+ * Convert a u32 into a hex string.
+ *
+ * @param dest Pointer to buffer to store output hex string characters.
+ */
+static void IntToHex(u8* dest, u32 v) {
+    for (int i = 0; i < 8; i += 2) {
+        dest[i + 1] = NibbleToHex(v >> (4 * i));
+        dest[i] = NibbleToHex(v >> (4 * (i + 1)));
+    }
+}
+
+/**
+ * Convert a hex string into a u32.
+ *
+ * @param src Pointer to hex string.
+ */
+static u32 HexToInt(u8* src) {
+    u32 output = 0;
+
+    for (int i = 0; i < 8; i += 2) {
+        output = (output << 4) | HexCharToValue(src[7 - i - 1]);
+        output = (output << 4) | HexCharToValue(src[7 - i]);
+    }
+
+    return output;
+}
+
+/// Read a byte from the gdb client.
+static u8 ReadByte() {
+    u8 c;
+    size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL);
+    if (received_size != 1) {
+        LOG_ERROR(Debug_GDBStub, "recv failed : %ld", received_size);
+        Deinit();
+    }
+
+    return c;
+}
+
+/// Calculate the checksum of the current command buffer.
+static u8 CalculateChecksum(u8 *buffer, u32 length) {
+    return static_cast<u8>(std::accumulate(buffer, buffer + length, 0, std::plus<u8>()));
+}
+
+/**
+ * Get the list of breakpoints for a given breakpoint type.
+ *
+ * @param type Type of breakpoint list.
+ */
+static std::map<u32, Breakpoint>& GetBreakpointList(BreakpointType type) {
+    switch (type) {
+    case BreakpointType::Execute:
+        return breakpoints_execute;
+    case BreakpointType::Read:
+        return breakpoints_read;
+    case BreakpointType::Write:
+        return breakpoints_write;
+    default:
+        return breakpoints_read;
+    }
+}
+
+/**
+ * Remove the breakpoint from the given address of the specified type.
+ *
+ * @param type Type of breakpoint.
+ * @param addr Address of breakpoint.
+ */
+static void RemoveBreakpoint(BreakpointType type, PAddr addr) {
+    std::map<u32, Breakpoint>& p = GetBreakpointList(type);
+
+    auto bp = p.find(addr);
+    if (bp != p.end()) {
+        LOG_DEBUG(Debug_GDBStub, "gdb: removed a breakpoint: %08x bytes at %08x of type %d\n", bp->second.len, bp->second.addr, type);
+        p.erase(addr);
+    }
+}
+
+PAddr GetNextBreakpointFromAddress(PAddr addr, BreakpointType type) {
+    std::map<u32, Breakpoint>& p = GetBreakpointList(type);
+    auto next_breakpoint = p.lower_bound(addr);
+    u32 breakpoint = -1;
+
+    if (next_breakpoint != p.end())
+        breakpoint = next_breakpoint->first;
+
+    return breakpoint;
+}
+
+bool CheckBreakpoint(PAddr addr, BreakpointType type) {
+    if (!IsConnected()) {
+        return false;
+    }
+
+    std::map<u32, Breakpoint>& p = GetBreakpointList(type);
+
+    auto bp = p.find(addr);
+    if (bp != p.end()) {
+        u32 len = bp->second.len;
+
+        // IDA Pro defaults to 4-byte breakpoints for all non-hardware breakpoints
+        // no matter if it's a 4-byte or 2-byte instruction. When you execute a
+        // Thumb instruction with a 4-byte breakpoint set, it will set a breakpoint on
+        // two instructions instead of the single instruction you placed the breakpoint
+        // on. So, as a way to make sure that execution breakpoints are only breaking
+        // on the instruction that was specified, set the length of an execution
+        // breakpoint to 1. This should be fine since the CPU should never begin executing
+        // an instruction anywhere except the beginning of the instruction.
+        if (type == BreakpointType::Execute) {
+            len = 1;
+        }
+
+        if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) {
+            LOG_DEBUG(Debug_GDBStub, "Found breakpoint type %d @ %08x, range: %08x - %08x (%d bytes)\n", type, addr, bp->second.addr, bp->second.addr + len, len);
+            return true;
+        }
+    }
+
+    return false;
+}
+
+/**
+ * Send packet to gdb client.
+ *
+ * @param packet Packet to be sent to client.
+ */
+static void SendPacket(const char packet) {
+    size_t sent_size = send(gdbserver_socket, &packet, 1, 0);
+    if (sent_size != 1) {
+        LOG_ERROR(Debug_GDBStub, "send failed");
+    }
+}
+
+/**
+ * Send reply to gdb client.
+ *
+ * @param reply Reply to be sent to client.
+ */
+static void SendReply(const char* reply) {
+    if (!IsConnected()) {
+        return;
+    }
+
+    memset(command_buffer, 0, sizeof(command_buffer));
+
+    command_length = strlen(reply);
+    if (command_length + 4 > sizeof(command_buffer)) {
+        LOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply");
+    }
+
+    memcpy(command_buffer + 1, reply, command_length);
+
+    u8 checksum = CalculateChecksum(command_buffer, command_length + 1);
+    command_buffer[0] = GDB_STUB_START;
+    command_buffer[command_length + 1] = GDB_STUB_END;
+    command_buffer[command_length + 2] = NibbleToHex(checksum >> 4);
+    command_buffer[command_length + 3] = NibbleToHex(checksum);
+
+    u8* ptr = command_buffer;
+    u32 left = command_length + 4;
+    while (left > 0) {
+        int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0);
+        if (sent_size < 0) {
+            LOG_ERROR(Debug_GDBStub, "gdb: send failed");
+            return Deinit();
+        }
+
+        left -= sent_size;
+        ptr += sent_size;
+    }
+}
+
+/// Handle query command from gdb client.
+static void HandleQuery() {
+    LOG_DEBUG(Debug_GDBStub, "gdb: query '%s'\n", command_buffer + 1);
+
+    if (!strcmp(reinterpret_cast<const char*>(command_buffer + 1), "TStatus")) {
+        SendReply("T0");
+    } else {
+        SendReply("");
+    }
+}
+
+/// Handle set thread command from gdb client.
+static void HandleSetThread() {
+    if (memcmp(command_buffer, "Hg0", 3) == 0 ||
+        memcmp(command_buffer, "Hc-1", 4) == 0 ||
+        memcmp(command_buffer, "Hc0", 4) == 0 ||
+        memcmp(command_buffer, "Hc1", 4) == 0) {
+        return SendReply("OK");
+    }
+
+    SendReply("E01");
+}
+
+/// Create and send signal packet.
+static void HandleSignal() {
+    std::string buffer = Common::StringFromFormat("T%02x%02x:%08x;%02x:%08x;", latest_signal, 15, htonl(Core::g_app_core->GetPC()), 13, htonl(Core::g_app_core->GetReg(13)));
+
+    LOG_DEBUG(Debug_GDBStub, "Response: %s", buffer.c_str());
+
+    SendReply(buffer.c_str());
+}
+
+/**
+ * Set signal and send packet to client through HandleSignal if signal flag is set using SendSignal.
+ *
+ * @param signal Signal to be sent to client.
+ */
+int SendSignal(u32 signal) {
+    if (gdbserver_socket == -1) {
+        return 1;
+    }
+
+    latest_signal = signal;
+
+    if (send_signal) {
+        HandleSignal();
+        send_signal = 0;
+    }
+
+    return 0;
+}
+
+/// Read command from gdb client.
+static void ReadCommand() {
+    command_length = 0;
+    memset(command_buffer, 0, sizeof(command_buffer));
+
+    u8 c = ReadByte();
+    if (c == '+') {
+        //ignore ack
+        return;
+    } else if (c == 0x03) {
+        LOG_INFO(Debug_GDBStub, "gdb: found break command\n");
+        halt_loop = true;
+        send_signal = 1;
+        SendSignal(SIGTRAP);
+        return;
+    } else if (c != GDB_STUB_START) {
+        LOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte %02x\n", c);
+        return;
+    }
+
+    while ((c = ReadByte()) != GDB_STUB_END) {
+        command_buffer[command_length++] = c;
+        if (command_length == sizeof(command_buffer)) {
+            LOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow\n");
+            SendPacket(GDB_STUB_NACK);
+            return;
+        }
+    }
+
+    u8 checksum_received = HexCharToValue(ReadByte()) << 4;
+    checksum_received |= HexCharToValue(ReadByte());
+
+    u8 checksum_calculated = CalculateChecksum(command_buffer, command_length);
+
+    if (checksum_received != checksum_calculated) {
+        LOG_ERROR(Debug_GDBStub, "gdb: invalid checksum: calculated %02x and read %02x for $%s# (length: %d)\n",
+            checksum_calculated, checksum_received, command_buffer, command_length);
+
+        command_length = 0;
+
+        SendPacket(GDB_STUB_NACK);
+        return;
+    }
+
+    SendPacket(GDB_STUB_ACK);
+}
+
+/// Check if there is data to be read from the gdb client.
+static bool IsDataAvailable() {
+    if (!IsConnected()) {
+        return false;
+    }
+
+    fd_set fd_socket;
+
+    FD_ZERO(&fd_socket);
+    FD_SET(gdbserver_socket, &fd_socket);
+
+    struct timeval t;
+    t.tv_sec = 0;
+    t.tv_usec = 0;
+
+    if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) {
+        LOG_ERROR(Debug_GDBStub, "select failed");
+        return false;
+    }
+
+    return FD_ISSET(gdbserver_socket, &fd_socket);
+}
+
+/// Send requested register to gdb client.
+static void ReadRegister() {
+    static u8 reply[64];
+    memset(reply, 0, sizeof(reply));
+
+    u32 id = HexCharToValue(command_buffer[1]);
+    if (command_buffer[2] != '\0') {
+        id <<= 4;
+        id |= HexCharToValue(command_buffer[2]);
+    }
+
+    if (id >= R0_REGISTER && id <= R15_REGISTER) {
+        IntToHex(reply, Core::g_app_core->GetReg(id));
+    } else if (id == CSPR_REGISTER) {
+        IntToHex(reply, Core::g_app_core->GetCPSR());
+    } else {
+        return SendReply("E01");
+    }
+
+    SendReply(reinterpret_cast<char*>(reply));
+}
+
+/// Send all registers to the gdb client.
+static void ReadRegisters() {
+    static u8 buffer[GDB_BUFFER_SIZE - 4];
+    memset(buffer, 0, sizeof(buffer));
+
+    u8* bufptr = buffer;
+    for (int i = 0; i <= CSPR_REGISTER; i++) {
+        if (i <= R15_REGISTER) {
+            IntToHex(bufptr + i * 8, Core::g_app_core->GetReg(i));
+        } else if (i == CSPR_REGISTER) {
+            IntToHex(bufptr + i * 8, Core::g_app_core->GetCPSR());
+        } else {
+            IntToHex(bufptr + i * 8, 0);
+            IntToHex(bufptr + (i + 1) * 8, 0);
+            i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
+        }
+    }
+
+    SendReply(reinterpret_cast<char*>(buffer));
+}
+
+/// Modify data of register specified by gdb client.
+static void WriteRegister() {
+    u8* buffer_ptr = command_buffer + 3;
+
+    u32 id = HexCharToValue(command_buffer[1]);
+    if (command_buffer[2] != '=') {
+        ++buffer_ptr;
+        id <<= 4;
+        id |= HexCharToValue(command_buffer[2]);
+    }
+
+    if (id >= R0_REGISTER && id <= R15_REGISTER) {
+        Core::g_app_core->SetReg(id, HexToInt(buffer_ptr));
+    } else if (id == CSPR_REGISTER) {
+        Core::g_app_core->SetCPSR(HexToInt(buffer_ptr));
+    } else {
+        return SendReply("E01");
+    }
+
+    SendReply("OK");
+}
+
+/// Modify all registers with data received from the client.
+static void WriteRegisters() {
+    u8* buffer_ptr = command_buffer + 1;
+
+    if (command_buffer[0] != 'G')
+        return SendReply("E01");
+
+    for (int i = 0; i <= CSPR_REGISTER; i++) {
+        if (i <= R15_REGISTER) {
+            Core::g_app_core->SetReg(i, HexToInt(buffer_ptr + i * 8));
+        } else if (i == CSPR_REGISTER) {
+            Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * 8));
+        } else {
+            i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
+        }
+    }
+
+    SendReply("OK");
+}
+
+/// Read location in memory specified by gdb client.
+static void ReadMemory() {
+    static u8 reply[GDB_BUFFER_SIZE - 4];
+
+    int i = 1;
+    PAddr addr = 0;
+    while (command_buffer[i] != ',') {
+        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
+    }
+    i++;
+
+    u32 len = 0;
+    while (i < command_length) {
+        len = (len << 4) | HexCharToValue(command_buffer[i++]);
+    }
+
+    if (len * 2 > sizeof(reply)) {
+        SendReply("E01");
+    }
+
+    u8* data = Memory::GetPointer(addr);
+    if (!data) {
+        return SendReply("E0");
+    }
+
+    MemToHex(reply, data, len);
+    reply[len * 2] = '\0';
+    SendReply(reinterpret_cast<char*>(reply));
+}
+
+/// Modify location in memory with data received from the gdb client.
+static void WriteMemory() {
+    int i = 1;
+    PAddr addr = 0;
+    while (command_buffer[i] != ',') {
+        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
+    }
+    i++;
+
+    u32 len = 0;
+    while (command_buffer[i] != ':') {
+        len = (len << 4) | HexCharToValue(command_buffer[i++]);
+    }
+
+    u8* dst = Memory::GetPointer(addr);
+    if (!dst) {
+        return SendReply("E00");
+    }
+
+    HexToMem(dst, command_buffer + i + 1, len);
+    SendReply("OK");
+}
+
+void Break(bool is_memory_break) {
+    if (!halt_loop) {
+        halt_loop = true;
+        send_signal = 1;
+        SendSignal(SIGTRAP);
+    }
+
+    memory_break = is_memory_break;
+}
+
+/// Tell the CPU that it should perform a single step.
+static void Step() {
+    step_loop = true;
+    halt_loop = true;
+    send_signal = 1;
+    step_break = 1;
+    SendSignal(SIGTRAP);
+}
+
+bool IsMemoryBreak() {
+    if (IsConnected()) {
+        return false;
+    }
+
+    return memory_break;
+}
+
+/// Tell the CPU to continue executing.
+static void Continue() {
+    memory_break = false;
+    step_break = 0;
+    step_loop = false;
+    halt_loop = false;
+}
+
+/**
+ * Commit breakpoint to list of breakpoints.
+ *
+ * @param type Type of breakpoint.
+ * @param addr Address of breakpoint.
+ * @param len Length of breakpoint.
+ */
+bool CommitBreakpoint(BreakpointType type, PAddr addr, u32 len) {
+    std::map<u32, Breakpoint>& p = GetBreakpointList(type);
+
+    Breakpoint breakpoint;
+    breakpoint.active = true;
+    breakpoint.addr = addr;
+    breakpoint.len = len;
+    p.insert({ addr, breakpoint });
+
+    LOG_DEBUG(Debug_GDBStub, "gdb: added %d breakpoint: %08x bytes at %08x\n", type, breakpoint.len, breakpoint.addr);
+
+    return true;
+}
+
+/// Handle add breakpoint command from gdb client.
+static void AddBreakpoint() {
+    BreakpointType type;
+
+    u8 type_id = HexCharToValue(command_buffer[1]);
+    switch (type_id) {
+    case 0:
+    case 1:
+        type = BreakpointType::Execute;
+        break;
+    case 2:
+        type = BreakpointType::Write;
+        break;
+    case 3:
+        type = BreakpointType::Read;
+        break;
+    case 4:
+        type = BreakpointType::Access;
+        break;
+    default:
+        return SendReply("E01");
+    }
+
+    int i = 3;
+    PAddr addr = 0;
+    while (command_buffer[i] != ',') {
+        addr = addr << 4 | HexCharToValue(command_buffer[i++]);
+    }
+    i++;
+
+    u32 len = 0;
+    while (i < command_length) {
+        len = len << 4 | HexCharToValue(command_buffer[i++]);
+    }
+
+    if (type == BreakpointType::Access) {
+        // Access is made up of Read and Write types, so add both breakpoints
+        type = BreakpointType::Read;
+
+        if (!CommitBreakpoint(type, addr, len)) {
+            return SendReply("E02");
+        }
+
+        type = BreakpointType::Write;
+    }
+
+    if (!CommitBreakpoint(type, addr, len)) {
+        return SendReply("E02");
+    }
+
+    SendReply("OK");
+}
+
+/// Handle remove breakpoint command from gdb client.
+static void RemoveBreakpoint() {
+    BreakpointType type;
+
+    u8 type_id = HexCharToValue(command_buffer[1]);
+    switch (type_id) {
+    case 0:
+    case 1:
+        type = BreakpointType::Execute;
+        break;
+    case 2:
+        type = BreakpointType::Write;
+        break;
+    case 3:
+        type = BreakpointType::Read;
+        break;
+    case 4:
+        type = BreakpointType::Access;
+        break;
+    default:
+        return SendReply("E01");
+    }
+
+    int i = 3;
+    PAddr addr = 0;
+    while (command_buffer[i] != ',') {
+        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
+    }
+    i++;
+
+    u32 len = 0;
+    while (i < command_length) {
+        len = (len << 4) | HexCharToValue(command_buffer[i++]);
+    }
+
+    if (type == BreakpointType::Access) {
+        // Access is made up of Read and Write types, so add both breakpoints
+        type = BreakpointType::Read;
+        RemoveBreakpoint(type, addr);
+
+        type = BreakpointType::Write;
+    }
+
+    RemoveBreakpoint(type, addr);
+    SendReply("OK");
+}
+
+void HandlePacket() {
+    if (!IsConnected()) {
+        return;
+    }
+
+    if (!IsDataAvailable()) {
+        return;
+    }
+
+    ReadCommand();
+    if (command_length == 0) {
+        return;
+    }
+
+    LOG_DEBUG(Debug_GDBStub, "Packet: %s", command_buffer);
+
+    switch (command_buffer[0]) {
+    case 'q':
+        HandleQuery();
+        break;
+    case 'H':
+        HandleSetThread();
+        break;
+    case '?':
+        HandleSignal();
+        break;
+    case 'k':
+        Deinit();
+        LOG_INFO(Debug_GDBStub, "killed by gdb");
+        return;
+    case 'g':
+        ReadRegisters();
+        break;
+    case 'G':
+        WriteRegisters();
+        break;
+    case 'p':
+        ReadRegister();
+        break;
+    case 'P':
+        WriteRegister();
+        break;
+    case 'm':
+        ReadMemory();
+        break;
+    case 'M':
+        WriteMemory();
+        break;
+    case 's':
+        Step();
+        return;
+    case 'C':
+    case 'c':
+        Continue();
+        return;
+    case 'z':
+        RemoveBreakpoint();
+        break;
+    case 'Z':
+        AddBreakpoint();
+        break;
+    default:
+        SendReply("");
+        break;
+    }
+}
+
+void SetServerPort(u16 port) {
+    gdbstub_port = port;
+}
+
+void ToggleServer(bool status) {
+    if (status) {
+        g_server_enabled = status;
+
+        // Start server
+        if (!IsConnected() && Core::g_sys_core != nullptr) {
+            Init();
+        }
+    }
+    else {
+        // Stop server
+        if (IsConnected()) {
+            Deinit();
+        }
+
+        g_server_enabled = status;
+    }
+}
+
+void Init(u16 port) {
+    if (!g_server_enabled) {
+        // Set the halt loop to false in case the user enabled the gdbstub mid-execution.
+        // This way the CPU can still execute normally.
+        halt_loop = false;
+        step_loop = false;
+        return;
+    }
+
+    // Setup initial gdbstub status
+    halt_loop = true;
+    step_loop = false;
+
+    breakpoints_execute.clear();
+    breakpoints_read.clear();
+    breakpoints_write.clear();
+
+    // Start gdb server
+    LOG_INFO(Debug_GDBStub, "Starting GDB server on port %d...", port);
+
+    sockaddr_in saddr_server = {};
+    saddr_server.sin_family = AF_INET;
+    saddr_server.sin_port = htons(port);
+    saddr_server.sin_addr.s_addr = INADDR_ANY;
+
+#ifdef _WIN32
+    WSAStartup(MAKEWORD(2, 2), &InitData);
+#endif
+
+    int tmpsock = socket(PF_INET, SOCK_STREAM, 0);
+    if (tmpsock == -1) {
+        LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket");
+    }
+
+    const sockaddr* server_addr = reinterpret_cast<const sockaddr*>(&saddr_server);
+    socklen_t server_addrlen = sizeof(saddr_server);
+    if (bind(tmpsock, server_addr, server_addrlen) < 0) {
+        LOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket");
+    }
+
+    if (listen(tmpsock, 1) < 0) {
+        LOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket");
+    }
+
+    // Wait for gdb to connect
+    LOG_INFO(Debug_GDBStub, "Waiting for gdb to connect...\n");
+    sockaddr_in saddr_client;
+    sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client);
+    socklen_t client_addrlen = sizeof(saddr_client);
+    gdbserver_socket = accept(tmpsock, client_addr, &client_addrlen);
+    if (gdbserver_socket < 0) {
+        // In the case that we couldn't start the server for whatever reason, just start CPU execution like normal.
+        halt_loop = false;
+        step_loop = false;
+
+        LOG_ERROR(Debug_GDBStub, "Failed to accept gdb client");
+    }
+    else {
+        LOG_INFO(Debug_GDBStub, "Client connected.\n");
+        saddr_client.sin_addr.s_addr = ntohl(saddr_client.sin_addr.s_addr);
+    }
+
+    // Clean up temporary socket if it's still alive at this point.
+    if (tmpsock != -1) {
+        shutdown(tmpsock, SHUT_RDWR);
+    }
+}
+
+void Init() {
+    Init(gdbstub_port);
+}
+
+void Deinit() {
+    if (!g_server_enabled) {
+        return;
+    }
+
+    LOG_INFO(Debug_GDBStub, "Stopping GDB ...");
+    if (gdbserver_socket != -1) {
+        shutdown(gdbserver_socket, SHUT_RDWR);
+        gdbserver_socket = -1;
+    }
+
+#ifdef _WIN32
+    WSACleanup();
+#endif
+
+    LOG_INFO(Debug_GDBStub, "GDB stopped.");
+}
+
+bool IsConnected() {
+    return g_server_enabled && gdbserver_socket != -1;
+}
+
+bool GetCpuHaltFlag() {
+    return halt_loop;
+}
+
+bool GetCpuStepFlag() {
+    return step_loop;
+}
+
+void SetCpuStepFlag(bool is_step) {
+    step_loop = is_step;
+}
+
+};
diff --git a/src/core/gdbstub/gdbstub.h b/src/core/gdbstub/gdbstub.h
new file mode 100644
index 0000000000..11ff823c3d
--- /dev/null
+++ b/src/core/gdbstub/gdbstub.h
@@ -0,0 +1,89 @@
+// Copyright 2013 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
+
+#pragma once
+#include <atomic>
+
+namespace GDBStub {
+
+/// Breakpoint Method
+enum class BreakpointType {
+    None,     ///< None
+    Execute,  ///< Execution Breakpoint
+    Read,     ///< Read Breakpoint
+    Write,    ///< Write Breakpoint
+    Access    ///< Access (R/W) Breakpoint
+};
+
+/// If set to false, the server will never be started and no gdbstub-related functions will be executed.
+extern std::atomic<bool> g_server_enabled;
+
+/**
+ * Set the port the gdbstub should use to listen for connections.
+ *
+ * @param port Port to listen for connection
+ */
+void SetServerPort(u16 port);
+
+/**
+ * Set the g_server_enabled flag and start or stop the server if possible.
+ *
+ * @param status Set the server to enabled or disabled.
+ */
+void ToggleServer(bool status);
+
+/// Start the gdbstub server.
+void Init();
+
+/// Stop gdbstub server.
+void Deinit();
+
+/// Returns true if there is an active socket connection.
+bool IsConnected();
+
+/**
+ * Signal to the gdbstub server that it should halt CPU execution.
+ *
+ * @param is_memory_break If true, the break resulted from a memory breakpoint.
+ */
+void Break(bool is_memory_break = false);
+
+/// Determine if there was a memory breakpoint.
+bool IsMemoryBreak();
+
+/// Read and handle packet from gdb client.
+void HandlePacket();
+
+/**
+ * Get the nearest breakpoint of the specified type at the given address.
+ *
+ * @param addr Address to search from.
+ * @param type Type of breakpoint.
+ */
+PAddr GetNextBreakpointFromAddress(u32 addr, GDBStub::BreakpointType type);
+
+/**
+ * Check if a breakpoint of the specified type exists at the given address.
+ *
+ * @param addr Address of breakpoint.
+ * @param type Type of breakpoint.
+ */
+bool CheckBreakpoint(u32 addr, GDBStub::BreakpointType type);
+
+// If set to true, the CPU will halt at the beginning of the next CPU loop.
+bool GetCpuHaltFlag();
+
+// If set to true and the CPU is halted, the CPU will step one instruction.
+bool GetCpuStepFlag();
+
+/**
+ * When set to true, the CPU will step one instruction when the CPU is halted next.
+ *
+ * @param is_step
+ */
+void SetCpuStepFlag(bool is_step);
+
+}
diff --git a/src/core/settings.h b/src/core/settings.h
index 0b05e5bee5..97ddcdff9d 100644
--- a/src/core/settings.h
+++ b/src/core/settings.h
@@ -6,6 +6,7 @@
 
 #include <string>
 #include <array>
+#include <common/file_util.h>
 
 namespace Settings {
 
@@ -60,6 +61,10 @@ struct Values {
     float bg_blue;
 
     std::string log_filter;
+
+    // Debugging
+    bool use_gdbstub;
+    u16 gdbstub_port;
 } extern values;
 
 }
diff --git a/src/core/system.cpp b/src/core/system.cpp
index 3cd84bf5e0..421fc48a78 100644
--- a/src/core/system.cpp
+++ b/src/core/system.cpp
@@ -12,6 +12,8 @@
 
 #include "video_core/video_core.h"
 
+#include "core/gdbstub/gdbstub.h"
+
 namespace System {
 
 void Init(EmuWindow* emu_window) {
@@ -22,9 +24,13 @@ void Init(EmuWindow* emu_window) {
     Kernel::Init();
     HLE::Init();
     VideoCore::Init(emu_window);
+
+    GDBStub::Init();
 }
 
 void Shutdown() {
+    GDBStub::Deinit();
+
     VideoCore::Shutdown();
     HLE::Shutdown();
     Kernel::Shutdown();

From 42928659e8d4ff4edffc36acabe3d9040dbc1326 Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Sun, 4 Oct 2015 11:22:31 -0400
Subject: [PATCH 03/14] Use BreakpointAddress struct instead of passing address
 directly

---
 src/core/arm/dyncom/arm_dyncom_interpreter.cpp |  6 +++---
 src/core/gdbstub/gdbstub.cpp                   | 13 +++++++++----
 src/core/gdbstub/gdbstub.h                     |  7 ++++++-
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
index 8293f4c601..88be27ab2d 100644
--- a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
@@ -3583,7 +3583,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     Common::Profiling::ScopeTimer timer_execute(profile_execute);
     MICROPROFILE_SCOPE(DynCom_Execute);
 
-    int breakpoint_offset = -1;
+    GDBStub::BreakpointAddress breakpoint_data;
 
     #undef RM
     #undef RS
@@ -3613,7 +3613,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     cpu->Cpsr &= ~(1 << 5); \
     cpu->Cpsr |= cpu->TFlag << 5; \
     if (GDBStub::g_server_enabled) { \
-        if (GDBStub::IsMemoryBreak() || PC == breakpoint_offset) { \
+        if (GDBStub::IsMemoryBreak() || (breakpoint_data.type != GDBStub::BreakpointType::None && PC == breakpoint_data.address)) { \
             GDBStub::Break(); \
             goto END; \
         } \
@@ -3923,7 +3923,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
 
         // Find breakpoint if one exists within the block
         if (GDBStub::g_server_enabled && GDBStub::IsConnected()) {
-            breakpoint_offset = GDBStub::GetNextBreakpointFromAddress(cpu->Reg[15], GDBStub::BreakpointType::Execute);
+            breakpoint_data = GDBStub::GetNextBreakpointFromAddress(cpu->Reg[15], GDBStub::BreakpointType::Execute);
         }
 
         inst_base = (arm_inst *)&inst_buf[ptr];
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index ced1c54f56..25ce63b29f 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -231,13 +231,18 @@ static void RemoveBreakpoint(BreakpointType type, PAddr addr) {
     }
 }
 
-PAddr GetNextBreakpointFromAddress(PAddr addr, BreakpointType type) {
+BreakpointAddress GetNextBreakpointFromAddress(PAddr addr, BreakpointType type) {
     std::map<u32, Breakpoint>& p = GetBreakpointList(type);
     auto next_breakpoint = p.lower_bound(addr);
-    u32 breakpoint = -1;
+    BreakpointAddress breakpoint;
 
-    if (next_breakpoint != p.end())
-        breakpoint = next_breakpoint->first;
+    if (next_breakpoint != p.end()) {
+        breakpoint.address = next_breakpoint->first;
+        breakpoint.type = type;
+    } else {
+        breakpoint.address = 0;
+        breakpoint.type = BreakpointType::None;
+    }
 
     return breakpoint;
 }
diff --git a/src/core/gdbstub/gdbstub.h b/src/core/gdbstub/gdbstub.h
index 11ff823c3d..da238f3498 100644
--- a/src/core/gdbstub/gdbstub.h
+++ b/src/core/gdbstub/gdbstub.h
@@ -18,6 +18,11 @@ enum class BreakpointType {
     Access    ///< Access (R/W) Breakpoint
 };
 
+struct BreakpointAddress {
+    PAddr address;
+    BreakpointType type;
+};
+
 /// If set to false, the server will never be started and no gdbstub-related functions will be executed.
 extern std::atomic<bool> g_server_enabled;
 
@@ -63,7 +68,7 @@ void HandlePacket();
  * @param addr Address to search from.
  * @param type Type of breakpoint.
  */
-PAddr GetNextBreakpointFromAddress(u32 addr, GDBStub::BreakpointType type);
+BreakpointAddress GetNextBreakpointFromAddress(u32 addr, GDBStub::BreakpointType type);
 
 /**
  * Check if a breakpoint of the specified type exists at the given address.

From 2b7316a379103edf4d22893e0f4432e6415eabfa Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Sun, 11 Oct 2015 20:07:58 -0400
Subject: [PATCH 04/14] Remove unnecessary new lines, changed Deinit to
 Shutdown

---
 src/citra/config.cpp                           |  2 +-
 src/core/arm/dyncom/arm_dyncom_interpreter.cpp |  1 -
 src/core/gdbstub/gdbstub.cpp                   | 10 +++++-----
 src/core/gdbstub/gdbstub.h                     |  2 +-
 src/core/system.cpp                            |  4 +---
 5 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/src/citra/config.cpp b/src/citra/config.cpp
index af343e9fe3..2f13c29a2f 100644
--- a/src/citra/config.cpp
+++ b/src/citra/config.cpp
@@ -76,7 +76,7 @@ void Config::ReadValues() {
     // Miscellaneous
     Settings::values.log_filter = glfw_config->Get("Miscellaneous", "log_filter", "*:Info");
 
-    // GDBStubebugging
+    // Debugging
     Settings::values.use_gdbstub = glfw_config->GetBoolean("Debugging", "use_gdbstub", false);
     Settings::values.gdbstub_port = glfw_config->GetInteger("Debugging", "gdbstub_port", 24689);
 }
diff --git a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
index 88be27ab2d..96c88c83a6 100644
--- a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
@@ -3895,7 +3895,6 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
     unsigned int addr;
     unsigned int num_instrs = 0;
 
-
     int ptr;
 
     LOAD_NZCVT;
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 25ce63b29f..15e6f36a0a 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -186,7 +186,7 @@ static u8 ReadByte() {
     size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL);
     if (received_size != 1) {
         LOG_ERROR(Debug_GDBStub, "recv failed : %ld", received_size);
-        Deinit();
+        Shutdown();
     }
 
     return c;
@@ -322,7 +322,7 @@ static void SendReply(const char* reply) {
         int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0);
         if (sent_size < 0) {
             LOG_ERROR(Debug_GDBStub, "gdb: send failed");
-            return Deinit();
+            return Shutdown();
         }
 
         left -= sent_size;
@@ -773,7 +773,7 @@ void HandlePacket() {
         HandleSignal();
         break;
     case 'k':
-        Deinit();
+        Shutdown();
         LOG_INFO(Debug_GDBStub, "killed by gdb");
         return;
     case 'g':
@@ -829,7 +829,7 @@ void ToggleServer(bool status) {
     else {
         // Stop server
         if (IsConnected()) {
-            Deinit();
+            Shutdown();
         }
 
         g_server_enabled = status;
@@ -908,7 +908,7 @@ void Init() {
     Init(gdbstub_port);
 }
 
-void Deinit() {
+void Shutdown() {
     if (!g_server_enabled) {
         return;
     }
diff --git a/src/core/gdbstub/gdbstub.h b/src/core/gdbstub/gdbstub.h
index da238f3498..aff705a32c 100644
--- a/src/core/gdbstub/gdbstub.h
+++ b/src/core/gdbstub/gdbstub.h
@@ -44,7 +44,7 @@ void ToggleServer(bool status);
 void Init();
 
 /// Stop gdbstub server.
-void Deinit();
+void Shutdown();
 
 /// Returns true if there is an active socket connection.
 bool IsConnected();
diff --git a/src/core/system.cpp b/src/core/system.cpp
index 421fc48a78..7e9c56538b 100644
--- a/src/core/system.cpp
+++ b/src/core/system.cpp
@@ -24,13 +24,11 @@ void Init(EmuWindow* emu_window) {
     Kernel::Init();
     HLE::Init();
     VideoCore::Init(emu_window);
-
     GDBStub::Init();
 }
 
 void Shutdown() {
-    GDBStub::Deinit();
-
+    GDBStub::Shutdown();
     VideoCore::Shutdown();
     HLE::Shutdown();
     Kernel::Shutdown();

From 9f66580d7e353b96690690f776d557f1c9028b37 Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Wed, 21 Oct 2015 06:49:49 -0400
Subject: [PATCH 05/14] Fix buffer overflow comments

---
 src/core/gdbstub/gdbstub.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 15e6f36a0a..0d586ca101 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -306,6 +306,7 @@ static void SendReply(const char* reply) {
     command_length = strlen(reply);
     if (command_length + 4 > sizeof(command_buffer)) {
         LOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply");
+        return;
     }
 
     memcpy(command_buffer + 1, reply, command_length);
@@ -403,12 +404,12 @@ static void ReadCommand() {
     }
 
     while ((c = ReadByte()) != GDB_STUB_END) {
-        command_buffer[command_length++] = c;
-        if (command_length == sizeof(command_buffer)) {
+        if (command_length >= sizeof(command_buffer)) {
             LOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow\n");
             SendPacket(GDB_STUB_NACK);
             return;
         }
+        command_buffer[command_length++] = c;
     }
 
     u8 checksum_received = HexCharToValue(ReadByte()) << 4;

From 53aa55fcaa6491d2cc0836e25f9303f7ba4126f3 Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Wed, 21 Oct 2015 07:14:43 -0400
Subject: [PATCH 06/14] Try to add support for VFP registers

---
 src/core/gdbstub/gdbstub.cpp | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 0d586ca101..a6977302c2 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -56,6 +56,7 @@ const u32 MSG_WAITALL = 8;
 const u32 R0_REGISTER = 0;
 const u32 R15_REGISTER = 15;
 const u32 CSPR_REGISTER = 25;
+const u32 FPSCR_REGISTER = 58;
 
 namespace GDBStub {
 
@@ -468,6 +469,10 @@ static void ReadRegister() {
         IntToHex(reply, Core::g_app_core->GetReg(id));
     } else if (id == CSPR_REGISTER) {
         IntToHex(reply, Core::g_app_core->GetCPSR());
+    } else if (id > CSPR_REGISTER && id < FPSCR_REGISTER) {
+        IntToHex(reply, Core::g_app_core->GetVFPReg(id - CSPR_REGISTER - 1)); // VFP registers should start at 26, so one after CSPR_REGISTER
+    } else if (id == FPSCR_REGISTER) {
+        IntToHex(reply, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR)); // Get FPSCR
     } else {
         return SendReply("E01");
     }
@@ -481,15 +486,19 @@ static void ReadRegisters() {
     memset(buffer, 0, sizeof(buffer));
 
     u8* bufptr = buffer;
-    for (int i = 0; i <= CSPR_REGISTER; i++) {
+    for (int i = 0; i <= FPSCR_REGISTER; i++) {
         if (i <= R15_REGISTER) {
             IntToHex(bufptr + i * 8, Core::g_app_core->GetReg(i));
         } else if (i == CSPR_REGISTER) {
             IntToHex(bufptr + i * 8, Core::g_app_core->GetCPSR());
-        } else {
+        } else if (i < CSPR_REGISTER) {
             IntToHex(bufptr + i * 8, 0);
             IntToHex(bufptr + (i + 1) * 8, 0);
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
+        } else if (i > CSPR_REGISTER && i < FPSCR_REGISTER) {
+            IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPReg(i - CSPR_REGISTER - 1));
+        } else if (i == FPSCR_REGISTER) {
+            IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR));
         }
     }
 
@@ -511,6 +520,10 @@ static void WriteRegister() {
         Core::g_app_core->SetReg(id, HexToInt(buffer_ptr));
     } else if (id == CSPR_REGISTER) {
         Core::g_app_core->SetCPSR(HexToInt(buffer_ptr));
+    } else if (id > CSPR_REGISTER && id < FPSCR_REGISTER) {
+        Core::g_app_core->SetVFPReg(id - CSPR_REGISTER - 1, HexToInt(buffer_ptr));
+    } else if (id == FPSCR_REGISTER) {
+        Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, HexToInt(buffer_ptr));
     } else {
         return SendReply("E01");
     }
@@ -525,13 +538,17 @@ static void WriteRegisters() {
     if (command_buffer[0] != 'G')
         return SendReply("E01");
 
-    for (int i = 0; i <= CSPR_REGISTER; i++) {
+    for (int i = 0; i <= FPSCR_REGISTER; i++) {
         if (i <= R15_REGISTER) {
             Core::g_app_core->SetReg(i, HexToInt(buffer_ptr + i * 8));
         } else if (i == CSPR_REGISTER) {
             Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * 8));
-        } else {
+        } else if (i < CSPR_REGISTER) {
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
+        } else if (i > CSPR_REGISTER && i < FPSCR_REGISTER) {
+            Core::g_app_core->SetVFPReg(i - CSPR_REGISTER - 1, HexToInt(buffer_ptr + i * 8));
+        } else if (i == FPSCR_REGISTER) {
+            Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, HexToInt(buffer_ptr + i * 8));
         }
     }
 

From 8a0d848646661c28a03c4659caf64e38fc92c127 Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Wed, 21 Oct 2015 07:19:03 -0400
Subject: [PATCH 07/14] Pad responses to gdb for VFP registers

---
 src/core/gdbstub/gdbstub.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index a6977302c2..7d8e9e3fb2 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -473,6 +473,7 @@ static void ReadRegister() {
         IntToHex(reply, Core::g_app_core->GetVFPReg(id - CSPR_REGISTER - 1)); // VFP registers should start at 26, so one after CSPR_REGISTER
     } else if (id == FPSCR_REGISTER) {
         IntToHex(reply, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR)); // Get FPSCR
+        IntToHex(reply + 8, 0);
     } else {
         return SendReply("E01");
     }
@@ -497,6 +498,8 @@ static void ReadRegisters() {
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
         } else if (i > CSPR_REGISTER && i < FPSCR_REGISTER) {
             IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPReg(i - CSPR_REGISTER - 1));
+            IntToHex(bufptr + (i + 1) * 8, 0);
+            i++;
         } else if (i == FPSCR_REGISTER) {
             IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR));
         }

From d7e346239bbbd6fbd28bdbe087966b0c994594a1 Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Wed, 21 Oct 2015 07:28:36 -0400
Subject: [PATCH 08/14] Update register read loops to go with last commit

---
 src/core/gdbstub/gdbstub.cpp | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 7d8e9e3fb2..e7fed68f7f 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -57,6 +57,7 @@ const u32 R0_REGISTER = 0;
 const u32 R15_REGISTER = 15;
 const u32 CSPR_REGISTER = 25;
 const u32 FPSCR_REGISTER = 58;
+const u32 MAX_REGISTERS = 90;
 
 namespace GDBStub {
 
@@ -487,7 +488,7 @@ static void ReadRegisters() {
     memset(buffer, 0, sizeof(buffer));
 
     u8* bufptr = buffer;
-    for (int i = 0; i <= FPSCR_REGISTER; i++) {
+    for (int i = 0; i <= MAX_REGISTERS; i++) {
         if (i <= R15_REGISTER) {
             IntToHex(bufptr + i * 8, Core::g_app_core->GetReg(i));
         } else if (i == CSPR_REGISTER) {
@@ -496,11 +497,11 @@ static void ReadRegisters() {
             IntToHex(bufptr + i * 8, 0);
             IntToHex(bufptr + (i + 1) * 8, 0);
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
-        } else if (i > CSPR_REGISTER && i < FPSCR_REGISTER) {
+        } else if (i > CSPR_REGISTER && i < MAX_REGISTERS) {
             IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPReg(i - CSPR_REGISTER - 1));
             IntToHex(bufptr + (i + 1) * 8, 0);
             i++;
-        } else if (i == FPSCR_REGISTER) {
+        } else if (i == MAX_REGISTERS) {
             IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR));
         }
     }
@@ -541,16 +542,16 @@ static void WriteRegisters() {
     if (command_buffer[0] != 'G')
         return SendReply("E01");
 
-    for (int i = 0; i <= FPSCR_REGISTER; i++) {
+    for (int i = 0; i <= MAX_REGISTERS; i++) {
         if (i <= R15_REGISTER) {
             Core::g_app_core->SetReg(i, HexToInt(buffer_ptr + i * 8));
         } else if (i == CSPR_REGISTER) {
             Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * 8));
         } else if (i < CSPR_REGISTER) {
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
-        } else if (i > CSPR_REGISTER && i < FPSCR_REGISTER) {
+        } else if (i > CSPR_REGISTER && i < MAX_REGISTERS) {
             Core::g_app_core->SetVFPReg(i - CSPR_REGISTER - 1, HexToInt(buffer_ptr + i * 8));
-        } else if (i == FPSCR_REGISTER) {
+        } else if (i == MAX_REGISTERS) {
             Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, HexToInt(buffer_ptr + i * 8));
         }
     }

From d1f73c424ff90d0de22bcd20d04b6ebef3a86645 Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Wed, 21 Oct 2015 07:45:35 -0400
Subject: [PATCH 09/14] Add a register variable to loops

---
 src/core/gdbstub/gdbstub.cpp | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index e7fed68f7f..6c21b59981 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -488,17 +488,18 @@ static void ReadRegisters() {
     memset(buffer, 0, sizeof(buffer));
 
     u8* bufptr = buffer;
-    for (int i = 0; i <= MAX_REGISTERS; i++) {
+    for (int i = 0, reg = 0; i <= MAX_REGISTERS; i++, reg++) {
         if (i <= R15_REGISTER) {
-            IntToHex(bufptr + i * 8, Core::g_app_core->GetReg(i));
+            IntToHex(bufptr + i * 8, Core::g_app_core->GetReg(reg));
         } else if (i == CSPR_REGISTER) {
             IntToHex(bufptr + i * 8, Core::g_app_core->GetCPSR());
         } else if (i < CSPR_REGISTER) {
             IntToHex(bufptr + i * 8, 0);
             IntToHex(bufptr + (i + 1) * 8, 0);
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
+            reg++;
         } else if (i > CSPR_REGISTER && i < MAX_REGISTERS) {
-            IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPReg(i - CSPR_REGISTER - 1));
+            IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPReg(reg - CSPR_REGISTER - 1));
             IntToHex(bufptr + (i + 1) * 8, 0);
             i++;
         } else if (i == MAX_REGISTERS) {
@@ -542,15 +543,17 @@ static void WriteRegisters() {
     if (command_buffer[0] != 'G')
         return SendReply("E01");
 
-    for (int i = 0; i <= MAX_REGISTERS; i++) {
+    for (int i = 0, reg = 0; i <= MAX_REGISTERS; i++, reg++) {
         if (i <= R15_REGISTER) {
-            Core::g_app_core->SetReg(i, HexToInt(buffer_ptr + i * 8));
+            Core::g_app_core->SetReg(reg, HexToInt(buffer_ptr + i * 8));
         } else if (i == CSPR_REGISTER) {
             Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * 8));
         } else if (i < CSPR_REGISTER) {
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
+            reg++;
         } else if (i > CSPR_REGISTER && i < MAX_REGISTERS) {
-            Core::g_app_core->SetVFPReg(i - CSPR_REGISTER - 1, HexToInt(buffer_ptr + i * 8));
+            Core::g_app_core->SetVFPReg(reg - CSPR_REGISTER - 1, HexToInt(buffer_ptr + i * 8));
+            i++; // Skip padding
         } else if (i == MAX_REGISTERS) {
             Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, HexToInt(buffer_ptr + i * 8));
         }

From a5ab8accc21e73be9564bebde4d3b20b38d85b6e Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Wed, 21 Oct 2015 22:19:55 -0400
Subject: [PATCH 10/14] Handle changes pointed out in comments on PR

---
 src/citra/citra.cpp          |  3 +-
 src/citra_qt/main.cpp        |  3 +-
 src/core/gdbstub/gdbstub.cpp | 95 +++++++++++++-----------------------
 3 files changed, 36 insertions(+), 65 deletions(-)

diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index bfbb211997..c96fc13744 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -23,6 +23,7 @@
 #include "core/settings.h"
 #include "core/system.h"
 #include "core/core.h"
+#include "core/gdbstub/gdbstub.h"
 #include "core/loader/loader.h"
 
 #include "citra/config.h"
@@ -30,8 +31,6 @@
 
 #include "video_core/video_core.h"
 
-#include "core/gdbstub/gdbstub.h"
-
 
 static void PrintHelp()
 {
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index d8d17f466e..e5ed01a11f 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -44,12 +44,11 @@
 #include "core/settings.h"
 #include "core/system.h"
 #include "core/arm/disassembler/load_symbol_map.h"
+#include "core/gdbstub/gdbstub.h"
 #include "core/loader/loader.h"
 
 #include "video_core/video_core.h"
 
-#include "core/gdbstub/gdbstub.h"
-
 GMainWindow::GMainWindow() : emu_thread(nullptr)
 {
     Pica::g_debug_context = Pica::DebugContext::Construct();
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 6c21b59981..6f9c8fa294 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -20,18 +20,18 @@
 #include <iphlpapi.h>
 #define SHUT_RDWR 2
 #else
+#include <unistd.h>
 #include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/in.h>
-#include <unistd.h>
 #endif
 
 #include "common/logging/log.h"
 #include "common/string_util.h"
-#include <core/arm/arm_interface.h>
 #include "core/core.h"
 #include "core/memory.h"
+#include "core/arm/arm_interface.h"
 #include "gdbstub.h"
 
 const int GDB_BUFFER_SIZE = 10000;
@@ -67,8 +67,7 @@ static u8 command_buffer[GDB_BUFFER_SIZE];
 static u32 command_length;
 
 static u32 latest_signal = 0;
-static u32 send_signal = 0;
-static u32 step_break = 0;
+static bool step_break = false;
 static bool memory_break = false;
 
 // Binding to a port within the reserved ports range (0-1023) requires root permissions,
@@ -356,33 +355,21 @@ static void HandleSetThread() {
     SendReply("E01");
 }
 
-/// Create and send signal packet.
-static void HandleSignal() {
-    std::string buffer = Common::StringFromFormat("T%02x%02x:%08x;%02x:%08x;", latest_signal, 15, htonl(Core::g_app_core->GetPC()), 13, htonl(Core::g_app_core->GetReg(13)));
-
-    LOG_DEBUG(Debug_GDBStub, "Response: %s", buffer.c_str());
-
-    SendReply(buffer.c_str());
-}
-
 /**
- * Set signal and send packet to client through HandleSignal if signal flag is set using SendSignal.
+ * Send signal packet to client.
  *
  * @param signal Signal to be sent to client.
  */
-int SendSignal(u32 signal) {
+void SendSignal(u32 signal) {
     if (gdbserver_socket == -1) {
-        return 1;
+        return;
     }
 
     latest_signal = signal;
 
-    if (send_signal) {
-        HandleSignal();
-        send_signal = 0;
-    }
-
-    return 0;
+    std::string buffer = Common::StringFromFormat("T%02x%02x:%08x;%02x:%08x;", latest_signal, 15, htonl(Core::g_app_core->GetPC()), 13, htonl(Core::g_app_core->GetReg(13)));
+    LOG_DEBUG(Debug_GDBStub, "Response: %s", buffer.c_str());
+    SendReply(buffer.c_str());
 }
 
 /// Read command from gdb client.
@@ -397,7 +384,6 @@ static void ReadCommand() {
     } else if (c == 0x03) {
         LOG_INFO(Debug_GDBStub, "gdb: found break command\n");
         halt_loop = true;
-        send_signal = 1;
         SendSignal(SIGTRAP);
         return;
     } else if (c != GDB_STUB_START) {
@@ -566,17 +552,14 @@ static void WriteRegisters() {
 static void ReadMemory() {
     static u8 reply[GDB_BUFFER_SIZE - 4];
 
-    int i = 1;
+    auto start_offset = command_buffer+1;
+    auto addr_pos = std::find(start_offset, command_buffer+command_length, ',');
     PAddr addr = 0;
-    while (command_buffer[i] != ',') {
-        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
-    }
-    i++;
+    HexToMem((u8*)&addr, start_offset, (addr_pos - start_offset) / 2);
 
+    start_offset = addr_pos+1;
     u32 len = 0;
-    while (i < command_length) {
-        len = (len << 4) | HexCharToValue(command_buffer[i++]);
-    }
+    HexToMem((u8*)&len, start_offset, ((command_buffer + command_length) - start_offset) / 2);
 
     if (len * 2 > sizeof(reply)) {
         SendReply("E01");
@@ -594,31 +577,28 @@ static void ReadMemory() {
 
 /// Modify location in memory with data received from the gdb client.
 static void WriteMemory() {
-    int i = 1;
+    auto start_offset = command_buffer+1;
+    auto addr_pos = std::find(start_offset, command_buffer+command_length, ',');
     PAddr addr = 0;
-    while (command_buffer[i] != ',') {
-        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
-    }
-    i++;
+    HexToMem((u8*)&addr, start_offset, (addr_pos - start_offset) / 2);
 
+    start_offset = addr_pos+1;
+    auto len_pos = std::find(start_offset, command_buffer+command_length, ':');
     u32 len = 0;
-    while (command_buffer[i] != ':') {
-        len = (len << 4) | HexCharToValue(command_buffer[i++]);
-    }
+    HexToMem((u8*)&len, start_offset, (len_pos - start_offset) / 2);
 
     u8* dst = Memory::GetPointer(addr);
     if (!dst) {
         return SendReply("E00");
     }
 
-    HexToMem(dst, command_buffer + i + 1, len);
+    HexToMem(dst, len_pos + 1, len);
     SendReply("OK");
 }
 
 void Break(bool is_memory_break) {
     if (!halt_loop) {
         halt_loop = true;
-        send_signal = 1;
         SendSignal(SIGTRAP);
     }
 
@@ -629,8 +609,7 @@ void Break(bool is_memory_break) {
 static void Step() {
     step_loop = true;
     halt_loop = true;
-    send_signal = 1;
-    step_break = 1;
+    step_break = true;
     SendSignal(SIGTRAP);
 }
 
@@ -645,7 +624,7 @@ bool IsMemoryBreak() {
 /// Tell the CPU to continue executing.
 static void Continue() {
     memory_break = false;
-    step_break = 0;
+    step_break = false;
     step_loop = false;
     halt_loop = false;
 }
@@ -694,17 +673,14 @@ static void AddBreakpoint() {
         return SendReply("E01");
     }
 
-    int i = 3;
+    auto start_offset = command_buffer+3;
+    auto addr_pos = std::find(start_offset, command_buffer+command_length, ',');
     PAddr addr = 0;
-    while (command_buffer[i] != ',') {
-        addr = addr << 4 | HexCharToValue(command_buffer[i++]);
-    }
-    i++;
+    HexToMem((u8*)&addr, start_offset, (addr_pos - start_offset) / 2);
 
+    start_offset = addr_pos+1;
     u32 len = 0;
-    while (i < command_length) {
-        len = len << 4 | HexCharToValue(command_buffer[i++]);
-    }
+    HexToMem((u8*)&len, start_offset, ((command_buffer + command_length) - start_offset) / 2);
 
     if (type == BreakpointType::Access) {
         // Access is made up of Read and Write types, so add both breakpoints
@@ -747,17 +723,14 @@ static void RemoveBreakpoint() {
         return SendReply("E01");
     }
 
-    int i = 3;
+    auto start_offset = command_buffer+3;
+    auto addr_pos = std::find(start_offset, command_buffer+command_length, ',');
     PAddr addr = 0;
-    while (command_buffer[i] != ',') {
-        addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
-    }
-    i++;
+    HexToMem((u8*)&addr, start_offset, (addr_pos - start_offset) / 2);
 
+    start_offset = addr_pos+1;
     u32 len = 0;
-    while (i < command_length) {
-        len = (len << 4) | HexCharToValue(command_buffer[i++]);
-    }
+    HexToMem((u8*)&len, start_offset, ((command_buffer + command_length) - start_offset) / 2);
 
     if (type == BreakpointType::Access) {
         // Access is made up of Read and Write types, so add both breakpoints
@@ -795,7 +768,7 @@ void HandlePacket() {
         HandleSetThread();
         break;
     case '?':
-        HandleSignal();
+        SendSignal(latest_signal);
         break;
     case 'k':
         Shutdown();

From 45ed9e7e5e9709ac6e27402a9000b13a30bc80bf Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Thu, 22 Oct 2015 00:19:44 -0400
Subject: [PATCH 11/14] Use CHAR_BIT instead of 8

---
 src/core/gdbstub/gdbstub.cpp | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 6f9c8fa294..b226b431a5 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -476,20 +476,20 @@ static void ReadRegisters() {
     u8* bufptr = buffer;
     for (int i = 0, reg = 0; i <= MAX_REGISTERS; i++, reg++) {
         if (i <= R15_REGISTER) {
-            IntToHex(bufptr + i * 8, Core::g_app_core->GetReg(reg));
+            IntToHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetReg(reg));
         } else if (i == CSPR_REGISTER) {
-            IntToHex(bufptr + i * 8, Core::g_app_core->GetCPSR());
+            IntToHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetCPSR());
         } else if (i < CSPR_REGISTER) {
-            IntToHex(bufptr + i * 8, 0);
-            IntToHex(bufptr + (i + 1) * 8, 0);
+            IntToHex(bufptr + i * CHAR_BIT, 0);
+            IntToHex(bufptr + (i + 1) * CHAR_BIT, 0);
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
             reg++;
         } else if (i > CSPR_REGISTER && i < MAX_REGISTERS) {
-            IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPReg(reg - CSPR_REGISTER - 1));
-            IntToHex(bufptr + (i + 1) * 8, 0);
+            IntToHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetVFPReg(reg - CSPR_REGISTER - 1));
+            IntToHex(bufptr + (i + 1) * CHAR_BIT, 0);
             i++;
         } else if (i == MAX_REGISTERS) {
-            IntToHex(bufptr + i * 8, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR));
+            IntToHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR));
         }
     }
 
@@ -531,17 +531,17 @@ static void WriteRegisters() {
 
     for (int i = 0, reg = 0; i <= MAX_REGISTERS; i++, reg++) {
         if (i <= R15_REGISTER) {
-            Core::g_app_core->SetReg(reg, HexToInt(buffer_ptr + i * 8));
+            Core::g_app_core->SetReg(reg, HexToInt(buffer_ptr + i * CHAR_BIT));
         } else if (i == CSPR_REGISTER) {
-            Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * 8));
+            Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * CHAR_BIT));
         } else if (i < CSPR_REGISTER) {
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
             reg++;
         } else if (i > CSPR_REGISTER && i < MAX_REGISTERS) {
-            Core::g_app_core->SetVFPReg(reg - CSPR_REGISTER - 1, HexToInt(buffer_ptr + i * 8));
+            Core::g_app_core->SetVFPReg(reg - CSPR_REGISTER - 1, HexToInt(buffer_ptr + i * CHAR_BIT));
             i++; // Skip padding
         } else if (i == MAX_REGISTERS) {
-            Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, HexToInt(buffer_ptr + i * 8));
+            Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, HexToInt(buffer_ptr + i * CHAR_BIT));
         }
     }
 

From a7eb6a4045c2fa2b9b674ddc6c749c1cb7532b9d Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Thu, 22 Oct 2015 00:31:49 -0400
Subject: [PATCH 12/14] Add some headers so TravisCI will hopefully work

---
 src/core/gdbstub/gdbstub.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index b226b431a5..5568df21b2 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -4,11 +4,13 @@
 
 // Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
 
+#include <algorithm>
 #include <csignal>
 #include <cstdarg>
 #include <cstdio>
 #include <cstring>
 #include <fcntl.h>
+#include <limits.h>
 #include <map>
 #include <numeric>
 

From b6422038b57c32c9842d8da506d40cc6872a1546 Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Thu, 29 Oct 2015 06:17:29 -0400
Subject: [PATCH 13/14] Change headers

---
 src/core/gdbstub/gdbstub.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index 5568df21b2..e37a2efd7a 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -5,12 +5,12 @@
 // Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
 
 #include <algorithm>
+#include <climits>
 #include <csignal>
 #include <cstdarg>
 #include <cstdio>
 #include <cstring>
 #include <fcntl.h>
-#include <limits.h>
 #include <map>
 #include <numeric>
 
@@ -34,7 +34,7 @@
 #include "core/core.h"
 #include "core/memory.h"
 #include "core/arm/arm_interface.h"
-#include "gdbstub.h"
+#include "core/gdbstub/gdbstub.h"
 
 const int GDB_BUFFER_SIZE = 10000;
 

From bcea9599100a0df945629cd50be066ae9dabf89f Mon Sep 17 00:00:00 2001
From: polaris- <nagatospam@gmail.com>
Date: Tue, 3 Nov 2015 21:50:53 -0500
Subject: [PATCH 14/14] Fix bug with reading addresses and lengths

---
 src/core/gdbstub/gdbstub.cpp | 100 +++++++++++++++++++----------------
 1 file changed, 55 insertions(+), 45 deletions(-)

diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
index e37a2efd7a..003ce41672 100644
--- a/src/core/gdbstub/gdbstub.cpp
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -126,6 +126,22 @@ static u8 NibbleToHex(u8 n) {
     }
 }
 
+/**
+* Converts input hex string characters into an array of equivalent of u8 bytes.
+*
+* @param dest Pointer to buffer to store u8 bytes.
+* @param src Pointer to array of output hex string characters.
+* @param len Length of src array.
+*/
+static u32 HexToInt(u8* src, u32 len) {
+    u32 output = 0;
+    while (len-- > 0) {
+        output = (output << 4) | HexCharToValue(src[0]);
+        src++;
+    }
+    return output;
+}
+
 /**
  * Converts input array of u8 bytes into their equivalent hex string characters.
  *
@@ -133,7 +149,7 @@ static u8 NibbleToHex(u8 n) {
  * @param src Pointer to array of u8 bytes.
  * @param len Length of src array.
  */
-static void MemToHex(u8* dest, u8* src, u32 len) {
+static void MemToGdbHex(u8* dest, u8* src, u32 len) {
     while (len-- > 0) {
         u8 tmp = *src++;
         *dest++ = NibbleToHex(tmp >> 4);
@@ -142,13 +158,13 @@ static void MemToHex(u8* dest, u8* src, u32 len) {
 }
 
 /**
- * Converts input hex string characters into an array of equivalent of u8 bytes.
+ * Converts input gdb-formatted hex string characters into an array of equivalent of u8 bytes.
  *
  * @param dest Pointer to buffer to store u8 bytes.
  * @param src Pointer to array of output hex string characters.
  * @param len Length of src array.
  */
-static void HexToMem(u8* dest, u8* src, u32 len) {
+static void GdbHexToMem(u8* dest, u8* src, u32 len) {
     while (len-- > 0) {
         *dest++ = (HexCharToValue(src[0]) << 4) | HexCharToValue(src[1]);
         src += 2;
@@ -156,11 +172,11 @@ static void HexToMem(u8* dest, u8* src, u32 len) {
 }
 
 /**
- * Convert a u32 into a hex string.
+ * Convert a u32 into a gdb-formatted hex string.
  *
  * @param dest Pointer to buffer to store output hex string characters.
  */
-static void IntToHex(u8* dest, u32 v) {
+static void IntToGdbHex(u8* dest, u32 v) {
     for (int i = 0; i < 8; i += 2) {
         dest[i + 1] = NibbleToHex(v >> (4 * i));
         dest[i] = NibbleToHex(v >> (4 * (i + 1)));
@@ -168,11 +184,11 @@ static void IntToHex(u8* dest, u32 v) {
 }
 
 /**
- * Convert a hex string into a u32.
+ * Convert a gdb-formatted hex string into a u32.
  *
  * @param src Pointer to hex string.
  */
-static u32 HexToInt(u8* src) {
+static u32 GdbHexToInt(u8* src) {
     u32 output = 0;
 
     for (int i = 0; i < 8; i += 2) {
@@ -455,14 +471,14 @@ static void ReadRegister() {
     }
 
     if (id >= R0_REGISTER && id <= R15_REGISTER) {
-        IntToHex(reply, Core::g_app_core->GetReg(id));
+        IntToGdbHex(reply, Core::g_app_core->GetReg(id));
     } else if (id == CSPR_REGISTER) {
-        IntToHex(reply, Core::g_app_core->GetCPSR());
+        IntToGdbHex(reply, Core::g_app_core->GetCPSR());
     } else if (id > CSPR_REGISTER && id < FPSCR_REGISTER) {
-        IntToHex(reply, Core::g_app_core->GetVFPReg(id - CSPR_REGISTER - 1)); // VFP registers should start at 26, so one after CSPR_REGISTER
+        IntToGdbHex(reply, Core::g_app_core->GetVFPReg(id - CSPR_REGISTER - 1)); // VFP registers should start at 26, so one after CSPR_REGISTER
     } else if (id == FPSCR_REGISTER) {
-        IntToHex(reply, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR)); // Get FPSCR
-        IntToHex(reply + 8, 0);
+        IntToGdbHex(reply, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR)); // Get FPSCR
+        IntToGdbHex(reply + 8, 0);
     } else {
         return SendReply("E01");
     }
@@ -478,20 +494,20 @@ static void ReadRegisters() {
     u8* bufptr = buffer;
     for (int i = 0, reg = 0; i <= MAX_REGISTERS; i++, reg++) {
         if (i <= R15_REGISTER) {
-            IntToHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetReg(reg));
+            IntToGdbHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetReg(reg));
         } else if (i == CSPR_REGISTER) {
-            IntToHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetCPSR());
+            IntToGdbHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetCPSR());
         } else if (i < CSPR_REGISTER) {
-            IntToHex(bufptr + i * CHAR_BIT, 0);
-            IntToHex(bufptr + (i + 1) * CHAR_BIT, 0);
+            IntToGdbHex(bufptr + i * CHAR_BIT, 0);
+            IntToGdbHex(bufptr + (i + 1) * CHAR_BIT, 0);
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
             reg++;
         } else if (i > CSPR_REGISTER && i < MAX_REGISTERS) {
-            IntToHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetVFPReg(reg - CSPR_REGISTER - 1));
-            IntToHex(bufptr + (i + 1) * CHAR_BIT, 0);
+            IntToGdbHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetVFPReg(reg - CSPR_REGISTER - 1));
+            IntToGdbHex(bufptr + (i + 1) * CHAR_BIT, 0);
             i++;
         } else if (i == MAX_REGISTERS) {
-            IntToHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR));
+            IntToGdbHex(bufptr + i * CHAR_BIT, Core::g_app_core->GetVFPSystemReg(VFP_FPSCR));
         }
     }
 
@@ -510,13 +526,13 @@ static void WriteRegister() {
     }
 
     if (id >= R0_REGISTER && id <= R15_REGISTER) {
-        Core::g_app_core->SetReg(id, HexToInt(buffer_ptr));
+        Core::g_app_core->SetReg(id, GdbHexToInt(buffer_ptr));
     } else if (id == CSPR_REGISTER) {
-        Core::g_app_core->SetCPSR(HexToInt(buffer_ptr));
+        Core::g_app_core->SetCPSR(GdbHexToInt(buffer_ptr));
     } else if (id > CSPR_REGISTER && id < FPSCR_REGISTER) {
-        Core::g_app_core->SetVFPReg(id - CSPR_REGISTER - 1, HexToInt(buffer_ptr));
+        Core::g_app_core->SetVFPReg(id - CSPR_REGISTER - 1, GdbHexToInt(buffer_ptr));
     } else if (id == FPSCR_REGISTER) {
-        Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, HexToInt(buffer_ptr));
+        Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, GdbHexToInt(buffer_ptr));
     } else {
         return SendReply("E01");
     }
@@ -533,17 +549,17 @@ static void WriteRegisters() {
 
     for (int i = 0, reg = 0; i <= MAX_REGISTERS; i++, reg++) {
         if (i <= R15_REGISTER) {
-            Core::g_app_core->SetReg(reg, HexToInt(buffer_ptr + i * CHAR_BIT));
+            Core::g_app_core->SetReg(reg, GdbHexToInt(buffer_ptr + i * CHAR_BIT));
         } else if (i == CSPR_REGISTER) {
-            Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * CHAR_BIT));
+            Core::g_app_core->SetCPSR(GdbHexToInt(buffer_ptr + i * CHAR_BIT));
         } else if (i < CSPR_REGISTER) {
             i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
             reg++;
         } else if (i > CSPR_REGISTER && i < MAX_REGISTERS) {
-            Core::g_app_core->SetVFPReg(reg - CSPR_REGISTER - 1, HexToInt(buffer_ptr + i * CHAR_BIT));
+            Core::g_app_core->SetVFPReg(reg - CSPR_REGISTER - 1, GdbHexToInt(buffer_ptr + i * CHAR_BIT));
             i++; // Skip padding
         } else if (i == MAX_REGISTERS) {
-            Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, HexToInt(buffer_ptr + i * CHAR_BIT));
+            Core::g_app_core->SetVFPSystemReg(VFP_FPSCR, GdbHexToInt(buffer_ptr + i * CHAR_BIT));
         }
     }
 
@@ -556,12 +572,12 @@ static void ReadMemory() {
 
     auto start_offset = command_buffer+1;
     auto addr_pos = std::find(start_offset, command_buffer+command_length, ',');
-    PAddr addr = 0;
-    HexToMem((u8*)&addr, start_offset, (addr_pos - start_offset) / 2);
+    PAddr addr = HexToInt(start_offset, addr_pos - start_offset);
 
     start_offset = addr_pos+1;
-    u32 len = 0;
-    HexToMem((u8*)&len, start_offset, ((command_buffer + command_length) - start_offset) / 2);
+    u32 len = HexToInt(start_offset, (command_buffer + command_length) - start_offset);
+
+    LOG_DEBUG(Debug_GDBStub, "gdb: addr: %08x len: %08x\n", addr, len);
 
     if (len * 2 > sizeof(reply)) {
         SendReply("E01");
@@ -572,7 +588,7 @@ static void ReadMemory() {
         return SendReply("E0");
     }
 
-    MemToHex(reply, data, len);
+    MemToGdbHex(reply, data, len);
     reply[len * 2] = '\0';
     SendReply(reinterpret_cast<char*>(reply));
 }
@@ -581,20 +597,18 @@ static void ReadMemory() {
 static void WriteMemory() {
     auto start_offset = command_buffer+1;
     auto addr_pos = std::find(start_offset, command_buffer+command_length, ',');
-    PAddr addr = 0;
-    HexToMem((u8*)&addr, start_offset, (addr_pos - start_offset) / 2);
+    PAddr addr = HexToInt(start_offset, addr_pos - start_offset);
 
     start_offset = addr_pos+1;
     auto len_pos = std::find(start_offset, command_buffer+command_length, ':');
-    u32 len = 0;
-    HexToMem((u8*)&len, start_offset, (len_pos - start_offset) / 2);
+    u32 len = HexToInt(start_offset, len_pos - start_offset);
 
     u8* dst = Memory::GetPointer(addr);
     if (!dst) {
         return SendReply("E00");
     }
 
-    HexToMem(dst, len_pos + 1, len);
+    GdbHexToMem(dst, len_pos + 1, len);
     SendReply("OK");
 }
 
@@ -677,12 +691,10 @@ static void AddBreakpoint() {
 
     auto start_offset = command_buffer+3;
     auto addr_pos = std::find(start_offset, command_buffer+command_length, ',');
-    PAddr addr = 0;
-    HexToMem((u8*)&addr, start_offset, (addr_pos - start_offset) / 2);
+    PAddr addr = HexToInt(start_offset, addr_pos - start_offset);
 
     start_offset = addr_pos+1;
-    u32 len = 0;
-    HexToMem((u8*)&len, start_offset, ((command_buffer + command_length) - start_offset) / 2);
+    u32 len = HexToInt(start_offset, (command_buffer + command_length) - start_offset);
 
     if (type == BreakpointType::Access) {
         // Access is made up of Read and Write types, so add both breakpoints
@@ -727,12 +739,10 @@ static void RemoveBreakpoint() {
 
     auto start_offset = command_buffer+3;
     auto addr_pos = std::find(start_offset, command_buffer+command_length, ',');
-    PAddr addr = 0;
-    HexToMem((u8*)&addr, start_offset, (addr_pos - start_offset) / 2);
+    PAddr addr = HexToInt(start_offset, addr_pos - start_offset);
 
     start_offset = addr_pos+1;
-    u32 len = 0;
-    HexToMem((u8*)&len, start_offset, ((command_buffer + command_length) - start_offset) / 2);
+    u32 len = HexToInt(start_offset, (command_buffer + command_length) - start_offset);
 
     if (type == BreakpointType::Access) {
         // Access is made up of Read and Write types, so add both breakpoints