Compare commits

..

No commits in common. "d79f526ce2581d2d0d9320297b2468c742cc3116" and "05f7a713a37427a57d4dbf3587d58597f805c3f9" have entirely different histories.

10 changed files with 1448 additions and 362 deletions

View File

@ -56,7 +56,6 @@
"typeinfo": "cpp",
"internals.h": "c",
"stdint.h": "c",
"ctype.h": "c",
"gdbstub.h": "c"
"ctype.h": "c"
}
}

View File

@ -101,4 +101,10 @@ void gdb_extend_set_table(GPtrArray *table);
*/
void gdb_extend_qsupported_features(char *qsupported_features);
/**
* Convert a hex string to bytes. Conversion is done per byte, so 2 hex digits
* are converted to 1 byte. Invalid hex digits are treated as 0 digits.
*/
void gdb_hextomem(GByteArray *mem, const char *buf, int len);
#endif /* GDBSTUB_COMMANDS_H */

View File

@ -796,6 +796,17 @@ enum CPUDumpFlags {
*/
CPUState *cpu_create(const char *tname);
/**
* parse_cpu_option:
* @cpu_option: The -cpu option including optional parameters.
*
* processes optional parameters and registers them as global properties
*
* Returns: type of CPU to create or prints error and terminates process
* if an error occurred.
*/
const char *parse_cpu_option(const char *cpu_option);
/**
* qemu_cpu_is_self:
* @cpu: The vCPU to check against.

View File

@ -23,6 +23,27 @@
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
// #include "qemu/osdep.h"
// #include "qemu/ctype.h"
// #include "qemu/cutils.h"
// #include "qemu/module.h"
// #include "qemu/error-report.h"
// #include "qemu/target-info.h"
// #include "trace.h"
// #include "exec/gdbstub.h"
// #include "gdbstub/commands.h"
// #include "gdbstub/syscalls.h"
// #include "hw/cpu/cluster.h"
// #include "hw/boards.h"
// #include "hw/core/cpu.h"
// #include "system/hw_accel.h"
// #include "system/runstate.h"
// #include "exec/replay-core.h"
// #include "exec/hwaddr.h"
#include "gdbstub.h"
#include <iostream>
#include "commands.h"
@ -30,6 +51,15 @@
#include "enums.h"
#include "internals.h"
// #include "trace.h"
typedef struct GDBRegisterState {
int base_reg;
gdb_get_reg_cb get_reg;
gdb_set_reg_cb set_reg;
const GDBFeature *feature;
} GDBRegisterState;
GDBState gdbserver_state;
void gdb_init_gdbserver_state(void) {
@ -71,10 +101,43 @@ void gdb_hextomem(GByteArray *mem, const char *buf, int len) {
}
}
static void hexdump(const char *buf, int len, void (*trace_fn)(size_t ofs, char const *text)) {
char line_buffer[3 * 16 + 4 + 16 + 1];
size_t i;
for (i = 0; i < len || (i & 0xF); ++i) {
size_t byte_ofs = i & 15;
if (byte_ofs == 0) {
memset(line_buffer, ' ', 3 * 16 + 4 + 16);
line_buffer[3 * 16 + 4 + 16] = 0;
}
size_t col_group = (i >> 2) & 3;
size_t hex_col = byte_ofs * 3 + col_group;
size_t txt_col = 3 * 16 + 4 + byte_ofs;
if (i < len) {
char value = buf[i];
line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
line_buffer[txt_col + 0] = (value >= ' ' && value < 127) ? value : '.';
}
// if (byte_ofs == 0xF) trace_fn(i & -16, line_buffer);
}
}
/* return -1 if error, 0 if OK */
int gdb_put_packet_binary(const char *buf, int len, bool dump) {
int csum, i;
uint8_t footer[3];
// if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
// hexdump(buf, len, trace_gdbstub_io_binaryreply);
// }
for (;;) {
g_byte_array_set_size(gdbserver_state.last_packet, 0);
g_byte_array_append(gdbserver_state.last_packet, (const uint8_t *)"$", 1);
@ -97,10 +160,16 @@ int gdb_put_packet_binary(const char *buf, int len, bool dump) {
return 0;
}
int gdb_put_packet(const char *buf) { return gdb_put_packet_binary(buf, strlen(buf), false); }
/* return -1 if error, 0 if OK */
int gdb_put_packet(const char *buf) {
// trace_gdbstub_io_reply(buf);
return gdb_put_packet_binary(buf, strlen(buf), false);
}
void gdb_put_strbuf(void) { gdb_put_packet(gdbserver_state.str_buf->str); }
/* Encode data using the encoding for 'x' packets. */
void gdb_memtox(GString *buf, const char *mem, int len) {
char c;
@ -121,6 +190,207 @@ void gdb_memtox(GString *buf, const char *mem, int len) {
}
}
static uint32_t gdb_get_cpu_pid(CPUState *cpu) {
if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
/* Return the default process' PID */
int index = gdbserver_state.process_num - 1;
return gdbserver_state.processes[index].pid;
}
return cpu->cluster_index + 1;
}
GDBProcess *gdb_get_process(uint32_t pid) {
int i;
if (!pid) {
/* 0 means any process, we take the first one */
return &gdbserver_state.processes[0];
}
for (i = 0; i < gdbserver_state.process_num; i++) {
if (gdbserver_state.processes[i].pid == pid) {
return &gdbserver_state.processes[i];
}
}
return NULL;
}
static GDBProcess *gdb_get_cpu_process(CPUState *cpu) { return gdb_get_process(gdb_get_cpu_pid(cpu)); }
static CPUState *find_cpu(uint32_t thread_id) {
CPUState *cpu;
CPU_FOREACH(cpu) {
if (gdb_get_cpu_index(c) == thread_id) {
return cpu;
}
}
return NULL;
}
CPUState *gdb_get_first_cpu_in_process(GDBProcess *process) {
CPUState *cpu;
CPU_FOREACH(cpu) {
if (gdb_get_cpu_pid(c) == process->pid) {
return c;
}
}
return NULL;
}
static CPUState *gdb_next_cpu_in_process(CPUState *cpu) {
uint32_t pid = gdb_get_cpu_pid(cpu);
cpu = cpu_next(cpu);
while (cpu) {
if (gdb_get_cpu_pid(cpu) == pid) {
break;
}
cpu = cpu_next(cpu);
}
return cpu;
}
/* Return the cpu following @cpu, while ignoring unattached processes. */
static CPUState *gdb_next_attached_cpu(CPUState *cpu) {
cpu = cpu_next(cpu);
while (cpu) {
if (gdb_get_cpu_process(cpu)->attached) {
break;
}
cpu = cpu_next(cpu);
}
return cpu;
}
/* Return the first attached cpu */
CPUState *gdb_first_attached_cpu(void) {
CPUState *cpu = get_cpu();
GDBProcess *process = gdb_get_cpu_process(cpu);
if (!process->attached) {
return gdb_next_attached_cpu(cpu);
}
return cpu;
}
static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid) {
GDBProcess *process;
CPUState *cpu;
if (!pid && !tid) {
/* 0 means any process/thread, we take the first attached one */
return gdb_first_attached_cpu();
} else if (pid && !tid) {
/* any thread in a specific process */
process = gdb_get_process(pid);
if (process == NULL) {
return NULL;
}
if (!process->attached) {
return NULL;
}
return gdb_get_first_cpu_in_process(process);
} else {
/* a specific thread */
cpu = find_cpu(tid);
if (cpu == NULL) {
return NULL;
}
process = gdb_get_cpu_process(cpu);
if (pid && process->pid != pid) {
return NULL;
}
if (!process->attached) {
return NULL;
}
return cpu;
}
}
static const char *get_feature_xml(const char *p, const char **newp, GDBProcess *process) {
CPUState *cpu = gdb_get_first_cpu_in_process(process);
GDBRegisterState *r;
size_t len;
/*
* qXfer:features:read:ANNEX:OFFSET,LENGTH'
* ^p ^newp
*/
const char *term = strchr(p, ':');
*newp = term + 1;
len = term - p;
/* Is it the main target xml? */
if (strncmp(p, "target.xml", len) == 0) {
if (!process->target_xml) {
g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free);
g_ptr_array_add(xml, g_strdup("<?xml version=\"1.0\"?>"
"<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
"<target>"));
if (cpu->cc->gdb_arch_name) {
g_ptr_array_add(xml, g_markup_printf_escaped("<architecture>%s</architecture>", cpu->cc->gdb_arch_name(cpu)));
}
for (guint i = 0; i < cpu->gdb_regs->len; i++) {
r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
g_ptr_array_add(xml, g_markup_printf_escaped("<xi:include href=\"%s\"/>", r->feature->xmlname));
}
g_ptr_array_add(xml, g_strdup("</target>"));
g_ptr_array_add(xml, NULL);
process->target_xml = g_strjoinv(NULL, (gchar **)xml->pdata);
}
return process->target_xml;
}
/* Is it one of the features? */
for (guint i = 0; i < cpu->gdb_regs->len; i++) {
r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
if (strncmp(p, r->feature->xmlname, len) == 0) {
return r->feature->xml;
}
}
/* failed */
return NULL;
}
void gdb_feature_builder_init(GDBFeatureBuilder *builder, GDBFeature *feature, const char *name, const char *xmlname,
int base_reg) {
char *header = g_markup_printf_escaped(
"<?xml version=\"1.0\"?>"
"<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">"
"<feature name=\"%s\">",
name);
builder->feature = feature;
builder->xml = g_ptr_array_new();
g_ptr_array_add(builder->xml, header);
builder->regs = g_ptr_array_new();
builder->base_reg = base_reg;
feature->xmlname = xmlname;
feature->name = name;
}
void gdb_feature_builder_append_tag(const GDBFeatureBuilder *builder, const char *format, ...) {
va_list ap;
va_start(ap, format);
@ -225,8 +495,87 @@ int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) {
return 0;
}
static void gdb_process_breakpoint_remove_all() {
CPUState *cpu = get_cpu();
static void gdb_register_feature(CPUState *cpu, int base_reg, gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
const GDBFeature *feature) {
GDBRegisterState s = {.base_reg = base_reg, .get_reg = get_reg, .set_reg = set_reg, .feature = feature};
g_array_append_val(cpu->gdb_regs, s);
}
static const char *gdb_get_core_xml_file(CPUState *cpu) {
CPUClass *cc = cpu->cc;
/*
* The CPU class can provide the XML filename via a method,
* or as a simple fixed string field.
*/
if (cc->gdb_get_core_xml_file) {
return cc->gdb_get_core_xml_file(cpu);
}
return cc->gdb_core_xml_file;
}
void gdb_init_cpu(CPUState *cpu) {
CPUClass *cc = cpu->cc;
const GDBFeature *feature;
const char *xmlfile = gdb_get_core_xml_file(cpu);
cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState));
if (xmlfile) {
feature = gdb_find_static_feature(xmlfile);
gdb_register_feature(cpu, 0, cc->gdb_read_register, cc->gdb_write_register, feature);
cpu->gdb_num_regs = cpu->gdb_num_g_regs = feature->num_regs;
}
if (cc->gdb_num_core_regs) {
cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;
}
}
void gdb_register_coprocessor(CPUState *cpu, gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, const GDBFeature *feature,
int g_pos) {
GDBRegisterState *s;
guint i;
int base_reg = cpu->gdb_num_regs;
for (i = 0; i < cpu->gdb_regs->len; i++) {
/* Check for duplicates. */
s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
if (s->feature == feature) {
return;
}
}
gdb_register_feature(cpu, base_reg, get_reg, set_reg, feature);
/* Add to end of list. */
cpu->gdb_num_regs += feature->num_regs;
if (g_pos) {
if (g_pos != base_reg) {
std::cout << "Error: Bad gdb register numbering for" << feature->xml << ", xpected " << g_pos << " got "
<< base_reg << std::endl;
} else {
cpu->gdb_num_g_regs = cpu->gdb_num_regs;
}
}
}
void gdb_unregister_coprocessor_all(CPUState *cpu) {
/*
* Safe to nuke everything. GDBRegisterState::xml is static const char so
* it won't be freed
*/
g_array_free(cpu->gdb_regs, true);
cpu->gdb_regs = NULL;
cpu->gdb_num_regs = 0;
cpu->gdb_num_g_regs = 0;
}
static void gdb_process_breakpoint_remove_all(GDBProcess *p) {
CPUState *cpu = gdb_get_first_cpu_in_process(p);
while (cpu) {
gdb_breakpoint_remove_all(cpu);
cpu = gdb_next_cpu_in_process(cpu);
@ -240,7 +589,13 @@ static void gdb_set_cpu_pc(vaddr pc) {
cpu_set_pc(cpu, pc);
}
void gdb_append_thread_id(CPUState *cpu, GString *buf) { g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu)); }
void gdb_append_thread_id(CPUState *cpu, GString *buf) {
if (gdbserver_state.multiprocess) {
g_string_append_printf(buf, "p%02x.%02x", gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu));
} else {
g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu));
}
}
static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, uint32_t *pid, uint32_t *tid) {
unsigned long p, t;
@ -287,6 +642,11 @@ static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, uin
return GDB_ONE_THREAD;
}
/**
* gdb_handle_vcont - Parses and handles a vCont packet.
* returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
* a format error, 0 on success.
*/
static int gdb_handle_vcont(const char *p) {
int res, signal = 0;
char cur_action;
@ -299,7 +659,8 @@ static int gdb_handle_vcont(const char *p) {
/* uninitialised CPUs stay 0 */
g_autofree char *newstates = g_new0(char, max_cpus);
newstates[get_cpu()->cpu_index] = 1;
/* mark valid CPUs with 1 */
CPU_FOREACH(cpu) { newstates[c->cpu_index] = 1; }
/*
* res keeps track of what error we are returning, with -ENOTSUP meaning
@ -368,22 +729,27 @@ static int gdb_handle_vcont(const char *p) {
break;
case GDB_ALL_THREADS:
process = gdb_get_process();
process = gdb_get_process(pid);
if (!process->attached) {
return -EINVAL;
}
cpu = get_cpu();
if (cpu) {
cpu = gdb_get_first_cpu_in_process(process);
while (cpu) {
if (newstates[cpu->cpu_index] == 1) {
newstates[cpu->cpu_index] = cur_action;
target_count++;
last_target = cpu;
}
cpu = gdb_next_cpu_in_process(cpu);
}
break;
case GDB_ONE_THREAD:
cpu = gdb_get_cpu();
cpu = gdb_get_cpu(pid, tid);
/* invalid CPU/thread specified */
if (!cpu) {
@ -551,16 +917,30 @@ static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd) {
static void handle_detach(GArray *params, void *user_ctx) {
GDBProcess *process;
uint32_t pid = 1;
process = gdb_get_process();
gdb_process_breakpoint_remove_all();
if (gdbserver_state.multiprocess) {
if (!params->len) {
gdb_put_packet("E22");
return;
}
pid = gdb_get_cmd_param(params, 0)->val_ul;
}
process = gdb_get_process(pid);
gdb_process_breakpoint_remove_all(process);
process->attached = false;
if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
gdbserver_state.c_cpu = gdb_first_attached_cpu();
}
if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
gdbserver_state.g_cpu = gdb_first_attached_cpu();
}
if (!gdbserver_state.c_cpu) {
/* No more process attached */
gdb_disable_syscalls();
gdb_continue();
}
@ -569,19 +949,23 @@ static void handle_detach(GArray *params, void *user_ctx) {
static void handle_thread_alive(GArray *params, void *user_ctx) {
CPUState *cpu;
if (!params->len) {
gdb_put_packet("E22");
return;
}
if (gdb_get_cmd_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
gdb_put_packet("E22");
return;
}
cpu = gdb_get_cpu();
cpu = gdb_get_cpu(gdb_get_cmd_param(params, 0)->thread_id.pid, gdb_get_cmd_param(params, 0)->thread_id.tid);
if (!cpu) {
gdb_put_packet("E22");
return;
}
gdb_put_packet("OK");
}
@ -589,12 +973,14 @@ static void handle_continue(GArray *params, void *user_ctx) {
if (params->len) {
gdb_set_cpu_pc(gdb_get_cmd_param(params, 0)->val_ull);
}
gdbserver_state.signal = 0;
gdb_continue();
}
static void handle_cont_with_sig(GArray *params, void *user_ctx) {
unsigned long signal = 0;
/*
* Note: C sig;[addr] is currently unsupported and we simply
* omit the addr parameter
@ -602,6 +988,7 @@ static void handle_cont_with_sig(GArray *params, void *user_ctx) {
if (params->len) {
signal = gdb_get_cmd_param(params, 0)->val_ul;
}
gdbserver_state.signal = gdb_signal_to_target(signal);
if (gdbserver_state.signal == -1) {
gdbserver_state.signal = 0;
@ -612,25 +999,31 @@ static void handle_cont_with_sig(GArray *params, void *user_ctx) {
static void handle_set_thread(GArray *params, void *user_ctx) {
uint32_t pid, tid;
CPUState *cpu;
if (params->len != 2) {
gdb_put_packet("E22");
return;
}
if (gdb_get_cmd_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
gdb_put_packet("E22");
return;
}
if (gdb_get_cmd_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
gdb_put_packet("OK");
return;
}
pid = gdb_get_cmd_param(params, 1)->thread_id.pid;
tid = gdb_get_cmd_param(params, 1)->thread_id.tid;
cpu = gdb_get_cpu();
cpu = gdb_get_cpu(pid, tid);
if (!cpu) {
gdb_put_packet("E22");
return;
}
/*
* Note: This command is deprecated and modern gdb's will be using the
* vCont command instead.
@ -692,6 +1085,17 @@ static void handle_remove_bp(GArray *params, void *user_ctx) {
gdb_put_packet("E22");
}
/*
* handle_set/get_reg
*
* Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
* This works, but can be very slow. Anything new enough to understand
* XML also knows how to use this properly. However to use this we
* need to define a local XML file as well as be talking to a
* reasonably modern gdb. Responding with an empty packet will cause
* the remote gdb to fallback to older methods.
*/
static void handle_set_reg(GArray *params, void *user_ctx) {
int reg_size;
@ -814,11 +1218,37 @@ static void handle_step(GArray *params, void *user_ctx) {
if (params->len) {
gdb_set_cpu_pc(gdb_get_cmd_param(params, 0)->val_ull);
}
cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
gdb_continue();
}
static void handle_backward(GArray *params, void *user_ctx) { gdb_put_packet("E22"); }
static void handle_backward(GArray *params, void *user_ctx) {
if (!gdb_can_reverse()) {
gdb_put_packet("E22");
}
if (params->len == 1) {
switch (gdb_get_cmd_param(params, 0)->opcode) {
case 's':
// if (replay_reverse_step()) {
// gdb_continue();
// } else {
gdb_put_packet("E14");
// }
return;
case 'c':
// if (replay_reverse_continue()) {
// gdb_continue();
// } else {
gdb_put_packet("E14");
// }
return;
}
}
/* Default invalid command */
gdb_put_packet("");
}
static void handle_v_cont_query(GArray *params, void *user_ctx) { gdb_put_packet("vCont;c;C;s;S"); }
@ -846,12 +1276,12 @@ static void handle_v_attach(GArray *params, void *user_ctx) {
goto cleanup;
}
process = gdb_get_process();
process = gdb_get_process(gdb_get_cmd_param(params, 0)->val_ul);
if (!process) {
goto cleanup;
}
cpu = get_cpu();
cpu = gdb_get_first_cpu_in_process(process);
if (!cpu) {
goto cleanup;
}
@ -942,8 +1372,8 @@ static void handle_query_curr_tid(GArray *params, void *user_ctx) {
* the first thread of the current process (gdb returns the
* first thread).
*/
process = gdb_get_process();
cpu = get_cpu();
process = gdb_get_cpu_process(gdbserver_state.g_cpu);
cpu = gdb_get_first_cpu_in_process(process);
g_string_assign(gdbserver_state.str_buf, "QC");
gdb_append_thread_id(cpu, gdbserver_state.str_buf);
gdb_put_strbuf();
@ -974,16 +1404,21 @@ static void handle_query_first_threads(GArray *params, void *user_ctx) {
static void handle_query_thread_extra(GArray *params, void *user_ctx) {
g_autoptr(GString) rs = g_string_new(NULL);
CPUState *cpu;
if (!params->len || gdb_get_cmd_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
gdb_put_packet("E22");
return;
}
cpu = gdb_get_cpu();
cpu = gdb_get_cpu(gdb_get_cmd_param(params, 0)->thread_id.pid, gdb_get_cmd_param(params, 0)->thread_id.tid);
if (!cpu) {
return;
}
cpu_synchronize_state(cpu);
g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index, cpu->halted ? "halted " : "running");
// trace_gdbstub_op_extra_info(rs->str);
gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
gdb_put_strbuf();
}
@ -1006,6 +1441,20 @@ static void handle_query_supported(GArray *params, void *user_ctx) {
if (gdb_get_core_xml_file(get_cpu())) {
g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
}
if (gdb_can_reverse()) {
g_string_append(gdbserver_state.str_buf, ";ReverseStep+;ReverseContinue+");
}
if (params->len) {
const char *gdb_supported = gdb_get_cmd_param(params, 0)->data;
if (strstr(gdb_supported, "multiprocess+")) {
gdbserver_state.multiprocess = true;
}
}
g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
if (extra_query_flags) {
int extras = g_strv_length(extra_query_flags);
for (int i = 0; i < extras; i++) {
@ -1027,7 +1476,7 @@ static void handle_query_xfer_features(GArray *params, void *user_ctx) {
return;
}
process = gdb_get_process();
process = gdb_get_cpu_process(gdbserver_state.g_cpu);
if (!gdb_get_core_xml_file(gdbserver_state.g_cpu)) {
gdb_put_packet("");
return;
@ -1082,6 +1531,17 @@ static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
{.handler = handle_set_qemu_sstep, .cmd = "qemu.sstep=", .cmd_startswith = true, .schema = "l0"},
};
/**
* extend_table() - extend one of the command tables
* @table: the command table to extend (or NULL)
* @extensions: a list of GdbCmdParseEntry pointers
*
* The entries themselves should be pointers to static const
* GdbCmdParseEntry entries. If the entry is already in the table we
* skip adding it again.
*
* Returns (a potentially freshly allocated) GPtrArray of GdbCmdParseEntry
*/
static GPtrArray *extend_table(GPtrArray *table, GPtrArray *extensions) {
if (!table) {
table = g_ptr_array_new();
@ -1097,6 +1557,13 @@ static GPtrArray *extend_table(GPtrArray *table, GPtrArray *extensions) {
return table;
}
/**
* process_extended_table() - run through an extended command table
* @table: the command table to check
* @data: parameters
*
* returns true if the command was found and executed
*/
static bool process_extended_table(GPtrArray *table, const char *data) {
for (int i = 0; i < table->len; i++) {
const GdbCmdParseEntry *entry = (GdbCmdParseEntry *)g_ptr_array_index(table, i);
@ -1107,6 +1574,7 @@ static bool process_extended_table(GPtrArray *table, const char *data) {
return false;
}
/* Ptr to GdbCmdParseEntry */
static GPtrArray *extended_query_table;
void gdb_extend_query_table(GPtrArray *new_queries) {
@ -1150,6 +1618,7 @@ static const GdbCmdParseEntry gdb_gen_query_table[] = {
},
};
/* Ptr to GdbCmdParseEntry */
static GPtrArray *extended_set_table;
void gdb_extend_set_table(GPtrArray *new_set) { extended_set_table = extend_table(extended_set_table, new_set); }
@ -1361,6 +1830,21 @@ static RSState gdb_handle_packet(const char *line_buf) {
return RS_IDLE;
}
void gdb_set_stop_cpu(CPUState *cpu) {
GDBProcess *p = gdb_get_cpu_process(cpu);
if (!p->attached) {
/*
* Having a stop CPU corresponding to a process that is not attached
* confuses GDB. So we ignore the request.
*/
return;
}
gdbserver_state.c_cpu = cpu;
gdbserver_state.g_cpu = cpu;
}
void gdb_read_byte(uint8_t ch) {
uint8_t reply;
@ -1517,3 +2001,28 @@ void gdb_read_byte(uint8_t ch) {
}
}
}
/*
* Create the process that will contain all the "orphan" CPUs (that are not
* part of a CPU cluster). Note that if this process contains no CPUs, it won't
* be attachable and thus will be invisible to the user.
*/
void gdb_create_default_process(GDBState *s) {
GDBProcess *process;
int pid;
if (gdbserver_state.process_num) {
pid = s->processes[s->process_num - 1].pid;
} else {
pid = 0;
}
/* We need an available PID slot for this process */
assert(pid < UINT32_MAX);
pid++;
s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
process = &s->processes[s->process_num - 1];
process->pid = pid;
process->attached = false;
process->target_xml = NULL;
}

157
gdbstub/gdbstub.h Normal file
View File

@ -0,0 +1,157 @@
#ifndef GDBSTUB_H
#define GDBSTUB_H
#include "cpu.h"
typedef struct GDBFeature {
const char *xmlname;
const char *xml;
const char *name;
const char *const *regs;
int num_regs;
} GDBFeature;
typedef struct GDBFeatureBuilder {
GDBFeature *feature;
GPtrArray *xml;
GPtrArray *regs;
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);
/**
* gdb_init_cpu(): Initialize the CPU for gdbstub.
* @cpu: The CPU to be initialized.
*/
void gdb_init_cpu(CPUState *cpu);
/**
* gdb_register_coprocessor() - register a supplemental set of registers
* @cpu - the CPU associated with registers
* @get_reg - get function (gdb reading)
* @set_reg - set function (gdb modifying)
* @num_regs - number of registers in set
* @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);
/**
* gdb_unregister_coprocessor_all() - unregisters supplemental set of registers
* @cpu - the CPU associated with registers
*/
void gdb_unregister_coprocessor_all(CPUState *cpu);
/**
* gdbserver_start: start the gdb server
* @port_or_device: connection spec for gdb
* @errp: error handle
*
* For CONFIG_USER this is either a tcp port or a path to a fifo. For
* system emulation you can use a full chardev spec for your gdbserver
* port.
*
* The error handle should be either &error_fatal (for start-up) or
* &error_warn (for QMP/HMP initiated sessions).
*
* Returns true when server successfully started.
*/
bool gdbserver_start(const char *port_or_device, Error **errp);
/**
* gdb_feature_builder_init() - Initialize GDBFeatureBuilder.
* @builder: The builder to be initialized.
* @feature: The feature to be filled.
* @name: The name of the feature.
* @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,
int base_reg);
/**
* gdb_feature_builder_append_tag() - Append a tag.
* @builder: The builder.
* @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, ...);
/**
* gdb_feature_builder_append_reg() - Append a register.
* @builder: The builder.
* @name: The register's name; it must be unique within a CPU.
* @bitsize: The register's size, in bits.
* @regnum: The offset of the register's number in the feature.
* @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);
/**
* gdb_feature_builder_end() - End building GDBFeature.
* @builder: The builder.
*/
void gdb_feature_builder_end(const GDBFeatureBuilder *builder);
/**
* gdb_find_static_feature() - Find a static feature.
* @xmlname: The name of the XML.
*
* Return: The static feature.
*/
const GDBFeature *gdb_find_static_feature(const char *xmlname);
/**
* gdb_read_register() - Read a register associated with a CPU.
* @cpu: The CPU associated with the register.
* @buf: The buffer that the read register will be appended to.
* @reg: The register's number returned by gdb_find_feature_register().
*
* Return: The number of read bytes.
*/
int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
/**
* gdb_write_register() - Write a register associated with a CPU.
* @cpu: The CPU associated with the register.
* @buf: The buffer that the register contents will be set to.
* @reg: The register's number returned by gdb_find_feature_register().
*
* The size of @buf must be at least the size of the register being
* written.
*
* Return: The number of written bytes, or 0 if an error occurred (for
* example, an unknown register was provided).
*/
int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg);
/**
* typedef GDBRegDesc - a register description from gdbstub
*/
typedef struct {
int gdb_reg;
const char *name;
const char *feature_name;
} GDBRegDesc;
/**
* gdb_get_register_list() - Return list of all registers for CPU
* @cpu: The CPU being searched
*
* Returns a GArray of GDBRegDesc, caller frees array but not the
* const strings.
*/
GArray *gdb_get_register_list(CPUState *cpu);
void gdb_set_stop_cpu(CPUState *cpu);
/* in gdbstub-xml.c, generated by scripts/feature_to_c.py */
extern const GDBFeature gdb_static_features[];
#endif /* GDBSTUB_H */

View File

@ -9,10 +9,35 @@
#ifndef GDBSTUB_INTERNALS_H
#define GDBSTUB_INTERNALS_H
// #include "exec/cpu-common.h"
#include "cpu.h"
/*
* Most "large" transfers (e.g. memory reads, feature XML
* transfer) have mechanisms in the gdb protocol for splitting
* them. However, register values in particular cannot currently
* be split. This packet size must therefore be at least big enough
* for the worst-case register size. Currently that is Arm SME
* ZA storage with a 256x256 byte value. We also must account
* for the conversion from raw data to hex in gdb_memtohex(),
* which writes 2 * size bytes, and for other protocol overhead
* including command, register number and checksum which add
* another 4 bytes of overhead. However, to be consistent with
* the changes made in gdbserver to address this same requirement,
* we add a total of 32 bytes to account for protocol overhead
* (unclear why specifically 32 bytes), bringing the value of
* MAX_PACKET_LENGTH to 2 * 256 * 256 + 32 = 131104.
*
* The commit making this change for gdbserver can be found here:
* https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=
* b816042e88583f280ad186ff124ab84d31fb592b
*/
#define MAX_PACKET_LENGTH 131104
/*
* Shared structures and definitions
*/
enum {
GDB_SIGNAL_0 = 0,
GDB_SIGNAL_INT = 2,
@ -42,32 +67,6 @@ enum RSState {
RS_CHKSUM2,
};
typedef struct GDBFeature {
const char *xmlname;
const char *xml;
const char *name;
const char *const *regs;
int num_regs;
} GDBFeature;
typedef struct GDBFeatureBuilder {
GDBFeature *feature;
GPtrArray *xml;
GPtrArray *regs;
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);
typedef struct GDBRegisterState {
int base_reg;
gdb_get_reg_cb get_reg;
gdb_set_reg_cb set_reg;
const GDBFeature *feature;
} GDBRegisterState;
typedef struct GDBState {
bool init; /* have we been initialised? */
CPUState *c_cpu; /* current CPU for step/continue ops */
@ -80,6 +79,7 @@ typedef struct GDBState {
int line_csum; /* checksum at the end of the packet */
GByteArray *last_packet;
int signal;
bool multiprocess;
GDBProcess *processes;
int process_num;
GString *str_buf;
@ -125,77 +125,71 @@ static inline int tohex(int v) {
*/
void gdb_put_strbuf(void);
void gdb_hextomem(GByteArray *mem, const char *buf, int len);
int gdb_put_packet_binary(const char *buf, int len, bool dump);
void gdb_memtohex(GString *buf, const uint8_t *mem, int len);
void gdb_memtox(GString *buf, const char *mem, int len);
void gdb_read_byte(uint8_t ch);
void gdb_init_cpu(CPUState *cpu);
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_unregister_coprocessor_all(CPUState *cpu);
bool gdbserver_start(const char *port_or_device, Error **errp);
/*
* Packet acknowledgement - we handle this slightly differently
* between user and system mode, mainly to deal with the differences
* between the flexible chardev and the direct fd approaches.
*
* We currently don't support a negotiated QStartNoAckMode
*/
/**
* gdb_feature_builder_append_tag() - Append a tag.
* @builder: The builder.
* @format: The format of the tag.
* @...: The values to be formatted.
* gdb_got_immediate_ack() - check ok to continue
*
* Returns true to continue, false to re-transmit for user only, the
* system stub always returns true.
*/
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.
* @builder: The builder.
* @name: The register's name; it must be unique within a CPU.
* @bitsize: The register's size, in bits.
* @regnum: The offset of the register's number in the feature.
* @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_end(const GDBFeatureBuilder *builder);
const GDBFeature *gdb_find_static_feature(const char *xmlname);
int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg);
typedef struct {
int gdb_reg;
const char *name;
const char *feature_name;
} GDBRegDesc;
GArray *gdb_get_register_list(CPUState *cpu);
/* in gdbstub-xml.c, generated by scripts/feature_to_c.py */
extern const GDBFeature gdb_static_features[];
void gdb_chr_receive(const uint8_t *buf, int size);
bool gdb_got_immediate_ack(void);
/* utility helpers */
GDBProcess *gdb_get_process();
GDBProcess *gdb_get_process(uint32_t pid);
CPUState *gdb_get_first_cpu_in_process(GDBProcess *process);
CPUState *gdb_first_attached_cpu(void);
void gdb_append_thread_id(CPUState *cpu, GString *buf);
int gdb_get_cpu_index(CPUState *cpu);
unsigned int gdb_get_max_cpus(void); /* both */
bool gdb_can_reverse(void); /* system emulation, stub for user */
// int gdb_target_sigtrap(void); /* user */
void gdb_create_default_process(GDBState *s);
/* signal mapping, common for system, specialised for user-mode */
int gdb_signal_to_target(int sig);
// int gdb_target_signal_to_gdb(int sig);
int gdb_get_char(void); /* user only */
/**
* gdb_continue() - handle continue in mode specific way.
*/
void gdb_continue(void);
/**
* gdb_continue_partial() - handle partial continue in mode specific way.
*/
int gdb_continue_partial(char *newstates);
/*
* Command handlers - either specialised or system or user only
*/
void gdb_init_gdbserver_state(void);
void gdb_handle_query_rcmd(GArray *params, void *ctx); /* system */
// void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */
// void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx); /*user */
// void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */
// void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */
// void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */
// void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */
// void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */
// void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx); /* user */
// void gdb_handle_query_supported_user(const char *gdb_supported); /* user */
// bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid); /* user */
// bool gdb_handle_detach_user(uint32_t pid); /* user */
void gdb_handle_query_attached(GArray *params, void *ctx); /* both */
@ -207,6 +201,11 @@ void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx);
void gdb_handle_file_io(GArray *params, void *user_ctx);
void gdb_disable_syscalls(void);
/*
* Break/Watch point support - there is an implementation for system
* and user mode.
*/
// TODO
bool runstate_is_running();
void vm_stop(RunState rs);
@ -233,23 +232,20 @@ void vm_start();
bool vm_prepare_start(bool step_requested);
void qemu_clock_enable();
uint32_t gdb_get_cpu_pid(CPUState *cpu);
#define CPU_FOREACH(cpu) for (auto c = get_cpu(); false;)
/**
* gdb_target_memory_rw_debug() - handle debug access to memory
* @cs: CPUState
* @addr: nominal address, could be an entire physical address
* @buf: data
* @len: length of access
* @is_write: is it a write operation
*
* This function is specialised depending on the mode we are running
* in. For system guests we can switch the interpretation of the
* address to a physical address.
*/
int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr, uint8_t *buf, int len, bool is_write);
CPUState *find_cpu(uint32_t thread_id);
CPUState *gdb_next_cpu_in_process(CPUState *cpu);
CPUState *gdb_next_attached_cpu(CPUState *cpu);
CPUState *gdb_first_attached_cpu(void);
CPUState *gdb_get_cpu();
const char *get_feature_xml(const char *p, const char **newp, GDBProcess *process);
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_unregister_coprocessor_all(CPUState *cpu);
const char *gdb_get_core_xml_file(CPUState *cpu);
#endif /* GDBSTUB_INTERNALS_H */

View File

@ -1,4 +1,4 @@
#include "internals.h"
#include "gdbstub.h"
const GDBFeature gdb_static_features[] = {
{
@ -47,12 +47,73 @@ const GDBFeature gdb_static_features[] = {
" <reg name=\"pc\" bitsize=\"64\" type=\"code_ptr\"/>\n"
"</feature>\n",
"org.gnu.gdb.riscv.cpu",
(const char* const[]){
[0] = "zero", [1] = "ra", [2] = "sp", [3] = "gp", [4] = "tp", [5] = "t0", [6] = "t1",
[7] = "t2", [8] = "fp", [9] = "s1", [10] = "a0", [11] = "a1", [12] = "a2", [13] = "a3",
[14] = "a4", [15] = "a5", [16] = "a6", [17] = "a7", [18] = "s2", [19] = "s3", [20] = "s4",
[21] = "s5", [22] = "s6", [23] = "s7", [24] = "s8", [25] = "s9", [26] = "s10", [27] = "s11",
[28] = "t3", [29] = "t4", [30] = "t5", [31] = "t6", [32] = "pc",
(const char * const []) {
[0] =
"zero",
[1] =
"ra",
[2] =
"sp",
[3] =
"gp",
[4] =
"tp",
[5] =
"t0",
[6] =
"t1",
[7] =
"t2",
[8] =
"fp",
[9] =
"s1",
[10] =
"a0",
[11] =
"a1",
[12] =
"a2",
[13] =
"a3",
[14] =
"a4",
[15] =
"a5",
[16] =
"a6",
[17] =
"a7",
[18] =
"s2",
[19] =
"s3",
[20] =
"s4",
[21] =
"s5",
[22] =
"s6",
[23] =
"s7",
[24] =
"s8",
[25] =
"s9",
[26] =
"s10",
[27] =
"s11",
[28] =
"t3",
[29] =
"t4",
[30] =
"t5",
[31] =
"t6",
[32] =
"pc",
},
33,
},
@ -101,12 +162,71 @@ const GDBFeature gdb_static_features[] = {
" <reg name=\"ft11\" bitsize=\"32\" type=\"ieee_single\"/>\n"
"</feature>\n",
"org.gnu.gdb.riscv.fpu",
(const char* const[]){
[0] = "ft0", [1] = "ft1", [2] = "ft2", [3] = "ft3", [4] = "ft4", [5] = "ft5", [6] = "ft6",
[7] = "ft7", [8] = "fs0", [9] = "fs1", [10] = "fa0", [11] = "fa1", [12] = "fa2", [13] = "fa3",
[14] = "fa4", [15] = "fa5", [16] = "fa6", [17] = "fa7", [18] = "fs2", [19] = "fs3", [20] = "fs4",
[21] = "fs5", [22] = "fs6", [23] = "fs7", [24] = "fs8", [25] = "fs9", [26] = "fs10", [27] = "fs11",
[28] = "ft8", [29] = "ft9", [30] = "ft10", [31] = "ft11",
(const char * const []) {
[0] =
"ft0",
[1] =
"ft1",
[2] =
"ft2",
[3] =
"ft3",
[4] =
"ft4",
[5] =
"ft5",
[6] =
"ft6",
[7] =
"ft7",
[8] =
"fs0",
[9] =
"fs1",
[10] =
"fa0",
[11] =
"fa1",
[12] =
"fa2",
[13] =
"fa3",
[14] =
"fa4",
[15] =
"fa5",
[16] =
"fa6",
[17] =
"fa7",
[18] =
"fs2",
[19] =
"fs3",
[20] =
"fs4",
[21] =
"fs5",
[22] =
"fs6",
[23] =
"fs7",
[24] =
"fs8",
[25] =
"fs9",
[26] =
"fs10",
[27] =
"fs11",
[28] =
"ft8",
[29] =
"ft9",
[30] =
"ft10",
[31] =
"ft11",
},
32,
},
@ -161,12 +281,71 @@ const GDBFeature gdb_static_features[] = {
" <reg name=\"ft11\" bitsize=\"64\" type=\"riscv_double\"/>\n"
"</feature>\n",
"org.gnu.gdb.riscv.fpu",
(const char* const[]){
[0] = "ft0", [1] = "ft1", [2] = "ft2", [3] = "ft3", [4] = "ft4", [5] = "ft5", [6] = "ft6",
[7] = "ft7", [8] = "fs0", [9] = "fs1", [10] = "fa0", [11] = "fa1", [12] = "fa2", [13] = "fa3",
[14] = "fa4", [15] = "fa5", [16] = "fa6", [17] = "fa7", [18] = "fs2", [19] = "fs3", [20] = "fs4",
[21] = "fs5", [22] = "fs6", [23] = "fs7", [24] = "fs8", [25] = "fs9", [26] = "fs10", [27] = "fs11",
[28] = "ft8", [29] = "ft9", [30] = "ft10", [31] = "ft11",
(const char * const []) {
[0] =
"ft0",
[1] =
"ft1",
[2] =
"ft2",
[3] =
"ft3",
[4] =
"ft4",
[5] =
"ft5",
[6] =
"ft6",
[7] =
"ft7",
[8] =
"fs0",
[9] =
"fs1",
[10] =
"fa0",
[11] =
"fa1",
[12] =
"fa2",
[13] =
"fa3",
[14] =
"fa4",
[15] =
"fa5",
[16] =
"fa6",
[17] =
"fa7",
[18] =
"fs2",
[19] =
"fs3",
[20] =
"fs4",
[21] =
"fs5",
[22] =
"fs6",
[23] =
"fs7",
[24] =
"fs8",
[25] =
"fs9",
[26] =
"fs10",
[27] =
"fs11",
[28] =
"ft8",
[29] =
"ft9",
[30] =
"ft10",
[31] =
"ft11",
},
32,
},
@ -184,8 +363,9 @@ const GDBFeature gdb_static_features[] = {
" <reg name=\"priv\" bitsize=\"64\"/>\n"
"</feature>\n",
"org.gnu.gdb.riscv.virtual",
(const char* const[]){
[0] = "priv",
(const char * const []) {
[0] =
"priv",
},
1,
},
@ -235,12 +415,73 @@ const GDBFeature gdb_static_features[] = {
" <reg name=\"pc\" bitsize=\"32\" type=\"code_ptr\"/>\n"
"</feature>\n",
"org.gnu.gdb.riscv.cpu",
(const char* const[]){
[0] = "zero", [1] = "ra", [2] = "sp", [3] = "gp", [4] = "tp", [5] = "t0", [6] = "t1",
[7] = "t2", [8] = "fp", [9] = "s1", [10] = "a0", [11] = "a1", [12] = "a2", [13] = "a3",
[14] = "a4", [15] = "a5", [16] = "a6", [17] = "a7", [18] = "s2", [19] = "s3", [20] = "s4",
[21] = "s5", [22] = "s6", [23] = "s7", [24] = "s8", [25] = "s9", [26] = "s10", [27] = "s11",
[28] = "t3", [29] = "t4", [30] = "t5", [31] = "t6", [32] = "pc",
(const char * const []) {
[0] =
"zero",
[1] =
"ra",
[2] =
"sp",
[3] =
"gp",
[4] =
"tp",
[5] =
"t0",
[6] =
"t1",
[7] =
"t2",
[8] =
"fp",
[9] =
"s1",
[10] =
"a0",
[11] =
"a1",
[12] =
"a2",
[13] =
"a3",
[14] =
"a4",
[15] =
"a5",
[16] =
"a6",
[17] =
"a7",
[18] =
"s2",
[19] =
"s3",
[20] =
"s4",
[21] =
"s5",
[22] =
"s6",
[23] =
"s7",
[24] =
"s8",
[25] =
"s9",
[26] =
"s10",
[27] =
"s11",
[28] =
"t3",
[29] =
"t4",
[30] =
"t5",
[31] =
"t6",
[32] =
"pc",
},
33,
},
@ -258,9 +499,11 @@ const GDBFeature gdb_static_features[] = {
" <reg name=\"priv\" bitsize=\"32\"/>\n"
"</feature>\n",
"org.gnu.gdb.riscv.virtual",
(const char* const[]){
[0] = "priv",
(const char * const []) {
[0] =
"priv",
},
1,
},
{NULL}};
{ NULL }
};

View File

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

View File

@ -10,8 +10,6 @@
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <iostream>
#include "commands.h"
#include "cpu.h"
#include "enums.h"
@ -24,16 +22,348 @@ static void reset_gdbserver_state(void) {
gdbserver_state.allow_stop_reply = false;
}
/*
* Return the GDB index for a given vCPU state.
*
* 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 0; }
/*
* We check the status of the last message in the chardev receive code
*/
bool gdb_got_immediate_ack(void) { return true; }
void gdb_chr_receive(const uint8_t *buf, int size) {
for (int i = 0; i < size; i++) {
/*
* GDB Connection management. For system emulation we do all of this
* via our existing Chardev infrastructure which allows us to support
* network and unix sockets.
*/
// static void gdb_chr_event(void *opaque, QEMUChrEvent event) {
// int i;
// GDBState *s = (GDBState *)opaque;
// switch (event) {
// case CHR_EVENT_OPENED:
// /* Start with first process attached, others detached */
// for (i = 0; i < s->process_num; i++) {
// s->processes[i].attached = !i;
// }
// s->c_cpu = gdb_first_attached_cpu();
// s->g_cpu = s->c_cpu;
// vm_stop(RUN_STATE_PAUSED);
// replay_gdb_attached();
// break;
// default:
// break;
// }
// }
// /*
// * In system-mode we stop the VM and wait to send the syscall packet
// * until notification that the CPU has stopped. This must be done
// * because if the packet is sent now the reply from the syscall
// * request could be received while the CPU is still in the running
// * 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) {
// vm_stop(RUN_STATE_DEBUG);
// qemu_cpu_kick(gdbserver_state.c_cpu);
// }
// 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);
// const char *type;
// int ret;
// if (running || gdbserver_state.state == RS_INACTIVE) {
// return;
// }
// /* Is there a GDB syscall waiting to be sent? */
// if (gdb_handled_syscall()) {
// return;
// }
// if (cpu == NULL) {
// /* No process attached */
// return;
// }
// if (!gdbserver_state.allow_stop_reply) {
// return;
// }
// gdb_append_thread_id(cpu, tid);
// switch (state) {
// case RUN_STATE_DEBUG:
// if (cpu->watchpoint_hit) {
// switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
// case BP_MEM_READ:
// type = "r";
// break;
// case BP_MEM_ACCESS:
// type = "a";
// break;
// default:
// 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,
// cpu->watchpoint_hit->vaddr);
// cpu->watchpoint_hit = NULL;
// goto send_packet;
// } else {
// trace_gdbstub_hit_break();
// }
// if (tcg_enabled()) {
// tb_flush(cpu);
// }
// ret = GDB_SIGNAL_TRAP;
// break;
// case RUN_STATE_PAUSED:
// trace_gdbstub_hit_paused();
// ret = GDB_SIGNAL_INT;
// break;
// case RUN_STATE_SHUTDOWN:
// trace_gdbstub_hit_shutdown();
// ret = GDB_SIGNAL_QUIT;
// break;
// case RUN_STATE_IO_ERROR:
// trace_gdbstub_hit_io_error();
// ret = GDB_SIGNAL_STOP;
// break;
// case RUN_STATE_WATCHDOG:
// trace_gdbstub_hit_watchdog();
// ret = GDB_SIGNAL_ALRM;
// break;
// case RUN_STATE_INTERNAL_ERROR:
// trace_gdbstub_hit_internal_error();
// ret = GDB_SIGNAL_ABRT;
// break;
// case RUN_STATE_SAVE_VM:
// case RUN_STATE_RESTORE_VM:
// return;
// case RUN_STATE_FINISH_MIGRATE:
// ret = GDB_SIGNAL_XCPU;
// break;
// default:
// trace_gdbstub_hit_unknown(state);
// ret = GDB_SIGNAL_UNKNOWN;
// break;
// }
// gdb_set_stop_cpu(cpu);
// g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
// send_packet:
// gdb_put_packet(buf->str);
// gdbserver_state.allow_stop_reply = false;
// /* disable single step if it was enabled */
// cpu_single_step(cpu, 0);
// }
// 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) {
// *be_opened = false;
// }
// static void char_gdb_class_init(ObjectClass *oc, const void *data) {
// ChardevClass *cc = CHARDEV_CLASS(oc);
// cc->internal = true;
// cc->open = gdb_monitor_open;
// cc->chr_write = gdb_monitor_write;
// }
// #define TYPE_CHARDEV_GDB "chardev-gdb"
// static const TypeInfo char_gdb_type_info = {
// .name = TYPE_CHARDEV_GDB,
// .parent = TYPE_CHARDEV,
// .class_init = char_gdb_class_init,
// };
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.
*/
return MAX_PACKET_LENGTH;
}
static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) {
int i;
for (i = 0; i < size; i++) {
gdb_read_byte(buf[i]);
}
}
// 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);
// GDBProcess *process;
// s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
// process = &s->processes[s->process_num - 1];
// /*
// * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
// * runtime, we enforce here that the machine does not use a cluster ID
// * that would lead to PID 0.
// */
// assert(cluster->cluster_id != UINT32_MAX);
// process->pid = cluster->cluster_id + 1;
// process->attached = false;
// process->target_xml = NULL;
// return 0;
// }
// return object_child_foreach(child, find_cpu_clusters, opaque);
// }
static int pid_order(const void *a, const void *b) {
GDBProcess *pa = (GDBProcess *)a;
GDBProcess *pb = (GDBProcess *)b;
if (pa->pid < pb->pid) {
return -1;
} else if (pa->pid > pb->pid) {
return 1;
} else {
return 0;
}
}
// 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);
// }
// gdb_create_default_process(s);
// }
// bool gdbserver_start(const char *device, Error **errp) {
// Chardev *chr = NULL;
// Chardev *mon_chr;
// g_autoptr(GString) cs = g_string_new(device);
// if (!get_cpu()) {
// 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 "
// "support guest debugging");
// return false;
// }
// if (cs->len == 0) {
// error_setg(errp, "gdbstub: missing connection string");
// return false;
// }
// trace_gdbstub_op_start(cs->str);
// if (g_strcmp0(cs->str, "none") != 0) {
// if (g_str_has_prefix(cs->str, "tcp:")) {
// /* enforce required TCP attributes */
// g_string_append_printf(cs, ",wait=off,nodelay=on,server=on");
// }
// /*
// * FIXME: it's a bit weird to allow using a mux chardev here
// * and implicitly setup a monitor. We may want to break this.
// */
// chr = qemu_chr_new_noreplay("gdb", cs->str, true, NULL);
// if (!chr) {
// error_setg(errp, "gdbstub: couldn't create chardev");
// return false;
// }
// }
// if (!gdbserver_state.init) {
// gdb_init_gdbserver_state();
// 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);
// monitor_init_hmp(mon_chr, false, &error_abort);
// } else {
// qemu_chr_fe_deinit(&gdbserver_system_state.chr, true);
// mon_chr = gdbserver_system_state.mon_chr;
// reset_gdbserver_state();
// }
// create_processes(&gdbserver_state);
// 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);
// }
// gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
// gdbserver_system_state.mon_chr = mon_chr;
// gdb_syscall_reset();
// return true;
// }
// 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) {
// char buf[4];
// if (!gdbserver_state.init) {
// return;
// }
// trace_gdbstub_op_exiting((uint8_t)code);
// if (gdbserver_state.allow_stop_reply) {
// snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
// gdb_put_packet(buf);
// gdbserver_state.allow_stop_reply = false;
// }
// 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); }
/*
* Memory access
*/
int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr, uint8_t *buf, int len, bool is_write) {
if (is_write) {
cpu_physical_memory_write(addr, buf, len);
@ -43,8 +373,18 @@ int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr, uint8_t *buf, int len
return 0;
}
/*
* cpu helpers
*/
unsigned int gdb_get_max_cpus(void) { return 1; }
bool gdb_can_reverse(void) { return false; }
/*
* Softmmu specific command helpers
*/
void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *ctx) {
g_string_printf(gdbserver_state.str_buf, "%d", 1);
gdb_put_strbuf();
@ -84,14 +424,22 @@ void gdb_handle_query_rcmd(GArray *params, void *ctx) {
gdb_put_packet("OK");
}
/*
* Execution state helpers
*/
void gdb_handle_query_attached(GArray *params, void *ctx) { gdb_put_packet("1"); }
void gdb_continue(void) {
if (!runstate_needs_reset()) {
// trace_gdbstub_op_continue();
vm_start();
}
}
/*
* Resume execution, per CPU actions.
*/
int gdb_continue_partial(char *newstates) {
CPUState *cpu;
int res = 0;
@ -99,33 +447,37 @@ int gdb_continue_partial(char *newstates) {
if (!runstate_needs_reset()) {
bool step_requested = false;
if (newstates[get_cpu()->cpu_index] == 's') {
step_requested = true;
CPU_FOREACH(cpu) {
if (newstates[c->cpu_index] == 's') {
step_requested = true;
break;
}
}
if (vm_prepare_start(step_requested)) {
return 0;
}
auto c = get_cpu();
switch (newstates[c->cpu_index]) {
case 0:
case 1:
break; /* nothing to do here */
case 's':
// trace_gdbstub_op_stepping(c->cpu_index);
cpu_single_step(c, gdbserver_state.sstep_flags);
cpu_resume(c);
flag = 1;
break;
case 'c':
// trace_gdbstub_op_continue_cpu(c->cpu_index);
cpu_resume(c);
flag = 1;
break;
default:
res = -1;
break;
CPU_FOREACH(cpu) {
switch (newstates[c->cpu_index]) {
case 0:
case 1:
break; /* nothing to do here */
case 's':
// trace_gdbstub_op_stepping(c->cpu_index);
cpu_single_step(c, gdbserver_state.sstep_flags);
cpu_resume(c);
flag = 1;
break;
case 'c':
// trace_gdbstub_op_continue_cpu(c->cpu_index);
cpu_resume(c);
flag = 1;
break;
default:
res = -1;
break;
}
}
}
if (flag) {
@ -134,6 +486,11 @@ int gdb_continue_partial(char *newstates) {
return res;
}
/*
* Signal Handling - in system mode we only need SIGINT and SIGTRAP; other
* signals are not yet supported.
*/
enum { TARGET_SIGINT = 2, TARGET_SIGTRAP = 5 };
int gdb_signal_to_target(int sig) {
@ -180,197 +537,4 @@ void qemu_clock_enable() {}
void gdb_handle_file_io(GArray *params, void *user_ctx) {}
void gdb_disable_syscalls(void) {}
void cpu_single_step(CPUState *cpu, int enabled) {}
uint32_t gdb_get_cpu_pid(CPUState *cpu) {
if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
/* Return the default process' PID */
int index = gdbserver_state.process_num - 1;
return gdbserver_state.processes[index].pid;
}
return cpu->cluster_index + 1;
}
CPUState *find_cpu(uint32_t thread_id) {
auto cpu = get_cpu();
if (gdb_get_cpu_index(cpu) == thread_id) {
return cpu;
}
return NULL;
}
CPUState *gdb_next_cpu_in_process(CPUState *cpu) {
uint32_t pid = gdb_get_cpu_pid(cpu);
cpu = cpu_next(cpu);
while (cpu) {
if (gdb_get_cpu_pid(cpu) == pid) {
break;
}
cpu = cpu_next(cpu);
}
return cpu;
}
/* Return the cpu following @cpu, while ignoring unattached processes. */
CPUState *gdb_next_attached_cpu(CPUState *cpu) {
cpu = cpu_next(cpu);
while (cpu) {
if (gdb_get_process()->attached) {
break;
}
cpu = cpu_next(cpu);
}
return cpu;
}
/* Return the first attached cpu */
CPUState *gdb_first_attached_cpu(void) {
CPUState *cpu = get_cpu();
GDBProcess *process = gdb_get_process();
if (!process->attached) {
return gdb_next_attached_cpu(cpu);
}
return cpu;
}
CPUState *gdb_get_cpu() { return gdb_first_attached_cpu(); }
const char *get_feature_xml(const char *p, const char **newp, GDBProcess *process) {
CPUState *cpu = get_cpu();
GDBRegisterState *r;
size_t len;
const char *term = strchr(p, ':');
*newp = term + 1;
len = term - p;
/* Is it the main target xml? */
if (strncmp(p, "target.xml", len) == 0) {
if (!process->target_xml) {
g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free);
g_ptr_array_add(xml, g_strdup("<?xml version=\"1.0\"?>"
"<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
"<target>"));
if (cpu->cc->gdb_arch_name) {
g_ptr_array_add(xml, g_markup_printf_escaped("<architecture>%s</architecture>", cpu->cc->gdb_arch_name(cpu)));
}
for (guint i = 0; i < cpu->gdb_regs->len; i++) {
r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
g_ptr_array_add(xml, g_markup_printf_escaped("<xi:include href=\"%s\"/>", r->feature->xmlname));
}
g_ptr_array_add(xml, g_strdup("</target>"));
g_ptr_array_add(xml, NULL);
process->target_xml = g_strjoinv(NULL, (gchar **)xml->pdata);
}
return process->target_xml;
}
/* Is it one of the features? */
for (guint i = 0; i < cpu->gdb_regs->len; i++) {
r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
if (strncmp(p, r->feature->xmlname, len) == 0) {
return r->feature->xml;
}
}
return NULL;
}
GDBProcess *gdb_get_process() { return gdbserver_state.processes; }
void gdb_create_default_process(GDBState *s) {
GDBProcess *process;
s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
process = &s->processes[s->process_num - 1];
process->pid = 0;
process->attached = false;
process->target_xml = NULL;
}
static void gdb_register_feature(CPUState *cpu, int base_reg, gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
const GDBFeature *feature) {
GDBRegisterState s = {.base_reg = base_reg, .get_reg = get_reg, .set_reg = set_reg, .feature = feature};
g_array_append_val(cpu->gdb_regs, s);
}
void gdb_init_cpu(CPUState *cpu) {
CPUClass *cc = cpu->cc;
const GDBFeature *feature;
const char *xmlfile = gdb_get_core_xml_file(cpu);
cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState));
if (xmlfile) {
feature = gdb_find_static_feature(xmlfile);
gdb_register_feature(cpu, 0, cc->gdb_read_register, cc->gdb_write_register, feature);
cpu->gdb_num_regs = cpu->gdb_num_g_regs = feature->num_regs;
}
if (cc->gdb_num_core_regs) {
cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs;
}
}
void gdb_register_coprocessor(CPUState *cpu, gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, const GDBFeature *feature,
int g_pos) {
GDBRegisterState *s;
guint i;
int base_reg = cpu->gdb_num_regs;
for (i = 0; i < cpu->gdb_regs->len; i++) {
/* Check for duplicates. */
s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
if (s->feature == feature) {
return;
}
}
gdb_register_feature(cpu, base_reg, get_reg, set_reg, feature);
/* Add to end of list. */
cpu->gdb_num_regs += feature->num_regs;
if (g_pos) {
if (g_pos != base_reg) {
std::cout << "Error: Bad gdb register numbering for" << feature->xml << ", xpected " << g_pos << " got "
<< base_reg << std::endl;
} else {
cpu->gdb_num_g_regs = cpu->gdb_num_regs;
}
}
}
void gdb_unregister_coprocessor_all(CPUState *cpu) {
/*
* Safe to nuke everything. GDBRegisterState::xml is static const char so
* it won't be freed
*/
g_array_free(cpu->gdb_regs, true);
cpu->gdb_regs = NULL;
cpu->gdb_num_regs = 0;
cpu->gdb_num_g_regs = 0;
}
const char *gdb_get_core_xml_file(CPUState *cpu) {
CPUClass *cc = cpu->cc;
/*
* The CPU class can provide the XML filename via a method,
* or as a simple fixed string field.
*/
if (cc->gdb_get_core_xml_file) {
return cc->gdb_get_core_xml_file(cpu);
}
return cc->gdb_core_xml_file;
}
void cpu_resume(CPUState *cpu) {
cpu->stop = false;
cpu->stopped = false;
// qemu_cpu_kick(cpu);
}
void cpu_single_step(CPUState *cpu, int enabled) {}

1
gdbstub/trace.h Normal file
View File

@ -0,0 +1 @@
#include "trace/trace-gdbstub.h"