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);
618 static void gdb_frontend_halted(struct target_s *target, connection_t *connection)
620 gdb_connection_t *gdb_connection = connection->priv;
622 /* In the GDB protocol when we are stepping or coninuing execution,
623 * we have a lingering reply. Upon receiving a halted event
624 * when we have that lingering packet, we reply to the original
625 * step or continue packet.
627 * Executing monitor commands can bring the target in and
628 * out of the running state so we'll see lots of TARGET_EVENT_XXX
629 * that are to be ignored.
631 if (gdb_connection->frontend_state == TARGET_RUNNING)
635 /* stop forwarding log packets! */
636 log_remove_callback(gdb_log_callback, connection);
638 if (gdb_connection->ctrl_c)
641 gdb_connection->ctrl_c = 0;
645 signal = gdb_last_signal(target);
648 snprintf(sig_reply, 4, "T%2.2x", signal);
649 gdb_put_packet(connection, sig_reply, 3);
650 gdb_connection->frontend_state = TARGET_HALTED;
654 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
656 connection_t *connection = priv;
658 target_handle_event( target, event );
661 case TARGET_EVENT_HALTED:
662 gdb_frontend_halted(target, connection);
664 case TARGET_EVENT_GDB_FLASH_ERASE_START:
665 target_handle_event( target, TARGET_EVENT_OLD_gdb_program_config );
666 jtag_execute_queue();
676 int gdb_new_connection(connection_t *connection)
678 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
679 gdb_service_t *gdb_service = connection->service->priv;
683 connection->priv = gdb_connection;
685 /* initialize gdb connection information */
686 gdb_connection->buf_p = gdb_connection->buffer;
687 gdb_connection->buf_cnt = 0;
688 gdb_connection->ctrl_c = 0;
689 gdb_connection->frontend_state = TARGET_HALTED;
690 gdb_connection->vflash_image = NULL;
691 gdb_connection->closed = 0;
692 gdb_connection->busy = 0;
693 gdb_connection->noack_mode = 0;
695 /* send ACK to GDB for debug request */
696 gdb_write(connection, "+", 1);
698 /* output goes through gdb connection */
699 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
701 /* we must remove all breakpoints registered to the target as a previous
702 * GDB session could leave dangling breakpoints if e.g. communication
705 breakpoint_clear_target(gdb_service->target);
706 watchpoint_clear_target(gdb_service->target);
708 /* register callback to be informed about target events */
709 target_register_event_callback(gdb_target_callback_event_handler, connection);
711 /* a gdb session just attached, try to put the target in halt mode.
715 * If the halt fails(e.g. target needs a reset, JTAG communication not
716 * working, etc.), then the GDB connect will succeed as
717 * the get_gdb_reg_list() will lie and return a register list with
720 * This allows GDB monitor commands to be run from a GDB init script to
721 * initialize the target
723 * Also, since the halt() is asynchronous target connect will be
724 * instantaneous and thus avoiding annoying timeout problems during
727 target_halt(gdb_service->target);
728 /* FIX!!!! could extended-remote work better here?
730 * wait a tiny bit for halted state or we just continue. The
731 * GDB register packet will then contain garbage
733 target_wait_state(gdb_service->target, TARGET_HALTED, 500);
735 /* remove the initial ACK from the incoming buffer */
736 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
739 /* FIX!!!??? would we actually ever receive a + here???
742 if (initial_ack != '+')
743 gdb_putback_char(connection, initial_ack);
744 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH );
748 int gdb_connection_closed(connection_t *connection)
750 gdb_service_t *gdb_service = connection->service->priv;
751 gdb_connection_t *gdb_connection = connection->priv;
753 /* see if an image built with vFlash commands is left */
754 if (gdb_connection->vflash_image)
756 image_close(gdb_connection->vflash_image);
757 free(gdb_connection->vflash_image);
758 gdb_connection->vflash_image = NULL;
761 /* if this connection registered a debug-message receiver delete it */
762 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
764 if (connection->priv)
766 free(connection->priv);
767 connection->priv = NULL;
771 LOG_ERROR("BUG: connection->priv == NULL");
774 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
775 log_remove_callback(gdb_log_callback, connection);
777 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH );
781 void gdb_send_error(connection_t *connection, u8 the_error)
784 snprintf(err, 4, "E%2.2X", the_error );
785 gdb_put_packet(connection, err, 3);
788 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
793 signal = gdb_last_signal(target);
795 snprintf(sig_reply, 4, "S%2.2x", signal);
796 gdb_put_packet(connection, sig_reply, 3);
801 /* Convert register to string of bits. NB! The # of bits in the
802 * register might be non-divisible by 8(a byte), in which
803 * case an entire byte is shown. */
804 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
811 buf_len = CEIL(reg->size, 8);
813 for (i = 0; i < buf_len; i++)
815 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
816 tstr[i*2+1] = DIGITS[buf[i]&0xf];
820 void gdb_target_to_str(target_t *target, char *tstr, char *str)
822 int str_len = strlen(tstr);
827 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
831 for (i = 0; i < str_len; i+=2)
833 str[str_len - i - 1] = tstr[i + 1];
834 str[str_len - i - 2] = tstr[i];
838 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
843 int reg_packet_size = 0;
848 #ifdef _DEBUG_GDB_IO_
852 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
854 return gdb_error(connection, retval);
857 for (i = 0; i < reg_list_size; i++)
859 reg_packet_size += reg_list[i]->size;
862 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
863 reg_packet_p = reg_packet;
865 for (i = 0; i < reg_list_size; i++)
867 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
868 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
871 #ifdef _DEBUG_GDB_IO_
874 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
875 LOG_DEBUG("reg_packet: %s", reg_packet_p);
880 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
888 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
896 #ifdef _DEBUG_GDB_IO_
900 /* skip command character */
906 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
907 return ERROR_SERVER_REMOTE_CLOSED;
910 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
912 return gdb_error(connection, retval);
916 for (i = 0; i < reg_list_size; i++)
920 reg_arch_type_t *arch_type;
922 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
923 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
924 gdb_target_to_str(target, packet_p, hex_buf);
926 /* convert hex-string to binary buffer */
927 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
928 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
930 /* get register arch_type, and call set method */
931 arch_type = register_get_arch_type(reg_list[i]->arch_type);
932 if (arch_type == NULL)
934 LOG_ERROR("BUG: encountered unregistered arch type");
937 arch_type->set(reg_list[i], bin_buf);
939 /* advance packet pointer */
940 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
946 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
949 gdb_put_packet(connection, "OK", 2);
954 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
957 int reg_num = strtoul(packet + 1, NULL, 16);
962 #ifdef _DEBUG_GDB_IO_
966 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
968 return gdb_error(connection, retval);
971 if (reg_list_size <= reg_num)
973 LOG_ERROR("gdb requested a non-existing register");
977 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
979 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
981 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
989 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
994 int reg_num = strtoul(packet + 1, &separator, 16);
998 reg_arch_type_t *arch_type;
1002 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1004 return gdb_error(connection, retval);
1007 if (reg_list_size < reg_num)
1009 LOG_ERROR("gdb requested a non-existing register");
1010 return ERROR_SERVER_REMOTE_CLOSED;
1013 if (*separator != '=')
1015 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1016 return ERROR_SERVER_REMOTE_CLOSED;
1019 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1020 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1021 gdb_target_to_str(target, separator + 1, hex_buf);
1023 /* convert hex-string to binary buffer */
1024 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1025 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1027 /* get register arch_type, and call set method */
1028 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1029 if (arch_type == NULL)
1031 LOG_ERROR("BUG: encountered unregistered arch type");
1034 arch_type->set(reg_list[reg_num], bin_buf);
1036 gdb_put_packet(connection, "OK", 2);
1045 int gdb_error(connection_t *connection, int retval)
1049 case ERROR_TARGET_DATA_ABORT:
1050 gdb_send_error(connection, EIO);
1052 case ERROR_TARGET_TRANSLATION_FAULT:
1053 gdb_send_error(connection, EFAULT);
1055 case ERROR_TARGET_UNALIGNED_ACCESS:
1056 gdb_send_error(connection, EFAULT);
1058 case ERROR_TARGET_NOT_HALTED:
1059 gdb_send_error(connection, EFAULT);
1062 /* This could be that the target reset itself. */
1063 LOG_ERROR("unexpected error %i", retval);
1064 gdb_send_error(connection, EFAULT);
1071 /* We don't have to worry about the default 2 second timeout for GDB packets,
1072 * because GDB breaks up large memory reads into smaller reads.
1074 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1076 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1085 int retval = ERROR_OK;
1087 /* skip command character */
1090 addr = strtoul(packet, &separator, 16);
1092 if (*separator != ',')
1094 LOG_ERROR("incomplete read memory packet received, dropping connection");
1095 return ERROR_SERVER_REMOTE_CLOSED;
1098 len = strtoul(separator+1, NULL, 16);
1100 buffer = malloc(len);
1102 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1104 retval = target_read_buffer(target, addr, len, buffer);
1106 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1108 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1109 * At some point this might be fixed in GDB, in which case this code can be removed.
1111 * OpenOCD developers are acutely aware of this problem, but there is nothing
1112 * gained by involving the user in this problem that hopefully will get resolved
1115 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1117 * For now, the default is to fix up things to make current GDB versions work.
1118 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1120 memset(buffer, 0, len);
1124 if (retval == ERROR_OK)
1126 hex_buffer = malloc(len * 2 + 1);
1129 for (i = 0; i < len; i++)
1132 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1133 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1136 gdb_put_packet(connection, hex_buffer, len * 2);
1142 retval = gdb_error(connection, retval);
1150 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1161 /* skip command character */
1164 addr = strtoul(packet, &separator, 16);
1166 if (*separator != ',')
1168 LOG_ERROR("incomplete write memory packet received, dropping connection");
1169 return ERROR_SERVER_REMOTE_CLOSED;
1172 len = strtoul(separator+1, &separator, 16);
1174 if (*(separator++) != ':')
1176 LOG_ERROR("incomplete write memory packet received, dropping connection");
1177 return ERROR_SERVER_REMOTE_CLOSED;
1180 buffer = malloc(len);
1182 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1184 for (i=0; i<len; i++)
1187 sscanf(separator + 2*i, "%2x", &tmp);
1191 retval = target_write_buffer(target, addr, len, buffer);
1193 if (retval == ERROR_OK)
1195 gdb_put_packet(connection, "OK", 2);
1199 retval = gdb_error(connection, retval);
1207 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1215 /* skip command character */
1218 addr = strtoul(packet, &separator, 16);
1220 if (*separator != ',')
1222 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1223 return ERROR_SERVER_REMOTE_CLOSED;
1226 len = strtoul(separator+1, &separator, 16);
1228 if (*(separator++) != ':')
1230 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1231 return ERROR_SERVER_REMOTE_CLOSED;
1237 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1239 retval = target_write_buffer(target, addr, len, (u8*)separator);
1242 if (retval == ERROR_OK)
1244 gdb_put_packet(connection, "OK", 2);
1248 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1255 int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1259 int retval=ERROR_OK;
1263 if (packet_size > 1)
1265 packet[packet_size] = 0;
1266 address = strtoul(packet + 1, NULL, 16);
1273 if (packet[0] == 'c')
1275 LOG_DEBUG("continue");
1276 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1277 retval=target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1279 else if (packet[0] == 's')
1282 retval=target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1287 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1290 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1291 enum watchpoint_rw wp_type;
1299 type = strtoul(packet + 1, &separator, 16);
1301 if (type == 0) /* memory breakpoint */
1302 bp_type = BKPT_SOFT;
1303 else if (type == 1) /* hardware breakpoint */
1304 bp_type = BKPT_HARD;
1305 else if (type == 2) /* write watchpoint */
1306 wp_type = WPT_WRITE;
1307 else if (type == 3) /* read watchpoint */
1309 else if (type == 4) /* access watchpoint */
1310 wp_type = WPT_ACCESS;
1312 if (gdb_breakpoint_override&&((bp_type==BKPT_SOFT)||(bp_type==BKPT_HARD)))
1314 bp_type=gdb_breakpoint_override_type;
1317 if (*separator != ',')
1319 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1320 return ERROR_SERVER_REMOTE_CLOSED;
1323 address = strtoul(separator+1, &separator, 16);
1325 if (*separator != ',')
1327 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1328 return ERROR_SERVER_REMOTE_CLOSED;
1331 size = strtoul(separator+1, &separator, 16);
1337 if (packet[0] == 'Z')
1339 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1341 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1346 gdb_put_packet(connection, "OK", 2);
1351 breakpoint_remove(target, address);
1352 gdb_put_packet(connection, "OK", 2);
1359 if (packet[0] == 'Z')
1361 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1363 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1368 gdb_put_packet(connection, "OK", 2);
1373 watchpoint_remove(target, address);
1374 gdb_put_packet(connection, "OK", 2);
1385 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1386 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1388 if (*retval != ERROR_OK)
1396 if ((*xml == NULL) || (!first))
1398 /* start by 0 to exercise all the code paths.
1399 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1401 *size = *size * 2 + 2;
1403 *xml = realloc(*xml, *size);
1408 *retval = ERROR_SERVER_REMOTE_CLOSED;
1416 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1418 if ((ret > 0) && ((ret + 1) < *size - *pos))
1423 /* there was just enough or not enough space, allocate more. */
1428 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1432 /* Extract and NUL-terminate the annex. */
1434 while (*buf && *buf != ':')
1440 /* After the read marker and annex, qXfer looks like a
1441 * traditional 'm' packet. */
1443 *ofs = strtoul(buf, &separator, 16);
1445 if (*separator != ',')
1448 *len = strtoul(separator+1, NULL, 16);
1453 int gdb_calc_blocksize(flash_bank_t *bank)
1456 int block_size = 0xffffffff;
1458 /* loop through all sectors and return smallest sector size */
1460 for (i = 0; i < bank->num_sectors; i++)
1462 if (bank->sectors[i].size < block_size)
1463 block_size = bank->sectors[i].size;
1469 static int compare_bank (const void * a, const void * b)
1471 flash_bank_t *b1, *b2;
1472 b1=*((flash_bank_t **)a);
1473 b2=*((flash_bank_t **)b);
1475 if (b1->base==b2->base)
1478 } else if (b1->base>b2->base)
1487 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1489 command_context_t *cmd_ctx = connection->cmd_ctx;
1490 gdb_connection_t *gdb_connection = connection->priv;
1492 if (strstr(packet, "qRcmd,"))
1494 if (packet_size > 6)
1498 cmd = malloc((packet_size - 6)/2 + 1);
1499 for (i=0; i < (packet_size - 6)/2; i++)
1502 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1505 cmd[(packet_size - 6)/2] = 0x0;
1507 /* We want to print all debug output to GDB connection */
1508 log_add_callback(gdb_log_callback, connection);
1509 target_call_timer_callbacks_now();
1510 command_run_line(cmd_ctx, cmd);
1511 target_call_timer_callbacks_now();
1512 log_remove_callback(gdb_log_callback, connection);
1515 gdb_put_packet(connection, "OK", 2);
1518 else if (strstr(packet, "qCRC:"))
1520 if (packet_size > 5)
1529 /* skip command character */
1532 addr = strtoul(packet, &separator, 16);
1534 if (*separator != ',')
1536 LOG_ERROR("incomplete read memory packet received, dropping connection");
1537 return ERROR_SERVER_REMOTE_CLOSED;
1540 len = strtoul(separator + 1, NULL, 16);
1542 retval = target_checksum_memory(target, addr, len, &checksum);
1544 if (retval == ERROR_OK)
1546 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1547 gdb_put_packet(connection, gdb_reply, 9);
1551 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1558 else if (strstr(packet, "qSupported"))
1560 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1561 * disable qXfer:features:read for the moment */
1562 int retval = ERROR_OK;
1563 char *buffer = NULL;
1567 xml_printf(&retval, &buffer, &pos, &size,
1568 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1569 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1571 if (retval != ERROR_OK)
1573 gdb_send_error(connection, 01);
1577 gdb_put_packet(connection, buffer, strlen(buffer));
1582 else if (strstr(packet, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1584 /* We get away with only specifying flash here. Regions that are not
1585 * specified are treated as if we provided no memory map(if not we
1586 * could detect the holes and mark them as RAM).
1587 * Normally we only execute this code once, but no big deal if we
1588 * have to regenerate it a couple of times. */
1594 int retval = ERROR_OK;
1601 /* skip command character */
1604 offset = strtoul(packet, &separator, 16);
1605 length = strtoul(separator + 1, &separator, 16);
1607 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1610 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1611 read/write) by default for GDB.
1612 GDB does not have a concept of non-cacheable read/write memory.
1614 flash_bank_t **banks=malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
1617 for (i=0; i<flash_get_bank_count(); i++)
1619 p = get_flash_bank_by_num(i);
1623 retval = ERROR_FAIL;
1624 gdb_send_error(connection, retval);
1630 qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
1633 for (i=0; i<flash_get_bank_count(); i++)
1637 if (ram_start<p->base)
1639 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1640 ram_start, p->base-ram_start);
1643 /* if device has uneven sector sizes, eg. str7, lpc
1644 * we pass the smallest sector size to gdb memory map */
1645 blocksize = gdb_calc_blocksize(p);
1647 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1648 "<property name=\"blocksize\">0x%x</property>\n" \
1650 p->base, p->size, blocksize);
1651 ram_start=p->base+p->size;
1655 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1656 ram_start, 0-ram_start);
1659 /* a flash chip could be at the very end of the 32 bit address space, in which case
1660 ram_start will be precisely 0 */
1666 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1668 if (retval != ERROR_OK)
1670 gdb_send_error(connection, retval);
1674 if (offset + length > pos)
1676 length = pos - offset;
1679 char *t = malloc(length + 1);
1681 memcpy(t + 1, xml + offset, length);
1682 gdb_put_packet(connection, t, length + 1);
1688 else if (strstr(packet, "qXfer:features:read:"))
1693 int retval = ERROR_OK;
1696 unsigned int length;
1699 /* skip command character */
1702 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1704 gdb_send_error(connection, 01);
1708 if (strcmp(annex, "target.xml") != 0)
1710 gdb_send_error(connection, 01);
1714 xml_printf(&retval, &xml, &pos, &size, \
1715 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1717 if (retval != ERROR_OK)
1719 gdb_send_error(connection, retval);
1723 gdb_put_packet(connection, xml, strlen(xml));
1728 else if (strstr(packet, "QStartNoAckMode"))
1730 gdb_connection->noack_mode = 1;
1731 gdb_put_packet(connection, "OK", 2);
1735 gdb_put_packet(connection, "", 0);
1739 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1741 gdb_connection_t *gdb_connection = connection->priv;
1742 gdb_service_t *gdb_service = connection->service->priv;
1745 /* if flash programming disabled - send a empty reply */
1747 if (gdb_flash_program == 0)
1749 gdb_put_packet(connection, "", 0);
1753 if (strstr(packet, "vFlashErase:"))
1756 unsigned long length;
1758 char *parse = packet + 12;
1761 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1762 return ERROR_SERVER_REMOTE_CLOSED;
1765 addr = strtoul(parse, &parse, 16);
1767 if (*(parse++) != ',' || *parse == '\0')
1769 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1770 return ERROR_SERVER_REMOTE_CLOSED;
1773 length = strtoul(parse, &parse, 16);
1777 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1778 return ERROR_SERVER_REMOTE_CLOSED;
1781 /* assume all sectors need erasing - stops any problems
1782 * when flash_write is called multiple times */
1785 /* perform any target specific operations before the erase */
1786 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1787 result = flash_erase_address_range(gdb_service->target, addr, length );
1788 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
1791 if (result != ERROR_OK)
1793 /* GDB doesn't evaluate the actual error number returned,
1794 * treat a failed erase as an I/O error
1796 gdb_send_error(connection, EIO);
1797 LOG_ERROR("flash_erase returned %i", result);
1800 gdb_put_packet(connection, "OK", 2);
1805 if (strstr(packet, "vFlashWrite:"))
1808 unsigned long length;
1809 char *parse = packet + 12;
1813 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1814 return ERROR_SERVER_REMOTE_CLOSED;
1816 addr = strtoul(parse, &parse, 16);
1817 if (*(parse++) != ':')
1819 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1820 return ERROR_SERVER_REMOTE_CLOSED;
1822 length = packet_size - (parse - packet);
1824 /* create a new image if there isn't already one */
1825 if (gdb_connection->vflash_image == NULL)
1827 gdb_connection->vflash_image = malloc(sizeof(image_t));
1828 image_open(gdb_connection->vflash_image, "", "build");
1831 /* create new section with content from packet buffer */
1832 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1834 gdb_put_packet(connection, "OK", 2);
1839 if (!strcmp(packet, "vFlashDone"))
1843 /* process the flashing buffer. No need to erase as GDB
1844 * always issues a vFlashErase first. */
1845 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
1846 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1847 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1848 if ( result != ERROR_OK)
1850 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1851 gdb_put_packet(connection, "E.memtype", 9);
1853 gdb_send_error(connection, EIO);
1857 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written);
1858 gdb_put_packet(connection, "OK", 2);
1861 image_close(gdb_connection->vflash_image);
1862 free(gdb_connection->vflash_image);
1863 gdb_connection->vflash_image = NULL;
1868 gdb_put_packet(connection, "", 0);
1872 int gdb_detach(connection_t *connection, target_t *target)
1875 switch( detach_mode )
1877 case GDB_DETACH_RESUME:
1878 target_handle_event( target, TARGET_EVENT_OLD_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);
1899 static void gdb_log_callback(void *priv, const char *file, int line,
1900 const char *function, const char *string)
1902 connection_t *connection = priv;
1903 gdb_connection_t *gdb_con = connection->priv;
1907 /* do not reply this using the O packet */
1911 gdb_output_con(connection, string);
1914 /* Do not allocate this on the stack */
1915 char gdb_packet_buffer[GDB_BUFFER_SIZE];
1917 static void gdb_sig_halted(connection_t *connection)
1920 snprintf(sig_reply, 4, "T%2.2x", 2);
1921 gdb_put_packet(connection, sig_reply, 3);
1925 int gdb_input_inner(connection_t *connection)
1927 gdb_service_t *gdb_service = connection->service->priv;
1928 target_t *target = gdb_service->target;
1929 char *packet=gdb_packet_buffer;
1932 gdb_connection_t *gdb_con = connection->priv;
1933 static int extended_protocol = 0;
1935 /* drain input buffer */
1938 packet_size = GDB_BUFFER_SIZE-1;
1939 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1944 /* terminate with zero */
1945 packet[packet_size] = 0;
1947 LOG_DEBUG("received packet: '%s'", packet);
1949 if (packet_size > 0)
1955 /* Hct... -- set thread
1956 * we don't have threads, send empty reply */
1957 gdb_put_packet(connection, NULL, 0);
1961 retval = gdb_query_packet(connection, target, packet, packet_size);
1964 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1967 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1970 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1973 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1976 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1979 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1983 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1986 gdb_last_signal_packet(connection, target, packet, packet_size);
1991 if (target->state != TARGET_HALTED)
1993 /* If the target isn't in the halted state, then we can't
1994 * step/continue. This might be early setup, etc.
1996 gdb_sig_halted(connection);
1999 /* We're running/stepping, in which case we can
2000 * forward log output until the target is halted
2002 gdb_connection_t *gdb_con = connection->priv;
2003 gdb_con->frontend_state = TARGET_RUNNING;
2004 log_add_callback(gdb_log_callback, connection);
2005 int retval=gdb_step_continue_packet(connection, target, packet, packet_size);
2006 if (retval!=ERROR_OK)
2008 /* we'll never receive a halted condition... issue a false one.. */
2009 gdb_frontend_halted(target, connection);
2015 retval = gdb_v_packet(connection, target, packet, packet_size);
2018 retval = gdb_detach(connection, target);
2019 extended_protocol = 0;
2022 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2026 if (extended_protocol != 0)
2028 gdb_put_packet(connection, "OK", 2);
2029 return ERROR_SERVER_REMOTE_CLOSED;
2031 /* handle extended remote protocol */
2032 extended_protocol = 1;
2033 gdb_put_packet(connection, "OK", 2);
2036 /* handle extended restart packet */
2037 breakpoint_clear_target(gdb_service->target);
2038 watchpoint_clear_target(gdb_service->target);
2039 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
2042 /* ignore unkown packets */
2043 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2044 gdb_put_packet(connection, NULL, 0);
2048 /* if a packet handler returned an error, exit input loop */
2049 if (retval != ERROR_OK)
2053 if (gdb_con->ctrl_c)
2055 if (target->state == TARGET_RUNNING)
2057 target_halt(target);
2058 gdb_con->ctrl_c = 0;
2062 } while (gdb_con->buf_cnt > 0);
2067 int gdb_input(connection_t *connection)
2069 int retval = gdb_input_inner(connection);
2070 gdb_connection_t *gdb_con = connection->priv;
2071 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2074 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2075 if (gdb_con->closed)
2076 return ERROR_SERVER_REMOTE_CLOSED;
2078 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2084 gdb_service_t *gdb_service;
2085 target_t *target = all_targets;
2089 LOG_WARNING("no gdb ports allocated as no target has been specified");
2095 LOG_WARNING("no gdb port specified, using default port 3333");
2101 char service_name[8];
2103 snprintf(service_name, 8, "gdb-%2.2i", target->target_number);
2105 gdb_service = malloc(sizeof(gdb_service_t));
2106 gdb_service->target = target;
2108 add_service("gdb", CONNECTION_GDB,
2109 gdb_port + target->target_number,
2110 1, gdb_new_connection, gdb_input,
2111 gdb_connection_closed,
2114 LOG_DEBUG("gdb service for target %s at port %i",
2116 gdb_port + target->target_number);
2118 target = target->next;
2124 /* daemon configuration command gdb_port */
2125 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2130 /* only if the port wasn't overwritten by cmdline */
2132 gdb_port = strtoul(args[0], NULL, 0);
2137 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2141 if (strcmp(args[0], "resume") == 0)
2143 detach_mode = GDB_DETACH_RESUME;
2146 else if (strcmp(args[0], "reset") == 0)
2148 detach_mode = GDB_DETACH_RESET;
2151 else if (strcmp(args[0], "halt") == 0)
2153 detach_mode = GDB_DETACH_HALT;
2156 else if (strcmp(args[0], "nothing") == 0)
2158 detach_mode = GDB_DETACH_NOTHING;
2163 LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2167 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2171 if (strcmp(args[0], "enable") == 0)
2173 gdb_use_memory_map = 1;
2176 else if (strcmp(args[0], "disable") == 0)
2178 gdb_use_memory_map = 0;
2183 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2187 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2191 if (strcmp(args[0], "enable") == 0)
2193 gdb_flash_program = 1;
2196 else if (strcmp(args[0], "disable") == 0)
2198 gdb_flash_program = 0;
2203 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2207 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2211 if (strcmp(args[0], "enable") == 0)
2213 gdb_report_data_abort = 1;
2216 else if (strcmp(args[0], "disable") == 0)
2218 gdb_report_data_abort = 0;
2223 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2227 /* daemon configuration command gdb_port */
2228 int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2235 gdb_breakpoint_override = 1;
2236 if (strcmp(args[0], "hard")==0)
2238 gdb_breakpoint_override_type=BKPT_HARD;
2239 } else if (strcmp(args[0], "soft")==0)
2241 gdb_breakpoint_override_type=BKPT_SOFT;
2242 } else if (strcmp(args[0], "disable") == 0)
2244 gdb_breakpoint_override = 0;
2248 return ERROR_COMMAND_SYNTAX_ERROR;
2250 if (gdb_breakpoint_override)
2252 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type==BKPT_HARD)?"hard":"soft");
2255 LOG_USER("breakpoint type is not overriden");
2262 int gdb_register_commands(command_context_t *command_context)
2264 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2265 COMMAND_CONFIG, "");
2266 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2267 COMMAND_CONFIG, "");
2268 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2269 COMMAND_CONFIG, "");
2270 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2271 COMMAND_CONFIG, "");
2272 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2273 COMMAND_CONFIG, "");
2274 register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
2275 COMMAND_EXEC, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2276 "The raison d'etre for this option is to support GDB GUI's without "
2277 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2278 "is not sufficient");
2285 * Local Variables: ***
2286 * c-basic-offset: 4 ***