1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * Copyright (C) 2011 by Broadcom Corporation *
12 * Evan Hunter - ehunter@broadcom.com *
14 * Copyright (C) ST-Ericsson SA 2011 *
15 * michel.jaouen@stericsson.com : smp minimum support *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
36 #include <target/breakpoints.h>
37 #include <target/target_request.h>
38 #include <target/register.h>
40 #include <flash/nor/core.h>
41 #include "gdb_server.h"
42 #include <target/image.h>
43 #include <jtag/jtag.h>
44 #include "rtos/rtos.h"
45 #include "target/smp.h"
50 * GDB server implementation.
52 * This implements the GDB Remote Serial Protocol, over TCP connections,
53 * giving GDB access to the JTAG or other hardware debugging facilities
54 * found in most modern embedded processors.
57 /* private connection data for GDB */
60 char buffer[GDB_BUFFER_SIZE];
64 enum target_state frontend_state;
65 struct image *vflash_image;
69 bool sync; /* set flag to true if you want the next stepi to return immediately.
70 allowing GDB to pick up a fresh set of register values from the target
71 without modifying the target state. */
72 /* We delay reporting memory write errors until next step/continue or memory
73 * write. This improves performance of gdb load significantly as the GDB packet
74 * can be replied immediately and a new GDB packet will be ready without delay
82 #define _DEBUG_GDB_IO_
85 static struct gdb_connection *current_gdb_connection;
87 static int gdb_breakpoint_override;
88 static enum breakpoint_type gdb_breakpoint_override_type;
90 static int gdb_error(struct connection *connection, int retval);
91 static const char *gdb_port;
92 static const char *gdb_port_next;
93 static const char DIGITS[16] = "0123456789abcdef";
95 static void gdb_log_callback(void *priv, const char *file, unsigned line,
96 const char *function, const char *string);
98 /* number of gdb connections, mainly to suppress gdb related debugging spam
99 * in helper/log.c when no gdb connections are actually active */
100 int gdb_actual_connections;
102 /* set if we are sending a memory map to gdb
103 * via qXfer:memory-map:read packet */
104 /* enabled by default*/
105 static int gdb_use_memory_map = 1;
106 /* enabled by default*/
107 static int gdb_flash_program = 1;
109 /* if set, data aborts cause an error to be reported in memory read packets
110 * see the code in gdb_read_memory_packet() for further explanations.
111 * Disabled by default.
113 static int gdb_report_data_abort;
115 static int gdb_last_signal(struct target *target)
117 switch (target->debug_reason)
119 case DBG_REASON_DBGRQ:
120 return 0x2; /* SIGINT */
121 case DBG_REASON_BREAKPOINT:
122 case DBG_REASON_WATCHPOINT:
123 case DBG_REASON_WPTANDBKPT:
124 return 0x05; /* SIGTRAP */
125 case DBG_REASON_SINGLESTEP:
126 return 0x05; /* SIGTRAP */
127 case DBG_REASON_NOTHALTED:
128 return 0x0; /* no signal... shouldn't happen */
130 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
135 static int check_pending(struct connection *connection,
136 int timeout_s, int *got_data)
138 /* a non-blocking socket will block if there is 0 bytes available on the socket,
139 * but return with as many bytes as are available immediately
143 struct gdb_connection *gdb_con = connection->priv;
145 if (got_data == NULL)
149 if (gdb_con->buf_cnt > 0)
156 FD_SET(connection->fd, &read_fds);
158 tv.tv_sec = timeout_s;
160 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
162 /* This can typically be because a "monitor" command took too long
163 * before printing any progress messages
167 return ERROR_GDB_TIMEOUT;
173 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
177 static int gdb_get_char_inner(struct connection *connection, int* next_char)
179 struct gdb_connection *gdb_con = connection->priv;
180 int retval = ERROR_OK;
182 #ifdef _DEBUG_GDB_IO_
187 if (connection->service->type != CONNECTION_TCP)
189 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
193 retval = check_pending(connection, 1, NULL);
194 if (retval != ERROR_OK)
196 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
199 if (gdb_con->buf_cnt > 0)
203 if (gdb_con->buf_cnt == 0)
206 return ERROR_SERVER_REMOTE_CLOSED;
210 errno = WSAGetLastError();
217 case WSAECONNABORTED:
219 return ERROR_SERVER_REMOTE_CLOSED;
222 return ERROR_SERVER_REMOTE_CLOSED;
224 LOG_ERROR("read: %d", errno);
235 return ERROR_SERVER_REMOTE_CLOSED;
238 return ERROR_SERVER_REMOTE_CLOSED;
240 LOG_ERROR("read: %s", strerror(errno));
242 return ERROR_SERVER_REMOTE_CLOSED;
247 #ifdef _DEBUG_GDB_IO_
248 debug_buffer = malloc(gdb_con->buf_cnt + 1);
249 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
250 debug_buffer[gdb_con->buf_cnt] = 0;
251 LOG_DEBUG("received '%s'", debug_buffer);
255 gdb_con->buf_p = gdb_con->buffer;
257 *next_char = *(gdb_con->buf_p++);
258 if (gdb_con->buf_cnt > 0)
259 connection->input_pending = 1;
261 connection->input_pending = 0;
262 #ifdef _DEBUG_GDB_IO_
263 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
270 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
271 * held in registers in the inner loop.
273 * For small caches and embedded systems this is important!
275 static inline int gdb_get_char_fast(struct connection *connection, int* next_char, char **buf_p, int *buf_cnt)
277 int retval = ERROR_OK;
279 if ((*buf_cnt)-- > 0)
281 *next_char = **buf_p;
284 connection->input_pending = 1;
286 connection->input_pending = 0;
288 #ifdef _DEBUG_GDB_IO_
289 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
295 struct gdb_connection *gdb_con = connection->priv;
296 gdb_con->buf_p = *buf_p;
297 gdb_con->buf_cnt = *buf_cnt;
298 retval = gdb_get_char_inner(connection, next_char);
299 *buf_p = gdb_con->buf_p;
300 *buf_cnt = gdb_con->buf_cnt;
306 static int gdb_get_char(struct connection *connection, int* next_char)
308 struct gdb_connection *gdb_con = connection->priv;
309 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
313 static int gdb_putback_char(struct connection *connection, int last_char)
315 struct gdb_connection *gdb_con = connection->priv;
317 if (gdb_con->buf_p > gdb_con->buffer)
319 *(--gdb_con->buf_p) = last_char;
324 LOG_ERROR("BUG: couldn't put character back");
330 /* The only way we can detect that the socket is closed is the first time
331 * we write to it, we will fail. Subsequent write operations will
332 * succeed. Shudder! */
333 static int gdb_write(struct connection *connection, void *data, int len)
335 struct gdb_connection *gdb_con = connection->priv;
337 return ERROR_SERVER_REMOTE_CLOSED;
339 if (connection_write(connection, data, len) == len)
344 return ERROR_SERVER_REMOTE_CLOSED;
347 static int gdb_put_packet_inner(struct connection *connection,
348 char *buffer, int len)
351 unsigned char my_checksum = 0;
352 #ifdef _DEBUG_GDB_IO_
357 struct gdb_connection *gdb_con = connection->priv;
359 for (i = 0; i < len; i++)
360 my_checksum += buffer[i];
362 #ifdef _DEBUG_GDB_IO_
364 * At this point we should have nothing in the input queue from GDB,
365 * however sometimes '-' is sent even though we've already received
366 * an ACK (+) for everything we've sent off.
371 retval = check_pending(connection, 0, &gotdata);
372 if (retval != ERROR_OK)
376 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
379 /* fix a problem with some IAR tools */
380 gdb_putback_char(connection, reply);
381 LOG_DEBUG("Unexpected start of new packet");
385 LOG_WARNING("Discard unexpected char %c", reply);
391 #ifdef _DEBUG_GDB_IO_
392 debug_buffer = malloc(len + 1);
393 memcpy(debug_buffer, buffer, len);
394 debug_buffer[len] = 0;
395 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
399 char local_buffer[1024];
400 local_buffer[0] = '$';
401 if ((size_t)len + 4 <= sizeof(local_buffer))
403 /* performance gain on smaller packets by only a single call to gdb_write() */
404 memcpy(local_buffer + 1, buffer, len++);
405 local_buffer[len++] = '#';
406 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
407 local_buffer[len++] = DIGITS[my_checksum & 0xf];
408 if ((retval = gdb_write(connection, local_buffer, len)) != ERROR_OK)
415 /* larger packets are transmitted directly from caller supplied buffer
416 by several calls to gdb_write() to avoid dynamic allocation */
417 local_buffer[1] = '#';
418 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
419 local_buffer[3] = DIGITS[my_checksum & 0xf];
420 if ((retval = gdb_write(connection, local_buffer, 1)) != ERROR_OK)
424 if ((retval = gdb_write(connection, buffer, len)) != ERROR_OK)
428 if ((retval = gdb_write(connection, local_buffer + 1, 3)) != ERROR_OK)
434 if (gdb_con->noack_mode)
437 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
442 else if (reply == '-')
444 /* Stop sending output packets for now */
445 log_remove_callback(gdb_log_callback, connection);
446 LOG_WARNING("negative reply, retrying");
448 else if (reply == 0x3)
451 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
455 else if (reply == '-')
457 /* Stop sending output packets for now */
458 log_remove_callback(gdb_log_callback, connection);
459 LOG_WARNING("negative reply, retrying");
461 else if (reply == '$') {
462 LOG_ERROR("GDB missing ack(1) - assumed good");
463 gdb_putback_char(connection, reply);
467 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
469 return ERROR_SERVER_REMOTE_CLOSED;
472 else if (reply == '$') {
473 LOG_ERROR("GDB missing ack(2) - assumed good");
474 gdb_putback_char(connection, reply);
479 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply);
481 return ERROR_SERVER_REMOTE_CLOSED;
485 return ERROR_SERVER_REMOTE_CLOSED;
490 int gdb_put_packet(struct connection *connection, char *buffer, int len)
492 struct gdb_connection *gdb_con = connection->priv;
494 int retval = gdb_put_packet_inner(connection, buffer, len);
497 /* we sent some data, reset timer for keep alive messages */
503 static __inline__ int fetch_packet(struct connection *connection, int *checksum_ok, int noack, int *len, char *buffer)
505 unsigned char my_checksum = 0;
508 int retval = ERROR_OK;
510 struct gdb_connection *gdb_con = connection->priv;
515 /* move this over into local variables to use registers and give the
516 * more freedom to optimize */
517 char *buf_p = gdb_con->buf_p;
518 int buf_cnt = gdb_con->buf_cnt;
522 /* The common case is that we have an entire packet with no escape chars.
523 * We need to leave at least 2 bytes in the buffer to have
524 * gdb_get_char() update various bits and bobs correctly.
526 if ((buf_cnt > 2) && ((buf_cnt + count) < *len))
528 /* The compiler will struggle a bit with constant propagation and
529 * aliasing, so we help it by showing that these values do not
530 * change inside the loop
534 int run = buf_cnt - 2;
541 if (character == '#')
543 /* Danger! character can be '#' when esc is
544 * used so we need an explicit boolean for done here.
550 if (character == '}')
552 /* data transmitted in binary mode (X packet)
553 * uses 0x7d as escape character */
554 my_checksum += character & 0xff;
557 my_checksum += character & 0xff;
558 buffer[count++] = (character ^ 0x20) & 0xff;
562 my_checksum += character & 0xff;
563 buffer[count++] = character & 0xff;
573 LOG_ERROR("packet buffer too small");
574 retval = ERROR_GDB_BUFFER_TOO_SMALL;
578 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
579 if (retval != ERROR_OK)
582 if (character == '#')
585 if (character == '}')
587 /* data transmitted in binary mode (X packet)
588 * uses 0x7d as escape character */
589 my_checksum += character & 0xff;
591 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
592 if (retval != ERROR_OK)
595 my_checksum += character & 0xff;
596 buffer[count++] = (character ^ 0x20) & 0xff;
600 my_checksum += character & 0xff;
601 buffer[count++] = character & 0xff;
605 gdb_con->buf_p = buf_p;
606 gdb_con->buf_cnt = buf_cnt;
608 if (retval != ERROR_OK)
613 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
615 checksum[0] = character;
616 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
618 checksum[1] = character;
623 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
629 static int gdb_get_packet_inner(struct connection *connection,
630 char *buffer, int *len)
634 struct gdb_connection *gdb_con = connection->priv;
640 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
643 #ifdef _DEBUG_GDB_IO_
644 LOG_DEBUG("character: '%c'", character);
652 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
653 * in case anyone tries to debug why they receive this warning every time */
654 LOG_WARNING("acknowledgment received, but no packet pending");
657 LOG_WARNING("negative acknowledgment, but no packet pending");
664 LOG_WARNING("ignoring character 0x%x", character);
667 } while (character != '$');
672 /* explicit code expansion here to get faster inlined code in -O3 by not
673 * calculating checksum
675 if (gdb_con->noack_mode)
677 if ((retval = fetch_packet(connection, &checksum_ok, 1, len, buffer)) != ERROR_OK)
681 if ((retval = fetch_packet(connection, &checksum_ok, 0, len, buffer)) != ERROR_OK)
685 if (gdb_con->noack_mode)
687 /* checksum is not checked in noack mode */
692 if ((retval = gdb_write(connection, "+", 1)) != ERROR_OK)
700 return ERROR_SERVER_REMOTE_CLOSED;
705 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
707 struct gdb_connection *gdb_con = connection->priv;
709 int retval = gdb_get_packet_inner(connection, buffer, len);
714 static int gdb_output_con(struct connection *connection, const char* line)
719 bin_size = strlen(line);
721 hex_buffer = malloc(bin_size*2 + 2);
722 if (hex_buffer == NULL)
723 return ERROR_GDB_BUFFER_TOO_SMALL;
726 for (i = 0; i < bin_size; i++)
727 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
728 hex_buffer[bin_size*2 + 1] = 0;
730 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
736 static int gdb_output(struct command_context *context, const char* line)
738 /* this will be dumped to the log and also sent as an O packet if possible */
739 LOG_USER_N("%s", line);
744 static void gdb_frontend_halted(struct target *target, struct connection *connection)
746 struct gdb_connection *gdb_connection = connection->priv;
748 /* In the GDB protocol when we are stepping or continuing execution,
749 * we have a lingering reply. Upon receiving a halted event
750 * when we have that lingering packet, we reply to the original
751 * step or continue packet.
753 * Executing monitor commands can bring the target in and
754 * out of the running state so we'll see lots of TARGET_EVENT_XXX
755 * that are to be ignored.
757 if (gdb_connection->frontend_state == TARGET_RUNNING)
762 /* stop forwarding log packets! */
763 log_remove_callback(gdb_log_callback, connection);
765 if (gdb_connection->ctrl_c)
768 gdb_connection->ctrl_c = 0;
772 signal_var = gdb_last_signal(target);
775 snprintf(sig_reply, 4, "T%2.2x", signal_var);
776 gdb_put_packet(connection, sig_reply, 3);
777 gdb_connection->frontend_state = TARGET_HALTED;
778 rtos_update_threads( target );
782 static int gdb_target_callback_event_handler(struct target *target,
783 enum target_event event, void *priv)
786 struct connection *connection = priv;
788 target_handle_event(target, event);
791 case TARGET_EVENT_GDB_HALT:
792 gdb_frontend_halted(target, connection);
794 case TARGET_EVENT_HALTED:
795 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
797 case TARGET_EVENT_GDB_FLASH_ERASE_START:
798 target_handle_event(target, TARGET_EVENT_OLD_gdb_program_config);
799 if ((retval = jtag_execute_queue()) != ERROR_OK)
811 static int gdb_new_connection(struct connection *connection)
813 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
814 struct gdb_service *gdb_service = connection->service->priv;
818 connection->priv = gdb_connection;
820 /* initialize gdb connection information */
821 gdb_connection->buf_p = gdb_connection->buffer;
822 gdb_connection->buf_cnt = 0;
823 gdb_connection->ctrl_c = 0;
824 gdb_connection->frontend_state = TARGET_HALTED;
825 gdb_connection->vflash_image = NULL;
826 gdb_connection->closed = 0;
827 gdb_connection->busy = 0;
828 gdb_connection->noack_mode = 0;
829 gdb_connection->sync = true;
830 gdb_connection->mem_write_error = false;
832 /* send ACK to GDB for debug request */
833 gdb_write(connection, "+", 1);
835 /* output goes through gdb connection */
836 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
838 /* we must remove all breakpoints registered to the target as a previous
839 * GDB session could leave dangling breakpoints if e.g. communication
842 breakpoint_clear_target(gdb_service->target);
843 watchpoint_clear_target(gdb_service->target);
845 /* remove the initial ACK from the incoming buffer */
846 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
849 /* FIX!!!??? would we actually ever receive a + here???
852 if (initial_ack != '+')
853 gdb_putback_char(connection, initial_ack);
854 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
856 if (gdb_use_memory_map)
858 /* Connect must fail if the memory map can't be set up correctly.
860 * This will cause an auto_probe to be invoked, which is either
861 * a no-op or it will fail when the target isn't ready(e.g. not halted).
864 for (i = 0; i < flash_get_bank_count(); i++)
866 struct flash_bank *p;
867 retval = get_flash_bank_by_num(i, &p);
868 if (retval != ERROR_OK)
870 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
876 gdb_actual_connections++;
877 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
878 gdb_actual_connections,
879 target_name(gdb_service->target),
880 target_state_name(gdb_service->target));
882 /* DANGER! If we fail subsequently, we must remove this handler,
883 * otherwise we occasionally see crashes as the timer can invoke the
886 * register callback to be informed about target events */
887 target_register_event_callback(gdb_target_callback_event_handler, connection);
892 static int gdb_connection_closed(struct connection *connection)
894 struct gdb_service *gdb_service = connection->service->priv;
895 struct gdb_connection *gdb_connection = connection->priv;
897 /* we're done forwarding messages. Tear down callback before
898 * cleaning up connection.
900 log_remove_callback(gdb_log_callback, connection);
902 gdb_actual_connections--;
903 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
904 target_name(gdb_service->target),
905 target_state_name(gdb_service->target),
906 gdb_actual_connections);
908 /* see if an image built with vFlash commands is left */
909 if (gdb_connection->vflash_image)
911 image_close(gdb_connection->vflash_image);
912 free(gdb_connection->vflash_image);
913 gdb_connection->vflash_image = NULL;
916 /* if this connection registered a debug-message receiver delete it */
917 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
919 if (connection->priv)
921 free(connection->priv);
922 connection->priv = NULL;
926 LOG_ERROR("BUG: connection->priv == NULL");
930 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
932 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
934 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
939 static void gdb_send_error(struct connection *connection, uint8_t the_error)
942 snprintf(err, 4, "E%2.2X", the_error);
943 gdb_put_packet(connection, err, 3);
946 static int gdb_last_signal_packet(struct connection *connection,
947 struct target *target, char* packet, int packet_size)
952 signal_var = gdb_last_signal(target);
954 snprintf(sig_reply, 4, "S%2.2x", signal_var);
955 gdb_put_packet(connection, sig_reply, 3);
960 static int gdb_reg_pos(struct target *target, int pos, int len)
962 if (target->endianness == TARGET_LITTLE_ENDIAN)
965 return len - 1 - pos;
968 /* Convert register to string of bytes. NB! The # of bits in the
969 * register might be non-divisible by 8(a byte), in which
970 * case an entire byte is shown.
972 * NB! the format on the wire is the target endianness
974 * The format of reg->value is little endian
977 static void gdb_str_to_target(struct target *target,
978 char *tstr, struct reg *reg)
985 buf_len = DIV_ROUND_UP(reg->size, 8);
987 for (i = 0; i < buf_len; i++)
989 int j = gdb_reg_pos(target, i, buf_len);
990 tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
991 tstr[i*2 + 1] = DIGITS[buf[j]&0xf];
995 static int hextoint(int c)
1006 LOG_ERROR("BUG: invalid register value %08x", c);
1010 /* copy over in register buffer */
1011 static void gdb_target_to_reg(struct target *target,
1012 char *tstr, int str_len, uint8_t *bin)
1016 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1021 for (i = 0; i < str_len; i += 2)
1023 uint8_t t = hextoint(tstr[i]) << 4;
1024 t |= hextoint(tstr[i + 1]);
1026 int j = gdb_reg_pos(target, i/2, str_len/2);
1031 static int gdb_get_registers_packet(struct connection *connection,
1032 struct target *target, char* packet, int packet_size)
1034 struct reg **reg_list;
1037 int reg_packet_size = 0;
1042 #ifdef _DEBUG_GDB_IO_
1046 if ( ( target->rtos != NULL ) &&
1047 ( ERROR_FAIL != rtos_get_gdb_reg_list( connection, target, ®_list, ®_list_size) ) )
1052 if ((retval = target_get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1054 return gdb_error(connection, retval);
1057 for (i = 0; i < reg_list_size; i++)
1059 reg_packet_size += reg_list[i]->size;
1062 reg_packet = malloc(DIV_ROUND_UP(reg_packet_size, 8) * 2);
1063 reg_packet_p = reg_packet;
1065 for (i = 0; i < reg_list_size; i++)
1067 if (!reg_list[i]->valid)
1068 reg_list[i]->type->get(reg_list[i]);
1069 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
1070 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1073 #ifdef _DEBUG_GDB_IO_
1076 reg_packet_p = strndup(reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
1077 LOG_DEBUG("reg_packet: %s", reg_packet_p);
1082 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
1090 static int gdb_set_registers_packet(struct connection *connection,
1091 struct target *target, char *packet, int packet_size)
1094 struct reg **reg_list;
1099 #ifdef _DEBUG_GDB_IO_
1103 /* skip command character */
1107 if (packet_size % 2)
1109 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1110 return ERROR_SERVER_REMOTE_CLOSED;
1113 if ((retval = target_get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1115 return gdb_error(connection, retval);
1119 for (i = 0; i < reg_list_size; i++)
1122 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1124 if (packet_p + chars > packet + packet_size)
1126 LOG_ERROR("BUG: register packet is too small for registers");
1129 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1130 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1132 reg_list[i]->type->set(reg_list[i], bin_buf);
1134 /* advance packet pointer */
1141 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1144 gdb_put_packet(connection, "OK", 2);
1149 static int gdb_get_register_packet(struct connection *connection,
1150 struct target *target, char *packet, int packet_size)
1153 int reg_num = strtoul(packet + 1, NULL, 16);
1154 struct reg **reg_list;
1158 #ifdef _DEBUG_GDB_IO_
1162 if ((retval = target_get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1164 return gdb_error(connection, retval);
1167 if (reg_list_size <= reg_num)
1169 LOG_ERROR("gdb requested a non-existing register");
1173 if (!reg_list[reg_num]->valid)
1174 reg_list[reg_num]->type->get(reg_list[reg_num]);
1176 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1178 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1180 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1188 static int gdb_set_register_packet(struct connection *connection,
1189 struct target *target, char *packet, int packet_size)
1193 int reg_num = strtoul(packet + 1, &separator, 16);
1194 struct reg **reg_list;
1200 if ((retval = target_get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1202 return gdb_error(connection, retval);
1205 if (reg_list_size < reg_num)
1207 LOG_ERROR("gdb requested a non-existing register");
1208 return ERROR_SERVER_REMOTE_CLOSED;
1211 if (*separator != '=')
1213 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1214 return ERROR_SERVER_REMOTE_CLOSED;
1217 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1218 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1219 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1221 /* fix!!! add some sanity checks on packet size here */
1223 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1225 reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1227 gdb_put_packet(connection, "OK", 2);
1235 /* No attempt is made to translate the "retval" to
1236 * GDB speak. This has to be done at the calling
1237 * site as no mapping really exists.
1239 static int gdb_error(struct connection *connection, int retval)
1241 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1242 gdb_send_error(connection, EFAULT);
1246 /* We don't have to worry about the default 2 second timeout for GDB packets,
1247 * because GDB breaks up large memory reads into smaller reads.
1249 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1251 static int gdb_read_memory_packet(struct connection *connection,
1252 struct target *target, char *packet, int packet_size)
1261 int retval = ERROR_OK;
1263 /* skip command character */
1266 addr = strtoul(packet, &separator, 16);
1268 if (*separator != ',')
1270 LOG_ERROR("incomplete read memory packet received, dropping connection");
1271 return ERROR_SERVER_REMOTE_CLOSED;
1274 len = strtoul(separator + 1, NULL, 16);
1276 buffer = malloc(len);
1278 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1280 retval = target_read_buffer(target, addr, len, buffer);
1282 if ((retval != ERROR_OK)&&!gdb_report_data_abort)
1284 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1285 * At some point this might be fixed in GDB, in which case this code can be removed.
1287 * OpenOCD developers are acutely aware of this problem, but there is nothing
1288 * gained by involving the user in this problem that hopefully will get resolved
1291 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1293 * For now, the default is to fix up things to make current GDB versions work.
1294 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1296 memset(buffer, 0, len);
1300 if (retval == ERROR_OK)
1302 hex_buffer = malloc(len * 2 + 1);
1305 for (i = 0; i < len; i++)
1307 uint8_t t = buffer[i];
1308 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1309 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1312 gdb_put_packet(connection, hex_buffer, len * 2);
1318 retval = gdb_error(connection, retval);
1326 static int gdb_write_memory_packet(struct connection *connection,
1327 struct target *target, char *packet, int packet_size)
1338 /* skip command character */
1341 addr = strtoul(packet, &separator, 16);
1343 if (*separator != ',')
1345 LOG_ERROR("incomplete write memory packet received, dropping connection");
1346 return ERROR_SERVER_REMOTE_CLOSED;
1349 len = strtoul(separator + 1, &separator, 16);
1351 if (*(separator++) != ':')
1353 LOG_ERROR("incomplete write memory packet received, dropping connection");
1354 return ERROR_SERVER_REMOTE_CLOSED;
1357 buffer = malloc(len);
1359 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1361 for (i = 0; i < len; i++)
1364 sscanf(separator + 2*i, "%2" SCNx32 , &tmp);
1368 retval = target_write_buffer(target, addr, len, buffer);
1370 if (retval == ERROR_OK)
1372 gdb_put_packet(connection, "OK", 2);
1376 retval = gdb_error(connection, retval);
1384 static int gdb_write_memory_binary_packet(struct connection *connection,
1385 struct target *target, char *packet, int packet_size)
1391 int retval = ERROR_OK;
1393 /* skip command character */
1396 addr = strtoul(packet, &separator, 16);
1398 if (*separator != ',')
1400 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1401 return ERROR_SERVER_REMOTE_CLOSED;
1404 len = strtoul(separator + 1, &separator, 16);
1406 if (*(separator++) != ':')
1408 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1409 return ERROR_SERVER_REMOTE_CLOSED;
1412 struct gdb_connection *gdb_connection = connection->priv;
1414 if (gdb_connection->mem_write_error)
1416 retval = ERROR_FAIL;
1417 /* now that we have reported the memory write error, we can clear the condition */
1418 gdb_connection->mem_write_error = false;
1421 /* By replying the packet *immediately* GDB will send us a new packet
1422 * while we write the last one to the target.
1424 if (retval == ERROR_OK)
1426 gdb_put_packet(connection, "OK", 2);
1430 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1436 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1438 retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
1439 if (retval != ERROR_OK)
1441 gdb_connection->mem_write_error = true;
1448 static int gdb_step_continue_packet(struct connection *connection,
1449 struct target *target, char *packet, int packet_size)
1452 uint32_t address = 0x0;
1453 int retval = ERROR_OK;
1457 if (packet_size > 1)
1459 packet[packet_size] = 0;
1460 address = strtoul(packet + 1, NULL, 16);
1467 if (packet[0] == 'c')
1469 LOG_DEBUG("continue");
1470 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
1471 retval = target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1473 else if (packet[0] == 's')
1476 /* step at current or address, don't handle breakpoints */
1477 retval = target_step(target, current, address, 0);
1482 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1483 struct target *target, char *packet, int packet_size)
1486 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1487 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1495 type = strtoul(packet + 1, &separator, 16);
1497 if (type == 0) /* memory breakpoint */
1498 bp_type = BKPT_SOFT;
1499 else if (type == 1) /* hardware breakpoint */
1500 bp_type = BKPT_HARD;
1501 else if (type == 2) /* write watchpoint */
1502 wp_type = WPT_WRITE;
1503 else if (type == 3) /* read watchpoint */
1505 else if (type == 4) /* access watchpoint */
1506 wp_type = WPT_ACCESS;
1509 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1510 return ERROR_SERVER_REMOTE_CLOSED;
1514 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT)||(bp_type == BKPT_HARD)))
1516 bp_type = gdb_breakpoint_override_type;
1519 if (*separator != ',')
1521 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1522 return ERROR_SERVER_REMOTE_CLOSED;
1525 address = strtoul(separator + 1, &separator, 16);
1527 if (*separator != ',')
1529 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1530 return ERROR_SERVER_REMOTE_CLOSED;
1533 size = strtoul(separator + 1, &separator, 16);
1539 if (packet[0] == 'Z')
1541 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1543 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1548 gdb_put_packet(connection, "OK", 2);
1553 breakpoint_remove(target, address);
1554 gdb_put_packet(connection, "OK", 2);
1561 if (packet[0] == 'Z')
1563 if ((retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu)) != ERROR_OK)
1565 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1570 gdb_put_packet(connection, "OK", 2);
1575 watchpoint_remove(target, address);
1576 gdb_put_packet(connection, "OK", 2);
1587 /* print out a string and allocate more space as needed,
1588 * mainly used for XML at this point
1590 static void xml_printf(int *retval, char **xml, int *pos, int *size,
1591 const char *fmt, ...)
1593 if (*retval != ERROR_OK)
1601 if ((*xml == NULL) || (!first))
1603 /* start by 0 to exercise all the code paths.
1604 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1606 *size = *size * 2 + 2;
1608 *xml = realloc(*xml, *size);
1613 *retval = ERROR_SERVER_REMOTE_CLOSED;
1621 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1623 if ((ret > 0) && ((ret + 1) < *size - *pos))
1628 /* there was just enough or not enough space, allocate more. */
1633 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1637 /* Extract and NUL-terminate the annex. */
1639 while (*buf && *buf != ':')
1645 /* After the read marker and annex, qXfer looks like a
1646 * traditional 'm' packet. */
1648 *ofs = strtoul(buf, &separator, 16);
1650 if (*separator != ',')
1653 *len = strtoul(separator + 1, NULL, 16);
1658 static int compare_bank (const void * a, const void * b)
1660 struct flash_bank *b1, *b2;
1661 b1=*((struct flash_bank **)a);
1662 b2=*((struct flash_bank **)b);
1664 if (b1->base == b2->base)
1667 } else if (b1->base > b2->base)
1676 static int gdb_memory_map(struct connection *connection,
1677 struct target *target, char *packet, int packet_size)
1679 /* We get away with only specifying flash here. Regions that are not
1680 * specified are treated as if we provided no memory map(if not we
1681 * could detect the holes and mark them as RAM).
1682 * Normally we only execute this code once, but no big deal if we
1683 * have to regenerate it a couple of times.
1686 struct flash_bank *p;
1690 int retval = ERROR_OK;
1691 struct flash_bank **banks;
1695 uint32_t ram_start = 0;
1697 int target_flash_banks = 0;
1699 /* skip command character */
1702 offset = strtoul(packet, &separator, 16);
1703 length = strtoul(separator + 1, &separator, 16);
1705 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1707 /* Sort banks in ascending order. We need to report non-flash
1708 * memory as ram (or rather read/write) by default for GDB, since
1709 * it has no concept of non-cacheable read/write memory (i/o etc).
1711 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1712 * Current versions of GDB assume unlisted addresses are RAM...
1714 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1716 for (i = 0; i < flash_get_bank_count(); i++) {
1717 retval = get_flash_bank_by_num(i, &p);
1718 if (retval != ERROR_OK)
1721 gdb_error(connection, retval);
1724 if(p->target == target)
1725 banks[target_flash_banks++] = p;
1728 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1731 for (i = 0; i < flash_get_bank_count(); i++) {
1733 unsigned sector_size = 0;
1739 if (ram_start < p->base)
1740 xml_printf(&retval, &xml, &pos, &size,
1741 "<memory type=\"ram\" start=\"0x%x\" "
1742 "length=\"0x%x\"/>\n",
1743 ram_start, p->base - ram_start);
1745 /* Report adjacent groups of same-size sectors. So for
1746 * example top boot CFI flash will list an initial region
1747 * with several large sectors (maybe 128KB) and several
1748 * smaller ones at the end (maybe 32KB). STR7 will have
1749 * regions with 8KB, 32KB, and 64KB sectors; etc.
1751 for (j = 0; j < p->num_sectors; j++) {
1754 /* Maybe start a new group of sectors. */
1755 if (sector_size == 0) {
1756 start = p->base + p->sectors[j].offset;
1757 xml_printf(&retval, &xml, &pos, &size,
1758 "<memory type=\"flash\" "
1761 sector_size = p->sectors[j].size;
1764 /* Does this finish a group of sectors?
1765 * If not, continue an already-started group.
1767 if (j == p->num_sectors -1)
1768 group_len = (p->base + p->size) - start;
1769 else if (p->sectors[j + 1].size != sector_size)
1770 group_len = p->base + p->sectors[j + 1].offset
1775 xml_printf(&retval, &xml, &pos, &size,
1776 "length=\"0x%x\">\n"
1777 "<property name=\"blocksize\">"
1785 ram_start = p->base + p->size;
1789 xml_printf(&retval, &xml, &pos, &size,
1790 "<memory type=\"ram\" start=\"0x%x\" "
1791 "length=\"0x%x\"/>\n",
1792 ram_start, 0-ram_start);
1793 /* ELSE a flash chip could be at the very end of the 32 bit address
1794 * space, in which case ram_start will be precisely 0
1800 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1802 if (retval != ERROR_OK) {
1803 gdb_error(connection, retval);
1807 if (offset + length > pos)
1808 length = pos - offset;
1810 char *t = malloc(length + 1);
1812 memcpy(t + 1, xml + offset, length);
1813 gdb_put_packet(connection, t, length + 1);
1820 static int gdb_query_packet(struct connection *connection,
1821 struct target *target, char *packet, int packet_size)
1823 struct command_context *cmd_ctx = connection->cmd_ctx;
1824 struct gdb_connection *gdb_connection = connection->priv;
1826 if (strstr(packet, "qRcmd,"))
1828 if (packet_size > 6)
1832 cmd = malloc((packet_size - 6)/2 + 1);
1833 for (i = 0; i < (packet_size - 6)/2; i++)
1836 sscanf(packet + 6 + 2*i, "%2" SCNx32 , &tmp);
1839 cmd[(packet_size - 6)/2] = 0x0;
1841 /* We want to print all debug output to GDB connection */
1842 log_add_callback(gdb_log_callback, connection);
1843 target_call_timer_callbacks_now();
1844 /* some commands need to know the GDB connection, make note of current
1845 * GDB connection. */
1846 current_gdb_connection = gdb_connection;
1847 command_run_line(cmd_ctx, cmd);
1848 current_gdb_connection = NULL;
1849 target_call_timer_callbacks_now();
1850 log_remove_callback(gdb_log_callback, connection);
1853 gdb_put_packet(connection, "OK", 2);
1856 else if (strstr(packet, "qCRC:"))
1858 if (packet_size > 5)
1867 /* skip command character */
1870 addr = strtoul(packet, &separator, 16);
1872 if (*separator != ',')
1874 LOG_ERROR("incomplete read memory packet received, dropping connection");
1875 return ERROR_SERVER_REMOTE_CLOSED;
1878 len = strtoul(separator + 1, NULL, 16);
1880 retval = target_checksum_memory(target, addr, len, &checksum);
1882 if (retval == ERROR_OK)
1884 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
1885 gdb_put_packet(connection, gdb_reply, 9);
1889 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1896 else if (strstr(packet, "qSupported"))
1898 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1899 * disable qXfer:features:read for the moment */
1900 int retval = ERROR_OK;
1901 char *buffer = NULL;
1905 xml_printf(&retval, &buffer, &pos, &size,
1906 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1907 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1909 if (retval != ERROR_OK)
1911 gdb_send_error(connection, 01);
1915 gdb_put_packet(connection, buffer, strlen(buffer));
1920 else if (strstr(packet, "qXfer:memory-map:read::")
1921 && (flash_get_bank_count() > 0))
1922 return gdb_memory_map(connection, target, packet, packet_size);
1923 else if (strstr(packet, "qXfer:features:read:"))
1928 int retval = ERROR_OK;
1931 unsigned int length;
1934 /* skip command character */
1937 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1939 gdb_send_error(connection, 01);
1943 if (strcmp(annex, "target.xml") != 0)
1945 gdb_send_error(connection, 01);
1949 xml_printf(&retval, &xml, &pos, &size, \
1950 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1952 if (retval != ERROR_OK)
1954 gdb_error(connection, retval);
1958 gdb_put_packet(connection, xml, strlen(xml));
1963 else if (strstr(packet, "QStartNoAckMode"))
1965 gdb_connection->noack_mode = 1;
1966 gdb_put_packet(connection, "OK", 2);
1970 gdb_put_packet(connection, "", 0);
1974 static int gdb_v_packet(struct connection *connection,
1975 struct target *target, char *packet, int packet_size)
1977 struct gdb_connection *gdb_connection = connection->priv;
1978 struct gdb_service *gdb_service = connection->service->priv;
1981 /* if flash programming disabled - send a empty reply */
1983 if (gdb_flash_program == 0)
1985 gdb_put_packet(connection, "", 0);
1989 if (strstr(packet, "vFlashErase:"))
1992 unsigned long length;
1994 char *parse = packet + 12;
1997 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1998 return ERROR_SERVER_REMOTE_CLOSED;
2001 addr = strtoul(parse, &parse, 16);
2003 if (*(parse++) != ',' || *parse == '\0')
2005 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2006 return ERROR_SERVER_REMOTE_CLOSED;
2009 length = strtoul(parse, &parse, 16);
2013 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2014 return ERROR_SERVER_REMOTE_CLOSED;
2017 /* assume all sectors need erasing - stops any problems
2018 * when flash_write is called multiple times */
2021 /* perform any target specific operations before the erase */
2022 target_call_event_callbacks(gdb_service->target,
2023 TARGET_EVENT_GDB_FLASH_ERASE_START);
2025 /* vFlashErase:addr,length messages require region start and
2026 * end to be "block" aligned ... if padding is ever needed,
2027 * GDB will have become dangerously confused.
2029 result = flash_erase_address_range(gdb_service->target,
2030 false, addr, length);
2032 /* perform any target specific operations after the erase */
2033 target_call_event_callbacks(gdb_service->target,
2034 TARGET_EVENT_GDB_FLASH_ERASE_END);
2037 if (result != ERROR_OK)
2039 /* GDB doesn't evaluate the actual error number returned,
2040 * treat a failed erase as an I/O error
2042 gdb_send_error(connection, EIO);
2043 LOG_ERROR("flash_erase returned %i", result);
2046 gdb_put_packet(connection, "OK", 2);
2051 if (strstr(packet, "vFlashWrite:"))
2055 unsigned long length;
2056 char *parse = packet + 12;
2060 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2061 return ERROR_SERVER_REMOTE_CLOSED;
2063 addr = strtoul(parse, &parse, 16);
2064 if (*(parse++) != ':')
2066 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2067 return ERROR_SERVER_REMOTE_CLOSED;
2069 length = packet_size - (parse - packet);
2071 /* create a new image if there isn't already one */
2072 if (gdb_connection->vflash_image == NULL)
2074 gdb_connection->vflash_image = malloc(sizeof(struct image));
2075 image_open(gdb_connection->vflash_image, "", "build");
2078 /* create new section with content from packet buffer */
2079 if ((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (uint8_t*)parse)) != ERROR_OK)
2084 gdb_put_packet(connection, "OK", 2);
2089 if (!strcmp(packet, "vFlashDone"))
2093 /* process the flashing buffer. No need to erase as GDB
2094 * always issues a vFlashErase first. */
2095 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
2096 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
2097 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
2098 if (result != ERROR_OK)
2100 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
2101 gdb_put_packet(connection, "E.memtype", 9);
2103 gdb_send_error(connection, EIO);
2107 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
2108 gdb_put_packet(connection, "OK", 2);
2111 image_close(gdb_connection->vflash_image);
2112 free(gdb_connection->vflash_image);
2113 gdb_connection->vflash_image = NULL;
2118 gdb_put_packet(connection, "", 0);
2122 static int gdb_detach(struct connection *connection, struct target *target)
2124 struct gdb_service *gdb_service = connection->service->priv;
2126 target_call_event_callbacks(gdb_service->target,
2127 TARGET_EVENT_GDB_DETACH);
2129 return gdb_put_packet(connection, "OK", 2);
2132 static void gdb_log_callback(void *priv, const char *file, unsigned line,
2133 const char *function, const char *string)
2135 struct connection *connection = priv;
2136 struct gdb_connection *gdb_con = connection->priv;
2140 /* do not reply this using the O packet */
2144 gdb_output_con(connection, string);
2147 static void gdb_sig_halted(struct connection *connection)
2150 snprintf(sig_reply, 4, "T%2.2x", 2);
2151 gdb_put_packet(connection, sig_reply, 3);
2155 static int gdb_input_inner(struct connection *connection)
2157 /* Do not allocate this on the stack */
2158 static char gdb_packet_buffer[GDB_BUFFER_SIZE];
2160 struct gdb_service *gdb_service = connection->service->priv;
2161 struct target *target = gdb_service->target;
2162 char *packet = gdb_packet_buffer;
2165 struct gdb_connection *gdb_con = connection->priv;
2166 static int extended_protocol = 0;
2168 /* drain input buffer. If one of the packets fail, then an error
2169 * packet is replied, if applicable.
2171 * This loop will terminate and the error code is returned.
2173 * The calling fn will check if this error is something that
2174 * can be recovered from, or if the connection must be closed.
2176 * If the error is recoverable, this fn is called again to
2177 * drain the rest of the buffer.
2181 packet_size = GDB_BUFFER_SIZE-1;
2182 retval = gdb_get_packet(connection, packet, &packet_size);
2183 if (retval != ERROR_OK)
2186 /* terminate with zero */
2187 packet[packet_size] = 0;
2189 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
2190 if (packet[0] == 'X') {
2191 // binary packets spew junk into the debug log stream
2194 for (x = 0 ; (x < 49) && (packet[x] != ':') ; x++) {
2198 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
2200 LOG_DEBUG("received packet: '%s'", packet);
2204 if (packet_size > 0)
2209 case 'T': // Is thread alive?
2210 gdb_thread_packet(connection, target, packet, packet_size);
2212 case 'H': // Set current thread ( 'c' for step and continue, 'g' for all other operations )
2213 gdb_thread_packet(connection, target, packet, packet_size);
2217 retval = gdb_thread_packet(connection,
2220 if ( retval == GDB_THREAD_PACKET_NOT_CONSUMED )
2222 retval = gdb_query_packet(connection,
2228 retval = gdb_get_registers_packet(
2230 packet, packet_size);
2233 retval = gdb_set_registers_packet(
2235 packet, packet_size);
2238 retval = gdb_get_register_packet(
2240 packet, packet_size);
2243 retval = gdb_set_register_packet(
2245 packet, packet_size);
2248 retval = gdb_read_memory_packet(
2250 packet, packet_size);
2253 retval = gdb_write_memory_packet(
2255 packet, packet_size);
2259 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
2262 gdb_last_signal_packet(
2264 packet, packet_size);
2269 log_add_callback(gdb_log_callback, connection);
2271 if (gdb_con->mem_write_error)
2273 LOG_ERROR("Memory write failure!");
2275 /* now that we have reported the memory write error, we can clear the condition */
2276 gdb_con->mem_write_error = false;
2279 bool nostep = false;
2280 bool already_running = false;
2281 if (target->state == TARGET_RUNNING)
2283 LOG_WARNING("WARNING! The target is already running. "
2284 "All changes GDB did to registers will be discarded! "
2285 "Waiting for target to halt.");
2286 already_running = true;
2287 } else if (target->state != TARGET_HALTED)
2289 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2291 } else if ((packet[0] == 's') && gdb_con->sync)
2293 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2294 * sent by GDB first to OpenOCD, thus defeating the check to
2295 * make only the single stepping have the sync feature...
2298 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2300 gdb_con->sync = false;
2302 if (!already_running && nostep)
2304 /* Either the target isn't in the halted state, then we can't
2305 * step/continue. This might be early setup, etc.
2307 * Or we want to allow GDB to pick up a fresh set of
2308 * register values without modifying the target state.
2311 gdb_sig_halted(connection);
2313 /* stop forwarding log packets! */
2314 log_remove_callback(gdb_log_callback, connection);
2317 /* We're running/stepping, in which case we can
2318 * forward log output until the target is halted
2320 gdb_con->frontend_state = TARGET_RUNNING;
2321 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2323 if (!already_running)
2325 /* Here we don't want packet processing to stop even if this fails,
2326 * so we use a local variable instead of retval. */
2327 retval = gdb_step_continue_packet(connection, target, packet, packet_size);
2328 if (retval != ERROR_OK)
2330 /* we'll never receive a halted condition... issue a false one.. */
2331 gdb_frontend_halted(target, connection);
2338 retval = gdb_v_packet(
2340 packet, packet_size);
2343 retval = gdb_detach(connection, target);
2344 extended_protocol = 0;
2347 retval = gdb_write_memory_binary_packet(
2349 packet, packet_size);
2350 if (retval != ERROR_OK)
2354 if (extended_protocol != 0)
2356 gdb_put_packet(connection, "OK", 2);
2357 return ERROR_SERVER_REMOTE_CLOSED;
2359 /* handle extended remote protocol */
2360 extended_protocol = 1;
2361 gdb_put_packet(connection, "OK", 2);
2364 /* handle extended restart packet */
2365 breakpoint_clear_target(gdb_service->target);
2366 watchpoint_clear_target(gdb_service->target);
2367 command_run_linef(connection->cmd_ctx,
2368 "ocd_gdb_restart %s",
2369 target_name(target));
2370 gdb_put_packet(connection, "OK", 2);
2374 /* packet supported only by smp target i.e cortex_a.c*/
2375 /* handle smp packet replying coreid played to gbd */
2376 gdb_read_smp_packet(
2378 packet, packet_size);
2382 /* packet supported only by smp target i.e cortex_a.c */
2383 /* handle smp packet setting coreid to be played at next
2385 gdb_write_smp_packet(
2387 packet, packet_size);
2391 /* ignore unknown packets */
2392 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2393 gdb_put_packet(connection, NULL, 0);
2397 /* if a packet handler returned an error, exit input loop */
2398 if (retval != ERROR_OK)
2402 if (gdb_con->ctrl_c)
2404 if (target->state == TARGET_RUNNING)
2406 retval = target_halt(target);
2407 if (retval != ERROR_OK)
2409 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2411 gdb_con->ctrl_c = 0;
2414 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2415 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2419 } while (gdb_con->buf_cnt > 0);
2424 static int gdb_input(struct connection *connection)
2426 int retval = gdb_input_inner(connection);
2427 struct gdb_connection *gdb_con = connection->priv;
2428 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2431 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2432 if (gdb_con->closed)
2433 return ERROR_SERVER_REMOTE_CLOSED;
2435 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2439 static int gdb_target_start(struct target *target, const char *port)
2442 struct gdb_service *gdb_service;
2444 gdb_service = malloc(sizeof(struct gdb_service));
2446 if (NULL == gdb_service)
2449 gdb_service->target = target;
2450 gdb_service->core[0] = -1;
2451 gdb_service->core[1] = -1;
2452 target->gdb_service = gdb_service;
2454 ret = add_service("gdb",
2455 port, 1, &gdb_new_connection, &gdb_input,
2456 &gdb_connection_closed, gdb_service);
2457 /* initialialize all targets gdb service with the same pointer */
2459 struct target_list *head;
2460 struct target *curr;
2461 head = target->head;
2462 while(head != (struct target_list*)NULL)
2464 curr = head->target;
2465 if (curr != target) curr->gdb_service = gdb_service;
2472 static int gdb_target_add_one(struct target *target)
2474 /* one gdb instance per smp list */
2475 if ((target->smp) && (target->gdb_service)) return ERROR_OK;
2476 int retval = gdb_target_start(target, gdb_port_next);
2477 if (retval == ERROR_OK)
2480 /* If we can parse the port number
2481 * then we increment the port number for the next target.
2484 portnumber = strtol(gdb_port_next, &end, 0);
2487 if (parse_long(gdb_port_next, &portnumber) == ERROR_OK)
2489 free((void *)gdb_port_next);
2490 gdb_port_next = alloc_printf("%d", portnumber+1);
2497 int gdb_target_add_all(struct target *target)
2501 LOG_WARNING("gdb services need one or more targets defined");
2505 while (NULL != target)
2507 int retval = gdb_target_add_one(target);
2508 if (ERROR_OK != retval)
2511 target = target->next;
2517 COMMAND_HANDLER(handle_gdb_sync_command)
2521 return ERROR_COMMAND_SYNTAX_ERROR;
2524 if (current_gdb_connection == NULL)
2526 command_print(CMD_CTX,
2527 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2531 current_gdb_connection->sync = true;
2536 /* daemon configuration command gdb_port */
2537 COMMAND_HANDLER(handle_gdb_port_command)
2539 int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
2540 if (ERROR_OK == retval) {
2541 free((void*)gdb_port_next);
2542 gdb_port_next = strdup(gdb_port);
2547 COMMAND_HANDLER(handle_gdb_memory_map_command)
2550 return ERROR_COMMAND_SYNTAX_ERROR;
2552 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
2556 COMMAND_HANDLER(handle_gdb_flash_program_command)
2559 return ERROR_COMMAND_SYNTAX_ERROR;
2561 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
2565 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
2568 return ERROR_COMMAND_SYNTAX_ERROR;
2570 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
2574 /* gdb_breakpoint_override */
2575 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
2580 } else if (CMD_ARGC == 1)
2582 gdb_breakpoint_override = 1;
2583 if (strcmp(CMD_ARGV[0], "hard") == 0)
2585 gdb_breakpoint_override_type = BKPT_HARD;
2586 } else if (strcmp(CMD_ARGV[0], "soft") == 0)
2588 gdb_breakpoint_override_type = BKPT_SOFT;
2589 } else if (strcmp(CMD_ARGV[0], "disable") == 0)
2591 gdb_breakpoint_override = 0;
2595 return ERROR_COMMAND_SYNTAX_ERROR;
2597 if (gdb_breakpoint_override)
2599 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type == BKPT_HARD)?"hard":"soft");
2602 LOG_USER("breakpoint type is not overridden");
2608 static const struct command_registration gdb_command_handlers[] = {
2611 .handler = handle_gdb_sync_command,
2612 .mode = COMMAND_ANY,
2613 .help = "next stepi will return immediately allowing "
2614 "GDB to fetch register state without affecting "
2619 .handler = handle_gdb_port_command,
2620 .mode = COMMAND_ANY,
2621 .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2622 "server listens for the next port number after the "
2623 "base port number specified. "
2624 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2625 "output to stdout, an integer is base port number, \"disable\" disables "
2626 "port. Any other string is are interpreted as named pipe to listen to. "
2627 "Output pipe is the same name as input pipe, but with 'o' appended.",
2628 .usage = "[port_num]",
2631 .name = "gdb_memory_map",
2632 .handler = handle_gdb_memory_map_command,
2633 .mode = COMMAND_CONFIG,
2634 .help = "enable or disable memory map",
2635 .usage = "('enable'|'disable')"
2638 .name = "gdb_flash_program",
2639 .handler = handle_gdb_flash_program_command,
2640 .mode = COMMAND_CONFIG,
2641 .help = "enable or disable flash program",
2642 .usage = "('enable'|'disable')"
2645 .name = "gdb_report_data_abort",
2646 .handler = handle_gdb_report_data_abort_command,
2647 .mode = COMMAND_CONFIG,
2648 .help = "enable or disable reporting data aborts",
2649 .usage = "('enable'|'disable')"
2652 .name = "gdb_breakpoint_override",
2653 .handler = handle_gdb_breakpoint_override_command,
2654 .mode = COMMAND_ANY,
2655 .help = "Display or specify type of breakpoint "
2656 "to be used by gdb 'break' commands.",
2657 .usage = "('hard'|'soft'|'disable')"
2659 COMMAND_REGISTRATION_DONE
2662 int gdb_register_commands(struct command_context *cmd_ctx)
2664 gdb_port = strdup("3333");
2665 gdb_port_next = strdup("3333");
2666 return register_commands(cmd_ctx, NULL, gdb_command_handlers);