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
689 * We need a synchronous halt, lest connect will fail.
690 * Also there is no guarantee that poll() will be invoked
691 * between here and serving the first packet, so the halt()
692 * statement above is *NOT* sufficient
694 if ((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK)
696 ERROR("error(%d) when trying to halt target, falling back to \"reset\"", retval);
697 command_run_line(connection->cmd_ctx, "reset");
699 command_run_line(connection->cmd_ctx, "halt");
701 /* remove the initial ACK from the incoming buffer */
702 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
705 /* FIX!!!??? would we actually ever receive a + here???
708 if (initial_ack != '+')
709 gdb_putback_char(connection, initial_ack);
714 int gdb_connection_closed(connection_t *connection)
716 gdb_service_t *gdb_service = connection->service->priv;
717 gdb_connection_t *gdb_connection = connection->priv;
719 /* see if an image built with vFlash commands is left */
720 if (gdb_connection->vflash_image)
722 image_close(gdb_connection->vflash_image);
723 free(gdb_connection->vflash_image);
724 gdb_connection->vflash_image = NULL;
727 /* if this connection registered a debug-message receiver delete it */
728 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
730 if (connection->priv)
732 free(connection->priv);
733 connection->priv = NULL;
737 ERROR("BUG: connection->priv == NULL");
740 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
741 log_remove_callback(gdb_log_callback, connection);
746 void gdb_send_error(connection_t *connection, u8 the_error)
749 snprintf(err, 4, "E%2.2X", the_error );
750 gdb_put_packet(connection, err, 3);
753 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
758 signal = gdb_last_signal(target);
760 snprintf(sig_reply, 4, "S%2.2x", signal);
761 gdb_put_packet(connection, sig_reply, 3);
766 /* Convert register to string of bits. NB! The # of bits in the
767 * register might be non-divisible by 8(a byte), in which
768 * case an entire byte is shown. */
769 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
776 buf_len = CEIL(reg->size, 8);
778 if (target->endianness == TARGET_LITTLE_ENDIAN)
780 for (i = 0; i < buf_len; i++)
782 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
783 tstr[i*2+1] = DIGITS[buf[i]&0xf];
788 for (i = 0; i < buf_len; i++)
790 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
791 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
796 void gdb_target_to_str(target_t *target, char *tstr, char *str)
798 int str_len = strlen(tstr);
803 ERROR("BUG: gdb value with uneven number of characters encountered");
807 if (target->endianness == TARGET_LITTLE_ENDIAN)
809 for (i = 0; i < str_len; i+=2)
811 str[str_len - i - 1] = tstr[i + 1];
812 str[str_len - i - 2] = tstr[i];
817 for (i = 0; i < str_len; i++)
824 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
829 int reg_packet_size = 0;
834 #ifdef _DEBUG_GDB_IO_
838 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
840 return gdb_error(connection, retval);
843 for (i = 0; i < reg_list_size; i++)
845 reg_packet_size += reg_list[i]->size;
848 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
849 reg_packet_p = reg_packet;
851 for (i = 0; i < reg_list_size; i++)
853 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
854 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
857 #ifdef _DEBUG_GDB_IO_
860 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
861 DEBUG("reg_packet: %s", reg_packet_p);
866 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
874 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
882 #ifdef _DEBUG_GDB_IO_
886 /* skip command character */
892 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
893 return ERROR_SERVER_REMOTE_CLOSED;
896 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
898 return gdb_error(connection, retval);
902 for (i = 0; i < reg_list_size; i++)
906 reg_arch_type_t *arch_type;
908 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
909 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
910 gdb_target_to_str(target, packet_p, hex_buf);
912 /* convert hex-string to binary buffer */
913 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
914 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
916 /* get register arch_type, and call set method */
917 arch_type = register_get_arch_type(reg_list[i]->arch_type);
918 if (arch_type == NULL)
920 ERROR("BUG: encountered unregistered arch type");
923 arch_type->set(reg_list[i], bin_buf);
925 /* advance packet pointer */
926 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
932 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
935 gdb_put_packet(connection, "OK", 2);
940 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
943 int reg_num = strtoul(packet + 1, NULL, 16);
948 #ifdef _DEBUG_GDB_IO_
952 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
954 return gdb_error(connection, retval);
957 if (reg_list_size <= reg_num)
959 ERROR("gdb requested a non-existing register");
963 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
965 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
967 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
975 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
980 int reg_num = strtoul(packet + 1, &separator, 16);
984 reg_arch_type_t *arch_type;
988 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
990 return gdb_error(connection, retval);
993 if (reg_list_size < reg_num)
995 ERROR("gdb requested a non-existing register");
996 return ERROR_SERVER_REMOTE_CLOSED;
999 if (*separator != '=')
1001 ERROR("GDB 'set register packet', but no '=' following the register number");
1002 return ERROR_SERVER_REMOTE_CLOSED;
1005 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1006 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1007 gdb_target_to_str(target, separator + 1, hex_buf);
1009 /* convert hex-string to binary buffer */
1010 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1011 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1013 /* get register arch_type, and call set method */
1014 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1015 if (arch_type == NULL)
1017 ERROR("BUG: encountered unregistered arch type");
1020 arch_type->set(reg_list[reg_num], bin_buf);
1022 gdb_put_packet(connection, "OK", 2);
1031 int gdb_error(connection_t *connection, int retval)
1035 case ERROR_TARGET_DATA_ABORT:
1036 gdb_send_error(connection, EIO);
1038 case ERROR_TARGET_TRANSLATION_FAULT:
1039 gdb_send_error(connection, EFAULT);
1041 case ERROR_TARGET_UNALIGNED_ACCESS:
1042 gdb_send_error(connection, EFAULT);
1044 case ERROR_TARGET_NOT_HALTED:
1045 gdb_send_error(connection, EFAULT);
1048 /* This could be that the target reset itself. */
1049 ERROR("unexpected error %i", retval);
1050 gdb_send_error(connection, EFAULT);
1057 /* We don't have to worry about the default 2 second timeout for GDB packets,
1058 * because GDB breaks up large memory reads into smaller reads.
1060 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1062 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1071 int retval = ERROR_OK;
1073 /* skip command character */
1076 addr = strtoul(packet, &separator, 16);
1078 if (*separator != ',')
1080 ERROR("incomplete read memory packet received, dropping connection");
1081 return ERROR_SERVER_REMOTE_CLOSED;
1084 len = strtoul(separator+1, NULL, 16);
1086 buffer = malloc(len);
1088 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1090 retval = target_read_buffer(target, addr, len, buffer);
1092 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1094 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1095 * At some point this might be fixed in GDB, in which case this code can be removed.
1097 * OpenOCD developers are acutely aware of this problem, but there is nothing
1098 * gained by involving the user in this problem that hopefully will get resolved
1101 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1103 * For now, the default is to fix up things to make current GDB versions work.
1104 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1106 memset(buffer, 0, len);
1110 if (retval == ERROR_OK)
1112 hex_buffer = malloc(len * 2 + 1);
1115 for (i = 0; i < len; i++)
1118 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1119 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1122 gdb_put_packet(connection, hex_buffer, len * 2);
1128 retval = gdb_error(connection, retval);
1136 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1147 /* skip command character */
1150 addr = strtoul(packet, &separator, 16);
1152 if (*separator != ',')
1154 ERROR("incomplete write memory packet received, dropping connection");
1155 return ERROR_SERVER_REMOTE_CLOSED;
1158 len = strtoul(separator+1, &separator, 16);
1160 if (*(separator++) != ':')
1162 ERROR("incomplete write memory packet received, dropping connection");
1163 return ERROR_SERVER_REMOTE_CLOSED;
1166 buffer = malloc(len);
1168 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1170 for (i=0; i<len; i++)
1173 sscanf(separator + 2*i, "%2x", &tmp);
1177 retval = target_write_buffer(target, addr, len, buffer);
1179 if (retval == ERROR_OK)
1181 gdb_put_packet(connection, "OK", 2);
1185 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1194 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1202 /* skip command character */
1205 addr = strtoul(packet, &separator, 16);
1207 if (*separator != ',')
1209 ERROR("incomplete write memory binary packet received, dropping connection");
1210 return ERROR_SERVER_REMOTE_CLOSED;
1213 len = strtoul(separator+1, &separator, 16);
1215 if (*(separator++) != ':')
1217 ERROR("incomplete write memory binary packet received, dropping connection");
1218 return ERROR_SERVER_REMOTE_CLOSED;
1224 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1226 retval = target_write_buffer(target, addr, len, (u8*)separator);
1229 if (retval == ERROR_OK)
1231 gdb_put_packet(connection, "OK", 2);
1235 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1242 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1249 if (packet_size > 1)
1251 packet[packet_size] = 0;
1252 address = strtoul(packet + 1, NULL, 16);
1259 if (packet[0] == 'c')
1262 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1264 else if (packet[0] == 's')
1267 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1271 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1274 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1275 enum watchpoint_rw wp_type;
1283 type = strtoul(packet + 1, &separator, 16);
1285 if (type == 0) /* memory breakpoint */
1286 bp_type = BKPT_SOFT;
1287 else if (type == 1) /* hardware breakpoint */
1288 bp_type = BKPT_HARD;
1289 else if (type == 2) /* write watchpoint */
1290 wp_type = WPT_WRITE;
1291 else if (type == 3) /* read watchpoint */
1293 else if (type == 4) /* access watchpoint */
1294 wp_type = WPT_ACCESS;
1296 if (*separator != ',')
1298 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1299 return ERROR_SERVER_REMOTE_CLOSED;
1302 address = strtoul(separator+1, &separator, 16);
1304 if (*separator != ',')
1306 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1307 return ERROR_SERVER_REMOTE_CLOSED;
1310 size = strtoul(separator+1, &separator, 16);
1316 if (packet[0] == 'Z')
1318 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1320 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1325 gdb_put_packet(connection, "OK", 2);
1330 breakpoint_remove(target, address);
1331 gdb_put_packet(connection, "OK", 2);
1338 if (packet[0] == 'Z')
1340 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1342 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1347 gdb_put_packet(connection, "OK", 2);
1352 watchpoint_remove(target, address);
1353 gdb_put_packet(connection, "OK", 2);
1364 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1365 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1367 if (*retval != ERROR_OK)
1375 if ((*xml == NULL) || (!first))
1377 /* start by 0 to exercise all the code paths.
1378 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1380 *size = *size * 2 + 2;
1382 *xml = realloc(*xml, *size);
1387 *retval = ERROR_SERVER_REMOTE_CLOSED;
1395 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1397 if ((ret > 0) && ((ret + 1) < *size - *pos))
1402 /* there was just enough or not enough space, allocate more. */
1407 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1411 /* Extract and NUL-terminate the annex. */
1413 while (*buf && *buf != ':')
1419 /* After the read marker and annex, qXfer looks like a
1420 * traditional 'm' packet. */
1422 *ofs = strtoul(buf, &separator, 16);
1424 if (*separator != ',')
1427 *len = strtoul(separator+1, NULL, 16);
1432 int gdb_calc_blocksize(flash_bank_t *bank)
1435 int block_size = 0xffffffff;
1437 /* loop through all sectors and return smallest sector size */
1439 for (i = 0; i < bank->num_sectors; i++)
1441 if (bank->sectors[i].size < block_size)
1442 block_size = bank->sectors[i].size;
1448 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1450 command_context_t *cmd_ctx = connection->cmd_ctx;
1452 if (strstr(packet, "qRcmd,"))
1454 if (packet_size > 6)
1458 cmd = malloc((packet_size - 6)/2 + 1);
1459 for (i=0; i < (packet_size - 6)/2; i++)
1462 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1465 cmd[(packet_size - 6)/2] = 0x0;
1467 /* We want to print all debug output to GDB connection */
1468 log_add_callback(gdb_log_callback, connection);
1469 target_call_timer_callbacks();
1470 command_run_line(cmd_ctx, cmd);
1471 target_call_timer_callbacks();
1472 log_remove_callback(gdb_log_callback, connection);
1475 gdb_put_packet(connection, "OK", 2);
1478 else if (strstr(packet, "qCRC:"))
1480 if (packet_size > 5)
1489 /* skip command character */
1492 addr = strtoul(packet, &separator, 16);
1494 if (*separator != ',')
1496 ERROR("incomplete read memory packet received, dropping connection");
1497 return ERROR_SERVER_REMOTE_CLOSED;
1500 len = strtoul(separator + 1, NULL, 16);
1502 retval = target_checksum_memory(target, addr, len, &checksum);
1504 if (retval == ERROR_OK)
1506 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1507 gdb_put_packet(connection, gdb_reply, 9);
1511 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1518 else if (strstr(packet, "qSupported"))
1520 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1521 * disable qXfer:features:read for the moment */
1522 int retval = ERROR_OK;
1523 char *buffer = NULL;
1527 xml_printf(&retval, &buffer, &pos, &size,
1528 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1529 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1531 if (retval != ERROR_OK)
1533 gdb_send_error(connection, 01);
1537 gdb_put_packet(connection, buffer, strlen(buffer));
1542 else if (strstr(packet, "qXfer:memory-map:read::"))
1544 /* We get away with only specifying flash here. Regions that are not
1545 * specified are treated as if we provided no memory map(if not we
1546 * could detect the holes and mark them as RAM).
1547 * Normally we only execute this code once, but no big deal if we
1548 * have to regenerate it a couple of times. */
1554 int retval = ERROR_OK;
1561 /* skip command character */
1564 offset = strtoul(packet, &separator, 16);
1565 length = strtoul(separator + 1, &separator, 16);
1567 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1570 for (i=0; i<flash_get_bank_count(); i++)
1572 p = get_flash_bank_by_num(i);
1576 /* if device has uneven sector sizes, eg. str7, lpc
1577 * we pass the smallest sector size to gdb memory map */
1578 blocksize = gdb_calc_blocksize(p);
1580 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1581 "<property name=\"blocksize\">0x%x</property>\n" \
1583 p->base, p->size, blocksize);
1586 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1588 if (retval != ERROR_OK)
1590 gdb_send_error(connection, retval);
1594 if (offset + length > pos)
1596 length = pos - offset;
1599 char *t = malloc(length + 1);
1601 memcpy(t + 1, xml + offset, length);
1602 gdb_put_packet(connection, t, length + 1);
1608 else if (strstr(packet, "qXfer:features:read:"))
1613 int retval = ERROR_OK;
1616 unsigned int length;
1619 /* skip command character */
1622 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1624 gdb_send_error(connection, 01);
1628 if (strcmp(annex, "target.xml") != 0)
1630 gdb_send_error(connection, 01);
1634 xml_printf(&retval, &xml, &pos, &size, \
1635 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1637 if (retval != ERROR_OK)
1639 gdb_send_error(connection, retval);
1643 gdb_put_packet(connection, xml, strlen(xml) + 1);
1649 gdb_put_packet(connection, "", 0);
1653 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1655 gdb_connection_t *gdb_connection = connection->priv;
1656 gdb_service_t *gdb_service = connection->service->priv;
1659 /* if flash programming disabled - send a empty reply */
1661 if (gdb_flash_program == 0)
1663 gdb_put_packet(connection, "", 0);
1667 if (strstr(packet, "vFlashErase:"))
1670 unsigned long length;
1672 char *parse = packet + 12;
1675 ERROR("incomplete vFlashErase packet received, dropping connection");
1676 return ERROR_SERVER_REMOTE_CLOSED;
1679 addr = strtoul(parse, &parse, 16);
1681 if (*(parse++) != ',' || *parse == '\0')
1683 ERROR("incomplete vFlashErase packet received, dropping connection");
1684 return ERROR_SERVER_REMOTE_CLOSED;
1687 length = strtoul(parse, &parse, 16);
1691 ERROR("incomplete vFlashErase packet received, dropping connection");
1692 return ERROR_SERVER_REMOTE_CLOSED;
1695 /* assume all sectors need erasing - stops any problems
1696 * when flash_write is called multiple times */
1699 /* perform any target specific operations before the erase */
1700 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1703 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1705 /* GDB doesn't evaluate the actual error number returned,
1706 * treat a failed erase as an I/O error
1708 gdb_send_error(connection, EIO);
1709 ERROR("flash_erase returned %i", result);
1712 gdb_put_packet(connection, "OK", 2);
1717 if (strstr(packet, "vFlashWrite:"))
1720 unsigned long length;
1721 char *parse = packet + 12;
1725 ERROR("incomplete vFlashErase packet received, dropping connection");
1726 return ERROR_SERVER_REMOTE_CLOSED;
1728 addr = strtoul(parse, &parse, 16);
1729 if (*(parse++) != ':')
1731 ERROR("incomplete vFlashErase packet received, dropping connection");
1732 return ERROR_SERVER_REMOTE_CLOSED;
1734 length = packet_size - (parse - packet);
1736 /* create a new image if there isn't already one */
1737 if (gdb_connection->vflash_image == NULL)
1739 gdb_connection->vflash_image = malloc(sizeof(image_t));
1740 image_open(gdb_connection->vflash_image, "", "build");
1743 /* create new section with content from packet buffer */
1744 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1746 gdb_put_packet(connection, "OK", 2);
1751 if (!strcmp(packet, "vFlashDone"))
1755 /* process the flashing buffer. No need to erase as GDB
1756 * always issues a vFlashErase first. */
1757 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK)
1759 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1760 gdb_put_packet(connection, "E.memtype", 9);
1762 gdb_send_error(connection, EIO);
1766 DEBUG("wrote %u bytes from vFlash image to flash", written);
1767 gdb_put_packet(connection, "OK", 2);
1770 image_close(gdb_connection->vflash_image);
1771 free(gdb_connection->vflash_image);
1772 gdb_connection->vflash_image = NULL;
1777 gdb_put_packet(connection, "", 0);
1781 int gdb_detach(connection_t *connection, target_t *target)
1783 switch( detach_mode )
1785 case GDB_DETACH_RESUME:
1786 target->type->resume(target, 1, 0, 1, 0);
1789 case GDB_DETACH_RESET:
1790 target_process_reset(connection->cmd_ctx);
1793 case GDB_DETACH_HALT:
1794 target->type->halt(target);
1797 case GDB_DETACH_NOTHING:
1801 gdb_put_packet(connection, "OK", 2);
1806 static void gdb_log_callback(void *priv, const char *file, int line,
1807 const char *function, const char *string)
1809 connection_t *connection = priv;
1810 gdb_connection_t *gdb_con = connection->priv;
1814 /* do not reply this using the O packet */
1818 gdb_output_con(connection, string);
1821 int gdb_input_inner(connection_t *connection)
1823 gdb_service_t *gdb_service = connection->service->priv;
1824 target_t *target = gdb_service->target;
1825 char packet[GDB_BUFFER_SIZE];
1828 gdb_connection_t *gdb_con = connection->priv;
1829 static int extended_protocol = 0;
1831 /* drain input buffer */
1834 packet_size = GDB_BUFFER_SIZE-1;
1835 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1840 /* terminate with zero */
1841 packet[packet_size] = 0;
1843 DEBUG("received packet: '%s'", packet);
1845 if (packet_size > 0)
1851 /* Hct... -- set thread
1852 * we don't have threads, send empty reply */
1853 gdb_put_packet(connection, NULL, 0);
1856 retval = gdb_query_packet(connection, target, packet, packet_size);
1859 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1862 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1865 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1868 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1871 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1874 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1878 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1881 gdb_last_signal_packet(connection, target, packet, packet_size);
1886 /* We're running/stepping, in which case we can
1887 * forward log output until the target is halted */
1888 gdb_connection_t *gdb_con = connection->priv;
1889 gdb_con->frontend_state = TARGET_RUNNING;
1890 log_add_callback(gdb_log_callback, connection);
1891 gdb_step_continue_packet(connection, target, packet, packet_size);
1895 retval = gdb_v_packet(connection, target, packet, packet_size);
1898 retval = gdb_detach(connection, target);
1899 extended_protocol = 0;
1902 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1906 if (extended_protocol != 0)
1908 gdb_put_packet(connection, "OK", 2);
1909 return ERROR_SERVER_REMOTE_CLOSED;
1911 /* handle extended remote protocol */
1912 extended_protocol = 1;
1913 gdb_put_packet(connection, "OK", 2);
1916 /* handle extended restart packet */
1917 target_process_reset(connection->cmd_ctx);
1920 /* ignore unkown packets */
1921 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1922 gdb_put_packet(connection, NULL, 0);
1926 /* if a packet handler returned an error, exit input loop */
1927 if (retval != ERROR_OK)
1931 if (gdb_con->ctrl_c)
1933 if (target->state == TARGET_RUNNING)
1935 target->type->halt(target);
1936 gdb_con->ctrl_c = 0;
1940 } while (gdb_con->buf_cnt > 0);
1945 int gdb_input(connection_t *connection)
1947 int retval = gdb_input_inner(connection);
1948 gdb_connection_t *gdb_con = connection->priv;
1949 if (retval == ERROR_SERVER_REMOTE_CLOSED)
1952 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
1953 if (gdb_con->closed)
1954 return ERROR_SERVER_REMOTE_CLOSED;
1956 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
1962 gdb_service_t *gdb_service;
1963 target_t *target = targets;
1968 WARNING("no gdb ports allocated as no target has been specified");
1974 WARNING("no gdb port specified, using default port 3333");
1980 char service_name[8];
1982 snprintf(service_name, 8, "gdb-%2.2i", i);
1984 gdb_service = malloc(sizeof(gdb_service_t));
1985 gdb_service->target = target;
1987 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1989 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1992 target = target->next;
1998 /* daemon configuration command gdb_port */
1999 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2004 /* only if the port wasn't overwritten by cmdline */
2006 gdb_port = strtoul(args[0], NULL, 0);
2011 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2015 if (strcmp(args[0], "resume") == 0)
2017 detach_mode = GDB_DETACH_RESUME;
2020 else if (strcmp(args[0], "reset") == 0)
2022 detach_mode = GDB_DETACH_RESET;
2025 else if (strcmp(args[0], "halt") == 0)
2027 detach_mode = GDB_DETACH_HALT;
2030 else if (strcmp(args[0], "nothing") == 0)
2032 detach_mode = GDB_DETACH_NOTHING;
2037 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2041 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2045 if (strcmp(args[0], "enable") == 0)
2047 gdb_use_memory_map = 1;
2050 else if (strcmp(args[0], "disable") == 0)
2052 gdb_use_memory_map = 0;
2057 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2061 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2065 if (strcmp(args[0], "enable") == 0)
2067 gdb_flash_program = 1;
2070 else if (strcmp(args[0], "disable") == 0)
2072 gdb_flash_program = 0;
2077 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2081 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2085 if (strcmp(args[0], "enable") == 0)
2087 gdb_report_data_abort = 1;
2090 else if (strcmp(args[0], "disable") == 0)
2092 gdb_report_data_abort = 0;
2097 WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2101 int gdb_register_commands(command_context_t *command_context)
2103 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2104 COMMAND_CONFIG, "");
2105 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2106 COMMAND_CONFIG, "");
2107 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2108 COMMAND_CONFIG, "");
2109 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2110 COMMAND_CONFIG, "");
2111 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2112 COMMAND_CONFIG, "");