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.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Greg Kurz <groug@kaod.org>
Message-Id: <20170712105216.747-2-alex.bennee@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Alex Bennée 2017-07-12 11:52:13 +01:00 committed by Paolo Bonzini
parent cb58a6d361
commit 118e226884
1 changed files with 33 additions and 44 deletions

View File

@ -272,7 +272,20 @@ static int gdb_signal_to_target (int sig)
return -1; 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 { typedef struct GDBRegisterState {
int base_reg; int base_reg;
@ -548,9 +561,7 @@ static int put_packet_binary(GDBState *s, const char *buf, int len)
/* return -1 if error, 0 if OK */ /* return -1 if error, 0 if OK */
static int put_packet(GDBState *s, const char *buf) static int put_packet(GDBState *s, const char *buf)
{ {
#ifdef DEBUG_GDB gdb_debug("reply='%s'\n", buf);
printf("reply='%s'\n", buf);
#endif
return put_packet_binary(s, buf, strlen(buf)); return put_packet_binary(s, buf, strlen(buf));
} }
@ -956,9 +967,9 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)
uint8_t *registers; uint8_t *registers;
target_ulong addr, len; target_ulong addr, len;
#ifdef DEBUG_GDB
printf("command='%s'\n", line_buf); gdb_debug("command='%s'\n", line_buf);
#endif
p = line_buf; p = line_buf;
ch = *p++; ch = *p++;
switch(ch) { switch(ch) {
@ -1519,17 +1530,14 @@ static void gdb_read_byte(GDBState *s, int ch)
/* Waiting for a response to the last packet. If we see the start /* Waiting for a response to the last packet. If we see the start
of a new command then abandon the previous response. */ of a new command then abandon the previous response. */
if (ch == '-') { if (ch == '-') {
#ifdef DEBUG_GDB gdb_debug("Got NACK, retransmitting\n");
printf("Got NACK, retransmitting\n");
#endif
put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); 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 == '$') if (ch == '+' || ch == '$')
s->last_packet_len = 0; s->last_packet_len = 0;
if (ch != '$') if (ch != '$')
@ -1550,9 +1558,7 @@ static void gdb_read_byte(GDBState *s, int ch)
s->line_sum = 0; s->line_sum = 0;
s->state = RS_GETLINE; s->state = RS_GETLINE;
} else { } else {
#ifdef DEBUG_GDB gdb_debug("received garbage between packets: 0x%x\n", ch);
printf("gdbstub received garbage between packets: 0x%x\n", ch);
#endif
} }
break; break;
case RS_GETLINE: case RS_GETLINE:
@ -1568,9 +1574,7 @@ static void gdb_read_byte(GDBState *s, int ch)
/* end of command, start of checksum*/ /* end of command, start of checksum*/
s->state = RS_CHKSUM1; s->state = RS_CHKSUM1;
} else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
#ifdef DEBUG_GDB gdb_debug("command buffer overrun, dropping command\n");
printf("gdbstub command buffer overrun, dropping command\n");
#endif
s->state = RS_IDLE; s->state = RS_IDLE;
} else { } else {
/* unescaped command character */ /* unescaped command character */
@ -1584,9 +1588,7 @@ static void gdb_read_byte(GDBState *s, int ch)
s->state = RS_CHKSUM1; s->state = RS_CHKSUM1;
} else if (s->line_buf_index >= sizeof(s->line_buf) - 1) { } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
/* command buffer overrun */ /* command buffer overrun */
#ifdef DEBUG_GDB gdb_debug("command buffer overrun, dropping command\n");
printf("gdbstub command buffer overrun, dropping command\n");
#endif
s->state = RS_IDLE; s->state = RS_IDLE;
} else { } else {
/* parse escaped character and leave escape state */ /* parse escaped character and leave escape state */
@ -1598,25 +1600,18 @@ static void gdb_read_byte(GDBState *s, int ch)
case RS_GETLINE_RLE: case RS_GETLINE_RLE:
if (ch < ' ') { if (ch < ' ') {
/* invalid RLE count encoding */ /* invalid RLE count encoding */
#ifdef DEBUG_GDB gdb_debug("got invalid RLE count: 0x%x\n", ch);
printf("gdbstub got invalid RLE count: 0x%x\n", ch);
#endif
s->state = RS_GETLINE; s->state = RS_GETLINE;
} else { } else {
/* decode repeat length */ /* decode repeat length */
int repeat = (unsigned char)ch - ' ' + 3; int repeat = (unsigned char)ch - ' ' + 3;
if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) { if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
/* that many repeats would overrun the command buffer */ /* that many repeats would overrun the command buffer */
#ifdef DEBUG_GDB gdb_debug("command buffer overrun, dropping command\n");
printf("gdbstub command buffer overrun,"
" dropping command\n");
#endif
s->state = RS_IDLE; s->state = RS_IDLE;
} else if (s->line_buf_index < 1) { } else if (s->line_buf_index < 1) {
/* got a repeat but we have nothing to repeat */ /* got a repeat but we have nothing to repeat */
#ifdef DEBUG_GDB gdb_debug("got invalid RLE sequence\n");
printf("gdbstub got invalid RLE sequence\n");
#endif
s->state = RS_GETLINE; s->state = RS_GETLINE;
} else { } else {
/* repeat the last character */ /* repeat the last character */
@ -1631,9 +1626,7 @@ static void gdb_read_byte(GDBState *s, int ch)
case RS_CHKSUM1: case RS_CHKSUM1:
/* get high hex digit of checksum */ /* get high hex digit of checksum */
if (!isxdigit(ch)) { if (!isxdigit(ch)) {
#ifdef DEBUG_GDB gdb_debug("got invalid command checksum digit\n");
printf("gdbstub got invalid command checksum digit\n");
#endif
s->state = RS_GETLINE; s->state = RS_GETLINE;
break; break;
} }
@ -1644,21 +1637,17 @@ static void gdb_read_byte(GDBState *s, int ch)
case RS_CHKSUM2: case RS_CHKSUM2:
/* get low hex digit of checksum */ /* get low hex digit of checksum */
if (!isxdigit(ch)) { if (!isxdigit(ch)) {
#ifdef DEBUG_GDB gdb_debug("got invalid command checksum digit\n");
printf("gdbstub got invalid command checksum digit\n");
#endif
s->state = RS_GETLINE; s->state = RS_GETLINE;
break; break;
} }
s->line_csum |= fromhex(ch); s->line_csum |= fromhex(ch);
if (s->line_csum != (s->line_sum & 0xff)) { if (s->line_csum != (s->line_sum & 0xff)) {
gdb_debug("got command packet with incorrect checksum\n");
/* send NAK reply */ /* send NAK reply */
reply = '-'; reply = '-';
put_buffer(s, &reply, 1); put_buffer(s, &reply, 1);
#ifdef DEBUG_GDB
printf("gdbstub got command packet with incorrect checksum\n");
#endif
s->state = RS_IDLE; s->state = RS_IDLE;
} else { } else {
/* send ACK reply */ /* send ACK reply */