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.
685 * If the halt fails(e.g. target needs a reset, JTAG communication not
686 * working, etc.), then the GDB connect will succeed as
687 * the get_gdb_reg_list() will lie and return a register list with
690 * This allows GDB monitor commands to be run from a GDB init script to
691 * initialize the target
693 * Also, since the halt() is asynchronous target connect will be
694 * instantaneous and thus avoiding annoying timeout problems during
697 gdb_service->target->type->halt(gdb_service->target);
699 /* remove the initial ACK from the incoming buffer */
700 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
703 /* FIX!!!??? would we actually ever receive a + here???
706 if (initial_ack != '+')
707 gdb_putback_char(connection, initial_ack);
712 int gdb_connection_closed(connection_t *connection)
714 gdb_service_t *gdb_service = connection->service->priv;
715 gdb_connection_t *gdb_connection = connection->priv;
717 /* see if an image built with vFlash commands is left */
718 if (gdb_connection->vflash_image)
720 image_close(gdb_connection->vflash_image);
721 free(gdb_connection->vflash_image);
722 gdb_connection->vflash_image = NULL;
725 /* if this connection registered a debug-message receiver delete it */
726 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
728 if (connection->priv)
730 free(connection->priv);
731 connection->priv = NULL;
735 ERROR("BUG: connection->priv == NULL");
738 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
739 log_remove_callback(gdb_log_callback, connection);
744 void gdb_send_error(connection_t *connection, u8 the_error)
747 snprintf(err, 4, "E%2.2X", the_error );
748 gdb_put_packet(connection, err, 3);
751 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
756 signal = gdb_last_signal(target);
758 snprintf(sig_reply, 4, "S%2.2x", signal);
759 gdb_put_packet(connection, sig_reply, 3);
764 /* Convert register to string of bits. NB! The # of bits in the
765 * register might be non-divisible by 8(a byte), in which
766 * case an entire byte is shown. */
767 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
774 buf_len = CEIL(reg->size, 8);
776 for (i = 0; i < buf_len; i++)
778 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
779 tstr[i*2+1] = DIGITS[buf[i]&0xf];
783 void gdb_target_to_str(target_t *target, char *tstr, char *str)
785 int str_len = strlen(tstr);
790 ERROR("BUG: gdb value with uneven number of characters encountered");
794 for (i = 0; i < str_len; i+=2)
796 str[str_len - i - 1] = tstr[i + 1];
797 str[str_len - i - 2] = tstr[i];
801 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
806 int reg_packet_size = 0;
811 #ifdef _DEBUG_GDB_IO_
815 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
817 return gdb_error(connection, retval);
820 for (i = 0; i < reg_list_size; i++)
822 reg_packet_size += reg_list[i]->size;
825 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
826 reg_packet_p = reg_packet;
828 for (i = 0; i < reg_list_size; i++)
830 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
831 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
834 #ifdef _DEBUG_GDB_IO_
837 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
838 DEBUG("reg_packet: %s", reg_packet_p);
843 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
851 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
859 #ifdef _DEBUG_GDB_IO_
863 /* skip command character */
869 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
870 return ERROR_SERVER_REMOTE_CLOSED;
873 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
875 return gdb_error(connection, retval);
879 for (i = 0; i < reg_list_size; i++)
883 reg_arch_type_t *arch_type;
885 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
886 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
887 gdb_target_to_str(target, packet_p, hex_buf);
889 /* convert hex-string to binary buffer */
890 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
891 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
893 /* get register arch_type, and call set method */
894 arch_type = register_get_arch_type(reg_list[i]->arch_type);
895 if (arch_type == NULL)
897 ERROR("BUG: encountered unregistered arch type");
900 arch_type->set(reg_list[i], bin_buf);
902 /* advance packet pointer */
903 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
909 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
912 gdb_put_packet(connection, "OK", 2);
917 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
920 int reg_num = strtoul(packet + 1, NULL, 16);
925 #ifdef _DEBUG_GDB_IO_
929 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
931 return gdb_error(connection, retval);
934 if (reg_list_size <= reg_num)
936 ERROR("gdb requested a non-existing register");
940 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
942 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
944 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
952 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
957 int reg_num = strtoul(packet + 1, &separator, 16);
961 reg_arch_type_t *arch_type;
965 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
967 return gdb_error(connection, retval);
970 if (reg_list_size < reg_num)
972 ERROR("gdb requested a non-existing register");
973 return ERROR_SERVER_REMOTE_CLOSED;
976 if (*separator != '=')
978 ERROR("GDB 'set register packet', but no '=' following the register number");
979 return ERROR_SERVER_REMOTE_CLOSED;
982 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
983 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
984 gdb_target_to_str(target, separator + 1, hex_buf);
986 /* convert hex-string to binary buffer */
987 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
988 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
990 /* get register arch_type, and call set method */
991 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
992 if (arch_type == NULL)
994 ERROR("BUG: encountered unregistered arch type");
997 arch_type->set(reg_list[reg_num], bin_buf);
999 gdb_put_packet(connection, "OK", 2);
1008 int gdb_error(connection_t *connection, int retval)
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);
1021 case ERROR_TARGET_NOT_HALTED:
1022 gdb_send_error(connection, EFAULT);
1025 /* This could be that the target reset itself. */
1026 ERROR("unexpected error %i", retval);
1027 gdb_send_error(connection, EFAULT);
1034 /* We don't have to worry about the default 2 second timeout for GDB packets,
1035 * because GDB breaks up large memory reads into smaller reads.
1037 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1039 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1048 int retval = ERROR_OK;
1050 /* skip command character */
1053 addr = strtoul(packet, &separator, 16);
1055 if (*separator != ',')
1057 ERROR("incomplete read memory packet received, dropping connection");
1058 return ERROR_SERVER_REMOTE_CLOSED;
1061 len = strtoul(separator+1, NULL, 16);
1063 buffer = malloc(len);
1065 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1067 retval = target_read_buffer(target, addr, len, buffer);
1069 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1071 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1072 * At some point this might be fixed in GDB, in which case this code can be removed.
1074 * OpenOCD developers are acutely aware of this problem, but there is nothing
1075 * gained by involving the user in this problem that hopefully will get resolved
1078 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1080 * For now, the default is to fix up things to make current GDB versions work.
1081 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1083 memset(buffer, 0, len);
1087 if (retval == ERROR_OK)
1089 hex_buffer = malloc(len * 2 + 1);
1092 for (i = 0; i < len; i++)
1095 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1096 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1099 gdb_put_packet(connection, hex_buffer, len * 2);
1105 retval = gdb_error(connection, retval);
1113 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1124 /* skip command character */
1127 addr = strtoul(packet, &separator, 16);
1129 if (*separator != ',')
1131 ERROR("incomplete write memory packet received, dropping connection");
1132 return ERROR_SERVER_REMOTE_CLOSED;
1135 len = strtoul(separator+1, &separator, 16);
1137 if (*(separator++) != ':')
1139 ERROR("incomplete write memory packet received, dropping connection");
1140 return ERROR_SERVER_REMOTE_CLOSED;
1143 buffer = malloc(len);
1145 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1147 for (i=0; i<len; i++)
1150 sscanf(separator + 2*i, "%2x", &tmp);
1154 retval = target_write_buffer(target, addr, len, buffer);
1156 if (retval == ERROR_OK)
1158 gdb_put_packet(connection, "OK", 2);
1162 retval = gdb_error(connection, retval);
1170 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1178 /* skip command character */
1181 addr = strtoul(packet, &separator, 16);
1183 if (*separator != ',')
1185 ERROR("incomplete write memory binary packet received, dropping connection");
1186 return ERROR_SERVER_REMOTE_CLOSED;
1189 len = strtoul(separator+1, &separator, 16);
1191 if (*(separator++) != ':')
1193 ERROR("incomplete write memory binary packet received, dropping connection");
1194 return ERROR_SERVER_REMOTE_CLOSED;
1200 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1202 retval = target_write_buffer(target, addr, len, (u8*)separator);
1205 if (retval == ERROR_OK)
1207 gdb_put_packet(connection, "OK", 2);
1211 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1218 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1225 if (packet_size > 1)
1227 packet[packet_size] = 0;
1228 address = strtoul(packet + 1, NULL, 16);
1235 if (packet[0] == 'c')
1238 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1240 else if (packet[0] == 's')
1243 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1247 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1250 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1251 enum watchpoint_rw wp_type;
1259 type = strtoul(packet + 1, &separator, 16);
1261 if (type == 0) /* memory breakpoint */
1262 bp_type = BKPT_SOFT;
1263 else if (type == 1) /* hardware breakpoint */
1264 bp_type = BKPT_HARD;
1265 else if (type == 2) /* write watchpoint */
1266 wp_type = WPT_WRITE;
1267 else if (type == 3) /* read watchpoint */
1269 else if (type == 4) /* access watchpoint */
1270 wp_type = WPT_ACCESS;
1272 if (*separator != ',')
1274 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1275 return ERROR_SERVER_REMOTE_CLOSED;
1278 address = strtoul(separator+1, &separator, 16);
1280 if (*separator != ',')
1282 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1283 return ERROR_SERVER_REMOTE_CLOSED;
1286 size = strtoul(separator+1, &separator, 16);
1292 if (packet[0] == 'Z')
1294 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1296 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1301 gdb_put_packet(connection, "OK", 2);
1306 breakpoint_remove(target, address);
1307 gdb_put_packet(connection, "OK", 2);
1314 if (packet[0] == 'Z')
1316 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1318 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1323 gdb_put_packet(connection, "OK", 2);
1328 watchpoint_remove(target, address);
1329 gdb_put_packet(connection, "OK", 2);
1340 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1341 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1343 if (*retval != ERROR_OK)
1351 if ((*xml == NULL) || (!first))
1353 /* start by 0 to exercise all the code paths.
1354 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1356 *size = *size * 2 + 2;
1358 *xml = realloc(*xml, *size);
1363 *retval = ERROR_SERVER_REMOTE_CLOSED;
1371 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1373 if ((ret > 0) && ((ret + 1) < *size - *pos))
1378 /* there was just enough or not enough space, allocate more. */
1383 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1387 /* Extract and NUL-terminate the annex. */
1389 while (*buf && *buf != ':')
1395 /* After the read marker and annex, qXfer looks like a
1396 * traditional 'm' packet. */
1398 *ofs = strtoul(buf, &separator, 16);
1400 if (*separator != ',')
1403 *len = strtoul(separator+1, NULL, 16);
1408 int gdb_calc_blocksize(flash_bank_t *bank)
1411 int block_size = 0xffffffff;
1413 /* loop through all sectors and return smallest sector size */
1415 for (i = 0; i < bank->num_sectors; i++)
1417 if (bank->sectors[i].size < block_size)
1418 block_size = bank->sectors[i].size;
1424 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1426 command_context_t *cmd_ctx = connection->cmd_ctx;
1428 if (strstr(packet, "qRcmd,"))
1430 if (packet_size > 6)
1434 cmd = malloc((packet_size - 6)/2 + 1);
1435 for (i=0; i < (packet_size - 6)/2; i++)
1438 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1441 cmd[(packet_size - 6)/2] = 0x0;
1443 /* We want to print all debug output to GDB connection */
1444 log_add_callback(gdb_log_callback, connection);
1445 target_call_timer_callbacks();
1446 command_run_line(cmd_ctx, cmd);
1447 target_call_timer_callbacks();
1448 log_remove_callback(gdb_log_callback, connection);
1451 gdb_put_packet(connection, "OK", 2);
1454 else if (strstr(packet, "qCRC:"))
1456 if (packet_size > 5)
1465 /* skip command character */
1468 addr = strtoul(packet, &separator, 16);
1470 if (*separator != ',')
1472 ERROR("incomplete read memory packet received, dropping connection");
1473 return ERROR_SERVER_REMOTE_CLOSED;
1476 len = strtoul(separator + 1, NULL, 16);
1478 retval = target_checksum_memory(target, addr, len, &checksum);
1480 if (retval == ERROR_OK)
1482 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1483 gdb_put_packet(connection, gdb_reply, 9);
1487 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1494 else if (strstr(packet, "qSupported"))
1496 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1497 * disable qXfer:features:read for the moment */
1498 int retval = ERROR_OK;
1499 char *buffer = NULL;
1503 xml_printf(&retval, &buffer, &pos, &size,
1504 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1505 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1507 if (retval != ERROR_OK)
1509 gdb_send_error(connection, 01);
1513 gdb_put_packet(connection, buffer, strlen(buffer));
1518 else if (strstr(packet, "qXfer:memory-map:read::"))
1520 /* We get away with only specifying flash here. Regions that are not
1521 * specified are treated as if we provided no memory map(if not we
1522 * could detect the holes and mark them as RAM).
1523 * Normally we only execute this code once, but no big deal if we
1524 * have to regenerate it a couple of times. */
1530 int retval = ERROR_OK;
1537 /* skip command character */
1540 offset = strtoul(packet, &separator, 16);
1541 length = strtoul(separator + 1, &separator, 16);
1543 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1546 for (i=0; i<flash_get_bank_count(); i++)
1548 p = get_flash_bank_by_num(i);
1552 /* if device has uneven sector sizes, eg. str7, lpc
1553 * we pass the smallest sector size to gdb memory map */
1554 blocksize = gdb_calc_blocksize(p);
1556 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1557 "<property name=\"blocksize\">0x%x</property>\n" \
1559 p->base, p->size, blocksize);
1562 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1564 if (retval != ERROR_OK)
1566 gdb_send_error(connection, retval);
1570 if (offset + length > pos)
1572 length = pos - offset;
1575 char *t = malloc(length + 1);
1577 memcpy(t + 1, xml + offset, length);
1578 gdb_put_packet(connection, t, length + 1);
1584 else if (strstr(packet, "qXfer:features:read:"))
1589 int retval = ERROR_OK;
1592 unsigned int length;
1595 /* skip command character */
1598 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1600 gdb_send_error(connection, 01);
1604 if (strcmp(annex, "target.xml") != 0)
1606 gdb_send_error(connection, 01);
1610 xml_printf(&retval, &xml, &pos, &size, \
1611 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1613 if (retval != ERROR_OK)
1615 gdb_send_error(connection, retval);
1619 gdb_put_packet(connection, xml, strlen(xml) + 1);
1625 gdb_put_packet(connection, "", 0);
1629 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1631 gdb_connection_t *gdb_connection = connection->priv;
1632 gdb_service_t *gdb_service = connection->service->priv;
1635 /* if flash programming disabled - send a empty reply */
1637 if (gdb_flash_program == 0)
1639 gdb_put_packet(connection, "", 0);
1643 if (strstr(packet, "vFlashErase:"))
1646 unsigned long length;
1648 char *parse = packet + 12;
1651 ERROR("incomplete vFlashErase packet received, dropping connection");
1652 return ERROR_SERVER_REMOTE_CLOSED;
1655 addr = strtoul(parse, &parse, 16);
1657 if (*(parse++) != ',' || *parse == '\0')
1659 ERROR("incomplete vFlashErase packet received, dropping connection");
1660 return ERROR_SERVER_REMOTE_CLOSED;
1663 length = strtoul(parse, &parse, 16);
1667 ERROR("incomplete vFlashErase packet received, dropping connection");
1668 return ERROR_SERVER_REMOTE_CLOSED;
1671 /* assume all sectors need erasing - stops any problems
1672 * when flash_write is called multiple times */
1675 /* perform any target specific operations before the erase */
1676 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1679 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1681 /* GDB doesn't evaluate the actual error number returned,
1682 * treat a failed erase as an I/O error
1684 gdb_send_error(connection, EIO);
1685 ERROR("flash_erase returned %i", result);
1688 gdb_put_packet(connection, "OK", 2);
1693 if (strstr(packet, "vFlashWrite:"))
1696 unsigned long length;
1697 char *parse = packet + 12;
1701 ERROR("incomplete vFlashErase packet received, dropping connection");
1702 return ERROR_SERVER_REMOTE_CLOSED;
1704 addr = strtoul(parse, &parse, 16);
1705 if (*(parse++) != ':')
1707 ERROR("incomplete vFlashErase packet received, dropping connection");
1708 return ERROR_SERVER_REMOTE_CLOSED;
1710 length = packet_size - (parse - packet);
1712 /* create a new image if there isn't already one */
1713 if (gdb_connection->vflash_image == NULL)
1715 gdb_connection->vflash_image = malloc(sizeof(image_t));
1716 image_open(gdb_connection->vflash_image, "", "build");
1719 /* create new section with content from packet buffer */
1720 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1722 gdb_put_packet(connection, "OK", 2);
1727 if (!strcmp(packet, "vFlashDone"))
1731 /* process the flashing buffer. No need to erase as GDB
1732 * always issues a vFlashErase first. */
1733 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK)
1735 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1736 gdb_put_packet(connection, "E.memtype", 9);
1738 gdb_send_error(connection, EIO);
1742 DEBUG("wrote %u bytes from vFlash image to flash", written);
1743 gdb_put_packet(connection, "OK", 2);
1746 image_close(gdb_connection->vflash_image);
1747 free(gdb_connection->vflash_image);
1748 gdb_connection->vflash_image = NULL;
1753 gdb_put_packet(connection, "", 0);
1757 int gdb_detach(connection_t *connection, target_t *target)
1759 switch( detach_mode )
1761 case GDB_DETACH_RESUME:
1762 target->type->resume(target, 1, 0, 1, 0);
1765 case GDB_DETACH_RESET:
1766 target_process_reset(connection->cmd_ctx);
1769 case GDB_DETACH_HALT:
1770 target->type->halt(target);
1773 case GDB_DETACH_NOTHING:
1777 gdb_put_packet(connection, "OK", 2);
1782 static void gdb_log_callback(void *priv, const char *file, int line,
1783 const char *function, const char *string)
1785 connection_t *connection = priv;
1786 gdb_connection_t *gdb_con = connection->priv;
1790 /* do not reply this using the O packet */
1794 gdb_output_con(connection, string);
1797 int gdb_input_inner(connection_t *connection)
1799 gdb_service_t *gdb_service = connection->service->priv;
1800 target_t *target = gdb_service->target;
1801 char packet[GDB_BUFFER_SIZE];
1804 gdb_connection_t *gdb_con = connection->priv;
1805 static int extended_protocol = 0;
1807 /* drain input buffer */
1810 packet_size = GDB_BUFFER_SIZE-1;
1811 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1816 /* terminate with zero */
1817 packet[packet_size] = 0;
1819 DEBUG("received packet: '%s'", packet);
1821 if (packet_size > 0)
1827 /* Hct... -- set thread
1828 * we don't have threads, send empty reply */
1829 gdb_put_packet(connection, NULL, 0);
1832 retval = gdb_query_packet(connection, target, packet, packet_size);
1835 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1838 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1841 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1844 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1847 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1850 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1854 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1857 gdb_last_signal_packet(connection, target, packet, packet_size);
1862 /* We're running/stepping, in which case we can
1863 * forward log output until the target is halted */
1864 gdb_connection_t *gdb_con = connection->priv;
1865 gdb_con->frontend_state = TARGET_RUNNING;
1866 log_add_callback(gdb_log_callback, connection);
1867 gdb_step_continue_packet(connection, target, packet, packet_size);
1871 retval = gdb_v_packet(connection, target, packet, packet_size);
1874 retval = gdb_detach(connection, target);
1875 extended_protocol = 0;
1878 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1882 if (extended_protocol != 0)
1884 gdb_put_packet(connection, "OK", 2);
1885 return ERROR_SERVER_REMOTE_CLOSED;
1887 /* handle extended remote protocol */
1888 extended_protocol = 1;
1889 gdb_put_packet(connection, "OK", 2);
1892 /* handle extended restart packet */
1893 target_process_reset(connection->cmd_ctx);
1896 /* ignore unkown packets */
1897 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1898 gdb_put_packet(connection, NULL, 0);
1902 /* if a packet handler returned an error, exit input loop */
1903 if (retval != ERROR_OK)
1907 if (gdb_con->ctrl_c)
1909 if (target->state == TARGET_RUNNING)
1911 target->type->halt(target);
1912 gdb_con->ctrl_c = 0;
1916 } while (gdb_con->buf_cnt > 0);
1921 int gdb_input(connection_t *connection)
1923 int retval = gdb_input_inner(connection);
1924 gdb_connection_t *gdb_con = connection->priv;
1925 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1928 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
1929 if (gdb_con->closed)
1930 return ERROR_SERVER_REMOTE_CLOSED;
1932 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1938 gdb_service_t *gdb_service;
1939 target_t *target = targets;
1944 WARNING("no gdb ports allocated as no target has been specified");
1950 WARNING("no gdb port specified, using default port 3333");
1956 char service_name[8];
1958 snprintf(service_name, 8, "gdb-%2.2i", i);
1960 gdb_service = malloc(sizeof(gdb_service_t));
1961 gdb_service->target = target;
1963 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1965 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1968 target = target->next;
1974 /* daemon configuration command gdb_port */
1975 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1980 /* only if the port wasn't overwritten by cmdline */
1982 gdb_port = strtoul(args[0], NULL, 0);
1987 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1991 if (strcmp(args[0], "resume") == 0)
1993 detach_mode = GDB_DETACH_RESUME;
1996 else if (strcmp(args[0], "reset") == 0)
1998 detach_mode = GDB_DETACH_RESET;
2001 else if (strcmp(args[0], "halt") == 0)
2003 detach_mode = GDB_DETACH_HALT;
2006 else if (strcmp(args[0], "nothing") == 0)
2008 detach_mode = GDB_DETACH_NOTHING;
2013 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2017 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2021 if (strcmp(args[0], "enable") == 0)
2023 gdb_use_memory_map = 1;
2026 else if (strcmp(args[0], "disable") == 0)
2028 gdb_use_memory_map = 0;
2033 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2037 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2041 if (strcmp(args[0], "enable") == 0)
2043 gdb_flash_program = 1;
2046 else if (strcmp(args[0], "disable") == 0)
2048 gdb_flash_program = 0;
2053 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2057 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2061 if (strcmp(args[0], "enable") == 0)
2063 gdb_report_data_abort = 1;
2066 else if (strcmp(args[0], "disable") == 0)
2068 gdb_report_data_abort = 0;
2073 WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2077 int gdb_register_commands(command_context_t *command_context)
2079 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2080 COMMAND_CONFIG, "");
2081 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2082 COMMAND_CONFIG, "");
2083 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2084 COMMAND_CONFIG, "");
2085 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2086 COMMAND_CONFIG, "");
2087 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2088 COMMAND_CONFIG, "");