gdb_packet: Done a spring pass on the types situation and cleaned things up

This commit is contained in:
dragonmux 2022-07-20 01:08:19 +01:00 committed by Piotr Esden-Tempski
parent a83e024eea
commit f448368d39
4 changed files with 45 additions and 50 deletions

View File

@ -130,4 +130,3 @@ int hostio_system(struct target_controller *tc,
gdb_putpacket_f("Fsystem,%08X/%X", cmd, cmd_len); gdb_putpacket_f("Fsystem,%08X/%X", cmd, cmd_len);
return gdb_main_loop(tc, true); return gdb_main_loop(tc, true);
} }

View File

@ -105,15 +105,14 @@ static struct target_controller gdb_controller = {
int gdb_main_loop(struct target_controller *tc, bool in_syscall) int gdb_main_loop(struct target_controller *tc, bool in_syscall)
{ {
int size;
bool single_step = false; bool single_step = false;
/* GDB protocol main loop */ /* GDB protocol main loop */
while(1) { while (1) {
SET_IDLE_STATE(1); SET_IDLE_STATE(1);
size = gdb_getpacket(pbuf, BUF_SIZE); size_t size = gdb_getpacket(pbuf, BUF_SIZE);
SET_IDLE_STATE(0); SET_IDLE_STATE(0);
switch(pbuf[0]) { switch (pbuf[0]) {
/* Implementation of these is mandatory! */ /* Implementation of these is mandatory! */
case 'g': { /* 'g': Read general registers */ case 'g': { /* 'g': Read general registers */
ERROR_IF_NO_TARGET(); ERROR_IF_NO_TARGET();
@ -171,7 +170,7 @@ int gdb_main_loop(struct target_controller *tc, bool in_syscall)
single_step = true; single_step = true;
/* fall through */ /* fall through */
case 'c': /* 'c [addr]': Continue [at addr] */ case 'c': /* 'c [addr]': Continue [at addr] */
if(!cur_target) { if (!cur_target) {
gdb_putpacketz("X1D"); gdb_putpacketz("X1D");
break; break;
} }
@ -194,12 +193,12 @@ int gdb_main_loop(struct target_controller *tc, bool in_syscall)
/* Wait for target halt */ /* Wait for target halt */
while(!(reason = target_halt_poll(cur_target, &watch))) { while(!(reason = target_halt_poll(cur_target, &watch))) {
unsigned char c = gdb_if_getchar_to(0); char c = (char)gdb_if_getchar_to(0);
if((c == '\x03') || (c == '\x04')) { if(c == '\x03' || c == '\x04')
target_halt_request(cur_target); target_halt_request(cur_target);
}
#ifdef ENABLE_RTT #ifdef ENABLE_RTT
if (rtt_enabled) poll_rtt(cur_target); if (rtt_enabled)
poll_rtt(cur_target);
#endif #endif
} }
SET_RUN_STATE(0); SET_RUN_STATE(0);

View File

@ -30,12 +30,11 @@
#include <stdarg.h> #include <stdarg.h>
int gdb_getpacket(char *packet, int size) size_t gdb_getpacket(char *packet, size_t size)
{ {
unsigned char c;
unsigned char csum; unsigned char csum;
char recv_csum[3]; char recv_csum[3];
int i; size_t offset = 0;
while (1) { while (1) {
/* Wait for packet start */ /* Wait for packet start */
@ -44,7 +43,8 @@ int gdb_getpacket(char *packet, int size)
* start ('$') or a BMP remote packet start ('!'). * start ('$') or a BMP remote packet start ('!').
*/ */
do { do {
packet[0] = gdb_if_getchar(); /* Smells like bad code */
packet[0] = (char)gdb_if_getchar();
if (packet[0] == 0x04) if (packet[0] == 0x04)
return 1; return 1;
} while ((packet[0] != '$') && (packet[0] != REMOTE_SOM)); } while ((packet[0] != '$') && (packet[0] != REMOTE_SOM));
@ -52,18 +52,19 @@ int gdb_getpacket(char *packet, int size)
if (packet[0] == REMOTE_SOM) { if (packet[0] == REMOTE_SOM) {
/* This is probably a remote control packet /* This is probably a remote control packet
* - get and handle it */ * - get and handle it */
i = 0; offset = 0;
bool gettingRemotePacket = true; bool gettingRemotePacket = true;
while (gettingRemotePacket) { while (gettingRemotePacket) {
c = gdb_if_getchar(); /* Smells like bad code */
const char c = (char)gdb_if_getchar();
switch (c) { switch (c) {
case REMOTE_SOM: /* Oh dear, packet restarts */ case REMOTE_SOM: /* Oh dear, packet restarts */
i = 0; offset = 0;
break; break;
case REMOTE_EOM: /* Complete packet for processing */ case REMOTE_EOM: /* Complete packet for processing */
packet[i] = 0; packet[offset] = 0;
remotePacketProcess(i, packet); remotePacketProcess(offset, packet);
gettingRemotePacket = false; gettingRemotePacket = false;
break; break;
@ -73,8 +74,8 @@ int gdb_getpacket(char *packet, int size)
break; break;
default: default:
if (i < size) { if (offset < size) {
packet[i++] = c; packet[offset++] = c;
} else { } else {
/* Who knows what is going on...return to normality */ /* Who knows what is going on...return to normality */
gettingRemotePacket = false; gettingRemotePacket = false;
@ -92,30 +93,32 @@ int gdb_getpacket(char *packet, int size)
#endif #endif
} while (packet[0] != '$'); } while (packet[0] != '$');
i = 0; offset = 0;
csum = 0; csum = 0;
char c;
/* Capture packet data into buffer */ /* Capture packet data into buffer */
while ((c = gdb_if_getchar()) != '#') { while ((c = (char)gdb_if_getchar()) != '#') {
if (i == size) /* Oh shit */ /* If we run out of buffer space, exit early */
if (offset == size)
break; break;
if (c == '$') { /* Restart capture */ if (c == '$') { /* Restart capture */
i = 0; offset = 0;
csum = 0; csum = 0;
continue; continue;
} }
if (c == '}') { /* escaped char */ if (c == '}') { /* escaped char */
c = gdb_if_getchar(); c = gdb_if_getchar();
csum += c + '}'; csum += c + '}';
packet[i++] = c ^ 0x20; packet[offset++] = c ^ 0x20;
continue; continue;
} }
csum += c; csum += c;
packet[i++] = c; packet[offset++] = c;
} }
recv_csum[0] = gdb_if_getchar(); recv_csum[0] = (char)gdb_if_getchar();
recv_csum[1] = gdb_if_getchar(); recv_csum[1] = (char)gdb_if_getchar();
recv_csum[2] = 0; recv_csum[2] = 0;
/* return packet if checksum matches */ /* return packet if checksum matches */
@ -126,20 +129,20 @@ int gdb_getpacket(char *packet, int size)
gdb_if_putchar('-', 1); /* send nack */ gdb_if_putchar('-', 1); /* send nack */
} }
gdb_if_putchar('+', 1); /* send ack */ gdb_if_putchar('+', 1); /* send ack */
packet[i] = 0; packet[offset] = 0;
#if PC_HOSTED == 1 #if PC_HOSTED == 1
DEBUG_GDB_WIRE("%s : ", __func__); DEBUG_GDB_WIRE("%s : ", __func__);
for(int j = 0; j < i; j++) { for (size_t j = 0; j < offset; j++) {
c = packet[j]; const char c = packet[j];
if ((c >= 32) && (c < 127)) if (c >= ' ' && c < 0x7F)
DEBUG_GDB_WIRE("%c", c); DEBUG_GDB_WIRE("%c", c);
else else
DEBUG_GDB_WIRE("\\x%02X", c); DEBUG_GDB_WIRE("\\x%02X", c);
} }
DEBUG_GDB_WIRE("\n"); DEBUG_GDB_WIRE("\n");
#endif #endif
return i; return offset;
} }
static void gdb_next_char(char c, unsigned char *csum) static void gdb_next_char(char c, unsigned char *csum)
@ -161,21 +164,19 @@ static void gdb_next_char(char c, unsigned char *csum)
} }
} }
void gdb_putpacket2(const char *packet1, int size1, const char *packet2, int size2) void gdb_putpacket2(const char *packet1, size_t size1, const char *packet2, size_t size2)
{ {
int i;
unsigned char csum;
char xmit_csum[3]; char xmit_csum[3];
int tries = 0; int tries = 0;
do { do {
DEBUG_GDB_WIRE("%s : ", __func__); DEBUG_GDB_WIRE("%s : ", __func__);
csum = 0; unsigned char csum = 0;
gdb_if_putchar('$', 0); gdb_if_putchar('$', 0);
for (i = 0; i < size1; ++i) for (size_t i = 0; i < size1; ++i)
gdb_next_char(packet1[i], &csum); gdb_next_char(packet1[i], &csum);
for (i = 0; i < size2; ++i) for (size_t i = 0; i < size2; ++i)
gdb_next_char(packet2[i], &csum); gdb_next_char(packet2[i], &csum);
gdb_if_putchar('#', 0); gdb_if_putchar('#', 0);
@ -186,18 +187,16 @@ void gdb_putpacket2(const char *packet1, int size1, const char *packet2, int siz
} while ((gdb_if_getchar_to(2000) != '+') && (tries++ < 3)); } while ((gdb_if_getchar_to(2000) != '+') && (tries++ < 3));
} }
void gdb_putpacket(const char *packet, int size) void gdb_putpacket(const char *packet, size_t size)
{ {
int i;
unsigned char csum;
char xmit_csum[3]; char xmit_csum[3];
int tries = 0; int tries = 0;
do { do {
DEBUG_GDB_WIRE("%s : ", __func__); DEBUG_GDB_WIRE("%s : ", __func__);
csum = 0; unsigned char csum = 0;
gdb_if_putchar('$', 0); gdb_if_putchar('$', 0);
for (i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
gdb_next_char(packet[i], &csum); gdb_next_char(packet[i], &csum);
gdb_if_putchar('#', 0); gdb_if_putchar('#', 0);
snprintf(xmit_csum, sizeof(xmit_csum), "%02X", csum); snprintf(xmit_csum, sizeof(xmit_csum), "%02X", csum);

View File

@ -23,9 +23,9 @@
#include <stdarg.h> #include <stdarg.h>
int gdb_getpacket(char *packet, int size); size_t gdb_getpacket(char *packet, size_t size);
void gdb_putpacket(const char *packet, int size); void gdb_putpacket(const char *packet, size_t size);
void gdb_putpacket2(const char *packet1, int size1, const char *packet2, int size2); void gdb_putpacket2(const char *packet1, size_t size1, const char *packet2, size_t size2);
#define gdb_putpacketz(packet) gdb_putpacket((packet), strlen(packet)) #define gdb_putpacketz(packet) gdb_putpacket((packet), strlen(packet))
void gdb_putpacket_f(const char *packet, ...); void gdb_putpacket_f(const char *packet, ...);
@ -34,5 +34,3 @@ void gdb_voutf(const char *fmt, va_list);
void gdb_outf(const char *fmt, ...); void gdb_outf(const char *fmt, ...);
#endif #endif