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 "replacements.h"
32 #include "gdb_server.h"
36 #include "binarybuffer.h"
38 #include "breakpoints.h"
40 #include "target_request.h"
41 #include "configuration.h"
49 #define _DEBUG_GDB_IO_
52 static int gdb_breakpoint_override;
53 static enum breakpoint_type gdb_breakpoint_override_type;
55 extern int gdb_error(connection_t *connection, int retval);
56 static unsigned short gdb_port;
57 static const char *DIGITS = "0123456789abcdef";
59 static void gdb_log_callback(void *priv, const char *file, int line,
60 const char *function, const char *string);
70 /* target behaviour on gdb detach */
71 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
73 /* set if we are sending a memory map to gdb
74 * via qXfer:memory-map:read packet */
75 /* enabled by default*/
76 int gdb_use_memory_map = 1;
77 /* enabled by default*/
78 int gdb_flash_program = 1;
80 /* if set, data aborts cause an error to be reported in memory read packets
81 * see the code in gdb_read_memory_packet() for further explanations */
82 int gdb_report_data_abort = 0;
84 int gdb_last_signal(target_t *target)
86 switch (target->debug_reason)
88 case DBG_REASON_DBGRQ:
89 return 0x2; /* SIGINT */
90 case DBG_REASON_BREAKPOINT:
91 case DBG_REASON_WATCHPOINT:
92 case DBG_REASON_WPTANDBKPT:
93 return 0x05; /* SIGTRAP */
94 case DBG_REASON_SINGLESTEP:
95 return 0x05; /* SIGTRAP */
96 case DBG_REASON_NOTHALTED:
97 return 0x0; /* no signal... shouldn't happen */
99 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
104 int check_pending(connection_t *connection, int timeout_s, int *got_data)
106 /* a non-blocking socket will block if there is 0 bytes available on the socket,
107 * but return with as many bytes as are available immediately
111 gdb_connection_t *gdb_con = connection->priv;
117 if (gdb_con->buf_cnt>0)
124 FD_SET(connection->fd, &read_fds);
126 tv.tv_sec = timeout_s;
128 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
130 /* This can typically be because a "monitor" command took too long
131 * before printing any progress messages
135 return ERROR_GDB_TIMEOUT;
141 *got_data=FD_ISSET(connection->fd, &read_fds)!=0;
145 int gdb_get_char(connection_t *connection, int* next_char)
147 gdb_connection_t *gdb_con = connection->priv;
150 #ifdef _DEBUG_GDB_IO_
154 if (gdb_con->buf_cnt-- > 0)
156 *next_char = *(gdb_con->buf_p++);
157 if (gdb_con->buf_cnt > 0)
158 connection->input_pending = 1;
160 connection->input_pending = 0;
162 #ifdef _DEBUG_GDB_IO_
163 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
171 retval=check_pending(connection, 1, NULL);
172 if (retval!=ERROR_OK)
174 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
175 if (gdb_con->buf_cnt > 0)
179 if (gdb_con->buf_cnt == 0)
182 return ERROR_SERVER_REMOTE_CLOSED;
186 errno = WSAGetLastError();
193 case WSAECONNABORTED:
195 return ERROR_SERVER_REMOTE_CLOSED;
198 return ERROR_SERVER_REMOTE_CLOSED;
200 LOG_ERROR("read: %d", errno);
211 return ERROR_SERVER_REMOTE_CLOSED;
214 return ERROR_SERVER_REMOTE_CLOSED;
216 LOG_ERROR("read: %s", strerror(errno));
218 return ERROR_SERVER_REMOTE_CLOSED;
223 #ifdef _DEBUG_GDB_IO_
224 debug_buffer = malloc(gdb_con->buf_cnt + 1);
225 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
226 debug_buffer[gdb_con->buf_cnt] = 0;
227 LOG_DEBUG("received '%s'", debug_buffer);
231 gdb_con->buf_p = gdb_con->buffer;
233 *next_char = *(gdb_con->buf_p++);
234 if (gdb_con->buf_cnt > 0)
235 connection->input_pending = 1;
237 connection->input_pending = 0;
238 #ifdef _DEBUG_GDB_IO_
239 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
245 int gdb_putback_char(connection_t *connection, int last_char)
247 gdb_connection_t *gdb_con = connection->priv;
249 if (gdb_con->buf_p > gdb_con->buffer)
251 *(--gdb_con->buf_p) = last_char;
256 LOG_ERROR("BUG: couldn't put character back");
262 /* The only way we can detect that the socket is closed is the first time
263 * we write to it, we will fail. Subsequent write operations will
264 * succeed. Shudder! */
265 int gdb_write(connection_t *connection, void *data, int len)
267 gdb_connection_t *gdb_con = connection->priv;
269 return ERROR_SERVER_REMOTE_CLOSED;
271 if (write_socket(connection->fd, data, len) == len)
276 return ERROR_SERVER_REMOTE_CLOSED;
279 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
282 unsigned char my_checksum = 0;
283 #ifdef _DEBUG_GDB_IO_
288 gdb_connection_t *gdb_con = connection->priv;
290 for (i = 0; i < len; i++)
291 my_checksum += buffer[i];
293 #ifdef _DEBUG_GDB_IO_
295 * At this point we should have nothing in the input queue from GDB,
296 * however sometimes '-' is sent even though we've already received
297 * an ACK (+) for everything we've sent off.
302 if ((retval=check_pending(connection, 0, &gotdata))!=ERROR_OK)
306 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
308 LOG_WARNING("Discard unexpected char %c", reply);
314 #ifdef _DEBUG_GDB_IO_
315 debug_buffer = malloc(len + 1);
316 memcpy(debug_buffer, buffer, len);
317 debug_buffer[len] = 0;
318 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
322 char local_buffer[1024];
323 local_buffer[0] = '$';
324 if (len+4 <= sizeof(local_buffer))
326 /* performance gain on smaller packets by only a single call to gdb_write() */
327 memcpy(local_buffer+1, buffer, len++);
328 local_buffer[len++] = '#';
329 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
330 local_buffer[len++] = DIGITS[my_checksum & 0xf];
331 gdb_write(connection, local_buffer, len);
335 /* larger packets are transmitted directly from caller supplied buffer
336 by several calls to gdb_write() to avoid dynamic allocation */
337 local_buffer[1] = '#';
338 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
339 local_buffer[3] = DIGITS[my_checksum & 0xf];
340 gdb_write(connection, local_buffer, 1);
341 gdb_write(connection, buffer, len);
342 gdb_write(connection, local_buffer+1, 3);
345 if (gdb_con->noack_mode)
348 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
353 else if (reply == '-')
355 /* Stop sending output packets for now */
356 log_remove_callback(gdb_log_callback, connection);
357 LOG_WARNING("negative reply, retrying");
359 else if (reply == 0x3)
362 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
366 else if (reply == '-')
368 /* Stop sending output packets for now */
369 log_remove_callback(gdb_log_callback, connection);
370 LOG_WARNING("negative reply, retrying");
374 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
376 return ERROR_SERVER_REMOTE_CLOSED;
381 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
383 return ERROR_SERVER_REMOTE_CLOSED;
387 return ERROR_SERVER_REMOTE_CLOSED;
392 int gdb_put_packet(connection_t *connection, char *buffer, int len)
394 gdb_connection_t *gdb_con = connection->priv;
396 int retval = gdb_put_packet_inner(connection, buffer, len);
401 static __inline__ int fetch_packet(connection_t *connection, int *checksum_ok, int noack, int *len, char *buffer)
403 unsigned char my_checksum = 0;
408 gdb_connection_t *gdb_con = connection->priv;
414 /* The common case is that we have an entire packet with no escape chars.
415 * We need to leave at least 2 bytes in the buffer to have
416 * gdb_get_char() update various bits and bobs correctly.
418 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
420 /* The compiler will struggle a bit with constant propagation and
421 * aliasing, so we help it by showing that these values do not
422 * change inside the loop
425 char *buf = gdb_con->buf_p;
426 int run = gdb_con->buf_cnt - 2;
433 if (character == '#')
435 /* Danger! character can be '#' when esc is
436 * used so we need an explicit boolean for done here.
442 if (character == '}')
444 /* data transmitted in binary mode (X packet)
445 * uses 0x7d as escape character */
446 my_checksum += character & 0xff;
449 my_checksum += character & 0xff;
450 buffer[count++] = (character ^ 0x20) & 0xff;
454 my_checksum += character & 0xff;
455 buffer[count++] = character & 0xff;
459 gdb_con->buf_cnt -= i;
465 LOG_ERROR("packet buffer too small");
466 return ERROR_GDB_BUFFER_TOO_SMALL;
469 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
472 if (character == '#')
475 if (character == '}')
477 /* data transmitted in binary mode (X packet)
478 * uses 0x7d as escape character */
479 my_checksum += character & 0xff;
480 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
482 my_checksum += character & 0xff;
483 buffer[count++] = (character ^ 0x20) & 0xff;
487 my_checksum += character & 0xff;
488 buffer[count++] = character & 0xff;
494 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
496 checksum[0] = character;
497 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
499 checksum[1] = character;
504 *checksum_ok=(my_checksum == strtoul(checksum, NULL, 16));
510 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
514 gdb_connection_t *gdb_con = connection->priv;
520 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
523 #ifdef _DEBUG_GDB_IO_
524 LOG_DEBUG("character: '%c'", character);
532 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
533 * incase anyone tries to debug why they receive this warning every time */
534 LOG_WARNING("acknowledgment received, but no packet pending");
537 LOG_WARNING("negative acknowledgment, but no packet pending");
544 LOG_WARNING("ignoring character 0x%x", character);
547 } while (character != '$');
552 /* explicit code expansion here to get faster inlined code in -O3 by not
553 * calculating checksum
555 if (gdb_con->noack_mode)
557 if ((retval=fetch_packet(connection, &checksum_ok, 1, len, buffer))!=ERROR_OK)
561 if ((retval=fetch_packet(connection, &checksum_ok, 0, len, buffer))!=ERROR_OK)
565 if (gdb_con->noack_mode)
567 /* checksum is not checked in noack mode */
572 gdb_write(connection, "+", 1);
577 return ERROR_SERVER_REMOTE_CLOSED;
582 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
584 gdb_connection_t *gdb_con = connection->priv;
586 int retval = gdb_get_packet_inner(connection, buffer, len);
591 int gdb_output_con(connection_t *connection, const char* line)
596 bin_size = strlen(line);
598 hex_buffer = malloc(bin_size*2 + 2);
599 if (hex_buffer == NULL)
600 return ERROR_GDB_BUFFER_TOO_SMALL;
603 for (i=0; i<bin_size; i++)
604 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
605 hex_buffer[bin_size*2+1] = 0;
607 gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
613 int gdb_output(struct command_context_s *context, const char* line)
615 /* this will be dumped to the log and also sent as an O packet if possible */
616 LOG_USER_N("%s", line);
621 static void gdb_frontend_halted(struct target_s *target, connection_t *connection)
623 gdb_connection_t *gdb_connection = connection->priv;
625 /* In the GDB protocol when we are stepping or coninuing execution,
626 * we have a lingering reply. Upon receiving a halted event
627 * when we have that lingering packet, we reply to the original
628 * step or continue packet.
630 * Executing monitor commands can bring the target in and
631 * out of the running state so we'll see lots of TARGET_EVENT_XXX
632 * that are to be ignored.
634 if (gdb_connection->frontend_state == TARGET_RUNNING)
638 /* stop forwarding log packets! */
639 log_remove_callback(gdb_log_callback, connection);
641 if (gdb_connection->ctrl_c)
644 gdb_connection->ctrl_c = 0;
648 signal = gdb_last_signal(target);
651 snprintf(sig_reply, 4, "T%2.2x", signal);
652 gdb_put_packet(connection, sig_reply, 3);
653 gdb_connection->frontend_state = TARGET_HALTED;
657 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
659 connection_t *connection = priv;
661 target_handle_event( target, event );
664 case TARGET_EVENT_HALTED:
665 gdb_frontend_halted(target, connection);
667 case TARGET_EVENT_GDB_FLASH_ERASE_START:
668 target_handle_event( target, TARGET_EVENT_OLD_gdb_program_config );
669 jtag_execute_queue();
679 int gdb_new_connection(connection_t *connection)
681 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
682 gdb_service_t *gdb_service = connection->service->priv;
686 connection->priv = gdb_connection;
688 /* initialize gdb connection information */
689 gdb_connection->buf_p = gdb_connection->buffer;
690 gdb_connection->buf_cnt = 0;
691 gdb_connection->ctrl_c = 0;
692 gdb_connection->frontend_state = TARGET_HALTED;
693 gdb_connection->vflash_image = NULL;
694 gdb_connection->closed = 0;
695 gdb_connection->busy = 0;
696 gdb_connection->noack_mode = 0;
698 /* send ACK to GDB for debug request */
699 gdb_write(connection, "+", 1);
701 /* output goes through gdb connection */
702 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
704 /* we must remove all breakpoints registered to the target as a previous
705 * GDB session could leave dangling breakpoints if e.g. communication
708 breakpoint_clear_target(gdb_service->target);
709 watchpoint_clear_target(gdb_service->target);
711 /* register callback to be informed about target events */
712 target_register_event_callback(gdb_target_callback_event_handler, connection);
714 /* a gdb session just attached, try to put the target in halt mode.
718 * If the halt fails(e.g. target needs a reset, JTAG communication not
719 * working, etc.), then the GDB connect will succeed as
720 * the get_gdb_reg_list() will lie and return a register list with
723 * This allows GDB monitor commands to be run from a GDB init script to
724 * initialize the target
726 * Also, since the halt() is asynchronous target connect will be
727 * instantaneous and thus avoiding annoying timeout problems during
730 target_halt(gdb_service->target);
731 /* FIX!!!! could extended-remote work better here?
733 * wait a tiny bit for halted state or we just continue. The
734 * GDB register packet will then contain garbage
736 target_wait_state(gdb_service->target, TARGET_HALTED, 500);
738 /* remove the initial ACK from the incoming buffer */
739 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
742 /* FIX!!!??? would we actually ever receive a + here???
745 if (initial_ack != '+')
746 gdb_putback_char(connection, initial_ack);
747 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH );
751 int gdb_connection_closed(connection_t *connection)
753 gdb_service_t *gdb_service = connection->service->priv;
754 gdb_connection_t *gdb_connection = connection->priv;
756 /* see if an image built with vFlash commands is left */
757 if (gdb_connection->vflash_image)
759 image_close(gdb_connection->vflash_image);
760 free(gdb_connection->vflash_image);
761 gdb_connection->vflash_image = NULL;
764 /* if this connection registered a debug-message receiver delete it */
765 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
767 if (connection->priv)
769 free(connection->priv);
770 connection->priv = NULL;
774 LOG_ERROR("BUG: connection->priv == NULL");
777 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
778 log_remove_callback(gdb_log_callback, connection);
780 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH );
784 void gdb_send_error(connection_t *connection, u8 the_error)
787 snprintf(err, 4, "E%2.2X", the_error );
788 gdb_put_packet(connection, err, 3);
791 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
796 signal = gdb_last_signal(target);
798 snprintf(sig_reply, 4, "S%2.2x", signal);
799 gdb_put_packet(connection, sig_reply, 3);
804 /* Convert register to string of bits. NB! The # of bits in the
805 * register might be non-divisible by 8(a byte), in which
806 * case an entire byte is shown. */
807 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
814 buf_len = CEIL(reg->size, 8);
816 for (i = 0; i < buf_len; i++)
818 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
819 tstr[i*2+1] = DIGITS[buf[i]&0xf];
823 void gdb_target_to_str(target_t *target, char *tstr, char *str)
825 int str_len = strlen(tstr);
830 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
834 for (i = 0; i < str_len; i+=2)
836 str[str_len - i - 1] = tstr[i + 1];
837 str[str_len - i - 2] = tstr[i];
841 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
846 int reg_packet_size = 0;
851 #ifdef _DEBUG_GDB_IO_
855 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
857 return gdb_error(connection, retval);
860 for (i = 0; i < reg_list_size; i++)
862 reg_packet_size += reg_list[i]->size;
865 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
866 reg_packet_p = reg_packet;
868 for (i = 0; i < reg_list_size; i++)
870 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
871 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
874 #ifdef _DEBUG_GDB_IO_
877 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
878 LOG_DEBUG("reg_packet: %s", reg_packet_p);
883 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
891 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
899 #ifdef _DEBUG_GDB_IO_
903 /* skip command character */
909 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
910 return ERROR_SERVER_REMOTE_CLOSED;
913 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
915 return gdb_error(connection, retval);
919 for (i = 0; i < reg_list_size; i++)
923 reg_arch_type_t *arch_type;
925 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
926 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
927 gdb_target_to_str(target, packet_p, hex_buf);
929 /* convert hex-string to binary buffer */
930 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
931 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
933 /* get register arch_type, and call set method */
934 arch_type = register_get_arch_type(reg_list[i]->arch_type);
935 if (arch_type == NULL)
937 LOG_ERROR("BUG: encountered unregistered arch type");
940 arch_type->set(reg_list[i], bin_buf);
942 /* advance packet pointer */
943 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
949 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
952 gdb_put_packet(connection, "OK", 2);
957 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
960 int reg_num = strtoul(packet + 1, NULL, 16);
965 #ifdef _DEBUG_GDB_IO_
969 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
971 return gdb_error(connection, retval);
974 if (reg_list_size <= reg_num)
976 LOG_ERROR("gdb requested a non-existing register");
980 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
982 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
984 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
992 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
997 int reg_num = strtoul(packet + 1, &separator, 16);
1001 reg_arch_type_t *arch_type;
1005 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1007 return gdb_error(connection, retval);
1010 if (reg_list_size < reg_num)
1012 LOG_ERROR("gdb requested a non-existing register");
1013 return ERROR_SERVER_REMOTE_CLOSED;
1016 if (*separator != '=')
1018 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1019 return ERROR_SERVER_REMOTE_CLOSED;
1022 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1023 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1024 gdb_target_to_str(target, separator + 1, hex_buf);
1026 /* convert hex-string to binary buffer */
1027 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1028 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1030 /* get register arch_type, and call set method */
1031 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1032 if (arch_type == NULL)
1034 LOG_ERROR("BUG: encountered unregistered arch type");
1037 arch_type->set(reg_list[reg_num], bin_buf);
1039 gdb_put_packet(connection, "OK", 2);
1048 int gdb_error(connection_t *connection, int retval)
1052 case ERROR_TARGET_DATA_ABORT:
1053 gdb_send_error(connection, EIO);
1055 case ERROR_TARGET_TRANSLATION_FAULT:
1056 gdb_send_error(connection, EFAULT);
1058 case ERROR_TARGET_UNALIGNED_ACCESS:
1059 gdb_send_error(connection, EFAULT);
1061 case ERROR_TARGET_NOT_HALTED:
1062 gdb_send_error(connection, EFAULT);
1065 /* This could be that the target reset itself. */
1066 LOG_ERROR("unexpected error %i", retval);
1067 gdb_send_error(connection, EFAULT);
1074 /* We don't have to worry about the default 2 second timeout for GDB packets,
1075 * because GDB breaks up large memory reads into smaller reads.
1077 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1079 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1088 int retval = ERROR_OK;
1090 /* skip command character */
1093 addr = strtoul(packet, &separator, 16);
1095 if (*separator != ',')
1097 LOG_ERROR("incomplete read memory packet received, dropping connection");
1098 return ERROR_SERVER_REMOTE_CLOSED;
1101 len = strtoul(separator+1, NULL, 16);
1103 buffer = malloc(len);
1105 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1107 retval = target_read_buffer(target, addr, len, buffer);
1109 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1111 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1112 * At some point this might be fixed in GDB, in which case this code can be removed.
1114 * OpenOCD developers are acutely aware of this problem, but there is nothing
1115 * gained by involving the user in this problem that hopefully will get resolved
1118 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1120 * For now, the default is to fix up things to make current GDB versions work.
1121 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1123 memset(buffer, 0, len);
1127 if (retval == ERROR_OK)
1129 hex_buffer = malloc(len * 2 + 1);
1132 for (i = 0; i < len; i++)
1135 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1136 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1139 gdb_put_packet(connection, hex_buffer, len * 2);
1145 retval = gdb_error(connection, retval);
1153 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1164 /* skip command character */
1167 addr = strtoul(packet, &separator, 16);
1169 if (*separator != ',')
1171 LOG_ERROR("incomplete write memory packet received, dropping connection");
1172 return ERROR_SERVER_REMOTE_CLOSED;
1175 len = strtoul(separator+1, &separator, 16);
1177 if (*(separator++) != ':')
1179 LOG_ERROR("incomplete write memory packet received, dropping connection");
1180 return ERROR_SERVER_REMOTE_CLOSED;
1183 buffer = malloc(len);
1185 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1187 for (i=0; i<len; i++)
1190 sscanf(separator + 2*i, "%2x", &tmp);
1194 retval = target_write_buffer(target, addr, len, buffer);
1196 if (retval == ERROR_OK)
1198 gdb_put_packet(connection, "OK", 2);
1202 retval = gdb_error(connection, retval);
1210 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1218 /* skip command character */
1221 addr = strtoul(packet, &separator, 16);
1223 if (*separator != ',')
1225 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1226 return ERROR_SERVER_REMOTE_CLOSED;
1229 len = strtoul(separator+1, &separator, 16);
1231 if (*(separator++) != ':')
1233 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1234 return ERROR_SERVER_REMOTE_CLOSED;
1240 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1242 retval = target_write_buffer(target, addr, len, (u8*)separator);
1245 if (retval == ERROR_OK)
1247 gdb_put_packet(connection, "OK", 2);
1251 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1258 int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1262 int retval=ERROR_OK;
1266 if (packet_size > 1)
1268 packet[packet_size] = 0;
1269 address = strtoul(packet + 1, NULL, 16);
1276 if (packet[0] == 'c')
1278 LOG_DEBUG("continue");
1279 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1280 retval=target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1282 else if (packet[0] == 's')
1285 retval=target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1290 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1293 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1294 enum watchpoint_rw wp_type;
1302 type = strtoul(packet + 1, &separator, 16);
1304 if (type == 0) /* memory breakpoint */
1305 bp_type = BKPT_SOFT;
1306 else if (type == 1) /* hardware breakpoint */
1307 bp_type = BKPT_HARD;
1308 else if (type == 2) /* write watchpoint */
1309 wp_type = WPT_WRITE;
1310 else if (type == 3) /* read watchpoint */
1312 else if (type == 4) /* access watchpoint */
1313 wp_type = WPT_ACCESS;
1315 if (gdb_breakpoint_override&&((bp_type==BKPT_SOFT)||(bp_type==BKPT_HARD)))
1317 bp_type=gdb_breakpoint_override_type;
1320 if (*separator != ',')
1322 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1323 return ERROR_SERVER_REMOTE_CLOSED;
1326 address = strtoul(separator+1, &separator, 16);
1328 if (*separator != ',')
1330 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1331 return ERROR_SERVER_REMOTE_CLOSED;
1334 size = strtoul(separator+1, &separator, 16);
1340 if (packet[0] == 'Z')
1342 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1344 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1349 gdb_put_packet(connection, "OK", 2);
1354 breakpoint_remove(target, address);
1355 gdb_put_packet(connection, "OK", 2);
1362 if (packet[0] == 'Z')
1364 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1366 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1371 gdb_put_packet(connection, "OK", 2);
1376 watchpoint_remove(target, address);
1377 gdb_put_packet(connection, "OK", 2);
1388 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1389 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1391 if (*retval != ERROR_OK)
1399 if ((*xml == NULL) || (!first))
1401 /* start by 0 to exercise all the code paths.
1402 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1404 *size = *size * 2 + 2;
1406 *xml = realloc(*xml, *size);
1411 *retval = ERROR_SERVER_REMOTE_CLOSED;
1419 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1421 if ((ret > 0) && ((ret + 1) < *size - *pos))
1426 /* there was just enough or not enough space, allocate more. */
1431 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1435 /* Extract and NUL-terminate the annex. */
1437 while (*buf && *buf != ':')
1443 /* After the read marker and annex, qXfer looks like a
1444 * traditional 'm' packet. */
1446 *ofs = strtoul(buf, &separator, 16);
1448 if (*separator != ',')
1451 *len = strtoul(separator+1, NULL, 16);
1456 int gdb_calc_blocksize(flash_bank_t *bank)
1459 int block_size = 0xffffffff;
1461 /* loop through all sectors and return smallest sector size */
1463 for (i = 0; i < bank->num_sectors; i++)
1465 if (bank->sectors[i].size < block_size)
1466 block_size = bank->sectors[i].size;
1472 static int compare_bank (const void * a, const void * b)
1474 flash_bank_t *b1, *b2;
1475 b1=*((flash_bank_t **)a);
1476 b2=*((flash_bank_t **)b);
1478 if (b1->base==b2->base)
1481 } else if (b1->base>b2->base)
1490 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1492 command_context_t *cmd_ctx = connection->cmd_ctx;
1493 gdb_connection_t *gdb_connection = connection->priv;
1495 if (strstr(packet, "qRcmd,"))
1497 if (packet_size > 6)
1501 cmd = malloc((packet_size - 6)/2 + 1);
1502 for (i=0; i < (packet_size - 6)/2; i++)
1505 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1508 cmd[(packet_size - 6)/2] = 0x0;
1510 /* We want to print all debug output to GDB connection */
1511 log_add_callback(gdb_log_callback, connection);
1512 target_call_timer_callbacks_now();
1513 command_run_line(cmd_ctx, cmd);
1514 target_call_timer_callbacks_now();
1515 log_remove_callback(gdb_log_callback, connection);
1518 gdb_put_packet(connection, "OK", 2);
1521 else if (strstr(packet, "qCRC:"))
1523 if (packet_size > 5)
1532 /* skip command character */
1535 addr = strtoul(packet, &separator, 16);
1537 if (*separator != ',')
1539 LOG_ERROR("incomplete read memory packet received, dropping connection");
1540 return ERROR_SERVER_REMOTE_CLOSED;
1543 len = strtoul(separator + 1, NULL, 16);
1545 retval = target_checksum_memory(target, addr, len, &checksum);
1547 if (retval == ERROR_OK)
1549 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1550 gdb_put_packet(connection, gdb_reply, 9);
1554 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1561 else if (strstr(packet, "qSupported"))
1563 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1564 * disable qXfer:features:read for the moment */
1565 int retval = ERROR_OK;
1566 char *buffer = NULL;
1570 xml_printf(&retval, &buffer, &pos, &size,
1571 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1572 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1574 if (retval != ERROR_OK)
1576 gdb_send_error(connection, 01);
1580 gdb_put_packet(connection, buffer, strlen(buffer));
1585 else if (strstr(packet, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1587 /* We get away with only specifying flash here. Regions that are not
1588 * specified are treated as if we provided no memory map(if not we
1589 * could detect the holes and mark them as RAM).
1590 * Normally we only execute this code once, but no big deal if we
1591 * have to regenerate it a couple of times. */
1597 int retval = ERROR_OK;
1604 /* skip command character */
1607 offset = strtoul(packet, &separator, 16);
1608 length = strtoul(separator + 1, &separator, 16);
1610 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1613 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1614 read/write) by default for GDB.
1615 GDB does not have a concept of non-cacheable read/write memory.
1617 flash_bank_t **banks=malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
1620 for (i=0; i<flash_get_bank_count(); i++)
1622 p = get_flash_bank_by_num(i);
1626 retval = ERROR_FAIL;
1627 gdb_send_error(connection, retval);
1633 qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
1636 for (i=0; i<flash_get_bank_count(); i++)
1640 if (ram_start<p->base)
1642 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1643 ram_start, p->base-ram_start);
1646 /* if device has uneven sector sizes, eg. str7, lpc
1647 * we pass the smallest sector size to gdb memory map */
1648 blocksize = gdb_calc_blocksize(p);
1650 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1651 "<property name=\"blocksize\">0x%x</property>\n" \
1653 p->base, p->size, blocksize);
1654 ram_start=p->base+p->size;
1658 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1659 ram_start, 0-ram_start);
1662 /* a flash chip could be at the very end of the 32 bit address space, in which case
1663 ram_start will be precisely 0 */
1669 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1671 if (retval != ERROR_OK)
1673 gdb_send_error(connection, retval);
1677 if (offset + length > pos)
1679 length = pos - offset;
1682 char *t = malloc(length + 1);
1684 memcpy(t + 1, xml + offset, length);
1685 gdb_put_packet(connection, t, length + 1);
1691 else if (strstr(packet, "qXfer:features:read:"))
1696 int retval = ERROR_OK;
1699 unsigned int length;
1702 /* skip command character */
1705 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1707 gdb_send_error(connection, 01);
1711 if (strcmp(annex, "target.xml") != 0)
1713 gdb_send_error(connection, 01);
1717 xml_printf(&retval, &xml, &pos, &size, \
1718 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1720 if (retval != ERROR_OK)
1722 gdb_send_error(connection, retval);
1726 gdb_put_packet(connection, xml, strlen(xml));
1731 else if (strstr(packet, "QStartNoAckMode"))
1733 gdb_connection->noack_mode = 1;
1734 gdb_put_packet(connection, "OK", 2);
1738 gdb_put_packet(connection, "", 0);
1742 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1744 gdb_connection_t *gdb_connection = connection->priv;
1745 gdb_service_t *gdb_service = connection->service->priv;
1748 /* if flash programming disabled - send a empty reply */
1750 if (gdb_flash_program == 0)
1752 gdb_put_packet(connection, "", 0);
1756 if (strstr(packet, "vFlashErase:"))
1759 unsigned long length;
1761 char *parse = packet + 12;
1764 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1765 return ERROR_SERVER_REMOTE_CLOSED;
1768 addr = strtoul(parse, &parse, 16);
1770 if (*(parse++) != ',' || *parse == '\0')
1772 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1773 return ERROR_SERVER_REMOTE_CLOSED;
1776 length = strtoul(parse, &parse, 16);
1780 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1781 return ERROR_SERVER_REMOTE_CLOSED;
1784 /* assume all sectors need erasing - stops any problems
1785 * when flash_write is called multiple times */
1788 /* perform any target specific operations before the erase */
1789 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1790 result = flash_erase_address_range(gdb_service->target, addr, length );
1791 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
1794 if (result != ERROR_OK)
1796 /* GDB doesn't evaluate the actual error number returned,
1797 * treat a failed erase as an I/O error
1799 gdb_send_error(connection, EIO);
1800 LOG_ERROR("flash_erase returned %i", result);
1803 gdb_put_packet(connection, "OK", 2);
1808 if (strstr(packet, "vFlashWrite:"))
1811 unsigned long length;
1812 char *parse = packet + 12;
1816 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1817 return ERROR_SERVER_REMOTE_CLOSED;
1819 addr = strtoul(parse, &parse, 16);
1820 if (*(parse++) != ':')
1822 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1823 return ERROR_SERVER_REMOTE_CLOSED;
1825 length = packet_size - (parse - packet);
1827 /* create a new image if there isn't already one */
1828 if (gdb_connection->vflash_image == NULL)
1830 gdb_connection->vflash_image = malloc(sizeof(image_t));
1831 image_open(gdb_connection->vflash_image, "", "build");
1834 /* create new section with content from packet buffer */
1835 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1837 gdb_put_packet(connection, "OK", 2);
1842 if (!strcmp(packet, "vFlashDone"))
1846 /* process the flashing buffer. No need to erase as GDB
1847 * always issues a vFlashErase first. */
1848 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
1849 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1850 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1851 if ( result != ERROR_OK)
1853 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1854 gdb_put_packet(connection, "E.memtype", 9);
1856 gdb_send_error(connection, EIO);
1860 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written);
1861 gdb_put_packet(connection, "OK", 2);
1864 image_close(gdb_connection->vflash_image);
1865 free(gdb_connection->vflash_image);
1866 gdb_connection->vflash_image = NULL;
1871 gdb_put_packet(connection, "", 0);
1875 int gdb_detach(connection_t *connection, target_t *target)
1878 switch( detach_mode )
1880 case GDB_DETACH_RESUME:
1881 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1882 target_resume(target, 1, 0, 1, 0);
1885 case GDB_DETACH_RESET:
1886 /* FIX?? make this configurable?? */
1887 target_process_reset(connection->cmd_ctx, RESET_HALT);
1890 case GDB_DETACH_HALT:
1891 target_halt(target);
1894 case GDB_DETACH_NOTHING:
1898 gdb_put_packet(connection, "OK", 2);
1902 static void gdb_log_callback(void *priv, const char *file, int line,
1903 const char *function, const char *string)
1905 connection_t *connection = priv;
1906 gdb_connection_t *gdb_con = connection->priv;
1910 /* do not reply this using the O packet */
1914 gdb_output_con(connection, string);
1917 /* Do not allocate this on the stack */
1918 char gdb_packet_buffer[GDB_BUFFER_SIZE];
1920 static void gdb_sig_halted(connection_t *connection)
1923 snprintf(sig_reply, 4, "T%2.2x", 2);
1924 gdb_put_packet(connection, sig_reply, 3);
1928 int gdb_input_inner(connection_t *connection)
1930 gdb_service_t *gdb_service = connection->service->priv;
1931 target_t *target = gdb_service->target;
1932 char *packet=gdb_packet_buffer;
1935 gdb_connection_t *gdb_con = connection->priv;
1936 static int extended_protocol = 0;
1938 /* drain input buffer */
1941 packet_size = GDB_BUFFER_SIZE-1;
1942 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1947 /* terminate with zero */
1948 packet[packet_size] = 0;
1950 LOG_DEBUG("received packet: '%s'", packet);
1952 if (packet_size > 0)
1958 /* Hct... -- set thread
1959 * we don't have threads, send empty reply */
1960 gdb_put_packet(connection, NULL, 0);
1964 retval = gdb_query_packet(connection, target, packet, packet_size);
1967 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1970 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1973 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1976 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1979 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1982 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1986 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1989 gdb_last_signal_packet(connection, target, packet, packet_size);
1994 if (target->state != TARGET_HALTED)
1996 /* If the target isn't in the halted state, then we can't
1997 * step/continue. This might be early setup, etc.
1999 gdb_sig_halted(connection);
2002 /* We're running/stepping, in which case we can
2003 * forward log output until the target is halted
2005 gdb_connection_t *gdb_con = connection->priv;
2006 gdb_con->frontend_state = TARGET_RUNNING;
2007 log_add_callback(gdb_log_callback, connection);
2008 int retval=gdb_step_continue_packet(connection, target, packet, packet_size);
2009 if (retval!=ERROR_OK)
2011 /* we'll never receive a halted condition... issue a false one.. */
2012 gdb_frontend_halted(target, connection);
2018 retval = gdb_v_packet(connection, target, packet, packet_size);
2021 retval = gdb_detach(connection, target);
2022 extended_protocol = 0;
2025 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2029 if (extended_protocol != 0)
2031 gdb_put_packet(connection, "OK", 2);
2032 return ERROR_SERVER_REMOTE_CLOSED;
2034 /* handle extended remote protocol */
2035 extended_protocol = 1;
2036 gdb_put_packet(connection, "OK", 2);
2039 /* handle extended restart packet */
2040 breakpoint_clear_target(gdb_service->target);
2041 watchpoint_clear_target(gdb_service->target);
2042 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
2045 /* ignore unkown packets */
2046 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2047 gdb_put_packet(connection, NULL, 0);
2051 /* if a packet handler returned an error, exit input loop */
2052 if (retval != ERROR_OK)
2056 if (gdb_con->ctrl_c)
2058 if (target->state == TARGET_RUNNING)
2060 target_halt(target);
2061 gdb_con->ctrl_c = 0;
2065 } while (gdb_con->buf_cnt > 0);
2070 int gdb_input(connection_t *connection)
2072 int retval = gdb_input_inner(connection);
2073 gdb_connection_t *gdb_con = connection->priv;
2074 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2077 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2078 if (gdb_con->closed)
2079 return ERROR_SERVER_REMOTE_CLOSED;
2081 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2087 gdb_service_t *gdb_service;
2088 target_t *target = all_targets;
2092 LOG_WARNING("no gdb ports allocated as no target has been specified");
2098 LOG_WARNING("no gdb port specified, using default port 3333");
2104 char service_name[8];
2106 snprintf(service_name, 8, "gdb-%2.2i", target->target_number);
2108 gdb_service = malloc(sizeof(gdb_service_t));
2109 gdb_service->target = target;
2111 add_service("gdb", CONNECTION_GDB,
2112 gdb_port + target->target_number,
2113 1, gdb_new_connection, gdb_input,
2114 gdb_connection_closed,
2117 LOG_DEBUG("gdb service for target %s at port %i",
2119 gdb_port + target->target_number);
2121 target = target->next;
2127 /* daemon configuration command gdb_port */
2128 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2133 /* only if the port wasn't overwritten by cmdline */
2135 gdb_port = strtoul(args[0], NULL, 0);
2140 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2144 if (strcmp(args[0], "resume") == 0)
2146 detach_mode = GDB_DETACH_RESUME;
2149 else if (strcmp(args[0], "reset") == 0)
2151 detach_mode = GDB_DETACH_RESET;
2154 else if (strcmp(args[0], "halt") == 0)
2156 detach_mode = GDB_DETACH_HALT;
2159 else if (strcmp(args[0], "nothing") == 0)
2161 detach_mode = GDB_DETACH_NOTHING;
2166 LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2170 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2174 if (strcmp(args[0], "enable") == 0)
2176 gdb_use_memory_map = 1;
2179 else if (strcmp(args[0], "disable") == 0)
2181 gdb_use_memory_map = 0;
2186 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2190 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2194 if (strcmp(args[0], "enable") == 0)
2196 gdb_flash_program = 1;
2199 else if (strcmp(args[0], "disable") == 0)
2201 gdb_flash_program = 0;
2206 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2210 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2214 if (strcmp(args[0], "enable") == 0)
2216 gdb_report_data_abort = 1;
2219 else if (strcmp(args[0], "disable") == 0)
2221 gdb_report_data_abort = 0;
2226 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2230 /* daemon configuration command gdb_port */
2231 int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2238 gdb_breakpoint_override = 1;
2239 if (strcmp(args[0], "hard")==0)
2241 gdb_breakpoint_override_type=BKPT_HARD;
2242 } else if (strcmp(args[0], "soft")==0)
2244 gdb_breakpoint_override_type=BKPT_SOFT;
2245 } else if (strcmp(args[0], "disable") == 0)
2247 gdb_breakpoint_override = 0;
2251 return ERROR_COMMAND_SYNTAX_ERROR;
2253 if (gdb_breakpoint_override)
2255 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type==BKPT_HARD)?"hard":"soft");
2258 LOG_USER("breakpoint type is not overriden");
2265 int gdb_register_commands(command_context_t *command_context)
2267 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2268 COMMAND_CONFIG, "");
2269 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2270 COMMAND_CONFIG, "");
2271 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2272 COMMAND_CONFIG, "");
2273 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2274 COMMAND_CONFIG, "");
2275 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2276 COMMAND_CONFIG, "");
2277 register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
2278 COMMAND_EXEC, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2279 "The raison d'etre for this option is to support GDB GUI's without "
2280 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2281 "is not sufficient");