Update gdbstub.

This commit is contained in:
Colin 2025-09-22 07:28:09 +00:00
parent 9ac39e6a12
commit 8eaf2c0b7b
10 changed files with 2808 additions and 482 deletions

View File

@ -53,6 +53,7 @@
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"internals.h": "c"
}
}

30
backup/breakpoint.h Normal file
View File

@ -0,0 +1,30 @@
/*
* QEMU breakpoint & watchpoint definitions
*
* Copyright (c) 2012 SUSE LINUX Products GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef EXEC_BREAKPOINT_H
#define EXEC_BREAKPOINT_H
#include "qemu/queue.h"
#include "exec/vaddr.h"
#include "exec/memattrs.h"
typedef struct CPUBreakpoint {
vaddr pc;
int flags; /* BP_* */
QTAILQ_ENTRY(CPUBreakpoint) entry;
} CPUBreakpoint;
typedef struct CPUWatchpoint {
vaddr vaddr;
vaddr len;
vaddr hitaddr;
MemTxAttrs hitattrs;
int flags; /* BP_* */
QTAILQ_ENTRY(CPUWatchpoint) entry;
} CPUWatchpoint;
#endif

1178
backup/cpu.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -46,6 +46,13 @@ set(CMAKE_CXX_EXTENSIONS ON)
find_package(PkgConfig REQUIRED) # GLib
pkg_check_modules(GLIB REQUIRED glib-2.0)
include_directories(${GLIB_INCLUDE_DIRS}) #
link_directories(${GLIB_LIBRARY_DIRS}) #
file(GLOB UTILS_SRC
${CMAKE_CURRENT_SOURCE_DIR}/utils/*.cpp
@ -55,4 +62,4 @@ set_source_files_properties(${UTILS_SRC} PROPERTIES LANGUAGE CXX)
add_executable(stub ${UTILS_SRC} stub.cpp)
target_include_directories(stub PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(stub PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/utils)
target_link_libraries(stub ${GLIB_LIBRARIES})

30
gdbstub/breakpoint.h Normal file
View File

@ -0,0 +1,30 @@
/*
* QEMU breakpoint & watchpoint definitions
*
* Copyright (c) 2012 SUSE LINUX Products GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef EXEC_BREAKPOINT_H
#define EXEC_BREAKPOINT_H
// #include "qemu/queue.h"
// #include "exec/vaddr.h"
// #include "exec/memattrs.h"
typedef struct CPUBreakpoint {
uintptr_t pc;
int flags; /* BP_* */
struct CPUBreakpoint* entry;
} CPUBreakpoint;
typedef struct CPUWatchpoint {
uintptr_t vaddr;
uintptr_t len;
uintptr_t hitaddr;
// MemTxAttrs hitattrs;
int flags; /* BP_* */
struct CPUWatchpoint* entry;
} CPUWatchpoint;
#endif

1151
gdbstub/cpu.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,12 @@
#define DEFAULT_GDBSTUB_PORT "1234"
typedef uintptr_t vaddr;
typedef int Error;
#define MMU_ACCESS_COUNT 16
/* GDB breakpoint/watchpoint types */
#define GDB_BREAKPOINT_SW 0
#define GDB_BREAKPOINT_HW 1

View File

