1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
26 #include "gdb_server.h"
30 #include "binarybuffer.h"
32 #include "breakpoints.h"
34 #include "target_request.h"
42 #define _DEBUG_GDB_IO_
45 static unsigned short gdb_port;
46 static const char *DIGITS = "0123456789abcdef";
48 static void gdb_log_callback(void *priv, const char *file, int line,
49 const char *function, const char *format, va_list args);
59 /* target behaviour on gdb detach */
60 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
62 /* set if we are sending a memory map to gdb
63 * via qXfer:memory-map:read packet */
64 int gdb_use_memory_map = 0;
65 int gdb_flash_program = 0;
67 /* if set, data aborts cause an error to be reported in memory read packets
68 * see the code in gdb_read_memory_packet() for further explanations */
69 int gdb_report_data_abort = 0;
71 int gdb_last_signal(target_t *target)
73 switch (target->debug_reason)
75 case DBG_REASON_DBGRQ:
76 return 0x2; /* SIGINT */
77 case DBG_REASON_BREAKPOINT:
78 case DBG_REASON_WATCHPOINT:
79 case DBG_REASON_WPTANDBKPT:
80 return 0x05; /* SIGTRAP */
81 case DBG_REASON_SINGLESTEP:
82 return 0x05; /* SIGTRAP */
83 case DBG_REASON_NOTHALTED:
84 return 0x0; /* no signal... shouldn't happen */
86 ERROR("BUG: undefined debug reason");
91 int gdb_get_char(connection_t *connection, int* next_char)
93 gdb_connection_t *gdb_con = connection->priv;
99 if (gdb_con->buf_cnt-- > 0)
101 *next_char = *(gdb_con->buf_p++);
102 if (gdb_con->buf_cnt > 0)
103 connection->input_pending = 1;
105 connection->input_pending = 0;
107 #ifdef _DEBUG_GDB_IO_
108 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
117 /* a non-blocking socket will block if there is 0 bytes available on the socket,
118 * but return with as many bytes as are available immediately
124 FD_SET(connection->fd, &read_fds);
128 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
130 /* This can typically be because a "monitor" command took too long
131 * before printing any progress messages
133 return ERROR_GDB_TIMEOUT;
136 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
137 if (gdb_con->buf_cnt > 0)
141 if (gdb_con->buf_cnt == 0)
144 return ERROR_SERVER_REMOTE_CLOSED;
148 errno = WSAGetLastError();
155 case WSAECONNABORTED:
156 return ERROR_SERVER_REMOTE_CLOSED;
158 return ERROR_SERVER_REMOTE_CLOSED;
160 ERROR("read: %d", errno);
170 return ERROR_SERVER_REMOTE_CLOSED;
172 return ERROR_SERVER_REMOTE_CLOSED;
174 ERROR("read: %s", strerror(errno));
175 return ERROR_SERVER_REMOTE_CLOSED;
180 #ifdef _DEBUG_GDB_IO_
181 debug_buffer = malloc(gdb_con->buf_cnt + 1);
182 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
183 debug_buffer[gdb_con->buf_cnt] = 0;
184 DEBUG("received '%s'", debug_buffer);
188 gdb_con->buf_p = gdb_con->buffer;
190 *next_char = *(gdb_con->buf_p++);
191 if (gdb_con->buf_cnt > 0)
192 connection->input_pending = 1;
194 connection->input_pending = 0;
195 #ifdef _DEBUG_GDB_IO_
196 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
202 int gdb_putback_char(connection_t *connection, int last_char)
204 gdb_connection_t *gdb_con = connection->priv;
206 if (gdb_con->buf_p > gdb_con->buffer)
208 *(--gdb_con->buf_p) = last_char;
213 ERROR("BUG: couldn't put character back");
219 /* The only way we can detect that the socket is closed is the first time
220 * we write to it, we will fail. Subsequent write operations will
221 * succeed. Shudder! */
222 int gdb_write(connection_t *connection, void *data, int len)
224 gdb_connection_t *gdb_con = connection->priv;
226 return ERROR_SERVER_REMOTE_CLOSED;
228 if (write_socket(connection->fd, data, len) == len)
233 return ERROR_SERVER_REMOTE_CLOSED;
236 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
239 unsigned char my_checksum = 0;
240 #ifdef _DEBUG_GDB_IO_
245 gdb_connection_t *gdb_con = connection->priv;
247 for (i = 0; i < len; i++)
248 my_checksum += buffer[i];
252 #ifdef _DEBUG_GDB_IO_
253 debug_buffer = malloc(len + 1);
254 memcpy(debug_buffer, buffer, len);
255 debug_buffer[len] = 0;
256 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
261 gdb_write(connection, "$", 1);
263 gdb_write(connection, buffer, len);
264 gdb_write(connection, "#", 1);
266 snprintf(checksum, 3, "%2.2x", my_checksum);
268 gdb_write(connection, checksum, 2);
270 void *allocated = NULL;
271 char stackAlloc[1024];
272 char *t = stackAlloc;
273 int totalLen = 1 + len + 1 + 2;
274 if (totalLen > sizeof(stackAlloc))
276 allocated = malloc(totalLen);
278 if (allocated == NULL)
280 ERROR("Ran out of memory trying to reply packet %d\n", totalLen);
285 memcpy(t + 1, buffer, len);
287 t[1 + len + 1] = DIGITS[(my_checksum >> 4) & 0xf];
288 t[1 + len + 2] = DIGITS[my_checksum & 0xf];
290 gdb_write(connection, t, totalLen);
297 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
302 else if (reply == '-')
304 /* Stop sending output packets for now */
305 log_setCallback(NULL, NULL);
306 WARNING("negative reply, retrying");
308 else if (reply == 0x3)
311 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
315 else if (reply == '-')
317 /* Stop sending output packets for now */
318 log_setCallback(NULL, NULL);
319 WARNING("negative reply, retrying");
323 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
324 return ERROR_SERVER_REMOTE_CLOSED;
329 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
330 return ERROR_SERVER_REMOTE_CLOSED;
334 return ERROR_SERVER_REMOTE_CLOSED;
339 int gdb_put_packet(connection_t *connection, char *buffer, int len)
341 gdb_connection_t *gdb_con = connection->priv;
343 int retval = gdb_put_packet_inner(connection, buffer, len);
348 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
354 unsigned char my_checksum = 0;
355 gdb_connection_t *gdb_con = connection->priv;
361 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
364 #ifdef _DEBUG_GDB_IO_
365 DEBUG("character: '%c'", character);
373 WARNING("acknowledgment received, but no packet pending");
376 WARNING("negative acknowledgment, but no packet pending");
383 WARNING("ignoring character 0x%x", character);
386 } while (character != '$');
391 gdb_connection_t *gdb_con = connection->priv;
394 /* The common case is that we have an entire packet with no escape chars.
395 * We need to leave at least 2 bytes in the buffer to have
396 * gdb_get_char() update various bits and bobs correctly.
398 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
400 /* The compiler will struggle a bit with constant propagation and
401 * aliasing, so we help it by showing that these values do not
402 * change inside the loop
405 char *buf = gdb_con->buf_p;
406 int run = gdb_con->buf_cnt - 2;
413 if (character == '#')
415 /* Danger! character can be '#' when esc is
416 * used so we need an explicit boolean for done here.
422 if (character == '}')
424 /* data transmitted in binary mode (X packet)
425 * uses 0x7d as escape character */
426 my_checksum += character & 0xff;
429 my_checksum += character & 0xff;
430 buffer[count++] = (character ^ 0x20) & 0xff;
433 my_checksum += character & 0xff;
434 buffer[count++] = character & 0xff;
438 gdb_con->buf_cnt -= i;
444 ERROR("packet buffer too small");
445 return ERROR_GDB_BUFFER_TOO_SMALL;
448 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
451 if (character == '#')
454 if (character == '}')
456 /* data transmitted in binary mode (X packet)
457 * uses 0x7d as escape character */
458 my_checksum += character & 0xff;
459 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
461 my_checksum += character & 0xff;
462 buffer[count++] = (character ^ 0x20) & 0xff;
466 my_checksum += character & 0xff;
467 buffer[count++] = character & 0xff;
474 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
476 checksum[0] = character;
477 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
479 checksum[1] = character;
482 if (my_checksum == strtoul(checksum, NULL, 16))
484 gdb_write(connection, "+", 1);
488 WARNING("checksum error, requesting retransmission");
489 gdb_write(connection, "-", 1);
492 return ERROR_SERVER_REMOTE_CLOSED;
497 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
499 gdb_connection_t *gdb_con = connection->priv;
501 int retval = gdb_get_packet_inner(connection, buffer, len);
506 int gdb_output_con(connection_t *connection, char* line)
511 bin_size = strlen(line);
513 hex_buffer = malloc(bin_size*2 + 4);
516 for (i=0; i<bin_size; i++)
517 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
518 hex_buffer[bin_size*2+1] = '0';
519 hex_buffer[bin_size*2+2] = 'a';
520 hex_buffer[bin_size*2+3] = 0x0;
522 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
528 int gdb_output(struct command_context_s *context, char* line)
530 /* this will be dumped to the log and also sent as an O packet if possible */
535 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
538 struct command_context_s *cmd_ctx = priv;
540 if (target->gdb_program_script)
542 script = fopen(target->gdb_program_script, "r");
545 ERROR("couldn't open script file %s", target->gdb_program_script);
549 INFO("executing gdb_program script '%s'", target->gdb_program_script);
550 command_run_file(cmd_ctx, script, COMMAND_EXEC);
553 jtag_execute_queue();
559 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
561 connection_t *connection = priv;
562 gdb_connection_t *gdb_connection = connection->priv;
568 case TARGET_EVENT_HALTED:
569 /* In the GDB protocol when we are stepping or coninuing execution,
570 * we have a lingering reply. Upon receiving a halted event
571 * when we have that lingering packet, we reply to the original
572 * step or continue packet.
574 * Executing monitor commands can bring the target in and
575 * out of the running state so we'll see lots of TARGET_EVENT_XXX
576 * that are to be ignored.
578 if (gdb_connection->frontend_state == TARGET_RUNNING)
580 /* stop forwarding log packets! */
581 log_setCallback(NULL, NULL);
583 if (gdb_connection->ctrl_c)
586 gdb_connection->ctrl_c = 0;
590 signal = gdb_last_signal(target);
593 snprintf(sig_reply, 4, "T%2.2x", signal);
594 gdb_put_packet(connection, sig_reply, 3);
595 gdb_connection->frontend_state = TARGET_HALTED;
598 case TARGET_EVENT_GDB_PROGRAM:
599 gdb_program_handler(target, event, connection->cmd_ctx);
608 int gdb_new_connection(connection_t *connection)
610 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
611 gdb_service_t *gdb_service = connection->service->priv;
615 connection->priv = gdb_connection;
617 /* initialize gdb connection information */
618 gdb_connection->buf_p = gdb_connection->buffer;
619 gdb_connection->buf_cnt = 0;
620 gdb_connection->ctrl_c = 0;
621 gdb_connection->frontend_state = TARGET_HALTED;
622 gdb_connection->vflash_image = NULL;
623 gdb_connection->closed = 0;
624 gdb_connection->busy = 0;
626 /* output goes through gdb connection */
627 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
629 /* register callback to be informed about target events */
630 target_register_event_callback(gdb_target_callback_event_handler, connection);
632 /* a gdb session just attached, put the target in halt mode */
633 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
634 (retval != ERROR_TARGET_ALREADY_HALTED))
636 ERROR("error(%d) when trying to halt target, falling back to \"reset halt\"", retval);
637 command_run_line(connection->cmd_ctx, "reset halt");
640 /* This will time out after 1 second */
641 command_run_line(connection->cmd_ctx, "wait_halt 1");
643 /* remove the initial ACK from the incoming buffer */
644 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
647 if (initial_ack != '+')
648 gdb_putback_char(connection, initial_ack);
653 int gdb_connection_closed(connection_t *connection)
655 gdb_service_t *gdb_service = connection->service->priv;
656 gdb_connection_t *gdb_connection = connection->priv;
658 /* see if an image built with vFlash commands is left */
659 if (gdb_connection->vflash_image)
661 image_close(gdb_connection->vflash_image);
662 free(gdb_connection->vflash_image);
663 gdb_connection->vflash_image = NULL;
666 /* if this connection registered a debug-message receiver delete it */
667 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
669 if (connection->priv)
671 free(connection->priv);
672 connection->priv = NULL;
676 ERROR("BUG: connection->priv == NULL");
679 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
684 void gdb_send_error(connection_t *connection, u8 the_error)
687 snprintf(err, 4, "E%2.2X", the_error );
688 gdb_put_packet(connection, err, 3);
691 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
696 signal = gdb_last_signal(target);
698 snprintf(sig_reply, 4, "S%2.2x", signal);
699 gdb_put_packet(connection, sig_reply, 3);
704 /* Convert register to string of bits. NB! The # of bits in the
705 * register might be non-divisible by 8(a byte), in which
706 * case an entire byte is shown. */
707 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
714 buf_len = CEIL(reg->size, 8);
716 if (target->endianness == TARGET_LITTLE_ENDIAN)
718 for (i = 0; i < buf_len; i++)
720 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
721 tstr[i*2+1] = DIGITS[buf[i]&0xf];
726 for (i = 0; i < buf_len; i++)
728 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
729 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
734 void gdb_target_to_str(target_t *target, char *tstr, char *str)
736 int str_len = strlen(tstr);
741 ERROR("BUG: gdb value with uneven number of characters encountered");
745 if (target->endianness == TARGET_LITTLE_ENDIAN)
747 for (i = 0; i < str_len; i+=2)
749 str[str_len - i - 1] = tstr[i + 1];
750 str[str_len - i - 2] = tstr[i];
755 for (i = 0; i < str_len; i++)
762 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
767 int reg_packet_size = 0;
772 #ifdef _DEBUG_GDB_IO_
776 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
780 case ERROR_TARGET_NOT_HALTED:
781 ERROR("gdb requested registers but we're not halted, dropping connection");
782 return ERROR_SERVER_REMOTE_CLOSED;
784 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
785 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
790 for (i = 0; i < reg_list_size; i++)
792 reg_packet_size += reg_list[i]->size;
795 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
796 reg_packet_p = reg_packet;
798 for (i = 0; i < reg_list_size; i++)
800 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
801 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
804 #ifdef _DEBUG_GDB_IO_
807 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
808 DEBUG("reg_packet: %s", reg_packet_p);
813 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
821 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
829 #ifdef _DEBUG_GDB_IO_
833 /* skip command character */
839 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
840 return ERROR_SERVER_REMOTE_CLOSED;
843 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
847 case ERROR_TARGET_NOT_HALTED:
848 ERROR("gdb tried to registers but we're not halted, dropping connection");
849 return ERROR_SERVER_REMOTE_CLOSED;
851 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
852 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
858 for (i = 0; i < reg_list_size; i++)
862 reg_arch_type_t *arch_type;
864 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
865 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
866 gdb_target_to_str(target, packet_p, hex_buf);
868 /* convert hex-string to binary buffer */
869 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
870 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
872 /* get register arch_type, and call set method */
873 arch_type = register_get_arch_type(reg_list[i]->arch_type);
874 if (arch_type == NULL)
876 ERROR("BUG: encountered unregistered arch type");
879 arch_type->set(reg_list[i], bin_buf);
881 /* advance packet pointer */
882 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
888 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
891 gdb_put_packet(connection, "OK", 2);
896 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
899 int reg_num = strtoul(packet + 1, NULL, 16);
904 #ifdef _DEBUG_GDB_IO_
908 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
912 case ERROR_TARGET_NOT_HALTED:
913 ERROR("gdb requested registers but we're not halted, dropping connection");
914 return ERROR_SERVER_REMOTE_CLOSED;
916 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
917 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
922 if (reg_list_size <= reg_num)
924 ERROR("gdb requested a non-existing register");
928 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
930 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
932 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
940 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
945 int reg_num = strtoul(packet + 1, &separator, 16);
949 reg_arch_type_t *arch_type;
953 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
957 case ERROR_TARGET_NOT_HALTED:
958 ERROR("gdb tried to set a register but we're not halted, dropping connection");
959 return ERROR_SERVER_REMOTE_CLOSED;
961 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
962 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
967 if (reg_list_size < reg_num)
969 ERROR("gdb requested a non-existing register");
970 return ERROR_SERVER_REMOTE_CLOSED;
973 if (*separator != '=')
975 ERROR("GDB 'set register packet', but no '=' following the register number");
976 return ERROR_SERVER_REMOTE_CLOSED;
979 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
980 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
981 gdb_target_to_str(target, separator + 1, hex_buf);
983 /* convert hex-string to binary buffer */
984 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
985 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
987 /* get register arch_type, and call set method */
988 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
989 if (arch_type == NULL)
991 ERROR("BUG: encountered unregistered arch type");
994 arch_type->set(reg_list[reg_num], bin_buf);
996 gdb_put_packet(connection, "OK", 2);
1005 int gdb_memory_packet_error(connection_t *connection, int retval)
1009 case ERROR_TARGET_NOT_HALTED:
1010 ERROR("gdb tried to read memory but we're not halted, dropping connection");
1011 return ERROR_SERVER_REMOTE_CLOSED;
1012 case ERROR_TARGET_DATA_ABORT:
1013 gdb_send_error(connection, EIO);
1015 case ERROR_TARGET_TRANSLATION_FAULT:
1016 gdb_send_error(connection, EFAULT);
1018 case ERROR_TARGET_UNALIGNED_ACCESS:
1019 gdb_send_error(connection, EFAULT);
1022 /* This could be that the target reset itself. */
1023 ERROR("unexpected error %i. Dropping connection.", retval);
1024 return ERROR_SERVER_REMOTE_CLOSED;
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 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 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_memory_packet_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 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 ERROR("incomplete write memory packet received, dropping connection");
1136 return ERROR_SERVER_REMOTE_CLOSED;
1139 buffer = malloc(len);
1141 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 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1167 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1175 /* skip command character */
1178 addr = strtoul(packet, &separator, 16);
1180 if (*separator != ',')
1182 ERROR("incomplete write memory binary packet received, dropping connection");
1183 return ERROR_SERVER_REMOTE_CLOSED;
1186 len = strtoul(separator+1, &separator, 16);
1188 if (*(separator++) != ':')
1190 ERROR("incomplete write memory binary packet received, dropping connection");
1191 return ERROR_SERVER_REMOTE_CLOSED;
1197 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1199 retval = target_write_buffer(target, addr, len, (u8*)separator);
1202 if (retval == ERROR_OK)
1204 gdb_put_packet(connection, "OK", 2);
1208 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1215 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1222 if (packet_size > 1)
1224 packet[packet_size] = 0;
1225 address = strtoul(packet + 1, NULL, 16);
1232 if (packet[0] == 'c')
1235 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1237 else if (packet[0] == 's')
1240 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1244 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1248 case ERROR_TARGET_NOT_HALTED:
1249 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1250 return ERROR_SERVER_REMOTE_CLOSED;
1252 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1253 gdb_send_error(connection, EBUSY);
1256 ERROR("BUG: unexpected error %i", retval);
1263 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1266 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1267 enum watchpoint_rw wp_type;
1275 type = strtoul(packet + 1, &separator, 16);
1277 if (type == 0) /* memory breakpoint */
1278 bp_type = BKPT_SOFT;
1279 else if (type == 1) /* hardware breakpoint */
1280 bp_type = BKPT_HARD;
1281 else if (type == 2) /* write watchpoint */
1282 wp_type = WPT_WRITE;
1283 else if (type == 3) /* read watchpoint */
1285 else if (type == 4) /* access watchpoint */
1286 wp_type = WPT_ACCESS;
1288 if (*separator != ',')
1290 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1291 return ERROR_SERVER_REMOTE_CLOSED;
1294 address = strtoul(separator+1, &separator, 16);
1296 if (*separator != ',')
1298 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1299 return ERROR_SERVER_REMOTE_CLOSED;
1302 size = strtoul(separator+1, &separator, 16);
1308 if (packet[0] == 'Z')
1310 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1312 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1317 gdb_put_packet(connection, "OK", 2);
1322 breakpoint_remove(target, address);
1323 gdb_put_packet(connection, "OK", 2);
1330 if (packet[0] == 'Z')
1332 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1334 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1339 gdb_put_packet(connection, "OK", 2);
1344 watchpoint_remove(target, address);
1345 gdb_put_packet(connection, "OK", 2);
1356 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1357 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1359 if (*retval != ERROR_OK)
1367 if ((*xml == NULL) || (!first))
1369 /* start by 0 to exercise all the code paths.
1370 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1372 *size = *size * 2 + 2;
1374 *xml = realloc(*xml, *size);
1379 *retval = ERROR_SERVER_REMOTE_CLOSED;
1387 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1389 if ((ret > 0) && ((ret + 1) < *size - *pos))
1394 /* there was just enough or not enough space, allocate more. */
1399 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1403 /* Extract and NUL-terminate the annex. */
1405 while (*buf && *buf != ':')
1411 /* After the read marker and annex, qXfer looks like a
1412 * traditional 'm' packet. */
1414 *ofs = strtoul(buf, &separator, 16);
1416 if (*separator != ',')
1419 *len = strtoul(separator+1, NULL, 16);
1424 int gdb_calc_blocksize(flash_bank_t *bank)
1427 int block_size = 0xffffffff;
1429 /* loop through all sectors and return smallest sector size */
1431 for (i = 0; i < bank->num_sectors; i++)
1433 if (bank->sectors[i].size < block_size)
1434 block_size = bank->sectors[i].size;
1440 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1442 command_context_t *cmd_ctx = connection->cmd_ctx;
1444 if (strstr(packet, "qRcmd,"))
1446 if (packet_size > 6)
1450 cmd = malloc((packet_size - 6)/2 + 1);
1451 for (i=0; i < (packet_size - 6)/2; i++)
1454 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1457 cmd[(packet_size - 6)/2] = 0x0;
1459 /* We want to print all debug output to GDB connection */
1460 log_setCallback(gdb_log_callback, connection);
1461 target_call_timer_callbacks();
1462 command_run_line(cmd_ctx, cmd);
1465 gdb_put_packet(connection, "OK", 2);
1468 else if (strstr(packet, "qCRC:"))
1470 if (packet_size > 5)
1479 /* skip command character */
1482 addr = strtoul(packet, &separator, 16);
1484 if (*separator != ',')
1486 ERROR("incomplete read memory packet received, dropping connection");
1487 return ERROR_SERVER_REMOTE_CLOSED;
1490 len = strtoul(separator + 1, NULL, 16);
1492 retval = target_checksum_memory(target, addr, len, &checksum);
1494 if (retval == ERROR_OK)
1496 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1497 gdb_put_packet(connection, gdb_reply, 9);
1501 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1508 else if (strstr(packet, "qSupported"))
1510 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1511 * disable qXfer:features:read for the moment */
1512 int retval = ERROR_OK;
1513 char *buffer = NULL;
1517 xml_printf(&retval, &buffer, &pos, &size,
1518 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1519 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1521 if (retval != ERROR_OK)
1523 gdb_send_error(connection, 01);
1527 gdb_put_packet(connection, buffer, strlen(buffer));
1532 else if (strstr(packet, "qXfer:memory-map:read::"))
1534 /* We get away with only specifying flash here. Regions that are not
1535 * specified are treated as if we provided no memory map(if not we
1536 * could detect the holes and mark them as RAM).
1537 * Normally we only execute this code once, but no big deal if we
1538 * have to regenerate it a couple of times. */
1544 int retval = ERROR_OK;
1551 /* skip command character */
1554 offset = strtoul(packet, &separator, 16);
1555 length = strtoul(separator + 1, &separator, 16);
1557 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1562 p = get_flash_bank_by_num(i);
1566 /* if device has uneven sector sizes, eg. str7, lpc
1567 * we pass the smallest sector size to gdb memory map */
1568 blocksize = gdb_calc_blocksize(p);
1570 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1571 "<property name=\"blocksize\">0x%x</property>\n" \
1573 p->base, p->size, blocksize);
1577 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1579 if (retval != ERROR_OK)
1581 gdb_send_error(connection, retval);
1585 if (offset + length > pos)
1587 length = pos - offset;
1590 char *t = malloc(length + 1);
1592 memcpy(t + 1, xml + offset, length);
1593 gdb_put_packet(connection, t, length + 1);
1599 else if (strstr(packet, "qXfer:features:read:"))
1604 int retval = ERROR_OK;
1607 unsigned int length;
1610 /* skip command character */
1613 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1615 gdb_send_error(connection, 01);
1619 if (strcmp(annex, "target.xml") != 0)
1621 gdb_send_error(connection, 01);
1625 xml_printf(&retval, &xml, &pos, &size, \
1626 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1628 if (retval != ERROR_OK)
1630 gdb_send_error(connection, retval);
1634 gdb_put_packet(connection, xml, strlen(xml) + 1);
1640 gdb_put_packet(connection, "", 0);
1644 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1646 gdb_connection_t *gdb_connection = connection->priv;
1647 gdb_service_t *gdb_service = connection->service->priv;
1650 /* if flash programming disabled - send a empty reply */
1652 if (gdb_flash_program == 0)
1654 gdb_put_packet(connection, "", 0);
1658 if (strstr(packet, "vFlashErase:"))
1661 unsigned long length;
1663 char *parse = packet + 12;
1666 ERROR("incomplete vFlashErase packet received, dropping connection");
1667 return ERROR_SERVER_REMOTE_CLOSED;
1670 addr = strtoul(parse, &parse, 16);
1672 if (*(parse++) != ',' || *parse == '\0')
1674 ERROR("incomplete vFlashErase packet received, dropping connection");
1675 return ERROR_SERVER_REMOTE_CLOSED;
1678 length = strtoul(parse, &parse, 16);
1682 ERROR("incomplete vFlashErase packet received, dropping connection");
1683 return ERROR_SERVER_REMOTE_CLOSED;
1686 /* assume all sectors need erasing - stops any problems
1687 * when flash_write is called multiple times */
1690 /* perform any target specific operations before the erase */
1691 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1694 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1696 /* GDB doesn't evaluate the actual error number returned,
1697 * treat a failed erase as an I/O error
1699 gdb_send_error(connection, EIO);
1700 ERROR("flash_erase returned %i", result);
1703 gdb_put_packet(connection, "OK", 2);
1708 if (strstr(packet, "vFlashWrite:"))
1711 unsigned long length;
1712 char *parse = packet + 12;
1716 ERROR("incomplete vFlashErase packet received, dropping connection");
1717 return ERROR_SERVER_REMOTE_CLOSED;
1719 addr = strtoul(parse, &parse, 16);
1720 if (*(parse++) != ':')
1722 ERROR("incomplete vFlashErase packet received, dropping connection");
1723 return ERROR_SERVER_REMOTE_CLOSED;
1725 length = packet_size - (parse - packet);
1727 /* create a new image if there isn't already one */
1728 if (gdb_connection->vflash_image == NULL)
1730 gdb_connection->vflash_image = malloc(sizeof(image_t));
1731 image_open(gdb_connection->vflash_image, "", "build");
1734 /* create new section with content from packet buffer */
1735 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1737 gdb_put_packet(connection, "OK", 2);
1742 if (!strcmp(packet, "vFlashDone"))
1747 /* process the flashing buffer. No need to erase as GDB
1748 * always issues a vFlashErase first. */
1749 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1751 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1752 gdb_put_packet(connection, "E.memtype", 9);
1754 gdb_send_error(connection, EIO);
1758 ERROR("flash writing failed: %s", error_str);
1764 DEBUG("wrote %u bytes from vFlash image to flash", written);
1765 gdb_put_packet(connection, "OK", 2);
1768 image_close(gdb_connection->vflash_image);
1769 free(gdb_connection->vflash_image);
1770 gdb_connection->vflash_image = NULL;
1775 gdb_put_packet(connection, "", 0);
1779 int gdb_detach(connection_t *connection, target_t *target)
1781 switch( detach_mode )
1783 case GDB_DETACH_RESUME:
1784 target->type->resume(target, 1, 0, 1, 0);
1787 case GDB_DETACH_RESET:
1788 target_process_reset(connection->cmd_ctx);
1791 case GDB_DETACH_HALT:
1792 target->type->halt(target);
1795 case GDB_DETACH_NOTHING:
1799 gdb_put_packet(connection, "OK", 2);
1804 static void gdb_log_callback(void *priv, const char *file, int line,
1805 const char *function, const char *format, va_list args)
1807 connection_t *connection = priv;
1808 gdb_connection_t *gdb_con = connection->priv;
1812 /* do not reply this using the O packet */
1816 char *t = allocPrintf(format, args);
1820 gdb_output_con(connection, t);
1825 int gdb_input_inner(connection_t *connection)
1827 gdb_service_t *gdb_service = connection->service->priv;
1828 target_t *target = gdb_service->target;
1829 char packet[GDB_BUFFER_SIZE];
1832 gdb_connection_t *gdb_con = connection->priv;
1833 static int extended_protocol = 0;
1835 /* drain input buffer */
1838 packet_size = GDB_BUFFER_SIZE-1;
1839 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1844 /* terminate with zero */
1845 packet[packet_size] = 0;
1847 DEBUG("received packet: '%s'", packet);
1849 if (packet_size > 0)
1855 /* Hct... -- set thread
1856 * we don't have threads, send empty reply */
1857 gdb_put_packet(connection, NULL, 0);
1860 retval = gdb_query_packet(connection, target, packet, packet_size);
1863 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1866 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1869 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1872 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1875 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1878 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1882 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1885 gdb_last_signal_packet(connection, target, packet, packet_size);
1890 /* We're running/stepping, in which case we can
1891 * forward log output until the target is halted */
1892 gdb_connection_t *gdb_con = connection->priv;
1893 gdb_con->frontend_state = TARGET_RUNNING;
1894 log_setCallback(gdb_log_callback, connection);
1895 gdb_step_continue_packet(connection, target, packet, packet_size);
1899 retval = gdb_v_packet(connection, target, packet, packet_size);
1902 retval = gdb_detach(connection, target);
1903 extended_protocol = 0;
1906 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1910 if (extended_protocol != 0)
1912 gdb_put_packet(connection, "OK", 2);
1913 return ERROR_SERVER_REMOTE_CLOSED;
1915 /* handle extended remote protocol */
1916 extended_protocol = 1;
1917 gdb_put_packet(connection, "OK", 2);
1920 /* handle extended restart packet */
1921 target_process_reset(connection->cmd_ctx);
1924 /* ignore unkown packets */
1925 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1926 gdb_put_packet(connection, NULL, 0);
1930 /* if a packet handler returned an error, exit input loop */
1931 if (retval != ERROR_OK)
1935 if (gdb_con->ctrl_c)
1937 if (target->state == TARGET_RUNNING)
1939 target->type->halt(target);
1940 gdb_con->ctrl_c = 0;
1944 } while (gdb_con->buf_cnt > 0);
1949 int gdb_input(connection_t *connection)
1951 int retval = gdb_input_inner(connection);
1952 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1954 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1960 gdb_service_t *gdb_service;
1961 target_t *target = targets;
1966 WARNING("no gdb ports allocated as no target has been specified");
1972 WARNING("no gdb port specified, using default port 3333");
1978 char service_name[8];
1980 snprintf(service_name, 8, "gdb-%2.2i", i);
1982 gdb_service = malloc(sizeof(gdb_service_t));
1983 gdb_service->target = target;
1985 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1987 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1990 target = target->next;
1996 /* daemon configuration command gdb_port */
1997 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2002 /* only if the port wasn't overwritten by cmdline */
2004 gdb_port = strtoul(args[0], NULL, 0);
2009 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2013 if (strcmp(args[0], "resume") == 0)
2015 detach_mode = GDB_DETACH_RESUME;
2018 else if (strcmp(args[0], "reset") == 0)
2020 detach_mode = GDB_DETACH_RESET;
2023 else if (strcmp(args[0], "halt") == 0)
2025 detach_mode = GDB_DETACH_HALT;
2028 else if (strcmp(args[0], "nothing") == 0)
2030 detach_mode = GDB_DETACH_NOTHING;
2035 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2039 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2043 if (strcmp(args[0], "enable") == 0)
2045 gdb_use_memory_map = 1;
2048 else if (strcmp(args[0], "disable") == 0)
2050 gdb_use_memory_map = 0;
2055 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2059 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2063 if (strcmp(args[0], "enable") == 0)
2065 gdb_flash_program = 1;
2068 else if (strcmp(args[0], "disable") == 0)
2070 gdb_flash_program = 0;
2075 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2079 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2083 if (strcmp(args[0], "enable") == 0)
2085 gdb_report_data_abort = 1;
2088 else if (strcmp(args[0], "disable") == 0)
2090 gdb_report_data_abort = 0;
2095 WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2099 int gdb_register_commands(command_context_t *command_context)
2101 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2102 COMMAND_CONFIG, "");
2103 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2104 COMMAND_CONFIG, "");
2105 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2106 COMMAND_CONFIG, "");
2107 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2108 COMMAND_CONFIG, "");
2109 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2110 COMMAND_CONFIG, "");