1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
30 #include "gdb_server.h"
31 #include "target_request.h"
40 #define _DEBUG_GDB_IO_
43 static int gdb_breakpoint_override;
44 static enum breakpoint_type gdb_breakpoint_override_type;
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 /* enabled by default*/
67 int gdb_use_memory_map = 1;
68 /* enabled by default*/
69 int gdb_flash_program = 1;
71 /* if set, data aborts cause an error to be reported in memory read packets
72 * see the code in gdb_read_memory_packet() for further explanations */
73 int gdb_report_data_abort = 0;
75 int gdb_last_signal(target_t *target)
77 switch (target->debug_reason)
79 case DBG_REASON_DBGRQ:
80 return 0x2; /* SIGINT */
81 case DBG_REASON_BREAKPOINT:
82 case DBG_REASON_WATCHPOINT:
83 case DBG_REASON_WPTANDBKPT:
84 return 0x05; /* SIGTRAP */
85 case DBG_REASON_SINGLESTEP:
86 return 0x05; /* SIGTRAP */
87 case DBG_REASON_NOTHALTED:
88 return 0x0; /* no signal... shouldn't happen */
90 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
95 int check_pending(connection_t *connection, int timeout_s, int *got_data)
97 /* a non-blocking socket will block if there is 0 bytes available on the socket,
98 * but return with as many bytes as are available immediately
102 gdb_connection_t *gdb_con = connection->priv;
108 if (gdb_con->buf_cnt>0)
115 FD_SET(connection->fd, &read_fds);
117 tv.tv_sec = timeout_s;
119 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
121 /* This can typically be because a "monitor" command took too long
122 * before printing any progress messages
126 return ERROR_GDB_TIMEOUT;
132 *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 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
162 if (connection->service->type == CONNECTION_PIPE)
164 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
168 retval = check_pending(connection, 1, NULL);
169 if (retval != ERROR_OK)
171 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
174 if (gdb_con->buf_cnt > 0)
178 if (gdb_con->buf_cnt == 0)
181 return ERROR_SERVER_REMOTE_CLOSED;
185 errno = WSAGetLastError();
192 case WSAECONNABORTED:
194 return ERROR_SERVER_REMOTE_CLOSED;
197 return ERROR_SERVER_REMOTE_CLOSED;
199 LOG_ERROR("read: %d", errno);
210 return ERROR_SERVER_REMOTE_CLOSED;
213 return ERROR_SERVER_REMOTE_CLOSED;
215 LOG_ERROR("read: %s", strerror(errno));
217 return ERROR_SERVER_REMOTE_CLOSED;
222 #ifdef _DEBUG_GDB_IO_
223 debug_buffer = malloc(gdb_con->buf_cnt + 1);
224 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
225 debug_buffer[gdb_con->buf_cnt] = 0;
226 LOG_DEBUG("received '%s'", debug_buffer);
230 gdb_con->buf_p = gdb_con->buffer;
232 *next_char = *(gdb_con->buf_p++);
233 if (gdb_con->buf_cnt > 0)
234 connection->input_pending = 1;
236 connection->input_pending = 0;
237 #ifdef _DEBUG_GDB_IO_
238 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
244 int gdb_putback_char(connection_t *connection, int last_char)
246 gdb_connection_t *gdb_con = connection->priv;
248 if (gdb_con->buf_p > gdb_con->buffer)
250 *(--gdb_con->buf_p) = last_char;
255 LOG_ERROR("BUG: couldn't put character back");
261 /* The only way we can detect that the socket is closed is the first time
262 * we write to it, we will fail. Subsequent write operations will
263 * succeed. Shudder! */
264 int gdb_write(connection_t *connection, void *data, int len)
266 gdb_connection_t *gdb_con = connection->priv;
268 return ERROR_SERVER_REMOTE_CLOSED;
270 if (connection->service->type == CONNECTION_PIPE)
272 /* write to stdout */
273 if (write(STDOUT_FILENO, data, len) == len)
280 if (write_socket(connection->fd, data, len) == len)
286 return ERROR_SERVER_REMOTE_CLOSED;
289 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
292 unsigned char my_checksum = 0;
293 #ifdef _DEBUG_GDB_IO_
298 gdb_connection_t *gdb_con = connection->priv;
300 for (i = 0; i < len; i++)
301 my_checksum += buffer[i];
303 #ifdef _DEBUG_GDB_IO_
305 * At this point we should have nothing in the input queue from GDB,
306 * however sometimes '-' is sent even though we've already received
307 * an ACK (+) for everything we've sent off.
312 if ((retval=check_pending(connection, 0, &gotdata))!=ERROR_OK)
316 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
319 /* fix a problem with some IAR tools */
320 gdb_putback_char( connection, reply );
321 LOG_DEBUG("Unexpected start of new packet");
325 LOG_WARNING("Discard unexpected char %c", reply);
331 #ifdef _DEBUG_GDB_IO_
332 debug_buffer = malloc(len + 1);
333 memcpy(debug_buffer, buffer, len);
334 debug_buffer[len] = 0;
335 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
339 char local_buffer[1024];
340 local_buffer[0] = '$';
341 if ((size_t)len + 4 <= sizeof(local_buffer))
343 /* performance gain on smaller packets by only a single call to gdb_write() */
344 memcpy(local_buffer+1, buffer, len++);
345 local_buffer[len++] = '#';
346 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
347 local_buffer[len++] = DIGITS[my_checksum & 0xf];
348 if((retval = gdb_write(connection, local_buffer, len)) != ERROR_OK)
355 /* larger packets are transmitted directly from caller supplied buffer
356 by several calls to gdb_write() to avoid dynamic allocation */
357 local_buffer[1] = '#';
358 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
359 local_buffer[3] = DIGITS[my_checksum & 0xf];
360 if((retval = gdb_write(connection, local_buffer, 1)) != ERROR_OK)
364 if((retval = gdb_write(connection, buffer, len)) != ERROR_OK)
368 if((retval = gdb_write(connection, local_buffer+1, 3)) != ERROR_OK)
374 if (gdb_con->noack_mode)
377 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
382 else if (reply == '-')
384 /* Stop sending output packets for now */
385 log_remove_callback(gdb_log_callback, connection);
386 LOG_WARNING("negative reply, retrying");
388 else if (reply == 0x3)
391 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
395 else if (reply == '-')
397 /* Stop sending output packets for now */
398 log_remove_callback(gdb_log_callback, connection);
399 LOG_WARNING("negative reply, retrying");
401 else if( reply == '$' ){
402 LOG_ERROR("GDB missing ack(1) - assumed good");
403 gdb_putback_char( connection, reply );
407 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
409 return ERROR_SERVER_REMOTE_CLOSED;
412 else if( reply == '$' ){
413 LOG_ERROR("GDB missing ack(2) - assumed good");
414 gdb_putback_char( connection, reply );
419 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply);
421 return ERROR_SERVER_REMOTE_CLOSED;
425 return ERROR_SERVER_REMOTE_CLOSED;
430 int gdb_put_packet(connection_t *connection, char *buffer, int len)
432 gdb_connection_t *gdb_con = connection->priv;
434 int retval = gdb_put_packet_inner(connection, buffer, len);
437 /* we sent some data, reset timer for keep alive messages */
443 static __inline__ int fetch_packet(connection_t *connection, int *checksum_ok, int noack, int *len, char *buffer)
445 unsigned char my_checksum = 0;
450 gdb_connection_t *gdb_con = connection->priv;
456 /* The common case is that we have an entire packet with no escape chars.
457 * We need to leave at least 2 bytes in the buffer to have
458 * gdb_get_char() update various bits and bobs correctly.
460 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
462 /* The compiler will struggle a bit with constant propagation and
463 * aliasing, so we help it by showing that these values do not
464 * change inside the loop
467 char *buf = gdb_con->buf_p;
468 int run = gdb_con->buf_cnt - 2;
475 if (character == '#')
477 /* Danger! character can be '#' when esc is
478 * used so we need an explicit boolean for done here.
484 if (character == '}')
486 /* data transmitted in binary mode (X packet)
487 * uses 0x7d as escape character */
488 my_checksum += character & 0xff;
491 my_checksum += character & 0xff;
492 buffer[count++] = (character ^ 0x20) & 0xff;
496 my_checksum += character & 0xff;
497 buffer[count++] = character & 0xff;
501 gdb_con->buf_cnt -= i;
507 LOG_ERROR("packet buffer too small");
508 return ERROR_GDB_BUFFER_TOO_SMALL;
511 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
514 if (character == '#')
517 if (character == '}')
519 /* data transmitted in binary mode (X packet)
520 * uses 0x7d as escape character */
521 my_checksum += character & 0xff;
522 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
524 my_checksum += character & 0xff;
525 buffer[count++] = (character ^ 0x20) & 0xff;
529 my_checksum += character & 0xff;
530 buffer[count++] = character & 0xff;
536 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
538 checksum[0] = character;
539 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
541 checksum[1] = character;
546 *checksum_ok=(my_checksum == strtoul(checksum, NULL, 16));
552 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
556 gdb_connection_t *gdb_con = connection->priv;
562 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
565 #ifdef _DEBUG_GDB_IO_
566 LOG_DEBUG("character: '%c'", character);
574 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
575 * incase anyone tries to debug why they receive this warning every time */
576 LOG_WARNING("acknowledgment received, but no packet pending");
579 LOG_WARNING("negative acknowledgment, but no packet pending");
586 LOG_WARNING("ignoring character 0x%x", character);
589 } while (character != '$');
594 /* explicit code expansion here to get faster inlined code in -O3 by not
595 * calculating checksum
597 if (gdb_con->noack_mode)
599 if ((retval=fetch_packet(connection, &checksum_ok, 1, len, buffer))!=ERROR_OK)
603 if ((retval=fetch_packet(connection, &checksum_ok, 0, len, buffer))!=ERROR_OK)
607 if (gdb_con->noack_mode)
609 /* checksum is not checked in noack mode */
614 if ((retval = gdb_write(connection, "+", 1)) != ERROR_OK)
622 return ERROR_SERVER_REMOTE_CLOSED;
627 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
629 gdb_connection_t *gdb_con = connection->priv;
631 int retval = gdb_get_packet_inner(connection, buffer, len);
636 int gdb_output_con(connection_t *connection, const char* line)
641 bin_size = strlen(line);
643 hex_buffer = malloc(bin_size*2 + 2);
644 if (hex_buffer == NULL)
645 return ERROR_GDB_BUFFER_TOO_SMALL;
648 for (i=0; i<bin_size; i++)
649 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
650 hex_buffer[bin_size*2+1] = 0;
652 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
658 int gdb_output(struct command_context_s *context, const char* line)
660 /* this will be dumped to the log and also sent as an O packet if possible */
661 LOG_USER_N("%s", line);
666 static void gdb_frontend_halted(struct target_s *target, connection_t *connection)
668 gdb_connection_t *gdb_connection = connection->priv;
670 /* In the GDB protocol when we are stepping or coninuing execution,
671 * we have a lingering reply. Upon receiving a halted event
672 * when we have that lingering packet, we reply to the original
673 * step or continue packet.
675 * Executing monitor commands can bring the target in and
676 * out of the running state so we'll see lots of TARGET_EVENT_XXX
677 * that are to be ignored.
679 if (gdb_connection->frontend_state == TARGET_RUNNING)
684 /* stop forwarding log packets! */
685 log_remove_callback(gdb_log_callback, connection);
687 if (gdb_connection->ctrl_c)
690 gdb_connection->ctrl_c = 0;
694 signal = gdb_last_signal(target);
697 snprintf(sig_reply, 4, "T%2.2x", signal);
698 gdb_put_packet(connection, sig_reply, 3);
699 gdb_connection->frontend_state = TARGET_HALTED;
703 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
706 connection_t *connection = priv;
708 target_handle_event( target, event );
711 case TARGET_EVENT_EARLY_HALTED:
712 gdb_frontend_halted(target, connection);
714 case TARGET_EVENT_HALTED:
715 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
717 case TARGET_EVENT_GDB_FLASH_ERASE_START:
718 target_handle_event( target, TARGET_EVENT_OLD_gdb_program_config );
719 if((retval = jtag_execute_queue()) != ERROR_OK)
731 int gdb_new_connection(connection_t *connection)
733 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
734 gdb_service_t *gdb_service = connection->service->priv;
738 connection->priv = gdb_connection;
740 /* initialize gdb connection information */
741 gdb_connection->buf_p = gdb_connection->buffer;
742 gdb_connection->buf_cnt = 0;
743 gdb_connection->ctrl_c = 0;
744 gdb_connection->frontend_state = TARGET_HALTED;
745 gdb_connection->vflash_image = NULL;
746 gdb_connection->closed = 0;
747 gdb_connection->busy = 0;
748 gdb_connection->noack_mode = 0;
750 /* send ACK to GDB for debug request */
751 gdb_write(connection, "+", 1);
753 /* output goes through gdb connection */
754 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
756 /* we must remove all breakpoints registered to the target as a previous
757 * GDB session could leave dangling breakpoints if e.g. communication
760 breakpoint_clear_target(gdb_service->target);
761 watchpoint_clear_target(gdb_service->target);
763 /* register callback to be informed about target events */
764 target_register_event_callback(gdb_target_callback_event_handler, connection);
766 /* a gdb session just attached, try to put the target in halt mode.
770 * If the halt fails(e.g. target needs a reset, JTAG communication not
771 * working, etc.), then the GDB connect will succeed as
772 * the get_gdb_reg_list() will lie and return a register list with
775 * This allows GDB monitor commands to be run from a GDB init script to
776 * initialize the target
778 * Also, since the halt() is asynchronous target connect will be
779 * instantaneous and thus avoiding annoying timeout problems during
782 target_halt(gdb_service->target);
783 /* FIX!!!! could extended-remote work better here?
785 * wait a tiny bit for halted state or we just continue. The
786 * GDB register packet will then contain garbage
788 target_wait_state(gdb_service->target, TARGET_HALTED, 500);
790 /* remove the initial ACK from the incoming buffer */
791 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
794 /* FIX!!!??? would we actually ever receive a + here???
797 if (initial_ack != '+')
798 gdb_putback_char(connection, initial_ack);
799 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH );
803 int gdb_connection_closed(connection_t *connection)
805 gdb_service_t *gdb_service = connection->service->priv;
806 gdb_connection_t *gdb_connection = connection->priv;
808 /* see if an image built with vFlash commands is left */
809 if (gdb_connection->vflash_image)
811 image_close(gdb_connection->vflash_image);
812 free(gdb_connection->vflash_image);
813 gdb_connection->vflash_image = NULL;
816 /* if this connection registered a debug-message receiver delete it */
817 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
819 if (connection->priv)
821 free(connection->priv);
822 connection->priv = NULL;
826 LOG_ERROR("BUG: connection->priv == NULL");
829 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
830 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
831 log_remove_callback(gdb_log_callback, connection);
833 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH );
838 void gdb_send_error(connection_t *connection, u8 the_error)
841 snprintf(err, 4, "E%2.2X", the_error );
842 gdb_put_packet(connection, err, 3);
845 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
850 signal = gdb_last_signal(target);
852 snprintf(sig_reply, 4, "S%2.2x", signal);
853 gdb_put_packet(connection, sig_reply, 3);
858 static int gdb_reg_pos(target_t *target, int pos, int len)
860 if (target->endianness == TARGET_LITTLE_ENDIAN)
863 return len - 1 - pos;
866 /* Convert register to string of bytes. NB! The # of bits in the
867 * register might be non-divisible by 8(a byte), in which
868 * case an entire byte is shown.
870 * NB! the format on the wire is the target endianess
872 * The format of reg->value is little endian
875 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
882 buf_len = CEIL(reg->size, 8);
884 for (i = 0; i < buf_len; i++)
886 int j = gdb_reg_pos(target, i, buf_len);
887 tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
888 tstr[i*2+1] = DIGITS[buf[j]&0xf];
892 static int hextoint(char c)
903 LOG_ERROR("BUG: invalid register value %08x", c);
907 /* copy over in register buffer */
908 void gdb_target_to_reg(target_t *target, char *tstr, int str_len, u8 *bin)
912 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
917 for (i = 0; i < str_len; i+=2)
919 u8 t = hextoint(tstr[i])<<4;
920 t |= hextoint(tstr[i+1]);
922 int j = gdb_reg_pos(target, i/2, str_len/2);
927 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
932 int reg_packet_size = 0;
937 #ifdef _DEBUG_GDB_IO_
941 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
943 return gdb_error(connection, retval);
946 for (i = 0; i < reg_list_size; i++)
948 reg_packet_size += reg_list[i]->size;
951 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
952 reg_packet_p = reg_packet;
954 for (i = 0; i < reg_list_size; i++)
956 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
957 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
960 #ifdef _DEBUG_GDB_IO_
963 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
964 LOG_DEBUG("reg_packet: %s", reg_packet_p);
969 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
977 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
985 #ifdef _DEBUG_GDB_IO_
989 /* skip command character */
995 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
996 return ERROR_SERVER_REMOTE_CLOSED;
999 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1001 return gdb_error(connection, retval);
1005 for (i = 0; i < reg_list_size; i++)
1008 int chars = (CEIL(reg_list[i]->size, 8) * 2);
1010 if (packet_p + chars > packet + packet_size)
1012 LOG_ERROR("BUG: register packet is too small for registers");
1015 reg_arch_type_t *arch_type;
1016 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
1017 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1019 /* get register arch_type, and call set method */
1020 arch_type = register_get_arch_type(reg_list[i]->arch_type);
1022 arch_type->set(reg_list[i], bin_buf);
1024 /* advance packet pointer */
1031 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
1034 gdb_put_packet(connection, "OK", 2);
1039 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1042 int reg_num = strtoul(packet + 1, NULL, 16);
1047 #ifdef _DEBUG_GDB_IO_
1051 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1053 return gdb_error(connection, retval);
1056 if (reg_list_size <= reg_num)
1058 LOG_ERROR("gdb requested a non-existing register");
1062 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1064 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1066 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
1074 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1078 int reg_num = strtoul(packet + 1, &separator, 16);
1082 reg_arch_type_t *arch_type;
1086 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1088 return gdb_error(connection, retval);
1091 if (reg_list_size < reg_num)
1093 LOG_ERROR("gdb requested a non-existing register");
1094 return ERROR_SERVER_REMOTE_CLOSED;
1097 if (*separator != '=')
1099 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1100 return ERROR_SERVER_REMOTE_CLOSED;
1103 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1104 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1105 int chars = (CEIL(reg_list[reg_num]->size, 8) * 2);
1107 /* fix!!! add some sanity checks on packet size here */
1109 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1111 /* get register arch_type, and call set method */
1112 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1113 arch_type->set(reg_list[reg_num], bin_buf);
1115 gdb_put_packet(connection, "OK", 2);
1123 int gdb_error(connection_t *connection, int retval)
1127 case ERROR_TARGET_DATA_ABORT:
1128 gdb_send_error(connection, EIO);
1130 case ERROR_TARGET_TRANSLATION_FAULT:
1131 gdb_send_error(connection, EFAULT);
1133 case ERROR_TARGET_UNALIGNED_ACCESS:
1134 gdb_send_error(connection, EFAULT);
1136 case ERROR_TARGET_NOT_HALTED:
1137 gdb_send_error(connection, EFAULT);
1140 /* This could be that the target reset itself. */
1141 LOG_ERROR("unexpected error %i", retval);
1142 gdb_send_error(connection, EFAULT);
1149 /* We don't have to worry about the default 2 second timeout for GDB packets,
1150 * because GDB breaks up large memory reads into smaller reads.
1152 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1154 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1163 int retval = ERROR_OK;
1165 /* skip command character */
1168 addr = strtoul(packet, &separator, 16);
1170 if (*separator != ',')
1172 LOG_ERROR("incomplete read memory packet received, dropping connection");
1173 return ERROR_SERVER_REMOTE_CLOSED;
1176 len = strtoul(separator+1, NULL, 16);
1178 buffer = malloc(len);
1180 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1182 retval = target_read_buffer(target, addr, len, buffer);
1184 if ((retval!=ERROR_OK)&&!gdb_report_data_abort)
1186 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1187 * At some point this might be fixed in GDB, in which case this code can be removed.
1189 * OpenOCD developers are acutely aware of this problem, but there is nothing
1190 * gained by involving the user in this problem that hopefully will get resolved
1193 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1195 * For now, the default is to fix up things to make current GDB versions work.
1196 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1198 memset(buffer, 0, len);
1202 if (retval == ERROR_OK)
1204 hex_buffer = malloc(len * 2 + 1);
1207 for (i = 0; i < len; i++)
1210 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1211 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1214 gdb_put_packet(connection, hex_buffer, len * 2);
1220 retval = gdb_error(connection, retval);
1228 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1239 /* skip command character */
1242 addr = strtoul(packet, &separator, 16);
1244 if (*separator != ',')
1246 LOG_ERROR("incomplete write memory packet received, dropping connection");
1247 return ERROR_SERVER_REMOTE_CLOSED;
1250 len = strtoul(separator+1, &separator, 16);
1252 if (*(separator++) != ':')
1254 LOG_ERROR("incomplete write memory packet received, dropping connection");
1255 return ERROR_SERVER_REMOTE_CLOSED;
1258 buffer = malloc(len);
1260 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1262 for (i=0; i<len; i++)
1265 sscanf(separator + 2*i, "%2x", &tmp);
1269 retval = target_write_buffer(target, addr, len, buffer);
1271 if (retval == ERROR_OK)
1273 gdb_put_packet(connection, "OK", 2);
1277 retval = gdb_error(connection, retval);
1285 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1293 /* skip command character */
1296 addr = strtoul(packet, &separator, 16);
1298 if (*separator != ',')
1300 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1301 return ERROR_SERVER_REMOTE_CLOSED;
1304 len = strtoul(separator+1, &separator, 16);
1306 if (*(separator++) != ':')
1308 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1309 return ERROR_SERVER_REMOTE_CLOSED;
1315 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1317 retval = target_write_buffer(target, addr, len, (u8*)separator);
1320 if (retval == ERROR_OK)
1322 gdb_put_packet(connection, "OK", 2);
1326 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1333 int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1337 int retval=ERROR_OK;
1341 if (packet_size > 1)
1343 packet[packet_size] = 0;
1344 address = strtoul(packet + 1, NULL, 16);
1351 if (packet[0] == 'c')
1353 LOG_DEBUG("continue");
1354 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1355 retval=target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1357 else if (packet[0] == 's')
1360 retval=target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1365 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1368 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1369 enum watchpoint_rw wp_type;
1377 type = strtoul(packet + 1, &separator, 16);
1379 if (type == 0) /* memory breakpoint */
1380 bp_type = BKPT_SOFT;
1381 else if (type == 1) /* hardware breakpoint */
1382 bp_type = BKPT_HARD;
1383 else if (type == 2) /* write watchpoint */
1384 wp_type = WPT_WRITE;
1385 else if (type == 3) /* read watchpoint */
1387 else if (type == 4) /* access watchpoint */
1388 wp_type = WPT_ACCESS;
1390 if (gdb_breakpoint_override&&((bp_type==BKPT_SOFT)||(bp_type==BKPT_HARD)))
1392 bp_type=gdb_breakpoint_override_type;
1395 if (*separator != ',')
1397 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1398 return ERROR_SERVER_REMOTE_CLOSED;
1401 address = strtoul(separator+1, &separator, 16);
1403 if (*separator != ',')
1405 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1406 return ERROR_SERVER_REMOTE_CLOSED;
1409 size = strtoul(separator+1, &separator, 16);
1415 if (packet[0] == 'Z')
1417 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1419 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1424 gdb_put_packet(connection, "OK", 2);
1429 breakpoint_remove(target, address);
1430 gdb_put_packet(connection, "OK", 2);
1437 if (packet[0] == 'Z')
1439 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1441 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1446 gdb_put_packet(connection, "OK", 2);
1451 watchpoint_remove(target, address);
1452 gdb_put_packet(connection, "OK", 2);
1463 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1464 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1466 if (*retval != ERROR_OK)
1474 if ((*xml == NULL) || (!first))
1476 /* start by 0 to exercise all the code paths.
1477 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1479 *size = *size * 2 + 2;
1481 *xml = realloc(*xml, *size);
1486 *retval = ERROR_SERVER_REMOTE_CLOSED;
1494 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1496 if ((ret > 0) && ((ret + 1) < *size - *pos))
1501 /* there was just enough or not enough space, allocate more. */
1506 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1510 /* Extract and NUL-terminate the annex. */
1512 while (*buf && *buf != ':')
1518 /* After the read marker and annex, qXfer looks like a
1519 * traditional 'm' packet. */
1521 *ofs = strtoul(buf, &separator, 16);
1523 if (*separator != ',')
1526 *len = strtoul(separator+1, NULL, 16);
1531 int gdb_calc_blocksize(flash_bank_t *bank)
1534 u32 block_size = 0xffffffff;
1536 /* loop through all sectors and return smallest sector size */
1538 for (i = 0; i < (u32)bank->num_sectors; i++)
1540 if (bank->sectors[i].size < block_size)
1541 block_size = bank->sectors[i].size;
1547 static int compare_bank (const void * a, const void * b)
1549 flash_bank_t *b1, *b2;
1550 b1=*((flash_bank_t **)a);
1551 b2=*((flash_bank_t **)b);
1553 if (b1->base==b2->base)
1556 } else if (b1->base>b2->base)
1565 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1567 command_context_t *cmd_ctx = connection->cmd_ctx;
1568 gdb_connection_t *gdb_connection = connection->priv;
1570 if (strstr(packet, "qRcmd,"))
1572 if (packet_size > 6)
1576 cmd = malloc((packet_size - 6)/2 + 1);
1577 for (i=0; i < (packet_size - 6)/2; i++)
1580 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1583 cmd[(packet_size - 6)/2] = 0x0;
1585 /* We want to print all debug output to GDB connection */
1586 log_add_callback(gdb_log_callback, connection);
1587 target_call_timer_callbacks_now();
1588 command_run_line(cmd_ctx, cmd);
1589 target_call_timer_callbacks_now();
1590 log_remove_callback(gdb_log_callback, connection);
1593 gdb_put_packet(connection, "OK", 2);
1596 else if (strstr(packet, "qCRC:"))
1598 if (packet_size > 5)
1607 /* skip command character */
1610 addr = strtoul(packet, &separator, 16);
1612 if (*separator != ',')
1614 LOG_ERROR("incomplete read memory packet received, dropping connection");
1615 return ERROR_SERVER_REMOTE_CLOSED;
1618 len = strtoul(separator + 1, NULL, 16);
1620 retval = target_checksum_memory(target, addr, len, &checksum);
1622 if (retval == ERROR_OK)
1624 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1625 gdb_put_packet(connection, gdb_reply, 9);
1629 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1636 else if (strstr(packet, "qSupported"))
1638 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1639 * disable qXfer:features:read for the moment */
1640 int retval = ERROR_OK;
1641 char *buffer = NULL;
1645 xml_printf(&retval, &buffer, &pos, &size,
1646 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1647 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1649 if (retval != ERROR_OK)
1651 gdb_send_error(connection, 01);
1655 gdb_put_packet(connection, buffer, strlen(buffer));
1660 else if (strstr(packet, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1662 /* We get away with only specifying flash here. Regions that are not
1663 * specified are treated as if we provided no memory map(if not we
1664 * could detect the holes and mark them as RAM).
1665 * Normally we only execute this code once, but no big deal if we
1666 * have to regenerate it a couple of times. */
1672 int retval = ERROR_OK;
1679 /* skip command character */
1682 offset = strtoul(packet, &separator, 16);
1683 length = strtoul(separator + 1, &separator, 16);
1685 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1688 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1689 read/write) by default for GDB.
1690 GDB does not have a concept of non-cacheable read/write memory.
1692 flash_bank_t **banks=malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
1695 for (i=0; i<flash_get_bank_count(); i++)
1697 p = get_flash_bank_by_num(i);
1701 retval = ERROR_FAIL;
1702 gdb_send_error(connection, retval);
1708 qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
1711 for (i=0; i<flash_get_bank_count(); i++)
1715 if (ram_start<p->base)
1717 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1718 ram_start, p->base-ram_start);
1721 /* if device has uneven sector sizes, eg. str7, lpc
1722 * we pass the smallest sector size to gdb memory map */
1723 blocksize = gdb_calc_blocksize(p);
1725 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1726 "<property name=\"blocksize\">0x%x</property>\n" \
1728 p->base, p->size, blocksize);
1729 ram_start=p->base+p->size;
1733 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1734 ram_start, 0-ram_start);
1737 /* a flash chip could be at the very end of the 32 bit address space, in which case
1738 ram_start will be precisely 0 */
1744 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1746 if (retval != ERROR_OK)
1748 gdb_send_error(connection, retval);
1752 if (offset + length > pos)
1754 length = pos - offset;
1757 char *t = malloc(length + 1);
1759 memcpy(t + 1, xml + offset, length);
1760 gdb_put_packet(connection, t, length + 1);
1766 else if (strstr(packet, "qXfer:features:read:"))
1771 int retval = ERROR_OK;
1774 unsigned int length;
1777 /* skip command character */
1780 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1782 gdb_send_error(connection, 01);
1786 if (strcmp(annex, "target.xml") != 0)
1788 gdb_send_error(connection, 01);
1792 xml_printf(&retval, &xml, &pos, &size, \
1793 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1795 if (retval != ERROR_OK)
1797 gdb_send_error(connection, retval);
1801 gdb_put_packet(connection, xml, strlen(xml));
1806 else if (strstr(packet, "QStartNoAckMode"))
1808 gdb_connection->noack_mode = 1;
1809 gdb_put_packet(connection, "OK", 2);
1813 gdb_put_packet(connection, "", 0);
1817 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1819 gdb_connection_t *gdb_connection = connection->priv;
1820 gdb_service_t *gdb_service = connection->service->priv;
1823 /* if flash programming disabled - send a empty reply */
1825 if (gdb_flash_program == 0)
1827 gdb_put_packet(connection, "", 0);
1831 if (strstr(packet, "vFlashErase:"))
1834 unsigned long length;
1836 char *parse = packet + 12;
1839 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1840 return ERROR_SERVER_REMOTE_CLOSED;
1843 addr = strtoul(parse, &parse, 16);
1845 if (*(parse++) != ',' || *parse == '\0')
1847 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1848 return ERROR_SERVER_REMOTE_CLOSED;
1851 length = strtoul(parse, &parse, 16);
1855 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1856 return ERROR_SERVER_REMOTE_CLOSED;
1859 /* assume all sectors need erasing - stops any problems
1860 * when flash_write is called multiple times */
1863 /* perform any target specific operations before the erase */
1864 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1865 result = flash_erase_address_range(gdb_service->target, addr, length );
1866 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
1869 if (result != ERROR_OK)
1871 /* GDB doesn't evaluate the actual error number returned,
1872 * treat a failed erase as an I/O error
1874 gdb_send_error(connection, EIO);
1875 LOG_ERROR("flash_erase returned %i", result);
1878 gdb_put_packet(connection, "OK", 2);
1883 if (strstr(packet, "vFlashWrite:"))
1887 unsigned long length;
1888 char *parse = packet + 12;
1892 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1893 return ERROR_SERVER_REMOTE_CLOSED;
1895 addr = strtoul(parse, &parse, 16);
1896 if (*(parse++) != ':')
1898 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1899 return ERROR_SERVER_REMOTE_CLOSED;
1901 length = packet_size - (parse - packet);
1903 /* create a new image if there isn't already one */
1904 if (gdb_connection->vflash_image == NULL)
1906 gdb_connection->vflash_image = malloc(sizeof(image_t));
1907 image_open(gdb_connection->vflash_image, "", "build");
1910 /* create new section with content from packet buffer */
1911 if((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse)) != ERROR_OK)
1916 gdb_put_packet(connection, "OK", 2);
1921 if (!strcmp(packet, "vFlashDone"))
1925 /* process the flashing buffer. No need to erase as GDB
1926 * always issues a vFlashErase first. */
1927 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
1928 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1929 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1930 if ( result != ERROR_OK)
1932 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1933 gdb_put_packet(connection, "E.memtype", 9);
1935 gdb_send_error(connection, EIO);
1939 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written);
1940 gdb_put_packet(connection, "OK", 2);
1943 image_close(gdb_connection->vflash_image);
1944 free(gdb_connection->vflash_image);
1945 gdb_connection->vflash_image = NULL;
1950 gdb_put_packet(connection, "", 0);
1954 int gdb_detach(connection_t *connection, target_t *target)
1957 switch( detach_mode )
1959 case GDB_DETACH_RESUME:
1960 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1961 target_resume(target, 1, 0, 1, 0);
1964 case GDB_DETACH_RESET:
1965 /* FIX?? make this configurable?? */
1966 target_process_reset(connection->cmd_ctx, RESET_HALT);
1969 case GDB_DETACH_HALT:
1970 target_halt(target);
1973 case GDB_DETACH_NOTHING:
1977 gdb_put_packet(connection, "OK", 2);
1981 static void gdb_log_callback(void *priv, const char *file, int line,
1982 const char *function, const char *string)
1984 connection_t *connection = priv;
1985 gdb_connection_t *gdb_con = connection->priv;
1989 /* do not reply this using the O packet */
1993 gdb_output_con(connection, string);
1996 /* Do not allocate this on the stack */
1997 char gdb_packet_buffer[GDB_BUFFER_SIZE];
1999 static void gdb_sig_halted(connection_t *connection)
2002 snprintf(sig_reply, 4, "T%2.2x", 2);
2003 gdb_put_packet(connection, sig_reply, 3);
2007 int gdb_input_inner(connection_t *connection)
2009 gdb_service_t *gdb_service = connection->service->priv;
2010 target_t *target = gdb_service->target;
2011 char *packet=gdb_packet_buffer;
2014 gdb_connection_t *gdb_con = connection->priv;
2015 static int extended_protocol = 0;
2017 /* drain input buffer */
2020 packet_size = GDB_BUFFER_SIZE-1;
2021 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
2026 /* terminate with zero */
2027 packet[packet_size] = 0;
2029 if( LOG_LEVEL_IS( LOG_LVL_DEBUG ) ){
2030 if( packet[0] == 'X' ){
2031 // binary packets spew junk into the debug log stream
2034 for( x = 0 ; (x < 49) && (packet[x] != ':') ; x++ ){
2038 LOG_DEBUG("received packet: '%s:<binary-data>'", buf );
2040 LOG_DEBUG("received packet: '%s'", packet );
2044 if (packet_size > 0)
2050 /* Hct... -- set thread
2051 * we don't have threads, send empty reply */
2052 gdb_put_packet(connection, NULL, 0);
2056 retval = gdb_query_packet(connection, target, packet, packet_size);
2059 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
2062 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
2065 retval = gdb_get_register_packet(connection, target, packet, packet_size);
2068 retval = gdb_set_register_packet(connection, target, packet, packet_size);
2071 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
2074 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
2078 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
2081 gdb_last_signal_packet(connection, target, packet, packet_size);
2086 if (target->state != TARGET_HALTED)
2088 /* If the target isn't in the halted state, then we can't
2089 * step/continue. This might be early setup, etc.
2091 gdb_sig_halted(connection);
2094 /* We're running/stepping, in which case we can
2095 * forward log output until the target is halted
2097 gdb_connection_t *gdb_con = connection->priv;
2098 gdb_con->frontend_state = TARGET_RUNNING;
2099 log_add_callback(gdb_log_callback, connection);
2100 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2101 int retval=gdb_step_continue_packet(connection, target, packet, packet_size);
2102 if (retval!=ERROR_OK)
2104 /* we'll never receive a halted condition... issue a false one.. */
2105 gdb_frontend_halted(target, connection);
2111 retval = gdb_v_packet(connection, target, packet, packet_size);
2114 retval = gdb_detach(connection, target);
2115 extended_protocol = 0;
2118 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2122 if (extended_protocol != 0)
2124 gdb_put_packet(connection, "OK", 2);
2125 return ERROR_SERVER_REMOTE_CLOSED;
2127 /* handle extended remote protocol */
2128 extended_protocol = 1;
2129 gdb_put_packet(connection, "OK", 2);
2132 /* handle extended restart packet */
2133 breakpoint_clear_target(gdb_service->target);
2134 watchpoint_clear_target(gdb_service->target);
2135 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
2138 /* ignore unkown packets */
2139 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2140 gdb_put_packet(connection, NULL, 0);
2144 /* if a packet handler returned an error, exit input loop */
2145 if (retval != ERROR_OK)
2149 if (gdb_con->ctrl_c)
2151 if (target->state == TARGET_RUNNING)
2153 target_halt(target);
2154 gdb_con->ctrl_c = 0;
2158 } while (gdb_con->buf_cnt > 0);
2163 int gdb_input(connection_t *connection)
2165 int retval = gdb_input_inner(connection);
2166 gdb_connection_t *gdb_con = connection->priv;
2167 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2170 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2171 if (gdb_con->closed)
2172 return ERROR_SERVER_REMOTE_CLOSED;
2174 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2180 gdb_service_t *gdb_service;
2181 target_t *target = all_targets;
2185 LOG_WARNING("no gdb ports allocated as no target has been specified");
2189 if (gdb_port == 0 && server_use_pipes == 0)
2191 LOG_DEBUG("no gdb port specified, using default port 3333");
2195 if (server_use_pipes)
2197 /* only a single gdb connection when using a pipe */
2199 gdb_service = malloc(sizeof(gdb_service_t));
2200 gdb_service->target = target;
2202 add_service("gdb", CONNECTION_PIPE, 0, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
2204 LOG_DEBUG("gdb service for target %s using pipes", target->type->name);
2210 gdb_service = malloc(sizeof(gdb_service_t));
2211 gdb_service->target = target;
2213 add_service("gdb", CONNECTION_TCP, gdb_port + target->target_number, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
2215 LOG_DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + target->target_number);
2216 target = target->next;
2223 /* daemon configuration command gdb_port */
2224 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2228 command_print(cmd_ctx, "%d", gdb_port);
2232 /* only if the port wasn't overwritten by cmdline */
2234 gdb_port = strtoul(args[0], NULL, 0);
2239 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2243 if (strcmp(args[0], "resume") == 0)
2245 detach_mode = GDB_DETACH_RESUME;
2248 else if (strcmp(args[0], "reset") == 0)
2250 detach_mode = GDB_DETACH_RESET;
2253 else if (strcmp(args[0], "halt") == 0)
2255 detach_mode = GDB_DETACH_HALT;
2258 else if (strcmp(args[0], "nothing") == 0)
2260 detach_mode = GDB_DETACH_NOTHING;
2264 LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2267 return ERROR_COMMAND_SYNTAX_ERROR;
2270 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2274 if (strcmp(args[0], "enable") == 0)
2276 gdb_use_memory_map = 1;
2279 else if (strcmp(args[0], "disable") == 0)
2281 gdb_use_memory_map = 0;
2285 LOG_WARNING("invalid gdb_memory_map configuration directive %s", args[0]);
2288 return ERROR_COMMAND_SYNTAX_ERROR;
2291 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2295 if (strcmp(args[0], "enable") == 0)
2297 gdb_flash_program = 1;
2300 else if (strcmp(args[0], "disable") == 0)
2302 gdb_flash_program = 0;
2306 LOG_WARNING("invalid gdb_flash_program configuration directive: %s", args[0]);
2309 return ERROR_COMMAND_SYNTAX_ERROR;
2312 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2316 if (strcmp(args[0], "enable") == 0)
2318 gdb_report_data_abort = 1;
2321 else if (strcmp(args[0], "disable") == 0)
2323 gdb_report_data_abort = 0;
2327 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2330 return ERROR_COMMAND_SYNTAX_ERROR;
2333 /* gdb_breakpoint_override */
2334 int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2341 gdb_breakpoint_override = 1;
2342 if (strcmp(args[0], "hard")==0)
2344 gdb_breakpoint_override_type=BKPT_HARD;
2345 } else if (strcmp(args[0], "soft")==0)
2347 gdb_breakpoint_override_type=BKPT_SOFT;
2348 } else if (strcmp(args[0], "disable") == 0)
2350 gdb_breakpoint_override = 0;
2354 return ERROR_COMMAND_SYNTAX_ERROR;
2356 if (gdb_breakpoint_override)
2358 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type==BKPT_HARD)?"hard":"soft");
2361 LOG_USER("breakpoint type is not overriden");
2367 int gdb_register_commands(command_context_t *command_context)
2369 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2370 COMMAND_ANY, "daemon configuration command gdb_port");
2371 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2372 COMMAND_CONFIG, "resume/reset/halt/nothing - "
2373 "specify behavior when GDB detaches from the target");
2374 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2375 COMMAND_CONFIG, "enable or disable memory map");
2376 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2377 COMMAND_CONFIG, "enable or disable flash program");
2378 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2379 COMMAND_CONFIG, "enable or disable reporting data aborts");
2380 register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
2381 COMMAND_EXEC, "hard/soft/disable - force breakpoint type for gdb 'break' commands.");