@ -1,6 +1,8 @@
#ifndef GDBSTUB_H
#define GDBSTUB_H
#include "cpu.h"
typedef struct GDBFeature {
const char *xmlname;
const char *xml;
@ -16,7 +18,6 @@ typedef struct GDBFeatureBuilder {
int base_reg;
} GDBFeatureBuilder;
/* Get or set a register. Returns the size of the register. */
typedef int (*gdb_get_reg_cb)(CPUState *cpu, GByteArray *buf, int reg);
typedef int (*gdb_set_reg_cb)(CPUState *cpu, uint8_t *buf, int reg);
@ -36,9 +37,8 @@ void gdb_init_cpu(CPUState *cpu);
* @xml - xml name of set
* @gpos - non-zero to append to "general" register set at @gpos
*/
void gdb_register_coprocessor(CPUState *cpu,
gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
const GDBFeature *feature, int g_pos);
void gdb_register_coprocessor(CPUState *cpu, gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, const GDBFeature *feature,
int g_pos);
/**
* gdb_unregister_coprocessor_all() - unregisters supplemental set of registers
@ -70,8 +70,7 @@ bool gdbserver_start(const char *port_or_device, Error **errp);
* @xmlname: The name of the XML.
* @base_reg: The base number of the register ID.
*/
void gdb_feature_builder_init(GDBFeatureBuilder *builder, GDBFeature *feature,
const char *name, const char *xmlname,
void gdb_feature_builder_init(GDBFeatureBuilder *builder, GDBFeature *feature, const char *name, const char *xmlname,
int base_reg);
/**
@ -80,9 +79,7 @@ void gdb_feature_builder_init(GDBFeatureBuilder *builder, GDBFeature *feature,
* @format: The format of the tag.
* @...: The values to be formatted.
*/
void G_GNUC_PRINTF(2, 3)
gdb_feature_builder_append_tag(const GDBFeatureBuilder *builder,
const char *format, ...);
void G_GNUC_PRINTF(2, 3) gdb_feature_builder_append_tag(const GDBFeatureBuilder *builder, const char *format, ...);
/**
* gdb_feature_builder_append_reg() - Append a register.
@ -93,12 +90,8 @@ gdb_feature_builder_append_tag(const GDBFeatureBuilder *builder,
* @type: The type of the register.
* @group: The register group to which this register belongs; it can be NULL.
*/
void gdb_feature_builder_append_reg(const GDBFeatureBuilder *builder,
const char *name,
int bitsize,
int regnum,
const char *type,
const char *group);
void gdb_feature_builder_append_reg(const GDBFeatureBuilder *builder, const char *name, int bitsize, int regnum,
const char *type, const char *group);
/**
* gdb_feature_builder_end() - End building GDBFeature.

View File

@ -1,8 +1,8 @@
// #include "gdbstub.h"
#include <iostream>
#include "conn.h"
#include "gdbstub.h"
int main(int argc, char *argv[]) {
conn_t conn;

View File

@ -10,29 +10,29 @@
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/cutils.h"
#include "accel/accel-cpu-ops.h"
#include "accel/accel-ops.h"
#include "chardev/char-fe.h"
#include "chardev/char.h"
#include "exec/gdbstub.h"
#include "gdbstub/syscalls.h"
#include "gdbstub/commands.h"
#include "exec/hwaddr.h"
#include "exec/tb-flush.h"
#include "accel/accel-ops.h"
#include "accel/accel-cpu-ops.h"
#include "system/cpus.h"
#include "system/runstate.h"
#include "system/replay.h"
#include "system/tcg.h"
#include "gdbstub/commands.h"
#include "gdbstub/syscalls.h"
#include "hw/boards.h"
#include "hw/core/cpu.h"
#include "hw/cpu/cluster.h"
#include "hw/boards.h"
#include "chardev/char.h"
#include "chardev/char-fe.h"
#include "monitor/monitor.h"
#include "trace.h"
#include "internals.h"
#include "monitor/monitor.h"
#include "qapi/error.h"
#include "qemu/cutils.h"
#include "qemu/error-report.h"
#include "qemu/osdep.h"
#include "system/cpus.h"
#include "system/replay.h"
#include "system/runstate.h"
#include "system/tcg.h"
#include "trace.h"
/* System emulation specific state */
typedef struct {
@ -42,8 +42,7 @@ typedef struct {
GDBSystemState gdbserver_system_state;
static void reset_gdbserver_state(void)
{
static void reset_gdbserver_state(void) {
g_free(gdbserver_state.processes);
gdbserver_state.processes = NULL;
gdbserver_state.process_num = 0;
@ -56,18 +55,12 @@ static void reset_gdbserver_state(void)
* In system mode GDB numbers CPUs from 1 as 0 is reserved as an "any
* cpu" index.
*/
int gdb_get_cpu_index(CPUState *cpu)
{
return cpu->cpu_index + 1;
}
int gdb_get_cpu_index(CPUState *cpu) { return cpu->cpu_index + 1; }
/*
* We check the status of the last message in the chardev receive code
*/
bool gdb_got_immediate_ack(void)
{
return true;
}
bool gdb_got_immediate_ack(void) { return true; }
/*
* GDB Connection management. For system emulation we do all of this
@ -75,8 +68,7 @@ bool gdb_got_immediate_ack(void)
* network and unix sockets.
*/
void gdb_put_buffer(const uint8_t *buf, int len)
{
void gdb_put_buffer(const uint8_t *buf, int len) {
/*
* XXX this blocks entire thread. Rewrite to use
* qemu_chr_fe_write and background I/O callbacks
@ -84,8 +76,7 @@ void gdb_put_buffer(const uint8_t *buf, int len)
qemu_chr_fe_write_all(&gdbserver_system_state.chr, buf, len);
}
static void gdb_chr_event(void *opaque, QEMUChrEvent event)
{
static void gdb_chr_event(void *opaque, QEMUChrEvent event) {
int i;
GDBState *s = (GDBState *)opaque;
@ -115,14 +106,12 @@ static void gdb_chr_event(void *opaque, QEMUChrEvent event)
* state, which can cause packets to be dropped and state transition
* 'T' packets to be sent while the syscall is still being processed.
*/
void gdb_syscall_handling(const char *syscall_packet)
{
void gdb_syscall_handling(const char *syscall_packet) {
vm_stop(RUN_STATE_DEBUG);
qemu_cpu_kick(gdbserver_state.c_cpu);
}
static void gdb_vm_state_change(void *opaque, bool running, RunState state)
{
static void gdb_vm_state_change(void *opaque, bool running, RunState state) {
CPUState *cpu = gdbserver_state.c_cpu;
g_autoptr(GString) buf = g_string_new(NULL);
g_autoptr(GString) tid = g_string_new(NULL);
@ -163,11 +152,8 @@ static void gdb_vm_state_change(void *opaque, bool running, RunState state)
type = "";
break;
}
trace_gdbstub_hit_watchpoint(type,
gdb_get_cpu_index(cpu),
cpu->watchpoint_hit->vaddr);
g_string_printf(buf, "T%02xthread:%s;%swatch:%" VADDR_PRIx ";",
GDB_SIGNAL_TRAP, tid->str, type,
trace_gdbstub_hit_watchpoint(type, gdb_get_cpu_index(cpu), cpu->watchpoint_hit->vaddr);
g_string_printf(buf, "T%02xthread:%s;%swatch:%" VADDR_PRIx ";", GDB_SIGNAL_TRAP, tid->str, type,
cpu->watchpoint_hit->vaddr);
cpu->watchpoint_hit = NULL;
goto send_packet;
@ -221,31 +207,18 @@ send_packet:
cpu_single_step(cpu, 0);
}
#ifndef _WIN32
static void gdb_sigterm_handler(int signal)
{
if (runstate_is_running()) {
vm_stop(RUN_STATE_PAUSED);
}
}
#endif
static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
{
static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len) {
g_autoptr(GString) hex_buf = g_string_new("O");
gdb_memtohex(hex_buf, buf, len);
gdb_put_packet(hex_buf->str);
return len;
}
static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
bool *be_opened, Error **errp)
{
static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend, bool *be_opened, Error **errp) {
*be_opened = false;
}
static void char_gdb_class_init(ObjectClass *oc, const void *data)
{
static void char_gdb_class_init(ObjectClass *oc, const void *data) {
ChardevClass *cc = CHARDEV_CLASS(oc);
cc->internal = true;
@ -261,8 +234,7 @@ static const TypeInfo char_gdb_type_info = {
.class_init = char_gdb_class_init,
};
static int gdb_chr_can_receive(void *opaque)
{
static int gdb_chr_can_receive(void *opaque) {
/*
* We can handle an arbitrarily large amount of data.
* Pick the maximum packet size, which is as good as anything.
@ -270,8 +242,7 @@ static int gdb_chr_can_receive(void *opaque)
return MAX_PACKET_LENGTH;
}
static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
{
static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) {
int i;
for (i = 0; i < size; i++) {
@ -279,8 +250,7 @@ static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
}
}
static int find_cpu_clusters(Object *child, void *opaque)
{
static int find_cpu_clusters(Object *child, void *opaque) {
if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
GDBState *s = (GDBState *)opaque;
CPUClusterState *cluster = CPU_CLUSTER(child);
@ -306,8 +276,7 @@ static int find_cpu_clusters(Object *child, void *opaque)
return object_child_foreach(child, find_cpu_clusters, opaque);
}
static int pid_order(const void *a, const void *b)
{
static int pid_order(const void *a, const void *b) {
GDBProcess *pa = (GDBProcess *)a;
GDBProcess *pb = (GDBProcess *)b;
@ -320,35 +289,32 @@ static int pid_order(const void *a, const void *b)
}
}
static void create_processes(GDBState *s)
{
static void create_processes(GDBState *s) {
object_child_foreach(object_get_root(), find_cpu_clusters, s);
if (gdbserver_state.processes) {
/* Sort by PID */
qsort(gdbserver_state.processes,
gdbserver_state.process_num,
sizeof(gdbserver_state.processes[0]),
pid_order);
qsort(gdbserver_state.processes, gdbserver_state.process_num, sizeof(gdbserver_state.processes[0]), pid_order);
}
gdb_create_default_process(s);
}
bool gdbserver_start(const char *device, Error **errp)
{
bool gdbserver_start(const char *device, Error **errp) {
Chardev *chr = NULL;
Chardev *mon_chr;
g_autoptr(GString) cs = g_string_new(device);
if (!first_cpu) {
error_setg(errp, "gdbstub: meaningless to attach gdb to a "
error_setg(errp,
"gdbstub: meaningless to attach gdb to a "
"machine without any CPU.");
return false;
}
if (!gdb_supports_guest_debug()) {
error_setg(errp, "gdbstub: current accelerator doesn't "
error_setg(errp,
"gdbstub: current accelerator doesn't "
"support guest debugging");
return false;
}
@ -391,8 +357,7 @@ bool gdbserver_start(const char *device, Error **errp)
qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
/* Initialize a monitor terminal for gdb */
mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
NULL, NULL, &error_abort);
mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB, NULL, NULL, &error_abort);
monitor_init_hmp(mon_chr, false, &error_abort);
} else {
qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
@ -404,10 +369,8 @@ bool gdbserver_start(const char *device, Error **errp)
if (chr) {
qemu_chr_fe_init(&gdbserver_system_state.chr, chr, &error_abort);
qemu_chr_fe_set_handlers(&gdbserver_system_state.chr,
gdb_chr_can_receive,
gdb_chr_receive, gdb_chr_event,
NULL, &gdbserver_state, NULL, true);
qemu_chr_fe_set_handlers(&gdbserver_system_state.chr, gdb_chr_can_receive, gdb_chr_receive, gdb_chr_event, NULL,
&gdbserver_state, NULL, true);
}
gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
gdbserver_system_state.mon_chr = mon_chr;
@ -416,16 +379,12 @@ bool gdbserver_start(const char *device, Error **errp)
return true;
}
static void register_types(void)
{
type_register_static(&char_gdb_type_info);
}
static void register_types(void) { type_register_static(&char_gdb_type_info); }
type_init(register_types);
/* Tell the remote gdb that the process has exited. */
void gdb_exit(int code)
{
void gdb_exit(int code) {
char buf[4];
if (!gdbserver_state.init) {
@ -443,20 +402,14 @@ void gdb_exit(int code)
qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
}
void gdb_qemu_exit(int code)
{
qemu_system_shutdown_request_with_code(SHUTDOWN_CAUSE_GUEST_SHUTDOWN,
code);
}
void gdb_qemu_exit(int code) { qemu_system_shutdown_request_with_code(SHUTDOWN_CAUSE_GUEST_SHUTDOWN, code); }
/*
* Memory access
*/
static int phy_memory_mode;
int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
uint8_t *buf, int len, bool is_write)
{
int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr, uint8_t *buf, int len, bool is_write) {
if (phy_memory_mode) {
if (is_write) {
cpu_physical_memory_write(addr, buf, len);
@ -477,30 +430,23 @@ int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
* cpu helpers
*/
unsigned int gdb_get_max_cpus(void)
{
unsigned int gdb_get_max_cpus(void) {
MachineState *ms = MACHINE(qdev_get_machine());
return ms->smp.max_cpus;
}
bool gdb_can_reverse(void)
{
return replay_mode == REPLAY_MODE_PLAY;
}
bool gdb_can_reverse(void) { return replay_mode == REPLAY_MODE_PLAY; }
/*
* Softmmu specific command helpers
*/
void gdb_handle_query_qemu_phy_mem_mode(GArray *params,
void *ctx)
{
void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *ctx) {
g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
gdb_put_strbuf();
}
void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx)
{
void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx) {
if (!params->len) {
gdb_put_packet("E22");
return;
@ -514,8 +460,7 @@ void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx)
gdb_put_packet("OK");
}
void gdb_handle_query_rcmd(GArray *params, void *ctx)
{
void gdb_handle_query_rcmd(GArray *params, void *ctx) {
const guint8 zero = 0;
int len;
@ -534,9 +479,7 @@ void gdb_handle_query_rcmd(GArray *params, void *ctx)
len = len / 2;
gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len);
g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
qemu_chr_be_write(gdbserver_system_state.mon_chr,
gdbserver_state.mem_buf->data,
gdbserver_state.mem_buf->len);
qemu_chr_be_write(gdbserver_system_state.mon_chr, gdbserver_state.mem_buf->data, gdbserver_state.mem_buf->len);
gdb_put_packet("OK");
}
@ -544,13 +487,9 @@ void gdb_handle_query_rcmd(GArray *params, void *ctx)
* Execution state helpers
*/
void gdb_handle_query_attached(GArray *params, void *ctx)
{
gdb_put_packet("1");
}
void gdb_handle_query_attached(GArray *params, void *ctx) { gdb_put_packet("1"); }
void gdb_continue(void)
{
void gdb_continue(void) {
if (!runstate_needs_reset()) {
trace_gdbstub_op_continue();
vm_start();
@ -560,8 +499,7 @@ void gdb_continue(void)
/*
* Resume execution, per CPU actions.
*/
int gdb_continue_partial(char *newstates)
{
int gdb_continue_partial(char *newstates) {
CPUState *cpu;
int res = 0;
int flag = 0;
@ -612,13 +550,9 @@ int gdb_continue_partial(char *newstates)
* signals are not yet supported.
*/
enum {
TARGET_SIGINT = 2,
TARGET_SIGTRAP = 5
};
enum { TARGET_SIGINT = 2, TARGET_SIGTRAP = 5 };
int gdb_signal_to_target(int sig)
{
int gdb_signal_to_target(int sig) {
switch (sig) {
case 2:
return TARGET_SIGINT;
@ -633,8 +567,7 @@ int gdb_signal_to_target(int sig)
* Break/Watch point helpers
*/
bool gdb_supports_guest_debug(void)
{
bool gdb_supports_guest_debug(void) {
const AccelOpsClass *ops = cpus_get_accel();
if (ops->supports_guest_debug) {
return ops->supports_guest_debug();
@ -642,8 +575,7 @@ bool gdb_supports_guest_debug(void)
return false;
}
int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
{
int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len) {
const AccelOpsClass *ops = cpus_get_accel();
if (ops->insert_breakpoint) {
return ops->insert_breakpoint(cs, type, addr, len);
@ -651,8 +583,7 @@ int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
return -ENOSYS;
}
int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
{
int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len) {
const AccelOpsClass *ops = cpus_get_accel();
if (ops->remove_breakpoint) {
return ops->remove_breakpoint(cs, type, addr, len);
@ -660,8 +591,7 @@ int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
return -ENOSYS;
}
void gdb_breakpoint_remove_all(CPUState *cs)
{
void gdb_breakpoint_remove_all(CPUState *cs) {
const AccelOpsClass *ops = cpus_get_accel();
if (ops->remove_all_breakpoints) {
ops->remove_all_breakpoints(cs);