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"
35 #include "configuration.h"
43 #define _DEBUG_GDB_IO_
46 static unsigned short gdb_port;
47 static const char *DIGITS = "0123456789abcdef";
49 static void gdb_log_callback(void *priv, const char *file, int line,
50 const char *function, const char *string);
60 /* target behaviour on gdb detach */
61 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
63 /* set if we are sending a memory map to gdb
64 * via qXfer:memory-map:read packet */
65 int gdb_use_memory_map = 0;
66 int gdb_flash_program = 0;
68 /* if set, data aborts cause an error to be reported in memory read packets
69 * see the code in gdb_read_memory_packet() for further explanations */
70 int gdb_report_data_abort = 0;
72 int gdb_last_signal(target_t *target)
74 switch (target->debug_reason)
76 case DBG_REASON_DBGRQ:
77 return 0x2; /* SIGINT */
78 case DBG_REASON_BREAKPOINT:
79 case DBG_REASON_WATCHPOINT:
80 case DBG_REASON_WPTANDBKPT:
81 return 0x05; /* SIGTRAP */
82 case DBG_REASON_SINGLESTEP:
83 return 0x05; /* SIGTRAP */
84 case DBG_REASON_NOTHALTED:
85 return 0x0; /* no signal... shouldn't happen */
87 ERROR("BUG: undefined debug reason");
92 int gdb_get_char(connection_t *connection, int* next_char)
94 gdb_connection_t *gdb_con = connection->priv;
100 if (gdb_con->buf_cnt-- > 0)
102 *next_char = *(gdb_con->buf_p++);
103 if (gdb_con->buf_cnt > 0)
104 connection->input_pending = 1;
106 connection->input_pending = 0;
108 #ifdef _DEBUG_GDB_IO_
109 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
118 /* a non-blocking socket will block if there is 0 bytes available on the socket,
119 * but return with as many bytes as are available immediately
125 FD_SET(connection->fd, &read_fds);
129 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
131 /* This can typically be because a "monitor" command took too long
132 * before printing any progress messages
134 return ERROR_GDB_TIMEOUT;
137 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
138 if (gdb_con->buf_cnt > 0)
142 if (gdb_con->buf_cnt == 0)
145 return ERROR_SERVER_REMOTE_CLOSED;
149 errno = WSAGetLastError();
156 case WSAECONNABORTED:
157 return ERROR_SERVER_REMOTE_CLOSED;
159 return ERROR_SERVER_REMOTE_CLOSED;
161 ERROR("read: %d", errno);
171 return ERROR_SERVER_REMOTE_CLOSED;
173 return ERROR_SERVER_REMOTE_CLOSED;
175 ERROR("read: %s", strerror(errno));
176 return ERROR_SERVER_REMOTE_CLOSED;
181 #ifdef _DEBUG_GDB_IO_
182 debug_buffer = malloc(gdb_con->buf_cnt + 1);
183 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
184 debug_buffer[gdb_con->buf_cnt] = 0;
185 DEBUG("received '%s'", debug_buffer);
189 gdb_con->buf_p = gdb_con->buffer;
191 *next_char = *(gdb_con->buf_p++);
192 if (gdb_con->buf_cnt > 0)
193 connection->input_pending = 1;
195 connection->input_pending = 0;
196 #ifdef _DEBUG_GDB_IO_
197 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
203 int gdb_putback_char(connection_t *connection, int last_char)
205 gdb_connection_t *gdb_con = connection->priv;
207 if (gdb_con->buf_p > gdb_con->buffer)
209 *(--gdb_con->buf_p) = last_char;
214 ERROR("BUG: couldn't put character back");
220 /* The only way we can detect that the socket is closed is the first time
221 * we write to it, we will fail. Subsequent write operations will
222 * succeed. Shudder! */
223 int gdb_write(connection_t *connection, void *data, int len)
225 gdb_connection_t *gdb_con = connection->priv;
227 return ERROR_SERVER_REMOTE_CLOSED;
229 if (write_socket(connection->fd, data, len) == len)
234 return ERROR_SERVER_REMOTE_CLOSED;
237 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
240 unsigned char my_checksum = 0;
241 #ifdef _DEBUG_GDB_IO_
246 gdb_connection_t *gdb_con = connection->priv;
248 for (i = 0; i < len; i++)
249 my_checksum += buffer[i];
253 #ifdef _DEBUG_GDB_IO_
254 debug_buffer = malloc(len + 1);
255 memcpy(debug_buffer, buffer, len);
256 debug_buffer[len] = 0;
257 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
262 gdb_write(connection, "$", 1);
264 gdb_write(connection, buffer, len);
265 gdb_write(connection, "#", 1);
267 snprintf(checksum, 3, "%2.2x", my_checksum);
269 gdb_write(connection, checksum, 2);
271 void *allocated = NULL;
272 char stackAlloc[1024];
273 char *t = stackAlloc;
274 int totalLen = 1 + len + 1 + 2;
275 if (totalLen > sizeof(stackAlloc))
277 allocated = malloc(totalLen);
279 if (allocated == NULL)
281 ERROR("Ran out of memory trying to reply packet %d\n", totalLen);
286 memcpy(t + 1, buffer, len);
288 t[1 + len + 1] = DIGITS[(my_checksum >> 4) & 0xf];
289 t[1 + len + 2] = DIGITS[my_checksum & 0xf];
291 gdb_write(connection, t, totalLen);
298 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
303 else if (reply == '-')
305 /* Stop sending output packets for now */
306 log_remove_callback(gdb_log_callback, connection);
307 WARNING("negative reply, retrying");
309 else if (reply == 0x3)
312 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
316 else if (reply == '-')
318 /* Stop sending output packets for now */
319 log_remove_callback(gdb_log_callback, connection);
320 WARNING("negative reply, retrying");
324 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
325 return ERROR_SERVER_REMOTE_CLOSED;
330 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
331 return ERROR_SERVER_REMOTE_CLOSED;
335 return ERROR_SERVER_REMOTE_CLOSED;
340 int gdb_put_packet(connection_t *connection, char *buffer, int len)
342 gdb_connection_t *gdb_con = connection->priv;
344 int retval = gdb_put_packet_inner(connection, buffer, len);
349 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
355 unsigned char my_checksum = 0;
356 gdb_connection_t *gdb_con = connection->priv;
362 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
365 #ifdef _DEBUG_GDB_IO_
366 DEBUG("character: '%c'", character);
374 WARNING("acknowledgment received, but no packet pending");
377 WARNING("negative acknowledgment, but no packet pending");
384 WARNING("ignoring character 0x%x", character);
387 } while (character != '$');
392 gdb_connection_t *gdb_con = connection->priv;
395 /* The common case is that we have an entire packet with no escape chars.
396 * We need to leave at least 2 bytes in the buffer to have
397 * gdb_get_char() update various bits and bobs correctly.
399 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
401 /* The compiler will struggle a bit with constant propagation and
402 * aliasing, so we help it by showing that these values do not
403 * change inside the loop
406 char *buf = gdb_con->buf_p;
407 int run = gdb_con->buf_cnt - 2;
414 if (character == '#')
416 /* Danger! character can be '#' when esc is
417 * used so we need an explicit boolean for done here.
423 if (character == '}')
425 /* data transmitted in binary mode (X packet)
426 * uses 0x7d as escape character */
427 my_checksum += character & 0xff;
430 my_checksum += character & 0xff;
431 buffer[count++] = (character ^ 0x20) & 0xff;
434 my_checksum += character & 0xff;
435 buffer[count++] = character & 0xff;
439 gdb_con->buf_cnt -= i;
445 ERROR("packet buffer too small");
446 return ERROR_GDB_BUFFER_TOO_SMALL;
449 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
452 if (character == '#')
455 if (character == '}')
457 /* data transmitted in binary mode (X packet)
458 * uses 0x7d as escape character */
459 my_checksum += character & 0xff;
460 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
462 my_checksum += character & 0xff;
463 buffer[count++] = (character ^ 0x20) & 0xff;
467 my_checksum += character & 0xff;
468 buffer[count++] = character & 0xff;
475 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
477 checksum[0] = character;
478 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
480 checksum[1] = character;
483 if (my_checksum == strtoul(checksum, NULL, 16))
485 gdb_write(connection, "+", 1);
489 WARNING("checksum error, requesting retransmission");
490 gdb_write(connection, "-", 1);
493 return ERROR_SERVER_REMOTE_CLOSED;
498 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
500 gdb_connection_t *gdb_con = connection->priv;
502 int retval = gdb_get_packet_inner(connection, buffer, len);
507 int gdb_output_con(connection_t *connection, const char* line)
512 bin_size = strlen(line);
514 hex_buffer = malloc(bin_size*2 + 2);
515 if (hex_buffer == NULL)
516 return ERROR_GDB_BUFFER_TOO_SMALL;
519 for (i=0; i<bin_size; i++)
520 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
521 hex_buffer[bin_size*2+1] = 0;
523 gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
529 int gdb_output(struct command_context_s *context, char* line)
531 /* this will be dumped to the log and also sent as an O packet if possible */
536 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
539 struct command_context_s *cmd_ctx = priv;
541 if (target->gdb_program_script)
543 script = open_file_from_path(target->gdb_program_script, "r");
546 ERROR("couldn't open script file %s", target->gdb_program_script);
550 INFO("executing gdb_program script '%s'", target->gdb_program_script);
551 command_run_file(cmd_ctx, script, COMMAND_EXEC);
554 jtag_execute_queue();
560 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
562 connection_t *connection = priv;
563 gdb_connection_t *gdb_connection = connection->priv;
569 case TARGET_EVENT_HALTED:
570 /* In the GDB protocol when we are stepping or coninuing execution,
571 * we have a lingering reply. Upon receiving a halted event
572 * when we have that lingering packet, we reply to the original
573 * step or continue packet.
575 * Executing monitor commands can bring the target in and
576 * out of the running state so we'll see lots of TARGET_EVENT_XXX
577 * that are to be ignored.
579 if (gdb_connection->frontend_state == TARGET_RUNNING)
581 /* stop forwarding log packets! */
582 log_remove_callback(gdb_log_callback, connection);
584 if (gdb_connection->ctrl_c)
587 gdb_connection->ctrl_c = 0;
591 signal = gdb_last_signal(target);
594 snprintf(sig_reply, 4, "T%2.2x", signal);
595 gdb_put_packet(connection, sig_reply, 3);
596 gdb_connection->frontend_state = TARGET_HALTED;
599 case TARGET_EVENT_GDB_PROGRAM:
600 gdb_program_handler(target, event, connection->cmd_ctx);
610 int gdb_new_connection(connection_t *connection)
612 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
613 gdb_service_t *gdb_service = connection->service->priv;
617 connection->priv = gdb_connection;
619 /* initialize gdb connection information */
620 gdb_connection->buf_p = gdb_connection->buffer;
621 gdb_connection->buf_cnt = 0;
622 gdb_connection->ctrl_c = 0;
623 gdb_connection->frontend_state = TARGET_HALTED;
624 gdb_connection->vflash_image = NULL;
625 gdb_connection->closed = 0;
626 gdb_connection->busy = 0;
628 /* output goes through gdb connection */
629 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
631 /* register callback to be informed about target events */
632 target_register_event_callback(gdb_target_callback_event_handler, connection);
634 /* a gdb session just attached, put the target in halt mode */
635 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
636 (retval != ERROR_TARGET_ALREADY_HALTED))
638 ERROR("error(%d) when trying to halt target, falling back to \"reset halt\"", retval);
639 command_run_line(connection->cmd_ctx, "reset halt");
642 /* This will time out after 1 second */
643 command_run_line(connection->cmd_ctx, "wait_halt 1");
645 /* remove the initial ACK from the incoming buffer */
646 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
649 if (initial_ack != '+')
650 gdb_putback_char(connection, initial_ack);
655 int gdb_connection_closed(connection_t *connection)
657 gdb_service_t *gdb_service = connection->service->priv;
658 gdb_connection_t *gdb_connection = connection->priv;
660 /* see if an image built with vFlash commands is left */
661 if (gdb_connection->vflash_image)
663 image_close(gdb_connection->vflash_image);
664 free(gdb_connection->vflash_image);
665 gdb_connection->vflash_image = NULL;
668 /* if this connection registered a debug-message receiver delete it */
669 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
671 if (connection->priv)
673 free(connection->priv);
674 connection->priv = NULL;
678 ERROR("BUG: connection->priv == NULL");
681 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
682 log_remove_callback(gdb_log_callback, connection);
687 void gdb_send_error(connection_t *connection, u8 the_error)
690 snprintf(err, 4, "E%2.2X", the_error );
691 gdb_put_packet(connection, err, 3);
694 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
699 signal = gdb_last_signal(target);
701 snprintf(sig_reply, 4, "S%2.2x", signal);
702 gdb_put_packet(connection, sig_reply, 3);
707 /* Convert register to string of bits. NB! The # of bits in the
708 * register might be non-divisible by 8(a byte), in which
709 * case an entire byte is shown. */
710 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
717 buf_len = CEIL(reg->size, 8);
719 if (target->endianness == TARGET_LITTLE_ENDIAN)
721 for (i = 0; i < buf_len; i++)
723 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
724 tstr[i*2+1] = DIGITS[buf[i]&0xf];
729 for (i = 0; i < buf_len; i++)
731 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
732 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
737 void gdb_target_to_str(target_t *target, char *tstr, char *str)
739 int str_len = strlen(tstr);
744 ERROR("BUG: gdb value with uneven number of characters encountered");
748 if (target->endianness == TARGET_LITTLE_ENDIAN)
750 for (i = 0; i < str_len; i+=2)
752 str[str_len - i - 1] = tstr[i + 1];
753 str[str_len - i - 2] = tstr[i];
758 for (i = 0; i < str_len; i++)
765 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
770 int reg_packet_size = 0;
775 #ifdef _DEBUG_GDB_IO_
779 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
783 case ERROR_TARGET_NOT_HALTED:
784 ERROR("gdb requested registers but we're not halted, dropping connection");
785 return ERROR_SERVER_REMOTE_CLOSED;
787 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
788 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
793 for (i = 0; i < reg_list_size; i++)
795 reg_packet_size += reg_list[i]->size;
798 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
799 reg_packet_p = reg_packet;
801 for (i = 0; i < reg_list_size; i++)
803 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
804 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
807 #ifdef _DEBUG_GDB_IO_
810 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
811 DEBUG("reg_packet: %s", reg_packet_p);
816 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
824 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
832 #ifdef _DEBUG_GDB_IO_
836 /* skip command character */
842 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
843 return ERROR_SERVER_REMOTE_CLOSED;
846 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
850 case ERROR_TARGET_NOT_HALTED:
851 ERROR("gdb tried to registers but we're not halted, dropping connection");
852 return ERROR_SERVER_REMOTE_CLOSED;
854 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
855 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
861 for (i = 0; i < reg_list_size; i++)
865 reg_arch_type_t *arch_type;
867 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
868 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
869 gdb_target_to_str(target, packet_p, hex_buf);
871 /* convert hex-string to binary buffer */
872 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
873 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
875 /* get register arch_type, and call set method */
876 arch_type = register_get_arch_type(reg_list[i]->arch_type);
877 if (arch_type == NULL)
879 ERROR("BUG: encountered unregistered arch type");
882 arch_type->set(reg_list[i], bin_buf);
884 /* advance packet pointer */
885 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
891 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
894 gdb_put_packet(connection, "OK", 2);
899 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
902 int reg_num = strtoul(packet + 1, NULL, 16);
907 #ifdef _DEBUG_GDB_IO_
911 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
915 case ERROR_TARGET_NOT_HALTED:
916 ERROR("gdb requested registers but we're not halted, dropping connection");
917 return ERROR_SERVER_REMOTE_CLOSED;
919 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
920 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
925 if (reg_list_size <= reg_num)
927 ERROR("gdb requested a non-existing register");
931 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
933 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
935 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
943 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
948 int reg_num = strtoul(packet + 1, &separator, 16);
952 reg_arch_type_t *arch_type;
956 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
960 case ERROR_TARGET_NOT_HALTED:
961 ERROR("gdb tried to set a register but we're not halted, dropping connection");
962 return ERROR_SERVER_REMOTE_CLOSED;
964 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
965 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
970 if (reg_list_size < reg_num)
972 ERROR("gdb requested a non-existing register");
973 return ERROR_SERVER_REMOTE_CLOSED;
976 if (*separator != '=')
978 ERROR("GDB 'set register packet', but no '=' following the register number");
979 return ERROR_SERVER_REMOTE_CLOSED;
982 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
983 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
984 gdb_target_to_str(target, separator + 1, hex_buf);
986 /* convert hex-string to binary buffer */
987 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
988 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
990 /* get register arch_type, and call set method */
991 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
992 if (arch_type == NULL)
994 ERROR("BUG: encountered unregistered arch type");
997 arch_type->set(reg_list[reg_num], bin_buf);
999 gdb_put_packet(connection, "OK", 2);
1008 int gdb_memory_packet_error(connection_t *connection, int retval)
1012 case ERROR_TARGET_NOT_HALTED:
1013 ERROR("gdb tried to read memory but we're not halted, dropping connection");
1014 return ERROR_SERVER_REMOTE_CLOSED;
1015 case ERROR_TARGET_DATA_ABORT:
1016 gdb_send_error(connection, EIO);
1018 case ERROR_TARGET_TRANSLATION_FAULT:
1019 gdb_send_error(connection, EFAULT);
1021 case ERROR_TARGET_UNALIGNED_ACCESS:
1022 gdb_send_error(connection, EFAULT);
1025 /* This could be that the target reset itself. */
1026 ERROR("unexpected error %i. Dropping connection.", retval);
1027 return ERROR_SERVER_REMOTE_CLOSED;
1033 /* We don't have to worry about the default 2 second timeout for GDB packets,
1034 * because GDB breaks up large memory reads into smaller reads.
1036 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1038 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1047 int retval = ERROR_OK;
1049 /* skip command character */
1052 addr = strtoul(packet, &separator, 16);
1054 if (*separator != ',')
1056 ERROR("incomplete read memory packet received, dropping connection");
1057 return ERROR_SERVER_REMOTE_CLOSED;
1060 len = strtoul(separator+1, NULL, 16);
1062 buffer = malloc(len);
1064 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1066 retval = target_read_buffer(target, addr, len, buffer);
1068 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1070 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1071 * At some point this might be fixed in GDB, in which case this code can be removed.
1073 * OpenOCD developers are acutely aware of this problem, but there is nothing
1074 * gained by involving the user in this problem that hopefully will get resolved
1077 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1079 * For now, the default is to fix up things to make current GDB versions work.
1080 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1082 memset(buffer, 0, len);
1086 if (retval == ERROR_OK)
1088 hex_buffer = malloc(len * 2 + 1);
1091 for (i = 0; i < len; i++)
1094 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1095 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1098 gdb_put_packet(connection, hex_buffer, len * 2);
1104 retval = gdb_memory_packet_error(connection, retval);
1112 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1123 /* skip command character */
1126 addr = strtoul(packet, &separator, 16);
1128 if (*separator != ',')
1130 ERROR("incomplete write memory packet received, dropping connection");
1131 return ERROR_SERVER_REMOTE_CLOSED;
1134 len = strtoul(separator+1, &separator, 16);
1136 if (*(separator++) != ':')
1138 ERROR("incomplete write memory packet received, dropping connection");
1139 return ERROR_SERVER_REMOTE_CLOSED;
1142 buffer = malloc(len);
1144 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1146 for (i=0; i<len; i++)
1149 sscanf(separator + 2*i, "%2x", &tmp);
1153 retval = target_write_buffer(target, addr, len, buffer);
1155 if (retval == ERROR_OK)
1157 gdb_put_packet(connection, "OK", 2);
1161 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1170 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1178 /* skip command character */
1181 addr = strtoul(packet, &separator, 16);
1183 if (*separator != ',')
1185 ERROR("incomplete write memory binary packet received, dropping connection");
1186 return ERROR_SERVER_REMOTE_CLOSED;
1189 len = strtoul(separator+1, &separator, 16);
1191 if (*(separator++) != ':')
1193 ERROR("incomplete write memory binary packet received, dropping connection");
1194 return ERROR_SERVER_REMOTE_CLOSED;
1200 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1202 retval = target_write_buffer(target, addr, len, (u8*)separator);
1205 if (retval == ERROR_OK)
1207 gdb_put_packet(connection, "OK", 2);
1211 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1218 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1225 if (packet_size > 1)
1227 packet[packet_size] = 0;
1228 address = strtoul(packet + 1, NULL, 16);
1235 if (packet[0] == 'c')
1238 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1240 else if (packet[0] == 's')
1243 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1247 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1251 case ERROR_TARGET_NOT_HALTED:
1252 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1253 return ERROR_SERVER_REMOTE_CLOSED;
1255 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1256 gdb_send_error(connection, EBUSY);
1259 ERROR("BUG: unexpected error %i", retval);
1266 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1269 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1270 enum watchpoint_rw wp_type;
1278 type = strtoul(packet + 1, &separator, 16);
1280 if (type == 0) /* memory breakpoint */
1281 bp_type = BKPT_SOFT;
1282 else if (type == 1) /* hardware breakpoint */
1283 bp_type = BKPT_HARD;
1284 else if (type == 2) /* write watchpoint */
1285 wp_type = WPT_WRITE;
1286 else if (type == 3) /* read watchpoint */
1288 else if (type == 4) /* access watchpoint */
1289 wp_type = WPT_ACCESS;
1291 if (*separator != ',')
1293 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1294 return ERROR_SERVER_REMOTE_CLOSED;
1297 address = strtoul(separator+1, &separator, 16);
1299 if (*separator != ',')
1301 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1302 return ERROR_SERVER_REMOTE_CLOSED;
1305 size = strtoul(separator+1, &separator, 16);
1311 if (packet[0] == 'Z')
1313 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1315 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1320 gdb_put_packet(connection, "OK", 2);
1325 breakpoint_remove(target, address);
1326 gdb_put_packet(connection, "OK", 2);
1333 if (packet[0] == 'Z')
1335 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1337 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1342 gdb_put_packet(connection, "OK", 2);
1347 watchpoint_remove(target, address);
1348 gdb_put_packet(connection, "OK", 2);
1359 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1360 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1362 if (*retval != ERROR_OK)
1370 if ((*xml == NULL) || (!first))
1372 /* start by 0 to exercise all the code paths.
1373 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1375 *size = *size * 2 + 2;
1377 *xml = realloc(*xml, *size);
1382 *retval = ERROR_SERVER_REMOTE_CLOSED;
1390 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1392 if ((ret > 0) && ((ret + 1) < *size - *pos))
1397 /* there was just enough or not enough space, allocate more. */
1402 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1406 /* Extract and NUL-terminate the annex. */
1408 while (*buf && *buf != ':')
1414 /* After the read marker and annex, qXfer looks like a
1415 * traditional 'm' packet. */
1417 *ofs = strtoul(buf, &separator, 16);
1419 if (*separator != ',')
1422 *len = strtoul(separator+1, NULL, 16);
1427 int gdb_calc_blocksize(flash_bank_t *bank)
1430 int block_size = 0xffffffff;
1432 /* loop through all sectors and return smallest sector size */
1434 for (i = 0; i < bank->num_sectors; i++)
1436 if (bank->sectors[i].size < block_size)
1437 block_size = bank->sectors[i].size;
1443 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1445 command_context_t *cmd_ctx = connection->cmd_ctx;
1447 if (strstr(packet, "qRcmd,"))
1449 if (packet_size > 6)
1453 cmd = malloc((packet_size - 6)/2 + 1);
1454 for (i=0; i < (packet_size - 6)/2; i++)
1457 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1460 cmd[(packet_size - 6)/2] = 0x0;
1462 /* We want to print all debug output to GDB connection */
1463 log_add_callback(gdb_log_callback, connection);
1464 target_call_timer_callbacks();
1465 command_run_line(cmd_ctx, cmd);
1466 log_remove_callback(gdb_log_callback, connection);
1469 gdb_put_packet(connection, "OK", 2);
1472 else if (strstr(packet, "qCRC:"))
1474 if (packet_size > 5)
1483 /* skip command character */
1486 addr = strtoul(packet, &separator, 16);
1488 if (*separator != ',')
1490 ERROR("incomplete read memory packet received, dropping connection");
1491 return ERROR_SERVER_REMOTE_CLOSED;
1494 len = strtoul(separator + 1, NULL, 16);
1496 retval = target_checksum_memory(target, addr, len, &checksum);
1498 if (retval == ERROR_OK)
1500 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1501 gdb_put_packet(connection, gdb_reply, 9);
1505 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1512 else if (strstr(packet, "qSupported"))
1514 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1515 * disable qXfer:features:read for the moment */
1516 int retval = ERROR_OK;
1517 char *buffer = NULL;
1521 xml_printf(&retval, &buffer, &pos, &size,
1522 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1523 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1525 if (retval != ERROR_OK)
1527 gdb_send_error(connection, 01);
1531 gdb_put_packet(connection, buffer, strlen(buffer));
1536 else if (strstr(packet, "qXfer:memory-map:read::"))
1538 /* We get away with only specifying flash here. Regions that are not
1539 * specified are treated as if we provided no memory map(if not we
1540 * could detect the holes and mark them as RAM).
1541 * Normally we only execute this code once, but no big deal if we
1542 * have to regenerate it a couple of times. */
1548 int retval = ERROR_OK;
1555 /* skip command character */
1558 offset = strtoul(packet, &separator, 16);
1559 length = strtoul(separator + 1, &separator, 16);
1561 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1564 for (i=0; i<flash_get_bank_count(); i++)
1566 p = get_flash_bank_by_num(i);
1570 /* if device has uneven sector sizes, eg. str7, lpc
1571 * we pass the smallest sector size to gdb memory map */
1572 blocksize = gdb_calc_blocksize(p);
1574 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1575 "<property name=\"blocksize\">0x%x</property>\n" \
1577 p->base, p->size, blocksize);
1580 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1582 if (retval != ERROR_OK)
1584 gdb_send_error(connection, retval);
1588 if (offset + length > pos)
1590 length = pos - offset;
1593 char *t = malloc(length + 1);
1595 memcpy(t + 1, xml + offset, length);
1596 gdb_put_packet(connection, t, length + 1);
1602 else if (strstr(packet, "qXfer:features:read:"))
1607 int retval = ERROR_OK;
1610 unsigned int length;
1613 /* skip command character */
1616 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1618 gdb_send_error(connection, 01);
1622 if (strcmp(annex, "target.xml") != 0)
1624 gdb_send_error(connection, 01);
1628 xml_printf(&retval, &xml, &pos, &size, \
1629 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1631 if (retval != ERROR_OK)
1633 gdb_send_error(connection, retval);
1637 gdb_put_packet(connection, xml, strlen(xml) + 1);
1643 gdb_put_packet(connection, "", 0);
1647 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1649 gdb_connection_t *gdb_connection = connection->priv;
1650 gdb_service_t *gdb_service = connection->service->priv;
1653 /* if flash programming disabled - send a empty reply */
1655 if (gdb_flash_program == 0)
1657 gdb_put_packet(connection, "", 0);
1661 if (strstr(packet, "vFlashErase:"))
1664 unsigned long length;
1666 char *parse = packet + 12;
1669 ERROR("incomplete vFlashErase packet received, dropping connection");
1670 return ERROR_SERVER_REMOTE_CLOSED;
1673 addr = strtoul(parse, &parse, 16);
1675 if (*(parse++) != ',' || *parse == '\0')
1677 ERROR("incomplete vFlashErase packet received, dropping connection");
1678 return ERROR_SERVER_REMOTE_CLOSED;
1681 length = strtoul(parse, &parse, 16);
1685 ERROR("incomplete vFlashErase packet received, dropping connection");
1686 return ERROR_SERVER_REMOTE_CLOSED;
1689 /* assume all sectors need erasing - stops any problems
1690 * when flash_write is called multiple times */
1693 /* perform any target specific operations before the erase */
1694 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1697 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1699 /* GDB doesn't evaluate the actual error number returned,
1700 * treat a failed erase as an I/O error
1702 gdb_send_error(connection, EIO);
1703 ERROR("flash_erase returned %i", result);
1706 gdb_put_packet(connection, "OK", 2);
1711 if (strstr(packet, "vFlashWrite:"))
1714 unsigned long length;
1715 char *parse = packet + 12;
1719 ERROR("incomplete vFlashErase packet received, dropping connection");
1720 return ERROR_SERVER_REMOTE_CLOSED;
1722 addr = strtoul(parse, &parse, 16);
1723 if (*(parse++) != ':')
1725 ERROR("incomplete vFlashErase packet received, dropping connection");
1726 return ERROR_SERVER_REMOTE_CLOSED;
1728 length = packet_size - (parse - packet);
1730 /* create a new image if there isn't already one */
1731 if (gdb_connection->vflash_image == NULL)
1733 gdb_connection->vflash_image = malloc(sizeof(image_t));
1734 image_open(gdb_connection->vflash_image, "", "build");
1737 /* create new section with content from packet buffer */
1738 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1740 gdb_put_packet(connection, "OK", 2);
1745 if (!strcmp(packet, "vFlashDone"))
1749 /* process the flashing buffer. No need to erase as GDB
1750 * always issues a vFlashErase first. */
1751 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK)
1753 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1754 gdb_put_packet(connection, "E.memtype", 9);
1756 gdb_send_error(connection, EIO);
1760 DEBUG("wrote %u bytes from vFlash image to flash", written);
1761 gdb_put_packet(connection, "OK", 2);
1764 image_close(gdb_connection->vflash_image);
1765 free(gdb_connection->vflash_image);
1766 gdb_connection->vflash_image = NULL;
1771 gdb_put_packet(connection, "", 0);
1775 int gdb_detach(connection_t *connection, target_t *target)
1777 switch( detach_mode )
1779 case GDB_DETACH_RESUME:
1780 target->type->resume(target, 1, 0, 1, 0);
1783 case GDB_DETACH_RESET:
1784 target_process_reset(connection->cmd_ctx);
1787 case GDB_DETACH_HALT:
1788 target->type->halt(target);
1791 case GDB_DETACH_NOTHING:
1795 gdb_put_packet(connection, "OK", 2);
1800 static void gdb_log_callback(void *priv, const char *file, int line,
1801 const char *function, const char *string)
1803 connection_t *connection = priv;
1804 gdb_connection_t *gdb_con = connection->priv;
1808 /* do not reply this using the O packet */
1812 gdb_output_con(connection, string);
1815 int gdb_input_inner(connection_t *connection)
1817 gdb_service_t *gdb_service = connection->service->priv;
1818 target_t *target = gdb_service->target;
1819 char packet[GDB_BUFFER_SIZE];
1822 gdb_connection_t *gdb_con = connection->priv;
1823 static int extended_protocol = 0;
1825 /* drain input buffer */
1828 packet_size = GDB_BUFFER_SIZE-1;
1829 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1834 /* terminate with zero */
1835 packet[packet_size] = 0;
1837 DEBUG("received packet: '%s'", packet);
1839 if (packet_size > 0)
1845 /* Hct... -- set thread
1846 * we don't have threads, send empty reply */
1847 gdb_put_packet(connection, NULL, 0);
1850 retval = gdb_query_packet(connection, target, packet, packet_size);
1853 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1856 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1859 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1862 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1865 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1868 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1872 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1875 gdb_last_signal_packet(connection, target, packet, packet_size);
1880 /* We're running/stepping, in which case we can
1881 * forward log output until the target is halted */
1882 gdb_connection_t *gdb_con = connection->priv;
1883 gdb_con->frontend_state = TARGET_RUNNING;
1884 log_add_callback(gdb_log_callback, connection);
1885 gdb_step_continue_packet(connection, target, packet, packet_size);
1889 retval = gdb_v_packet(connection, target, packet, packet_size);
1892 retval = gdb_detach(connection, target);
1893 extended_protocol = 0;
1896 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1900 if (extended_protocol != 0)
1902 gdb_put_packet(connection, "OK", 2);
1903 return ERROR_SERVER_REMOTE_CLOSED;
1905 /* handle extended remote protocol */
1906 extended_protocol = 1;
1907 gdb_put_packet(connection, "OK", 2);
1910 /* handle extended restart packet */
1911 target_process_reset(connection->cmd_ctx);
1914 /* ignore unkown packets */
1915 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1916 gdb_put_packet(connection, NULL, 0);
1920 /* if a packet handler returned an error, exit input loop */
1921 if (retval != ERROR_OK)
1925 if (gdb_con->ctrl_c)
1927 if (target->state == TARGET_RUNNING)
1929 target->type->halt(target);
1930 gdb_con->ctrl_c = 0;
1934 } while (gdb_con->buf_cnt > 0);
1939 int gdb_input(connection_t *connection)
1941 int retval = gdb_input_inner(connection);
1942 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1944 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1950 gdb_service_t *gdb_service;
1951 target_t *target = targets;
1956 WARNING("no gdb ports allocated as no target has been specified");
1962 WARNING("no gdb port specified, using default port 3333");
1968 char service_name[8];
1970 snprintf(service_name, 8, "gdb-%2.2i", i);
1972 gdb_service = malloc(sizeof(gdb_service_t));
1973 gdb_service->target = target;
1975 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1977 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1980 target = target->next;
1986 /* daemon configuration command gdb_port */
1987 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1992 /* only if the port wasn't overwritten by cmdline */
1994 gdb_port = strtoul(args[0], NULL, 0);
1999 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2003 if (strcmp(args[0], "resume") == 0)
2005 detach_mode = GDB_DETACH_RESUME;
2008 else if (strcmp(args[0], "reset") == 0)
2010 detach_mode = GDB_DETACH_RESET;
2013 else if (strcmp(args[0], "halt") == 0)
2015 detach_mode = GDB_DETACH_HALT;
2018 else if (strcmp(args[0], "nothing") == 0)
2020 detach_mode = GDB_DETACH_NOTHING;
2025 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2029 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2033 if (strcmp(args[0], "enable") == 0)
2035 gdb_use_memory_map = 1;
2038 else if (strcmp(args[0], "disable") == 0)
2040 gdb_use_memory_map = 0;
2045 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2049 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2053 if (strcmp(args[0], "enable") == 0)
2055 gdb_flash_program = 1;
2058 else if (strcmp(args[0], "disable") == 0)
2060 gdb_flash_program = 0;
2065 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2069 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2073 if (strcmp(args[0], "enable") == 0)
2075 gdb_report_data_abort = 1;
2078 else if (strcmp(args[0], "disable") == 0)
2080 gdb_report_data_abort = 0;
2085 WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2089 int gdb_register_commands(command_context_t *command_context)
2091 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2092 COMMAND_CONFIG, "");
2093 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2094 COMMAND_CONFIG, "");
2095 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2096 COMMAND_CONFIG, "");
2097 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2098 COMMAND_CONFIG, "");
2099 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2100 COMMAND_CONFIG, "");