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 char local_buffer[1024];
318 local_buffer[0] = '$';
319 if (len+4 <= sizeof(local_buffer))
321 /* performance gain on smaller packets by only a single call to gdb_write() */
322 memcpy(local_buffer+1, buffer, len++);
323 local_buffer[len++] = '#';
324 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
325 local_buffer[len++] = DIGITS[my_checksum & 0xf];
326 gdb_write(connection, local_buffer, len);
330 /* larger packets are transmitted directly from caller supplied buffer
331 by several calls to gdb_write() to avoid dynamic allocation */
332 local_buffer[1] = '#';
333 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
334 local_buffer[3] = DIGITS[my_checksum & 0xf];
335 gdb_write(connection, local_buffer, 1);
336 gdb_write(connection, buffer, len);
337 gdb_write(connection, local_buffer+1, 3);
340 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
345 else if (reply == '-')
347 /* Stop sending output packets for now */
348 log_remove_callback(gdb_log_callback, connection);
349 WARNING("negative reply, retrying");
351 else if (reply == 0x3)
354 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
358 else if (reply == '-')
360 /* Stop sending output packets for now */
361 log_remove_callback(gdb_log_callback, connection);
362 WARNING("negative reply, retrying");
366 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
368 return ERROR_SERVER_REMOTE_CLOSED;
373 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
375 return ERROR_SERVER_REMOTE_CLOSED;
379 return ERROR_SERVER_REMOTE_CLOSED;
384 int gdb_put_packet(connection_t *connection, char *buffer, int len)
386 gdb_connection_t *gdb_con = connection->priv;
388 int retval = gdb_put_packet_inner(connection, buffer, len);
393 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
399 unsigned char my_checksum = 0;
400 gdb_connection_t *gdb_con = connection->priv;
406 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
409 #ifdef _DEBUG_GDB_IO_
410 DEBUG("character: '%c'", character);
418 WARNING("acknowledgment received, but no packet pending");
421 WARNING("negative acknowledgment, but no packet pending");
428 WARNING("ignoring character 0x%x", character);
431 } while (character != '$');
436 gdb_connection_t *gdb_con = connection->priv;
439 /* The common case is that we have an entire packet with no escape chars.
440 * We need to leave at least 2 bytes in the buffer to have
441 * gdb_get_char() update various bits and bobs correctly.
443 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
445 /* The compiler will struggle a bit with constant propagation and
446 * aliasing, so we help it by showing that these values do not
447 * change inside the loop
450 char *buf = gdb_con->buf_p;
451 int run = gdb_con->buf_cnt - 2;
458 if (character == '#')
460 /* Danger! character can be '#' when esc is
461 * used so we need an explicit boolean for done here.
467 if (character == '}')
469 /* data transmitted in binary mode (X packet)
470 * uses 0x7d as escape character */
471 my_checksum += character & 0xff;
474 my_checksum += character & 0xff;
475 buffer[count++] = (character ^ 0x20) & 0xff;
478 my_checksum += character & 0xff;
479 buffer[count++] = character & 0xff;
483 gdb_con->buf_cnt -= i;
489 ERROR("packet buffer too small");
490 return ERROR_GDB_BUFFER_TOO_SMALL;
493 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
496 if (character == '#')
499 if (character == '}')
501 /* data transmitted in binary mode (X packet)
502 * uses 0x7d as escape character */
503 my_checksum += character & 0xff;
504 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
506 my_checksum += character & 0xff;
507 buffer[count++] = (character ^ 0x20) & 0xff;
511 my_checksum += character & 0xff;
512 buffer[count++] = character & 0xff;
519 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
521 checksum[0] = character;
522 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
524 checksum[1] = character;
527 if (my_checksum == strtoul(checksum, NULL, 16))
529 gdb_write(connection, "+", 1);
533 WARNING("checksum error, requesting retransmission");
534 gdb_write(connection, "-", 1);
537 return ERROR_SERVER_REMOTE_CLOSED;
542 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
544 gdb_connection_t *gdb_con = connection->priv;
546 int retval = gdb_get_packet_inner(connection, buffer, len);
551 int gdb_output_con(connection_t *connection, const char* line)
556 bin_size = strlen(line);
558 hex_buffer = malloc(bin_size*2 + 2);
559 if (hex_buffer == NULL)
560 return ERROR_GDB_BUFFER_TOO_SMALL;
563 for (i=0; i<bin_size; i++)
564 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
565 hex_buffer[bin_size*2+1] = 0;
567 gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
573 int gdb_output(struct command_context_s *context, char* line)
575 /* this will be dumped to the log and also sent as an O packet if possible */
580 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
583 struct command_context_s *cmd_ctx = priv;
585 if (target->gdb_program_script)
587 script = open_file_from_path(target->gdb_program_script, "r");
590 ERROR("couldn't open script file %s", target->gdb_program_script);
594 INFO("executing gdb_program script '%s'", target->gdb_program_script);
595 command_run_file(cmd_ctx, script, COMMAND_EXEC);
598 jtag_execute_queue();
604 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
606 connection_t *connection = priv;
607 gdb_connection_t *gdb_connection = connection->priv;
613 case TARGET_EVENT_HALTED:
614 /* In the GDB protocol when we are stepping or coninuing execution,
615 * we have a lingering reply. Upon receiving a halted event
616 * when we have that lingering packet, we reply to the original
617 * step or continue packet.
619 * Executing monitor commands can bring the target in and
620 * out of the running state so we'll see lots of TARGET_EVENT_XXX
621 * that are to be ignored.
623 if (gdb_connection->frontend_state == TARGET_RUNNING)
625 /* stop forwarding log packets! */
626 log_remove_callback(gdb_log_callback, connection);
628 if (gdb_connection->ctrl_c)
631 gdb_connection->ctrl_c = 0;
635 signal = gdb_last_signal(target);
638 snprintf(sig_reply, 4, "T%2.2x", signal);
639 gdb_put_packet(connection, sig_reply, 3);
640 gdb_connection->frontend_state = TARGET_HALTED;
643 case TARGET_EVENT_GDB_PROGRAM:
644 gdb_program_handler(target, event, connection->cmd_ctx);
654 int gdb_new_connection(connection_t *connection)
656 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
657 gdb_service_t *gdb_service = connection->service->priv;
661 connection->priv = gdb_connection;
663 /* initialize gdb connection information */
664 gdb_connection->buf_p = gdb_connection->buffer;
665 gdb_connection->buf_cnt = 0;
666 gdb_connection->ctrl_c = 0;
667 gdb_connection->frontend_state = TARGET_HALTED;
668 gdb_connection->vflash_image = NULL;
669 gdb_connection->closed = 0;
670 gdb_connection->busy = 0;
672 /* send ACK to GDB for debug request */
673 gdb_write(connection, "+", 1);
675 /* output goes through gdb connection */
676 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
678 /* register callback to be informed about target events */
679 target_register_event_callback(gdb_target_callback_event_handler, connection);
681 /* a gdb session just attached, try to put the target in halt mode
682 * or alterantively try to issue a reset.
684 * GDB connection will fail if e.g. register read packets fail,
685 * otherwise resetting/halting the target could have been left to GDB init
688 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
689 (retval != ERROR_TARGET_ALREADY_HALTED))
691 ERROR("error(%d) when trying to halt target, falling back to \"reset\"", retval);
692 command_run_line(connection->cmd_ctx, "reset");
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 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 if (target->endianness == TARGET_LITTLE_ENDIAN)
774 for (i = 0; i < buf_len; i++)
776 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
777 tstr[i*2+1] = DIGITS[buf[i]&0xf];
782 for (i = 0; i < buf_len; i++)
784 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
785 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
790 void gdb_target_to_str(target_t *target, char *tstr, char *str)
792 int str_len = strlen(tstr);
797 ERROR("BUG: gdb value with uneven number of characters encountered");
801 if (target->endianness == TARGET_LITTLE_ENDIAN)
803 for (i = 0; i < str_len; i+=2)
805 str[str_len - i - 1] = tstr[i + 1];
806 str[str_len - i - 2] = tstr[i];
811 for (i = 0; i < str_len; i++)
818 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
823 int reg_packet_size = 0;
828 #ifdef _DEBUG_GDB_IO_
832 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
834 return gdb_error(connection, retval);
837 for (i = 0; i < reg_list_size; i++)
839 reg_packet_size += reg_list[i]->size;
842 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
843 reg_packet_p = reg_packet;
845 for (i = 0; i < reg_list_size; i++)
847 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
848 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
851 #ifdef _DEBUG_GDB_IO_
854 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
855 DEBUG("reg_packet: %s", reg_packet_p);
860 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
868 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
876 #ifdef _DEBUG_GDB_IO_
880 /* skip command character */
886 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
887 return ERROR_SERVER_REMOTE_CLOSED;
890 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
892 return gdb_error(connection, retval);
896 for (i = 0; i < reg_list_size; i++)
900 reg_arch_type_t *arch_type;
902 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
903 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
904 gdb_target_to_str(target, packet_p, hex_buf);
906 /* convert hex-string to binary buffer */
907 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
908 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
910 /* get register arch_type, and call set method */
911 arch_type = register_get_arch_type(reg_list[i]->arch_type);
912 if (arch_type == NULL)
914 ERROR("BUG: encountered unregistered arch type");
917 arch_type->set(reg_list[i], bin_buf);
919 /* advance packet pointer */
920 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
926 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
929 gdb_put_packet(connection, "OK", 2);
934 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
937 int reg_num = strtoul(packet + 1, NULL, 16);
942 #ifdef _DEBUG_GDB_IO_
946 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
948 return gdb_error(connection, retval);
951 if (reg_list_size <= reg_num)
953 ERROR("gdb requested a non-existing register");
957 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
959 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
961 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
969 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
974 int reg_num = strtoul(packet + 1, &separator, 16);
978 reg_arch_type_t *arch_type;
982 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
984 return gdb_error(connection, retval);
987 if (reg_list_size < reg_num)
989 ERROR("gdb requested a non-existing register");
990 return ERROR_SERVER_REMOTE_CLOSED;
993 if (*separator != '=')
995 ERROR("GDB 'set register packet', but no '=' following the register number");
996 return ERROR_SERVER_REMOTE_CLOSED;
999 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1000 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1001 gdb_target_to_str(target, separator + 1, hex_buf);
1003 /* convert hex-string to binary buffer */
1004 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1005 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1007 /* get register arch_type, and call set method */
1008 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1009 if (arch_type == NULL)
1011 ERROR("BUG: encountered unregistered arch type");
1014 arch_type->set(reg_list[reg_num], bin_buf);
1016 gdb_put_packet(connection, "OK", 2);
1025 int gdb_error(connection_t *connection, int retval)
1029 case ERROR_TARGET_DATA_ABORT:
1030 gdb_send_error(connection, EIO);
1032 case ERROR_TARGET_TRANSLATION_FAULT:
1033 gdb_send_error(connection, EFAULT);
1035 case ERROR_TARGET_UNALIGNED_ACCESS:
1036 gdb_send_error(connection, EFAULT);
1038 case ERROR_TARGET_NOT_HALTED:
1039 gdb_send_error(connection, EFAULT);
1042 /* This could be that the target reset itself. */
1043 ERROR("unexpected error %i", retval);
1044 gdb_send_error(connection, EFAULT);
1051 /* We don't have to worry about the default 2 second timeout for GDB packets,
1052 * because GDB breaks up large memory reads into smaller reads.
1054 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1056 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1065 int retval = ERROR_OK;
1067 /* skip command character */
1070 addr = strtoul(packet, &separator, 16);
1072 if (*separator != ',')
1074 ERROR("incomplete read memory packet received, dropping connection");
1075 return ERROR_SERVER_REMOTE_CLOSED;
1078 len = strtoul(separator+1, NULL, 16);
1080 buffer = malloc(len);
1082 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1084 retval = target_read_buffer(target, addr, len, buffer);
1086 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1088 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1089 * At some point this might be fixed in GDB, in which case this code can be removed.
1091 * OpenOCD developers are acutely aware of this problem, but there is nothing
1092 * gained by involving the user in this problem that hopefully will get resolved
1095 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1097 * For now, the default is to fix up things to make current GDB versions work.
1098 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1100 memset(buffer, 0, len);
1104 if (retval == ERROR_OK)
1106 hex_buffer = malloc(len * 2 + 1);
1109 for (i = 0; i < len; i++)
1112 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1113 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1116 gdb_put_packet(connection, hex_buffer, len * 2);
1122 retval = gdb_error(connection, retval);
1130 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1141 /* skip command character */
1144 addr = strtoul(packet, &separator, 16);
1146 if (*separator != ',')
1148 ERROR("incomplete write memory packet received, dropping connection");
1149 return ERROR_SERVER_REMOTE_CLOSED;
1152 len = strtoul(separator+1, &separator, 16);
1154 if (*(separator++) != ':')
1156 ERROR("incomplete write memory packet received, dropping connection");
1157 return ERROR_SERVER_REMOTE_CLOSED;
1160 buffer = malloc(len);
1162 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1164 for (i=0; i<len; i++)
1167 sscanf(separator + 2*i, "%2x", &tmp);
1171 retval = target_write_buffer(target, addr, len, buffer);
1173 if (retval == ERROR_OK)
1175 gdb_put_packet(connection, "OK", 2);
1179 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1188 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1196 /* skip command character */
1199 addr = strtoul(packet, &separator, 16);
1201 if (*separator != ',')
1203 ERROR("incomplete write memory binary packet received, dropping connection");
1204 return ERROR_SERVER_REMOTE_CLOSED;
1207 len = strtoul(separator+1, &separator, 16);
1209 if (*(separator++) != ':')
1211 ERROR("incomplete write memory binary packet received, dropping connection");
1212 return ERROR_SERVER_REMOTE_CLOSED;
1218 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1220 retval = target_write_buffer(target, addr, len, (u8*)separator);
1223 if (retval == ERROR_OK)
1225 gdb_put_packet(connection, "OK", 2);
1229 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1236 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1243 if (packet_size > 1)
1245 packet[packet_size] = 0;
1246 address = strtoul(packet + 1, NULL, 16);
1253 if (packet[0] == 'c')
1256 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1258 else if (packet[0] == 's')
1261 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1265 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1268 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1269 enum watchpoint_rw wp_type;
1277 type = strtoul(packet + 1, &separator, 16);
1279 if (type == 0) /* memory breakpoint */
1280 bp_type = BKPT_SOFT;
1281 else if (type == 1) /* hardware breakpoint */
1282 bp_type = BKPT_HARD;
1283 else if (type == 2) /* write watchpoint */
1284 wp_type = WPT_WRITE;
1285 else if (type == 3) /* read watchpoint */
1287 else if (type == 4) /* access watchpoint */
1288 wp_type = WPT_ACCESS;
1290 if (*separator != ',')
1292 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1293 return ERROR_SERVER_REMOTE_CLOSED;
1296 address = strtoul(separator+1, &separator, 16);
1298 if (*separator != ',')
1300 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1301 return ERROR_SERVER_REMOTE_CLOSED;
1304 size = strtoul(separator+1, &separator, 16);
1310 if (packet[0] == 'Z')
1312 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1314 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1319 gdb_put_packet(connection, "OK", 2);
1324 breakpoint_remove(target, address);
1325 gdb_put_packet(connection, "OK", 2);
1332 if (packet[0] == 'Z')
1334 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1336 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1341 gdb_put_packet(connection, "OK", 2);
1346 watchpoint_remove(target, address);
1347 gdb_put_packet(connection, "OK", 2);
1358 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1359 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1361 if (*retval != ERROR_OK)
1369 if ((*xml == NULL) || (!first))
1371 /* start by 0 to exercise all the code paths.
1372 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1374 *size = *size * 2 + 2;
1376 *xml = realloc(*xml, *size);
1381 *retval = ERROR_SERVER_REMOTE_CLOSED;
1389 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1391 if ((ret > 0) && ((ret + 1) < *size - *pos))
1396 /* there was just enough or not enough space, allocate more. */
1401 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1405 /* Extract and NUL-terminate the annex. */
1407 while (*buf && *buf != ':')
1413 /* After the read marker and annex, qXfer looks like a
1414 * traditional 'm' packet. */
1416 *ofs = strtoul(buf, &separator, 16);
1418 if (*separator != ',')
1421 *len = strtoul(separator+1, NULL, 16);
1426 int gdb_calc_blocksize(flash_bank_t *bank)
1429 int block_size = 0xffffffff;
1431 /* loop through all sectors and return smallest sector size */
1433 for (i = 0; i < bank->num_sectors; i++)
1435 if (bank->sectors[i].size < block_size)
1436 block_size = bank->sectors[i].size;
1442 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1444 command_context_t *cmd_ctx = connection->cmd_ctx;
1446 if (strstr(packet, "qRcmd,"))
1448 if (packet_size > 6)
1452 cmd = malloc((packet_size - 6)/2 + 1);
1453 for (i=0; i < (packet_size - 6)/2; i++)
1456 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1459 cmd[(packet_size - 6)/2] = 0x0;
1461 /* We want to print all debug output to GDB connection */
1462 log_add_callback(gdb_log_callback, connection);
1463 target_call_timer_callbacks();
1464 command_run_line(cmd_ctx, cmd);
1465 log_remove_callback(gdb_log_callback, connection);
1468 gdb_put_packet(connection, "OK", 2);
1471 else if (strstr(packet, "qCRC:"))
1473 if (packet_size > 5)
1482 /* skip command character */
1485 addr = strtoul(packet, &separator, 16);
1487 if (*separator != ',')
1489 ERROR("incomplete read memory packet received, dropping connection");
1490 return ERROR_SERVER_REMOTE_CLOSED;
1493 len = strtoul(separator + 1, NULL, 16);
1495 retval = target_checksum_memory(target, addr, len, &checksum);
1497 if (retval == ERROR_OK)
1499 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1500 gdb_put_packet(connection, gdb_reply, 9);
1504 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1511 else if (strstr(packet, "qSupported"))
1513 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1514 * disable qXfer:features:read for the moment */
1515 int retval = ERROR_OK;
1516 char *buffer = NULL;
1520 xml_printf(&retval, &buffer, &pos, &size,
1521 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1522 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1524 if (retval != ERROR_OK)
1526 gdb_send_error(connection, 01);
1530 gdb_put_packet(connection, buffer, strlen(buffer));
1535 else if (strstr(packet, "qXfer:memory-map:read::"))
1537 /* We get away with only specifying flash here. Regions that are not
1538 * specified are treated as if we provided no memory map(if not we
1539 * could detect the holes and mark them as RAM).
1540 * Normally we only execute this code once, but no big deal if we
1541 * have to regenerate it a couple of times. */
1547 int retval = ERROR_OK;
1554 /* skip command character */
1557 offset = strtoul(packet, &separator, 16);
1558 length = strtoul(separator + 1, &separator, 16);
1560 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1563 for (i=0; i<flash_get_bank_count(); i++)
1565 p = get_flash_bank_by_num(i);
1569 /* if device has uneven sector sizes, eg. str7, lpc
1570 * we pass the smallest sector size to gdb memory map */
1571 blocksize = gdb_calc_blocksize(p);
1573 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1574 "<property name=\"blocksize\">0x%x</property>\n" \
1576 p->base, p->size, blocksize);
1579 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1581 if (retval != ERROR_OK)
1583 gdb_send_error(connection, retval);
1587 if (offset + length > pos)
1589 length = pos - offset;
1592 char *t = malloc(length + 1);
1594 memcpy(t + 1, xml + offset, length);
1595 gdb_put_packet(connection, t, length + 1);
1601 else if (strstr(packet, "qXfer:features:read:"))
1606 int retval = ERROR_OK;
1609 unsigned int length;
1612 /* skip command character */
1615 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1617 gdb_send_error(connection, 01);
1621 if (strcmp(annex, "target.xml") != 0)
1623 gdb_send_error(connection, 01);
1627 xml_printf(&retval, &xml, &pos, &size, \
1628 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1630 if (retval != ERROR_OK)
1632 gdb_send_error(connection, retval);
1636 gdb_put_packet(connection, xml, strlen(xml) + 1);
1642 gdb_put_packet(connection, "", 0);
1646 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1648 gdb_connection_t *gdb_connection = connection->priv;
1649 gdb_service_t *gdb_service = connection->service->priv;
1652 /* if flash programming disabled - send a empty reply */
1654 if (gdb_flash_program == 0)
1656 gdb_put_packet(connection, "", 0);
1660 if (strstr(packet, "vFlashErase:"))
1663 unsigned long length;
1665 char *parse = packet + 12;
1668 ERROR("incomplete vFlashErase packet received, dropping connection");
1669 return ERROR_SERVER_REMOTE_CLOSED;
1672 addr = strtoul(parse, &parse, 16);
1674 if (*(parse++) != ',' || *parse == '\0')
1676 ERROR("incomplete vFlashErase packet received, dropping connection");
1677 return ERROR_SERVER_REMOTE_CLOSED;
1680 length = strtoul(parse, &parse, 16);
1684 ERROR("incomplete vFlashErase packet received, dropping connection");
1685 return ERROR_SERVER_REMOTE_CLOSED;
1688 /* assume all sectors need erasing - stops any problems
1689 * when flash_write is called multiple times */
1692 /* perform any target specific operations before the erase */
1693 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1696 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1698 /* GDB doesn't evaluate the actual error number returned,
1699 * treat a failed erase as an I/O error
1701 gdb_send_error(connection, EIO);
1702 ERROR("flash_erase returned %i", result);
1705 gdb_put_packet(connection, "OK", 2);
1710 if (strstr(packet, "vFlashWrite:"))
1713 unsigned long length;
1714 char *parse = packet + 12;
1718 ERROR("incomplete vFlashErase packet received, dropping connection");
1719 return ERROR_SERVER_REMOTE_CLOSED;
1721 addr = strtoul(parse, &parse, 16);
1722 if (*(parse++) != ':')
1724 ERROR("incomplete vFlashErase packet received, dropping connection");
1725 return ERROR_SERVER_REMOTE_CLOSED;
1727 length = packet_size - (parse - packet);
1729 /* create a new image if there isn't already one */
1730 if (gdb_connection->vflash_image == NULL)
1732 gdb_connection->vflash_image = malloc(sizeof(image_t));
1733 image_open(gdb_connection->vflash_image, "", "build");
1736 /* create new section with content from packet buffer */
1737 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1739 gdb_put_packet(connection, "OK", 2);
1744 if (!strcmp(packet, "vFlashDone"))
1748 /* process the flashing buffer. No need to erase as GDB
1749 * always issues a vFlashErase first. */
1750 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK)
1752 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1753 gdb_put_packet(connection, "E.memtype", 9);
1755 gdb_send_error(connection, EIO);
1759 DEBUG("wrote %u bytes from vFlash image to flash", written);
1760 gdb_put_packet(connection, "OK", 2);
1763 image_close(gdb_connection->vflash_image);
1764 free(gdb_connection->vflash_image);
1765 gdb_connection->vflash_image = NULL;
1770 gdb_put_packet(connection, "", 0);
1774 int gdb_detach(connection_t *connection, target_t *target)
1776 switch( detach_mode )
1778 case GDB_DETACH_RESUME:
1779 target->type->resume(target, 1, 0, 1, 0);
1782 case GDB_DETACH_RESET:
1783 target_process_reset(connection->cmd_ctx);
1786 case GDB_DETACH_HALT:
1787 target->type->halt(target);
1790 case GDB_DETACH_NOTHING:
1794 gdb_put_packet(connection, "OK", 2);
1799 static void gdb_log_callback(void *priv, const char *file, int line,
1800 const char *function, const char *string)
1802 connection_t *connection = priv;
1803 gdb_connection_t *gdb_con = connection->priv;
1807 /* do not reply this using the O packet */
1811 gdb_output_con(connection, string);
1814 int gdb_input_inner(connection_t *connection)
1816 gdb_service_t *gdb_service = connection->service->priv;
1817 target_t *target = gdb_service->target;
1818 char packet[GDB_BUFFER_SIZE];
1821 gdb_connection_t *gdb_con = connection->priv;
1822 static int extended_protocol = 0;
1824 /* drain input buffer */
1827 packet_size = GDB_BUFFER_SIZE-1;
1828 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1833 /* terminate with zero */
1834 packet[packet_size] = 0;
1836 DEBUG("received packet: '%s'", packet);
1838 if (packet_size > 0)
1844 /* Hct... -- set thread
1845 * we don't have threads, send empty reply */
1846 gdb_put_packet(connection, NULL, 0);
1849 retval = gdb_query_packet(connection, target, packet, packet_size);
1852 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1855 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1858 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1861 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1864 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1867 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1871 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1874 gdb_last_signal_packet(connection, target, packet, packet_size);
1879 /* We're running/stepping, in which case we can
1880 * forward log output until the target is halted */
1881 gdb_connection_t *gdb_con = connection->priv;
1882 gdb_con->frontend_state = TARGET_RUNNING;
1883 log_add_callback(gdb_log_callback, connection);
1884 gdb_step_continue_packet(connection, target, packet, packet_size);
1888 retval = gdb_v_packet(connection, target, packet, packet_size);
1891 retval = gdb_detach(connection, target);
1892 extended_protocol = 0;
1895 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1899 if (extended_protocol != 0)
1901 gdb_put_packet(connection, "OK", 2);
1902 return ERROR_SERVER_REMOTE_CLOSED;
1904 /* handle extended remote protocol */
1905 extended_protocol = 1;
1906 gdb_put_packet(connection, "OK", 2);
1909 /* handle extended restart packet */
1910 target_process_reset(connection->cmd_ctx);
1913 /* ignore unkown packets */
1914 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1915 gdb_put_packet(connection, NULL, 0);
1919 /* if a packet handler returned an error, exit input loop */
1920 if (retval != ERROR_OK)
1924 if (gdb_con->ctrl_c)
1926 if (target->state == TARGET_RUNNING)
1928 target->type->halt(target);
1929 gdb_con->ctrl_c = 0;
1933 } while (gdb_con->buf_cnt > 0);
1938 int gdb_input(connection_t *connection)
1940 int retval = gdb_input_inner(connection);
1941 gdb_connection_t *gdb_con = connection->priv;
1942 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1945 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
1946 if (gdb_con->closed)
1947 return ERROR_SERVER_REMOTE_CLOSED;
1949 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1955 gdb_service_t *gdb_service;
1956 target_t *target = targets;
1961 WARNING("no gdb ports allocated as no target has been specified");
1967 WARNING("no gdb port specified, using default port 3333");
1973 char service_name[8];
1975 snprintf(service_name, 8, "gdb-%2.2i", i);
1977 gdb_service = malloc(sizeof(gdb_service_t));
1978 gdb_service->target = target;
1980 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1982 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1985 target = target->next;
1991 /* daemon configuration command gdb_port */
1992 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1997 /* only if the port wasn't overwritten by cmdline */
1999 gdb_port = strtoul(args[0], NULL, 0);
2004 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2008 if (strcmp(args[0], "resume") == 0)
2010 detach_mode = GDB_DETACH_RESUME;
2013 else if (strcmp(args[0], "reset") == 0)
2015 detach_mode = GDB_DETACH_RESET;
2018 else if (strcmp(args[0], "halt") == 0)
2020 detach_mode = GDB_DETACH_HALT;
2023 else if (strcmp(args[0], "nothing") == 0)
2025 detach_mode = GDB_DETACH_NOTHING;
2030 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2034 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2038 if (strcmp(args[0], "enable") == 0)
2040 gdb_use_memory_map = 1;
2043 else if (strcmp(args[0], "disable") == 0)
2045 gdb_use_memory_map = 0;
2050 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2054 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2058 if (strcmp(args[0], "enable") == 0)
2060 gdb_flash_program = 1;
2063 else if (strcmp(args[0], "disable") == 0)
2065 gdb_flash_program = 0;
2070 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2074 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2078 if (strcmp(args[0], "enable") == 0)
2080 gdb_report_data_abort = 1;
2083 else if (strcmp(args[0], "disable") == 0)
2085 gdb_report_data_abort = 0;
2090 WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2094 int gdb_register_commands(command_context_t *command_context)
2096 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2097 COMMAND_CONFIG, "");
2098 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2099 COMMAND_CONFIG, "");
2100 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2101 COMMAND_CONFIG, "");
2102 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2103 COMMAND_CONFIG, "");
2104 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2105 COMMAND_CONFIG, "");