2015-09-02 14:56:38 +02:00
|
|
|
// 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.
|
|
|
|
|
2015-10-22 06:31:49 +02:00
|
|
|
#include <algorithm>
|
2016-12-15 22:19:30 +01:00
|
|
|
#include <atomic>
|
2015-10-29 11:17:29 +01:00
|
|
|
#include <climits>
|
2015-09-02 14:56:38 +02:00
|
|
|
#include <csignal>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
|
|
|
#include <numeric>
|
2016-09-21 09:13:46 +02:00
|
|
|
#include <fcntl.h>
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2016-08-01 23:13:35 +02:00
|
|
|
#ifdef _WIN32
|
2016-12-03 21:08:02 +01:00
|
|
|
#include <winsock2.h>
|
|
|
|
// winsock2.h needs to be included first to prevent winsock.h being included by other includes
|
2015-09-02 14:56:38 +02:00
|
|
|
#include <io.h>
|
|
|
|
#include <iphlpapi.h>
|
2016-09-18 02:38:01 +02:00
|
|
|
#include <ws2tcpip.h>
|
2015-09-02 14:56:38 +02:00
|
|
|
#define SHUT_RDWR 2
|
|
|
|
#else
|
2016-09-18 02:38:01 +02:00
|
|
|
#include <netinet/in.h>
|
2015-09-02 14:56:38 +02:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2016-09-18 02:38:01 +02:00
|
|
|
#include <unistd.h>
|
2015-09-02 14:56:38 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/string_util.h"
|
2018-06-06 06:20:47 +02:00
|
|
|
#include "common/swap.h"
|
2015-10-22 04:19:55 +02:00
|
|
|
#include "core/arm/arm_interface.h"
|
2016-09-18 02:38:01 +02:00
|
|
|
#include "core/core.h"
|
2018-06-06 06:20:47 +02:00
|
|
|
#include "core/core_cpu.h"
|
2015-10-29 11:17:29 +01:00
|
|
|
#include "core/gdbstub/gdbstub.h"
|
2018-09-25 02:01:45 +02:00
|
|
|
#include "core/hle/kernel/process.h"
|
2018-06-06 06:20:47 +02:00
|
|
|
#include "core/hle/kernel/scheduler.h"
|
2018-09-25 02:01:45 +02:00
|
|
|
#include "core/hle/kernel/vm_manager.h"
|
2016-12-17 07:20:47 +01:00
|
|
|
#include "core/loader/loader.h"
|
2016-09-18 02:38:01 +02:00
|
|
|
#include "core/memory.h"
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
namespace GDBStub {
|
|
|
|
namespace {
|
|
|
|
constexpr int GDB_BUFFER_SIZE = 10000;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
constexpr char GDB_STUB_START = '$';
|
|
|
|
constexpr char GDB_STUB_END = '#';
|
|
|
|
constexpr char GDB_STUB_ACK = '+';
|
|
|
|
constexpr char GDB_STUB_NACK = '-';
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
#ifndef SIGTRAP
|
2018-08-05 21:59:53 +02:00
|
|
|
constexpr u32 SIGTRAP = 5;
|
2015-09-02 14:56:38 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SIGTERM
|
2018-08-05 21:59:53 +02:00
|
|
|
constexpr u32 SIGTERM = 15;
|
2015-09-02 14:56:38 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MSG_WAITALL
|
2018-08-05 21:59:53 +02:00
|
|
|
constexpr u32 MSG_WAITALL = 8;
|
2015-09-02 14:56:38 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
constexpr u32 LR_REGISTER = 30;
|
|
|
|
constexpr u32 SP_REGISTER = 31;
|
|
|
|
constexpr u32 PC_REGISTER = 32;
|
2018-09-18 08:49:40 +02:00
|
|
|
constexpr u32 PSTATE_REGISTER = 33;
|
2018-08-05 21:59:53 +02:00
|
|
|
constexpr u32 UC_ARM64_REG_Q0 = 34;
|
2018-09-18 08:49:40 +02:00
|
|
|
constexpr u32 FPCR_REGISTER = 66;
|
2018-07-13 05:22:59 +02:00
|
|
|
|
|
|
|
// TODO/WiP - Used while working on support for FPU
|
2018-08-05 21:59:53 +02:00
|
|
|
constexpr u32 TODO_DUMMY_REG_997 = 997;
|
|
|
|
constexpr u32 TODO_DUMMY_REG_998 = 998;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2016-04-06 13:01:00 +02:00
|
|
|
// For sample XML files see the GDB source /gdb/features
|
|
|
|
// GDB also wants the l character at the start
|
|
|
|
// This XML defines what the registers are for this specific ARM device
|
2018-08-05 21:59:53 +02:00
|
|
|
constexpr char target_xml[] =
|
2016-09-18 02:38:01 +02:00
|
|
|
R"(l<?xml version="1.0"?>
|
2016-04-06 13:01:00 +02:00
|
|
|
<!DOCTYPE target SYSTEM "gdb-target.dtd">
|
|
|
|
<target version="1.0">
|
2018-01-21 17:53:09 +01:00
|
|
|
<feature name="org.gnu.gdb.aarch64.core">
|
|
|
|
<reg name="x0" bitsize="64"/>
|
|
|
|
<reg name="x1" bitsize="64"/>
|
|
|
|
<reg name="x2" bitsize="64"/>
|
|
|
|
<reg name="x3" bitsize="64"/>
|
|
|
|
<reg name="x4" bitsize="64"/>
|
|
|
|
<reg name="x5" bitsize="64"/>
|
|
|
|
<reg name="x6" bitsize="64"/>
|
|
|
|
<reg name="x7" bitsize="64"/>
|
|
|
|
<reg name="x8" bitsize="64"/>
|
|
|
|
<reg name="x9" bitsize="64"/>
|
|
|
|
<reg name="x10" bitsize="64"/>
|
|
|
|
<reg name="x11" bitsize="64"/>
|
|
|
|
<reg name="x12" bitsize="64"/>
|
|
|
|
<reg name="x13" bitsize="64"/>
|
|
|
|
<reg name="x14" bitsize="64"/>
|
|
|
|
<reg name="x15" bitsize="64"/>
|
|
|
|
<reg name="x16" bitsize="64"/>
|
|
|
|
<reg name="x17" bitsize="64"/>
|
|
|
|
<reg name="x18" bitsize="64"/>
|
|
|
|
<reg name="x19" bitsize="64"/>
|
|
|
|
<reg name="x20" bitsize="64"/>
|
|
|
|
<reg name="x21" bitsize="64"/>
|
|
|
|
<reg name="x22" bitsize="64"/>
|
|
|
|
<reg name="x23" bitsize="64"/>
|
|
|
|
<reg name="x24" bitsize="64"/>
|
|
|
|
<reg name="x25" bitsize="64"/>
|
|
|
|
<reg name="x26" bitsize="64"/>
|
|
|
|
<reg name="x27" bitsize="64"/>
|
|
|
|
<reg name="x28" bitsize="64"/>
|
|
|
|
<reg name="x29" bitsize="64"/>
|
|
|
|
<reg name="x30" bitsize="64"/>
|
|
|
|
<reg name="sp" bitsize="64" type="data_ptr"/>
|
|
|
|
|
|
|
|
<reg name="pc" bitsize="64" type="code_ptr"/>
|
|
|
|
|
2018-09-18 08:49:40 +02:00
|
|
|
<flags id="pstate_flags" size="4">
|
2018-01-21 17:53:09 +01:00
|
|
|
<field name="SP" start="0" end="0"/>
|
|
|
|
<field name="" start="1" end="1"/>
|
|
|
|
<field name="EL" start="2" end="3"/>
|
|
|
|
<field name="nRW" start="4" end="4"/>
|
|
|
|
<field name="" start="5" end="5"/>
|
|
|
|
<field name="F" start="6" end="6"/>
|
|
|
|
<field name="I" start="7" end="7"/>
|
|
|
|
<field name="A" start="8" end="8"/>
|
|
|
|
<field name="D" start="9" end="9"/>
|
|
|
|
|
|
|
|
<field name="IL" start="20" end="20"/>
|
|
|
|
<field name="SS" start="21" end="21"/>
|
|
|
|
|
|
|
|
<field name="V" start="28" end="28"/>
|
|
|
|
<field name="C" start="29" end="29"/>
|
|
|
|
<field name="Z" start="30" end="30"/>
|
|
|
|
<field name="N" start="31" end="31"/>
|
|
|
|
</flags>
|
2018-09-18 08:49:40 +02:00
|
|
|
<reg name="pstate" bitsize="32" type="pstate_flags"/>
|
2016-04-06 13:01:00 +02:00
|
|
|
</feature>
|
2018-07-13 05:22:59 +02:00
|
|
|
<feature name="org.gnu.gdb.aarch64.fpu">
|
|
|
|
</feature>
|
2016-04-06 13:01:00 +02:00
|
|
|
</target>
|
|
|
|
)";
|
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
int gdbserver_socket = -1;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
u8 command_buffer[GDB_BUFFER_SIZE];
|
|
|
|
u32 command_length;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
u32 latest_signal = 0;
|
|
|
|
bool memory_break = false;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
Kernel::Thread* current_thread = nullptr;
|
|
|
|
u32 current_core = 0;
|
2018-06-06 06:20:47 +02:00
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
// Binding to a port within the reserved ports range (0-1023) requires root permissions,
|
|
|
|
// so default to a port outside of that range.
|
2018-08-05 21:59:53 +02:00
|
|
|
u16 gdbstub_port = 24689;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
bool halt_loop = true;
|
|
|
|
bool step_loop = false;
|
|
|
|
bool send_trap = false;
|
2016-12-15 22:19:30 +01:00
|
|
|
|
|
|
|
// If set to false, the server will never be started and no
|
|
|
|
// gdbstub-related functions will be executed.
|
2018-08-05 21:59:53 +02:00
|
|
|
std::atomic<bool> server_enabled(false);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA InitData;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct Breakpoint {
|
|
|
|
bool active;
|
2018-08-05 21:55:57 +02:00
|
|
|
VAddr addr;
|
2018-01-21 17:53:09 +01:00
|
|
|
u64 len;
|
2018-08-07 04:01:24 +02:00
|
|
|
std::array<u8, 4> inst;
|
2015-09-02 14:56:38 +02:00
|
|
|
};
|
|
|
|
|
2018-08-05 22:11:29 +02:00
|
|
|
using BreakpointMap = std::map<VAddr, Breakpoint>;
|
|
|
|
BreakpointMap breakpoints_execute;
|
|
|
|
BreakpointMap breakpoints_read;
|
|
|
|
BreakpointMap breakpoints_write;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-07-13 05:22:59 +02:00
|
|
|
struct Module {
|
|
|
|
std::string name;
|
2018-08-05 21:55:57 +02:00
|
|
|
VAddr beg;
|
|
|
|
VAddr end;
|
2018-07-13 05:22:59 +02:00
|
|
|
};
|
|
|
|
|
2018-08-05 21:59:53 +02:00
|
|
|
std::vector<Module> modules;
|
|
|
|
} // Anonymous namespace
|
2018-07-13 05:22:59 +02:00
|
|
|
|
2018-08-05 21:55:57 +02:00
|
|
|
void RegisterModule(std::string name, VAddr beg, VAddr end, bool add_elf_ext) {
|
2018-07-13 05:22:59 +02:00
|
|
|
Module module;
|
|
|
|
if (add_elf_ext) {
|
|
|
|
Common::SplitPath(name, nullptr, &module.name, nullptr);
|
|
|
|
module.name += ".elf";
|
|
|
|
} else {
|
|
|
|
module.name = std::move(name);
|
|
|
|
}
|
|
|
|
module.beg = beg;
|
|
|
|
module.end = end;
|
|
|
|
modules.push_back(std::move(module));
|
|
|
|
}
|
|
|
|
|
2018-06-06 06:20:47 +02:00
|
|
|
static Kernel::Thread* FindThreadById(int id) {
|
2018-07-13 05:22:59 +02:00
|
|
|
for (u32 core = 0; core < Core::NUM_CPU_CORES; core++) {
|
|
|
|
const auto& threads = Core::System::GetInstance().Scheduler(core)->GetThreadList();
|
|
|
|
for (auto& thread : threads) {
|
2018-07-19 21:17:50 +02:00
|
|
|
if (thread->GetThreadId() == static_cast<u32>(id)) {
|
2018-07-13 05:22:59 +02:00
|
|
|
current_core = core;
|
|
|
|
return thread.get();
|
2018-06-06 06:20:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-19 21:17:50 +02:00
|
|
|
static u64 RegRead(std::size_t id, Kernel::Thread* thread = nullptr) {
|
2018-06-06 06:20:47 +02:00
|
|
|
if (!thread) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id < SP_REGISTER) {
|
|
|
|
return thread->context.cpu_registers[id];
|
|
|
|
} else if (id == SP_REGISTER) {
|
|
|
|
return thread->context.sp;
|
|
|
|
} else if (id == PC_REGISTER) {
|
|
|
|
return thread->context.pc;
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (id == PSTATE_REGISTER) {
|
|
|
|
return thread->context.pstate;
|
|
|
|
} else if (id > PSTATE_REGISTER && id < FPCR_REGISTER) {
|
|
|
|
return thread->context.vector_registers[id - UC_ARM64_REG_Q0][0];
|
2018-06-06 06:20:47 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 21:17:50 +02:00
|
|
|
static void RegWrite(std::size_t id, u64 val, Kernel::Thread* thread = nullptr) {
|
2018-06-06 06:20:47 +02:00
|
|
|
if (!thread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id < SP_REGISTER) {
|
|
|
|
thread->context.cpu_registers[id] = val;
|
|
|
|
} else if (id == SP_REGISTER) {
|
|
|
|
thread->context.sp = val;
|
|
|
|
} else if (id == PC_REGISTER) {
|
|
|
|
thread->context.pc = val;
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (id == PSTATE_REGISTER) {
|
|
|
|
thread->context.pstate = val;
|
|
|
|
} else if (id > PSTATE_REGISTER && id < FPCR_REGISTER) {
|
|
|
|
thread->context.vector_registers[id - (PSTATE_REGISTER + 1)][0] = val;
|
2018-06-06 06:20:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "Invalid nibble: {} ({:02X})", hex, hex);
|
2015-09-02 14:56:38 +02:00
|
|
|
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 {
|
2018-06-06 06:20:47 +02:00
|
|
|
return 'a' + n - 0xA;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 03:50:53 +01:00
|
|
|
/**
|
2018-01-20 00:01:41 +01:00
|
|
|
* Converts input hex string characters into an array of equivalent of u8 bytes.
|
|
|
|
*
|
|
|
|
* @param src Pointer to array of output hex string characters.
|
|
|
|
* @param len Length of src array.
|
|
|
|
*/
|
2018-09-15 15:21:06 +02:00
|
|
|
static u32 HexToInt(const u8* src, std::size_t len) {
|
2015-11-04 03:50:53 +01:00
|
|
|
u32 output = 0;
|
|
|
|
while (len-- > 0) {
|
|
|
|
output = (output << 4) | HexCharToValue(src[0]);
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2018-01-21 17:53:09 +01:00
|
|
|
/**
|
|
|
|
* Converts input hex string characters into an array of equivalent of u8 bytes.
|
|
|
|
*
|
|
|
|
* @param src Pointer to array of output hex string characters.
|
|
|
|
* @param len Length of src array.
|
|
|
|
*/
|
2018-09-15 15:21:06 +02:00
|
|
|
static u64 HexToLong(const u8* src, std::size_t len) {
|
2018-01-21 17:53:09 +01:00
|
|
|
u64 output = 0;
|
|
|
|
while (len-- > 0) {
|
|
|
|
output = (output << 4) | HexCharToValue(src[0]);
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-09-15 15:21:06 +02:00
|
|
|
static void MemToGdbHex(u8* dest, const u8* src, std::size_t len) {
|
2015-09-02 14:56:38 +02:00
|
|
|
while (len-- > 0) {
|
|
|
|
u8 tmp = *src++;
|
|
|
|
*dest++ = NibbleToHex(tmp >> 4);
|
|
|
|
*dest++ = NibbleToHex(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-04 03:50:53 +01:00
|
|
|
* Converts input gdb-formatted hex string characters into an array of equivalent of u8 bytes.
|
2015-09-02 14:56:38 +02:00
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2018-09-15 15:21:06 +02:00
|
|
|
static void GdbHexToMem(u8* dest, const u8* src, std::size_t len) {
|
2015-09-02 14:56:38 +02:00
|
|
|
while (len-- > 0) {
|
|
|
|
*dest++ = (HexCharToValue(src[0]) << 4) | HexCharToValue(src[1]);
|
|
|
|
src += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-04 03:50:53 +01:00
|
|
|
* Convert a u32 into a gdb-formatted hex string.
|
2015-09-02 14:56:38 +02:00
|
|
|
*
|
|
|
|
* @param dest Pointer to buffer to store output hex string characters.
|
2017-02-27 02:58:51 +01:00
|
|
|
* @param v Value to convert.
|
2015-09-02 14:56:38 +02:00
|
|
|
*/
|
2015-11-04 03:50:53 +01:00
|
|
|
static void IntToGdbHex(u8* dest, u32 v) {
|
2015-09-02 14:56:38 +02:00
|
|
|
for (int i = 0; i < 8; i += 2) {
|
2018-01-21 17:53:09 +01:00
|
|
|
dest[i + 1] = NibbleToHex(static_cast<u8>(v >> (4 * i)));
|
|
|
|
dest[i] = NibbleToHex(static_cast<u8>(v >> (4 * (i + 1))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a u64 into a gdb-formatted hex string.
|
|
|
|
*
|
|
|
|
* @param dest Pointer to buffer to store output hex string characters.
|
|
|
|
* @param v Value to convert.
|
|
|
|
*/
|
|
|
|
static void LongToGdbHex(u8* dest, u64 v) {
|
|
|
|
for (int i = 0; i < 16; i += 2) {
|
|
|
|
dest[i + 1] = NibbleToHex(static_cast<u8>(v >> (4 * i)));
|
|
|
|
dest[i] = NibbleToHex(static_cast<u8>(v >> (4 * (i + 1))));
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-04 03:50:53 +01:00
|
|
|
* Convert a gdb-formatted hex string into a u32.
|
2015-09-02 14:56:38 +02:00
|
|
|
*
|
|
|
|
* @param src Pointer to hex string.
|
|
|
|
*/
|
2016-12-16 10:05:55 +01:00
|
|
|
static u32 GdbHexToInt(const u8* src) {
|
2015-09-02 14:56:38 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-21 17:53:09 +01:00
|
|
|
/**
|
|
|
|
* Convert a gdb-formatted hex string into a u64.
|
|
|
|
*
|
|
|
|
* @param src Pointer to hex string.
|
|
|
|
*/
|
|
|
|
static u64 GdbHexToLong(const u8* src) {
|
|
|
|
u64 output = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 16; i += 2) {
|
|
|
|
output = (output << 4) | HexCharToValue(src[15 - i - 1]);
|
|
|
|
output = (output << 4) | HexCharToValue(src[15 - i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
/// Read a byte from the gdb client.
|
|
|
|
static u8 ReadByte() {
|
|
|
|
u8 c;
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t received_size = recv(gdbserver_socket, reinterpret_cast<char*>(&c), 1, MSG_WAITALL);
|
2015-09-02 14:56:38 +02:00
|
|
|
if (received_size != 1) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "recv failed: {}", received_size);
|
2015-10-12 02:07:58 +02:00
|
|
|
Shutdown();
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate the checksum of the current command buffer.
|
2018-09-15 15:21:06 +02:00
|
|
|
static u8 CalculateChecksum(const u8* buffer, std::size_t length) {
|
2015-09-02 14:56:38 +02:00
|
|
|
return static_cast<u8>(std::accumulate(buffer, buffer + length, 0, std::plus<u8>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-05 22:11:29 +02:00
|
|
|
* Get the map of breakpoints for a given breakpoint type.
|
2015-09-02 14:56:38 +02:00
|
|
|
*
|
2018-08-05 22:11:29 +02:00
|
|
|
* @param type Type of breakpoint map.
|
2015-09-02 14:56:38 +02:00
|
|
|
*/
|
2018-08-05 22:11:29 +02:00
|
|
|
static BreakpointMap& GetBreakpointMap(BreakpointType type) {
|
2015-09-02 14:56:38 +02:00
|
|
|
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.
|
|
|
|
*/
|
2018-08-05 21:55:57 +02:00
|
|
|
static void RemoveBreakpoint(BreakpointType type, VAddr addr) {
|
2018-08-05 22:11:29 +02:00
|
|
|
BreakpointMap& p = GetBreakpointMap(type);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 22:11:29 +02:00
|
|
|
const auto bp = p.find(addr);
|
|
|
|
if (bp == p.end()) {
|
|
|
|
return;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
2018-08-05 22:11:29 +02:00
|
|
|
|
|
|
|
LOG_DEBUG(Debug_GDBStub, "gdb: removed a breakpoint: {:016X} bytes at {:016X} of type {}",
|
|
|
|
bp->second.len, bp->second.addr, static_cast<int>(type));
|
2018-08-07 04:01:24 +02:00
|
|
|
Memory::WriteBlock(bp->second.addr, bp->second.inst.data(), bp->second.inst.size());
|
|
|
|
Core::System::GetInstance().InvalidateCpuInstructionCaches();
|
2018-08-05 22:11:29 +02:00
|
|
|
p.erase(addr);
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 21:55:57 +02:00
|
|
|
BreakpointAddress GetNextBreakpointFromAddress(VAddr addr, BreakpointType type) {
|
2018-08-05 22:11:29 +02:00
|
|
|
const BreakpointMap& p = GetBreakpointMap(type);
|
|
|
|
const auto next_breakpoint = p.lower_bound(addr);
|
2015-10-04 17:22:31 +02:00
|
|
|
BreakpointAddress breakpoint;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2015-10-04 17:22:31 +02:00
|
|
|
if (next_breakpoint != p.end()) {
|
|
|
|
breakpoint.address = next_breakpoint->first;
|
|
|
|
breakpoint.type = type;
|
|
|
|
} else {
|
|
|
|
breakpoint.address = 0;
|
|
|
|
breakpoint.type = BreakpointType::None;
|
|
|
|
}
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
return breakpoint;
|
|
|
|
}
|
|
|
|
|
2018-08-05 21:55:57 +02:00
|
|
|
bool CheckBreakpoint(VAddr addr, BreakpointType type) {
|
2015-09-02 14:56:38 +02:00
|
|
|
if (!IsConnected()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:11:29 +02:00
|
|
|
const BreakpointMap& p = GetBreakpointMap(type);
|
|
|
|
const auto bp = p.find(addr);
|
|
|
|
|
|
|
|
if (bp == p.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 22:11:29 +02:00
|
|
|
u64 len = bp->second.len;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 22:11:29 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-08-05 22:11:29 +02:00
|
|
|
if (bp->second.active && (addr >= bp->second.addr && addr < bp->second.addr + len)) {
|
|
|
|
LOG_DEBUG(Debug_GDBStub,
|
|
|
|
"Found breakpoint type {} @ {:016X}, range: {:016X}"
|
|
|
|
" - {:016X} ({:X} bytes)",
|
|
|
|
static_cast<int>(type), addr, bp->second.addr, bp->second.addr + len, len);
|
|
|
|
return true;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send packet to gdb client.
|
|
|
|
*
|
|
|
|
* @param packet Packet to be sent to client.
|
|
|
|
*/
|
|
|
|
static void SendPacket(const char packet) {
|
2018-09-15 15:21:06 +02:00
|
|
|
std::size_t sent_size = send(gdbserver_socket, &packet, 1, 0);
|
2015-09-02 14:56:38 +02:00
|
|
|
if (sent_size != 1) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "send failed");
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send reply to gdb client.
|
|
|
|
*
|
|
|
|
* @param reply Reply to be sent to client.
|
|
|
|
*/
|
|
|
|
static void SendReply(const char* reply) {
|
|
|
|
if (!IsConnected()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Debug_GDBStub, "Reply: {}", reply);
|
2018-06-06 06:20:47 +02:00
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
memset(command_buffer, 0, sizeof(command_buffer));
|
|
|
|
|
2016-04-25 22:10:03 +02:00
|
|
|
command_length = static_cast<u32>(strlen(reply));
|
2015-09-02 14:56:38 +02:00
|
|
|
if (command_length + 4 > sizeof(command_buffer)) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "command_buffer overflow in SendReply");
|
2015-10-21 12:49:49 +02:00
|
|
|
return;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "gdb: send failed");
|
2015-10-12 02:07:58 +02:00
|
|
|
return Shutdown();
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
left -= sent_size;
|
|
|
|
ptr += sent_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle query command from gdb client.
|
|
|
|
static void HandleQuery() {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Debug_GDBStub, "gdb: query '{}'", command_buffer + 1);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2016-04-06 13:01:00 +02:00
|
|
|
const char* query = reinterpret_cast<const char*>(command_buffer + 1);
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
if (strcmp(query, "TStatus") == 0) {
|
2015-09-02 14:56:38 +02:00
|
|
|
SendReply("T0");
|
2016-10-28 19:50:45 +02:00
|
|
|
} else if (strncmp(query, "Supported", strlen("Supported")) == 0) {
|
2016-04-06 13:01:00 +02:00
|
|
|
// PacketSize needs to be large enough for target xml
|
2018-07-13 05:22:59 +02:00
|
|
|
std::string buffer = "PacketSize=2000;qXfer:features:read+;qXfer:threads:read+";
|
|
|
|
if (!modules.empty()) {
|
|
|
|
buffer += ";qXfer:libraries:read+";
|
|
|
|
}
|
|
|
|
SendReply(buffer.c_str());
|
2016-09-18 02:38:01 +02:00
|
|
|
} else if (strncmp(query, "Xfer:features:read:target.xml:",
|
|
|
|
strlen("Xfer:features:read:target.xml:")) == 0) {
|
2016-04-06 13:01:00 +02:00
|
|
|
SendReply(target_xml);
|
2018-06-06 06:20:47 +02:00
|
|
|
} else if (strncmp(query, "Offsets", strlen("Offsets")) == 0) {
|
2018-09-25 02:01:45 +02:00
|
|
|
const VAddr base_address = Core::CurrentProcess()->vm_manager.GetCodeRegionBaseAddress();
|
|
|
|
std::string buffer = fmt::format("TextSeg={:0x}", base_address);
|
2018-06-06 06:20:47 +02:00
|
|
|
SendReply(buffer.c_str());
|
|
|
|
} else if (strncmp(query, "fThreadInfo", strlen("fThreadInfo")) == 0) {
|
|
|
|
std::string val = "m";
|
2018-07-13 05:22:59 +02:00
|
|
|
for (u32 core = 0; core < Core::NUM_CPU_CORES; core++) {
|
|
|
|
const auto& threads = Core::System::GetInstance().Scheduler(core)->GetThreadList();
|
|
|
|
for (const auto& thread : threads) {
|
2018-06-06 06:20:47 +02:00
|
|
|
val += fmt::format("{:x}", thread->GetThreadId());
|
|
|
|
val += ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val.pop_back();
|
|
|
|
SendReply(val.c_str());
|
|
|
|
} else if (strncmp(query, "sThreadInfo", strlen("sThreadInfo")) == 0) {
|
|
|
|
SendReply("l");
|
2018-07-13 05:22:59 +02:00
|
|
|
} else if (strncmp(query, "Xfer:threads:read", strlen("Xfer:threads:read")) == 0) {
|
|
|
|
std::string buffer;
|
|
|
|
buffer += "l<?xml version=\"1.0\"?>";
|
|
|
|
buffer += "<threads>";
|
|
|
|
for (u32 core = 0; core < Core::NUM_CPU_CORES; core++) {
|
|
|
|
const auto& threads = Core::System::GetInstance().Scheduler(core)->GetThreadList();
|
|
|
|
for (const auto& thread : threads) {
|
|
|
|
buffer +=
|
|
|
|
fmt::format(R"*(<thread id="{:x}" core="{:d}" name="Thread {:x}"></thread>)*",
|
|
|
|
thread->GetThreadId(), core, thread->GetThreadId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer += "</threads>";
|
|
|
|
SendReply(buffer.c_str());
|
|
|
|
} else if (strncmp(query, "Xfer:libraries:read", strlen("Xfer:libraries:read")) == 0) {
|
|
|
|
std::string buffer;
|
|
|
|
buffer += "l<?xml version=\"1.0\"?>";
|
|
|
|
buffer += "<library-list>";
|
|
|
|
for (const auto& module : modules) {
|
|
|
|
buffer +=
|
|
|
|
fmt::format(R"*("<library name = "{}"><segment address = "0x{:x}"/></library>)*",
|
|
|
|
module.name, module.beg);
|
|
|
|
}
|
|
|
|
buffer += "</library-list>";
|
|
|
|
SendReply(buffer.c_str());
|
2015-09-02 14:56:38 +02:00
|
|
|
} else {
|
|
|
|
SendReply("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle set thread command from gdb client.
|
|
|
|
static void HandleSetThread() {
|
2018-07-13 05:22:59 +02:00
|
|
|
int thread_id = -1;
|
|
|
|
if (command_buffer[2] != '-') {
|
|
|
|
thread_id = static_cast<int>(HexToInt(command_buffer + 2, command_length - 2));
|
|
|
|
}
|
|
|
|
if (thread_id >= 1) {
|
|
|
|
current_thread = FindThreadById(thread_id);
|
|
|
|
}
|
|
|
|
if (!current_thread) {
|
|
|
|
thread_id = 1;
|
|
|
|
current_thread = FindThreadById(thread_id);
|
|
|
|
}
|
|
|
|
if (current_thread) {
|
|
|
|
SendReply("OK");
|
|
|
|
return;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
2018-06-06 06:20:47 +02:00
|
|
|
SendReply("E01");
|
|
|
|
}
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-06-06 06:20:47 +02:00
|
|
|
/// Handle thread alive command from gdb client.
|
|
|
|
static void HandleThreadAlive() {
|
2018-07-13 05:22:59 +02:00
|
|
|
int thread_id = static_cast<int>(HexToInt(command_buffer + 1, command_length - 1));
|
2018-06-06 06:20:47 +02:00
|
|
|
if (thread_id == 0) {
|
|
|
|
thread_id = 1;
|
|
|
|
}
|
|
|
|
if (FindThreadById(thread_id)) {
|
|
|
|
SendReply("OK");
|
|
|
|
return;
|
|
|
|
}
|
2015-09-02 14:56:38 +02:00
|
|
|
SendReply("E01");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-22 04:19:55 +02:00
|
|
|
* Send signal packet to client.
|
2015-09-02 14:56:38 +02:00
|
|
|
*
|
|
|
|
* @param signal Signal to be sent to client.
|
|
|
|
*/
|
2018-06-06 06:20:47 +02:00
|
|
|
static void SendSignal(Kernel::Thread* thread, u32 signal, bool full = true) {
|
2015-09-02 14:56:38 +02:00
|
|
|
if (gdbserver_socket == -1) {
|
2015-10-22 04:19:55 +02:00
|
|
|
return;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
latest_signal = signal;
|
|
|
|
|
2018-07-13 05:22:59 +02:00
|
|
|
if (!thread) {
|
|
|
|
full = false;
|
|
|
|
}
|
|
|
|
|
2018-06-06 06:20:47 +02:00
|
|
|
std::string buffer;
|
|
|
|
if (full) {
|
2018-07-13 05:22:59 +02:00
|
|
|
buffer = fmt::format("T{:02x}{:02x}:{:016x};{:02x}:{:016x};{:02x}:{:016x}", latest_signal,
|
|
|
|
PC_REGISTER, Common::swap64(RegRead(PC_REGISTER, thread)), SP_REGISTER,
|
|
|
|
Common::swap64(RegRead(SP_REGISTER, thread)), LR_REGISTER,
|
|
|
|
Common::swap64(RegRead(LR_REGISTER, thread)));
|
2018-06-06 06:20:47 +02:00
|
|
|
} else {
|
2018-07-13 05:22:59 +02:00
|
|
|
buffer = fmt::format("T{:02x}", latest_signal);
|
2018-06-06 06:20:47 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 05:22:59 +02:00
|
|
|
if (thread) {
|
|
|
|
buffer += fmt::format(";thread:{:x};", thread->GetThreadId());
|
|
|
|
}
|
2018-06-06 06:20:47 +02:00
|
|
|
|
2015-10-22 04:19:55 +02:00
|
|
|
SendReply(buffer.c_str());
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read command from gdb client.
|
|
|
|
static void ReadCommand() {
|
|
|
|
command_length = 0;
|
|
|
|
memset(command_buffer, 0, sizeof(command_buffer));
|
|
|
|
|
|
|
|
u8 c = ReadByte();
|
|
|
|
if (c == '+') {
|
2016-09-18 02:38:01 +02:00
|
|
|
// ignore ack
|
2015-09-02 14:56:38 +02:00
|
|
|
return;
|
|
|
|
} else if (c == 0x03) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_INFO(Debug_GDBStub, "gdb: found break command");
|
2015-09-02 14:56:38 +02:00
|
|
|
halt_loop = true;
|
2018-06-06 06:20:47 +02:00
|
|
|
SendSignal(current_thread, SIGTRAP);
|
2015-09-02 14:56:38 +02:00
|
|
|
return;
|
|
|
|
} else if (c != GDB_STUB_START) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Debug_GDBStub, "gdb: read invalid byte {:02X}", c);
|
2015-09-02 14:56:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((c = ReadByte()) != GDB_STUB_END) {
|
2015-10-21 12:49:49 +02:00
|
|
|
if (command_length >= sizeof(command_buffer)) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "gdb: command_buffer overflow");
|
2015-09-02 14:56:38 +02:00
|
|
|
SendPacket(GDB_STUB_NACK);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-21 12:49:49 +02:00
|
|
|
command_buffer[command_length++] = c;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 checksum_received = HexCharToValue(ReadByte()) << 4;
|
|
|
|
checksum_received |= HexCharToValue(ReadByte());
|
|
|
|
|
|
|
|
u8 checksum_calculated = CalculateChecksum(command_buffer, command_length);
|
|
|
|
|
|
|
|
if (checksum_received != checksum_calculated) {
|
2018-07-02 18:20:50 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub,
|
|
|
|
"gdb: invalid checksum: calculated {:02X} and read {:02X} for ${}# (length: {})",
|
|
|
|
checksum_calculated, checksum_received, command_buffer, command_length);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
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);
|
2018-07-19 21:17:50 +02:00
|
|
|
FD_SET(static_cast<u32>(gdbserver_socket), &fd_socket);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
struct timeval t;
|
|
|
|
t.tv_sec = 0;
|
|
|
|
t.tv_usec = 0;
|
|
|
|
|
|
|
|
if (select(gdbserver_socket + 1, &fd_socket, nullptr, nullptr, &t) < 0) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "select failed");
|
2015-09-02 14:56:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:10:03 +02:00
|
|
|
return FD_ISSET(gdbserver_socket, &fd_socket) != 0;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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]);
|
|
|
|
}
|
|
|
|
|
2018-01-21 17:53:09 +01:00
|
|
|
if (id <= SP_REGISTER) {
|
2018-06-06 06:20:47 +02:00
|
|
|
LongToGdbHex(reply, RegRead(id, current_thread));
|
2018-01-21 17:53:09 +01:00
|
|
|
} else if (id == PC_REGISTER) {
|
2018-06-06 06:20:47 +02:00
|
|
|
LongToGdbHex(reply, RegRead(id, current_thread));
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (id == PSTATE_REGISTER) {
|
|
|
|
IntToGdbHex(reply, static_cast<u32>(RegRead(id, current_thread)));
|
|
|
|
} else if (id >= UC_ARM64_REG_Q0 && id < FPCR_REGISTER) {
|
2018-07-13 05:22:59 +02:00
|
|
|
LongToGdbHex(reply, RegRead(id, current_thread));
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (id == FPCR_REGISTER) {
|
2018-07-13 05:22:59 +02:00
|
|
|
LongToGdbHex(reply, RegRead(TODO_DUMMY_REG_998, current_thread));
|
2015-09-02 14:56:38 +02:00
|
|
|
} else {
|
2018-07-13 05:22:59 +02:00
|
|
|
LongToGdbHex(reply, RegRead(TODO_DUMMY_REG_997, current_thread));
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-04-06 13:01:00 +02:00
|
|
|
|
2018-07-19 21:17:50 +02:00
|
|
|
for (u32 reg = 0; reg <= SP_REGISTER; reg++) {
|
2018-06-06 06:20:47 +02:00
|
|
|
LongToGdbHex(bufptr + reg * 16, RegRead(reg, current_thread));
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 05:22:59 +02:00
|
|
|
bufptr += 32 * 16;
|
2016-04-06 13:01:00 +02:00
|
|
|
|
2018-06-06 06:20:47 +02:00
|
|
|
LongToGdbHex(bufptr, RegRead(PC_REGISTER, current_thread));
|
2016-04-06 13:01:00 +02:00
|
|
|
|
2018-01-21 17:53:09 +01:00
|
|
|
bufptr += 16;
|
2016-04-06 13:01:00 +02:00
|
|
|
|
2018-09-18 08:49:40 +02:00
|
|
|
IntToGdbHex(bufptr, static_cast<u32>(RegRead(PSTATE_REGISTER, current_thread)));
|
2016-04-06 13:01:00 +02:00
|
|
|
|
2018-01-21 17:53:09 +01:00
|
|
|
bufptr += 8;
|
2016-04-06 13:01:00 +02:00
|
|
|
|
2018-07-19 21:17:50 +02:00
|
|
|
for (u32 reg = UC_ARM64_REG_Q0; reg <= UC_ARM64_REG_Q0 + 31; reg++) {
|
2018-07-13 05:22:59 +02:00
|
|
|
LongToGdbHex(bufptr + reg * 16, RegRead(reg, current_thread));
|
|
|
|
}
|
|
|
|
|
|
|
|
bufptr += 32 * 32;
|
|
|
|
|
|
|
|
LongToGdbHex(bufptr, RegRead(TODO_DUMMY_REG_998, current_thread));
|
|
|
|
|
|
|
|
bufptr += 8;
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
SendReply(reinterpret_cast<char*>(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Modify data of register specified by gdb client.
|
|
|
|
static void WriteRegister() {
|
2016-12-16 10:05:55 +01:00
|
|
|
const u8* buffer_ptr = command_buffer + 3;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
u32 id = HexCharToValue(command_buffer[1]);
|
|
|
|
if (command_buffer[2] != '=') {
|
|
|
|
++buffer_ptr;
|
|
|
|
id <<= 4;
|
|
|
|
id |= HexCharToValue(command_buffer[2]);
|
|
|
|
}
|
|
|
|
|
2018-01-21 17:53:09 +01:00
|
|
|
if (id <= SP_REGISTER) {
|
2018-06-06 06:20:47 +02:00
|
|
|
RegWrite(id, GdbHexToLong(buffer_ptr), current_thread);
|
2018-01-21 17:53:09 +01:00
|
|
|
} else if (id == PC_REGISTER) {
|
2018-06-06 06:20:47 +02:00
|
|
|
RegWrite(id, GdbHexToLong(buffer_ptr), current_thread);
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (id == PSTATE_REGISTER) {
|
2018-06-06 06:20:47 +02:00
|
|
|
RegWrite(id, GdbHexToInt(buffer_ptr), current_thread);
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (id >= UC_ARM64_REG_Q0 && id < FPCR_REGISTER) {
|
2018-07-13 05:22:59 +02:00
|
|
|
RegWrite(id, GdbHexToLong(buffer_ptr), current_thread);
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (id == FPCR_REGISTER) {
|
2018-07-13 05:22:59 +02:00
|
|
|
RegWrite(TODO_DUMMY_REG_998, GdbHexToLong(buffer_ptr), current_thread);
|
2015-09-02 14:56:38 +02:00
|
|
|
} else {
|
2018-07-13 05:22:59 +02:00
|
|
|
RegWrite(TODO_DUMMY_REG_997, GdbHexToLong(buffer_ptr), current_thread);
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 05:22:59 +02:00
|
|
|
// Update Unicorn context skipping scheduler, no running threads at this point
|
|
|
|
Core::System::GetInstance().ArmInterface(current_core).LoadContext(current_thread->context);
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
SendReply("OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Modify all registers with data received from the client.
|
|
|
|
static void WriteRegisters() {
|
2016-12-16 10:05:55 +01:00
|
|
|
const u8* buffer_ptr = command_buffer + 1;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
if (command_buffer[0] != 'G')
|
|
|
|
return SendReply("E01");
|
|
|
|
|
2018-09-18 08:49:40 +02:00
|
|
|
for (u32 i = 0, reg = 0; reg <= FPCR_REGISTER; i++, reg++) {
|
2018-01-21 17:53:09 +01:00
|
|
|
if (reg <= SP_REGISTER) {
|
2018-06-06 06:20:47 +02:00
|
|
|
RegWrite(reg, GdbHexToLong(buffer_ptr + i * 16), current_thread);
|
2018-01-21 17:53:09 +01:00
|
|
|
} else if (reg == PC_REGISTER) {
|
2018-06-06 06:20:47 +02:00
|
|
|
RegWrite(PC_REGISTER, GdbHexToLong(buffer_ptr + i * 16), current_thread);
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (reg == PSTATE_REGISTER) {
|
|
|
|
RegWrite(PSTATE_REGISTER, GdbHexToInt(buffer_ptr + i * 16), current_thread);
|
|
|
|
} else if (reg >= UC_ARM64_REG_Q0 && reg < FPCR_REGISTER) {
|
2018-07-13 05:22:59 +02:00
|
|
|
RegWrite(reg, GdbHexToLong(buffer_ptr + i * 16), current_thread);
|
2018-09-18 08:49:40 +02:00
|
|
|
} else if (reg == FPCR_REGISTER) {
|
2018-07-13 05:22:59 +02:00
|
|
|
RegWrite(TODO_DUMMY_REG_998, GdbHexToLong(buffer_ptr + i * 16), current_thread);
|
2018-01-21 17:53:09 +01:00
|
|
|
} else {
|
2018-01-03 04:24:12 +01:00
|
|
|
UNIMPLEMENTED();
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 05:22:59 +02:00
|
|
|
// Update Unicorn context skipping scheduler, no running threads at this point
|
|
|
|
Core::System::GetInstance().ArmInterface(current_core).LoadContext(current_thread->context);
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
SendReply("OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read location in memory specified by gdb client.
|
|
|
|
static void ReadMemory() {
|
|
|
|
static u8 reply[GDB_BUFFER_SIZE - 4];
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
auto start_offset = command_buffer + 1;
|
2018-09-25 02:01:45 +02:00
|
|
|
const auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
|
|
|
|
const VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
start_offset = addr_pos + 1;
|
2018-09-25 02:01:45 +02:00
|
|
|
const u64 len =
|
2018-01-21 17:53:09 +01:00
|
|
|
HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset));
|
2015-11-04 03:50:53 +01:00
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Debug_GDBStub, "gdb: addr: {:016X} len: {:016X}", addr, len);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
if (len * 2 > sizeof(reply)) {
|
|
|
|
SendReply("E01");
|
|
|
|
}
|
|
|
|
|
2018-09-25 02:01:45 +02:00
|
|
|
const auto& vm_manager = Core::CurrentProcess()->vm_manager;
|
|
|
|
if (addr < vm_manager.GetCodeRegionBaseAddress() ||
|
|
|
|
addr >= vm_manager.GetMapRegionEndAddress()) {
|
2018-07-13 05:22:59 +02:00
|
|
|
return SendReply("E00");
|
|
|
|
}
|
|
|
|
|
2017-10-04 18:33:32 +02:00
|
|
|
if (!Memory::IsValidVirtualAddress(addr)) {
|
2016-06-08 23:14:59 +02:00
|
|
|
return SendReply("E00");
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 18:33:32 +02:00
|
|
|
std::vector<u8> data(len);
|
|
|
|
Memory::ReadBlock(addr, data.data(), len);
|
|
|
|
|
|
|
|
MemToGdbHex(reply, data.data(), len);
|
2015-09-02 14:56:38 +02:00
|
|
|
reply[len * 2] = '\0';
|
|
|
|
SendReply(reinterpret_cast<char*>(reply));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Modify location in memory with data received from the gdb client.
|
|
|
|
static void WriteMemory() {
|
2016-09-18 02:38:01 +02:00
|
|
|
auto start_offset = command_buffer + 1;
|
|
|
|
auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
|
2018-01-21 17:53:09 +01:00
|
|
|
VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
start_offset = addr_pos + 1;
|
|
|
|
auto len_pos = std::find(start_offset, command_buffer + command_length, ':');
|
2018-01-21 17:53:09 +01:00
|
|
|
u64 len = HexToLong(start_offset, static_cast<u64>(len_pos - start_offset));
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2017-10-04 18:33:32 +02:00
|
|
|
if (!Memory::IsValidVirtualAddress(addr)) {
|
2015-09-02 14:56:38 +02:00
|
|
|
return SendReply("E00");
|
|
|
|
}
|
|
|
|
|
2017-10-04 18:33:32 +02:00
|
|
|
std::vector<u8> data(len);
|
|
|
|
|
|
|
|
GdbHexToMem(data.data(), len_pos + 1, len);
|
|
|
|
Memory::WriteBlock(addr, data.data(), len);
|
2018-08-07 04:01:24 +02:00
|
|
|
Core::System::GetInstance().InvalidateCpuInstructionCaches();
|
2015-09-02 14:56:38 +02:00
|
|
|
SendReply("OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Break(bool is_memory_break) {
|
2018-07-13 05:22:59 +02:00
|
|
|
send_trap = true;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
memory_break = is_memory_break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell the CPU that it should perform a single step.
|
|
|
|
static void Step() {
|
2018-07-13 05:22:59 +02:00
|
|
|
if (command_length > 1) {
|
|
|
|
RegWrite(PC_REGISTER, GdbHexToLong(command_buffer + 1), current_thread);
|
|
|
|
// Update Unicorn context skipping scheduler, no running threads at this point
|
|
|
|
Core::System::GetInstance().ArmInterface(current_core).LoadContext(current_thread->context);
|
|
|
|
}
|
2015-09-02 14:56:38 +02:00
|
|
|
step_loop = true;
|
|
|
|
halt_loop = true;
|
2018-06-06 06:20:47 +02:00
|
|
|
send_trap = true;
|
2018-08-07 04:01:24 +02:00
|
|
|
Core::System::GetInstance().InvalidateCpuInstructionCaches();
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2018-06-06 06:20:47 +02:00
|
|
|
/// Tell the CPU if we hit a memory breakpoint.
|
2015-09-02 14:56:38 +02:00
|
|
|
bool IsMemoryBreak() {
|
|
|
|
if (IsConnected()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return memory_break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tell the CPU to continue executing.
|
|
|
|
static void Continue() {
|
|
|
|
memory_break = false;
|
|
|
|
step_loop = false;
|
|
|
|
halt_loop = false;
|
2018-08-07 04:01:24 +02:00
|
|
|
Core::System::GetInstance().InvalidateCpuInstructionCaches();
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit breakpoint to list of breakpoints.
|
|
|
|
*
|
|
|
|
* @param type Type of breakpoint.
|
|
|
|
* @param addr Address of breakpoint.
|
|
|
|
* @param len Length of breakpoint.
|
|
|
|
*/
|
2018-08-05 21:55:57 +02:00
|
|
|
static bool CommitBreakpoint(BreakpointType type, VAddr addr, u64 len) {
|
2018-08-05 22:11:29 +02:00
|
|
|
BreakpointMap& p = GetBreakpointMap(type);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
Breakpoint breakpoint;
|
|
|
|
breakpoint.active = true;
|
|
|
|
breakpoint.addr = addr;
|
|
|
|
breakpoint.len = len;
|
2018-08-07 04:01:24 +02:00
|
|
|
Memory::ReadBlock(addr, breakpoint.inst.data(), breakpoint.inst.size());
|
2018-09-20 23:03:57 +02:00
|
|
|
static constexpr std::array<u8, 4> btrap{{0x00, 0x7d, 0x20, 0xd4}};
|
2018-08-07 04:01:24 +02:00
|
|
|
Memory::WriteBlock(addr, btrap.data(), btrap.size());
|
|
|
|
Core::System::GetInstance().InvalidateCpuInstructionCaches();
|
2016-09-18 02:38:01 +02:00
|
|
|
p.insert({addr, breakpoint});
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Debug_GDBStub, "gdb: added {} breakpoint: {:016X} bytes at {:016X}",
|
2018-07-02 18:20:50 +02:00
|
|
|
static_cast<int>(type), breakpoint.len, breakpoint.addr);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
auto start_offset = command_buffer + 3;
|
|
|
|
auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
|
2018-08-05 21:55:57 +02:00
|
|
|
VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
|
2015-09-02 14:56:38 +02:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
start_offset = addr_pos + 1;
|
2018-01-21 17:53:09 +01:00
|
|
|
u64 len =
|
|
|
|
HexToLong(start_offset, static_cast<u64>((command_buffer + command_length) - start_offset));
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
auto start_offset = command_buffer + 3;
|
|
|
|
auto addr_pos = std::find(start_offset, command_buffer + command_length, ',');
|
2018-08-05 21:55:57 +02:00
|
|
|
VAddr addr = HexToLong(start_offset, static_cast<u64>(addr_pos - start_offset));
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_DEBUG(Debug_GDBStub, "Packet: {}", command_buffer);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
switch (command_buffer[0]) {
|
|
|
|
case 'q':
|
|
|
|
HandleQuery();
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
HandleSetThread();
|
|
|
|
break;
|
|
|
|
case '?':
|
2018-06-06 06:20:47 +02:00
|
|
|
SendSignal(current_thread, latest_signal);
|
2015-09-02 14:56:38 +02:00
|
|
|
break;
|
|
|
|
case 'k':
|
2015-10-12 02:07:58 +02:00
|
|
|
Shutdown();
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_INFO(Debug_GDBStub, "killed by gdb");
|
2015-09-02 14:56:38 +02:00
|
|
|
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;
|
2018-06-06 06:20:47 +02:00
|
|
|
case 'T':
|
|
|
|
HandleThreadAlive();
|
|
|
|
break;
|
2015-09-02 14:56:38 +02:00
|
|
|
default:
|
|
|
|
SendReply("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetServerPort(u16 port) {
|
|
|
|
gdbstub_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToggleServer(bool status) {
|
|
|
|
if (status) {
|
2016-12-15 22:19:30 +01:00
|
|
|
server_enabled = status;
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
// Start server
|
2018-01-17 15:25:25 +01:00
|
|
|
if (!IsConnected() && Core::System::GetInstance().IsPoweredOn()) {
|
2015-09-02 14:56:38 +02:00
|
|
|
Init();
|
|
|
|
}
|
2016-09-18 02:38:01 +02:00
|
|
|
} else {
|
2015-09-02 14:56:38 +02:00
|
|
|
// Stop server
|
|
|
|
if (IsConnected()) {
|
2015-10-12 02:07:58 +02:00
|
|
|
Shutdown();
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 22:19:30 +01:00
|
|
|
server_enabled = status;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 15:17:07 +02:00
|
|
|
static void Init(u16 port) {
|
2016-12-15 22:19:30 +01:00
|
|
|
if (!server_enabled) {
|
2015-09-02 14:56:38 +02:00
|
|
|
// 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();
|
|
|
|
|
2018-07-13 05:22:59 +02:00
|
|
|
modules.clear();
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
// Start gdb server
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_INFO(Debug_GDBStub, "Starting GDB server on port {}...", port);
|
2015-09-02 14:56:38 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-09-27 01:26:09 +02:00
|
|
|
int tmpsock = static_cast<int>(socket(PF_INET, SOCK_STREAM, 0));
|
2015-09-02 14:56:38 +02:00
|
|
|
if (tmpsock == -1) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket");
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 13:01:00 +02:00
|
|
|
// Set socket to SO_REUSEADDR so it can always bind on the same port
|
|
|
|
int reuse_enabled = 1;
|
2016-09-18 02:38:01 +02:00
|
|
|
if (setsockopt(tmpsock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse_enabled,
|
|
|
|
sizeof(reuse_enabled)) < 0) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "Failed to set gdb socket option");
|
2016-04-06 13:01:00 +02:00
|
|
|
}
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
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) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "Failed to bind gdb socket");
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(tmpsock, 1) < 0) {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "Failed to listen to gdb socket");
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for gdb to connect
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_INFO(Debug_GDBStub, "Waiting for gdb to connect...");
|
2015-09-02 14:56:38 +02:00
|
|
|
sockaddr_in saddr_client;
|
|
|
|
sockaddr* client_addr = reinterpret_cast<sockaddr*>(&saddr_client);
|
|
|
|
socklen_t client_addrlen = sizeof(saddr_client);
|
2017-09-27 01:26:09 +02:00
|
|
|
gdbserver_socket = static_cast<int>(accept(tmpsock, client_addr, &client_addrlen));
|
2015-09-02 14:56:38 +02:00
|
|
|
if (gdbserver_socket < 0) {
|
2016-09-18 02:38:01 +02:00
|
|
|
// In the case that we couldn't start the server for whatever reason, just start CPU
|
|
|
|
// execution like normal.
|
2015-09-02 14:56:38 +02:00
|
|
|
halt_loop = false;
|
|
|
|
step_loop = false;
|
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_ERROR(Debug_GDBStub, "Failed to accept gdb client");
|
2016-09-18 02:38:01 +02:00
|
|
|
} else {
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_INFO(Debug_GDBStub, "Client connected.");
|
2015-09-02 14:56:38 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-10-12 02:07:58 +02:00
|
|
|
void Shutdown() {
|
2016-12-15 22:19:30 +01:00
|
|
|
if (!server_enabled) {
|
2015-09-02 14:56:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_INFO(Debug_GDBStub, "Stopping GDB ...");
|
2015-09-02 14:56:38 +02:00
|
|
|
if (gdbserver_socket != -1) {
|
|
|
|
shutdown(gdbserver_socket, SHUT_RDWR);
|
|
|
|
gdbserver_socket = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
|
2018-07-02 18:13:26 +02:00
|
|
|
LOG_INFO(Debug_GDBStub, "GDB stopped.");
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 22:19:30 +01:00
|
|
|
bool IsServerEnabled() {
|
|
|
|
return server_enabled;
|
|
|
|
}
|
|
|
|
|
2015-09-02 14:56:38 +02:00
|
|
|
bool IsConnected() {
|
2016-12-15 22:19:30 +01:00
|
|
|
return IsServerEnabled() && gdbserver_socket != -1;
|
2015-09-02 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetCpuHaltFlag() {
|
|
|
|
return halt_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetCpuStepFlag() {
|
|
|
|
return step_loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCpuStepFlag(bool is_step) {
|
|
|
|
step_loop = is_step;
|
|
|
|
}
|
2018-06-06 06:20:47 +02:00
|
|
|
|
|
|
|
void SendTrap(Kernel::Thread* thread, int trap) {
|
|
|
|
if (send_trap) {
|
2018-07-13 05:22:59 +02:00
|
|
|
if (!halt_loop || current_thread == thread) {
|
|
|
|
current_thread = thread;
|
|
|
|
SendSignal(thread, trap);
|
|
|
|
}
|
|
|
|
halt_loop = true;
|
2018-06-06 06:20:47 +02:00
|
|
|
send_trap = false;
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 00:01:41 +01:00
|
|
|
}; // namespace GDBStub
|