summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar polaris-2015-09-02 08:56:38 -0400
committerGravatar polaris-2015-10-04 11:16:59 -0400
commit31dee93e849d79a91f280faf16941806e3cb3c6b (patch)
tree22f64217b38dfa38b25a772f9fc5a9b025e1cbd6 /src
parentOS X build uploading: auto-confirm SSH host key (diff)
downloadyuzu-31dee93e849d79a91f280faf16941806e3cb3c6b.tar.gz
yuzu-31dee93e849d79a91f280faf16941806e3cb3c6b.tar.xz
yuzu-31dee93e849d79a91f280faf16941806e3cb3c6b.zip
Implement gdbstub
Diffstat (limited to 'src')
-rw-r--r--src/citra/citra.cpp3
-rw-r--r--src/citra/config.cpp4
-rw-r--r--src/citra/default_ini.h5
-rw-r--r--src/citra_qt/config.cpp10
-rw-r--r--src/citra_qt/main.cpp12
-rw-r--r--src/citra_qt/main.h1
-rw-r--r--src/citra_qt/main.ui9
-rw-r--r--src/common/logging/backend.cpp1
-rw-r--r--src/common/logging/log.h1
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/arm/dyncom/arm_dyncom_interpreter.cpp41
-rw-r--r--src/core/arm/skyeye_common/armstate.cpp35
-rw-r--r--src/core/arm/skyeye_common/armstate.h2
-rw-r--r--src/core/core.cpp17
-rw-r--r--src/core/gdbstub/gdbstub.cpp940
-rw-r--r--src/core/gdbstub/gdbstub.h89
-rw-r--r--src/core/settings.h5
-rw-r--r--src/core/system.cpp6
18 files changed, 1174 insertions, 9 deletions
diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index 46f4a07c9..b36865395 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -30,6 +30,8 @@
30 30
31#include "video_core/video_core.h" 31#include "video_core/video_core.h"
32 32
33#include "core/gdbstub/gdbstub.h"
34
33 35
34static void PrintHelp() 36static void PrintHelp()
35{ 37{
@@ -72,6 +74,7 @@ int main(int argc, char **argv) {
72 Config config; 74 Config config;
73 log_filter.ParseFilterString(Settings::values.log_filter); 75 log_filter.ParseFilterString(Settings::values.log_filter);
74 76
77 GDBStub::SetServerPort(static_cast<u32>(Settings::values.gdbstub_port));
75 78
76 EmuWindow_GLFW* emu_window = new EmuWindow_GLFW; 79 EmuWindow_GLFW* emu_window = new EmuWindow_GLFW;
77 80
diff --git a/src/citra/config.cpp b/src/citra/config.cpp
index 8a98bda87..af343e9fe 100644
--- a/src/citra/config.cpp
+++ b/src/citra/config.cpp
@@ -75,6 +75,10 @@ void Config::ReadValues() {
75 75
76 // Miscellaneous 76 // Miscellaneous
77 Settings::values.log_filter = glfw_config->Get("Miscellaneous", "log_filter", "*:Info"); 77 Settings::values.log_filter = glfw_config->Get("Miscellaneous", "log_filter", "*:Info");
78
79 // GDBStubebugging
80 Settings::values.use_gdbstub = glfw_config->GetBoolean("Debugging", "use_gdbstub", false);
81 Settings::values.gdbstub_port = glfw_config->GetInteger("Debugging", "gdbstub_port", 24689);
78} 82}
79 83
80void Config::Reload() { 84void Config::Reload() {
diff --git a/src/citra/default_ini.h b/src/citra/default_ini.h
index 7e5d49729..5ba40a8ed 100644
--- a/src/citra/default_ini.h
+++ b/src/citra/default_ini.h
@@ -66,6 +66,11 @@ region_value =
66# A filter which removes logs below a certain logging level. 66# A filter which removes logs below a certain logging level.
67# Examples: *:Debug Kernel.SVC:Trace Service.*:Critical 67# Examples: *:Debug Kernel.SVC:Trace Service.*:Critical
68log_filter = *:Info 68log_filter = *:Info
69
70[Debugging]
71# Port for listening to GDB connections.
72use_gdbstub=false
73gdbstub_port=24689
69)"; 74)";
70 75
71} 76}
diff --git a/src/citra_qt/config.cpp b/src/citra_qt/config.cpp
index 1f4981ce1..8e247ff5c 100644
--- a/src/citra_qt/config.cpp
+++ b/src/citra_qt/config.cpp
@@ -62,6 +62,11 @@ void Config::ReadValues() {
62 qt_config->beginGroup("Miscellaneous"); 62 qt_config->beginGroup("Miscellaneous");
63 Settings::values.log_filter = qt_config->value("log_filter", "*:Info").toString().toStdString(); 63 Settings::values.log_filter = qt_config->value("log_filter", "*:Info").toString().toStdString();
64 qt_config->endGroup(); 64 qt_config->endGroup();
65
66 qt_config->beginGroup("Debugging");
67 Settings::values.use_gdbstub = qt_config->value("use_gdbstub", false).toBool();
68 Settings::values.gdbstub_port = qt_config->value("gdbstub_port", 24689).toInt();
69 qt_config->endGroup();
65} 70}
66 71
67void Config::SaveValues() { 72void Config::SaveValues() {
@@ -97,6 +102,11 @@ void Config::SaveValues() {
97 qt_config->beginGroup("Miscellaneous"); 102 qt_config->beginGroup("Miscellaneous");
98 qt_config->setValue("log_filter", QString::fromStdString(Settings::values.log_filter)); 103 qt_config->setValue("log_filter", QString::fromStdString(Settings::values.log_filter));
99 qt_config->endGroup(); 104 qt_config->endGroup();
105
106 qt_config->beginGroup("Debugging");
107 qt_config->setValue("use_gdbstub", Settings::values.use_gdbstub);
108 qt_config->setValue("gdbstub_port", Settings::values.gdbstub_port);
109 qt_config->endGroup();
100} 110}
101 111
102void Config::Reload() { 112void Config::Reload() {
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index 298649aaf..d8d17f466 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -48,6 +48,8 @@
48 48
49#include "video_core/video_core.h" 49#include "video_core/video_core.h"
50 50
51#include "core/gdbstub/gdbstub.h"
52
51GMainWindow::GMainWindow() : emu_thread(nullptr) 53GMainWindow::GMainWindow() : emu_thread(nullptr)
52{ 54{
53 Pica::g_debug_context = Pica::DebugContext::Construct(); 55 Pica::g_debug_context = Pica::DebugContext::Construct();
@@ -143,6 +145,11 @@ GMainWindow::GMainWindow() : emu_thread(nullptr)
143 145
144 game_list->LoadInterfaceLayout(settings); 146 game_list->LoadInterfaceLayout(settings);
145 147
148 ui.action_Use_Gdbstub->setChecked(Settings::values.use_gdbstub);
149 SetGdbstubEnabled(ui.action_Use_Gdbstub->isChecked());
150
151 GDBStub::SetServerPort(static_cast<u32>(Settings::values.gdbstub_port));
152
146 ui.action_Use_Hardware_Renderer->setChecked(Settings::values.use_hw_renderer); 153 ui.action_Use_Hardware_Renderer->setChecked(Settings::values.use_hw_renderer);
147 SetHardwareRendererEnabled(ui.action_Use_Hardware_Renderer->isChecked()); 154 SetHardwareRendererEnabled(ui.action_Use_Hardware_Renderer->isChecked());
148 155
@@ -175,6 +182,7 @@ GMainWindow::GMainWindow() : emu_thread(nullptr)
175 connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame())); 182 connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame()));
176 connect(ui.action_Use_Hardware_Renderer, SIGNAL(triggered(bool)), this, SLOT(SetHardwareRendererEnabled(bool))); 183 connect(ui.action_Use_Hardware_Renderer, SIGNAL(triggered(bool)), this, SLOT(SetHardwareRendererEnabled(bool)));
177 connect(ui.action_Use_Shader_JIT, SIGNAL(triggered(bool)), this, SLOT(SetShaderJITEnabled(bool))); 184 connect(ui.action_Use_Shader_JIT, SIGNAL(triggered(bool)), this, SLOT(SetShaderJITEnabled(bool)));
185 connect(ui.action_Use_Gdbstub, SIGNAL(triggered(bool)), this, SLOT(SetGdbstubEnabled(bool)));
178 connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode())); 186 connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode()));
179 connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog())); 187 connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog()));
180 188
@@ -445,6 +453,10 @@ void GMainWindow::SetHardwareRendererEnabled(bool enabled) {
445 VideoCore::g_hw_renderer_enabled = enabled; 453 VideoCore::g_hw_renderer_enabled = enabled;
446} 454}
447 455
456void GMainWindow::SetGdbstubEnabled(bool enabled) {
457 GDBStub::ToggleServer(enabled);
458}
459
448void GMainWindow::SetShaderJITEnabled(bool enabled) { 460void GMainWindow::SetShaderJITEnabled(bool enabled) {
449 VideoCore::g_shader_jit_enabled = enabled; 461 VideoCore::g_shader_jit_enabled = enabled;
450} 462}
diff --git a/src/citra_qt/main.h b/src/citra_qt/main.h
index 6d27ce6a9..f6d429cd9 100644
--- a/src/citra_qt/main.h
+++ b/src/citra_qt/main.h
@@ -99,6 +99,7 @@ private slots:
99 void OnConfigure(); 99 void OnConfigure();
100 void OnDisplayTitleBars(bool); 100 void OnDisplayTitleBars(bool);
101 void SetHardwareRendererEnabled(bool); 101 void SetHardwareRendererEnabled(bool);
102 void SetGdbstubEnabled(bool);
102 void SetShaderJITEnabled(bool); 103 void SetShaderJITEnabled(bool);
103 void ToggleWindowMode(); 104 void ToggleWindowMode();
104 105
diff --git a/src/citra_qt/main.ui b/src/citra_qt/main.ui
index 997597642..1e8a07cfb 100644
--- a/src/citra_qt/main.ui
+++ b/src/citra_qt/main.ui
@@ -75,6 +75,7 @@
75 <addaction name="separator"/> 75 <addaction name="separator"/>
76 <addaction name="action_Use_Hardware_Renderer"/> 76 <addaction name="action_Use_Hardware_Renderer"/>
77 <addaction name="action_Use_Shader_JIT"/> 77 <addaction name="action_Use_Shader_JIT"/>
78 <addaction name="action_Use_Gdbstub"/>
78 <addaction name="action_Configure"/> 79 <addaction name="action_Configure"/>
79 </widget> 80 </widget>
80 <widget class="QMenu" name="menu_View"> 81 <widget class="QMenu" name="menu_View">
@@ -170,6 +171,14 @@
170 <string>Use Shader JIT</string> 171 <string>Use Shader JIT</string>
171 </property> 172 </property>
172 </action> 173 </action>
174 <action name="action_Use_Gdbstub">
175 <property name="checkable">
176 <bool>true</bool>
177 </property>
178 <property name="text">
179 <string>Use Gdbstub</string>
180 </property>
181 </action>
173 <action name="action_Configure"> 182 <action name="action_Configure">
174 <property name="text"> 183 <property name="text">
175 <string>Configure ...</string> 184 <string>Configure ...</string>
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 92e8e742d..21a9ae8d0 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -29,6 +29,7 @@ namespace Log {
29 SUB(Debug, Emulated) \ 29 SUB(Debug, Emulated) \
30 SUB(Debug, GPU) \ 30 SUB(Debug, GPU) \
31 SUB(Debug, Breakpoint) \ 31 SUB(Debug, Breakpoint) \
32 SUB(Debug, GDBStub) \
32 CLS(Kernel) \ 33 CLS(Kernel) \
33 SUB(Kernel, SVC) \ 34 SUB(Kernel, SVC) \
34 CLS(Service) \ 35 CLS(Service) \
diff --git a/src/common/logging/log.h b/src/common/logging/log.h
index 5fd3bd7f5..43f0c59e4 100644
--- a/src/common/logging/log.h
+++ b/src/common/logging/log.h
@@ -43,6 +43,7 @@ enum class Class : ClassType {
43 Debug_Emulated, ///< Debug messages from the emulated programs 43 Debug_Emulated, ///< Debug messages from the emulated programs
44 Debug_GPU, ///< GPU debugging tools 44 Debug_GPU, ///< GPU debugging tools
45 Debug_Breakpoint, ///< Logging breakpoints and watchpoints 45 Debug_Breakpoint, ///< Logging breakpoints and watchpoints
46 Debug_GDBStub, ///< GDB Stub
46 Kernel, ///< The HLE implementation of the CTR kernel 47 Kernel, ///< The HLE implementation of the CTR kernel
47 Kernel_SVC, ///< Kernel system calls 48 Kernel_SVC, ///< Kernel system calls
48 Service, ///< HLE implementation of system services. Each major service 49 Service, ///< HLE implementation of system services. Each major service
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index c17290b9b..861b711c7 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -22,6 +22,7 @@ set(SRCS
22 file_sys/archive_systemsavedata.cpp 22 file_sys/archive_systemsavedata.cpp
23 file_sys/disk_archive.cpp 23 file_sys/disk_archive.cpp
24 file_sys/ivfc_archive.cpp 24 file_sys/ivfc_archive.cpp
25 gdbstub/gdbstub.cpp
25 hle/config_mem.cpp 26 hle/config_mem.cpp
26 hle/hle.cpp 27 hle/hle.cpp
27 hle/applets/applet.cpp 28 hle/applets/applet.cpp
@@ -149,6 +150,7 @@ set(HEADERS
149 file_sys/disk_archive.h 150 file_sys/disk_archive.h
150 file_sys/file_backend.h 151 file_sys/file_backend.h
151 file_sys/ivfc_archive.h 152 file_sys/ivfc_archive.h
153 gdbstub/gdbstub.h
152 hle/config_mem.h 154 hle/config_mem.h
153 hle/function_wrappers.h 155 hle/function_wrappers.h
154 hle/hle.h 156 hle/hle.h
diff --git a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
index fbd6f94f9..8293f4c60 100644
--- a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
@@ -23,6 +23,8 @@
23#include "core/arm/skyeye_common/armsupp.h" 23#include "core/arm/skyeye_common/armsupp.h"
24#include "core/arm/skyeye_common/vfp/vfp.h" 24#include "core/arm/skyeye_common/vfp/vfp.h"
25 25
26#include "core/gdbstub/gdbstub.h"
27
26Common::Profiling::TimingCategory profile_execute("DynCom::Execute"); 28Common::Profiling::TimingCategory profile_execute("DynCom::Execute");
27Common::Profiling::TimingCategory profile_decode("DynCom::Decode"); 29Common::Profiling::TimingCategory profile_decode("DynCom::Decode");
28 30
@@ -3548,6 +3550,7 @@ static int InterpreterTranslate(ARMul_State* cpu, int& bb_start, u32 addr) {
3548 CITRA_IGNORE_EXIT(-1); 3550 CITRA_IGNORE_EXIT(-1);
3549 } 3551 }
3550 inst_base = arm_instruction_trans[idx](inst, idx); 3552 inst_base = arm_instruction_trans[idx](inst, idx);
3553
3551translated: 3554translated:
3552 phys_addr += inst_size; 3555 phys_addr += inst_size;
3553 3556
@@ -3580,6 +3583,8 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
3580 Common::Profiling::ScopeTimer timer_execute(profile_execute); 3583 Common::Profiling::ScopeTimer timer_execute(profile_execute);
3581 MICROPROFILE_SCOPE(DynCom_Execute); 3584 MICROPROFILE_SCOPE(DynCom_Execute);
3582 3585
3586 int breakpoint_offset = -1;
3587
3583 #undef RM 3588 #undef RM
3584 #undef RS 3589 #undef RS
3585 3590
@@ -3604,15 +3609,27 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
3604 #define INC_PC(l) ptr += sizeof(arm_inst) + l 3609 #define INC_PC(l) ptr += sizeof(arm_inst) + l
3605 #define INC_PC_STUB ptr += sizeof(arm_inst) 3610 #define INC_PC_STUB ptr += sizeof(arm_inst)
3606 3611
3612#define GDB_BP_CHECK \
3613 cpu->Cpsr &= ~(1 << 5); \
3614 cpu->Cpsr |= cpu->TFlag << 5; \
3615 if (GDBStub::g_server_enabled) { \
3616 if (GDBStub::IsMemoryBreak() || PC == breakpoint_offset) { \
3617 GDBStub::Break(); \
3618 goto END; \
3619 } \
3620 }
3621
3607// GCC and Clang have a C++ extension to support a lookup table of labels. Otherwise, fallback to a 3622// GCC and Clang have a C++ extension to support a lookup table of labels. Otherwise, fallback to a
3608// clunky switch statement. 3623// clunky switch statement.
3609#if defined __GNUC__ || defined __clang__ 3624#if defined __GNUC__ || defined __clang__
3610#define GOTO_NEXT_INST \ 3625#define GOTO_NEXT_INST \
3626 GDB_BP_CHECK; \
3611 if (num_instrs >= cpu->NumInstrsToExecute) goto END; \ 3627 if (num_instrs >= cpu->NumInstrsToExecute) goto END; \
3612 num_instrs++; \ 3628 num_instrs++; \
3613 goto *InstLabel[inst_base->idx] 3629 goto *InstLabel[inst_base->idx]
3614#else 3630#else
3615#define GOTO_NEXT_INST \ 3631#define GOTO_NEXT_INST \
3632 GDB_BP_CHECK; \
3616 if (num_instrs >= cpu->NumInstrsToExecute) goto END; \ 3633 if (num_instrs >= cpu->NumInstrsToExecute) goto END; \
3617 num_instrs++; \ 3634 num_instrs++; \
3618 switch(inst_base->idx) { \ 3635 switch(inst_base->idx) { \
@@ -3878,6 +3895,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
3878 unsigned int addr; 3895 unsigned int addr;
3879 unsigned int num_instrs = 0; 3896 unsigned int num_instrs = 0;
3880 3897
3898
3881 int ptr; 3899 int ptr;
3882 3900
3883 LOAD_NZCVT; 3901 LOAD_NZCVT;
@@ -3903,6 +3921,11 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
3903 goto END; 3921 goto END;
3904 } 3922 }
3905 3923
3924 // Find breakpoint if one exists within the block
3925 if (GDBStub::g_server_enabled && GDBStub::IsConnected()) {
3926 breakpoint_offset = GDBStub::GetNextBreakpointFromAddress(cpu->Reg[15], GDBStub::BreakpointType::Execute);
3927 }
3928
3906 inst_base = (arm_inst *)&inst_buf[ptr]; 3929 inst_base = (arm_inst *)&inst_buf[ptr];
3907 GOTO_NEXT_INST; 3930 GOTO_NEXT_INST;
3908 } 3931 }
@@ -4454,7 +4477,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
4454 ldst_inst* inst_cream = (ldst_inst*)inst_base->component; 4477 ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
4455 inst_cream->get_addr(cpu, inst_cream->inst, addr); 4478 inst_cream->get_addr(cpu, inst_cream->inst, addr);
4456 4479
4457 cpu->Reg[BITS(inst_cream->inst, 12, 15)] = Memory::Read8(addr); 4480 cpu->Reg[BITS(inst_cream->inst, 12, 15)] = cpu->ReadMemory8(addr);
4458 4481
4459 if (BITS(inst_cream->inst, 12, 15) == 15) { 4482 if (BITS(inst_cream->inst, 12, 15) == 15) {
4460 INC_PC(sizeof(ldst_inst)); 4483 INC_PC(sizeof(ldst_inst));
@@ -4472,7 +4495,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
4472 ldst_inst* inst_cream = (ldst_inst*)inst_base->component; 4495 ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
4473 inst_cream->get_addr(cpu, inst_cream->inst, addr); 4496 inst_cream->get_addr(cpu, inst_cream->inst, addr);
4474 4497
4475 cpu->Reg[BITS(inst_cream->inst, 12, 15)] = Memory::Read8(addr); 4498 cpu->Reg[BITS(inst_cream->inst, 12, 15)] = cpu->ReadMemory8(addr);
4476 4499
4477 if (BITS(inst_cream->inst, 12, 15) == 15) { 4500 if (BITS(inst_cream->inst, 12, 15) == 15) {
4478 INC_PC(sizeof(ldst_inst)); 4501 INC_PC(sizeof(ldst_inst));
@@ -4531,7 +4554,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
4531 4554
4532 cpu->SetExclusiveMemoryAddress(read_addr); 4555 cpu->SetExclusiveMemoryAddress(read_addr);
4533 4556
4534 RD = Memory::Read8(read_addr); 4557 RD = cpu->ReadMemory8(read_addr);
4535 if (inst_cream->Rd == 15) { 4558 if (inst_cream->Rd == 15) {
4536 INC_PC(sizeof(generic_arm_inst)); 4559 INC_PC(sizeof(generic_arm_inst));
4537 goto DISPATCH; 4560 goto DISPATCH;
@@ -4604,7 +4627,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
4604 if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) { 4627 if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
4605 ldst_inst* inst_cream = (ldst_inst*)inst_base->component; 4628 ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
4606 inst_cream->get_addr(cpu, inst_cream->inst, addr); 4629 inst_cream->get_addr(cpu, inst_cream->inst, addr);
4607 unsigned int value = Memory::Read8(addr); 4630 unsigned int value = cpu->ReadMemory8(addr);
4608 if (BIT(value, 7)) { 4631 if (BIT(value, 7)) {
4609 value |= 0xffffff00; 4632 value |= 0xffffff00;
4610 } 4633 }
@@ -6027,7 +6050,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
6027 ldst_inst* inst_cream = (ldst_inst*)inst_base->component; 6050 ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
6028 inst_cream->get_addr(cpu, inst_cream->inst, addr); 6051 inst_cream->get_addr(cpu, inst_cream->inst, addr);
6029 unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff; 6052 unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
6030 Memory::Write8(addr, value); 6053 cpu->WriteMemory8(addr, value);
6031 } 6054 }
6032 cpu->Reg[15] += cpu->GetInstructionSize(); 6055 cpu->Reg[15] += cpu->GetInstructionSize();
6033 INC_PC(sizeof(ldst_inst)); 6056 INC_PC(sizeof(ldst_inst));
@@ -6040,7 +6063,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
6040 ldst_inst* inst_cream = (ldst_inst*)inst_base->component; 6063 ldst_inst* inst_cream = (ldst_inst*)inst_base->component;
6041 inst_cream->get_addr(cpu, inst_cream->inst, addr); 6064 inst_cream->get_addr(cpu, inst_cream->inst, addr);
6042 unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff; 6065 unsigned int value = cpu->Reg[BITS(inst_cream->inst, 12, 15)] & 0xff;
6043 Memory::Write8(addr, value); 6066 cpu->WriteMemory8(addr, value);
6044 } 6067 }
6045 cpu->Reg[15] += cpu->GetInstructionSize(); 6068 cpu->Reg[15] += cpu->GetInstructionSize();
6046 INC_PC(sizeof(ldst_inst)); 6069 INC_PC(sizeof(ldst_inst));
@@ -6091,7 +6114,7 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
6091 6114
6092 if (cpu->IsExclusiveMemoryAccess(write_addr)) { 6115 if (cpu->IsExclusiveMemoryAccess(write_addr)) {
6093 cpu->UnsetExclusiveMemoryAddress(); 6116 cpu->UnsetExclusiveMemoryAddress();
6094 Memory::Write8(write_addr, cpu->Reg[inst_cream->Rm]); 6117 cpu->WriteMemory8(write_addr, cpu->Reg[inst_cream->Rm]);
6095 RD = 0; 6118 RD = 0;
6096 } else { 6119 } else {
6097 // Failed to write due to mutex access 6120 // Failed to write due to mutex access
@@ -6250,8 +6273,8 @@ unsigned InterpreterMainLoop(ARMul_State* cpu) {
6250 if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) { 6273 if (inst_base->cond == ConditionCode::AL || CondPassed(cpu, inst_base->cond)) {
6251 swp_inst* inst_cream = (swp_inst*)inst_base->component; 6274 swp_inst* inst_cream = (swp_inst*)inst_base->component;
6252 addr = RN; 6275 addr = RN;
6253 unsigned int value = Memory::Read8(addr); 6276 unsigned int value = cpu->ReadMemory8(addr);
6254 Memory::Write8(addr, (RM & 0xFF)); 6277 cpu->WriteMemory8(addr, (RM & 0xFF));
6255 RD = value; 6278 RD = value;
6256 } 6279 }
6257 cpu->Reg[15] += cpu->GetInstructionSize(); 6280 cpu->Reg[15] += cpu->GetInstructionSize();
diff --git a/src/core/arm/skyeye_common/armstate.cpp b/src/core/arm/skyeye_common/armstate.cpp
index 0491717dc..2d814345a 100644
--- a/src/core/arm/skyeye_common/armstate.cpp
+++ b/src/core/arm/skyeye_common/armstate.cpp
@@ -7,6 +7,7 @@
7#include "core/memory.h" 7#include "core/memory.h"
8#include "core/arm/skyeye_common/armstate.h" 8#include "core/arm/skyeye_common/armstate.h"
9#include "core/arm/skyeye_common/vfp/vfp.h" 9#include "core/arm/skyeye_common/vfp/vfp.h"
10#include "core/gdbstub/gdbstub.h"
10 11
11ARMul_State::ARMul_State(PrivilegeMode initial_mode) 12ARMul_State::ARMul_State(PrivilegeMode initial_mode)
12{ 13{
@@ -185,8 +186,25 @@ void ARMul_State::ResetMPCoreCP15Registers()
185 CP15[CP15_TLB_DEBUG_CONTROL] = 0x00000000; 186 CP15[CP15_TLB_DEBUG_CONTROL] = 0x00000000;
186} 187}
187 188
189static void CheckMemoryBreakpoint(u32 address, GDBStub::BreakpointType type)
190{
191 if (GDBStub::g_server_enabled && GDBStub::CheckBreakpoint(address, type)) {
192 LOG_DEBUG(Debug, "Found memory breakpoint @ %08x", address);
193 GDBStub::Break(true);
194 }
195}
196
197u8 ARMul_State::ReadMemory8(u32 address) const
198{
199 CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
200
201 return Memory::Read8(address);
202}
203
188u16 ARMul_State::ReadMemory16(u32 address) const 204u16 ARMul_State::ReadMemory16(u32 address) const
189{ 205{
206 CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
207
190 u16 data = Memory::Read16(address); 208 u16 data = Memory::Read16(address);
191 209
192 if (InBigEndianMode()) 210 if (InBigEndianMode())
@@ -197,6 +215,8 @@ u16 ARMul_State::ReadMemory16(u32 address) const
197 215
198u32 ARMul_State::ReadMemory32(u32 address) const 216u32 ARMul_State::ReadMemory32(u32 address) const
199{ 217{
218 CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
219
200 u32 data = Memory::Read32(address); 220 u32 data = Memory::Read32(address);
201 221
202 if (InBigEndianMode()) 222 if (InBigEndianMode())
@@ -207,6 +227,8 @@ u32 ARMul_State::ReadMemory32(u32 address) const
207 227
208u64 ARMul_State::ReadMemory64(u32 address) const 228u64 ARMul_State::ReadMemory64(u32 address) const
209{ 229{
230 CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Read);
231
210 u64 data = Memory::Read64(address); 232 u64 data = Memory::Read64(address);
211 233
212 if (InBigEndianMode()) 234 if (InBigEndianMode())
@@ -215,8 +237,17 @@ u64 ARMul_State::ReadMemory64(u32 address) const
215 return data; 237 return data;
216} 238}
217 239
240void ARMul_State::WriteMemory8(u32 address, u8 data)
241{
242 CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
243
244 Memory::Write8(address, data);
245}
246
218void ARMul_State::WriteMemory16(u32 address, u16 data) 247void ARMul_State::WriteMemory16(u32 address, u16 data)
219{ 248{
249 CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
250
220 if (InBigEndianMode()) 251 if (InBigEndianMode())
221 data = Common::swap16(data); 252 data = Common::swap16(data);
222 253
@@ -225,6 +256,8 @@ void ARMul_State::WriteMemory16(u32 address, u16 data)
225 256
226void ARMul_State::WriteMemory32(u32 address, u32 data) 257void ARMul_State::WriteMemory32(u32 address, u32 data)
227{ 258{
259 CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
260
228 if (InBigEndianMode()) 261 if (InBigEndianMode())
229 data = Common::swap32(data); 262 data = Common::swap32(data);
230 263
@@ -233,6 +266,8 @@ void ARMul_State::WriteMemory32(u32 address, u32 data)
233 266
234void ARMul_State::WriteMemory64(u32 address, u64 data) 267void ARMul_State::WriteMemory64(u32 address, u64 data)
235{ 268{
269 CheckMemoryBreakpoint(address, GDBStub::BreakpointType::Write);
270
236 if (InBigEndianMode()) 271 if (InBigEndianMode())
237 data = Common::swap64(data); 272 data = Common::swap64(data);
238 273
diff --git a/src/core/arm/skyeye_common/armstate.h b/src/core/arm/skyeye_common/armstate.h
index ceb159d14..98dad9b1f 100644
--- a/src/core/arm/skyeye_common/armstate.h
+++ b/src/core/arm/skyeye_common/armstate.h
@@ -153,9 +153,11 @@ public:
153 153
154 // Reads/writes data in big/little endian format based on the 154 // Reads/writes data in big/little endian format based on the
155 // state of the E (endian) bit in the APSR. 155 // state of the E (endian) bit in the APSR.
156 u8 ReadMemory8(u32 address) const;
156 u16 ReadMemory16(u32 address) const; 157 u16 ReadMemory16(u32 address) const;
157 u32 ReadMemory32(u32 address) const; 158 u32 ReadMemory32(u32 address) const;
158 u64 ReadMemory64(u32 address) const; 159 u64 ReadMemory64(u32 address) const;
160 void WriteMemory8(u32 address, u8 data);
159 void WriteMemory16(u32 address, u16 data); 161 void WriteMemory16(u32 address, u16 data);
160 void WriteMemory32(u32 address, u32 data); 162 void WriteMemory32(u32 address, u32 data);
161 void WriteMemory64(u32 address, u64 data); 163 void WriteMemory64(u32 address, u64 data);
diff --git a/src/core/core.cpp b/src/core/core.cpp
index dddc16708..219b03af4 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -13,6 +13,8 @@
13#include "core/hle/kernel/thread.h" 13#include "core/hle/kernel/thread.h"
14#include "core/hw/hw.h" 14#include "core/hw/hw.h"
15 15
16#include "core/gdbstub/gdbstub.h"
17
16namespace Core { 18namespace Core {
17 19
18ARM_Interface* g_app_core = nullptr; ///< ARM11 application core 20ARM_Interface* g_app_core = nullptr; ///< ARM11 application core
@@ -20,6 +22,21 @@ ARM_Interface* g_sys_core = nullptr; ///< ARM11 system (OS) core
20 22
21/// Run the core CPU loop 23/// Run the core CPU loop
22void RunLoop(int tight_loop) { 24void RunLoop(int tight_loop) {
25 if (GDBStub::g_server_enabled) {
26 GDBStub::HandlePacket();
27
28 // If the loop is halted and we want to step, use a tiny (1) number of instructions to execute.
29 // Otherwise get out of the loop function.
30 if (GDBStub::GetCpuHaltFlag()) {
31 if (GDBStub::GetCpuStepFlag()) {
32 GDBStub::SetCpuStepFlag(false);
33 tight_loop = 1;
34 } else {
35 return;
36 }
37 }
38 }
39
23 // If we don't have a currently active thread then don't execute instructions, 40 // If we don't have a currently active thread then don't execute instructions,
24 // instead advance to the next event and try to yield to the next thread 41 // instead advance to the next event and try to yield to the next thread
25 if (Kernel::GetCurrentThread() == nullptr) { 42 if (Kernel::GetCurrentThread() == nullptr) {
diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp
new file mode 100644
index 000000000..ced1c54f5
--- /dev/null
+++ b/src/core/gdbstub/gdbstub.cpp
@@ -0,0 +1,940 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
6
7#include <csignal>
8#include <cstdarg>
9#include <cstdio>
10#include <cstring>
11#include <fcntl.h>
12#include <map>
13#include <numeric>
14
15#ifdef _MSC_VER
16#include <WinSock2.h>
17#include <ws2tcpip.h>
18#include <common/x64/abi.h>
19#include <io.h>
20#include <iphlpapi.h>
21#define SHUT_RDWR 2
22#else
23#include <sys/select.h>
24#include <sys/socket.h>
25#include <sys/un.h>
26#include <netinet/in.h>
27#include <unistd.h>
28#endif
29
30#include "common/logging/log.h"
31#include "common/string_util.h"
32#include <core/arm/arm_interface.h>
33#include "core/core.h"
34#include "core/memory.h"
35#include "gdbstub.h"
36
37const int GDB_BUFFER_SIZE = 10000;
38
39const char GDB_STUB_START = '$';
40const char GDB_STUB_END = '#';
41const char GDB_STUB_ACK = '+';
42const char GDB_STUB_NACK = '-';
43
44#ifndef SIGTRAP
45const u32 SIGTRAP = 5;
46#endif
47
48#ifndef SIGTERM
49const u32 SIGTERM = 15;
50#endif
51
52#ifndef MSG_WAITALL
53const u32 MSG_WAITALL = 8;
54#endif
55
56const u32 R0_REGISTER = 0;
57const u32 R15_REGISTER = 15;
58const u32 CSPR_REGISTER = 25;
59
60namespace GDBStub {
61
62static int gdbserver_socket = -1;
63
64static u8 command_buffer[GDB_BUFFER_SIZE];
65static u32 command_length;
66
67static u32 latest_signal = 0;
68static u32 send_signal = 0;
69static u32 step_break = 0;
70static bool memory_break = false;
71
72// Binding to a port within the reserved ports range (0-1023) requires root permissions,
73// so default to a port outside of that range.
74static u16 gdbstub_port = 24689;
75
76static bool halt_loop = true;
77static bool step_loop = false;
78std::atomic<bool> g_server_enabled(false);
79
80#ifdef _WIN32
81WSADATA InitData;
82#endif
83
84struct Breakpoint {
85 bool active;
86 PAddr addr;
87 u32 len;
88};
89
90static std::map<u32, Breakpoint> breakpoints_execute;
91static std::map<u32, Breakpoint> breakpoints_read;
92static std::map<u32, Breakpoint> breakpoints_write;
93
94/**
95 * Turns hex string character into the equivalent byte.
96 *
97 * @param hex Input hex character to be turned into byte.
98 */
99static u8 HexCharToValue(u8 hex) {
100 if (hex >= '0' && hex <= '9') {
101 return hex - '0';
102 } else if (hex >= 'a' && hex <= 'f') {
103 return hex - 'a' + 0xA;
104 } else if (hex >= 'A' && hex <= 'F') {
105 return hex - 'A' + 0xA;
106 }
107
108 LOG_ERROR(Debug_GDBStub, "Invalid nibble: %c (%02x)\n", hex, hex);
109 return 0;
110}
111
112/**
113 * Turn nibble of byte into hex string character.
114 *
115 * @param n Nibble to be turned into hex character.
116 */
117static u8 NibbleToHex(u8 n) {
118 n &= 0xF;
119 if (n < 0xA) {
120 return '0' + n;
121 } else {
122 return 'A' + n - 0xA;
123 }
124}
125
126/**
127 * Converts input array of u8 bytes into their equivalent hex string characters.
128 *
129 * @param dest Pointer to buffer to store output hex string characters.
130 * @param src Pointer to array of u8 bytes.
131 * @param len Length of src array.
132 */
133static void MemToHex(u8* dest, u8* src, u32 len) {
134 while (len-- > 0) {
135 u8 tmp = *src++;
136 *dest++ = NibbleToHex(tmp >> 4);
137 *dest++ = NibbleToHex(tmp);
138 }
139}
140
141/**
142 * Converts input hex string characters into an array of equivalent of u8 bytes.
143 *
144 * @param dest Pointer to buffer to store u8 bytes.
145 * @param src Pointer to array of output hex string characters.
146 * @param len Length of src array.
147 */
148static void HexToMem(u8* dest, u8* src, u32 len) {
149 while (len-- > 0) {
150 *dest++ = (HexCharToValue(src[0]) << 4) | HexCharToValue(src[1]);
151 src += 2;
152 }
153}
154
155/**
156 * Convert a u32 into a hex string.
157 *
158 * @param dest Pointer to buffer to store output hex string characters.
159 */
160static void IntToHex(u8* dest, u32 v) {
161 for (int i = 0; i < 8; i += 2) {
162 dest[i + 1] = NibbleToHex(v >> (4 * i));
163 dest[i] = NibbleToHex(v >> (4 * (i + 1)));
164 }
165}
166
167/**
168 * Convert a hex string into a u32.
169 *
170 * @param src Pointer to hex string.
171 */
172static u32 HexToInt(u8* src) {
173 u32 output = 0;
174
175 for (int i = 0; i < 8; i += 2) {
176 output = (output << 4) | HexCharToValue(src[7 - i - 1]);
177 output = (output << 4) | HexCharToValue(src[7 - i]);
178 }
179
180 return output;
181}
182
183/// Read a byte from the gdb client.
184static u8 ReadByte() {
185 u8 c;
186 size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL);
187 if (received_size != 1) {
188 LOG_ERROR(Debug_GDBStub, "recv failed : %ld", received_size);
189 Deinit();
190 }
191
192 return c;
193}
194
195/// Calculate the checksum of the current command buffer.
196static u8 CalculateChecksum(u8 *buffer, u32 length) {
197 return static_cast<u8>(std::accumulate(buffer, buffer + length, 0, std::plus<u8>()));
198}
199
200/**
201 * Get the list of breakpoints for a given breakpoint type.
202 *
203 * @param type Type of breakpoint list.
204 */
205static std::map<u32, Breakpoint>& GetBreakpointList(BreakpointType type) {
206 switch (type) {
207 case BreakpointType::Execute:
208 return breakpoints_execute;
209 case BreakpointType::Read:
210 return breakpoints_read;
211 case BreakpointType::Write:
212 return breakpoints_write;
213 default:
214 return breakpoints_read;
215 }
216}
217
218/**
219 * Remove the breakpoint from the given address of the specified type.
220 *
221 * @param type Type of breakpoint.
222 * @param addr Address of breakpoint.
223 */
224static void RemoveBreakpoint(BreakpointType type, PAddr addr) {
225 std::map<u32, Breakpoint>& p = GetBreakpointList(type);
226
227 auto bp = p.find(addr);
228 if (bp != p.end()) {
229 LOG_DEBUG(Debug_GDBStub, "gdb: removed a breakpoint: %08x bytes at %08x of type %d\n", bp->second.len, bp->second.addr, type);
230 p.erase(addr);
231 }
232}
233
234PAddr GetNextBreakpointFromAddress(PAddr addr, BreakpointType type) {
235 std::map<u32, Breakpoint>& p = GetBreakpointList(type);
236 auto next_breakpoint = p.lower_bound(addr);
237 u32 breakpoint = -1;
238
239 if (next_breakpoint != p.end())
240 breakpoint = next_breakpoint->first;
241
242 return breakpoint;
243}
244
245bool CheckBreakpoint(PAddr addr, BreakpointType type) {
246 if (!IsConnected()) {
247 return false;
248 }
249
250 std::map<u32, Breakpoint>& p = GetBreakpointList(type);
251
252 auto bp = p.find(addr);
253 if (bp != p.end()) {
254 u32 len = bp->second.len;
255
256 // IDA Pro defaults to 4-byte breakpoints for all non-hardware breakpoints
257 // no matter if it's a 4-byte or 2-byte instruction. When you execute a
258 // Thumb instruction with a 4-byte breakpoint set, it will set a breakpoint on
259 // two instructions instead of the single instruction you placed the breakpoint
260 // on. So, as a way to make sure that execution breakpoints are only breaking
261 // on the instruction that was specified, set the length of an execution
262 // breakpoint to 1. This should be fine since the CPU should never begin executing
263 // an instruction anywhere except the beginning of the instruction.
264 if (type == BreakpointType::Execute) {
265 len = 1;
266 }
267
268 if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) {
269 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);
270 return true;
271 }
272 }
273
274 return false;
275}
276
277/**
278 * Send packet to gdb client.
279 *
280 * @param packet Packet to be sent to client.
281 */
282static void SendPacket(const char packet) {
283 size_t sent_size = send(gdbserver_socket, &packet, 1, 0);
284 if (sent_size != 1) {
285 LOG_ERROR(Debug_GDBStub, "send failed");
286 }
287}
288
289/**
290 * Send reply to gdb client.
291 *
292 * @param reply Reply to be sent to client.
293 */
294static void SendReply(const char* reply) {
295 if (!IsConnected()) {
296 return;
297 }
298
299 memset(command_buffer, 0, sizeof(command_buffer));
300
301 command_length = strlen(reply);
302 if (command_length + 4 > sizeof(command_buffer)) {
303 LOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply");
304 }
305
306 memcpy(command_buffer + 1, reply, command_length);
307
308 u8 checksum = CalculateChecksum(command_buffer, command_length + 1);
309 command_buffer[0] = GDB_STUB_START;
310 command_buffer[command_length + 1] = GDB_STUB_END;
311 command_buffer[command_length + 2] = NibbleToHex(checksum >> 4);
312 command_buffer[command_length + 3] = NibbleToHex(checksum);
313
314 u8* ptr = command_buffer;
315 u32 left = command_length + 4;
316 while (left > 0) {
317 int sent_size = send(gdbserver_socket, reinterpret_cast<char*>(ptr), left, 0);
318 if (sent_size < 0) {
319 LOG_ERROR(Debug_GDBStub, "gdb: send failed");
320 return Deinit();
321 }
322
323 left -= sent_size;
324 ptr += sent_size;
325 }
326}
327
328/// Handle query command from gdb client.
329static void HandleQuery() {
330 LOG_DEBUG(Debug_GDBStub, "gdb: query '%s'\n", command_buffer + 1);
331
332 if (!strcmp(reinterpret_cast<const char*>(command_buffer + 1), "TStatus")) {
333 SendReply("T0");
334 } else {
335 SendReply("");
336 }
337}
338
339/// Handle set thread command from gdb client.
340static void HandleSetThread() {
341 if (memcmp(command_buffer, "Hg0", 3) == 0 ||
342 memcmp(command_buffer, "Hc-1", 4) == 0 ||
343 memcmp(command_buffer, "Hc0", 4) == 0 ||
344 memcmp(command_buffer, "Hc1", 4) == 0) {
345 return SendReply("OK");
346 }
347
348 SendReply("E01");
349}
350
351/// Create and send signal packet.
352static void HandleSignal() {
353 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)));
354
355 LOG_DEBUG(Debug_GDBStub, "Response: %s", buffer.c_str());
356
357 SendReply(buffer.c_str());
358}
359
360/**
361 * Set signal and send packet to client through HandleSignal if signal flag is set using SendSignal.
362 *
363 * @param signal Signal to be sent to client.
364 */
365int SendSignal(u32 signal) {
366 if (gdbserver_socket == -1) {
367 return 1;
368 }
369
370 latest_signal = signal;
371
372 if (send_signal) {
373 HandleSignal();
374 send_signal = 0;
375 }
376
377 return 0;
378}
379
380/// Read command from gdb client.
381static void ReadCommand() {
382 command_length = 0;
383 memset(command_buffer, 0, sizeof(command_buffer));
384
385 u8 c = ReadByte();
386 if (c == '+') {
387 //ignore ack
388 return;
389 } else if (c == 0x03) {
390 LOG_INFO(Debug_GDBStub, "gdb: found break command\n");
391 halt_loop = true;
392 send_signal = 1;
393 SendSignal(SIGTRAP);
394 return;
395 } else if (c != GDB_STUB_START) {
396 LOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte %02x\n", c);
397 return;
398 }
399
400 while ((c = ReadByte()) != GDB_STUB_END) {
401 command_buffer[command_length++] = c;
402 if (command_length == sizeof(command_buffer)) {
403 LOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow\n");
404 SendPacket(GDB_STUB_NACK);
405 return;
406 }
407 }
408
409 u8 checksum_received = HexCharToValue(ReadByte()) << 4;
410 checksum_received |= HexCharToValue(ReadByte());
411
412 u8 checksum_calculated = CalculateChecksum(command_buffer, command_length);
413
414 if (checksum_received != checksum_calculated) {
415 LOG_ERROR(Debug_GDBStub, "gdb: invalid checksum: calculated %02x and read %02x for $%s# (length: %d)\n",
416 checksum_calculated, checksum_received, command_buffer, command_length);
417
418 command_length = 0;
419
420 SendPacket(GDB_STUB_NACK);
421 return;
422 }
423
424 SendPacket(GDB_STUB_ACK);
425}
426
427/// Check if there is data to be read from the gdb client.
428static bool IsDataAvailable() {
429 if (!IsConnected()) {
430 return false;
431 }
432
433 fd_set fd_socket;
434
435 FD_ZERO(&fd_socket);
436 FD_SET(gdbserver_socket, &fd_socket);
437
438 struct timeval t;
439 t.tv_sec = 0;
440 t.tv_usec = 0;
441
442 if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) {
443 LOG_ERROR(Debug_GDBStub, "select failed");
444 return false;
445 }
446
447 return FD_ISSET(gdbserver_socket, &fd_socket);
448}
449
450/// Send requested register to gdb client.
451static void ReadRegister() {
452 static u8 reply[64];
453 memset(reply, 0, sizeof(reply));
454
455 u32 id = HexCharToValue(command_buffer[1]);
456 if (command_buffer[2] != '\0') {
457 id <<= 4;
458 id |= HexCharToValue(command_buffer[2]);
459 }
460
461 if (id >= R0_REGISTER && id <= R15_REGISTER) {
462 IntToHex(reply, Core::g_app_core->GetReg(id));
463 } else if (id == CSPR_REGISTER) {
464 IntToHex(reply, Core::g_app_core->GetCPSR());
465 } else {
466 return SendReply("E01");
467 }
468
469 SendReply(reinterpret_cast<char*>(reply));
470}
471
472/// Send all registers to the gdb client.
473static void ReadRegisters() {
474 static u8 buffer[GDB_BUFFER_SIZE - 4];
475 memset(buffer, 0, sizeof(buffer));
476
477 u8* bufptr = buffer;
478 for (int i = 0; i <= CSPR_REGISTER; i++) {
479 if (i <= R15_REGISTER) {
480 IntToHex(bufptr + i * 8, Core::g_app_core->GetReg(i));
481 } else if (i == CSPR_REGISTER) {
482 IntToHex(bufptr + i * 8, Core::g_app_core->GetCPSR());
483 } else {
484 IntToHex(bufptr + i * 8, 0);
485 IntToHex(bufptr + (i + 1) * 8, 0);
486 i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
487 }
488 }
489
490 SendReply(reinterpret_cast<char*>(buffer));
491}
492
493/// Modify data of register specified by gdb client.
494static void WriteRegister() {
495 u8* buffer_ptr = command_buffer + 3;
496
497 u32 id = HexCharToValue(command_buffer[1]);
498 if (command_buffer[2] != '=') {
499 ++buffer_ptr;
500 id <<= 4;
501 id |= HexCharToValue(command_buffer[2]);
502 }
503
504 if (id >= R0_REGISTER && id <= R15_REGISTER) {
505 Core::g_app_core->SetReg(id, HexToInt(buffer_ptr));
506 } else if (id == CSPR_REGISTER) {
507 Core::g_app_core->SetCPSR(HexToInt(buffer_ptr));
508 } else {
509 return SendReply("E01");
510 }
511
512 SendReply("OK");
513}
514
515/// Modify all registers with data received from the client.
516static void WriteRegisters() {
517 u8* buffer_ptr = command_buffer + 1;
518
519 if (command_buffer[0] != 'G')
520 return SendReply("E01");
521
522 for (int i = 0; i <= CSPR_REGISTER; i++) {
523 if (i <= R15_REGISTER) {
524 Core::g_app_core->SetReg(i, HexToInt(buffer_ptr + i * 8));
525 } else if (i == CSPR_REGISTER) {
526 Core::g_app_core->SetCPSR(HexToInt(buffer_ptr + i * 8));
527 } else {
528 i++; // These registers seem to be all 64bit instead of 32bit, so skip two instead of one
529 }
530 }
531
532 SendReply("OK");
533}
534
535/// Read location in memory specified by gdb client.
536static void ReadMemory() {
537 static u8 reply[GDB_BUFFER_SIZE - 4];
538
539 int i = 1;
540 PAddr addr = 0;
541 while (command_buffer[i] != ',') {
542 addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
543 }
544 i++;
545
546 u32 len = 0;
547 while (i < command_length) {
548 len = (len << 4) | HexCharToValue(command_buffer[i++]);
549 }
550
551 if (len * 2 > sizeof(reply)) {
552 SendReply("E01");
553 }
554
555 u8* data = Memory::GetPointer(addr);
556 if (!data) {
557 return SendReply("E0");
558 }
559
560 MemToHex(reply, data, len);
561 reply[len * 2] = '\0';
562 SendReply(reinterpret_cast<char*>(reply));
563}
564
565/// Modify location in memory with data received from the gdb client.
566static void WriteMemory() {
567 int i = 1;
568 PAddr addr = 0;
569 while (command_buffer[i] != ',') {
570 addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
571 }
572 i++;
573
574 u32 len = 0;
575 while (command_buffer[i] != ':') {
576 len = (len << 4) | HexCharToValue(command_buffer[i++]);
577 }
578
579 u8* dst = Memory::GetPointer(addr);
580 if (!dst) {
581 return SendReply("E00");
582 }
583
584 HexToMem(dst, command_buffer + i + 1, len);
585 SendReply("OK");
586}
587
588void Break(bool is_memory_break) {
589 if (!halt_loop) {
590 halt_loop = true;
591 send_signal = 1;
592 SendSignal(SIGTRAP);
593 }
594
595 memory_break = is_memory_break;
596}
597
598/// Tell the CPU that it should perform a single step.
599static void Step() {
600 step_loop = true;
601 halt_loop = true;
602 send_signal = 1;
603 step_break = 1;
604 SendSignal(SIGTRAP);
605}
606
607bool IsMemoryBreak() {
608 if (IsConnected()) {
609 return false;
610 }
611
612 return memory_break;
613}
614
615/// Tell the CPU to continue executing.
616static void Continue() {
617 memory_break = false;
618 step_break = 0;
619 step_loop = false;
620 halt_loop = false;
621}
622
623/**
624 * Commit breakpoint to list of breakpoints.
625 *
626 * @param type Type of breakpoint.
627 * @param addr Address of breakpoint.
628 * @param len Length of breakpoint.
629 */
630bool CommitBreakpoint(BreakpointType type, PAddr addr, u32 len) {
631 std::map<u32, Breakpoint>& p = GetBreakpointList(type);
632
633 Breakpoint breakpoint;
634 breakpoint.active = true;
635 breakpoint.addr = addr;
636 breakpoint.len = len;
637 p.insert({ addr, breakpoint });
638
639 LOG_DEBUG(Debug_GDBStub, "gdb: added %d breakpoint: %08x bytes at %08x\n", type, breakpoint.len, breakpoint.addr);
640
641 return true;
642}
643
644/// Handle add breakpoint command from gdb client.
645static void AddBreakpoint() {
646 BreakpointType type;
647
648 u8 type_id = HexCharToValue(command_buffer[1]);
649 switch (type_id) {
650 case 0:
651 case 1:
652 type = BreakpointType::Execute;
653 break;
654 case 2:
655 type = BreakpointType::Write;
656 break;
657 case 3:
658 type = BreakpointType::Read;
659 break;
660 case 4:
661 type = BreakpointType::Access;
662 break;
663 default:
664 return SendReply("E01");
665 }
666
667 int i = 3;
668 PAddr addr = 0;
669 while (command_buffer[i] != ',') {
670 addr = addr << 4 | HexCharToValue(command_buffer[i++]);
671 }
672 i++;
673
674 u32 len = 0;
675 while (i < command_length) {
676 len = len << 4 | HexCharToValue(command_buffer[i++]);
677 }
678
679 if (type == BreakpointType::Access) {
680 // Access is made up of Read and Write types, so add both breakpoints
681 type = BreakpointType::Read;
682
683 if (!CommitBreakpoint(type, addr, len)) {
684 return SendReply("E02");
685 }
686
687 type = BreakpointType::Write;
688 }
689
690 if (!CommitBreakpoint(type, addr, len)) {
691 return SendReply("E02");
692 }
693
694 SendReply("OK");
695}
696
697/// Handle remove breakpoint command from gdb client.
698static void RemoveBreakpoint() {
699 BreakpointType type;
700
701 u8 type_id = HexCharToValue(command_buffer[1]);
702 switch (type_id) {
703 case 0:
704 case 1:
705 type = BreakpointType::Execute;
706 break;
707 case 2:
708 type = BreakpointType::Write;
709 break;
710 case 3:
711 type = BreakpointType::Read;
712 break;
713 case 4:
714 type = BreakpointType::Access;
715 break;
716 default:
717 return SendReply("E01");
718 }
719
720 int i = 3;
721 PAddr addr = 0;
722 while (command_buffer[i] != ',') {
723 addr = (addr << 4) | HexCharToValue(command_buffer[i++]);
724 }
725 i++;
726
727 u32 len = 0;
728 while (i < command_length) {
729 len = (len << 4) | HexCharToValue(command_buffer[i++]);
730 }
731
732 if (type == BreakpointType::Access) {
733 // Access is made up of Read and Write types, so add both breakpoints
734 type = BreakpointType::Read;
735 RemoveBreakpoint(type, addr);
736
737 type = BreakpointType::Write;
738 }
739
740 RemoveBreakpoint(type, addr);
741 SendReply("OK");
742}
743
744void HandlePacket() {
745 if (!IsConnected()) {
746 return;
747 }
748
749 if (!IsDataAvailable()) {
750 return;
751 }
752
753 ReadCommand();
754 if (command_length == 0) {
755 return;
756 }
757
758 LOG_DEBUG(Debug_GDBStub, "Packet: %s", command_buffer);
759
760 switch (command_buffer[0]) {
761 case 'q':
762 HandleQuery();
763 break;
764 case 'H':
765 HandleSetThread();
766 break;
767 case '?':
768 HandleSignal();
769 break;
770 case 'k':
771 Deinit();
772 LOG_INFO(Debug_GDBStub, "killed by gdb");
773 return;
774 case 'g':
775 ReadRegisters();
776 break;
777 case 'G':
778 WriteRegisters();
779 break;
780 case 'p':
781 ReadRegister();
782 break;
783 case 'P':
784 WriteRegister();
785 break;
786 case 'm':
787 ReadMemory();
788 break;
789 case 'M':
790 WriteMemory();
791 break;
792 case 's':
793 Step();
794 return;
795 case 'C':
796 case 'c':
797 Continue();
798 return;
799 case 'z':
800 RemoveBreakpoint();
801 break;
802 case 'Z':
803 AddBreakpoint();
804 break;
805 default:
806 SendReply("");
807 break;
808 }
809}
810
811void SetServerPort(u16 port) {
812 gdbstub_port = port;
813}
814
815void ToggleServer(bool status) {
816 if (status) {
817 g_server_enabled = status;
818
819 // Start server
820 if (!IsConnected() && Core::g_sys_core != nullptr) {
821 Init();
822 }
823 }
824 else {
825 // Stop server
826 if (IsConnected()) {
827 Deinit();
828 }
829
830 g_server_enabled = status;
831 }
832}
833
834void Init(u16 port) {
835 if (!g_server_enabled) {
836 // Set the halt loop to false in case the user enabled the gdbstub mid-execution.
837 // This way the CPU can still execute normally.
838 halt_loop = false;
839 step_loop = false;
840 return;
841 }
842
843 // Setup initial gdbstub status
844 halt_loop = true;
845 step_loop = false;
846
847 breakpoints_execute.clear();
848 breakpoints_read.clear();
849 breakpoints_write.clear();
850
851 // Start gdb server
852 LOG_INFO(Debug_GDBStub, "Starting GDB server on port %d...", port);
853
854 sockaddr_in saddr_server = {};
855 saddr_server.sin_family = AF_INET;
856 saddr_server.sin_port = htons(port);
857 saddr_server.sin_addr.s_addr = INADDR_ANY;
858
859#ifdef _WIN32
860 WSAStartup(MAKEWORD(2, 2), &InitData);
861#endif
862
863 int tmpsock = socket(PF_INET, SOCK_STREAM, 0);
864 if (tmpsock == -1) {
865 LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket");
866 }
867
868 const sockaddr* server_addr = reinterpret_cast<const sockaddr*>(&saddr_server);
869 socklen_t server_addrlen = sizeof(saddr_server);
870 if (bind(tmpsock, server_addr, server_addrlen) < 0) {
871 LOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket");
872 }
873
874 if (listen(tmpsock, 1) < 0) {
875 LOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket");
876 }
877
878 // Wait for gdb to connect
879 LOG_INFO(Debug_GDBStub, "Waiting for gdb to connect...\n");
880 sockaddr_in saddr_client;
881 sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client);
882 socklen_t client_addrlen = sizeof(saddr_client);
883 gdbserver_socket = accept(tmpsock, client_addr, &client_addrlen);
884 if (gdbserver_socket < 0) {
885 // In the case that we couldn't start the server for whatever reason, just start CPU execution like normal.
886 halt_loop = false;
887 step_loop = false;
888
889 LOG_ERROR(Debug_GDBStub, "Failed to accept gdb client");
890 }
891 else {
892 LOG_INFO(Debug_GDBStub, "Client connected.\n");
893 saddr_client.sin_addr.s_addr = ntohl(saddr_client.sin_addr.s_addr);
894 }
895
896 // Clean up temporary socket if it's still alive at this point.
897 if (tmpsock != -1) {
898 shutdown(tmpsock, SHUT_RDWR);
899 }
900}
901
902void Init() {
903 Init(gdbstub_port);
904}
905
906void Deinit() {
907 if (!g_server_enabled) {
908 return;
909 }
910
911 LOG_INFO(Debug_GDBStub, "Stopping GDB ...");
912 if (gdbserver_socket != -1) {
913 shutdown(gdbserver_socket, SHUT_RDWR);
914 gdbserver_socket = -1;
915 }
916
917#ifdef _WIN32
918 WSACleanup();
919#endif
920
921 LOG_INFO(Debug_GDBStub, "GDB stopped.");
922}
923
924bool IsConnected() {
925 return g_server_enabled && gdbserver_socket != -1;
926}
927
928bool GetCpuHaltFlag() {
929 return halt_loop;
930}
931
932bool GetCpuStepFlag() {
933 return step_loop;
934}
935
936void SetCpuStepFlag(bool is_step) {
937 step_loop = is_step;
938}
939
940};
diff --git a/src/core/gdbstub/gdbstub.h b/src/core/gdbstub/gdbstub.h
new file mode 100644
index 000000000..11ff823c3
--- /dev/null
+++ b/src/core/gdbstub/gdbstub.h
@@ -0,0 +1,89 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5// Originally written by Sven Peter <sven@fail0verflow.com> for anergistic.
6
7#pragma once
8#include <atomic>
9
10namespace GDBStub {
11
12/// Breakpoint Method
13enum class BreakpointType {
14 None, ///< None
15 Execute, ///< Execution Breakpoint
16 Read, ///< Read Breakpoint
17 Write, ///< Write Breakpoint
18 Access ///< Access (R/W) Breakpoint
19};
20
21/// If set to false, the server will never be started and no gdbstub-related functions will be executed.
22extern std::atomic<bool> g_server_enabled;
23
24/**
25 * Set the port the gdbstub should use to listen for connections.
26 *
27 * @param port Port to listen for connection
28 */
29void SetServerPort(u16 port);
30
31/**
32 * Set the g_server_enabled flag and start or stop the server if possible.
33 *
34 * @param status Set the server to enabled or disabled.
35 */
36void ToggleServer(bool status);
37
38/// Start the gdbstub server.
39void Init();
40
41/// Stop gdbstub server.
42void Deinit();
43
44/// Returns true if there is an active socket connection.
45bool IsConnected();
46
47/**
48 * Signal to the gdbstub server that it should halt CPU execution.
49 *
50 * @param is_memory_break If true, the break resulted from a memory breakpoint.
51 */
52void Break(bool is_memory_break = false);
53
54/// Determine if there was a memory breakpoint.
55bool IsMemoryBreak();
56
57/// Read and handle packet from gdb client.
58void HandlePacket();
59
60/**
61 * Get the nearest breakpoint of the specified type at the given address.
62 *
63 * @param addr Address to search from.
64 * @param type Type of breakpoint.
65 */
66PAddr GetNextBreakpointFromAddress(u32 addr, GDBStub::BreakpointType type);
67
68/**
69 * Check if a breakpoint of the specified type exists at the given address.
70 *
71 * @param addr Address of breakpoint.
72 * @param type Type of breakpoint.
73 */
74bool CheckBreakpoint(u32 addr, GDBStub::BreakpointType type);
75
76// If set to true, the CPU will halt at the beginning of the next CPU loop.
77bool GetCpuHaltFlag();
78
79// If set to true and the CPU is halted, the CPU will step one instruction.
80bool GetCpuStepFlag();
81
82/**
83 * When set to true, the CPU will step one instruction when the CPU is halted next.
84 *
85 * @param is_step
86 */
87void SetCpuStepFlag(bool is_step);
88
89}
diff --git a/src/core/settings.h b/src/core/settings.h
index 0b05e5bee..97ddcdff9 100644
--- a/src/core/settings.h
+++ b/src/core/settings.h
@@ -6,6 +6,7 @@
6 6
7#include <string> 7#include <string>
8#include <array> 8#include <array>
9#include <common/file_util.h>
9 10
10namespace Settings { 11namespace Settings {
11 12
@@ -60,6 +61,10 @@ struct Values {
60 float bg_blue; 61 float bg_blue;
61 62
62 std::string log_filter; 63 std::string log_filter;
64
65 // Debugging
66 bool use_gdbstub;
67 u16 gdbstub_port;
63} extern values; 68} extern values;
64 69
65} 70}
diff --git a/src/core/system.cpp b/src/core/system.cpp
index 3cd84bf5e..421fc48a7 100644
--- a/src/core/system.cpp
+++ b/src/core/system.cpp
@@ -12,6 +12,8 @@
12 12
13#include "video_core/video_core.h" 13#include "video_core/video_core.h"
14 14
15#include "core/gdbstub/gdbstub.h"
16
15namespace System { 17namespace System {
16 18
17void Init(EmuWindow* emu_window) { 19void Init(EmuWindow* emu_window) {
@@ -22,9 +24,13 @@ void Init(EmuWindow* emu_window) {
22 Kernel::Init(); 24 Kernel::Init();
23 HLE::Init(); 25 HLE::Init();
24 VideoCore::Init(emu_window); 26 VideoCore::Init(emu_window);
27
28 GDBStub::Init();
25} 29}
26 30
27void Shutdown() { 31void Shutdown() {
32 GDBStub::Deinit();
33
28 VideoCore::Shutdown(); 34 VideoCore::Shutdown();
29 HLE::Shutdown(); 35 HLE::Shutdown();
30 Kernel::Shutdown(); 36 Kernel::Shutdown();