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 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
93 int check_pending(connection_t *connection, int timeout_s, int *got_data)
95 /* a non-blocking socket will block if there is 0 bytes available on the socket,
96 * but return with as many bytes as are available immediately
100 gdb_connection_t *gdb_con = connection->priv;
106 if (gdb_con->buf_cnt>0)
113 FD_SET(connection->fd, &read_fds);
115 tv.tv_sec = timeout_s;
117 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
119 /* This can typically be because a "monitor" command took too long
120 * before printing any progress messages
124 return ERROR_GDB_TIMEOUT;
130 *got_data=FD_ISSET(connection->fd, &read_fds)!=0;
134 int gdb_get_char(connection_t *connection, int* next_char)
136 gdb_connection_t *gdb_con = connection->priv;
139 #ifdef _DEBUG_GDB_IO_
143 if (gdb_con->buf_cnt-- > 0)
145 *next_char = *(gdb_con->buf_p++);
146 if (gdb_con->buf_cnt > 0)
147 connection->input_pending = 1;
149 connection->input_pending = 0;
151 #ifdef _DEBUG_GDB_IO_
152 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
160 retval=check_pending(connection, 1, NULL);
161 if (retval!=ERROR_OK)
163 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
164 if (gdb_con->buf_cnt > 0)
168 if (gdb_con->buf_cnt == 0)
171 return ERROR_SERVER_REMOTE_CLOSED;
175 errno = WSAGetLastError();
182 case WSAECONNABORTED:
184 return ERROR_SERVER_REMOTE_CLOSED;
187 return ERROR_SERVER_REMOTE_CLOSED;
189 LOG_ERROR("read: %d", errno);
200 return ERROR_SERVER_REMOTE_CLOSED;
203 return ERROR_SERVER_REMOTE_CLOSED;
205 LOG_ERROR("read: %s", strerror(errno));
207 return ERROR_SERVER_REMOTE_CLOSED;
212 #ifdef _DEBUG_GDB_IO_
213 debug_buffer = malloc(gdb_con->buf_cnt + 1);
214 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
215 debug_buffer[gdb_con->buf_cnt] = 0;
216 LOG_DEBUG("received '%s'", debug_buffer);
220 gdb_con->buf_p = gdb_con->buffer;
222 *next_char = *(gdb_con->buf_p++);
223 if (gdb_con->buf_cnt > 0)
224 connection->input_pending = 1;
226 connection->input_pending = 0;
227 #ifdef _DEBUG_GDB_IO_
228 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
234 int gdb_putback_char(connection_t *connection, int last_char)
236 gdb_connection_t *gdb_con = connection->priv;
238 if (gdb_con->buf_p > gdb_con->buffer)
240 *(--gdb_con->buf_p) = last_char;
245 LOG_ERROR("BUG: couldn't put character back");
251 /* The only way we can detect that the socket is closed is the first time
252 * we write to it, we will fail. Subsequent write operations will
253 * succeed. Shudder! */
254 int gdb_write(connection_t *connection, void *data, int len)
256 gdb_connection_t *gdb_con = connection->priv;
258 return ERROR_SERVER_REMOTE_CLOSED;
260 if (write_socket(connection->fd, data, len) == len)
265 return ERROR_SERVER_REMOTE_CLOSED;
268 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
271 unsigned char my_checksum = 0;
272 #ifdef _DEBUG_GDB_IO_
277 gdb_connection_t *gdb_con = connection->priv;
279 for (i = 0; i < len; i++)
280 my_checksum += buffer[i];
282 #ifdef _DEBUG_GDB_IO_
284 * At this point we should have nothing in the input queue from GDB,
285 * however sometimes '-' is sent even though we've already received
286 * an ACK (+) for everything we've sent off.
292 if ((retval=check_pending(connection, 0, &gotdata))!=ERROR_OK)
296 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
298 LOG_WARNING("Discard unexpected char %c", reply);
305 #ifdef _DEBUG_GDB_IO_
306 debug_buffer = malloc(len + 1);
307 memcpy(debug_buffer, buffer, len);
308 debug_buffer[len] = 0;
309 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
313 char local_buffer[1024];
314 local_buffer[0] = '$';
315 if (len+4 <= sizeof(local_buffer))
317 /* performance gain on smaller packets by only a single call to gdb_write() */
318 memcpy(local_buffer+1, buffer, len++);
319 local_buffer[len++] = '#';
320 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
321 local_buffer[len++] = DIGITS[my_checksum & 0xf];
322 gdb_write(connection, local_buffer, len);
326 /* larger packets are transmitted directly from caller supplied buffer
327 by several calls to gdb_write() to avoid dynamic allocation */
328 local_buffer[1] = '#';
329 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
330 local_buffer[3] = DIGITS[my_checksum & 0xf];
331 gdb_write(connection, local_buffer, 1);
332 gdb_write(connection, buffer, len);
333 gdb_write(connection, local_buffer+1, 3);
336 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
341 else if (reply == '-')
343 /* Stop sending output packets for now */
344 log_remove_callback(gdb_log_callback, connection);
345 LOG_WARNING("negative reply, retrying");
347 else if (reply == 0x3)
350 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
354 else if (reply == '-')
356 /* Stop sending output packets for now */
357 log_remove_callback(gdb_log_callback, connection);
358 LOG_WARNING("negative reply, retrying");
362 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
364 return ERROR_SERVER_REMOTE_CLOSED;
369 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
371 return ERROR_SERVER_REMOTE_CLOSED;
375 return ERROR_SERVER_REMOTE_CLOSED;
380 int gdb_put_packet(connection_t *connection, char *buffer, int len)
382 gdb_connection_t *gdb_con = connection->priv;
384 int retval = gdb_put_packet_inner(connection, buffer, len);
389 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
395 unsigned char my_checksum = 0;
396 gdb_connection_t *gdb_con = connection->priv;
402 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
405 #ifdef _DEBUG_GDB_IO_
406 LOG_DEBUG("character: '%c'", character);
414 LOG_WARNING("acknowledgment received, but no packet pending");
417 LOG_WARNING("negative acknowledgment, but no packet pending");
424 LOG_WARNING("ignoring character 0x%x", character);
427 } while (character != '$');
432 gdb_connection_t *gdb_con = connection->priv;
435 /* The common case is that we have an entire packet with no escape chars.
436 * We need to leave at least 2 bytes in the buffer to have
437 * gdb_get_char() update various bits and bobs correctly.
439 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
441 /* The compiler will struggle a bit with constant propagation and
442 * aliasing, so we help it by showing that these values do not
443 * change inside the loop
446 char *buf = gdb_con->buf_p;
447 int run = gdb_con->buf_cnt - 2;
454 if (character == '#')
456 /* Danger! character can be '#' when esc is
457 * used so we need an explicit boolean for done here.
463 if (character == '}')
465 /* data transmitted in binary mode (X packet)
466 * uses 0x7d as escape character */
467 my_checksum += character & 0xff;
470 my_checksum += character & 0xff;
471 buffer[count++] = (character ^ 0x20) & 0xff;
474 my_checksum += character & 0xff;
475 buffer[count++] = character & 0xff;
479 gdb_con->buf_cnt -= i;
485 LOG_ERROR("packet buffer too small");
486 return ERROR_GDB_BUFFER_TOO_SMALL;
489 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
492 if (character == '#')
495 if (character == '}')
497 /* data transmitted in binary mode (X packet)
498 * uses 0x7d as escape character */
499 my_checksum += character & 0xff;
500 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
502 my_checksum += character & 0xff;
503 buffer[count++] = (character ^ 0x20) & 0xff;
507 my_checksum += character & 0xff;
508 buffer[count++] = character & 0xff;
515 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
517 checksum[0] = character;
518 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
520 checksum[1] = character;
523 if (my_checksum == strtoul(checksum, NULL, 16))
525 gdb_write(connection, "+", 1);
529 LOG_WARNING("checksum error, requesting retransmission");
530 gdb_write(connection, "-", 1);
533 return ERROR_SERVER_REMOTE_CLOSED;
538 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
540 gdb_connection_t *gdb_con = connection->priv;
542 int retval = gdb_get_packet_inner(connection, buffer, len);
547 int gdb_output_con(connection_t *connection, const char* line)
552 bin_size = strlen(line);
554 hex_buffer = malloc(bin_size*2 + 2);
555 if (hex_buffer == NULL)
556 return ERROR_GDB_BUFFER_TOO_SMALL;
559 for (i=0; i<bin_size; i++)
560 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
561 hex_buffer[bin_size*2+1] = 0;
563 gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
569 int gdb_output(struct command_context_s *context, char* line)
571 /* this will be dumped to the log and also sent as an O packet if possible */
572 LOG_USER_N("%s", line);
576 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
579 struct command_context_s *cmd_ctx = priv;
581 if (target->gdb_program_script)
583 script = open_file_from_path(target->gdb_program_script, "r");
586 LOG_ERROR("couldn't open script file %s", target->gdb_program_script);
590 LOG_INFO("executing gdb_program script '%s'", target->gdb_program_script);
591 command_run_file(cmd_ctx, script, COMMAND_EXEC);
594 jtag_execute_queue();
600 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
602 connection_t *connection = priv;
603 gdb_connection_t *gdb_connection = connection->priv;
609 case TARGET_EVENT_HALTED:
610 /* In the GDB protocol when we are stepping or coninuing execution,
611 * we have a lingering reply. Upon receiving a halted event
612 * when we have that lingering packet, we reply to the original
613 * step or continue packet.
615 * Executing monitor commands can bring the target in and
616 * out of the running state so we'll see lots of TARGET_EVENT_XXX
617 * that are to be ignored.
619 if (gdb_connection->frontend_state == TARGET_RUNNING)
621 /* stop forwarding log packets! */
622 log_remove_callback(gdb_log_callback, connection);
624 if (gdb_connection->ctrl_c)
627 gdb_connection->ctrl_c = 0;
631 signal = gdb_last_signal(target);
634 snprintf(sig_reply, 4, "T%2.2x", signal);
635 gdb_put_packet(connection, sig_reply, 3);
636 gdb_connection->frontend_state = TARGET_HALTED;
639 case TARGET_EVENT_GDB_PROGRAM:
640 gdb_program_handler(target, event, connection->cmd_ctx);
650 int gdb_new_connection(connection_t *connection)
652 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
653 gdb_service_t *gdb_service = connection->service->priv;
657 connection->priv = gdb_connection;
659 /* initialize gdb connection information */
660 gdb_connection->buf_p = gdb_connection->buffer;
661 gdb_connection->buf_cnt = 0;
662 gdb_connection->ctrl_c = 0;
663 gdb_connection->frontend_state = TARGET_HALTED;
664 gdb_connection->vflash_image = NULL;
665 gdb_connection->closed = 0;
666 gdb_connection->busy = 0;
668 /* send ACK to GDB for debug request */
669 gdb_write(connection, "+", 1);
671 /* output goes through gdb connection */
672 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
674 /* register callback to be informed about target events */
675 target_register_event_callback(gdb_target_callback_event_handler, connection);
677 /* a gdb session just attached, try to put the target in halt mode.
681 * If the halt fails(e.g. target needs a reset, JTAG communication not
682 * working, etc.), then the GDB connect will succeed as
683 * the get_gdb_reg_list() will lie and return a register list with
686 * This allows GDB monitor commands to be run from a GDB init script to
687 * initialize the target
689 * Also, since the halt() is asynchronous target connect will be
690 * instantaneous and thus avoiding annoying timeout problems during
693 target_halt(gdb_service->target);
695 /* remove the initial ACK from the incoming buffer */
696 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
699 /* FIX!!!??? would we actually ever receive a + here???
702 if (initial_ack != '+')
703 gdb_putback_char(connection, initial_ack);
708 int gdb_connection_closed(connection_t *connection)
710 gdb_service_t *gdb_service = connection->service->priv;
711 gdb_connection_t *gdb_connection = connection->priv;
713 /* see if an image built with vFlash commands is left */
714 if (gdb_connection->vflash_image)
716 image_close(gdb_connection->vflash_image);
717 free(gdb_connection->vflash_image);
718 gdb_connection->vflash_image = NULL;
721 /* if this connection registered a debug-message receiver delete it */
722 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
724 if (connection->priv)
726 free(connection->priv);
727 connection->priv = NULL;
731 LOG_ERROR("BUG: connection->priv == NULL");
734 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
735 log_remove_callback(gdb_log_callback, connection);
740 void gdb_send_error(connection_t *connection, u8 the_error)
743 snprintf(err, 4, "E%2.2X", the_error );
744 gdb_put_packet(connection, err, 3);
747 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
752 signal = gdb_last_signal(target);
754 snprintf(sig_reply, 4, "S%2.2x", signal);
755 gdb_put_packet(connection, sig_reply, 3);
760 /* Convert register to string of bits. NB! The # of bits in the
761 * register might be non-divisible by 8(a byte), in which
762 * case an entire byte is shown. */
763 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
770 buf_len = CEIL(reg->size, 8);
772 for (i = 0; i < buf_len; i++)
774 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
775 tstr[i*2+1] = DIGITS[buf[i]&0xf];
779 void gdb_target_to_str(target_t *target, char *tstr, char *str)
781 int str_len = strlen(tstr);
786 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
790 for (i = 0; i < str_len; i+=2)
792 str[str_len - i - 1] = tstr[i + 1];
793 str[str_len - i - 2] = tstr[i];
797 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
802 int reg_packet_size = 0;
807 #ifdef _DEBUG_GDB_IO_
811 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
813 return gdb_error(connection, retval);
816 for (i = 0; i < reg_list_size; i++)
818 reg_packet_size += reg_list[i]->size;
821 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
822 reg_packet_p = reg_packet;
824 for (i = 0; i < reg_list_size; i++)
826 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
827 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
830 #ifdef _DEBUG_GDB_IO_
833 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
834 LOG_DEBUG("reg_packet: %s", reg_packet_p);
839 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
847 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
855 #ifdef _DEBUG_GDB_IO_
859 /* skip command character */
865 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
866 return ERROR_SERVER_REMOTE_CLOSED;
869 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
871 return gdb_error(connection, retval);
875 for (i = 0; i < reg_list_size; i++)
879 reg_arch_type_t *arch_type;
881 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
882 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
883 gdb_target_to_str(target, packet_p, hex_buf);
885 /* convert hex-string to binary buffer */
886 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
887 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
889 /* get register arch_type, and call set method */
890 arch_type = register_get_arch_type(reg_list[i]->arch_type);
891 if (arch_type == NULL)
893 LOG_ERROR("BUG: encountered unregistered arch type");
896 arch_type->set(reg_list[i], bin_buf);
898 /* advance packet pointer */
899 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
905 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
908 gdb_put_packet(connection, "OK", 2);
913 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
916 int reg_num = strtoul(packet + 1, NULL, 16);
921 #ifdef _DEBUG_GDB_IO_
925 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
927 return gdb_error(connection, retval);
930 if (reg_list_size <= reg_num)
932 LOG_ERROR("gdb requested a non-existing register");
936 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
938 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
940 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
948 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
953 int reg_num = strtoul(packet + 1, &separator, 16);
957 reg_arch_type_t *arch_type;
961 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
963 return gdb_error(connection, retval);
966 if (reg_list_size < reg_num)
968 LOG_ERROR("gdb requested a non-existing register");
969 return ERROR_SERVER_REMOTE_CLOSED;
972 if (*separator != '=')
974 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
975 return ERROR_SERVER_REMOTE_CLOSED;
978 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
979 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
980 gdb_target_to_str(target, separator + 1, hex_buf);
982 /* convert hex-string to binary buffer */
983 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
984 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
986 /* get register arch_type, and call set method */
987 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
988 if (arch_type == NULL)
990 LOG_ERROR("BUG: encountered unregistered arch type");
993 arch_type->set(reg_list[reg_num], bin_buf);
995 gdb_put_packet(connection, "OK", 2);
1004 int gdb_error(connection_t *connection, int retval)
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);
1017 case ERROR_TARGET_NOT_HALTED:
1018 gdb_send_error(connection, EFAULT);
1021 /* This could be that the target reset itself. */
1022 LOG_ERROR("unexpected error %i", retval);
1023 gdb_send_error(connection, EFAULT);
1030 /* We don't have to worry about the default 2 second timeout for GDB packets,
1031 * because GDB breaks up large memory reads into smaller reads.
1033 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1035 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1044 int retval = ERROR_OK;
1046 /* skip command character */
1049 addr = strtoul(packet, &separator, 16);
1051 if (*separator != ',')
1053 LOG_ERROR("incomplete read memory packet received, dropping connection");
1054 return ERROR_SERVER_REMOTE_CLOSED;
1057 len = strtoul(separator+1, NULL, 16);
1059 buffer = malloc(len);
1061 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1063 retval = target_read_buffer(target, addr, len, buffer);
1065 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1067 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1068 * At some point this might be fixed in GDB, in which case this code can be removed.
1070 * OpenOCD developers are acutely aware of this problem, but there is nothing
1071 * gained by involving the user in this problem that hopefully will get resolved
1074 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1076 * For now, the default is to fix up things to make current GDB versions work.
1077 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1079 memset(buffer, 0, len);
1083 if (retval == ERROR_OK)
1085 hex_buffer = malloc(len * 2 + 1);
1088 for (i = 0; i < len; i++)
1091 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1092 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1095 gdb_put_packet(connection, hex_buffer, len * 2);
1101 retval = gdb_error(connection, retval);
1109 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1120 /* skip command character */
1123 addr = strtoul(packet, &separator, 16);
1125 if (*separator != ',')
1127 LOG_ERROR("incomplete write memory packet received, dropping connection");
1128 return ERROR_SERVER_REMOTE_CLOSED;
1131 len = strtoul(separator+1, &separator, 16);
1133 if (*(separator++) != ':')
1135 LOG_ERROR("incomplete write memory packet received, dropping connection");
1136 return ERROR_SERVER_REMOTE_CLOSED;
1139 buffer = malloc(len);
1141 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1143 for (i=0; i<len; i++)
1146 sscanf(separator + 2*i, "%2x", &tmp);
1150 retval = target_write_buffer(target, addr, len, buffer);
1152 if (retval == ERROR_OK)
1154 gdb_put_packet(connection, "OK", 2);
1158 retval = gdb_error(connection, retval);
1166 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1174 /* skip command character */
1177 addr = strtoul(packet, &separator, 16);
1179 if (*separator != ',')
1181 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1182 return ERROR_SERVER_REMOTE_CLOSED;
1185 len = strtoul(separator+1, &separator, 16);
1187 if (*(separator++) != ':')
1189 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1190 return ERROR_SERVER_REMOTE_CLOSED;
1196 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1198 retval = target_write_buffer(target, addr, len, (u8*)separator);
1201 if (retval == ERROR_OK)
1203 gdb_put_packet(connection, "OK", 2);
1207 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1214 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1221 if (packet_size > 1)
1223 packet[packet_size] = 0;
1224 address = strtoul(packet + 1, NULL, 16);
1231 if (packet[0] == 'c')
1233 LOG_DEBUG("continue");
1234 target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1236 else if (packet[0] == 's')
1239 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1243 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1246 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1247 enum watchpoint_rw wp_type;
1255 type = strtoul(packet + 1, &separator, 16);
1257 if (type == 0) /* memory breakpoint */
1258 bp_type = BKPT_SOFT;
1259 else if (type == 1) /* hardware breakpoint */
1260 bp_type = BKPT_HARD;
1261 else if (type == 2) /* write watchpoint */
1262 wp_type = WPT_WRITE;
1263 else if (type == 3) /* read watchpoint */
1265 else if (type == 4) /* access watchpoint */
1266 wp_type = WPT_ACCESS;
1268 if (*separator != ',')
1270 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1271 return ERROR_SERVER_REMOTE_CLOSED;
1274 address = strtoul(separator+1, &separator, 16);
1276 if (*separator != ',')
1278 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1279 return ERROR_SERVER_REMOTE_CLOSED;
1282 size = strtoul(separator+1, &separator, 16);
1288 if (packet[0] == 'Z')
1290 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1292 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1297 gdb_put_packet(connection, "OK", 2);
1302 breakpoint_remove(target, address);
1303 gdb_put_packet(connection, "OK", 2);
1310 if (packet[0] == 'Z')
1312 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1314 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1319 gdb_put_packet(connection, "OK", 2);
1324 watchpoint_remove(target, address);
1325 gdb_put_packet(connection, "OK", 2);
1336 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1337 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1339 if (*retval != ERROR_OK)
1347 if ((*xml == NULL) || (!first))
1349 /* start by 0 to exercise all the code paths.
1350 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1352 *size = *size * 2 + 2;
1354 *xml = realloc(*xml, *size);
1359 *retval = ERROR_SERVER_REMOTE_CLOSED;
1367 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1369 if ((ret > 0) && ((ret + 1) < *size - *pos))
1374 /* there was just enough or not enough space, allocate more. */
1379 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1383 /* Extract and NUL-terminate the annex. */
1385 while (*buf && *buf != ':')
1391 /* After the read marker and annex, qXfer looks like a
1392 * traditional 'm' packet. */
1394 *ofs = strtoul(buf, &separator, 16);
1396 if (*separator != ',')
1399 *len = strtoul(separator+1, NULL, 16);
1404 int gdb_calc_blocksize(flash_bank_t *bank)
1407 int block_size = 0xffffffff;
1409 /* loop through all sectors and return smallest sector size */
1411 for (i = 0; i < bank->num_sectors; i++)
1413 if (bank->sectors[i].size < block_size)
1414 block_size = bank->sectors[i].size;
1420 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1422 command_context_t *cmd_ctx = connection->cmd_ctx;
1424 if (strstr(packet, "qRcmd,"))
1426 if (packet_size > 6)
1430 cmd = malloc((packet_size - 6)/2 + 1);
1431 for (i=0; i < (packet_size - 6)/2; i++)
1434 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1437 cmd[(packet_size - 6)/2] = 0x0;
1439 /* We want to print all debug output to GDB connection */
1440 log_add_callback(gdb_log_callback, connection);
1441 target_call_timer_callbacks_now();
1442 command_run_line(cmd_ctx, cmd);
1443 target_call_timer_callbacks_now();
1444 log_remove_callback(gdb_log_callback, connection);
1447 gdb_put_packet(connection, "OK", 2);
1450 else if (strstr(packet, "qCRC:"))
1452 if (packet_size > 5)
1461 /* skip command character */
1464 addr = strtoul(packet, &separator, 16);
1466 if (*separator != ',')
1468 LOG_ERROR("incomplete read memory packet received, dropping connection");
1469 return ERROR_SERVER_REMOTE_CLOSED;
1472 len = strtoul(separator + 1, NULL, 16);
1474 retval = target_checksum_memory(target, addr, len, &checksum);
1476 if (retval == ERROR_OK)
1478 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1479 gdb_put_packet(connection, gdb_reply, 9);
1483 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1490 else if (strstr(packet, "qSupported"))
1492 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1493 * disable qXfer:features:read for the moment */
1494 int retval = ERROR_OK;
1495 char *buffer = NULL;
1499 xml_printf(&retval, &buffer, &pos, &size,
1500 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1501 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1503 if (retval != ERROR_OK)
1505 gdb_send_error(connection, 01);
1509 gdb_put_packet(connection, buffer, strlen(buffer));
1514 else if (strstr(packet, "qXfer:memory-map:read::"))
1516 /* We get away with only specifying flash here. Regions that are not
1517 * specified are treated as if we provided no memory map(if not we
1518 * could detect the holes and mark them as RAM).
1519 * Normally we only execute this code once, but no big deal if we
1520 * have to regenerate it a couple of times. */
1526 int retval = ERROR_OK;
1533 /* skip command character */
1536 offset = strtoul(packet, &separator, 16);
1537 length = strtoul(separator + 1, &separator, 16);
1539 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1542 for (i=0; i<flash_get_bank_count(); i++)
1544 p = get_flash_bank_by_num(i);
1548 /* if device has uneven sector sizes, eg. str7, lpc
1549 * we pass the smallest sector size to gdb memory map */
1550 blocksize = gdb_calc_blocksize(p);
1552 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1553 "<property name=\"blocksize\">0x%x</property>\n" \
1555 p->base, p->size, blocksize);
1558 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1560 if (retval != ERROR_OK)
1562 gdb_send_error(connection, retval);
1566 if (offset + length > pos)
1568 length = pos - offset;
1571 char *t = malloc(length + 1);
1573 memcpy(t + 1, xml + offset, length);
1574 gdb_put_packet(connection, t, length + 1);
1580 else if (strstr(packet, "qXfer:features:read:"))
1585 int retval = ERROR_OK;
1588 unsigned int length;
1591 /* skip command character */
1594 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1596 gdb_send_error(connection, 01);
1600 if (strcmp(annex, "target.xml") != 0)
1602 gdb_send_error(connection, 01);
1606 xml_printf(&retval, &xml, &pos, &size, \
1607 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1609 if (retval != ERROR_OK)
1611 gdb_send_error(connection, retval);
1615 gdb_put_packet(connection, xml, strlen(xml));
1621 gdb_put_packet(connection, "", 0);
1625 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1627 gdb_connection_t *gdb_connection = connection->priv;
1628 gdb_service_t *gdb_service = connection->service->priv;
1631 /* if flash programming disabled - send a empty reply */
1633 if (gdb_flash_program == 0)
1635 gdb_put_packet(connection, "", 0);
1639 if (strstr(packet, "vFlashErase:"))
1642 unsigned long length;
1644 char *parse = packet + 12;
1647 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1648 return ERROR_SERVER_REMOTE_CLOSED;
1651 addr = strtoul(parse, &parse, 16);
1653 if (*(parse++) != ',' || *parse == '\0')
1655 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1656 return ERROR_SERVER_REMOTE_CLOSED;
1659 length = strtoul(parse, &parse, 16);
1663 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1664 return ERROR_SERVER_REMOTE_CLOSED;
1667 /* assume all sectors need erasing - stops any problems
1668 * when flash_write is called multiple times */
1671 /* perform any target specific operations before the erase */
1672 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1675 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1677 /* GDB doesn't evaluate the actual error number returned,
1678 * treat a failed erase as an I/O error
1680 gdb_send_error(connection, EIO);
1681 LOG_ERROR("flash_erase returned %i", result);
1684 gdb_put_packet(connection, "OK", 2);
1689 if (strstr(packet, "vFlashWrite:"))
1692 unsigned long length;
1693 char *parse = packet + 12;
1697 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1698 return ERROR_SERVER_REMOTE_CLOSED;
1700 addr = strtoul(parse, &parse, 16);
1701 if (*(parse++) != ':')
1703 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1704 return ERROR_SERVER_REMOTE_CLOSED;
1706 length = packet_size - (parse - packet);
1708 /* create a new image if there isn't already one */
1709 if (gdb_connection->vflash_image == NULL)
1711 gdb_connection->vflash_image = malloc(sizeof(image_t));
1712 image_open(gdb_connection->vflash_image, "", "build");
1715 /* create new section with content from packet buffer */
1716 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1718 gdb_put_packet(connection, "OK", 2);
1723 if (!strcmp(packet, "vFlashDone"))
1727 /* process the flashing buffer. No need to erase as GDB
1728 * always issues a vFlashErase first. */
1729 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK)
1731 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1732 gdb_put_packet(connection, "E.memtype", 9);
1734 gdb_send_error(connection, EIO);
1738 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written);
1739 gdb_put_packet(connection, "OK", 2);
1742 image_close(gdb_connection->vflash_image);
1743 free(gdb_connection->vflash_image);
1744 gdb_connection->vflash_image = NULL;
1749 gdb_put_packet(connection, "", 0);
1753 int gdb_detach(connection_t *connection, target_t *target)
1755 switch( detach_mode )
1757 case GDB_DETACH_RESUME:
1758 target_resume(target, 1, 0, 1, 0);
1761 case GDB_DETACH_RESET:
1762 target_process_reset(connection->cmd_ctx);
1765 case GDB_DETACH_HALT:
1766 target_halt(target);
1769 case GDB_DETACH_NOTHING:
1773 gdb_put_packet(connection, "OK", 2);
1778 static void gdb_log_callback(void *priv, const char *file, int line,
1779 const char *function, const char *string)
1781 connection_t *connection = priv;
1782 gdb_connection_t *gdb_con = connection->priv;
1786 /* do not reply this using the O packet */
1790 gdb_output_con(connection, string);
1793 int gdb_input_inner(connection_t *connection)
1795 gdb_service_t *gdb_service = connection->service->priv;
1796 target_t *target = gdb_service->target;
1797 char packet[GDB_BUFFER_SIZE];
1800 gdb_connection_t *gdb_con = connection->priv;
1801 static int extended_protocol = 0;
1803 /* drain input buffer */
1806 packet_size = GDB_BUFFER_SIZE-1;
1807 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1812 /* terminate with zero */
1813 packet[packet_size] = 0;
1815 LOG_DEBUG("received packet: '%s'", packet);
1817 if (packet_size > 0)
1823 /* Hct... -- set thread
1824 * we don't have threads, send empty reply */
1825 gdb_put_packet(connection, NULL, 0);
1828 retval = gdb_query_packet(connection, target, packet, packet_size);
1831 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1834 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1837 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1840 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1843 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1846 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1850 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1853 gdb_last_signal_packet(connection, target, packet, packet_size);
1858 if (target->state != TARGET_HALTED)
1860 /* If the target isn't in the halted state, then we can't
1861 * step/continue. This might be early setup, etc.
1864 snprintf(sig_reply, 4, "T%2.2x", 2);
1865 gdb_put_packet(connection, sig_reply, 3);
1868 /* We're running/stepping, in which case we can
1869 * forward log output until the target is halted
1871 gdb_connection_t *gdb_con = connection->priv;
1872 gdb_con->frontend_state = TARGET_RUNNING;
1873 log_add_callback(gdb_log_callback, connection);
1874 gdb_step_continue_packet(connection, target, packet, packet_size);
1879 retval = gdb_v_packet(connection, target, packet, packet_size);
1882 retval = gdb_detach(connection, target);
1883 extended_protocol = 0;
1886 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1890 if (extended_protocol != 0)
1892 gdb_put_packet(connection, "OK", 2);
1893 return ERROR_SERVER_REMOTE_CLOSED;
1895 /* handle extended remote protocol */
1896 extended_protocol = 1;
1897 gdb_put_packet(connection, "OK", 2);
1900 /* handle extended restart packet */
1901 target_process_reset(connection->cmd_ctx);
1904 /* ignore unkown packets */
1905 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
1906 gdb_put_packet(connection, NULL, 0);
1910 /* if a packet handler returned an error, exit input loop */
1911 if (retval != ERROR_OK)
1915 if (gdb_con->ctrl_c)
1917 if (target->state == TARGET_RUNNING)
1919 target_halt(target);
1920 gdb_con->ctrl_c = 0;
1924 } while (gdb_con->buf_cnt > 0);
1929 int gdb_input(connection_t *connection)
1931 int retval = gdb_input_inner(connection);
1932 gdb_connection_t *gdb_con = connection->priv;
1933 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1936 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
1937 if (gdb_con->closed)
1938 return ERROR_SERVER_REMOTE_CLOSED;
1940 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1946 gdb_service_t *gdb_service;
1947 target_t *target = targets;
1952 LOG_WARNING("no gdb ports allocated as no target has been specified");
1958 LOG_WARNING("no gdb port specified, using default port 3333");
1964 char service_name[8];
1966 snprintf(service_name, 8, "gdb-%2.2i", i);
1968 gdb_service = malloc(sizeof(gdb_service_t));
1969 gdb_service->target = target;
1971 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1973 LOG_DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1976 target = target->next;
1982 /* daemon configuration command gdb_port */
1983 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1988 /* only if the port wasn't overwritten by cmdline */
1990 gdb_port = strtoul(args[0], NULL, 0);
1995 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1999 if (strcmp(args[0], "resume") == 0)
2001 detach_mode = GDB_DETACH_RESUME;
2004 else if (strcmp(args[0], "reset") == 0)
2006 detach_mode = GDB_DETACH_RESET;
2009 else if (strcmp(args[0], "halt") == 0)
2011 detach_mode = GDB_DETACH_HALT;
2014 else if (strcmp(args[0], "nothing") == 0)
2016 detach_mode = GDB_DETACH_NOTHING;
2021 LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2025 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2029 if (strcmp(args[0], "enable") == 0)
2031 gdb_use_memory_map = 1;
2034 else if (strcmp(args[0], "disable") == 0)
2036 gdb_use_memory_map = 0;
2041 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2045 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2049 if (strcmp(args[0], "enable") == 0)
2051 gdb_flash_program = 1;
2054 else if (strcmp(args[0], "disable") == 0)
2056 gdb_flash_program = 0;
2061 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2065 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2069 if (strcmp(args[0], "enable") == 0)
2071 gdb_report_data_abort = 1;
2074 else if (strcmp(args[0], "disable") == 0)
2076 gdb_report_data_abort = 0;
2081 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2085 int gdb_register_commands(command_context_t *command_context)
2087 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2088 COMMAND_CONFIG, "");
2089 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2090 COMMAND_CONFIG, "");
2091 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2092 COMMAND_CONFIG, "");
2093 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2094 COMMAND_CONFIG, "");
2095 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2096 COMMAND_CONFIG, "");