1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
26 #include "gdb_server.h"
30 #include "binarybuffer.h"
32 #include "breakpoints.h"
34 #include "target_request.h"
42 #define _DEBUG_GDB_IO_
45 static unsigned short gdb_port;
46 static const char *DIGITS = "0123456789abcdef";
48 static void gdb_log_callback(void *priv, const char *file, int line,
49 const char *function, const char *format, va_list args);
59 /* target behaviour on gdb detach */
60 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
62 /* set if we are sending a memory map to gdb
63 * via qXfer:memory-map:read packet */
64 int gdb_use_memory_map = 0;
65 int gdb_flash_program = 0;
67 int gdb_last_signal(target_t *target)
69 switch (target->debug_reason)
71 case DBG_REASON_DBGRQ:
72 return 0x2; /* SIGINT */
73 case DBG_REASON_BREAKPOINT:
74 case DBG_REASON_WATCHPOINT:
75 case DBG_REASON_WPTANDBKPT:
76 return 0x05; /* SIGTRAP */
77 case DBG_REASON_SINGLESTEP:
78 return 0x05; /* SIGTRAP */
79 case DBG_REASON_NOTHALTED:
80 return 0x0; /* no signal... shouldn't happen */
82 ERROR("BUG: undefined debug reason");
87 int gdb_get_char(connection_t *connection, int* next_char)
89 gdb_connection_t *gdb_con = connection->priv;
95 if (gdb_con->buf_cnt-- > 0)
97 *next_char = *(gdb_con->buf_p++);
98 if (gdb_con->buf_cnt > 0)
99 connection->input_pending = 1;
101 connection->input_pending = 0;
103 #ifdef _DEBUG_GDB_IO_
104 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
113 /* a non-blocking socket will block if there is 0 bytes available on the socket,
114 * but return with as many bytes as are available immediately
120 FD_SET(connection->fd, &read_fds);
124 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
126 /* This can typically be because a "monitor" command took too long
127 * before printing any progress messages
129 return ERROR_GDB_TIMEOUT;
132 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
133 if (gdb_con->buf_cnt > 0)
137 if (gdb_con->buf_cnt == 0)
140 return ERROR_SERVER_REMOTE_CLOSED;
144 errno = WSAGetLastError();
151 case WSAECONNABORTED:
152 return ERROR_SERVER_REMOTE_CLOSED;
154 return ERROR_SERVER_REMOTE_CLOSED;
156 ERROR("read: %d", errno);
166 return ERROR_SERVER_REMOTE_CLOSED;
168 return ERROR_SERVER_REMOTE_CLOSED;
170 ERROR("read: %s", strerror(errno));
171 return ERROR_SERVER_REMOTE_CLOSED;
176 #ifdef _DEBUG_GDB_IO_
177 debug_buffer = malloc(gdb_con->buf_cnt + 1);
178 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
179 debug_buffer[gdb_con->buf_cnt] = 0;
180 DEBUG("received '%s'", debug_buffer);
184 gdb_con->buf_p = gdb_con->buffer;
186 *next_char = *(gdb_con->buf_p++);
187 if (gdb_con->buf_cnt > 0)
188 connection->input_pending = 1;
190 connection->input_pending = 0;
191 #ifdef _DEBUG_GDB_IO_
192 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
198 int gdb_putback_char(connection_t *connection, int last_char)
200 gdb_connection_t *gdb_con = connection->priv;
202 if (gdb_con->buf_p > gdb_con->buffer)
204 *(--gdb_con->buf_p) = last_char;
209 ERROR("BUG: couldn't put character back");
215 /* The only way we can detect that the socket is closed is the first time
216 * we write to it, we will fail. Subsequent write operations will
217 * succeed. Shudder! */
218 int gdb_write(connection_t *connection, void *data, int len)
220 gdb_connection_t *gdb_con = connection->priv;
222 return ERROR_SERVER_REMOTE_CLOSED;
224 if (write_socket(connection->fd, data, len) == len)
229 return ERROR_SERVER_REMOTE_CLOSED;
232 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
235 unsigned char my_checksum = 0;
236 #ifdef _DEBUG_GDB_IO_
241 gdb_connection_t *gdb_con = connection->priv;
243 for (i = 0; i < len; i++)
244 my_checksum += buffer[i];
248 #ifdef _DEBUG_GDB_IO_
249 debug_buffer = malloc(len + 1);
250 memcpy(debug_buffer, buffer, len);
251 debug_buffer[len] = 0;
252 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
257 gdb_write(connection, "$", 1);
259 gdb_write(connection, buffer, len);
260 gdb_write(connection, "#", 1);
262 snprintf(checksum, 3, "%2.2x", my_checksum);
264 gdb_write(connection, checksum, 2);
266 void *allocated = NULL;
267 char stackAlloc[1024];
268 char *t = stackAlloc;
269 int totalLen = 1 + len + 1 + 2;
270 if (totalLen > sizeof(stackAlloc))
272 allocated = malloc(totalLen);
274 if (allocated == NULL)
276 ERROR("Ran out of memory trying to reply packet %d\n", totalLen);
281 memcpy(t + 1, buffer, len);
283 t[1 + len + 1] = DIGITS[(my_checksum >> 4) & 0xf];
284 t[1 + len + 2] = DIGITS[my_checksum & 0xf];
286 gdb_write(connection, t, totalLen);
293 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
298 else if (reply == '-')
300 /* Stop sending output packets for now */
301 log_setCallback(NULL, NULL);
302 WARNING("negative reply, retrying");
304 else if (reply == 0x3)
307 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
311 else if (reply == '-')
313 /* Stop sending output packets for now */
314 log_setCallback(NULL, NULL);
315 WARNING("negative reply, retrying");
319 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
320 return ERROR_SERVER_REMOTE_CLOSED;
325 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
326 return ERROR_SERVER_REMOTE_CLOSED;
330 return ERROR_SERVER_REMOTE_CLOSED;
335 int gdb_put_packet(connection_t *connection, char *buffer, int len)
337 gdb_connection_t *gdb_con = connection->priv;
339 int retval = gdb_put_packet_inner(connection, buffer, len);
344 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
350 unsigned char my_checksum = 0;
351 gdb_connection_t *gdb_con = connection->priv;
357 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
360 #ifdef _DEBUG_GDB_IO_
361 DEBUG("character: '%c'", character);
369 WARNING("acknowledgment received, but no packet pending");
372 WARNING("negative acknowledgment, but no packet pending");
379 WARNING("ignoring character 0x%x", character);
382 } while (character != '$');
387 gdb_connection_t *gdb_con = connection->priv;
390 /* The common case is that we have an entire packet with no escape chars.
391 * We need to leave at least 2 bytes in the buffer to have
392 * gdb_get_char() update various bits and bobs correctly.
394 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
396 /* The compiler will struggle a bit with constant propagation and
397 * aliasing, so we help it by showing that these values do not
398 * change inside the loop
401 char *buf = gdb_con->buf_p;
402 int run = gdb_con->buf_cnt - 2;
409 if (character == '#')
411 /* Danger! character can be '#' when esc is
412 * used so we need an explicit boolean for done here.
418 if (character == '}')
420 /* data transmitted in binary mode (X packet)
421 * uses 0x7d as escape character */
422 my_checksum += character & 0xff;
425 my_checksum += character & 0xff;
426 buffer[count++] = (character ^ 0x20) & 0xff;
429 my_checksum += character & 0xff;
430 buffer[count++] = character & 0xff;
434 gdb_con->buf_cnt -= i;
440 ERROR("packet buffer too small");
441 return ERROR_GDB_BUFFER_TOO_SMALL;
444 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
447 if (character == '#')
450 if (character == '}')
452 /* data transmitted in binary mode (X packet)
453 * uses 0x7d as escape character */
454 my_checksum += character & 0xff;
455 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
457 my_checksum += character & 0xff;
458 buffer[count++] = (character ^ 0x20) & 0xff;
462 my_checksum += character & 0xff;
463 buffer[count++] = character & 0xff;
470 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
472 checksum[0] = character;
473 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
475 checksum[1] = character;
478 if (my_checksum == strtoul(checksum, NULL, 16))
480 gdb_write(connection, "+", 1);
484 WARNING("checksum error, requesting retransmission");
485 gdb_write(connection, "-", 1);
488 return ERROR_SERVER_REMOTE_CLOSED;
493 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
495 gdb_connection_t *gdb_con = connection->priv;
497 int retval = gdb_get_packet_inner(connection, buffer, len);
502 int gdb_output_con(connection_t *connection, char* line)
507 bin_size = strlen(line);
509 hex_buffer = malloc(bin_size*2 + 4);
512 for (i=0; i<bin_size; i++)
513 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
514 hex_buffer[bin_size*2+1] = '0';
515 hex_buffer[bin_size*2+2] = 'a';
516 hex_buffer[bin_size*2+3] = 0x0;
518 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
524 int gdb_output(struct command_context_s *context, char* line)
526 /* this will be dumped to the log and also sent as an O packet if possible */
531 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
534 struct command_context_s *cmd_ctx = priv;
536 if (target->gdb_program_script)
538 script = fopen(target->gdb_program_script, "r");
541 ERROR("couldn't open script file %s", target->gdb_program_script);
545 INFO("executing gdb_program script '%s'", target->gdb_program_script);
546 command_run_file(cmd_ctx, script, COMMAND_EXEC);
549 jtag_execute_queue();
555 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
557 connection_t *connection = priv;
558 gdb_connection_t *gdb_connection = connection->priv;
564 case TARGET_EVENT_HALTED:
565 /* In the GDB protocol when we are stepping or coninuing execution,
566 * we have a lingering reply. Upon receiving a halted event
567 * when we have that lingering packet, we reply to the original
568 * step or continue packet.
570 * Executing monitor commands can bring the target in and
571 * out of the running state so we'll see lots of TARGET_EVENT_XXX
572 * that are to be ignored.
574 if (gdb_connection->frontend_state == TARGET_RUNNING)
576 /* stop forwarding log packets! */
577 log_setCallback(NULL, NULL);
579 if (gdb_connection->ctrl_c)
582 gdb_connection->ctrl_c = 0;
586 signal = gdb_last_signal(target);
589 snprintf(sig_reply, 4, "T%2.2x", signal);
590 gdb_put_packet(connection, sig_reply, 3);
591 gdb_connection->frontend_state = TARGET_HALTED;
594 case TARGET_EVENT_GDB_PROGRAM:
595 gdb_program_handler(target, event, connection->cmd_ctx);
604 int gdb_new_connection(connection_t *connection)
606 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
607 gdb_service_t *gdb_service = connection->service->priv;
611 connection->priv = gdb_connection;
613 /* initialize gdb connection information */
614 gdb_connection->buf_p = gdb_connection->buffer;
615 gdb_connection->buf_cnt = 0;
616 gdb_connection->ctrl_c = 0;
617 gdb_connection->frontend_state = TARGET_HALTED;
618 gdb_connection->vflash_image = NULL;
619 gdb_connection->closed = 0;
620 gdb_connection->busy = 0;
622 /* output goes through gdb connection */
623 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
625 /* register callback to be informed about target events */
626 target_register_event_callback(gdb_target_callback_event_handler, connection);
628 /* a gdb session just attached, put the target in halt mode */
629 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
630 (retval != ERROR_TARGET_ALREADY_HALTED))
632 ERROR("error(%d) when trying to halt target, falling back to \"reset halt\"", retval);
633 command_run_line(connection->cmd_ctx, "reset halt");
636 /* This will time out after 1 second */
637 command_run_line(connection->cmd_ctx, "wait_halt 1");
639 /* remove the initial ACK from the incoming buffer */
640 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
643 if (initial_ack != '+')
644 gdb_putback_char(connection, initial_ack);
649 int gdb_connection_closed(connection_t *connection)
651 gdb_service_t *gdb_service = connection->service->priv;
652 gdb_connection_t *gdb_connection = connection->priv;
654 /* see if an image built with vFlash commands is left */
655 if (gdb_connection->vflash_image)
657 image_close(gdb_connection->vflash_image);
658 free(gdb_connection->vflash_image);
659 gdb_connection->vflash_image = NULL;
662 /* if this connection registered a debug-message receiver delete it */
663 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
665 if (connection->priv)
667 free(connection->priv);
668 connection->priv = NULL;
672 ERROR("BUG: connection->priv == NULL");
675 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
680 void gdb_send_error(connection_t *connection, u8 the_error)
683 snprintf(err, 4, "E%2.2X", the_error );
684 gdb_put_packet(connection, err, 3);
687 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
692 signal = gdb_last_signal(target);
694 snprintf(sig_reply, 4, "S%2.2x", signal);
695 gdb_put_packet(connection, sig_reply, 3);
700 /* Convert register to string of bits. NB! The # of bits in the
701 * register might be non-divisible by 8(a byte), in which
702 * case an entire byte is shown. */
703 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
710 buf_len = CEIL(reg->size, 8);
712 if (target->endianness == TARGET_LITTLE_ENDIAN)
714 for (i = 0; i < buf_len; i++)
716 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
717 tstr[i*2+1] = DIGITS[buf[i]&0xf];
722 for (i = 0; i < buf_len; i++)
724 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
725 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
730 void gdb_target_to_str(target_t *target, char *tstr, char *str)
732 int str_len = strlen(tstr);
737 ERROR("BUG: gdb value with uneven number of characters encountered");
741 if (target->endianness == TARGET_LITTLE_ENDIAN)
743 for (i = 0; i < str_len; i+=2)
745 str[str_len - i - 1] = tstr[i + 1];
746 str[str_len - i - 2] = tstr[i];
751 for (i = 0; i < str_len; i++)
758 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
763 int reg_packet_size = 0;
768 #ifdef _DEBUG_GDB_IO_
772 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
776 case ERROR_TARGET_NOT_HALTED:
777 ERROR("gdb requested registers but we're not halted, dropping connection");
778 return ERROR_SERVER_REMOTE_CLOSED;
780 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
781 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
786 for (i = 0; i < reg_list_size; i++)
788 reg_packet_size += reg_list[i]->size;
791 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
792 reg_packet_p = reg_packet;
794 for (i = 0; i < reg_list_size; i++)
796 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
797 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
800 #ifdef _DEBUG_GDB_IO_
803 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
804 DEBUG("reg_packet: %s", reg_packet_p);
809 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
817 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
825 #ifdef _DEBUG_GDB_IO_
829 /* skip command character */
835 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
836 return ERROR_SERVER_REMOTE_CLOSED;
839 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
843 case ERROR_TARGET_NOT_HALTED:
844 ERROR("gdb tried to registers but we're not halted, dropping connection");
845 return ERROR_SERVER_REMOTE_CLOSED;
847 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
848 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
854 for (i = 0; i < reg_list_size; i++)
858 reg_arch_type_t *arch_type;
860 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
861 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
862 gdb_target_to_str(target, packet_p, hex_buf);
864 /* convert hex-string to binary buffer */
865 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
866 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
868 /* get register arch_type, and call set method */
869 arch_type = register_get_arch_type(reg_list[i]->arch_type);
870 if (arch_type == NULL)
872 ERROR("BUG: encountered unregistered arch type");
875 arch_type->set(reg_list[i], bin_buf);
877 /* advance packet pointer */
878 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
884 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
887 gdb_put_packet(connection, "OK", 2);
892 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
895 int reg_num = strtoul(packet + 1, NULL, 16);
900 #ifdef _DEBUG_GDB_IO_
904 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
908 case ERROR_TARGET_NOT_HALTED:
909 ERROR("gdb requested registers but we're not halted, dropping connection");
910 return ERROR_SERVER_REMOTE_CLOSED;
912 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
913 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
918 if (reg_list_size <= reg_num)
920 ERROR("gdb requested a non-existing register");
924 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
926 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
928 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
936 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
941 int reg_num = strtoul(packet + 1, &separator, 16);
945 reg_arch_type_t *arch_type;
949 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
953 case ERROR_TARGET_NOT_HALTED:
954 ERROR("gdb tried to set a register but we're not halted, dropping connection");
955 return ERROR_SERVER_REMOTE_CLOSED;
957 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
958 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
963 if (reg_list_size < reg_num)
965 ERROR("gdb requested a non-existing register");
966 return ERROR_SERVER_REMOTE_CLOSED;
969 if (*separator != '=')
971 ERROR("GDB 'set register packet', but no '=' following the register number");
972 return ERROR_SERVER_REMOTE_CLOSED;
975 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
976 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
977 gdb_target_to_str(target, separator + 1, hex_buf);
979 /* convert hex-string to binary buffer */
980 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
981 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
983 /* get register arch_type, and call set method */
984 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
985 if (arch_type == NULL)
987 ERROR("BUG: encountered unregistered arch type");
990 arch_type->set(reg_list[reg_num], bin_buf);
992 gdb_put_packet(connection, "OK", 2);
1001 int gdb_memory_packet_error(connection_t *connection, int retval)
1005 case ERROR_TARGET_NOT_HALTED:
1006 ERROR("gdb tried to read memory but we're not halted, dropping connection");
1007 return ERROR_SERVER_REMOTE_CLOSED;
1008 case ERROR_TARGET_DATA_ABORT:
1009 gdb_send_error(connection, EIO);
1011 case ERROR_TARGET_TRANSLATION_FAULT:
1012 gdb_send_error(connection, EFAULT);
1014 case ERROR_TARGET_UNALIGNED_ACCESS:
1015 gdb_send_error(connection, EFAULT);
1018 /* This could be that the target reset itself. */
1019 ERROR("unexpected error %i. Dropping connection.", retval);
1020 return ERROR_SERVER_REMOTE_CLOSED;
1026 /* We don't have to worry about the default 2 second timeout for GDB packets,
1027 * because GDB breaks up large memory reads into smaller reads.
1029 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1031 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1040 int retval = ERROR_OK;
1042 /* skip command character */
1045 addr = strtoul(packet, &separator, 16);
1047 if (*separator != ',')
1049 ERROR("incomplete read memory packet received, dropping connection");
1050 return ERROR_SERVER_REMOTE_CLOSED;
1053 len = strtoul(separator+1, NULL, 16);
1055 buffer = malloc(len);
1057 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1059 retval = target_read_buffer(target, addr, len, buffer);
1062 if (retval == ERROR_TARGET_DATA_ABORT)
1064 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1065 * At some point this might be fixed in GDB, in which case this code can be removed.
1067 * OpenOCD developers are acutely aware of this problem, but there is nothing
1068 * gained by involving the user in this problem that hopefully will get resolved
1071 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1073 memset(buffer, 0, len);
1078 if (retval == ERROR_OK)
1080 hex_buffer = malloc(len * 2 + 1);
1083 for (i = 0; i < len; i++)
1086 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1087 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1090 gdb_put_packet(connection, hex_buffer, len * 2);
1096 retval = gdb_memory_packet_error(connection, retval);
1104 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1115 /* skip command character */
1118 addr = strtoul(packet, &separator, 16);
1120 if (*separator != ',')
1122 ERROR("incomplete write memory packet received, dropping connection");
1123 return ERROR_SERVER_REMOTE_CLOSED;
1126 len = strtoul(separator+1, &separator, 16);
1128 if (*(separator++) != ':')
1130 ERROR("incomplete write memory packet received, dropping connection");
1131 return ERROR_SERVER_REMOTE_CLOSED;
1134 buffer = malloc(len);
1136 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1138 for (i=0; i<len; i++)
1141 sscanf(separator + 2*i, "%2x", &tmp);
1145 retval = target_write_buffer(target, addr, len, buffer);
1147 if (retval == ERROR_OK)
1149 gdb_put_packet(connection, "OK", 2);
1153 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1162 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1170 /* skip command character */
1173 addr = strtoul(packet, &separator, 16);
1175 if (*separator != ',')
1177 ERROR("incomplete write memory binary packet received, dropping connection");
1178 return ERROR_SERVER_REMOTE_CLOSED;
1181 len = strtoul(separator+1, &separator, 16);
1183 if (*(separator++) != ':')
1185 ERROR("incomplete write memory binary packet received, dropping connection");
1186 return ERROR_SERVER_REMOTE_CLOSED;
1192 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1194 retval = target_write_buffer(target, addr, len, separator);
1197 if (retval == ERROR_OK)
1199 gdb_put_packet(connection, "OK", 2);
1203 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1210 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1217 if (packet_size > 1)
1219 packet[packet_size] = 0;
1220 address = strtoul(packet + 1, NULL, 16);
1227 if (packet[0] == 'c')
1230 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1232 else if (packet[0] == 's')
1235 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1239 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1243 case ERROR_TARGET_NOT_HALTED:
1244 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1245 return ERROR_SERVER_REMOTE_CLOSED;
1247 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1248 gdb_send_error(connection, EBUSY);
1251 ERROR("BUG: unexpected error %i", retval);
1258 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1261 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1262 enum watchpoint_rw wp_type;
1270 type = strtoul(packet + 1, &separator, 16);
1272 if (type == 0) /* memory breakpoint */
1273 bp_type = BKPT_SOFT;
1274 else if (type == 1) /* hardware breakpoint */
1275 bp_type = BKPT_HARD;
1276 else if (type == 2) /* write watchpoint */
1277 wp_type = WPT_WRITE;
1278 else if (type == 3) /* read watchpoint */
1280 else if (type == 4) /* access watchpoint */
1281 wp_type = WPT_ACCESS;
1283 if (*separator != ',')
1285 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1286 return ERROR_SERVER_REMOTE_CLOSED;
1289 address = strtoul(separator+1, &separator, 16);
1291 if (*separator != ',')
1293 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1294 return ERROR_SERVER_REMOTE_CLOSED;
1297 size = strtoul(separator+1, &separator, 16);
1303 if (packet[0] == 'Z')
1305 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1307 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1312 gdb_put_packet(connection, "OK", 2);
1317 breakpoint_remove(target, address);
1318 gdb_put_packet(connection, "OK", 2);
1325 if (packet[0] == 'Z')
1327 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1329 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1334 gdb_put_packet(connection, "OK", 2);
1339 watchpoint_remove(target, address);
1340 gdb_put_packet(connection, "OK", 2);
1351 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1352 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1354 if (*retval != ERROR_OK)
1362 if ((*xml == NULL) || (!first))
1364 /* start by 0 to exercise all the code paths.
1365 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1367 *size = *size * 2 + 2;
1369 *xml = realloc(*xml, *size);
1374 *retval = ERROR_SERVER_REMOTE_CLOSED;
1382 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1384 if ((ret > 0) && ((ret + 1) < *size - *pos))
1389 /* there was just enough or not enough space, allocate more. */
1394 static int decode_xfer_read (char *buf, char **annex, int *ofs, unsigned int *len)
1398 /* Extract and NUL-terminate the annex. */
1400 while (*buf && *buf != ':')
1406 /* After the read marker and annex, qXfer looks like a
1407 * traditional 'm' packet. */
1409 *ofs = strtoul(buf, &separator, 16);
1411 if (*separator != ',')
1414 *len = strtoul(separator+1, NULL, 16);
1419 int gdb_calc_blocksize(flash_bank_t *bank)
1422 int block_size = 0xffffffff;
1424 /* loop through all sectors and return smallest sector size */
1426 for (i = 0; i < bank->num_sectors; i++)
1428 if (bank->sectors[i].size < block_size)
1429 block_size = bank->sectors[i].size;
1435 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1437 command_context_t *cmd_ctx = connection->cmd_ctx;
1439 if (strstr(packet, "qRcmd,"))
1441 if (packet_size > 6)
1445 cmd = malloc((packet_size - 6)/2 + 1);
1446 for (i=0; i < (packet_size - 6)/2; i++)
1449 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1452 cmd[(packet_size - 6)/2] = 0x0;
1454 /* We want to print all debug output to GDB connection */
1455 log_setCallback(gdb_log_callback, connection);
1456 target_call_timer_callbacks();
1457 command_run_line(cmd_ctx, cmd);
1460 gdb_put_packet(connection, "OK", 2);
1463 else if (strstr(packet, "qCRC:"))
1465 if (packet_size > 5)
1474 /* skip command character */
1477 addr = strtoul(packet, &separator, 16);
1479 if (*separator != ',')
1481 ERROR("incomplete read memory packet received, dropping connection");
1482 return ERROR_SERVER_REMOTE_CLOSED;
1485 len = strtoul(separator + 1, NULL, 16);
1487 retval = target_checksum_memory(target, addr, len, &checksum);
1489 if (retval == ERROR_OK)
1491 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1492 gdb_put_packet(connection, gdb_reply, 9);
1496 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1503 else if (strstr(packet, "qSupported"))
1505 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1506 * disable qXfer:features:read for the moment */
1507 int retval = ERROR_OK;
1508 char *buffer = NULL;
1512 xml_printf(&retval, &buffer, &pos, &size,
1513 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1514 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1516 if (retval != ERROR_OK)
1518 gdb_send_error(connection, 01);
1522 gdb_put_packet(connection, buffer, strlen(buffer));
1527 else if (strstr(packet, "qXfer:memory-map:read::"))
1529 /* We get away with only specifying flash here. Regions that are not
1530 * specified are treated as if we provided no memory map(if not we
1531 * could detect the holes and mark them as RAM).
1532 * Normally we only execute this code once, but no big deal if we
1533 * have to regenerate it a couple of times. */
1539 int retval = ERROR_OK;
1546 /* skip command character */
1549 offset = strtoul(packet, &separator, 16);
1550 length = strtoul(separator + 1, &separator, 16);
1552 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1557 p = get_flash_bank_by_num(i);
1561 /* if device has uneven sector sizes, eg. str7, lpc
1562 * we pass the smallest sector size to gdb memory map */
1563 blocksize = gdb_calc_blocksize(p);
1565 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1566 "<property name=\"blocksize\">0x%x</property>\n" \
1568 p->base, p->size, blocksize);
1572 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1574 if (retval != ERROR_OK)
1576 gdb_send_error(connection, retval);
1580 if (offset + length > pos)
1582 length = pos - offset;
1585 char *t = malloc(length + 1);
1587 memcpy(t + 1, xml + offset, length);
1588 gdb_put_packet(connection, t, length + 1);
1594 else if (strstr(packet, "qXfer:features:read:"))
1599 int retval = ERROR_OK;
1605 /* skip command character */
1608 if (decode_xfer_read( packet, &annex, &offset, &length ) < 0)
1610 gdb_send_error(connection, 01);
1614 if (strcmp(annex, "target.xml") != 0)
1616 gdb_send_error(connection, 01);
1620 xml_printf(&retval, &xml, &pos, &size, \
1621 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1623 if (retval != ERROR_OK)
1625 gdb_send_error(connection, retval);
1629 gdb_put_packet(connection, xml, strlen(xml) + 1);
1635 gdb_put_packet(connection, "", 0);
1639 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1641 gdb_connection_t *gdb_connection = connection->priv;
1642 gdb_service_t *gdb_service = connection->service->priv;
1645 /* if flash programming disabled - send a empty reply */
1647 if (gdb_flash_program == 0)
1649 gdb_put_packet(connection, "", 0);
1653 if (strstr(packet, "vFlashErase:"))
1656 unsigned long length;
1658 char *parse = packet + 12;
1661 ERROR("incomplete vFlashErase packet received, dropping connection");
1662 return ERROR_SERVER_REMOTE_CLOSED;
1665 addr = strtoul(parse, &parse, 16);
1667 if (*(parse++) != ',' || *parse == '\0')
1669 ERROR("incomplete vFlashErase packet received, dropping connection");
1670 return ERROR_SERVER_REMOTE_CLOSED;
1673 length = strtoul(parse, &parse, 16);
1677 ERROR("incomplete vFlashErase packet received, dropping connection");
1678 return ERROR_SERVER_REMOTE_CLOSED;
1681 /* assume all sectors need erasing - stops any problems
1682 * when flash_write is called multiple times */
1685 /* perform any target specific operations before the erase */
1686 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1689 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1691 /* GDB doesn't evaluate the actual error number returned,
1692 * treat a failed erase as an I/O error
1694 gdb_send_error(connection, EIO);
1695 ERROR("flash_erase returned %i", result);
1698 gdb_put_packet(connection, "OK", 2);
1703 if (strstr(packet, "vFlashWrite:"))
1706 unsigned long length;
1707 char *parse = packet + 12;
1711 ERROR("incomplete vFlashErase packet received, dropping connection");
1712 return ERROR_SERVER_REMOTE_CLOSED;
1714 addr = strtoul(parse, &parse, 16);
1715 if (*(parse++) != ':')
1717 ERROR("incomplete vFlashErase packet received, dropping connection");
1718 return ERROR_SERVER_REMOTE_CLOSED;
1720 length = packet_size - (parse - packet);
1722 /* create a new image if there isn't already one */
1723 if (gdb_connection->vflash_image == NULL)
1725 gdb_connection->vflash_image = malloc(sizeof(image_t));
1726 image_open(gdb_connection->vflash_image, "", "build");
1729 /* create new section with content from packet buffer */
1730 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1732 gdb_put_packet(connection, "OK", 2);
1737 if (!strcmp(packet, "vFlashDone"))
1742 /* process the flashing buffer. No need to erase as GDB
1743 * always issues a vFlashErase first. */
1744 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1746 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1747 gdb_put_packet(connection, "E.memtype", 9);
1749 gdb_send_error(connection, EIO);
1753 ERROR("flash writing failed: %s", error_str);
1759 DEBUG("wrote %u bytes from vFlash image to flash", written);
1760 gdb_put_packet(connection, "OK", 2);
1763 image_close(gdb_connection->vflash_image);
1764 free(gdb_connection->vflash_image);
1765 gdb_connection->vflash_image = NULL;
1770 gdb_put_packet(connection, "", 0);
1774 int gdb_detach(connection_t *connection, target_t *target)
1776 switch( detach_mode )
1778 case GDB_DETACH_RESUME:
1779 target->type->resume(target, 1, 0, 1, 0);
1782 case GDB_DETACH_RESET:
1783 target_process_reset(connection->cmd_ctx);
1786 case GDB_DETACH_HALT:
1787 target->type->halt(target);
1790 case GDB_DETACH_NOTHING:
1794 gdb_put_packet(connection, "OK", 2);
1799 static void gdb_log_callback(void *priv, const char *file, int line,
1800 const char *function, const char *format, va_list args)
1802 connection_t *connection = priv;
1803 gdb_connection_t *gdb_con = connection->priv;
1807 /* do not reply this using the O packet */
1811 char *t = allocPrintf(format, args);
1815 gdb_output_con(connection, t);
1820 int gdb_input_inner(connection_t *connection)
1822 gdb_service_t *gdb_service = connection->service->priv;
1823 target_t *target = gdb_service->target;
1824 char packet[GDB_BUFFER_SIZE];
1827 gdb_connection_t *gdb_con = connection->priv;
1828 static int extended_protocol = 0;
1830 /* drain input buffer */
1833 packet_size = GDB_BUFFER_SIZE-1;
1834 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1839 /* terminate with zero */
1840 packet[packet_size] = 0;
1842 DEBUG("received packet: '%s'", packet);
1844 if (packet_size > 0)
1850 /* Hct... -- set thread
1851 * we don't have threads, send empty reply */
1852 gdb_put_packet(connection, NULL, 0);
1855 retval = gdb_query_packet(connection, target, packet, packet_size);
1858 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1861 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1864 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1867 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1870 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1873 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1877 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1880 gdb_last_signal_packet(connection, target, packet, packet_size);
1885 /* We're running/stepping, in which case we can
1886 * forward log output until the target is halted */
1887 gdb_connection_t *gdb_con = connection->priv;
1888 gdb_con->frontend_state = TARGET_RUNNING;
1889 log_setCallback(gdb_log_callback, connection);
1890 gdb_step_continue_packet(connection, target, packet, packet_size);
1894 retval = gdb_v_packet(connection, target, packet, packet_size);
1897 retval = gdb_detach(connection, target);
1898 extended_protocol = 0;
1901 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1905 if (extended_protocol != 0)
1907 gdb_put_packet(connection, "OK", 2);
1908 return ERROR_SERVER_REMOTE_CLOSED;
1910 /* handle extended remote protocol */
1911 extended_protocol = 1;
1912 gdb_put_packet(connection, "OK", 2);
1915 /* handle extended restart packet */
1916 target_process_reset(connection->cmd_ctx);
1919 /* ignore unkown packets */
1920 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1921 gdb_put_packet(connection, NULL, 0);
1925 /* if a packet handler returned an error, exit input loop */
1926 if (retval != ERROR_OK)
1930 if (gdb_con->ctrl_c)
1932 if (target->state == TARGET_RUNNING)
1934 target->type->halt(target);
1935 gdb_con->ctrl_c = 0;
1939 } while (gdb_con->buf_cnt > 0);
1944 int gdb_input(connection_t *connection)
1946 int retval = gdb_input_inner(connection);
1947 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1949 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1955 gdb_service_t *gdb_service;
1956 target_t *target = targets;
1961 WARNING("no gdb ports allocated as no target has been specified");
1967 WARNING("no gdb port specified, using default port 3333");
1973 char service_name[8];
1975 snprintf(service_name, 8, "gdb-%2.2i", i);
1977 gdb_service = malloc(sizeof(gdb_service_t));
1978 gdb_service->target = target;
1980 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1982 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1985 target = target->next;
1991 /* daemon configuration command gdb_port */
1992 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1997 /* only if the port wasn't overwritten by cmdline */
1999 gdb_port = strtoul(args[0], NULL, 0);
2004 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2008 if (strcmp(args[0], "resume") == 0)
2010 detach_mode = GDB_DETACH_RESUME;
2013 else if (strcmp(args[0], "reset") == 0)
2015 detach_mode = GDB_DETACH_RESET;
2018 else if (strcmp(args[0], "halt") == 0)
2020 detach_mode = GDB_DETACH_HALT;
2023 else if (strcmp(args[0], "nothing") == 0)
2025 detach_mode = GDB_DETACH_NOTHING;
2030 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2034 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2038 if (strcmp(args[0], "enable") == 0)
2040 gdb_use_memory_map = 1;
2043 else if (strcmp(args[0], "disable") == 0)
2045 gdb_use_memory_map = 0;
2050 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2054 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2058 if (strcmp(args[0], "enable") == 0)
2060 gdb_flash_program = 1;
2063 else if (strcmp(args[0], "disable") == 0)
2065 gdb_flash_program = 0;
2070 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2074 int gdb_register_commands(command_context_t *command_context)
2076 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2077 COMMAND_CONFIG, "");
2078 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2079 COMMAND_CONFIG, "");
2080 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2081 COMMAND_CONFIG, "");
2082 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2083 COMMAND_CONFIG, "");