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 extern int gdb_error(connection_t *connection, int retval);
47 static unsigned short gdb_port;
48 static const char *DIGITS = "0123456789abcdef";
50 static void gdb_log_callback(void *priv, const char *file, int line,
51 const char *function, const char *string);
61 /* target behaviour on gdb detach */
62 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
64 /* set if we are sending a memory map to gdb
65 * via qXfer:memory-map:read packet */
66 int gdb_use_memory_map = 0;
67 int gdb_flash_program = 0;
69 /* if set, data aborts cause an error to be reported in memory read packets
70 * see the code in gdb_read_memory_packet() for further explanations */
71 int gdb_report_data_abort = 0;
73 int gdb_last_signal(target_t *target)
75 switch (target->debug_reason)
77 case DBG_REASON_DBGRQ:
78 return 0x2; /* SIGINT */
79 case DBG_REASON_BREAKPOINT:
80 case DBG_REASON_WATCHPOINT:
81 case DBG_REASON_WPTANDBKPT:
82 return 0x05; /* SIGTRAP */
83 case DBG_REASON_SINGLESTEP:
84 return 0x05; /* SIGTRAP */
85 case DBG_REASON_NOTHALTED:
86 return 0x0; /* no signal... shouldn't happen */
88 USER("undefined debug reason %d - target needs reset", target->debug_reason);
94 int check_pending(connection_t *connection, int timeout_s, int *got_data)
96 /* a non-blocking socket will block if there is 0 bytes available on the socket,
97 * but return with as many bytes as are available immediately
101 gdb_connection_t *gdb_con = connection->priv;
107 if (gdb_con->buf_cnt>0)
114 FD_SET(connection->fd, &read_fds);
116 tv.tv_sec = timeout_s;
118 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
120 /* This can typically be because a "monitor" command took too long
121 * before printing any progress messages
125 return ERROR_GDB_TIMEOUT;
131 *got_data=FD_ISSET(connection->fd, &read_fds)!=0;
136 int gdb_get_char(connection_t *connection, int* next_char)
138 gdb_connection_t *gdb_con = connection->priv;
141 #ifdef _DEBUG_GDB_IO_
145 if (gdb_con->buf_cnt-- > 0)
147 *next_char = *(gdb_con->buf_p++);
148 if (gdb_con->buf_cnt > 0)
149 connection->input_pending = 1;
151 connection->input_pending = 0;
153 #ifdef _DEBUG_GDB_IO_
154 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
163 retval=check_pending(connection, 1, NULL);
164 if (retval!=ERROR_OK)
167 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
168 if (gdb_con->buf_cnt > 0)
172 if (gdb_con->buf_cnt == 0)
175 return ERROR_SERVER_REMOTE_CLOSED;
179 errno = WSAGetLastError();
186 case WSAECONNABORTED:
188 return ERROR_SERVER_REMOTE_CLOSED;
191 return ERROR_SERVER_REMOTE_CLOSED;
193 ERROR("read: %d", errno);
204 return ERROR_SERVER_REMOTE_CLOSED;
207 return ERROR_SERVER_REMOTE_CLOSED;
209 ERROR("read: %s", strerror(errno));
211 return ERROR_SERVER_REMOTE_CLOSED;
216 #ifdef _DEBUG_GDB_IO_
217 debug_buffer = malloc(gdb_con->buf_cnt + 1);
218 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
219 debug_buffer[gdb_con->buf_cnt] = 0;
220 DEBUG("received '%s'", debug_buffer);
224 gdb_con->buf_p = gdb_con->buffer;
226 *next_char = *(gdb_con->buf_p++);
227 if (gdb_con->buf_cnt > 0)
228 connection->input_pending = 1;
230 connection->input_pending = 0;
231 #ifdef _DEBUG_GDB_IO_
232 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
238 int gdb_putback_char(connection_t *connection, int last_char)
240 gdb_connection_t *gdb_con = connection->priv;
242 if (gdb_con->buf_p > gdb_con->buffer)
244 *(--gdb_con->buf_p) = last_char;
249 ERROR("BUG: couldn't put character back");
255 /* The only way we can detect that the socket is closed is the first time
256 * we write to it, we will fail. Subsequent write operations will
257 * succeed. Shudder! */
258 int gdb_write(connection_t *connection, void *data, int len)
260 gdb_connection_t *gdb_con = connection->priv;
262 return ERROR_SERVER_REMOTE_CLOSED;
264 if (write_socket(connection->fd, data, len) == len)
269 return ERROR_SERVER_REMOTE_CLOSED;
272 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
275 unsigned char my_checksum = 0;
276 #ifdef _DEBUG_GDB_IO_
281 gdb_connection_t *gdb_con = connection->priv;
283 for (i = 0; i < len; i++)
284 my_checksum += buffer[i];
286 #ifdef _DEBUG_GDB_IO_
288 * At this point we should have nothing in the input queue from GDB,
289 * however sometimes '-' is sent even though we've already received
290 * an ACK (+) for everything we've sent off.
296 if ((retval=check_pending(connection, 0, &gotdata))!=ERROR_OK)
300 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
302 WARNING("Discard unexpected char %c", reply);
309 #ifdef _DEBUG_GDB_IO_
310 debug_buffer = malloc(len + 1);
311 memcpy(debug_buffer, buffer, len);
312 debug_buffer[len] = 0;
313 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
317 void *allocated = NULL;
318 char stackAlloc[1024];
319 char *t = stackAlloc;
320 int totalLen = 1 + len + 1 + 2;
321 if (totalLen > sizeof(stackAlloc))
323 allocated = malloc(totalLen);
325 if (allocated == NULL)
327 ERROR("Ran out of memory trying to reply packet %d\n", totalLen);
332 memcpy(t + 1, buffer, len);
334 t[1 + len + 1] = DIGITS[(my_checksum >> 4) & 0xf];
335 t[1 + len + 2] = DIGITS[my_checksum & 0xf];
337 gdb_write(connection, t, totalLen);
344 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
349 else if (reply == '-')
351 /* Stop sending output packets for now */
352 log_remove_callback(gdb_log_callback, connection);
353 WARNING("negative reply, retrying");
355 else if (reply == 0x3)
358 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
362 else if (reply == '-')
364 /* Stop sending output packets for now */
365 log_remove_callback(gdb_log_callback, connection);
366 WARNING("negative reply, retrying");
370 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
372 return ERROR_SERVER_REMOTE_CLOSED;
377 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
379 return ERROR_SERVER_REMOTE_CLOSED;
383 return ERROR_SERVER_REMOTE_CLOSED;
388 int gdb_put_packet(connection_t *connection, char *buffer, int len)
390 gdb_connection_t *gdb_con = connection->priv;
392 int retval = gdb_put_packet_inner(connection, buffer, len);
397 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
403 unsigned char my_checksum = 0;
404 gdb_connection_t *gdb_con = connection->priv;
410 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
413 #ifdef _DEBUG_GDB_IO_
414 DEBUG("character: '%c'", character);
422 WARNING("acknowledgment received, but no packet pending");
425 WARNING("negative acknowledgment, but no packet pending");
432 WARNING("ignoring character 0x%x", character);
435 } while (character != '$');
440 gdb_connection_t *gdb_con = connection->priv;
443 /* The common case is that we have an entire packet with no escape chars.
444 * We need to leave at least 2 bytes in the buffer to have
445 * gdb_get_char() update various bits and bobs correctly.
447 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
449 /* The compiler will struggle a bit with constant propagation and
450 * aliasing, so we help it by showing that these values do not
451 * change inside the loop
454 char *buf = gdb_con->buf_p;
455 int run = gdb_con->buf_cnt - 2;
462 if (character == '#')
464 /* Danger! character can be '#' when esc is
465 * used so we need an explicit boolean for done here.
471 if (character == '}')
473 /* data transmitted in binary mode (X packet)
474 * uses 0x7d as escape character */
475 my_checksum += character & 0xff;
478 my_checksum += character & 0xff;
479 buffer[count++] = (character ^ 0x20) & 0xff;
482 my_checksum += character & 0xff;
483 buffer[count++] = character & 0xff;
487 gdb_con->buf_cnt -= i;
493 ERROR("packet buffer too small");
494 return ERROR_GDB_BUFFER_TOO_SMALL;
497 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
500 if (character == '#')
503 if (character == '}')
505 /* data transmitted in binary mode (X packet)
506 * uses 0x7d as escape character */
507 my_checksum += character & 0xff;
508 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
510 my_checksum += character & 0xff;
511 buffer[count++] = (character ^ 0x20) & 0xff;
515 my_checksum += character & 0xff;
516 buffer[count++] = character & 0xff;
523 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
525 checksum[0] = character;
526 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
528 checksum[1] = character;
531 if (my_checksum == strtoul(checksum, NULL, 16))
533 gdb_write(connection, "+", 1);
537 WARNING("checksum error, requesting retransmission");
538 gdb_write(connection, "-", 1);
541 return ERROR_SERVER_REMOTE_CLOSED;
546 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
548 gdb_connection_t *gdb_con = connection->priv;
550 int retval = gdb_get_packet_inner(connection, buffer, len);
555 int gdb_output_con(connection_t *connection, const char* line)
560 bin_size = strlen(line);
562 hex_buffer = malloc(bin_size*2 + 2);
563 if (hex_buffer == NULL)
564 return ERROR_GDB_BUFFER_TOO_SMALL;
567 for (i=0; i<bin_size; i++)
568 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
569 hex_buffer[bin_size*2+1] = 0;
571 gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
577 int gdb_output(struct command_context_s *context, char* line)
579 /* this will be dumped to the log and also sent as an O packet if possible */
584 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
587 struct command_context_s *cmd_ctx = priv;
589 if (target->gdb_program_script)
591 script = open_file_from_path(target->gdb_program_script, "r");
594 ERROR("couldn't open script file %s", target->gdb_program_script);
598 INFO("executing gdb_program script '%s'", target->gdb_program_script);
599 command_run_file(cmd_ctx, script, COMMAND_EXEC);
602 jtag_execute_queue();
608 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
610 connection_t *connection = priv;
611 gdb_connection_t *gdb_connection = connection->priv;
617 case TARGET_EVENT_HALTED:
618 /* In the GDB protocol when we are stepping or coninuing execution,
619 * we have a lingering reply. Upon receiving a halted event
620 * when we have that lingering packet, we reply to the original
621 * step or continue packet.
623 * Executing monitor commands can bring the target in and
624 * out of the running state so we'll see lots of TARGET_EVENT_XXX
625 * that are to be ignored.
627 if (gdb_connection->frontend_state == TARGET_RUNNING)
629 /* stop forwarding log packets! */
630 log_remove_callback(gdb_log_callback, connection);
632 if (gdb_connection->ctrl_c)
635 gdb_connection->ctrl_c = 0;
639 signal = gdb_last_signal(target);
642 snprintf(sig_reply, 4, "T%2.2x", signal);
643 gdb_put_packet(connection, sig_reply, 3);
644 gdb_connection->frontend_state = TARGET_HALTED;
647 case TARGET_EVENT_GDB_PROGRAM:
648 gdb_program_handler(target, event, connection->cmd_ctx);
658 int gdb_new_connection(connection_t *connection)
660 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
661 gdb_service_t *gdb_service = connection->service->priv;
665 connection->priv = gdb_connection;
667 /* initialize gdb connection information */
668 gdb_connection->buf_p = gdb_connection->buffer;
669 gdb_connection->buf_cnt = 0;
670 gdb_connection->ctrl_c = 0;
671 gdb_connection->frontend_state = TARGET_HALTED;
672 gdb_connection->vflash_image = NULL;
673 gdb_connection->closed = 0;
674 gdb_connection->busy = 0;
676 /* send ACK to GDB for debug request */
677 gdb_write(connection, "+", 1);
679 /* output goes through gdb connection */
680 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
682 /* register callback to be informed about target events */
683 target_register_event_callback(gdb_target_callback_event_handler, connection);
685 /* a gdb session just attached, try to put the target in halt mode
686 * or alterantively try to issue a reset.
688 * GDB connection will fail if e.g. register read packets fail,
689 * otherwise resetting/halting the target could have been left to GDB init
692 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
693 (retval != ERROR_TARGET_ALREADY_HALTED))
695 ERROR("error(%d) when trying to halt target, falling back to \"reset\"", retval);
696 command_run_line(connection->cmd_ctx, "reset");
699 /* remove the initial ACK from the incoming buffer */
700 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
703 /* FIX!!!??? would we actually ever receive a + here???
706 if (initial_ack != '+')
707 gdb_putback_char(connection, initial_ack);
712 int gdb_connection_closed(connection_t *connection)
714 gdb_service_t *gdb_service = connection->service->priv;
715 gdb_connection_t *gdb_connection = connection->priv;
717 /* see if an image built with vFlash commands is left */
718 if (gdb_connection->vflash_image)
720 image_close(gdb_connection->vflash_image);
721 free(gdb_connection->vflash_image);
722 gdb_connection->vflash_image = NULL;
725 /* if this connection registered a debug-message receiver delete it */
726 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
728 if (connection->priv)
730 free(connection->priv);
731 connection->priv = NULL;
735 ERROR("BUG: connection->priv == NULL");
738 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
739 log_remove_callback(gdb_log_callback, connection);
744 void gdb_send_error(connection_t *connection, u8 the_error)
747 snprintf(err, 4, "E%2.2X", the_error );
748 gdb_put_packet(connection, err, 3);
751 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
756 signal = gdb_last_signal(target);
758 snprintf(sig_reply, 4, "S%2.2x", signal);
759 gdb_put_packet(connection, sig_reply, 3);
764 /* Convert register to string of bits. NB! The # of bits in the
765 * register might be non-divisible by 8(a byte), in which
766 * case an entire byte is shown. */
767 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
774 buf_len = CEIL(reg->size, 8);
776 if (target->endianness == TARGET_LITTLE_ENDIAN)
778 for (i = 0; i < buf_len; i++)
780 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
781 tstr[i*2+1] = DIGITS[buf[i]&0xf];
786 for (i = 0; i < buf_len; i++)
788 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
789 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
794 void gdb_target_to_str(target_t *target, char *tstr, char *str)
796 int str_len = strlen(tstr);
801 ERROR("BUG: gdb value with uneven number of characters encountered");
805 if (target->endianness == TARGET_LITTLE_ENDIAN)
807 for (i = 0; i < str_len; i+=2)
809 str[str_len - i - 1] = tstr[i + 1];
810 str[str_len - i - 2] = tstr[i];
815 for (i = 0; i < str_len; i++)
822 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
827 int reg_packet_size = 0;
832 #ifdef _DEBUG_GDB_IO_
836 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
838 return gdb_error(connection, retval);
841 for (i = 0; i < reg_list_size; i++)
843 reg_packet_size += reg_list[i]->size;
846 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
847 reg_packet_p = reg_packet;
849 for (i = 0; i < reg_list_size; i++)
851 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
852 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
855 #ifdef _DEBUG_GDB_IO_
858 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
859 DEBUG("reg_packet: %s", reg_packet_p);
864 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
872 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
880 #ifdef _DEBUG_GDB_IO_
884 /* skip command character */
890 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
891 return ERROR_SERVER_REMOTE_CLOSED;
894 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
896 return gdb_error(connection, retval);
900 for (i = 0; i < reg_list_size; i++)
904 reg_arch_type_t *arch_type;
906 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
907 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
908 gdb_target_to_str(target, packet_p, hex_buf);
910 /* convert hex-string to binary buffer */
911 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
912 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
914 /* get register arch_type, and call set method */
915 arch_type = register_get_arch_type(reg_list[i]->arch_type);
916 if (arch_type == NULL)
918 ERROR("BUG: encountered unregistered arch type");
921 arch_type->set(reg_list[i], bin_buf);
923 /* advance packet pointer */
924 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
930 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
933 gdb_put_packet(connection, "OK", 2);
938 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
941 int reg_num = strtoul(packet + 1, NULL, 16);
946 #ifdef _DEBUG_GDB_IO_
950 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
952 return gdb_error(connection, retval);
955 if (reg_list_size <= reg_num)
957 ERROR("gdb requested a non-existing register");
961 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
963 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
965 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
973 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
978 int reg_num = strtoul(packet + 1, &separator, 16);
982 reg_arch_type_t *arch_type;
986 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
988 return gdb_error(connection, retval);
991 if (reg_list_size < reg_num)
993 ERROR("gdb requested a non-existing register");
994 return ERROR_SERVER_REMOTE_CLOSED;
997 if (*separator != '=')
999 ERROR("GDB 'set register packet', but no '=' following the register number");
1000 return ERROR_SERVER_REMOTE_CLOSED;
1003 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1004 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1005 gdb_target_to_str(target, separator + 1, hex_buf);
1007 /* convert hex-string to binary buffer */
1008 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1009 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1011 /* get register arch_type, and call set method */
1012 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1013 if (arch_type == NULL)
1015 ERROR("BUG: encountered unregistered arch type");
1018 arch_type->set(reg_list[reg_num], bin_buf);
1020 gdb_put_packet(connection, "OK", 2);
1029 int gdb_error(connection_t *connection, int retval)
1033 case ERROR_TARGET_DATA_ABORT:
1034 gdb_send_error(connection, EIO);
1036 case ERROR_TARGET_TRANSLATION_FAULT:
1037 gdb_send_error(connection, EFAULT);
1039 case ERROR_TARGET_UNALIGNED_ACCESS:
1040 gdb_send_error(connection, EFAULT);
1042 case ERROR_TARGET_NOT_HALTED:
1043 gdb_send_error(connection, EFAULT);
1046 /* This could be that the target reset itself. */
1047 ERROR("unexpected error %i", retval);
1048 gdb_send_error(connection, EFAULT);
1055 /* We don't have to worry about the default 2 second timeout for GDB packets,
1056 * because GDB breaks up large memory reads into smaller reads.
1058 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1060 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1069 int retval = ERROR_OK;
1071 /* skip command character */
1074 addr = strtoul(packet, &separator, 16);
1076 if (*separator != ',')
1078 ERROR("incomplete read memory packet received, dropping connection");
1079 return ERROR_SERVER_REMOTE_CLOSED;
1082 len = strtoul(separator+1, NULL, 16);
1084 buffer = malloc(len);
1086 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1088 retval = target_read_buffer(target, addr, len, buffer);
1090 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1092 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1093 * At some point this might be fixed in GDB, in which case this code can be removed.
1095 * OpenOCD developers are acutely aware of this problem, but there is nothing
1096 * gained by involving the user in this problem that hopefully will get resolved
1099 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1101 * For now, the default is to fix up things to make current GDB versions work.
1102 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1104 memset(buffer, 0, len);
1108 if (retval == ERROR_OK)
1110 hex_buffer = malloc(len * 2 + 1);
1113 for (i = 0; i < len; i++)
1116 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1117 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1120 gdb_put_packet(connection, hex_buffer, len * 2);
1126 retval = gdb_error(connection, retval);
1134 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1145 /* skip command character */
1148 addr = strtoul(packet, &separator, 16);
1150 if (*separator != ',')
1152 ERROR("incomplete write memory packet received, dropping connection");
1153 return ERROR_SERVER_REMOTE_CLOSED;
1156 len = strtoul(separator+1, &separator, 16);
1158 if (*(separator++) != ':')
1160 ERROR("incomplete write memory packet received, dropping connection");
1161 return ERROR_SERVER_REMOTE_CLOSED;
1164 buffer = malloc(len);
1166 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1168 for (i=0; i<len; i++)
1171 sscanf(separator + 2*i, "%2x", &tmp);
1175 retval = target_write_buffer(target, addr, len, buffer);
1177 if (retval == ERROR_OK)
1179 gdb_put_packet(connection, "OK", 2);
1183 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1192 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1200 /* skip command character */
1203 addr = strtoul(packet, &separator, 16);
1205 if (*separator != ',')
1207 ERROR("incomplete write memory binary packet received, dropping connection");
1208 return ERROR_SERVER_REMOTE_CLOSED;
1211 len = strtoul(separator+1, &separator, 16);
1213 if (*(separator++) != ':')
1215 ERROR("incomplete write memory binary packet received, dropping connection");
1216 return ERROR_SERVER_REMOTE_CLOSED;
1222 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1224 retval = target_write_buffer(target, addr, len, (u8*)separator);
1227 if (retval == ERROR_OK)
1229 gdb_put_packet(connection, "OK", 2);
1233 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1240 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1247 if (packet_size > 1)
1249 packet[packet_size] = 0;
1250 address = strtoul(packet + 1, NULL, 16);
1257 if (packet[0] == 'c')
1260 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1262 else if (packet[0] == 's')
1265 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1269 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1272 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1273 enum watchpoint_rw wp_type;
1281 type = strtoul(packet + 1, &separator, 16);
1283 if (type == 0) /* memory breakpoint */
1284 bp_type = BKPT_SOFT;
1285 else if (type == 1) /* hardware breakpoint */
1286 bp_type = BKPT_HARD;
1287 else if (type == 2) /* write watchpoint */
1288 wp_type = WPT_WRITE;
1289 else if (type == 3) /* read watchpoint */
1291 else if (type == 4) /* access watchpoint */
1292 wp_type = WPT_ACCESS;
1294 if (*separator != ',')
1296 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1297 return ERROR_SERVER_REMOTE_CLOSED;
1300 address = strtoul(separator+1, &separator, 16);
1302 if (*separator != ',')
1304 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1305 return ERROR_SERVER_REMOTE_CLOSED;
1308 size = strtoul(separator+1, &separator, 16);
1314 if (packet[0] == 'Z')
1316 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1318 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1323 gdb_put_packet(connection, "OK", 2);
1328 breakpoint_remove(target, address);
1329 gdb_put_packet(connection, "OK", 2);
1336 if (packet[0] == 'Z')
1338 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1340 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1345 gdb_put_packet(connection, "OK", 2);
1350 watchpoint_remove(target, address);
1351 gdb_put_packet(connection, "OK", 2);
1362 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1363 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1365 if (*retval != ERROR_OK)
1373 if ((*xml == NULL) || (!first))
1375 /* start by 0 to exercise all the code paths.
1376 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1378 *size = *size * 2 + 2;
1380 *xml = realloc(*xml, *size);
1385 *retval = ERROR_SERVER_REMOTE_CLOSED;
1393 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1395 if ((ret > 0) && ((ret + 1) < *size - *pos))
1400 /* there was just enough or not enough space, allocate more. */
1405 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1409 /* Extract and NUL-terminate the annex. */
1411 while (*buf && *buf != ':')
1417 /* After the read marker and annex, qXfer looks like a
1418 * traditional 'm' packet. */
1420 *ofs = strtoul(buf, &separator, 16);
1422 if (*separator != ',')
1425 *len = strtoul(separator+1, NULL, 16);
1430 int gdb_calc_blocksize(flash_bank_t *bank)
1433 int block_size = 0xffffffff;
1435 /* loop through all sectors and return smallest sector size */
1437 for (i = 0; i < bank->num_sectors; i++)
1439 if (bank->sectors[i].size < block_size)
1440 block_size = bank->sectors[i].size;
1446 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1448 command_context_t *cmd_ctx = connection->cmd_ctx;
1450 if (strstr(packet, "qRcmd,"))
1452 if (packet_size > 6)
1456 cmd = malloc((packet_size - 6)/2 + 1);
1457 for (i=0; i < (packet_size - 6)/2; i++)
1460 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1463 cmd[(packet_size - 6)/2] = 0x0;
1465 /* We want to print all debug output to GDB connection */
1466 log_add_callback(gdb_log_callback, connection);
1467 target_call_timer_callbacks();
1468 command_run_line(cmd_ctx, cmd);
1469 log_remove_callback(gdb_log_callback, connection);
1472 gdb_put_packet(connection, "OK", 2);
1475 else if (strstr(packet, "qCRC:"))
1477 if (packet_size > 5)
1486 /* skip command character */
1489 addr = strtoul(packet, &separator, 16);
1491 if (*separator != ',')
1493 ERROR("incomplete read memory packet received, dropping connection");
1494 return ERROR_SERVER_REMOTE_CLOSED;
1497 len = strtoul(separator + 1, NULL, 16);
1499 retval = target_checksum_memory(target, addr, len, &checksum);
1501 if (retval == ERROR_OK)
1503 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1504 gdb_put_packet(connection, gdb_reply, 9);
1508 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1515 else if (strstr(packet, "qSupported"))
1517 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1518 * disable qXfer:features:read for the moment */
1519 int retval = ERROR_OK;
1520 char *buffer = NULL;
1524 xml_printf(&retval, &buffer, &pos, &size,
1525 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1526 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1528 if (retval != ERROR_OK)
1530 gdb_send_error(connection, 01);
1534 gdb_put_packet(connection, buffer, strlen(buffer));
1539 else if (strstr(packet, "qXfer:memory-map:read::"))
1541 /* We get away with only specifying flash here. Regions that are not
1542 * specified are treated as if we provided no memory map(if not we
1543 * could detect the holes and mark them as RAM).
1544 * Normally we only execute this code once, but no big deal if we
1545 * have to regenerate it a couple of times. */
1551 int retval = ERROR_OK;
1558 /* skip command character */
1561 offset = strtoul(packet, &separator, 16);
1562 length = strtoul(separator + 1, &separator, 16);
1564 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1567 for (i=0; i<flash_get_bank_count(); i++)
1569 p = get_flash_bank_by_num(i);
1573 /* if device has uneven sector sizes, eg. str7, lpc
1574 * we pass the smallest sector size to gdb memory map */
1575 blocksize = gdb_calc_blocksize(p);
1577 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1578 "<property name=\"blocksize\">0x%x</property>\n" \
1580 p->base, p->size, blocksize);
1583 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1585 if (retval != ERROR_OK)
1587 gdb_send_error(connection, retval);
1591 if (offset + length > pos)
1593 length = pos - offset;
1596 char *t = malloc(length + 1);
1598 memcpy(t + 1, xml + offset, length);
1599 gdb_put_packet(connection, t, length + 1);
1605 else if (strstr(packet, "qXfer:features:read:"))
1610 int retval = ERROR_OK;
1613 unsigned int length;
1616 /* skip command character */
1619 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1621 gdb_send_error(connection, 01);
1625 if (strcmp(annex, "target.xml") != 0)
1627 gdb_send_error(connection, 01);
1631 xml_printf(&retval, &xml, &pos, &size, \
1632 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1634 if (retval != ERROR_OK)
1636 gdb_send_error(connection, retval);
1640 gdb_put_packet(connection, xml, strlen(xml) + 1);
1646 gdb_put_packet(connection, "", 0);
1650 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1652 gdb_connection_t *gdb_connection = connection->priv;
1653 gdb_service_t *gdb_service = connection->service->priv;
1656 /* if flash programming disabled - send a empty reply */
1658 if (gdb_flash_program == 0)
1660 gdb_put_packet(connection, "", 0);
1664 if (strstr(packet, "vFlashErase:"))
1667 unsigned long length;
1669 char *parse = packet + 12;
1672 ERROR("incomplete vFlashErase packet received, dropping connection");
1673 return ERROR_SERVER_REMOTE_CLOSED;
1676 addr = strtoul(parse, &parse, 16);
1678 if (*(parse++) != ',' || *parse == '\0')
1680 ERROR("incomplete vFlashErase packet received, dropping connection");
1681 return ERROR_SERVER_REMOTE_CLOSED;
1684 length = strtoul(parse, &parse, 16);
1688 ERROR("incomplete vFlashErase packet received, dropping connection");
1689 return ERROR_SERVER_REMOTE_CLOSED;
1692 /* assume all sectors need erasing - stops any problems
1693 * when flash_write is called multiple times */
1696 /* perform any target specific operations before the erase */
1697 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1700 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1702 /* GDB doesn't evaluate the actual error number returned,
1703 * treat a failed erase as an I/O error
1705 gdb_send_error(connection, EIO);
1706 ERROR("flash_erase returned %i", result);
1709 gdb_put_packet(connection, "OK", 2);
1714 if (strstr(packet, "vFlashWrite:"))
1717 unsigned long length;
1718 char *parse = packet + 12;
1722 ERROR("incomplete vFlashErase packet received, dropping connection");
1723 return ERROR_SERVER_REMOTE_CLOSED;
1725 addr = strtoul(parse, &parse, 16);
1726 if (*(parse++) != ':')
1728 ERROR("incomplete vFlashErase packet received, dropping connection");
1729 return ERROR_SERVER_REMOTE_CLOSED;
1731 length = packet_size - (parse - packet);
1733 /* create a new image if there isn't already one */
1734 if (gdb_connection->vflash_image == NULL)
1736 gdb_connection->vflash_image = malloc(sizeof(image_t));
1737 image_open(gdb_connection->vflash_image, "", "build");
1740 /* create new section with content from packet buffer */
1741 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1743 gdb_put_packet(connection, "OK", 2);
1748 if (!strcmp(packet, "vFlashDone"))
1752 /* process the flashing buffer. No need to erase as GDB
1753 * always issues a vFlashErase first. */
1754 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK)
1756 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1757 gdb_put_packet(connection, "E.memtype", 9);
1759 gdb_send_error(connection, EIO);
1763 DEBUG("wrote %u bytes from vFlash image to flash", written);
1764 gdb_put_packet(connection, "OK", 2);
1767 image_close(gdb_connection->vflash_image);
1768 free(gdb_connection->vflash_image);
1769 gdb_connection->vflash_image = NULL;
1774 gdb_put_packet(connection, "", 0);
1778 int gdb_detach(connection_t *connection, target_t *target)
1780 switch( detach_mode )
1782 case GDB_DETACH_RESUME:
1783 target->type->resume(target, 1, 0, 1, 0);
1786 case GDB_DETACH_RESET:
1787 target_process_reset(connection->cmd_ctx);
1790 case GDB_DETACH_HALT:
1791 target->type->halt(target);
1794 case GDB_DETACH_NOTHING:
1798 gdb_put_packet(connection, "OK", 2);
1803 static void gdb_log_callback(void *priv, const char *file, int line,
1804 const char *function, const char *string)
1806 connection_t *connection = priv;
1807 gdb_connection_t *gdb_con = connection->priv;
1811 /* do not reply this using the O packet */
1815 gdb_output_con(connection, string);
1818 int gdb_input_inner(connection_t *connection)
1820 gdb_service_t *gdb_service = connection->service->priv;
1821 target_t *target = gdb_service->target;
1822 char packet[GDB_BUFFER_SIZE];
1825 gdb_connection_t *gdb_con = connection->priv;
1826 static int extended_protocol = 0;
1828 /* drain input buffer */
1831 packet_size = GDB_BUFFER_SIZE-1;
1832 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1837 /* terminate with zero */
1838 packet[packet_size] = 0;
1840 DEBUG("received packet: '%s'", packet);
1842 if (packet_size > 0)
1848 /* Hct... -- set thread
1849 * we don't have threads, send empty reply */
1850 gdb_put_packet(connection, NULL, 0);
1853 retval = gdb_query_packet(connection, target, packet, packet_size);
1856 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1859 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1862 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1865 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1868 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1871 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1875 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1878 gdb_last_signal_packet(connection, target, packet, packet_size);
1883 /* We're running/stepping, in which case we can
1884 * forward log output until the target is halted */
1885 gdb_connection_t *gdb_con = connection->priv;
1886 gdb_con->frontend_state = TARGET_RUNNING;
1887 log_add_callback(gdb_log_callback, connection);
1888 gdb_step_continue_packet(connection, target, packet, packet_size);
1892 retval = gdb_v_packet(connection, target, packet, packet_size);
1895 retval = gdb_detach(connection, target);
1896 extended_protocol = 0;
1899 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1903 if (extended_protocol != 0)
1905 gdb_put_packet(connection, "OK", 2);
1906 return ERROR_SERVER_REMOTE_CLOSED;
1908 /* handle extended remote protocol */
1909 extended_protocol = 1;
1910 gdb_put_packet(connection, "OK", 2);
1913 /* handle extended restart packet */
1914 target_process_reset(connection->cmd_ctx);
1917 /* ignore unkown packets */
1918 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1919 gdb_put_packet(connection, NULL, 0);
1923 /* if a packet handler returned an error, exit input loop */
1924 if (retval != ERROR_OK)
1928 if (gdb_con->ctrl_c)
1930 if (target->state == TARGET_RUNNING)
1932 target->type->halt(target);
1933 gdb_con->ctrl_c = 0;
1937 } while (gdb_con->buf_cnt > 0);
1942 int gdb_input(connection_t *connection)
1944 int retval = gdb_input_inner(connection);
1945 gdb_connection_t *gdb_con = connection->priv;
1946 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1949 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
1950 if (gdb_con->closed)
1951 return ERROR_SERVER_REMOTE_CLOSED;
1953 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1959 gdb_service_t *gdb_service;
1960 target_t *target = targets;
1965 WARNING("no gdb ports allocated as no target has been specified");
1971 WARNING("no gdb port specified, using default port 3333");
1977 char service_name[8];
1979 snprintf(service_name, 8, "gdb-%2.2i", i);
1981 gdb_service = malloc(sizeof(gdb_service_t));
1982 gdb_service->target = target;
1984 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1986 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1989 target = target->next;
1995 /* daemon configuration command gdb_port */
1996 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2001 /* only if the port wasn't overwritten by cmdline */
2003 gdb_port = strtoul(args[0], NULL, 0);
2008 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2012 if (strcmp(args[0], "resume") == 0)
2014 detach_mode = GDB_DETACH_RESUME;
2017 else if (strcmp(args[0], "reset") == 0)
2019 detach_mode = GDB_DETACH_RESET;
2022 else if (strcmp(args[0], "halt") == 0)
2024 detach_mode = GDB_DETACH_HALT;
2027 else if (strcmp(args[0], "nothing") == 0)
2029 detach_mode = GDB_DETACH_NOTHING;
2034 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2038 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2042 if (strcmp(args[0], "enable") == 0)
2044 gdb_use_memory_map = 1;
2047 else if (strcmp(args[0], "disable") == 0)
2049 gdb_use_memory_map = 0;
2054 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2058 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2062 if (strcmp(args[0], "enable") == 0)
2064 gdb_flash_program = 1;
2067 else if (strcmp(args[0], "disable") == 0)
2069 gdb_flash_program = 0;
2074 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2078 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2082 if (strcmp(args[0], "enable") == 0)
2084 gdb_report_data_abort = 1;
2087 else if (strcmp(args[0], "disable") == 0)
2089 gdb_report_data_abort = 0;
2094 WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2098 int gdb_register_commands(command_context_t *command_context)
2100 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2101 COMMAND_CONFIG, "");
2102 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2103 COMMAND_CONFIG, "");
2104 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2105 COMMAND_CONFIG, "");
2106 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2107 COMMAND_CONFIG, "");
2108 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2109 COMMAND_CONFIG, "");