some gdbstub fixes for debug and vcont
v3 cover some gdbstub fixes for debug and vcont
Hi,
This is v3 and hopefully the final version the gdbstub fixes. There is one minor change to patch two where I pulled forward the move of cpu_gdb_index into gdbstub.c from the third patch.
Otherwise it is basically a re-base and a whole host of Reviewed-by tags.
So the final question is who's tree is going to accept these patches?
The gdbstub is currently marked as "Odd fixes" via qemu-devel. If no one wants to take it via their tree and Peter is happy I can roll the pullreq directly.
Alex Bennée (4):
gdbstub: modernise DEBUG_GDB
gdbstub: rename cpu_index -> cpu_gdb_index
qom/cpu: remove host_tid field
gdbstub: don't fail on vCont;C04:0;c packets
gdbstub.c | 117 ++++++++++++++++++++++++------------------------- include/exec/gdbstub.h | 9 ---- include/qom/cpu.h | 2 - linux-user/syscall.c | 1 - 4 files changed, 57 insertions(+), 72 deletions(-)
Signature · 1 line
2.13.0
PATCH v3 1/4 gdbstub: modernise DEBUG_GDB
Convert the a gdb_debug helper which compiles away to nothing when not used but still ensures the format strings are checked. There is some minor code motion for the incorrect checksum message to report it before we attempt to send the reply.
--- gdbstub.c | 77 +++++++++++++++++++++++++++------------------------------------ 1 file changed, 33 insertions(+), 44 deletions(-)
gdbstub.c +33 -44
@@ -271,7 +271,20 @@ static int gdb_signal_to_target (int sig)return -1 ; } -//#define DEBUG_GDB +/* #define DEBUG_GDB */ + +#ifdef DEBUG_GDB +# define DEBUG_GDB_GATE 1 +#else +# define DEBUG_GDB_GATE 0 +#endif + +#define gdb_debug (fmt, ...) do { \ + if (DEBUG_GDB_GATE) { \ + fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \ + } \ +} while (0) + typedef struct GDBRegisterState { int base_reg;
@@ -547,9 +560,7 @@ static int put_packet_binary(GDBState *s, const char *buf, int len)/* return -1 if error, 0 if OK */ static int put_packet (GDBState *s,const char *buf) { -#ifdef DEBUG_GDB -printf ("reply='%s'\n" , buf); -#endif +gdb_debug ("reply='%s'\n" , buf);return put_packet_binary (s, buf,strlen (buf)); }
@@ -955,9 +966,9 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)uint8_t *registers;target_ulong addr, len; -#ifdef DEBUG_GDB -printf ("command='%s'\n" , line_buf); -#endif + +gdb_debug ("command='%s'\n" , line_buf); + p = line_buf; ch = *p++;switch (ch) {
@@ -1518,17 +1529,14 @@ static void gdb_read_byte(GDBState *s, int ch)/* Waiting for a response to the last packet. If we see the start of a new command then abandon the previous response. */ if (ch =='-' ) { -#ifdef DEBUG_GDB -printf ("Got NACK, retransmitting\n" ); -#endif +gdb_debug ("Got NACK, retransmitting\n" );put_buffer (s, (uint8_t *)s->last_packet, s->last_packet_len); + }else if (ch =='+' ) { +gdb_debug ("Got ACK\n" ); + }else { +gdb_debug ("Got '%c' when expecting ACK/NACK\n" , ch); } -#ifdef DEBUG_GDB - else if (ch == '+') - printf("Got ACK\n"); - else - printf("Got '%c' when expecting ACK/NACK\n", ch); -#endif +if (ch =='+' || ch =='$' ) s->last_packet_len =0 ;if (ch !='$' )
@@ -1549,9 +1557,7 @@ static void gdb_read_byte(GDBState *s, int ch) s->line_sum =0 ; s->state =RS_GETLINE ; }else { -#ifdef DEBUG_GDB -printf ("gdbstub received garbage between packets: 0x%x\n" , ch); -#endif +gdb_debug ("received garbage between packets: 0x%x\n" , ch); }break ;case RS_GETLINE :
@@ -1567,9 +1573,7 @@ static void gdb_read_byte(GDBState *s, int ch)/* end of command, start of checksum*/ s->state =RS_CHKSUM1 ; }else if (s->line_buf_index >=sizeof (s->line_buf) -1 ) { -#ifdef DEBUG_GDB -printf ("gdbstub command buffer overrun, dropping command\n" ); -#endif +gdb_debug ("command buffer overrun, dropping command\n" ); s->state =RS_IDLE ; }else {/* unescaped command character */
@@ -1583,9 +1587,7 @@ static void gdb_read_byte(GDBState *s, int ch) s->state =RS_CHKSUM1 ; }else if (s->line_buf_index >=sizeof (s->line_buf) -1 ) {/* command buffer overrun */ -#ifdef DEBUG_GDB -printf ("gdbstub command buffer overrun, dropping command\n" ); -#endif +gdb_debug ("command buffer overrun, dropping command\n" ); s->state =RS_IDLE ; }else {/* parse escaped character and leave escape state */
@@ -1597,25 +1599,18 @@ static void gdb_read_byte(GDBState *s, int ch)case RS_GETLINE_RLE :if (ch <' ' ) {/* invalid RLE count encoding */ -#ifdef DEBUG_GDB -printf ("gdbstub got invalid RLE count: 0x%x\n" , ch); -#endif +gdb_debug ("got invalid RLE count: 0x%x\n" , ch); s->state =RS_GETLINE ; }else {/* decode repeat length */ int repeat = (unsigned char )ch -' ' +3 ;if (s->line_buf_index + repeat >=sizeof (s->line_buf) -1 ) {/* that many repeats would overrun the command buffer */ -#ifdef DEBUG_GDB -printf ("gdbstub command buffer overrun," -" dropping command\n" ); -#endif +gdb_debug ("command buffer overrun, dropping command\n" ); s->state =RS_IDLE ; }else if (s->line_buf_index <1 ) {/* got a repeat but we have nothing to repeat */ -#ifdef DEBUG_GDB -printf ("gdbstub got invalid RLE sequence\n" ); -#endif +gdb_debug ("got invalid RLE sequence\n" ); s->state =RS_GETLINE ; }else {/* repeat the last character */
@@ -1630,9 +1625,7 @@ static void gdb_read_byte(GDBState *s, int ch)case RS_CHKSUM1 :/* get high hex digit of checksum */ if (!isxdigit (ch)) { -#ifdef DEBUG_GDB -printf ("gdbstub got invalid command checksum digit\n" ); -#endif +gdb_debug ("got invalid command checksum digit\n" ); s->state =RS_GETLINE ;break ; }
@@ -1643,21 +1636,17 @@ static void gdb_read_byte(GDBState *s, int ch)case RS_CHKSUM2 :/* get low hex digit of checksum */ if (!isxdigit (ch)) { -#ifdef DEBUG_GDB -printf ("gdbstub got invalid command checksum digit\n" ); -#endif +gdb_debug ("got invalid command checksum digit\n" ); s->state =RS_GETLINE ;break ; } s->line_csum |=fromhex (ch);if (s->line_csum != (s->line_sum &0xff )) { +gdb_debug ("got command packet with incorrect checksum\n" );/* send NAK reply */ reply ='-' ;put_buffer (s, &reply,1 ); -#ifdef DEBUG_GDB -printf ("gdbstub got command packet with incorrect checksum\n" ); -#endif s->state =RS_IDLE ; }else {/* send ACK reply */
Signature · 1 line
2.13.0
PATCH v3 2/4 gdbstub: rename cpu_index -> cpu_gdb_index
In reply to Alex BennéeThis is to make it clear the index is purely a gdbstub function and should not be confused with the value of cpu->cpu_index. At the same time we move the function from the header to gdbstub itself which will help with later changes.
---
v3
- move cpu_gdb_index into gdbstub
--- gdbstub.c | 26 ++++++++++++++++++++------ include/exec/gdbstub.h | 9 --------- 2 files changed, 20 insertions(+), 15 deletions(-)
gdbstub.c +20 -6
@@ -55,6 +55,20 @@ static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,return cpu_memory_rw_debug (cpu, addr, buf, len, is_write); } +/* Return the GDB index for a given vCPU state. +* +* For user mode this is simply the thread id. In system mode GDB +* numbers CPUs from 1 as 0 is reserved as an "any cpu" index. +*/ +static inline int cpu_gdb_index (CPUState *cpu) +{ +#if defined(CONFIG_USER_ONLY ) +return cpu->host_tid; +#else +return cpu->cpu_index +1 ; +#endif +} +enum {GDB_SIGNAL_0 =0 ,GDB_SIGNAL_INT =2 ,
@@ -837,7 +851,7 @@ static CPUState *find_cpu(uint32_t thread_id)CPUState *cpu;CPU_FOREACH (cpu) { -if (cpu_index (cpu) == thread_id) { +if (cpu_gdb_index (cpu) == thread_id) {return cpu; } }
@@ -925,7 +939,7 @@ static int gdb_handle_vcont(GDBState *s, const char *p) idx = tmp;/* 0 means any thread, so we pick the first valid CPU */ if (!idx) { - idx =cpu_index (first_cpu); + idx =cpu_gdb_index (first_cpu); }/*
@@ -975,7 +989,7 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)case '?' :/* TODO: Make this return the correct value for user-mode. */ snprintf (buf,sizeof (buf),"T%02xthread:%02x;" ,GDB_SIGNAL_TRAP , -cpu_index (s->c_cpu)); +cpu_gdb_index (s->c_cpu));put_packet (s, buf);/* Remove all the breakpoints when this query is issued, * because gdb is doing and initial connect and the state
@@ -1243,7 +1257,7 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf) }else if (strcmp (p,"sThreadInfo" ) ==0 ) { report_cpuinfo:if (s->query_cpu) { -snprintf (buf,sizeof (buf),"m%x" ,cpu_index (s->query_cpu)); +snprintf (buf,sizeof (buf),"m%x" ,cpu_gdb_index (s->query_cpu));put_packet (s, buf); s->query_cpu =CPU_NEXT (s->query_cpu); }else
@@ -1400,7 +1414,7 @@ static void gdb_vm_state_change(void *opaque, int running, RunState state) }snprintf (buf,sizeof (buf),"T%02xthread:%02x;%swatch:" TARGET_FMT_lx";" , -GDB_SIGNAL_TRAP ,cpu_index (cpu), type, +GDB_SIGNAL_TRAP ,cpu_gdb_index (cpu), type, (target_ulong )cpu->watchpoint_hit->vaddr); cpu->watchpoint_hit =NULL ; goto send_packet;
@@ -1434,7 +1448,7 @@ static void gdb_vm_state_change(void *opaque, int running, RunState state)break ; }gdb_set_stop_cpu (cpu); -snprintf (buf,sizeof (buf),"T%02xthread:%02x;" , ret,cpu_index (cpu)); +snprintf (buf,sizeof (buf),"T%02xthread:%02x;" , ret,cpu_gdb_index (cpu)); send_packet:put_packet (s, buf);
include/exec/gdbstub.h +0 -9
@@ -58,15 +58,6 @@ void gdb_register_coprocessor(CPUState *cpu, gdb_reg_cb get_reg, gdb_reg_cb set_reg, int num_regs, const char *xml, int g_pos); -static inline int cpu_index (CPUState *cpu) -{ -#if defined(CONFIG_USER_ONLY ) -return cpu->host_tid; -#else -return cpu->cpu_index +1 ; -#endif -} -/* The GDB remote protocol transfers values in target byte order. This means * we can use the raw memory access routines to access the value buffer. * Conveniently, these also handle the case where the buffer is mis-aligned.
Signature · 1 line
2.13.0
PATCH v3 3/4 qom/cpu: remove host_tid field
In reply to Alex BennéeThis was only used by the gdbstub and even then was only being set for subsequent threads. Rather the continue duplicating the number just make the gdbstub get the information from TaskState structure.
Now the tid is correctly reported for all threads the bug I was seeing with "vCont;C04:0;c" packets is fixed as the correct tid is reported to gdb.
I moved cpu_gdb_index into the gdbstub to facilitate easy access to the TaskState which is used elsewhere in gdbstub.
---
v3
- fix merge, did move of function in previous commit
--- gdbstub.c | 3 ++- include/qom/cpu.h | 2 -- linux-user/syscall.c | 1 - 3 files changed, 2 insertions(+), 4 deletions(-)
gdbstub.c +2 -1
@@ -63,7 +63,8 @@ static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,static inline int cpu_gdb_index (CPUState *cpu) {#if defined(CONFIG_USER_ONLY ) -return cpu->host_tid; +TaskState *ts = (TaskState *) cpu->opaque; +return ts->ts_tid;#else return cpu->cpu_index +1 ;#endif
include/qom/cpu.h +0 -2
@@ -266,7 +266,6 @@ struct qemu_work_item;* @nr_cores: Number of cores within this CPU package. * @nr_threads: Number of threads within this CPU. * @numa_node: NUMA node this CPU is belonging to. -* @host_tid: Host thread ID. * @running: #true if CPU is currently running (lockless). * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end; * valid under cpu_list_lock.
@@ -321,7 +320,6 @@ struct CPUState {HANDLE hThread;#endif int thread_id; -uint32_t host_tid;bool running, has_waiter;struct QemuCond *halt_cond;bool thread_kicked;
linux-user/syscall.c +0 -1
@@ -6219,7 +6219,6 @@ static void *clone_func(void *arg) thread_cpu = cpu; ts = (TaskState *)cpu->opaque; info->tid =gettid (); - cpu->host_tid = info->tid;task_settid (ts);if (info->child_tidptr)put_user_u32 (info->tid, info->child_tidptr);
Signature · 1 line
2.13.0
Re: [Qemu-devel] [PATCH v3 3/4] qom/cpu: remove host_tid field
Quoted 10 lines · Alex Bennée
This was only used by the gdbstub and even then was only being set for subsequent threads. Rather the continue duplicating the number just make the gdbstub get the information from TaskState structure.
Now the tid is correctly reported for all threads the bug I was seeing with "vCont;C04:0;c" packets is fixed as the correct tid is reported to gdb.
I moved
cpu_gdb_indexinto the gdbstub to facilitate easy access to the TaskState which is used elsewhere in gdbstub.
I think bsd-user does not have ts_tid?
Thanks,
Paolo
Quoted 62 lines
Signed-off-by Alex Bennée <[email protected]>Reviewed-by Greg Kurz <[email protected]>Reviewed-by Claudio Imbrenda <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Laurent Vivier <[email protected]>---
v3
- fix merge, did move of function in previous commit--- gdbstub.c | 3 ++- include/qom/cpu.h | 2 -- linux-user/syscall.c | 1 - 3 files changed, 2 insertions(+), 4 deletions(-)gdbstub.c +2
-1@@ -63,7 +63,8 @@ static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,static inline int cpu_gdb_index (CPUState *cpu) {#if defined(CONFIG_USER_ONLY ) -return cpu->host_tid; +TaskState *ts = (TaskState *) cpu->opaque; +return ts->ts_tid;#else return cpu->cpu_index +1 ;#endif include/qom/cpu.h +0
-2@@ -266,7 +266,6 @@ struct qemu_work_item;* @nr_cores: Number of cores within this CPU package. * @nr_threads: Number of threads within this CPU. * @numa_node: NUMA node this CPU is belonging to. -* @host_tid: Host thread ID. * @running: #true if CPU is currently running (lockless). * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end; * valid under cpu_list_lock. @@ -321,7 +320,6 @@ struct CPUState {HANDLE hThread;#endif int thread_id; -uint32_t host_tid;bool running, has_waiter;struct QemuCond *halt_cond;bool thread_kicked;linux-user/syscall.c +0
-1@@ -6219,7 +6219,6 @@ static void *clone_func(void *arg) thread_cpu = cpu; ts = (TaskState *)cpu->opaque; info->tid =gettid (); - cpu->host_tid = info->tid;task_settid (ts);if (info->child_tidptr)put_user_u32 (info->tid, info->child_tidptr);
Re: [Qemu-devel] [PATCH v3 3/4] qom/cpu: remove host_tid field
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Alex Bennée
This was only used by the gdbstub and even then was only being set for subsequent threads. Rather the continue duplicating the number just make the gdbstub get the information from TaskState structure.
Quoted 8 lines
Quoted 6 lines
Now the tid is correctly reported for all threads the bug I was seeing with "vCont;C04:0;c" packets is fixed as the correct tid is reported to gdb.
I moved
cpu_gdb_indexinto the gdbstub to facilitate easy access to the TaskState which is used elsewhere in gdbstub.I think bsd-user does not have
ts_tid?
I'll test the build on BSD, I think I have a VM somewhere.
Quoted 59 lines
Thanks,
Paolo
Quoted 54 lines
Signed-off-by Alex Bennée <[email protected]>Reviewed-by Greg Kurz <[email protected]>Reviewed-by Claudio Imbrenda <[email protected]>Reviewed-by Philippe Mathieu-Daudé <[email protected]>Reviewed-by Laurent Vivier <[email protected]>---
v3
- fix merge, did move of function in previous commit--- gdbstub.c | 3 ++- include/qom/cpu.h | 2 -- linux-user/syscall.c | 1 - 3 files changed, 2 insertions(+), 4 deletions(-)gdbstub.c +2
-1@@ -63,7 +63,8 @@ static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,static inline int cpu_gdb_index (CPUState *cpu) {#if defined(CONFIG_USER_ONLY ) -return cpu->host_tid; +TaskState *ts = (TaskState *) cpu->opaque; +return ts->ts_tid;#else return cpu->cpu_index +1 ;#endif include/qom/cpu.h +0
-2@@ -266,7 +266,6 @@ struct qemu_work_item;* @nr_cores: Number of cores within this CPU package. * @nr_threads: Number of threads within this CPU. * @numa_node: NUMA node this CPU is belonging to. -* @host_tid: Host thread ID. * @running: #true if CPU is currently running (lockless). * @has_waiter: #true if a CPU is currently waiting for the cpu_exec_end; * valid under cpu_list_lock. @@ -321,7 +320,6 @@ struct CPUState {HANDLE hThread;#endif int thread_id; -uint32_t host_tid;bool running, has_waiter;struct QemuCond *halt_cond;bool thread_kicked;linux-user/syscall.c +0
-0@@ -6219,7 +6219,6 @@ static void *clone_func(void *arg)
Quoted 8 lines
linux-user/syscall.c static void *clone_func(void *arg) · line 6224
Quoted 8 lines
linux-user/syscall.c +0
-1thread_cpu = cpu; ts = (TaskState *)cpu->opaque; info->tid =gettid (); - cpu->host_tid = info->tid;task_settid (ts);if (info->child_tidptr)put_user_u32 (info->tid, info->child_tidptr);
--
Alex Bennée
PATCH v3 4/4 gdbstub: don't fail on vCont; C04:0; c packets
In reply to Alex BennéeThe thread-id of 0 means any CPU but we then ignore the fact we find the first_cpu in this case who can have an index of 0. Instead of bailing out just test if we have managed to match up thread-id to a CPU.
Otherwise you get:
gdb_handle_packet: command='vCont;C04:0;c'
put_packet: reply='E22'
The actual reason for gdb sending vCont;C04:0;c was fixed in a previous commit where we ensure the first_cpu's tid is correctly reported to gdb however we should still behave correctly next time it does send 0.
---
v2
- used Greg's less convoluted suggestion
- expand commit message
--- gdbstub.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
gdbstub.c +4 -11
@@ -937,23 +937,16 @@ static int gdb_handle_vcont(GDBState *s, const char *p)if (res) { goto out; } - idx = tmp; +/* 0 means any thread, so we pick the first valid CPU */ -if (!idx) { - idx =cpu_gdb_index (first_cpu); - } + cpu = tmp ?find_cpu (tmp) : first_cpu; -/* -* If we are in user mode, the thread specified is actually a -* thread id, and not an index. We need to find the actual -* CPU first, and only then we can use its index. -*/ - cpu =find_cpu (idx);/* invalid CPU/thread specified */ -if (!idx || !cpu) { +if (!cpu) { res = -EINVAL ; goto out; } +/* only use if no previous match occourred */ if (newstates[cpu->cpu_index] ==1 ) { newstates[cpu->cpu_index] = cur_action;
Signature · 1 line
2.13.0
Re: [Qemu-devel] [PATCH v3 4/4] gdbstub: don't fail on vCont; C04:0; c packets
Quoted 9 lines · Alex Bennée
The thread-id of 0 means any CPU but we then ignore the fact we find the
first_cpuin this case who can have an index of 0. Instead of bailing out just test if we have managed to match up thread-id to a CPU.Otherwise you get:
gdb_handle_packet: command='vCont;C04:0;c'
put_packet: reply='E22'
Quoted 8 lines
The actual reason for gdb sending vCont;C04:0;c was fixed in a previous commit where we ensure the first_cpu's tid is correctly reported to gdb however we should still behave correctly next time it does send 0.
Signed-off-by Alex Bennée <[email protected]>Reviewed-by Greg Kurz <[email protected]>Reviewed-by Claudio Imbrenda <[email protected]>
Quoted 42 lines
---
v2
- used Greg's less convoluted suggestion
- expand commit message--- gdbstub.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)gdbstub.c +4
-11@@ -937,23 +937,16 @@ static int gdb_handle_vcont(GDBState *s, const char *p)if (res) { goto out; } - idx = tmp; +/* 0 means any thread, so we pick the first valid CPU */ -if (!idx) { - idx =cpu_gdb_index (first_cpu); - } + cpu = tmp ?find_cpu (tmp) : first_cpu; -/* -* If we are in user mode, the thread specified is actually a -* thread id, and not an index. We need to find the actual -* CPU first, and only then we can use its index. -*/ - cpu =find_cpu (idx);/* invalid CPU/thread specified */ -if (!idx || !cpu) { +if (!cpu) { res = -EINVAL ; goto out; } +/* only use if no previous match occourred */ if (newstates[cpu->cpu_index] ==1 ) { newstates[cpu->cpu_index] = cur_action;
Quoted 6 lines · Alex Bennée
Hi,
This is v3 and hopefully the final version the gdbstub fixes. There is one minor change to patch two where I pulled forward the move of
cpu_gdb_indexintogdbstub.cfrom the third patch.
Quoted 8 lines
Otherwise it is basically a re-base and a whole host of Reviewed-by tags.
So the final question is who's tree is going to accept these patches?
The gdbstub is currently marked as "Odd fixes" via qemu-devel. If no one wants to take it via their tree and Peter is happy I can roll the pullreq directly.
With pleasure (but I reported an issue on patch 3).
Thanks,
Paolo