* Copyright (C) 2005 by Dominic Rath *
* Dominic.Rath@gmx.de *
* *
- * Copyright (C) 2007-2009 Øyvind Harboe *
+ * Copyright (C) 2007-2010 Øyvind Harboe *
* oyvind.harboe@zylin.com *
* *
* Copyright (C) 2008 by Spencer Oliver *
bool sync; /* set flag to true if you want the next stepi to return immediately.
allowing GDB to pick up a fresh set of register values from the target
without modifying the target state. */
-
+ /* We delay reporting memory write errors until next step/continue or memory
+ * write. This improves performance of gdb load significantly as the GDB packet
+ * can be replied immediately and a new GDB packet will be ready without delay
+ * (ca. 10% or so...).
+ */
+ bool mem_write_error;
};
static int gdb_breakpoint_override;
static enum breakpoint_type gdb_breakpoint_override_type;
-extern int gdb_error(struct connection *connection, int retval);
+static int gdb_error(struct connection *connection, int retval);
static unsigned short gdb_port = 3333;
static unsigned short gdb_port_next = 0;
static const char DIGITS[16] = "0123456789abcdef";
/* set if we are sending a memory map to gdb
* via qXfer:memory-map:read packet */
/* enabled by default*/
-int gdb_use_memory_map = 1;
+static int gdb_use_memory_map = 1;
/* enabled by default*/
-int gdb_flash_program = 1;
+static int gdb_flash_program = 1;
/* if set, data aborts cause an error to be reported in memory read packets
- * see the code in gdb_read_memory_packet() for further explanations */
-int gdb_report_data_abort = 0;
+ * see the code in gdb_read_memory_packet() for further explanations.
+ * Disabled by default.
+ */
+static int gdb_report_data_abort;
-int gdb_last_signal(struct target *target)
+static int gdb_last_signal(struct target *target)
{
switch (target->debug_reason)
{
}
}
-int check_pending(struct connection *connection, int timeout_s, int *got_data)
+static int check_pending(struct connection *connection,
+ int timeout_s, int *got_data)
{
/* a non-blocking socket will block if there is 0 bytes available on the socket,
* but return with as many bytes as are available immediately
struct gdb_connection *gdb_con = connection->priv;
int retval = ERROR_OK;
+#ifdef _DEBUG_GDB_IO_
+ char *debug_buffer;
+#endif
for (;;)
{
if (connection->service->type == CONNECTION_PIPE)
}
-int gdb_get_char(struct connection *connection, int* next_char)
+static int gdb_get_char(struct connection *connection, int* next_char)
{
struct gdb_connection *gdb_con = connection->priv;
return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
}
-int gdb_putback_char(struct connection *connection, int last_char)
+static int gdb_putback_char(struct connection *connection, int last_char)
{
struct gdb_connection *gdb_con = connection->priv;
/* The only way we can detect that the socket is closed is the first time
* we write to it, we will fail. Subsequent write operations will
* succeed. Shudder! */
-int gdb_write(struct connection *connection, void *data, int len)
+static int gdb_write(struct connection *connection, void *data, int len)
{
struct gdb_connection *gdb_con = connection->priv;
if (gdb_con->closed)
return ERROR_SERVER_REMOTE_CLOSED;
}
-int gdb_put_packet_inner(struct connection *connection, char *buffer, int len)
+static int gdb_put_packet_inner(struct connection *connection,
+ char *buffer, int len)
{
int i;
unsigned char my_checksum = 0;
int gotdata;
for (;;)
{
- if ((retval = check_pending(connection, 0, &gotdata)) != ERROR_OK)
+ retval = check_pending(connection, 0, &gotdata);
+ if (retval != ERROR_OK)
return retval;
if (!gotdata)
break;
return ERROR_OK;
}
-int gdb_put_packet(struct connection *connection, char *buffer, int len)
+static int gdb_put_packet(struct connection *connection, char *buffer, int len)
{
struct gdb_connection *gdb_con = connection->priv;
gdb_con->busy = 1;
return ERROR_OK;
}
-int gdb_get_packet_inner(struct connection *connection, char *buffer, int *len)
+static int gdb_get_packet_inner(struct connection *connection,
+ char *buffer, int *len)
{
int character;
int retval;
return ERROR_OK;
}
-int gdb_get_packet(struct connection *connection, char *buffer, int *len)
+static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
{
struct gdb_connection *gdb_con = connection->priv;
gdb_con->busy = 1;
return retval;
}
-int gdb_output_con(struct connection *connection, const char* line)
+static int gdb_output_con(struct connection *connection, const char* line)
{
char *hex_buffer;
int i, bin_size;
return retval;
}
-int gdb_output(struct command_context *context, const char* line)
+static int gdb_output(struct command_context *context, const char* line)
{
/* this will be dumped to the log and also sent as an O packet if possible */
LOG_USER_N("%s", line);
}
}
-int gdb_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
+static int gdb_target_callback_event_handler(struct target *target,
+ enum target_event event, void *priv)
{
int retval;
struct connection *connection = priv;
return ERROR_OK;
}
-int gdb_new_connection(struct connection *connection)
+static int gdb_new_connection(struct connection *connection)
{
struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
struct gdb_service *gdb_service = connection->service->priv;
gdb_connection->busy = 0;
gdb_connection->noack_mode = 0;
gdb_connection->sync = true;
+ gdb_connection->mem_write_error = false;
/* send ACK to GDB for debug request */
gdb_write(connection, "+", 1);
gdb_putback_char(connection, initial_ack);
target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
+ if (gdb_use_memory_map)
+ {
+ /* Connect must fail if the memory map can't be set up correctly.
+ *
+ * This will cause an auto_probe to be invoked, which is either
+ * a no-op or it will fail when the target isn't ready(e.g. not halted).
+ */
+ int i;
+ for (i = 0; i < flash_get_bank_count(); i++)
+ {
+ struct flash_bank *p;
+ retval = get_flash_bank_by_num(i, &p);
+ if (retval != ERROR_OK)
+ {
+ LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect.");
+ return retval;
+ }
+ }
+ }
+
gdb_actual_connections++;
LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
gdb_actual_connections,
return ERROR_OK;
}
-int gdb_connection_closed(struct connection *connection)
+static int gdb_connection_closed(struct connection *connection)
{
struct gdb_service *gdb_service = connection->service->priv;
struct gdb_connection *gdb_connection = connection->priv;
return ERROR_OK;
}
-void gdb_send_error(struct connection *connection, uint8_t the_error)
+static void gdb_send_error(struct connection *connection, uint8_t the_error)
{
char err[4];
snprintf(err, 4, "E%2.2X", the_error);
gdb_put_packet(connection, err, 3);
}
-int gdb_last_signal_packet(struct connection *connection, struct target *target, char* packet, int packet_size)
+static int gdb_last_signal_packet(struct connection *connection,
+ struct target *target, char* packet, int packet_size)
{
char sig_reply[4];
int signal;
* The format of reg->value is little endian
*
*/
-void gdb_str_to_target(struct target *target, char *tstr, struct reg *reg)
+static void gdb_str_to_target(struct target *target,
+ char *tstr, struct reg *reg)
{
int i;
}
/* copy over in register buffer */
-void gdb_target_to_reg(struct target *target, char *tstr, int str_len, uint8_t *bin)
+static void gdb_target_to_reg(struct target *target,
+ char *tstr, int str_len, uint8_t *bin)
{
if (str_len % 2)
{
}
}
-int gdb_get_registers_packet(struct connection *connection, struct target *target, char* packet, int packet_size)
+static int gdb_get_registers_packet(struct connection *connection,
+ struct target *target, char* packet, int packet_size)
{
struct reg **reg_list;
int reg_list_size;
return ERROR_OK;
}
-int gdb_set_registers_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_set_registers_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
int i;
struct reg **reg_list;
return ERROR_OK;
}
-int gdb_get_register_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_get_register_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
char *reg_packet;
int reg_num = strtoul(packet + 1, NULL, 16);
return ERROR_OK;
}
-int gdb_set_register_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_set_register_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
char *separator;
uint8_t *bin_buf;
return ERROR_OK;
}
-int gdb_error(struct connection *connection, int retval)
+/* No attempt is made to translate the "retval" to
+ * GDB speak. This has to be done at the calling
+ * site as no mapping really exists.
+ */
+static int gdb_error(struct connection *connection, int retval)
{
- switch (retval)
- {
- case ERROR_TARGET_DATA_ABORT:
- gdb_send_error(connection, EIO);
- break;
- case ERROR_TARGET_TRANSLATION_FAULT:
- gdb_send_error(connection, EFAULT);
- break;
- case ERROR_TARGET_UNALIGNED_ACCESS:
- gdb_send_error(connection, EFAULT);
- break;
- case ERROR_TARGET_NOT_HALTED:
- gdb_send_error(connection, EFAULT);
- break;
- default:
- /* This could be that the target reset itself. */
- LOG_ERROR("unexpected error %i", retval);
- gdb_send_error(connection, EFAULT);
- break;
- }
-
+ LOG_DEBUG("Reporting %i to GDB as generic error", retval);
+ gdb_send_error(connection, EFAULT);
return ERROR_OK;
}
*
* 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
*/
-int gdb_read_memory_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_read_memory_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
char *separator;
uint32_t addr = 0;
return retval;
}
-int gdb_write_memory_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_write_memory_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
char *separator;
uint32_t addr = 0;
return retval;
}
-int gdb_write_memory_binary_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_write_memory_binary_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
char *separator;
uint32_t addr = 0;
uint32_t len = 0;
- int retval;
+ int retval = ERROR_OK;
/* skip command character */
packet++;
return ERROR_SERVER_REMOTE_CLOSED;
}
- retval = ERROR_OK;
- if (len)
- {
- LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
+ struct gdb_connection *gdb_connection = connection->priv;
- retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
+ if (gdb_connection->mem_write_error)
+ {
+ retval = ERROR_FAIL;
+ /* now that we have reported the memory write error, we can clear the condition */
+ gdb_connection->mem_write_error = false;
}
+ /* By replying the packet *immediately* GDB will send us a new packet
+ * while we write the last one to the target.
+ */
if (retval == ERROR_OK)
{
gdb_put_packet(connection, "OK", 2);
return retval;
}
+ if (len)
+ {
+ LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
+
+ retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
+ if (retval != ERROR_OK)
+ {
+ gdb_connection->mem_write_error = true;
+ }
+ }
+
return ERROR_OK;
}
-int gdb_step_continue_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_step_continue_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
int current = 0;
uint32_t address = 0x0;
return retval;
}
-int gdb_breakpoint_watchpoint_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
int type;
enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
- enum watchpoint_rw wp_type;
+ enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
uint32_t address;
uint32_t size;
char *separator;
wp_type = WPT_READ;
else if (type == 4) /* access watchpoint */
wp_type = WPT_ACCESS;
+ else
+ {
+ LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
+ return ERROR_SERVER_REMOTE_CLOSED;
+ }
+
if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT)||(bp_type == BKPT_HARD)))
{
{
if (packet[0] == 'Z')
{
- if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
+ if ((retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu)) != ERROR_OK)
{
if ((retval = gdb_error(connection, retval)) != ERROR_OK)
return retval;
return ERROR_OK;
}
-/* print out a string and allocate more space as needed, mainly used for XML at this point */
-void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
+/* print out a string and allocate more space as needed,
+ * mainly used for XML at this point
+ */
+static void xml_printf(int *retval, char **xml, int *pos, int *size,
+ const char *fmt, ...)
{
if (*retval != ERROR_OK)
{
return 0;
}
-int gdb_calc_blocksize(struct flash_bank *bank)
-{
- uint32_t i;
- uint32_t block_size = 0xffffffff;
-
- /* loop through all sectors and return smallest sector size */
-
- for (i = 0; i < (uint32_t)bank->num_sectors; i++)
- {
- if (bank->sectors[i].size < block_size)
- block_size = bank->sectors[i].size;
- }
-
- return block_size;
-}
-
static int compare_bank (const void * a, const void * b)
{
struct flash_bank *b1, *b2;
}
}
-int gdb_query_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_memory_map(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
+{
+ /* We get away with only specifying flash here. Regions that are not
+ * specified are treated as if we provided no memory map(if not we
+ * could detect the holes and mark them as RAM).
+ * Normally we only execute this code once, but no big deal if we
+ * have to regenerate it a couple of times.
+ */
+
+ struct flash_bank *p;
+ char *xml = NULL;
+ int size = 0;
+ int pos = 0;
+ int retval = ERROR_OK;
+ struct flash_bank **banks;
+ int offset;
+ int length;
+ char *separator;
+ uint32_t ram_start = 0;
+ int i;
+
+ /* skip command character */
+ packet += 23;
+
+ offset = strtoul(packet, &separator, 16);
+ length = strtoul(separator + 1, &separator, 16);
+
+ xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
+
+ /* Sort banks in ascending order. We need to report non-flash
+ * memory as ram (or rather read/write) by default for GDB, since
+ * it has no concept of non-cacheable read/write memory (i/o etc).
+ *
+ * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
+ * Current versions of GDB assume unlisted addresses are RAM...
+ */
+ banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
+
+ for (i = 0; i < flash_get_bank_count(); i++) {
+ retval = get_flash_bank_by_num(i, &p);
+ if (retval != ERROR_OK)
+ {
+ free(banks);
+ gdb_error(connection, retval);
+ return retval;
+ }
+ banks[i] = p;
+ }
+
+ qsort(banks, flash_get_bank_count(), sizeof(struct flash_bank *),
+ compare_bank);
+
+ for (i = 0; i < flash_get_bank_count(); i++) {
+ int j;
+ unsigned sector_size = 0;
+ uint32_t start, end;
+
+ p = banks[i];
+ start = p->base;
+ end = p->base + p->size;
+
+ if (ram_start < p->base)
+ xml_printf(&retval, &xml, &pos, &size,
+ "<memory type=\"ram\" start=\"0x%x\" "
+ "length=\"0x%x\"/>\n",
+ ram_start, p->base - ram_start);
+
+ /* Report adjacent groups of same-size sectors. So for
+ * example top boot CFI flash will list an initial region
+ * with several large sectors (maybe 128KB) and several
+ * smaller ones at the end (maybe 32KB). STR7 will have
+ * regions with 8KB, 32KB, and 64KB sectors; etc.
+ */
+ for (j = 0; j < p->num_sectors; j++) {
+ unsigned group_len;
+
+ /* Maybe start a new group of sectors. */
+ if (sector_size == 0) {
+ start = p->base + p->sectors[j].offset;
+ xml_printf(&retval, &xml, &pos, &size,
+ "<memory type=\"flash\" "
+ "start=\"0x%x\" ",
+ start);
+ sector_size = p->sectors[j].size;
+ }
+
+ /* Does this finish a group of sectors?
+ * If not, continue an already-started group.
+ */
+ if (j == p->num_sectors -1)
+ group_len = (p->base + p->size) - start;
+ else if (p->sectors[j + 1].size != sector_size)
+ group_len = p->base + p->sectors[j + 1].offset
+ - start;
+ else
+ continue;
+
+ xml_printf(&retval, &xml, &pos, &size,
+ "length=\"0x%x\">\n"
+ "<property name=\"blocksize\">"
+ "0x%x</property>\n"
+ "</memory>\n",
+ group_len,
+ sector_size);
+ sector_size = 0;
+ }
+
+ ram_start = p->base + p->size;
+ }
+
+ if (ram_start != 0)
+ xml_printf(&retval, &xml, &pos, &size,
+ "<memory type=\"ram\" start=\"0x%x\" "
+ "length=\"0x%x\"/>\n",
+ ram_start, 0-ram_start);
+ /* ELSE a flash chip could be at the very end of the 32 bit address
+ * space, in which case ram_start will be precisely 0
+ */
+
+ free(banks);
+ banks = NULL;
+
+ xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
+
+ if (retval != ERROR_OK) {
+ gdb_error(connection, retval);
+ return retval;
+ }
+
+ if (offset + length > pos)
+ length = pos - offset;
+
+ char *t = malloc(length + 1);
+ t[0] = 'l';
+ memcpy(t + 1, xml + offset, length);
+ gdb_put_packet(connection, t, length + 1);
+
+ free(t);
+ free(xml);
+ return ERROR_OK;
+}
+
+static int gdb_query_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
struct command_context *cmd_ctx = connection->cmd_ctx;
struct gdb_connection *gdb_connection = connection->priv;
return ERROR_OK;
}
- else if (strstr(packet, "qXfer:memory-map:read::") && (flash_get_bank_count() > 0))
- {
- /* We get away with only specifying flash here. Regions that are not
- * specified are treated as if we provided no memory map(if not we
- * could detect the holes and mark them as RAM).
- * Normally we only execute this code once, but no big deal if we
- * have to regenerate it a couple of times. */
-
- struct flash_bank *p;
- char *xml = NULL;
- int size = 0;
- int pos = 0;
- int retval = ERROR_OK;
-
- int offset;
- int length;
- char *separator;
- int blocksize;
-
- /* skip command character */
- packet += 23;
-
- offset = strtoul(packet, &separator, 16);
- length = strtoul(separator + 1, &separator, 16);
-
- xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
-
- /*
- sort banks in ascending order, we need to make non-flash memory be ram(or rather
- read/write) by default for GDB.
- GDB does not have a concept of non-cacheable read/write memory.
- */
- struct flash_bank **banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
- int i;
-
- for (i = 0; i < flash_get_bank_count(); i++)
- {
- p = get_flash_bank_by_num(i);
- if (p == NULL)
- {
- free(banks);
- retval = ERROR_FAIL;
- gdb_send_error(connection, retval);
- return retval;
- }
- banks[i]=p;
- }
-
- qsort(banks, flash_get_bank_count(), sizeof(struct flash_bank *), compare_bank);
-
- uint32_t ram_start = 0;
- for (i = 0; i < flash_get_bank_count(); i++)
- {
- p = banks[i];
-
- if (ram_start < p->base)
- {
- xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
- ram_start, p->base-ram_start);
- }
-
- /* if device has uneven sector sizes, eg. str7, lpc
- * we pass the smallest sector size to gdb memory map */
- blocksize = gdb_calc_blocksize(p);
-
- xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
- "<property name=\"blocksize\">0x%x</property>\n" \
- "</memory>\n", \
- p->base, p->size, blocksize);
- ram_start = p->base + p->size;
- }
- if (ram_start != 0)
- {
- xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
- ram_start, 0-ram_start);
- } else
- {
- /* a flash chip could be at the very end of the 32 bit address space, in which case
- ram_start will be precisely 0 */
- }
-
- free(banks);
- banks = NULL;
-
- xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
-
- if (retval != ERROR_OK)
- {
- gdb_send_error(connection, retval);
- return retval;
- }
-
- if (offset + length > pos)
- {
- length = pos - offset;
- }
-
- char *t = malloc(length + 1);
- t[0] = 'l';
- memcpy(t + 1, xml + offset, length);
- gdb_put_packet(connection, t, length + 1);
-
- free(t);
- free(xml);
- return ERROR_OK;
- }
+ else if (strstr(packet, "qXfer:memory-map:read::")
+ && (flash_get_bank_count() > 0))
+ return gdb_memory_map(connection, target, packet, packet_size);
else if (strstr(packet, "qXfer:features:read:"))
{
char *xml = NULL;
if (retval != ERROR_OK)
{
- gdb_send_error(connection, retval);
+ gdb_error(connection, retval);
return retval;
}
return ERROR_OK;
}
-int gdb_v_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
+static int gdb_v_packet(struct connection *connection,
+ struct target *target, char *packet, int packet_size)
{
struct gdb_connection *gdb_connection = connection->priv;
struct gdb_service *gdb_service = connection->service->priv;
flash_set_dirty();
/* perform any target specific operations before the erase */
- target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
- result = flash_erase_address_range(gdb_service->target, addr, length);
- target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
+ target_call_event_callbacks(gdb_service->target,
+ TARGET_EVENT_GDB_FLASH_ERASE_START);
+
+ /* vFlashErase:addr,length messages require region start and
+ * end to be "block" aligned ... if padding is ever needed,
+ * GDB will have become dangerously confused.
+ */
+ result = flash_erase_address_range(gdb_service->target,
+ false, addr, length);
+
+ /* perform any target specific operations after the erase */
+ target_call_event_callbacks(gdb_service->target,
+ TARGET_EVENT_GDB_FLASH_ERASE_END);
/* perform erase */
if (result != ERROR_OK)
return ERROR_OK;
}
-int gdb_detach(struct connection *connection, struct target *target)
+static int gdb_detach(struct connection *connection, struct target *target)
{
struct gdb_service *gdb_service = connection->service->priv;
- target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
+ target_call_event_callbacks(gdb_service->target,
+ TARGET_EVENT_GDB_DETACH);
return gdb_put_packet(connection, "OK", 2);
}
gdb_output_con(connection, string);
}
-/* Do not allocate this on the stack */
-char gdb_packet_buffer[GDB_BUFFER_SIZE];
-
static void gdb_sig_halted(struct connection *connection)
{
char sig_reply[4];
}
-int gdb_input_inner(struct connection *connection)
+static int gdb_input_inner(struct connection *connection)
{
+ /* Do not allocate this on the stack */
+ static char gdb_packet_buffer[GDB_BUFFER_SIZE];
+
struct gdb_service *gdb_service = connection->service->priv;
struct target *target = gdb_service->target;
char *packet = gdb_packet_buffer;
struct gdb_connection *gdb_con = connection->priv;
static int extended_protocol = 0;
- /* drain input buffer */
+ /* drain input buffer. If one of the packets fail, then an error
+ * packet is replied, if applicable.
+ *
+ * This loop will terminate and the error code is returned.
+ *
+ * The calling fn will check if this error is something that
+ * can be recovered from, or if the connection must be closed.
+ *
+ * If the error is recoverable, this fn is called again to
+ * drain the rest of the buffer.
+ */
do
{
packet_size = GDB_BUFFER_SIZE-1;
- if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
- {
+ retval = gdb_get_packet(connection, packet, &packet_size);
+ if (retval != ERROR_OK)
return retval;
- }
/* terminate with zero */
packet[packet_size] = 0;
break;
case 'q':
case 'Q':
- retval = gdb_query_packet(connection, target, packet, packet_size);
+ retval = gdb_query_packet(connection,
+ target, packet,
+ packet_size);
break;
case 'g':
- retval = gdb_get_registers_packet(connection, target, packet, packet_size);
+ retval = gdb_get_registers_packet(
+ connection, target,
+ packet, packet_size);
break;
case 'G':
- retval = gdb_set_registers_packet(connection, target, packet, packet_size);
+ retval = gdb_set_registers_packet(
+ connection, target,
+ packet, packet_size);
break;
case 'p':
- retval = gdb_get_register_packet(connection, target, packet, packet_size);
+ retval = gdb_get_register_packet(
+ connection, target,
+ packet, packet_size);
break;
case 'P':
- retval = gdb_set_register_packet(connection, target, packet, packet_size);
+ retval = gdb_set_register_packet(
+ connection, target,
+ packet, packet_size);
break;
case 'm':
- retval = gdb_read_memory_packet(connection, target, packet, packet_size);
+ retval = gdb_read_memory_packet(
+ connection, target,
+ packet, packet_size);
break;
case 'M':
- retval = gdb_write_memory_packet(connection, target, packet, packet_size);
+ retval = gdb_write_memory_packet(
+ connection, target,
+ packet, packet_size);
break;
case 'z':
case 'Z':
retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
break;
case '?':
- gdb_last_signal_packet(connection, target, packet, packet_size);
+ gdb_last_signal_packet(
+ connection, target,
+ packet, packet_size);
break;
case 'c':
case 's':
{
- int retval = ERROR_OK;
-
- struct gdb_connection *gdb_con = connection->priv;
log_add_callback(gdb_log_callback, connection);
+ if (gdb_con->mem_write_error)
+ {
+ LOG_ERROR("Memory write failure!");
+
+ /* now that we have reported the memory write error, we can clear the condition */
+ gdb_con->mem_write_error = false;
+ }
+
bool nostep = false;
+ bool already_running = false;
if (target->state == TARGET_RUNNING)
{
- LOG_WARNING("The target is already running. Halt target before stepi/continue.");
- retval = target_halt(target);
- if (retval == ERROR_OK)
- retval = target_wait_state(target, TARGET_HALTED, 100);
+ LOG_WARNING("WARNING! The target is already running. "
+ "All changes GDB did to registers will be discarded! "
+ "Waiting for target to halt.");
+ already_running = true;
} else if (target->state != TARGET_HALTED)
{
LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
}
gdb_con->sync = false;
- if ((retval!=ERROR_OK) || nostep)
+ if (!already_running && nostep)
{
/* Either the target isn't in the halted state, then we can't
* step/continue. This might be early setup, etc.
*/
gdb_con->frontend_state = TARGET_RUNNING;
target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
- int retval = gdb_step_continue_packet(connection, target, packet, packet_size);
- if (retval != ERROR_OK)
+
+ if (!already_running)
{
- /* we'll never receive a halted condition... issue a false one.. */
- gdb_frontend_halted(target, connection);
+ /* Here we don't want packet processing to stop even if this fails,
+ * so we use a local variable instead of retval. */
+ retval = gdb_step_continue_packet(connection, target, packet, packet_size);
+ if (retval != ERROR_OK)
+ {
+ /* we'll never receive a halted condition... issue a false one.. */
+ gdb_frontend_halted(target, connection);
+ }
}
}
}
break;
case 'v':
- retval = gdb_v_packet(connection, target, packet, packet_size);
+ retval = gdb_v_packet(
+ connection, target,
+ packet, packet_size);
break;
case 'D':
retval = gdb_detach(connection, target);
extended_protocol = 0;
break;
case 'X':
- if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
+ retval = gdb_write_memory_binary_packet(
+ connection, target,
+ packet, packet_size);
+ if (retval != ERROR_OK)
return retval;
break;
case 'k':
return ERROR_OK;
}
-int gdb_input(struct connection *connection)
+static int gdb_input(struct connection *connection)
{
int retval = gdb_input_inner(connection);
struct gdb_connection *gdb_con = connection->priv;
return ERROR_OK;
}
-/* FIXME static */
-int gdb_target_add_one(struct target *target)
+static int gdb_target_add_one(struct target *target)
{
if (gdb_port == 0 && server_use_pipes == 0)
{
COMMAND_HANDLER(handle_gdb_memory_map_command)
{
- if (CMD_ARGC == 1)
- COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
+ if (CMD_ARGC != 1)
+ return ERROR_COMMAND_SYNTAX_ERROR;
- return ERROR_COMMAND_SYNTAX_ERROR;
+ COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
+ return ERROR_OK;
}
COMMAND_HANDLER(handle_gdb_flash_program_command)
{
- if (CMD_ARGC == 1)
- COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
+ if (CMD_ARGC != 1)
+ return ERROR_COMMAND_SYNTAX_ERROR;
- return ERROR_COMMAND_SYNTAX_ERROR;
+ COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
+ return ERROR_OK;
}
COMMAND_HANDLER(handle_gdb_report_data_abort_command)
{
- if (CMD_ARGC == 1)
- COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
+ if (CMD_ARGC != 1)
+ return ERROR_COMMAND_SYNTAX_ERROR;
- return ERROR_COMMAND_SYNTAX_ERROR;
+ COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
+ return ERROR_OK;
}
/* gdb_breakpoint_override */
{
.name = "gdb_breakpoint_override",
.handler = handle_gdb_breakpoint_override_command,
- .mode = COMMAND_EXEC,
+ .mode = COMMAND_ANY,
.help = "Display or specify type of breakpoint "
"to be used by gdb 'break' commands.",
.usage = "('hard'|'soft'|'disable')"