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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
29 #include "gdb_server.h"
33 #include "binarybuffer.h"
35 #include "breakpoints.h"
37 #include "target_request.h"
38 #include "configuration.h"
46 #define _DEBUG_GDB_IO_
49 static int gdb_breakpoint_override;
50 static enum breakpoint_type gdb_breakpoint_override_type;
52 extern int gdb_error(connection_t *connection, int retval);
53 static unsigned short gdb_port;
54 static const char *DIGITS = "0123456789abcdef";
56 static void gdb_log_callback(void *priv, const char *file, int line,
57 const char *function, const char *string);
67 /* target behaviour on gdb detach */
68 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
70 /* set if we are sending a memory map to gdb
71 * via qXfer:memory-map:read packet */
72 /* enabled by default*/
73 int gdb_use_memory_map = 1;
74 /* enabled by default*/
75 int gdb_flash_program = 1;
77 /* if set, data aborts cause an error to be reported in memory read packets
78 * see the code in gdb_read_memory_packet() for further explanations */
79 int gdb_report_data_abort = 0;
81 int gdb_last_signal(target_t *target)
83 switch (target->debug_reason)
85 case DBG_REASON_DBGRQ:
86 return 0x2; /* SIGINT */
87 case DBG_REASON_BREAKPOINT:
88 case DBG_REASON_WATCHPOINT:
89 case DBG_REASON_WPTANDBKPT:
90 return 0x05; /* SIGTRAP */
91 case DBG_REASON_SINGLESTEP:
92 return 0x05; /* SIGTRAP */
93 case DBG_REASON_NOTHALTED:
94 return 0x0; /* no signal... shouldn't happen */
96 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
101 int check_pending(connection_t *connection, int timeout_s, int *got_data)
103 /* a non-blocking socket will block if there is 0 bytes available on the socket,
104 * but return with as many bytes as are available immediately
108 gdb_connection_t *gdb_con = connection->priv;
114 if (gdb_con->buf_cnt>0)
121 FD_SET(connection->fd, &read_fds);
123 tv.tv_sec = timeout_s;
125 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
127 /* This can typically be because a "monitor" command took too long
128 * before printing any progress messages
132 return ERROR_GDB_TIMEOUT;
138 *got_data=FD_ISSET(connection->fd, &read_fds)!=0;
142 int gdb_get_char(connection_t *connection, int* next_char)
144 gdb_connection_t *gdb_con = connection->priv;
147 #ifdef _DEBUG_GDB_IO_
151 if (gdb_con->buf_cnt-- > 0)
153 *next_char = *(gdb_con->buf_p++);
154 if (gdb_con->buf_cnt > 0)
155 connection->input_pending = 1;
157 connection->input_pending = 0;
159 #ifdef _DEBUG_GDB_IO_
160 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
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);
172 if (gdb_con->buf_cnt > 0)
176 if (gdb_con->buf_cnt == 0)
179 return ERROR_SERVER_REMOTE_CLOSED;
183 errno = WSAGetLastError();
190 case WSAECONNABORTED:
192 return ERROR_SERVER_REMOTE_CLOSED;
195 return ERROR_SERVER_REMOTE_CLOSED;
197 LOG_ERROR("read: %d", errno);
208 return ERROR_SERVER_REMOTE_CLOSED;
211 return ERROR_SERVER_REMOTE_CLOSED;
213 LOG_ERROR("read: %s", strerror(errno));
215 return ERROR_SERVER_REMOTE_CLOSED;
220 #ifdef _DEBUG_GDB_IO_
221 debug_buffer = malloc(gdb_con->buf_cnt + 1);
222 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
223 debug_buffer[gdb_con->buf_cnt] = 0;
224 LOG_DEBUG("received '%s'", debug_buffer);
228 gdb_con->buf_p = gdb_con->buffer;
230 *next_char = *(gdb_con->buf_p++);
231 if (gdb_con->buf_cnt > 0)
232 connection->input_pending = 1;
234 connection->input_pending = 0;
235 #ifdef _DEBUG_GDB_IO_
236 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
242 int gdb_putback_char(connection_t *connection, int last_char)
244 gdb_connection_t *gdb_con = connection->priv;
246 if (gdb_con->buf_p > gdb_con->buffer)
248 *(--gdb_con->buf_p) = last_char;
253 LOG_ERROR("BUG: couldn't put character back");
259 /* The only way we can detect that the socket is closed is the first time
260 * we write to it, we will fail. Subsequent write operations will
261 * succeed. Shudder! */
262 int gdb_write(connection_t *connection, void *data, int len)
264 gdb_connection_t *gdb_con = connection->priv;
266 return ERROR_SERVER_REMOTE_CLOSED;
268 if (write_socket(connection->fd, data, len) == len)
273 return ERROR_SERVER_REMOTE_CLOSED;
276 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
279 unsigned char my_checksum = 0;
280 #ifdef _DEBUG_GDB_IO_
285 gdb_connection_t *gdb_con = connection->priv;
287 for (i = 0; i < len; i++)
288 my_checksum += buffer[i];
290 #ifdef _DEBUG_GDB_IO_
292 * At this point we should have nothing in the input queue from GDB,
293 * however sometimes '-' is sent even though we've already received
294 * an ACK (+) for everything we've sent off.
299 if ((retval=check_pending(connection, 0, &gotdata))!=ERROR_OK)
303 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
305 LOG_WARNING("Discard unexpected char %c", reply);
311 #ifdef _DEBUG_GDB_IO_
312 debug_buffer = malloc(len + 1);
313 memcpy(debug_buffer, buffer, len);
314 debug_buffer[len] = 0;
315 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
319 char local_buffer[1024];
320 local_buffer[0] = '$';
321 if (len+4 <= sizeof(local_buffer))
323 /* performance gain on smaller packets by only a single call to gdb_write() */
324 memcpy(local_buffer+1, buffer, len++);
325 local_buffer[len++] = '#';
326 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
327 local_buffer[len++] = DIGITS[my_checksum & 0xf];
328 gdb_write(connection, local_buffer, len);
332 /* larger packets are transmitted directly from caller supplied buffer
333 by several calls to gdb_write() to avoid dynamic allocation */
334 local_buffer[1] = '#';
335 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
336 local_buffer[3] = DIGITS[my_checksum & 0xf];
337 gdb_write(connection, local_buffer, 1);
338 gdb_write(connection, buffer, len);
339 gdb_write(connection, local_buffer+1, 3);
342 if (gdb_con->noack_mode)
345 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
350 else if (reply == '-')
352 /* Stop sending output packets for now */
353 log_remove_callback(gdb_log_callback, connection);
354 LOG_WARNING("negative reply, retrying");
356 else if (reply == 0x3)
359 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
363 else if (reply == '-')
365 /* Stop sending output packets for now */
366 log_remove_callback(gdb_log_callback, connection);
367 LOG_WARNING("negative reply, retrying");
371 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
373 return ERROR_SERVER_REMOTE_CLOSED;
378 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
380 return ERROR_SERVER_REMOTE_CLOSED;
384 return ERROR_SERVER_REMOTE_CLOSED;
389 int gdb_put_packet(connection_t *connection, char *buffer, int len)
391 gdb_connection_t *gdb_con = connection->priv;
393 int retval = gdb_put_packet_inner(connection, buffer, len);
398 static __inline__ int fetch_packet(connection_t *connection, int *checksum_ok, int noack, int *len, char *buffer)
400 unsigned char my_checksum = 0;
405 gdb_connection_t *gdb_con = connection->priv;
411 /* The common case is that we have an entire packet with no escape chars.
412 * We need to leave at least 2 bytes in the buffer to have
413 * gdb_get_char() update various bits and bobs correctly.
415 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
417 /* The compiler will struggle a bit with constant propagation and
418 * aliasing, so we help it by showing that these values do not
419 * change inside the loop
422 char *buf = gdb_con->buf_p;
423 int run = gdb_con->buf_cnt - 2;
430 if (character == '#')
432 /* Danger! character can be '#' when esc is
433 * used so we need an explicit boolean for done here.
439 if (character == '}')
441 /* data transmitted in binary mode (X packet)
442 * uses 0x7d as escape character */
443 my_checksum += character & 0xff;
446 my_checksum += character & 0xff;
447 buffer[count++] = (character ^ 0x20) & 0xff;
451 my_checksum += character & 0xff;
452 buffer[count++] = character & 0xff;
456 gdb_con->buf_cnt -= i;
462 LOG_ERROR("packet buffer too small");
463 return ERROR_GDB_BUFFER_TOO_SMALL;
466 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
469 if (character == '#')
472 if (character == '}')
474 /* data transmitted in binary mode (X packet)
475 * uses 0x7d as escape character */
476 my_checksum += character & 0xff;
477 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
479 my_checksum += character & 0xff;
480 buffer[count++] = (character ^ 0x20) & 0xff;
484 my_checksum += character & 0xff;
485 buffer[count++] = character & 0xff;
491 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
493 checksum[0] = character;
494 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
496 checksum[1] = character;
501 *checksum_ok=(my_checksum == strtoul(checksum, NULL, 16));
507 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
511 gdb_connection_t *gdb_con = connection->priv;
517 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
520 #ifdef _DEBUG_GDB_IO_
521 LOG_DEBUG("character: '%c'", character);
529 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
530 * incase anyone tries to debug why they receive this warning every time */
531 LOG_WARNING("acknowledgment received, but no packet pending");
534 LOG_WARNING("negative acknowledgment, but no packet pending");
541 LOG_WARNING("ignoring character 0x%x", character);
544 } while (character != '$');
549 /* explicit code expansion here to get faster inlined code in -O3 by not
550 * calculating checksum
552 if (gdb_con->noack_mode)
554 if ((retval=fetch_packet(connection, &checksum_ok, 1, len, buffer))!=ERROR_OK)
558 if ((retval=fetch_packet(connection, &checksum_ok, 0, len, buffer))!=ERROR_OK)
562 if (gdb_con->noack_mode)
564 /* checksum is not checked in noack mode */
569 gdb_write(connection, "+", 1);
574 return ERROR_SERVER_REMOTE_CLOSED;
579 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
581 gdb_connection_t *gdb_con = connection->priv;
583 int retval = gdb_get_packet_inner(connection, buffer, len);
588 int gdb_output_con(connection_t *connection, const char* line)
593 bin_size = strlen(line);
595 hex_buffer = malloc(bin_size*2 + 2);
596 if (hex_buffer == NULL)
597 return ERROR_GDB_BUFFER_TOO_SMALL;
600 for (i=0; i<bin_size; i++)
601 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
602 hex_buffer[bin_size*2+1] = 0;
604 gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
610 int gdb_output(struct command_context_s *context, const char* line)
612 /* this will be dumped to the log and also sent as an O packet if possible */
613 LOG_USER_N("%s", line);
617 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
619 struct command_context_s *cmd_ctx = priv;
621 target_invoke_script(cmd_ctx, target, "gdb_program");
622 jtag_execute_queue();
627 static void gdb_frontend_halted(struct target_s *target, connection_t *connection)
629 gdb_connection_t *gdb_connection = connection->priv;
631 /* In the GDB protocol when we are stepping or coninuing execution,
632 * we have a lingering reply. Upon receiving a halted event
633 * when we have that lingering packet, we reply to the original
634 * step or continue packet.
636 * Executing monitor commands can bring the target in and
637 * out of the running state so we'll see lots of TARGET_EVENT_XXX
638 * that are to be ignored.
640 if (gdb_connection->frontend_state == TARGET_RUNNING)
644 /* stop forwarding log packets! */
645 log_remove_callback(gdb_log_callback, connection);
647 if (gdb_connection->ctrl_c)
650 gdb_connection->ctrl_c = 0;
654 signal = gdb_last_signal(target);
657 snprintf(sig_reply, 4, "T%2.2x", signal);
658 gdb_put_packet(connection, sig_reply, 3);
659 gdb_connection->frontend_state = TARGET_HALTED;
663 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
665 connection_t *connection = priv;
669 case TARGET_EVENT_HALTED:
670 gdb_frontend_halted(target, connection);
672 case TARGET_EVENT_GDB_FLASH_ERASE_START:
673 gdb_program_handler(target, event, connection->cmd_ctx);
683 int gdb_new_connection(connection_t *connection)
685 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
686 gdb_service_t *gdb_service = connection->service->priv;
690 connection->priv = gdb_connection;
692 /* initialize gdb connection information */
693 gdb_connection->buf_p = gdb_connection->buffer;
694 gdb_connection->buf_cnt = 0;
695 gdb_connection->ctrl_c = 0;
696 gdb_connection->frontend_state = TARGET_HALTED;
697 gdb_connection->vflash_image = NULL;
698 gdb_connection->closed = 0;
699 gdb_connection->busy = 0;
700 gdb_connection->noack_mode = 0;
702 /* send ACK to GDB for debug request */
703 gdb_write(connection, "+", 1);
705 /* output goes through gdb connection */
706 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
708 /* we must remove all breakpoints registered to the target as a previous
709 * GDB session could leave dangling breakpoints if e.g. communication
712 breakpoint_clear_target(gdb_service->target);
713 watchpoint_clear_target(gdb_service->target);
715 /* register callback to be informed about target events */
716 target_register_event_callback(gdb_target_callback_event_handler, connection);
718 /* a gdb session just attached, try to put the target in halt mode.
722 * If the halt fails(e.g. target needs a reset, JTAG communication not
723 * working, etc.), then the GDB connect will succeed as
724 * the get_gdb_reg_list() will lie and return a register list with
727 * This allows GDB monitor commands to be run from a GDB init script to
728 * initialize the target
730 * Also, since the halt() is asynchronous target connect will be
731 * instantaneous and thus avoiding annoying timeout problems during
734 target_halt(gdb_service->target);
735 /* FIX!!!! could extended-remote work better here?
737 * wait a tiny bit for halted state or we just continue. The
738 * GDB register packet will then contain garbage
740 target_wait_state(gdb_service->target, TARGET_HALTED, 500);
742 /* remove the initial ACK from the incoming buffer */
743 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
746 /* FIX!!!??? would we actually ever receive a + here???
749 if (initial_ack != '+')
750 gdb_putback_char(connection, initial_ack);
755 int gdb_connection_closed(connection_t *connection)
757 gdb_service_t *gdb_service = connection->service->priv;
758 gdb_connection_t *gdb_connection = connection->priv;
760 /* see if an image built with vFlash commands is left */
761 if (gdb_connection->vflash_image)
763 image_close(gdb_connection->vflash_image);
764 free(gdb_connection->vflash_image);
765 gdb_connection->vflash_image = NULL;
768 /* if this connection registered a debug-message receiver delete it */
769 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
771 if (connection->priv)
773 free(connection->priv);
774 connection->priv = NULL;
778 LOG_ERROR("BUG: connection->priv == NULL");
781 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
782 log_remove_callback(gdb_log_callback, connection);
787 void gdb_send_error(connection_t *connection, u8 the_error)
790 snprintf(err, 4, "E%2.2X", the_error );
791 gdb_put_packet(connection, err, 3);
794 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
799 signal = gdb_last_signal(target);
801 snprintf(sig_reply, 4, "S%2.2x", signal);
802 gdb_put_packet(connection, sig_reply, 3);
807 /* Convert register to string of bits. NB! The # of bits in the
808 * register might be non-divisible by 8(a byte), in which
809 * case an entire byte is shown. */
810 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
817 buf_len = CEIL(reg->size, 8);
819 for (i = 0; i < buf_len; i++)
821 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
822 tstr[i*2+1] = DIGITS[buf[i]&0xf];
826 void gdb_target_to_str(target_t *target, char *tstr, char *str)
828 int str_len = strlen(tstr);
833 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
837 for (i = 0; i < str_len; i+=2)
839 str[str_len - i - 1] = tstr[i + 1];
840 str[str_len - i - 2] = tstr[i];
844 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
849 int reg_packet_size = 0;
854 #ifdef _DEBUG_GDB_IO_
858 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
860 return gdb_error(connection, retval);
863 for (i = 0; i < reg_list_size; i++)
865 reg_packet_size += reg_list[i]->size;
868 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
869 reg_packet_p = reg_packet;
871 for (i = 0; i < reg_list_size; i++)
873 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
874 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
877 #ifdef _DEBUG_GDB_IO_
880 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
881 LOG_DEBUG("reg_packet: %s", reg_packet_p);
886 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
894 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
902 #ifdef _DEBUG_GDB_IO_
906 /* skip command character */
912 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
913 return ERROR_SERVER_REMOTE_CLOSED;
916 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
918 return gdb_error(connection, retval);
922 for (i = 0; i < reg_list_size; i++)
926 reg_arch_type_t *arch_type;
928 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
929 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
930 gdb_target_to_str(target, packet_p, hex_buf);
932 /* convert hex-string to binary buffer */
933 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
934 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
936 /* get register arch_type, and call set method */
937 arch_type = register_get_arch_type(reg_list[i]->arch_type);
938 if (arch_type == NULL)
940 LOG_ERROR("BUG: encountered unregistered arch type");
943 arch_type->set(reg_list[i], bin_buf);
945 /* advance packet pointer */
946 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
952 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
955 gdb_put_packet(connection, "OK", 2);
960 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
963 int reg_num = strtoul(packet + 1, NULL, 16);
968 #ifdef _DEBUG_GDB_IO_
972 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
974 return gdb_error(connection, retval);
977 if (reg_list_size <= reg_num)
979 LOG_ERROR("gdb requested a non-existing register");
983 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
985 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
987 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
995 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1000 int reg_num = strtoul(packet + 1, &separator, 16);
1004 reg_arch_type_t *arch_type;
1008 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1010 return gdb_error(connection, retval);
1013 if (reg_list_size < reg_num)
1015 LOG_ERROR("gdb requested a non-existing register");
1016 return ERROR_SERVER_REMOTE_CLOSED;
1019 if (*separator != '=')
1021 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1022 return ERROR_SERVER_REMOTE_CLOSED;
1025 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1026 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1027 gdb_target_to_str(target, separator + 1, hex_buf);
1029 /* convert hex-string to binary buffer */
1030 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1031 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1033 /* get register arch_type, and call set method */
1034 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1035 if (arch_type == NULL)
1037 LOG_ERROR("BUG: encountered unregistered arch type");
1040 arch_type->set(reg_list[reg_num], bin_buf);
1042 gdb_put_packet(connection, "OK", 2);
1051 int gdb_error(connection_t *connection, int retval)
1055 case ERROR_TARGET_DATA_ABORT:
1056 gdb_send_error(connection, EIO);
1058 case ERROR_TARGET_TRANSLATION_FAULT:
1059 gdb_send_error(connection, EFAULT);
1061 case ERROR_TARGET_UNALIGNED_ACCESS:
1062 gdb_send_error(connection, EFAULT);
1064 case ERROR_TARGET_NOT_HALTED:
1065 gdb_send_error(connection, EFAULT);
1068 /* This could be that the target reset itself. */
1069 LOG_ERROR("unexpected error %i", retval);
1070 gdb_send_error(connection, EFAULT);
1077 /* We don't have to worry about the default 2 second timeout for GDB packets,
1078 * because GDB breaks up large memory reads into smaller reads.
1080 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1082 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1091 int retval = ERROR_OK;
1093 /* skip command character */
1096 addr = strtoul(packet, &separator, 16);
1098 if (*separator != ',')
1100 LOG_ERROR("incomplete read memory packet received, dropping connection");
1101 return ERROR_SERVER_REMOTE_CLOSED;
1104 len = strtoul(separator+1, NULL, 16);
1106 buffer = malloc(len);
1108 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1110 retval = target_read_buffer(target, addr, len, buffer);
1112 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1114 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1115 * At some point this might be fixed in GDB, in which case this code can be removed.
1117 * OpenOCD developers are acutely aware of this problem, but there is nothing
1118 * gained by involving the user in this problem that hopefully will get resolved
1121 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1123 * For now, the default is to fix up things to make current GDB versions work.
1124 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1126 memset(buffer, 0, len);
1130 if (retval == ERROR_OK)
1132 hex_buffer = malloc(len * 2 + 1);
1135 for (i = 0; i < len; i++)
1138 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1139 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1142 gdb_put_packet(connection, hex_buffer, len * 2);
1148 retval = gdb_error(connection, retval);
1156 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1167 /* skip command character */
1170 addr = strtoul(packet, &separator, 16);
1172 if (*separator != ',')
1174 LOG_ERROR("incomplete write memory packet received, dropping connection");
1175 return ERROR_SERVER_REMOTE_CLOSED;
1178 len = strtoul(separator+1, &separator, 16);
1180 if (*(separator++) != ':')
1182 LOG_ERROR("incomplete write memory packet received, dropping connection");
1183 return ERROR_SERVER_REMOTE_CLOSED;
1186 buffer = malloc(len);
1188 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1190 for (i=0; i<len; i++)
1193 sscanf(separator + 2*i, "%2x", &tmp);
1197 retval = target_write_buffer(target, addr, len, buffer);
1199 if (retval == ERROR_OK)
1201 gdb_put_packet(connection, "OK", 2);
1205 retval = gdb_error(connection, retval);
1213 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1221 /* skip command character */
1224 addr = strtoul(packet, &separator, 16);
1226 if (*separator != ',')
1228 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1229 return ERROR_SERVER_REMOTE_CLOSED;
1232 len = strtoul(separator+1, &separator, 16);
1234 if (*(separator++) != ':')
1236 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1237 return ERROR_SERVER_REMOTE_CLOSED;
1243 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1245 retval = target_write_buffer(target, addr, len, (u8*)separator);
1248 if (retval == ERROR_OK)
1250 gdb_put_packet(connection, "OK", 2);
1254 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1261 int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1265 int retval=ERROR_OK;
1269 if (packet_size > 1)
1271 packet[packet_size] = 0;
1272 address = strtoul(packet + 1, NULL, 16);
1279 if (packet[0] == 'c')
1281 LOG_DEBUG("continue");
1282 target_invoke_script(connection->cmd_ctx, target, "pre_resume");
1283 retval=target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1285 else if (packet[0] == 's')
1288 retval=target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1293 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1296 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1297 enum watchpoint_rw wp_type;
1305 type = strtoul(packet + 1, &separator, 16);
1307 if (type == 0) /* memory breakpoint */
1308 bp_type = BKPT_SOFT;
1309 else if (type == 1) /* hardware breakpoint */
1310 bp_type = BKPT_HARD;
1311 else if (type == 2) /* write watchpoint */
1312 wp_type = WPT_WRITE;
1313 else if (type == 3) /* read watchpoint */
1315 else if (type == 4) /* access watchpoint */
1316 wp_type = WPT_ACCESS;
1318 if (gdb_breakpoint_override&&((bp_type==BKPT_SOFT)||(bp_type==BKPT_HARD)))
1320 bp_type=gdb_breakpoint_override_type;
1323 if (*separator != ',')
1325 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1326 return ERROR_SERVER_REMOTE_CLOSED;
1329 address = strtoul(separator+1, &separator, 16);
1331 if (*separator != ',')
1333 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1334 return ERROR_SERVER_REMOTE_CLOSED;
1337 size = strtoul(separator+1, &separator, 16);
1343 if (packet[0] == 'Z')
1345 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1347 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1352 gdb_put_packet(connection, "OK", 2);
1357 breakpoint_remove(target, address);
1358 gdb_put_packet(connection, "OK", 2);
1365 if (packet[0] == 'Z')
1367 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1369 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1374 gdb_put_packet(connection, "OK", 2);
1379 watchpoint_remove(target, address);
1380 gdb_put_packet(connection, "OK", 2);
1391 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1392 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1394 if (*retval != ERROR_OK)
1402 if ((*xml == NULL) || (!first))
1404 /* start by 0 to exercise all the code paths.
1405 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1407 *size = *size * 2 + 2;
1409 *xml = realloc(*xml, *size);
1414 *retval = ERROR_SERVER_REMOTE_CLOSED;
1422 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1424 if ((ret > 0) && ((ret + 1) < *size - *pos))
1429 /* there was just enough or not enough space, allocate more. */
1434 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1438 /* Extract and NUL-terminate the annex. */
1440 while (*buf && *buf != ':')
1446 /* After the read marker and annex, qXfer looks like a
1447 * traditional 'm' packet. */
1449 *ofs = strtoul(buf, &separator, 16);
1451 if (*separator != ',')
1454 *len = strtoul(separator+1, NULL, 16);
1459 int gdb_calc_blocksize(flash_bank_t *bank)
1462 int block_size = 0xffffffff;
1464 /* loop through all sectors and return smallest sector size */
1466 for (i = 0; i < bank->num_sectors; i++)
1468 if (bank->sectors[i].size < block_size)
1469 block_size = bank->sectors[i].size;
1475 static int compare_bank (const void * a, const void * b)
1477 flash_bank_t *b1, *b2;
1478 b1=*((flash_bank_t **)a);
1479 b2=*((flash_bank_t **)b);
1481 if (b1->base==b2->base)
1484 } else if (b1->base>b2->base)
1493 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1495 command_context_t *cmd_ctx = connection->cmd_ctx;
1496 gdb_connection_t *gdb_connection = connection->priv;
1498 if (strstr(packet, "qRcmd,"))
1500 if (packet_size > 6)
1504 cmd = malloc((packet_size - 6)/2 + 1);
1505 for (i=0; i < (packet_size - 6)/2; i++)
1508 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1511 cmd[(packet_size - 6)/2] = 0x0;
1513 /* We want to print all debug output to GDB connection */
1514 log_add_callback(gdb_log_callback, connection);
1515 target_call_timer_callbacks_now();
1516 command_run_line(cmd_ctx, cmd);
1517 target_call_timer_callbacks_now();
1518 log_remove_callback(gdb_log_callback, connection);
1521 gdb_put_packet(connection, "OK", 2);
1524 else if (strstr(packet, "qCRC:"))
1526 if (packet_size > 5)
1535 /* skip command character */
1538 addr = strtoul(packet, &separator, 16);
1540 if (*separator != ',')
1542 LOG_ERROR("incomplete read memory packet received, dropping connection");
1543 return ERROR_SERVER_REMOTE_CLOSED;
1546 len = strtoul(separator + 1, NULL, 16);
1548 retval = target_checksum_memory(target, addr, len, &checksum);
1550 if (retval == ERROR_OK)
1552 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1553 gdb_put_packet(connection, gdb_reply, 9);
1557 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1564 else if (strstr(packet, "qSupported"))
1566 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1567 * disable qXfer:features:read for the moment */
1568 int retval = ERROR_OK;
1569 char *buffer = NULL;
1573 xml_printf(&retval, &buffer, &pos, &size,
1574 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1575 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1577 if (retval != ERROR_OK)
1579 gdb_send_error(connection, 01);
1583 gdb_put_packet(connection, buffer, strlen(buffer));
1588 else if (strstr(packet, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1590 /* We get away with only specifying flash here. Regions that are not
1591 * specified are treated as if we provided no memory map(if not we
1592 * could detect the holes and mark them as RAM).
1593 * Normally we only execute this code once, but no big deal if we
1594 * have to regenerate it a couple of times. */
1600 int retval = ERROR_OK;
1607 /* skip command character */
1610 offset = strtoul(packet, &separator, 16);
1611 length = strtoul(separator + 1, &separator, 16);
1613 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1616 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1617 read/write) by default for GDB.
1618 GDB does not have a concept of non-cacheable read/write memory.
1620 flash_bank_t **banks=malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
1623 for (i=0; i<flash_get_bank_count(); i++)
1625 p = get_flash_bank_by_num(i);
1629 retval = ERROR_FAIL;
1630 gdb_send_error(connection, retval);
1636 qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
1639 for (i=0; i<flash_get_bank_count(); i++)
1643 if (ram_start<p->base)
1645 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1646 ram_start, p->base-ram_start);
1649 /* if device has uneven sector sizes, eg. str7, lpc
1650 * we pass the smallest sector size to gdb memory map */
1651 blocksize = gdb_calc_blocksize(p);
1653 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1654 "<property name=\"blocksize\">0x%x</property>\n" \
1656 p->base, p->size, blocksize);
1657 ram_start=p->base+p->size;
1661 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1662 ram_start, 0-ram_start);
1665 /* a flash chip could be at the very end of the 32 bit address space, in which case
1666 ram_start will be precisely 0 */
1672 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1674 if (retval != ERROR_OK)
1676 gdb_send_error(connection, retval);
1680 if (offset + length > pos)
1682 length = pos - offset;
1685 char *t = malloc(length + 1);
1687 memcpy(t + 1, xml + offset, length);
1688 gdb_put_packet(connection, t, length + 1);
1694 else if (strstr(packet, "qXfer:features:read:"))
1699 int retval = ERROR_OK;
1702 unsigned int length;
1705 /* skip command character */
1708 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1710 gdb_send_error(connection, 01);
1714 if (strcmp(annex, "target.xml") != 0)
1716 gdb_send_error(connection, 01);
1720 xml_printf(&retval, &xml, &pos, &size, \
1721 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1723 if (retval != ERROR_OK)
1725 gdb_send_error(connection, retval);
1729 gdb_put_packet(connection, xml, strlen(xml));
1734 else if (strstr(packet, "QStartNoAckMode"))
1736 gdb_connection->noack_mode = 1;
1737 gdb_put_packet(connection, "OK", 2);
1741 gdb_put_packet(connection, "", 0);
1745 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1747 gdb_connection_t *gdb_connection = connection->priv;
1748 gdb_service_t *gdb_service = connection->service->priv;
1751 /* if flash programming disabled - send a empty reply */
1753 if (gdb_flash_program == 0)
1755 gdb_put_packet(connection, "", 0);
1759 if (strstr(packet, "vFlashErase:"))
1762 unsigned long length;
1764 char *parse = packet + 12;
1767 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1768 return ERROR_SERVER_REMOTE_CLOSED;
1771 addr = strtoul(parse, &parse, 16);
1773 if (*(parse++) != ',' || *parse == '\0')
1775 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1776 return ERROR_SERVER_REMOTE_CLOSED;
1779 length = strtoul(parse, &parse, 16);
1783 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1784 return ERROR_SERVER_REMOTE_CLOSED;
1787 /* assume all sectors need erasing - stops any problems
1788 * when flash_write is called multiple times */
1791 /* perform any target specific operations before the erase */
1792 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1795 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1797 /* GDB doesn't evaluate the actual error number returned,
1798 * treat a failed erase as an I/O error
1800 gdb_send_error(connection, EIO);
1801 LOG_ERROR("flash_erase returned %i", result);
1804 gdb_put_packet(connection, "OK", 2);
1809 if (strstr(packet, "vFlashWrite:"))
1812 unsigned long length;
1813 char *parse = packet + 12;
1817 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1818 return ERROR_SERVER_REMOTE_CLOSED;
1820 addr = strtoul(parse, &parse, 16);
1821 if (*(parse++) != ':')
1823 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1824 return ERROR_SERVER_REMOTE_CLOSED;
1826 length = packet_size - (parse - packet);
1828 /* create a new image if there isn't already one */
1829 if (gdb_connection->vflash_image == NULL)
1831 gdb_connection->vflash_image = malloc(sizeof(image_t));
1832 image_open(gdb_connection->vflash_image, "", "build");
1835 /* create new section with content from packet buffer */
1836 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1838 gdb_put_packet(connection, "OK", 2);
1843 if (!strcmp(packet, "vFlashDone"))
1847 /* process the flashing buffer. No need to erase as GDB
1848 * always issues a vFlashErase first. */
1849 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0)) != ERROR_OK)
1851 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1852 gdb_put_packet(connection, "E.memtype", 9);
1854 gdb_send_error(connection, EIO);
1858 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written);
1859 gdb_put_packet(connection, "OK", 2);
1862 image_close(gdb_connection->vflash_image);
1863 free(gdb_connection->vflash_image);
1864 gdb_connection->vflash_image = NULL;
1869 gdb_put_packet(connection, "", 0);
1873 int gdb_detach(connection_t *connection, target_t *target)
1875 switch( detach_mode )
1877 case GDB_DETACH_RESUME:
1878 target_invoke_script(connection->cmd_ctx, target, "pre_resume");
1879 target_resume(target, 1, 0, 1, 0);
1882 case GDB_DETACH_RESET:
1883 /* FIX?? make this configurable?? */
1884 target_process_reset(connection->cmd_ctx, RESET_HALT);
1887 case GDB_DETACH_HALT:
1888 target_halt(target);
1891 case GDB_DETACH_NOTHING:
1895 gdb_put_packet(connection, "OK", 2);
1900 static void gdb_log_callback(void *priv, const char *file, int line,
1901 const char *function, const char *string)
1903 connection_t *connection = priv;
1904 gdb_connection_t *gdb_con = connection->priv;
1908 /* do not reply this using the O packet */
1912 gdb_output_con(connection, string);
1915 /* Do not allocate this on the stack */
1916 char gdb_packet_buffer[GDB_BUFFER_SIZE];
1918 static void gdb_sig_halted(connection_t *connection)
1921 snprintf(sig_reply, 4, "T%2.2x", 2);
1922 gdb_put_packet(connection, sig_reply, 3);
1926 int gdb_input_inner(connection_t *connection)
1928 gdb_service_t *gdb_service = connection->service->priv;
1929 target_t *target = gdb_service->target;
1930 char *packet=gdb_packet_buffer;
1933 gdb_connection_t *gdb_con = connection->priv;
1934 static int extended_protocol = 0;
1936 /* drain input buffer */
1939 packet_size = GDB_BUFFER_SIZE-1;
1940 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1945 /* terminate with zero */
1946 packet[packet_size] = 0;
1948 LOG_DEBUG("received packet: '%s'", packet);
1950 if (packet_size > 0)
1956 /* Hct... -- set thread
1957 * we don't have threads, send empty reply */
1958 gdb_put_packet(connection, NULL, 0);
1962 retval = gdb_query_packet(connection, target, packet, packet_size);
1965 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1968 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1971 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1974 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1977 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1980 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1984 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1987 gdb_last_signal_packet(connection, target, packet, packet_size);
1992 if (target->state != TARGET_HALTED)
1994 /* If the target isn't in the halted state, then we can't
1995 * step/continue. This might be early setup, etc.
1997 gdb_sig_halted(connection);
2000 /* We're running/stepping, in which case we can
2001 * forward log output until the target is halted
2003 gdb_connection_t *gdb_con = connection->priv;
2004 gdb_con->frontend_state = TARGET_RUNNING;
2005 log_add_callback(gdb_log_callback, connection);
2006 int retval=gdb_step_continue_packet(connection, target, packet, packet_size);
2007 if (retval!=ERROR_OK)
2009 /* we'll never receive a halted condition... issue a false one.. */
2010 gdb_frontend_halted(target, connection);
2016 retval = gdb_v_packet(connection, target, packet, packet_size);
2019 retval = gdb_detach(connection, target);
2020 extended_protocol = 0;
2023 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2027 if (extended_protocol != 0)
2029 gdb_put_packet(connection, "OK", 2);
2030 return ERROR_SERVER_REMOTE_CLOSED;
2032 /* handle extended remote protocol */
2033 extended_protocol = 1;
2034 gdb_put_packet(connection, "OK", 2);
2037 /* handle extended restart packet */
2038 breakpoint_clear_target(gdb_service->target);
2039 watchpoint_clear_target(gdb_service->target);
2040 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
2043 /* ignore unkown packets */
2044 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2045 gdb_put_packet(connection, NULL, 0);
2049 /* if a packet handler returned an error, exit input loop */
2050 if (retval != ERROR_OK)
2054 if (gdb_con->ctrl_c)
2056 if (target->state == TARGET_RUNNING)
2058 target_halt(target);
2059 gdb_con->ctrl_c = 0;
2063 } while (gdb_con->buf_cnt > 0);
2068 int gdb_input(connection_t *connection)
2070 int retval = gdb_input_inner(connection);
2071 gdb_connection_t *gdb_con = connection->priv;
2072 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2075 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2076 if (gdb_con->closed)
2077 return ERROR_SERVER_REMOTE_CLOSED;
2079 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2085 gdb_service_t *gdb_service;
2086 target_t *target = all_targets;
2090 LOG_WARNING("no gdb ports allocated as no target has been specified");
2096 LOG_WARNING("no gdb port specified, using default port 3333");
2102 char service_name[8];
2104 snprintf(service_name, 8, "gdb-%2.2i", target->target_number);
2106 gdb_service = malloc(sizeof(gdb_service_t));
2107 gdb_service->target = target;
2109 add_service("gdb", CONNECTION_GDB,
2110 gdb_port + target->target_number,
2111 1, gdb_new_connection, gdb_input,
2112 gdb_connection_closed,
2115 LOG_DEBUG("gdb service for target %s at port %i",
2117 gdb_port + target->target_number);
2119 target = target->next;
2125 /* daemon configuration command gdb_port */
2126 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2131 /* only if the port wasn't overwritten by cmdline */
2133 gdb_port = strtoul(args[0], NULL, 0);
2138 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2142 if (strcmp(args[0], "resume") == 0)
2144 detach_mode = GDB_DETACH_RESUME;
2147 else if (strcmp(args[0], "reset") == 0)
2149 detach_mode = GDB_DETACH_RESET;
2152 else if (strcmp(args[0], "halt") == 0)
2154 detach_mode = GDB_DETACH_HALT;
2157 else if (strcmp(args[0], "nothing") == 0)
2159 detach_mode = GDB_DETACH_NOTHING;
2164 LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2168 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2172 if (strcmp(args[0], "enable") == 0)
2174 gdb_use_memory_map = 1;
2177 else if (strcmp(args[0], "disable") == 0)
2179 gdb_use_memory_map = 0;
2184 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2188 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2192 if (strcmp(args[0], "enable") == 0)
2194 gdb_flash_program = 1;
2197 else if (strcmp(args[0], "disable") == 0)
2199 gdb_flash_program = 0;
2204 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2208 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2212 if (strcmp(args[0], "enable") == 0)
2214 gdb_report_data_abort = 1;
2217 else if (strcmp(args[0], "disable") == 0)
2219 gdb_report_data_abort = 0;
2224 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2228 /* daemon configuration command gdb_port */
2229 int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2236 gdb_breakpoint_override = 1;
2237 if (strcmp(args[0], "hard")==0)
2239 gdb_breakpoint_override_type=BKPT_HARD;
2240 } else if (strcmp(args[0], "soft")==0)
2242 gdb_breakpoint_override_type=BKPT_SOFT;
2243 } else if (strcmp(args[0], "disable") == 0)
2245 gdb_breakpoint_override = 0;
2249 return ERROR_COMMAND_SYNTAX_ERROR;
2251 if (gdb_breakpoint_override)
2253 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type==BKPT_HARD)?"hard":"soft");
2256 LOG_USER("breakpoint type is not overriden");
2263 int gdb_register_commands(command_context_t *command_context)
2265 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2266 COMMAND_CONFIG, "");
2267 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2268 COMMAND_CONFIG, "");
2269 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2270 COMMAND_CONFIG, "");
2271 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2272 COMMAND_CONFIG, "");
2273 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2274 COMMAND_CONFIG, "");
2275 register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
2276 COMMAND_EXEC, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2277 "The raison d'etre for this option is to support GDB GUI's without "
2278 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2279 "is not sufficient");