1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008 by Spencer Oliver *
11 * spen@spen-soft.co.uk *
13 * Copyright (C) 2011 by Broadcom Corporation *
14 * Evan Hunter - ehunter@broadcom.com *
16 * Copyright (C) ST-Ericsson SA 2011 *
17 * michel.jaouen@stericsson.com : smp minimum support *
19 * Copyright (C) 2013 Andes Technology *
20 * Hsiangkai Wang <hkwang@andestech.com> *
22 * Copyright (C) 2013 Franck Jullien *
23 * elec4fun@gmail.com *
24 ***************************************************************************/
30 #include <target/breakpoints.h>
31 #include <target/target_request.h>
32 #include <target/register.h>
33 #include <target/target.h>
34 #include <target/target_type.h>
35 #include <target/semihosting_common.h>
37 #include <flash/nor/core.h>
38 #include "gdb_server.h"
39 #include <target/image.h>
40 #include <jtag/jtag.h>
41 #include "rtos/rtos.h"
42 #include "target/smp.h"
46 * GDB server implementation.
48 * This implements the GDB Remote Serial Protocol, over TCP connections,
49 * giving GDB access to the JTAG or other hardware debugging facilities
50 * found in most modern embedded processors.
53 enum gdb_output_flag
{
54 /* GDB doesn't accept 'O' packets */
56 /* GDB accepts 'O' packets */
60 struct target_desc_format
{
62 uint32_t tdesc_length
;
65 /* private connection data for GDB */
66 struct gdb_connection
{
67 char buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for null-termination */
71 enum target_state frontend_state
;
72 struct image
*vflash_image
;
76 /* set flag to true if you want the next stepi to return immediately.
77 * allowing GDB to pick up a fresh set of register values from the target
78 * without modifying the target state. */
80 /* We delay reporting memory write errors until next step/continue or memory
81 * write. This improves performance of gdb load significantly as the GDB packet
82 * can be replied immediately and a new GDB packet will be ready without delay
83 * (ca. 10% or so...). */
85 /* with extended-remote it seems we need to better emulate attach/detach.
86 * what this means is we reply with a W stop reply after a kill packet,
87 * normally we reply with a S reply via gdb_last_signal_packet.
88 * as a side note this behaviour only effects gdb > 6.8 */
90 /* set when extended protocol is used */
91 bool extended_protocol
;
92 /* temporarily used for target description support */
93 struct target_desc_format target_desc
;
94 /* temporarily used for thread list support */
96 /* flag to mask the output from gdb_log_callback() */
97 enum gdb_output_flag output_flag
;
101 #define _DEBUG_GDB_IO_
104 static struct gdb_connection
*current_gdb_connection
;
106 static int gdb_breakpoint_override
;
107 static enum breakpoint_type gdb_breakpoint_override_type
;
109 static int gdb_error(struct connection
*connection
, int retval
);
110 static char *gdb_port
;
111 static char *gdb_port_next
;
113 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
114 const char *function
, const char *string
);
116 static void gdb_sig_halted(struct connection
*connection
);
118 /* number of gdb connections, mainly to suppress gdb related debugging spam
119 * in helper/log.c when no gdb connections are actually active */
120 static int gdb_actual_connections
;
122 /* set if we are sending a memory map to gdb
123 * via qXfer:memory-map:read packet */
124 /* enabled by default*/
125 static int gdb_use_memory_map
= 1;
126 /* enabled by default*/
127 static int gdb_flash_program
= 1;
129 /* if set, data aborts cause an error to be reported in memory read packets
130 * see the code in gdb_read_memory_packet() for further explanations.
131 * Disabled by default.
133 static int gdb_report_data_abort
;
134 /* If set, errors when accessing registers are reported to gdb. Disabled by
136 static int gdb_report_register_access_error
;
138 /* set if we are sending target descriptions to gdb
139 * via qXfer:features:read packet */
140 /* enabled by default */
141 static int gdb_use_target_description
= 1;
143 /* current processing free-run type, used by file-I/O */
144 static char gdb_running_type
;
146 static int gdb_last_signal(struct target
*target
)
148 LOG_TARGET_DEBUG(target
, "Debug reason is: %s",
149 target_debug_reason_str(target
->debug_reason
));
151 switch (target
->debug_reason
) {
152 case DBG_REASON_DBGRQ
:
153 return 0x2; /* SIGINT */
154 case DBG_REASON_BREAKPOINT
:
155 case DBG_REASON_WATCHPOINT
:
156 case DBG_REASON_WPTANDBKPT
:
157 return 0x05; /* SIGTRAP */
158 case DBG_REASON_SINGLESTEP
:
159 return 0x05; /* SIGTRAP */
160 case DBG_REASON_EXC_CATCH
:
162 case DBG_REASON_NOTHALTED
:
163 return 0x0; /* no signal... shouldn't happen */
165 LOG_USER("undefined debug reason %d (%s) - target needs reset",
166 target
->debug_reason
,
167 target_debug_reason_str(target
->debug_reason
));
172 static int check_pending(struct connection
*connection
,
173 int timeout_s
, int *got_data
)
175 /* a non-blocking socket will block if there is 0 bytes available on the socket,
176 * but return with as many bytes as are available immediately
180 struct gdb_connection
*gdb_con
= connection
->priv
;
186 if (gdb_con
->buf_cnt
> 0) {
192 FD_SET(connection
->fd
, &read_fds
);
194 tv
.tv_sec
= timeout_s
;
196 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
197 /* This can typically be because a "monitor" command took too long
198 * before printing any progress messages
201 return ERROR_GDB_TIMEOUT
;
205 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
209 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
211 struct gdb_connection
*gdb_con
= connection
->priv
;
212 int retval
= ERROR_OK
;
214 #ifdef _DEBUG_GDB_IO_
218 if (connection
->service
->type
!= CONNECTION_TCP
)
219 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
221 retval
= check_pending(connection
, 1, NULL
);
222 if (retval
!= ERROR_OK
)
224 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
229 if (gdb_con
->buf_cnt
> 0)
231 if (gdb_con
->buf_cnt
== 0) {
232 LOG_DEBUG("GDB connection closed by the remote client");
233 gdb_con
->closed
= true;
234 return ERROR_SERVER_REMOTE_CLOSED
;
238 bool retry
= (WSAGetLastError() == WSAEWOULDBLOCK
);
240 bool retry
= (errno
== EAGAIN
);
244 // Try again after a delay
247 // Print error and close the socket
248 log_socket_error("GDB");
249 gdb_con
->closed
= true;
250 return ERROR_SERVER_REMOTE_CLOSED
;
254 #ifdef _DEBUG_GDB_IO_
255 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
256 LOG_DEBUG("received '%s'", debug_buffer
);
260 gdb_con
->buf_p
= gdb_con
->buffer
;
262 *next_char
= *(gdb_con
->buf_p
++);
263 if (gdb_con
->buf_cnt
> 0)
264 connection
->input_pending
= true;
266 connection
->input_pending
= false;
267 #ifdef _DEBUG_GDB_IO_
268 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
275 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
276 * held in registers in the inner loop.
278 * For small caches and embedded systems this is important!
280 static inline int gdb_get_char_fast(struct connection
*connection
,
281 int *next_char
, char **buf_p
, int *buf_cnt
)
283 int retval
= ERROR_OK
;
285 if ((*buf_cnt
)-- > 0) {
286 *next_char
= **buf_p
;
289 connection
->input_pending
= true;
291 connection
->input_pending
= false;
293 #ifdef _DEBUG_GDB_IO_
294 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
300 struct gdb_connection
*gdb_con
= connection
->priv
;
301 gdb_con
->buf_p
= *buf_p
;
302 gdb_con
->buf_cnt
= *buf_cnt
;
303 retval
= gdb_get_char_inner(connection
, next_char
);
304 *buf_p
= gdb_con
->buf_p
;
305 *buf_cnt
= gdb_con
->buf_cnt
;
310 static int gdb_get_char(struct connection
*connection
, int *next_char
)
312 struct gdb_connection
*gdb_con
= connection
->priv
;
313 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
316 static int gdb_putback_char(struct connection
*connection
, int last_char
)
318 struct gdb_connection
*gdb_con
= connection
->priv
;
320 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
321 *(--gdb_con
->buf_p
) = last_char
;
324 LOG_ERROR("BUG: couldn't put character back");
329 /* The only way we can detect that the socket is closed is the first time
330 * we write to it, we will fail. Subsequent write operations will
331 * succeed. Shudder! */
332 static int gdb_write(struct connection
*connection
, void *data
, int len
)
334 struct gdb_connection
*gdb_con
= connection
->priv
;
335 if (gdb_con
->closed
) {
336 LOG_DEBUG("GDB socket marked as closed, cannot write to it.");
337 return ERROR_SERVER_REMOTE_CLOSED
;
340 if (connection_write(connection
, data
, len
) == len
)
343 LOG_WARNING("Error writing to GDB socket. Dropping the connection.");
344 gdb_con
->closed
= true;
345 return ERROR_SERVER_REMOTE_CLOSED
;
348 static void gdb_log_incoming_packet(struct connection
*connection
, char *packet
)
350 if (!LOG_LEVEL_IS(LOG_LVL_DEBUG
))
353 struct target
*target
= get_target_from_connection(connection
);
355 /* Avoid dumping non-printable characters to the terminal */
356 const unsigned packet_len
= strlen(packet
);
357 const char *nonprint
= find_nonprint_char(packet
, packet_len
);
359 /* Does packet at least have a prefix that is printable?
360 * Look within the first 50 chars of the packet. */
361 const char *colon
= memchr(packet
, ':', MIN(50, packet_len
));
362 const bool packet_has_prefix
= (colon
);
363 const bool packet_prefix_printable
= (packet_has_prefix
&& nonprint
> colon
);
365 if (packet_prefix_printable
) {
366 const unsigned int prefix_len
= colon
- packet
+ 1; /* + 1 to include the ':' */
367 const unsigned int payload_len
= packet_len
- prefix_len
;
368 LOG_TARGET_DEBUG(target
, "received packet: %.*s<binary-data-%u-bytes>", prefix_len
,
369 packet
, payload_len
);
371 LOG_TARGET_DEBUG(target
, "received packet: <binary-data-%u-bytes>", packet_len
);
374 /* All chars printable, dump the packet as is */
375 LOG_TARGET_DEBUG(target
, "received packet: %s", packet
);
379 static void gdb_log_outgoing_packet(struct connection
*connection
, char *packet_buf
,
380 unsigned int packet_len
, unsigned char checksum
)
382 if (!LOG_LEVEL_IS(LOG_LVL_DEBUG
))
385 struct target
*target
= get_target_from_connection(connection
);
387 if (find_nonprint_char(packet_buf
, packet_len
))
388 LOG_TARGET_DEBUG(target
, "sending packet: $<binary-data-%u-bytes>#%2.2x",
389 packet_len
, checksum
);
391 LOG_TARGET_DEBUG(target
, "sending packet: $%.*s#%2.2x", packet_len
, packet_buf
,
395 static int gdb_put_packet_inner(struct connection
*connection
,
396 char *buffer
, int len
)
399 unsigned char my_checksum
= 0;
402 struct gdb_connection
*gdb_con
= connection
->priv
;
404 for (i
= 0; i
< len
; i
++)
405 my_checksum
+= buffer
[i
];
407 #ifdef _DEBUG_GDB_IO_
409 * At this point we should have nothing in the input queue from GDB,
410 * however sometimes '-' is sent even though we've already received
411 * an ACK (+) for everything we've sent off.
415 retval
= check_pending(connection
, 0, &gotdata
);
416 if (retval
!= ERROR_OK
)
420 retval
= gdb_get_char(connection
, &reply
);
421 if (retval
!= ERROR_OK
)
424 /* fix a problem with some IAR tools */
425 gdb_putback_char(connection
, reply
);
426 LOG_DEBUG("Unexpected start of new packet");
430 LOG_WARNING("Discard unexpected char %c", reply
);
435 gdb_log_outgoing_packet(connection
, buffer
, len
, my_checksum
);
437 char local_buffer
[1024];
438 local_buffer
[0] = '$';
439 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
440 /* performance gain on smaller packets by only a single call to gdb_write() */
441 memcpy(local_buffer
+ 1, buffer
, len
++);
442 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
443 retval
= gdb_write(connection
, local_buffer
, len
);
444 if (retval
!= ERROR_OK
)
447 /* larger packets are transmitted directly from caller supplied buffer
448 * by several calls to gdb_write() to avoid dynamic allocation */
449 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
450 retval
= gdb_write(connection
, local_buffer
, 1);
451 if (retval
!= ERROR_OK
)
453 retval
= gdb_write(connection
, buffer
, len
);
454 if (retval
!= ERROR_OK
)
456 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
457 if (retval
!= ERROR_OK
)
461 if (gdb_con
->noack_mode
)
464 retval
= gdb_get_char(connection
, &reply
);
465 if (retval
!= ERROR_OK
)
469 gdb_log_incoming_packet(connection
, "+");
471 } else if (reply
== '-') {
472 /* Stop sending output packets for now */
473 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
474 gdb_log_incoming_packet(connection
, "-");
475 LOG_WARNING("negative reply, retrying");
476 } else if (reply
== 0x3) {
477 gdb_con
->ctrl_c
= true;
478 gdb_log_incoming_packet(connection
, "<Ctrl-C>");
479 retval
= gdb_get_char(connection
, &reply
);
480 if (retval
!= ERROR_OK
)
483 gdb_log_incoming_packet(connection
, "+");
485 } else if (reply
== '-') {
486 /* Stop sending output packets for now */
487 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
488 gdb_log_incoming_packet(connection
, "-");
489 LOG_WARNING("negative reply, retrying");
490 } else if (reply
== '$') {
491 LOG_ERROR("GDB missing ack(1) - assumed good");
492 gdb_putback_char(connection
, reply
);
495 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
496 gdb_con
->closed
= true;
497 return ERROR_SERVER_REMOTE_CLOSED
;
499 } else if (reply
== '$') {
500 LOG_ERROR("GDB missing ack(2) - assumed good");
501 gdb_putback_char(connection
, reply
);
504 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
506 gdb_con
->closed
= true;
507 return ERROR_SERVER_REMOTE_CLOSED
;
511 return ERROR_SERVER_REMOTE_CLOSED
;
516 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
518 struct gdb_connection
*gdb_con
= connection
->priv
;
519 gdb_con
->busy
= true;
520 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
521 gdb_con
->busy
= false;
523 /* we sent some data, reset timer for keep alive messages */
529 static inline int fetch_packet(struct connection
*connection
,
530 int *checksum_ok
, int noack
, int *len
, char *buffer
)
532 unsigned char my_checksum
= 0;
535 int retval
= ERROR_OK
;
537 struct gdb_connection
*gdb_con
= connection
->priv
;
542 /* move this over into local variables to use registers and give the
543 * more freedom to optimize */
544 char *buf_p
= gdb_con
->buf_p
;
545 int buf_cnt
= gdb_con
->buf_cnt
;
548 /* The common case is that we have an entire packet with no escape chars.
549 * We need to leave at least 2 bytes in the buffer to have
550 * gdb_get_char() update various bits and bobs correctly.
552 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
553 /* The compiler will struggle a bit with constant propagation and
554 * aliasing, so we help it by showing that these values do not
555 * change inside the loop
559 int run
= buf_cnt
- 2;
565 if (character
== '#') {
566 /* Danger! character can be '#' when esc is
567 * used so we need an explicit boolean for done here. */
572 if (character
== '}') {
573 /* data transmitted in binary mode (X packet)
574 * uses 0x7d as escape character */
575 my_checksum
+= character
& 0xff;
578 my_checksum
+= character
& 0xff;
579 buffer
[count
++] = (character
^ 0x20) & 0xff;
581 my_checksum
+= character
& 0xff;
582 buffer
[count
++] = character
& 0xff;
591 LOG_ERROR("packet buffer too small");
592 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
596 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
597 if (retval
!= ERROR_OK
)
600 if (character
== '#')
603 if (character
== '}') {
604 /* data transmitted in binary mode (X packet)
605 * uses 0x7d as escape character */
606 my_checksum
+= character
& 0xff;
608 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
609 if (retval
!= ERROR_OK
)
612 my_checksum
+= character
& 0xff;
613 buffer
[count
++] = (character
^ 0x20) & 0xff;
615 my_checksum
+= character
& 0xff;
616 buffer
[count
++] = character
& 0xff;
620 gdb_con
->buf_p
= buf_p
;
621 gdb_con
->buf_cnt
= buf_cnt
;
623 if (retval
!= ERROR_OK
)
628 retval
= gdb_get_char(connection
, &character
);
629 if (retval
!= ERROR_OK
)
631 checksum
[0] = character
;
632 retval
= gdb_get_char(connection
, &character
);
633 if (retval
!= ERROR_OK
)
635 checksum
[1] = character
;
639 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
644 static int gdb_get_packet_inner(struct connection
*connection
,
645 char *buffer
, int *len
)
649 struct gdb_connection
*gdb_con
= connection
->priv
;
653 retval
= gdb_get_char(connection
, &character
);
654 if (retval
!= ERROR_OK
)
657 #ifdef _DEBUG_GDB_IO_
658 LOG_DEBUG("character: '%c'", character
);
665 gdb_log_incoming_packet(connection
, "+");
666 /* According to the GDB documentation
667 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
668 * "gdb sends a final `+` acknowledgment of the stub's `OK`
669 * response, which can be safely ignored by the stub."
670 * However OpenOCD server already is in noack mode at this
671 * point and instead of ignoring this it was emitting a
672 * warning. This code makes server ignore the first ACK
673 * that will be received after going into noack mode,
674 * warning only about subsequent ACK's. */
675 if (gdb_con
->noack_mode
> 1) {
676 LOG_WARNING("acknowledgment received, but no packet pending");
677 } else if (gdb_con
->noack_mode
) {
678 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
679 gdb_con
->noack_mode
= 2;
683 gdb_log_incoming_packet(connection
, "-");
684 LOG_WARNING("negative acknowledgment, but no packet pending");
687 gdb_log_incoming_packet(connection
, "<Ctrl-C>");
688 gdb_con
->ctrl_c
= true;
692 LOG_WARNING("ignoring character 0x%x", character
);
695 } while (character
!= '$');
698 /* explicit code expansion here to get faster inlined code in -O3 by not
699 * calculating checksum */
700 if (gdb_con
->noack_mode
) {
701 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
702 if (retval
!= ERROR_OK
)
705 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
706 if (retval
!= ERROR_OK
)
710 if (gdb_con
->noack_mode
) {
711 /* checksum is not checked in noack mode */
715 retval
= gdb_write(connection
, "+", 1);
716 if (retval
!= ERROR_OK
)
722 return ERROR_SERVER_REMOTE_CLOSED
;
727 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
729 struct gdb_connection
*gdb_con
= connection
->priv
;
730 gdb_con
->busy
= true;
731 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
732 gdb_con
->busy
= false;
736 static int gdb_output_con(struct connection
*connection
, const char *line
)
741 bin_size
= strlen(line
);
743 hex_buffer
= malloc(bin_size
* 2 + 2);
745 return ERROR_GDB_BUFFER_TOO_SMALL
;
748 size_t pkt_len
= hexify(hex_buffer
+ 1, (const uint8_t *)line
, bin_size
,
750 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
756 static int gdb_output(struct command_context
*context
, const char *line
)
758 /* this will be dumped to the log and also sent as an O packet if possible */
759 LOG_USER_N("%s", line
);
763 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
765 struct gdb_connection
*gdb_connection
= connection
->priv
;
767 char stop_reason
[32];
768 char current_thread
[25];
772 rtos_update_threads(target
);
774 if (target
->debug_reason
== DBG_REASON_EXIT
) {
775 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
779 target
->rtos
->current_threadid
= target
->rtos
->current_thread
;
780 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &ct
);
785 if (gdb_connection
->ctrl_c
) {
786 LOG_TARGET_DEBUG(target
, "Responding with signal 2 (SIGINT) to debugger due to Ctrl-C");
789 signal_var
= gdb_last_signal(ct
);
791 stop_reason
[0] = '\0';
792 if (ct
->debug_reason
== DBG_REASON_WATCHPOINT
) {
793 enum watchpoint_rw hit_wp_type
;
794 target_addr_t hit_wp_address
;
796 if (watchpoint_hit(ct
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
798 switch (hit_wp_type
) {
800 snprintf(stop_reason
, sizeof(stop_reason
),
801 "watch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
804 snprintf(stop_reason
, sizeof(stop_reason
),
805 "rwatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
808 snprintf(stop_reason
, sizeof(stop_reason
),
809 "awatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
817 current_thread
[0] = '\0';
819 snprintf(current_thread
, sizeof(current_thread
), "thread:%" PRIx64
";",
820 target
->rtos
->current_thread
);
822 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s%s",
823 signal_var
, stop_reason
, current_thread
);
825 gdb_connection
->ctrl_c
= false;
828 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
829 gdb_connection
->frontend_state
= TARGET_HALTED
;
832 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
834 struct gdb_connection
*gdb_connection
= connection
->priv
;
835 char fileio_command
[256];
837 bool program_exited
= false;
839 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
840 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
841 target
->fileio_info
->param_1
,
842 target
->fileio_info
->param_2
+ 1, /* len + trailing zero */
843 target
->fileio_info
->param_3
,
844 target
->fileio_info
->param_4
);
845 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
846 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
847 target
->fileio_info
->param_1
);
848 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
849 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
850 target
->fileio_info
->param_1
,
851 target
->fileio_info
->param_2
,
852 target
->fileio_info
->param_3
);
853 else if (strcmp(target
->fileio_info
->identifier
, "write") == 0)
854 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
855 target
->fileio_info
->param_1
,
856 target
->fileio_info
->param_2
,
857 target
->fileio_info
->param_3
);
858 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
859 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
860 target
->fileio_info
->param_1
,
861 target
->fileio_info
->param_2
,
862 target
->fileio_info
->param_3
);
863 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
864 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
865 target
->fileio_info
->param_1
,
866 target
->fileio_info
->param_2
+ 1, /* len + trailing zero */
867 target
->fileio_info
->param_3
,
868 target
->fileio_info
->param_4
+ 1); /* len + trailing zero */
869 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
870 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
871 target
->fileio_info
->param_1
,
872 target
->fileio_info
->param_2
+ 1); /* len + trailing zero */
873 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
874 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
875 target
->fileio_info
->param_1
,
876 target
->fileio_info
->param_2
,
877 target
->fileio_info
->param_3
);
878 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
879 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
880 target
->fileio_info
->param_1
,
881 target
->fileio_info
->param_2
);
882 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
883 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
884 target
->fileio_info
->param_1
,
885 target
->fileio_info
->param_2
);
886 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
887 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
888 target
->fileio_info
->param_1
);
889 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
890 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
891 target
->fileio_info
->param_1
,
892 target
->fileio_info
->param_2
+ 1); /* len + trailing zero */
893 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
894 /* If target hits exit syscall, report to GDB the program is terminated.
895 * In addition, let target run its own exit syscall handler. */
896 program_exited
= true;
897 sprintf(fileio_command
, "W%02" PRIx64
, target
->fileio_info
->param_1
);
899 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
901 /* encounter unknown syscall, continue */
902 gdb_connection
->frontend_state
= TARGET_RUNNING
;
903 target_resume(target
, 1, 0x0, 0, 0);
907 command_len
= strlen(fileio_command
);
908 gdb_put_packet(connection
, fileio_command
, command_len
);
910 if (program_exited
) {
911 /* Use target_resume() to let target run its own exit syscall handler. */
912 gdb_connection
->frontend_state
= TARGET_RUNNING
;
913 target_resume(target
, 1, 0x0, 0, 0);
915 gdb_connection
->frontend_state
= TARGET_HALTED
;
916 rtos_update_threads(target
);
920 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
922 struct gdb_connection
*gdb_connection
= connection
->priv
;
924 /* In the GDB protocol when we are stepping or continuing execution,
925 * we have a lingering reply. Upon receiving a halted event
926 * when we have that lingering packet, we reply to the original
927 * step or continue packet.
929 * Executing monitor commands can bring the target in and
930 * out of the running state so we'll see lots of TARGET_EVENT_XXX
931 * that are to be ignored.
933 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
934 /* stop forwarding log packets! */
935 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
937 /* check fileio first */
938 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
939 gdb_fileio_reply(target
, connection
);
941 gdb_signal_reply(target
, connection
);
945 static int gdb_target_callback_event_handler(struct target
*target
,
946 enum target_event event
, void *priv
)
948 struct connection
*connection
= priv
;
949 struct gdb_service
*gdb_service
= connection
->service
->priv
;
951 if (gdb_service
->target
!= target
)
955 case TARGET_EVENT_GDB_HALT
:
956 gdb_frontend_halted(target
, connection
);
958 case TARGET_EVENT_HALTED
:
959 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
968 static int gdb_new_connection(struct connection
*connection
)
970 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
971 struct target
*target
;
975 target
= get_target_from_connection(connection
);
976 connection
->priv
= gdb_connection
;
977 connection
->cmd_ctx
->current_target
= target
;
979 /* initialize gdb connection information */
980 gdb_connection
->buf_p
= gdb_connection
->buffer
;
981 gdb_connection
->buf_cnt
= 0;
982 gdb_connection
->ctrl_c
= false;
983 gdb_connection
->frontend_state
= TARGET_HALTED
;
984 gdb_connection
->vflash_image
= NULL
;
985 gdb_connection
->closed
= false;
986 gdb_connection
->busy
= false;
987 gdb_connection
->noack_mode
= 0;
988 gdb_connection
->sync
= false;
989 gdb_connection
->mem_write_error
= false;
990 gdb_connection
->attached
= true;
991 gdb_connection
->extended_protocol
= false;
992 gdb_connection
->target_desc
.tdesc
= NULL
;
993 gdb_connection
->target_desc
.tdesc_length
= 0;
994 gdb_connection
->thread_list
= NULL
;
995 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
997 /* send ACK to GDB for debug request */
998 gdb_write(connection
, "+", 1);
1000 /* output goes through gdb connection */
1001 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
1003 /* we must remove all breakpoints registered to the target as a previous
1004 * GDB session could leave dangling breakpoints if e.g. communication
1007 breakpoint_clear_target(target
);
1008 watchpoint_clear_target(target
);
1010 /* Since version 3.95 (gdb-19990504), with the exclusion of 6.5~6.8, GDB
1011 * sends an ACK at connection with the following comment in its source code:
1012 * "Ack any packet which the remote side has already sent."
1013 * LLDB does the same since the first gdb-remote implementation.
1014 * Remove the initial ACK from the incoming buffer.
1016 retval
= gdb_get_char(connection
, &initial_ack
);
1017 if (retval
!= ERROR_OK
)
1020 if (initial_ack
!= '+')
1021 gdb_putback_char(connection
, initial_ack
);
1023 target_call_event_callbacks(target
, TARGET_EVENT_GDB_ATTACH
);
1026 /* clean previous rtos session if supported*/
1027 if (target
->rtos
->type
->clean
)
1028 target
->rtos
->type
->clean(target
);
1030 /* update threads */
1031 rtos_update_threads(target
);
1034 if (gdb_use_memory_map
) {
1035 /* Connect must fail if the memory map can't be set up correctly.
1037 * This will cause an auto_probe to be invoked, which is either
1038 * a no-op or it will fail when the target isn't ready(e.g. not halted).
1040 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1041 struct flash_bank
*p
;
1042 p
= get_flash_bank_by_num_noprobe(i
);
1043 if (p
->target
!= target
)
1045 retval
= get_flash_bank_by_num(i
, &p
);
1046 if (retval
!= ERROR_OK
) {
1047 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target "
1048 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1054 gdb_actual_connections
++;
1055 log_printf_lf(all_targets
->next
? LOG_LVL_INFO
: LOG_LVL_DEBUG
,
1056 __FILE__
, __LINE__
, __func__
,
1057 "New GDB Connection: %d, Target %s, state: %s",
1058 gdb_actual_connections
,
1059 target_name(target
),
1060 target_state_name(target
));
1062 if (!target_was_examined(target
)) {
1063 LOG_ERROR("Target %s not examined yet, refuse gdb connection %d!",
1064 target_name(target
), gdb_actual_connections
);
1065 gdb_actual_connections
--;
1066 return ERROR_TARGET_NOT_EXAMINED
;
1069 if (target
->state
!= TARGET_HALTED
)
1070 LOG_WARNING("GDB connection %d on target %s not halted",
1071 gdb_actual_connections
, target_name(target
));
1073 /* DANGER! If we fail subsequently, we must remove this handler,
1074 * otherwise we occasionally see crashes as the timer can invoke the
1077 * register callback to be informed about target events */
1078 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1080 log_add_callback(gdb_log_callback
, connection
);
1085 static int gdb_connection_closed(struct connection
*connection
)
1087 struct target
*target
;
1088 struct gdb_connection
*gdb_connection
= connection
->priv
;
1090 target
= get_target_from_connection(connection
);
1092 /* we're done forwarding messages. Tear down callback before
1093 * cleaning up connection.
1095 log_remove_callback(gdb_log_callback
, connection
);
1097 gdb_actual_connections
--;
1098 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1099 target_name(target
),
1100 target_state_name(target
),
1101 gdb_actual_connections
);
1103 /* see if an image built with vFlash commands is left */
1104 if (gdb_connection
->vflash_image
) {
1105 image_close(gdb_connection
->vflash_image
);
1106 free(gdb_connection
->vflash_image
);
1107 gdb_connection
->vflash_image
= NULL
;
1110 /* if this connection registered a debug-message receiver delete it */
1111 delete_debug_msg_receiver(connection
->cmd_ctx
, target
);
1113 free(connection
->priv
);
1114 connection
->priv
= NULL
;
1116 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1118 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
1120 target_call_event_callbacks(target
, TARGET_EVENT_GDB_DETACH
);
1125 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1128 snprintf(err
, 4, "E%2.2X", the_error
);
1129 gdb_put_packet(connection
, err
, 3);
1132 static int gdb_last_signal_packet(struct connection
*connection
,
1133 char const *packet
, int packet_size
)
1135 struct target
*target
= get_target_from_connection(connection
);
1136 struct gdb_connection
*gdb_con
= connection
->priv
;
1140 if (!gdb_con
->attached
) {
1141 /* if we are here we have received a kill packet
1142 * reply W stop reply otherwise gdb gets very unhappy */
1143 gdb_put_packet(connection
, "W00", 3);
1147 signal_var
= gdb_last_signal(target
);
1149 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1150 gdb_put_packet(connection
, sig_reply
, 3);
1155 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1157 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1160 return len
- 1 - pos
;
1163 /* Convert register to string of bytes. NB! The # of bits in the
1164 * register might be non-divisible by 8(a byte), in which
1165 * case an entire byte is shown.
1167 * NB! the format on the wire is the target endianness
1169 * The format of reg->value is little endian
1172 static void gdb_str_to_target(struct target
*target
,
1173 char *tstr
, struct reg
*reg
)
1180 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1182 for (i
= 0; i
< buf_len
; i
++) {
1183 int j
= gdb_reg_pos(target
, i
, buf_len
);
1184 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1188 /* copy over in register buffer */
1189 static void gdb_target_to_reg(struct target
*target
,
1190 char const *tstr
, int str_len
, uint8_t *bin
)
1193 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1198 for (i
= 0; i
< str_len
; i
+= 2) {
1200 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1201 LOG_ERROR("BUG: unable to convert register value");
1205 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1210 static int gdb_get_registers_packet(struct connection
*connection
,
1211 char const *packet
, int packet_size
)
1213 struct target
*target
= get_target_from_connection(connection
);
1214 struct reg
**reg_list
;
1217 int reg_packet_size
= 0;
1222 #ifdef _DEBUG_GDB_IO_
1226 if ((target
->rtos
) && (rtos_get_gdb_reg_list(connection
) == ERROR_OK
))
1229 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1231 if (retval
!= ERROR_OK
)
1232 return gdb_error(connection
, retval
);
1234 for (i
= 0; i
< reg_list_size
; i
++) {
1235 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1237 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1240 assert(reg_packet_size
> 0);
1242 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1246 reg_packet_p
= reg_packet
;
1248 for (i
= 0; i
< reg_list_size
; i
++) {
1249 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1251 if (!reg_list
[i
]->valid
) {
1252 retval
= reg_list
[i
]->type
->get(reg_list
[i
]);
1253 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1254 LOG_DEBUG("Couldn't get register %s.", reg_list
[i
]->name
);
1257 return gdb_error(connection
, retval
);
1260 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1261 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1264 #ifdef _DEBUG_GDB_IO_
1266 char *reg_packet_p_debug
;
1267 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1268 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1269 free(reg_packet_p_debug
);
1273 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1281 static int gdb_set_registers_packet(struct connection
*connection
,
1282 char const *packet
, int packet_size
)
1284 struct target
*target
= get_target_from_connection(connection
);
1286 struct reg
**reg_list
;
1289 char const *packet_p
;
1291 #ifdef _DEBUG_GDB_IO_
1295 /* skip command character */
1299 if (packet_size
% 2) {
1300 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1301 return ERROR_SERVER_REMOTE_CLOSED
;
1304 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1306 if (retval
!= ERROR_OK
)
1307 return gdb_error(connection
, retval
);
1310 for (i
= 0; i
< reg_list_size
; i
++) {
1312 if (!reg_list
[i
] || !reg_list
[i
]->exist
|| reg_list
[i
]->hidden
)
1314 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1316 if (packet_p
+ chars
> packet
+ packet_size
)
1317 LOG_ERROR("BUG: register packet is too small for registers");
1319 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1320 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1322 retval
= reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1323 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1324 LOG_DEBUG("Couldn't set register %s.", reg_list
[i
]->name
);
1327 return gdb_error(connection
, retval
);
1330 /* advance packet pointer */
1336 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1339 gdb_put_packet(connection
, "OK", 2);
1344 static int gdb_get_register_packet(struct connection
*connection
,
1345 char const *packet
, int packet_size
)
1347 struct target
*target
= get_target_from_connection(connection
);
1349 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1350 struct reg
**reg_list
;
1354 #ifdef _DEBUG_GDB_IO_
1358 if ((target
->rtos
) && (rtos_get_gdb_reg(connection
, reg_num
) == ERROR_OK
))
1361 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1363 if (retval
!= ERROR_OK
)
1364 return gdb_error(connection
, retval
);
1366 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1367 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1368 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1369 return ERROR_SERVER_REMOTE_CLOSED
;
1372 if (!reg_list
[reg_num
]->valid
) {
1373 retval
= reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1374 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1375 LOG_DEBUG("Couldn't get register %s.", reg_list
[reg_num
]->name
);
1377 return gdb_error(connection
, retval
);
1381 reg_packet
= calloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1, 1); /* plus one for string termination null */
1383 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1385 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1393 static int gdb_set_register_packet(struct connection
*connection
,
1394 char const *packet
, int packet_size
)
1396 struct target
*target
= get_target_from_connection(connection
);
1398 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1399 struct reg
**reg_list
;
1403 #ifdef _DEBUG_GDB_IO_
1407 if (*separator
!= '=') {
1408 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1409 return ERROR_SERVER_REMOTE_CLOSED
;
1411 size_t chars
= strlen(separator
+ 1);
1412 uint8_t *bin_buf
= malloc(chars
/ 2);
1413 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1415 if ((target
->rtos
) &&
1416 (rtos_set_reg(connection
, reg_num
, bin_buf
) == ERROR_OK
)) {
1418 gdb_put_packet(connection
, "OK", 2);
1422 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1424 if (retval
!= ERROR_OK
) {
1426 return gdb_error(connection
, retval
);
1429 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1430 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1431 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1434 return ERROR_SERVER_REMOTE_CLOSED
;
1437 if (chars
!= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2)) {
1438 LOG_ERROR("gdb sent %zu bits for a %" PRIu32
"-bit register (%s)",
1439 chars
* 4, reg_list
[reg_num
]->size
, reg_list
[reg_num
]->name
);
1442 return ERROR_SERVER_REMOTE_CLOSED
;
1445 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1447 retval
= reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1448 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1449 LOG_DEBUG("Couldn't set register %s.", reg_list
[reg_num
]->name
);
1452 return gdb_error(connection
, retval
);
1455 gdb_put_packet(connection
, "OK", 2);
1463 /* No attempt is made to translate the "retval" to
1464 * GDB speak. This has to be done at the calling
1465 * site as no mapping really exists.
1467 static int gdb_error(struct connection
*connection
, int retval
)
1469 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1470 gdb_send_error(connection
, EFAULT
);
1474 /* We don't have to worry about the default 2 second timeout for GDB packets,
1475 * because GDB breaks up large memory reads into smaller reads.
1477 static int gdb_read_memory_packet(struct connection
*connection
,
1478 char const *packet
, int packet_size
)
1480 struct target
*target
= get_target_from_connection(connection
);
1488 int retval
= ERROR_OK
;
1490 /* skip command character */
1493 addr
= strtoull(packet
, &separator
, 16);
1495 if (*separator
!= ',') {
1496 LOG_ERROR("incomplete read memory packet received, dropping connection");
1497 return ERROR_SERVER_REMOTE_CLOSED
;
1500 len
= strtoul(separator
+ 1, NULL
, 16);
1503 LOG_WARNING("invalid read memory packet received (len == 0)");
1504 gdb_put_packet(connection
, "", 0);
1508 buffer
= malloc(len
);
1510 LOG_DEBUG("addr: 0x%16.16" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1512 retval
= ERROR_NOT_IMPLEMENTED
;
1514 retval
= rtos_read_buffer(target
, addr
, len
, buffer
);
1515 if (retval
== ERROR_NOT_IMPLEMENTED
)
1516 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1518 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1519 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1520 * At some point this might be fixed in GDB, in which case this code can be removed.
1522 * OpenOCD developers are acutely aware of this problem, but there is nothing
1523 * gained by involving the user in this problem that hopefully will get resolved
1526 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1527 * cmd = view%20audit-trail&database = gdb&pr = 2395
1529 * For now, the default is to fix up things to make current GDB versions work.
1530 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1532 memset(buffer
, 0, len
);
1536 if (retval
== ERROR_OK
) {
1537 hex_buffer
= malloc(len
* 2 + 1);
1539 size_t pkt_len
= hexify(hex_buffer
, buffer
, len
, len
* 2 + 1);
1541 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1545 retval
= gdb_error(connection
, retval
);
1552 static int gdb_write_memory_packet(struct connection
*connection
,
1553 char const *packet
, int packet_size
)
1555 struct target
*target
= get_target_from_connection(connection
);
1563 /* skip command character */
1566 addr
= strtoull(packet
, &separator
, 16);
1568 if (*separator
!= ',') {
1569 LOG_ERROR("incomplete write memory packet received, dropping connection");
1570 return ERROR_SERVER_REMOTE_CLOSED
;
1573 len
= strtoul(separator
+ 1, &separator
, 16);
1575 if (*(separator
++) != ':') {
1576 LOG_ERROR("incomplete write memory packet received, dropping connection");
1577 return ERROR_SERVER_REMOTE_CLOSED
;
1580 buffer
= malloc(len
);
1582 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1584 if (unhexify(buffer
, separator
, len
) != len
)
1585 LOG_ERROR("unable to decode memory packet");
1587 retval
= ERROR_NOT_IMPLEMENTED
;
1589 retval
= rtos_write_buffer(target
, addr
, len
, buffer
);
1590 if (retval
== ERROR_NOT_IMPLEMENTED
)
1591 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1593 if (retval
== ERROR_OK
)
1594 gdb_put_packet(connection
, "OK", 2);
1596 retval
= gdb_error(connection
, retval
);
1603 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1604 char const *packet
, int packet_size
)
1606 struct target
*target
= get_target_from_connection(connection
);
1611 int retval
= ERROR_OK
;
1612 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1613 * the assumption that we're in a download and it's important to go as fast
1615 uint32_t fast_limit
= 8;
1617 /* skip command character */
1620 addr
= strtoull(packet
, &separator
, 16);
1622 if (*separator
!= ',') {
1623 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1624 return ERROR_SERVER_REMOTE_CLOSED
;
1627 len
= strtoul(separator
+ 1, &separator
, 16);
1629 if (*(separator
++) != ':') {
1630 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1631 return ERROR_SERVER_REMOTE_CLOSED
;
1634 struct gdb_connection
*gdb_connection
= connection
->priv
;
1636 if (gdb_connection
->mem_write_error
)
1637 retval
= ERROR_FAIL
;
1639 if (retval
== ERROR_OK
) {
1640 if (len
>= fast_limit
) {
1641 /* By replying the packet *immediately* GDB will send us a new packet
1642 * while we write the last one to the target.
1643 * We only do this for larger writes, so that users who do something like:
1644 * p *((int*)0xdeadbeef)=8675309
1645 * will get immediate feedback that that write failed.
1647 gdb_put_packet(connection
, "OK", 2);
1650 retval
= gdb_error(connection
, retval
);
1651 /* now that we have reported the memory write error, we can clear the condition */
1652 gdb_connection
->mem_write_error
= false;
1653 if (retval
!= ERROR_OK
)
1658 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1660 retval
= ERROR_NOT_IMPLEMENTED
;
1662 retval
= rtos_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1663 if (retval
== ERROR_NOT_IMPLEMENTED
)
1664 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1666 if (retval
!= ERROR_OK
)
1667 gdb_connection
->mem_write_error
= true;
1670 if (len
< fast_limit
) {
1671 if (retval
!= ERROR_OK
) {
1672 gdb_error(connection
, retval
);
1673 gdb_connection
->mem_write_error
= false;
1675 gdb_put_packet(connection
, "OK", 2);
1682 static int gdb_step_continue_packet(struct connection
*connection
,
1683 char const *packet
, int packet_size
)
1685 struct target
*target
= get_target_from_connection(connection
);
1687 uint64_t address
= 0x0;
1688 int retval
= ERROR_OK
;
1692 if (packet_size
> 1)
1693 address
= strtoull(packet
+ 1, NULL
, 16);
1697 gdb_running_type
= packet
[0];
1698 if (packet
[0] == 'c') {
1699 LOG_DEBUG("continue");
1700 /* resume at current address, don't handle breakpoints, not debugging */
1701 retval
= target_resume(target
, current
, address
, 0, 0);
1702 } else if (packet
[0] == 's') {
1704 /* step at current or address, don't handle breakpoints */
1705 retval
= target_step(target
, current
, address
, 0);
1710 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1711 char const *packet
, int packet_size
)
1713 struct target
*target
= get_target_from_connection(connection
);
1715 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1716 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1722 LOG_DEBUG("[%s]", target_name(target
));
1724 type
= strtoul(packet
+ 1, &separator
, 16);
1726 if (type
== 0) /* memory breakpoint */
1727 bp_type
= BKPT_SOFT
;
1728 else if (type
== 1) /* hardware breakpoint */
1729 bp_type
= BKPT_HARD
;
1730 else if (type
== 2) /* write watchpoint */
1731 wp_type
= WPT_WRITE
;
1732 else if (type
== 3) /* read watchpoint */
1734 else if (type
== 4) /* access watchpoint */
1735 wp_type
= WPT_ACCESS
;
1737 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1738 return ERROR_SERVER_REMOTE_CLOSED
;
1741 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1742 bp_type
= gdb_breakpoint_override_type
;
1744 if (*separator
!= ',') {
1745 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1746 return ERROR_SERVER_REMOTE_CLOSED
;
1749 address
= strtoull(separator
+ 1, &separator
, 16);
1751 if (*separator
!= ',') {
1752 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1753 return ERROR_SERVER_REMOTE_CLOSED
;
1756 size
= strtoul(separator
+ 1, &separator
, 16);
1761 if (packet
[0] == 'Z') {
1762 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1763 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1764 /* Send empty reply to report that breakpoints of this type are not supported */
1765 gdb_put_packet(connection
, "", 0);
1766 } else if (retval
!= ERROR_OK
) {
1767 retval
= gdb_error(connection
, retval
);
1768 if (retval
!= ERROR_OK
)
1771 gdb_put_packet(connection
, "OK", 2);
1773 breakpoint_remove(target
, address
);
1774 gdb_put_packet(connection
, "OK", 2);
1781 if (packet
[0] == 'Z') {
1782 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, WATCHPOINT_IGNORE_DATA_VALUE_MASK
);
1783 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1784 /* Send empty reply to report that watchpoints of this type are not supported */
1785 gdb_put_packet(connection
, "", 0);
1786 } else if (retval
!= ERROR_OK
) {
1787 retval
= gdb_error(connection
, retval
);
1788 if (retval
!= ERROR_OK
)
1791 gdb_put_packet(connection
, "OK", 2);
1793 watchpoint_remove(target
, address
);
1794 gdb_put_packet(connection
, "OK", 2);
1805 /* print out a string and allocate more space as needed,
1806 * mainly used for XML at this point
1808 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 5, 6))) void xml_printf(int *retval
,
1809 char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1811 if (*retval
!= ERROR_OK
)
1816 if ((!*xml
) || (!first
)) {
1817 /* start by 0 to exercise all the code paths.
1818 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1820 *size
= *size
* 2 + 2;
1822 *xml
= realloc(*xml
, *size
);
1825 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1833 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1835 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1839 /* there was just enough or not enough space, allocate more. */
1844 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1846 /* Locate the annex. */
1847 const char *annex_end
= strchr(buf
, ':');
1851 /* After the read marker and annex, qXfer looks like a
1852 * traditional 'm' packet. */
1854 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1856 if (*separator
!= ',')
1859 *len
= strtoul(separator
+ 1, NULL
, 16);
1861 /* Extract the annex if needed */
1863 *annex
= strndup(buf
, annex_end
- buf
);
1871 static int compare_bank(const void *a
, const void *b
)
1873 struct flash_bank
*b1
, *b2
;
1874 b1
= *((struct flash_bank
**)a
);
1875 b2
= *((struct flash_bank
**)b
);
1877 if (b1
->base
== b2
->base
)
1879 else if (b1
->base
> b2
->base
)
1885 static int gdb_memory_map(struct connection
*connection
,
1886 char const *packet
, int packet_size
)
1888 /* We get away with only specifying flash here. Regions that are not
1889 * specified are treated as if we provided no memory map(if not we
1890 * could detect the holes and mark them as RAM).
1891 * Normally we only execute this code once, but no big deal if we
1892 * have to regenerate it a couple of times.
1895 struct target
*target
= get_target_from_connection(connection
);
1896 struct flash_bank
*p
;
1900 int retval
= ERROR_OK
;
1901 struct flash_bank
**banks
;
1905 target_addr_t ram_start
= 0;
1906 unsigned int target_flash_banks
= 0;
1908 /* skip command character */
1911 offset
= strtoul(packet
, &separator
, 16);
1912 length
= strtoul(separator
+ 1, &separator
, 16);
1914 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1916 /* Sort banks in ascending order. We need to report non-flash
1917 * memory as ram (or rather read/write) by default for GDB, since
1918 * it has no concept of non-cacheable read/write memory (i/o etc).
1920 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1922 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1923 p
= get_flash_bank_by_num_noprobe(i
);
1924 if (p
->target
!= target
)
1926 retval
= get_flash_bank_by_num(i
, &p
);
1927 if (retval
!= ERROR_OK
) {
1929 gdb_error(connection
, retval
);
1932 banks
[target_flash_banks
++] = p
;
1935 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1938 for (unsigned int i
= 0; i
< target_flash_banks
; i
++) {
1939 unsigned sector_size
= 0;
1940 unsigned group_len
= 0;
1944 if (ram_start
< p
->base
)
1945 xml_printf(&retval
, &xml
, &pos
, &size
,
1946 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1947 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
1948 ram_start
, p
->base
- ram_start
);
1950 /* Report adjacent groups of same-size sectors. So for
1951 * example top boot CFI flash will list an initial region
1952 * with several large sectors (maybe 128KB) and several
1953 * smaller ones at the end (maybe 32KB). STR7 will have
1954 * regions with 8KB, 32KB, and 64KB sectors; etc.
1956 for (unsigned int j
= 0; j
< p
->num_sectors
; j
++) {
1958 /* Maybe start a new group of sectors. */
1959 if (sector_size
== 0) {
1960 if (p
->sectors
[j
].offset
+ p
->sectors
[j
].size
> p
->size
) {
1961 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1962 " overflows the end of %s bank.",
1963 p
->sectors
[j
].offset
, p
->name
);
1964 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1967 target_addr_t start
;
1968 start
= p
->base
+ p
->sectors
[j
].offset
;
1969 xml_printf(&retval
, &xml
, &pos
, &size
,
1970 "<memory type=\"flash\" "
1971 "start=\"" TARGET_ADDR_FMT
"\" ",
1973 sector_size
= p
->sectors
[j
].size
;
1974 group_len
= sector_size
;
1976 group_len
+= sector_size
; /* equal to p->sectors[j].size */
1979 /* Does this finish a group of sectors?
1980 * If not, continue an already-started group.
1982 if (j
< p
->num_sectors
- 1
1983 && p
->sectors
[j
+ 1].size
== sector_size
1984 && p
->sectors
[j
+ 1].offset
== p
->sectors
[j
].offset
+ sector_size
1985 && p
->sectors
[j
+ 1].offset
+ p
->sectors
[j
+ 1].size
<= p
->size
)
1988 xml_printf(&retval
, &xml
, &pos
, &size
,
1989 "length=\"0x%x\">\n"
1990 "<property name=\"blocksize\">"
1998 ram_start
= p
->base
+ p
->size
;
2002 xml_printf(&retval
, &xml
, &pos
, &size
,
2003 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
2004 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
2005 ram_start
, target_address_max(target
) - ram_start
+ 1);
2006 /* ELSE a flash chip could be at the very end of the address space, in
2007 * which case ram_start will be precisely 0 */
2011 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
2013 if (retval
!= ERROR_OK
) {
2015 gdb_error(connection
, retval
);
2019 if (offset
+ length
> pos
)
2020 length
= pos
- offset
;
2022 char *t
= malloc(length
+ 1);
2024 memcpy(t
+ 1, xml
+ offset
, length
);
2025 gdb_put_packet(connection
, t
, length
+ 1);
2032 static const char *gdb_get_reg_type_name(enum reg_type type
)
2041 case REG_TYPE_INT16
:
2043 case REG_TYPE_INT32
:
2045 case REG_TYPE_INT64
:
2047 case REG_TYPE_INT128
:
2051 case REG_TYPE_UINT8
:
2053 case REG_TYPE_UINT16
:
2055 case REG_TYPE_UINT32
:
2057 case REG_TYPE_UINT64
:
2059 case REG_TYPE_UINT128
:
2061 case REG_TYPE_CODE_PTR
:
2063 case REG_TYPE_DATA_PTR
:
2065 case REG_TYPE_FLOAT
:
2067 case REG_TYPE_IEEE_SINGLE
:
2068 return "ieee_single";
2069 case REG_TYPE_IEEE_DOUBLE
:
2070 return "ieee_double";
2071 case REG_TYPE_ARCH_DEFINED
:
2072 return "int"; /* return arbitrary string to avoid compile warning. */
2075 return "int"; /* "int" as default value */
2078 static int lookup_add_arch_defined_types(char const **arch_defined_types_list
[], const char *type_id
,
2079 int *num_arch_defined_types
)
2081 int tbl_sz
= *num_arch_defined_types
;
2083 if (type_id
&& (strcmp(type_id
, ""))) {
2084 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2085 if (!((*arch_defined_types_list
)[j
])) {
2086 (*arch_defined_types_list
)[tbl_sz
++] = type_id
;
2087 *arch_defined_types_list
= realloc(*arch_defined_types_list
,
2088 sizeof(char *) * (tbl_sz
+ 1));
2089 (*arch_defined_types_list
)[tbl_sz
] = NULL
;
2090 *num_arch_defined_types
= tbl_sz
;
2093 if (!strcmp((*arch_defined_types_list
)[j
], type_id
))
2102 static int gdb_generate_reg_type_description(struct target
*target
,
2103 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
,
2104 char const **arch_defined_types_list
[], int *num_arch_defined_types
)
2106 int retval
= ERROR_OK
;
2108 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
2109 struct reg_data_type
*data_type
= type
->reg_type_vector
->type
;
2110 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2111 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2112 num_arch_defined_types
))
2113 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2114 arch_defined_types_list
,
2115 num_arch_defined_types
);
2117 /* <vector id="id" type="type" count="count"/> */
2118 xml_printf(&retval
, tdesc
, pos
, size
,
2119 "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32
"\"/>\n",
2120 type
->id
, type
->reg_type_vector
->type
->id
,
2121 type
->reg_type_vector
->count
);
2123 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
2124 struct reg_data_type_union_field
*field
;
2125 field
= type
->reg_type_union
->fields
;
2127 struct reg_data_type
*data_type
= field
->type
;
2128 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2129 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2130 num_arch_defined_types
))
2131 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2132 arch_defined_types_list
,
2133 num_arch_defined_types
);
2136 field
= field
->next
;
2139 * <field name="name" type="type"/> ...
2141 xml_printf(&retval
, tdesc
, pos
, size
,
2142 "<union id=\"%s\">\n",
2145 field
= type
->reg_type_union
->fields
;
2147 xml_printf(&retval
, tdesc
, pos
, size
,
2148 "<field name=\"%s\" type=\"%s\"/>\n",
2149 field
->name
, field
->type
->id
);
2151 field
= field
->next
;
2154 xml_printf(&retval
, tdesc
, pos
, size
,
2157 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
2158 struct reg_data_type_struct_field
*field
;
2159 field
= type
->reg_type_struct
->fields
;
2161 if (field
->use_bitfields
) {
2162 /* <struct id="id" size="size">
2163 * <field name="name" start="start" end="end"/> ...
2165 xml_printf(&retval
, tdesc
, pos
, size
,
2166 "<struct id=\"%s\" size=\"%" PRIu32
"\">\n",
2167 type
->id
, type
->reg_type_struct
->size
);
2169 xml_printf(&retval
, tdesc
, pos
, size
,
2170 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2171 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2172 gdb_get_reg_type_name(field
->bitfield
->type
));
2174 field
= field
->next
;
2178 struct reg_data_type
*data_type
= field
->type
;
2179 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2180 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2181 num_arch_defined_types
))
2182 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2183 arch_defined_types_list
,
2184 num_arch_defined_types
);
2189 * <field name="name" type="type"/> ...
2191 xml_printf(&retval
, tdesc
, pos
, size
,
2192 "<struct id=\"%s\">\n",
2195 xml_printf(&retval
, tdesc
, pos
, size
,
2196 "<field name=\"%s\" type=\"%s\"/>\n",
2197 field
->name
, field
->type
->id
);
2199 field
= field
->next
;
2203 xml_printf(&retval
, tdesc
, pos
, size
,
2206 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2207 /* <flags id="id" size="size">
2208 * <field name="name" start="start" end="end"/> ...
2210 xml_printf(&retval
, tdesc
, pos
, size
,
2211 "<flags id=\"%s\" size=\"%" PRIu32
"\">\n",
2212 type
->id
, type
->reg_type_flags
->size
);
2214 struct reg_data_type_flags_field
*field
;
2215 field
= type
->reg_type_flags
->fields
;
2217 xml_printf(&retval
, tdesc
, pos
, size
,
2218 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2219 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2220 gdb_get_reg_type_name(field
->bitfield
->type
));
2222 field
= field
->next
;
2225 xml_printf(&retval
, tdesc
, pos
, size
,
2233 /* Get a list of available target registers features. feature_list must
2234 * be freed by caller.
2236 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2237 struct reg
**reg_list
, int reg_list_size
)
2241 /* Start with only one element */
2242 *feature_list
= calloc(1, sizeof(char *));
2244 for (int i
= 0; i
< reg_list_size
; i
++) {
2245 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2248 if (reg_list
[i
]->feature
2249 && reg_list
[i
]->feature
->name
2250 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2251 /* We found a feature, check if the feature is already in the
2252 * table. If not, allocate a new entry for the table and
2253 * put the new feature in it.
2255 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2256 if (!((*feature_list
)[j
])) {
2257 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2258 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2259 (*feature_list
)[tbl_sz
] = NULL
;
2262 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2269 if (feature_list_size
)
2270 *feature_list_size
= tbl_sz
;
2275 /* Create a register list that's the union of all the registers of the SMP
2276 * group this target is in. If the target is not part of an SMP group, this
2277 * returns the same as target_get_gdb_reg_list_noread().
2279 static int smp_reg_list_noread(struct target
*target
,
2280 struct reg
**combined_list
[], int *combined_list_size
,
2281 enum target_register_class reg_class
)
2284 return target_get_gdb_reg_list_noread(target
, combined_list
,
2285 combined_list_size
, REG_CLASS_ALL
);
2287 unsigned int combined_allocated
= 256;
2288 struct reg
**local_list
= malloc(combined_allocated
* sizeof(struct reg
*));
2290 LOG_ERROR("malloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2293 unsigned int local_list_size
= 0;
2295 struct target_list
*head
;
2296 foreach_smp_target(head
, target
->smp_targets
) {
2297 if (!target_was_examined(head
->target
))
2300 struct reg
**reg_list
= NULL
;
2302 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2303 ®_list_size
, reg_class
);
2304 if (result
!= ERROR_OK
) {
2308 for (int i
= 0; i
< reg_list_size
; i
++) {
2310 struct reg
*a
= reg_list
[i
];
2312 /* Nested loop makes this O(n^2), but this entire function with
2313 * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2315 for (unsigned int j
= 0; j
< local_list_size
; j
++) {
2316 struct reg
*b
= local_list
[j
];
2317 if (!strcmp(a
->name
, b
->name
)) {
2319 if (a
->size
!= b
->size
) {
2320 LOG_ERROR("SMP register %s is %d bits on one "
2321 "target, but %d bits on another target.",
2322 a
->name
, a
->size
, b
->size
);
2331 LOG_DEBUG("[%s] %s not found in combined list", target_name(target
), a
->name
);
2332 if (local_list_size
>= combined_allocated
) {
2333 combined_allocated
*= 2;
2334 local_list
= realloc(local_list
, combined_allocated
* sizeof(struct reg
*));
2336 LOG_ERROR("realloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2341 local_list
[local_list_size
] = a
;
2349 if (local_list_size
== 0) {
2350 LOG_ERROR("Unable to get register list");
2355 /* Now warn the user about any registers that weren't found in every target. */
2356 foreach_smp_target(head
, target
->smp_targets
) {
2357 if (!target_was_examined(head
->target
))
2360 struct reg
**reg_list
= NULL
;
2362 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2363 ®_list_size
, reg_class
);
2364 if (result
!= ERROR_OK
) {
2368 for (unsigned int i
= 0; i
< local_list_size
; i
++) {
2370 struct reg
*a
= local_list
[i
];
2371 for (int j
= 0; j
< reg_list_size
; j
++) {
2372 struct reg
*b
= reg_list
[j
];
2373 if (b
->exist
&& !strcmp(a
->name
, b
->name
)) {
2379 LOG_WARNING("Register %s does not exist in %s, which is part of an SMP group where "
2380 "this register does exist.",
2381 a
->name
, target_name(head
->target
));
2387 *combined_list
= local_list
;
2388 *combined_list_size
= local_list_size
;
2392 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2394 int retval
= ERROR_OK
;
2395 struct reg
**reg_list
= NULL
;
2397 char const *architecture
;
2398 char const **features
= NULL
;
2399 int feature_list_size
= 0;
2405 retval
= smp_reg_list_noread(target
, ®_list
, ®_list_size
,
2408 if (retval
!= ERROR_OK
) {
2409 LOG_ERROR("get register list failed");
2410 retval
= ERROR_FAIL
;
2414 if (reg_list_size
<= 0) {
2415 LOG_ERROR("get register list failed");
2416 retval
= ERROR_FAIL
;
2420 /* Get a list of available target registers features */
2421 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2422 if (retval
!= ERROR_OK
) {
2423 LOG_ERROR("Can't get the registers feature list");
2424 retval
= ERROR_FAIL
;
2428 /* If we found some features associated with registers, create sections */
2429 int current_feature
= 0;
2431 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2432 "<?xml version=\"1.0\"?>\n"
2433 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2434 "<target version=\"1.0\">\n");
2436 /* generate architecture element if supported by target */
2437 architecture
= target_get_gdb_arch(target
);
2439 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2440 "<architecture>%s</architecture>\n", architecture
);
2442 /* generate target description according to register list */
2444 while (features
[current_feature
]) {
2445 char const **arch_defined_types
= NULL
;
2446 int num_arch_defined_types
= 0;
2448 arch_defined_types
= calloc(1, sizeof(char *));
2449 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2450 "<feature name=\"%s\">\n",
2451 features
[current_feature
]);
2454 for (i
= 0; i
< reg_list_size
; i
++) {
2456 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2459 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2462 const char *type_str
;
2463 if (reg_list
[i
]->reg_data_type
) {
2464 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2465 /* generate <type... first, if there are architecture-defined types. */
2466 if (lookup_add_arch_defined_types(&arch_defined_types
,
2467 reg_list
[i
]->reg_data_type
->id
,
2468 &num_arch_defined_types
))
2469 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2470 reg_list
[i
]->reg_data_type
,
2471 &arch_defined_types
,
2472 &num_arch_defined_types
);
2474 type_str
= reg_list
[i
]->reg_data_type
->id
;
2476 /* predefined type */
2477 type_str
= gdb_get_reg_type_name(
2478 reg_list
[i
]->reg_data_type
->type
);
2481 /* Default type is "int" */
2485 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2486 "<reg name=\"%s\"", reg_list
[i
]->name
);
2487 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2488 " bitsize=\"%" PRIu32
"\"", reg_list
[i
]->size
);
2489 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2490 " regnum=\"%" PRIu32
"\"", reg_list
[i
]->number
);
2491 if (reg_list
[i
]->caller_save
)
2492 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2493 " save-restore=\"yes\"");
2495 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2496 " save-restore=\"no\"");
2498 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2499 " type=\"%s\"", type_str
);
2501 if (reg_list
[i
]->group
)
2502 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2503 " group=\"%s\"", reg_list
[i
]->group
);
2505 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2509 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2513 free(arch_defined_types
);
2517 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2524 if (retval
== ERROR_OK
)
2532 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2533 char **chunk
, int32_t offset
, uint32_t length
)
2536 LOG_ERROR("Unable to Generate Target Description");
2540 char *tdesc
= target_desc
->tdesc
;
2541 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2544 int retval
= gdb_generate_target_description(target
, &tdesc
);
2545 if (retval
!= ERROR_OK
) {
2546 LOG_ERROR("Unable to Generate Target Description");
2550 tdesc_length
= strlen(tdesc
);
2555 if (length
< (tdesc_length
- offset
))
2556 transfer_type
= 'm';
2558 transfer_type
= 'l';
2560 *chunk
= malloc(length
+ 2);
2562 LOG_ERROR("Unable to allocate memory");
2566 (*chunk
)[0] = transfer_type
;
2567 if (transfer_type
== 'm') {
2568 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2569 (*chunk
)[1 + length
] = '\0';
2571 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2572 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2574 /* After gdb-server sends out last chunk, invalidate tdesc. */
2580 target_desc
->tdesc
= tdesc
;
2581 target_desc
->tdesc_length
= tdesc_length
;
2586 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2588 int retval
= ERROR_OK
;
2589 struct reg
**reg_list
= NULL
;
2590 int reg_list_size
= 0;
2591 char const **features
= NULL
;
2592 int feature_list_size
= 0;
2594 char const *architecture
= target_get_gdb_arch(target
);
2596 retval
= target_get_gdb_reg_list_noread(target
, ®_list
,
2597 ®_list_size
, REG_CLASS_ALL
);
2598 if (retval
!= ERROR_OK
) {
2599 LOG_ERROR("get register list failed");
2603 if (reg_list_size
<= 0) {
2604 LOG_ERROR("get register list failed");
2605 retval
= ERROR_FAIL
;
2609 /* Get a list of available target registers features */
2610 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2611 if (retval
!= ERROR_OK
) {
2612 LOG_ERROR("Can't get the registers feature list");
2617 if (architecture
|| feature_list_size
)
2631 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2633 struct rtos
*rtos
= target
->rtos
;
2634 int retval
= ERROR_OK
;
2635 char *thread_list
= NULL
;
2639 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2640 "<?xml version=\"1.0\"?>\n"
2644 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2645 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2647 if (!thread_detail
->exists
)
2650 if (thread_detail
->thread_name_str
)
2651 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2652 "<thread id=\"%" PRIx64
"\" name=\"%s\">",
2653 thread_detail
->threadid
,
2654 thread_detail
->thread_name_str
);
2656 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2657 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2659 if (thread_detail
->thread_name_str
)
2660 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2661 "Name: %s", thread_detail
->thread_name_str
);
2663 if (thread_detail
->extra_info_str
) {
2664 if (thread_detail
->thread_name_str
)
2665 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2667 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2668 "%s", thread_detail
->extra_info_str
);
2671 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2676 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2679 if (retval
== ERROR_OK
)
2680 *thread_list_out
= thread_list
;
2687 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2688 char **chunk
, int32_t offset
, uint32_t length
)
2690 if (!*thread_list
) {
2691 int retval
= gdb_generate_thread_list(target
, thread_list
);
2692 if (retval
!= ERROR_OK
) {
2693 LOG_ERROR("Unable to Generate Thread List");
2698 size_t thread_list_length
= strlen(*thread_list
);
2701 length
= MIN(length
, thread_list_length
- offset
);
2702 if (length
< (thread_list_length
- offset
))
2703 transfer_type
= 'm';
2705 transfer_type
= 'l';
2707 *chunk
= malloc(length
+ 2 + 3);
2708 /* Allocating extra 3 bytes prevents false positive valgrind report
2709 * of strlen(chunk) word access:
2710 * Invalid read of size 4
2711 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2713 LOG_ERROR("Unable to allocate memory");
2717 (*chunk
)[0] = transfer_type
;
2718 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2719 (*chunk
)[1 + length
] = '\0';
2721 /* After gdb-server sends out last chunk, invalidate thread list. */
2722 if (transfer_type
== 'l') {
2724 *thread_list
= NULL
;
2730 static int gdb_query_packet(struct connection
*connection
,
2731 char const *packet
, int packet_size
)
2733 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2734 struct gdb_connection
*gdb_connection
= connection
->priv
;
2735 struct target
*target
= get_target_from_connection(connection
);
2737 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2738 if (packet_size
> 6) {
2739 Jim_Interp
*interp
= cmd_ctx
->interp
;
2741 cmd
= malloc((packet_size
- 6) / 2 + 1);
2742 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2745 /* We want to print all debug output to GDB connection */
2746 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
2747 target_call_timer_callbacks_now();
2748 /* some commands need to know the GDB connection, make note of current
2749 * GDB connection. */
2750 current_gdb_connection
= gdb_connection
;
2752 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
2753 cmd_ctx
->current_target_override
= NULL
;
2755 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
2756 Jim_DeleteAssocData(interp
, "context");
2757 int retval
= Jim_SetAssocData(interp
, "context", NULL
, cmd_ctx
);
2758 if (retval
== JIM_OK
) {
2759 retval
= Jim_EvalObj(interp
, Jim_NewStringObj(interp
, cmd
, -1));
2760 Jim_DeleteAssocData(interp
, "context");
2762 int inner_retval
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
2763 if (retval
== JIM_OK
)
2764 retval
= inner_retval
;
2766 cmd_ctx
->current_target_override
= saved_target_override
;
2768 current_gdb_connection
= NULL
;
2769 target_call_timer_callbacks_now();
2770 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
2772 if (retval
== JIM_RETURN
)
2773 retval
= interp
->returnCode
;
2775 const char *cretmsg
= Jim_GetString(Jim_GetResult(interp
), &lenmsg
);
2777 if (lenmsg
&& cretmsg
[lenmsg
- 1] != '\n') {
2778 retmsg
= alloc_printf("%s\n", cretmsg
);
2781 retmsg
= strdup(cretmsg
);
2784 return ERROR_GDB_BUFFER_TOO_SMALL
;
2786 if (retval
== JIM_OK
) {
2788 char *hex_buffer
= malloc(lenmsg
* 2 + 1);
2791 return ERROR_GDB_BUFFER_TOO_SMALL
;
2794 size_t pkt_len
= hexify(hex_buffer
, (const uint8_t *)retmsg
, lenmsg
,
2796 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
2799 gdb_put_packet(connection
, "OK", 2);
2803 gdb_output_con(connection
, retmsg
);
2804 gdb_send_error(connection
, retval
);
2809 gdb_put_packet(connection
, "OK", 2);
2811 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2812 if (packet_size
> 5) {
2817 target_addr_t addr
= 0;
2820 /* skip command character */
2823 addr
= strtoull(packet
, &separator
, 16);
2825 if (*separator
!= ',') {
2826 LOG_ERROR("incomplete read memory packet received, dropping connection");
2827 return ERROR_SERVER_REMOTE_CLOSED
;
2830 len
= strtoul(separator
+ 1, NULL
, 16);
2832 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2834 if (retval
== ERROR_OK
) {
2835 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2836 gdb_put_packet(connection
, gdb_reply
, 9);
2838 retval
= gdb_error(connection
, retval
);
2839 if (retval
!= ERROR_OK
)
2845 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2846 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2847 * qXfer:features:read is supported for some targets */
2848 int retval
= ERROR_OK
;
2849 char *buffer
= NULL
;
2852 int gdb_target_desc_supported
= 0;
2854 /* we need to test that the target supports target descriptions */
2855 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2856 if (retval
!= ERROR_OK
) {
2857 LOG_INFO("Failed detecting Target Description Support, disabling");
2858 gdb_target_desc_supported
= 0;
2861 /* support may be disabled globally */
2862 if (gdb_use_target_description
== 0) {
2863 if (gdb_target_desc_supported
)
2864 LOG_WARNING("Target Descriptions Supported, but disabled");
2865 gdb_target_desc_supported
= 0;
2872 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2874 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2875 (gdb_target_desc_supported
== 1) ? '+' : '-');
2877 if (retval
!= ERROR_OK
) {
2878 gdb_send_error(connection
, 01);
2882 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2886 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2887 && (flash_get_bank_count() > 0))
2888 return gdb_memory_map(connection
, packet
, packet_size
);
2889 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2891 int retval
= ERROR_OK
;
2894 unsigned int length
;
2896 /* skip command character */
2899 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2900 gdb_send_error(connection
, 01);
2904 /* Target should prepare correct target description for annex.
2905 * The first character of returned xml is 'm' or 'l'. 'm' for
2906 * there are *more* chunks to transfer. 'l' for it is the *last*
2907 * chunk of target description.
2909 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2910 &xml
, offset
, length
);
2911 if (retval
!= ERROR_OK
) {
2912 gdb_error(connection
, retval
);
2916 gdb_put_packet(connection
, xml
, strlen(xml
));
2920 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2922 int retval
= ERROR_OK
;
2925 unsigned int length
;
2927 /* skip command character */
2930 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2931 gdb_send_error(connection
, 01);
2935 /* Target should prepare correct thread list for annex.
2936 * The first character of returned xml is 'm' or 'l'. 'm' for
2937 * there are *more* chunks to transfer. 'l' for it is the *last*
2938 * chunk of target description.
2940 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2941 &xml
, offset
, length
);
2942 if (retval
!= ERROR_OK
) {
2943 gdb_error(connection
, retval
);
2947 gdb_put_packet(connection
, xml
, strlen(xml
));
2951 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2952 gdb_connection
->noack_mode
= 1;
2953 gdb_put_packet(connection
, "OK", 2);
2955 } else if (target
->type
->gdb_query_custom
) {
2956 char *buffer
= NULL
;
2957 int ret
= target
->type
->gdb_query_custom(target
, packet
, &buffer
);
2958 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2962 gdb_put_packet(connection
, "", 0);
2966 static bool gdb_handle_vcont_packet(struct connection
*connection
, const char *packet
,
2967 __attribute__((unused
)) int packet_size
)
2969 struct gdb_connection
*gdb_connection
= connection
->priv
;
2970 struct target
*target
= get_target_from_connection(connection
);
2971 const char *parse
= packet
;
2974 /* query for vCont supported */
2975 if (parse
[0] == '?') {
2976 if (target
->type
->step
) {
2977 /* gdb doesn't accept c without C and s without S */
2978 gdb_put_packet(connection
, "vCont;c;C;s;S", 13);
2984 if (parse
[0] == ';') {
2988 /* simple case, a continue packet */
2989 if (parse
[0] == 'c') {
2990 gdb_running_type
= 'c';
2991 LOG_DEBUG("target %s continue", target_name(target
));
2992 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
2993 retval
= target_resume(target
, 1, 0, 0, 0);
2994 if (retval
== ERROR_TARGET_NOT_HALTED
)
2995 LOG_INFO("target %s was not halted when resume was requested", target_name(target
));
2997 /* poll target in an attempt to make its internal state consistent */
2998 if (retval
!= ERROR_OK
) {
2999 retval
= target_poll(target
);
3000 if (retval
!= ERROR_OK
)
3001 LOG_DEBUG("error polling target %s after failed resume", target_name(target
));
3005 * We don't report errors to gdb here, move frontend_state to
3006 * TARGET_RUNNING to stay in sync with gdb's expectation of the
3009 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3010 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3015 /* single-step or step-over-breakpoint */
3016 if (parse
[0] == 's') {
3017 gdb_running_type
= 's';
3018 bool fake_step
= false;
3020 struct target
*ct
= target
;
3024 if (parse
[0] == ':') {
3027 thread_id
= strtoll(parse
, &endp
, 16);
3036 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
3037 rtos_update_threads(target
);
3039 target
->rtos
->gdb_target_for_threadid(connection
, thread_id
, &ct
);
3042 * check if the thread to be stepped is the current rtos thread
3043 * if not, we must fake the step
3045 if (target
->rtos
->current_thread
!= thread_id
)
3049 if (parse
[0] == ';') {
3052 if (parse
[0] == 'c') {
3055 /* check if thread-id follows */
3056 if (parse
[0] == ':') {
3060 tid
= strtoll(parse
, NULL
, 16);
3061 if (tid
== thread_id
) {
3063 * Special case: only step a single thread (core),
3064 * keep the other threads halted. Currently, only
3065 * aarch64 target understands it. Other target types don't
3066 * care (nobody checks the actual value of 'current')
3067 * and it doesn't really matter. This deserves
3068 * a symbolic constant and a formal interface documentation
3071 LOG_DEBUG("request to step current core only");
3072 /* uncomment after checking that indeed other targets are safe */
3079 LOG_DEBUG("target %s single-step thread %"PRIx64
, target_name(ct
), thread_id
);
3080 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3081 target_call_event_callbacks(ct
, TARGET_EVENT_GDB_START
);
3084 * work around an annoying gdb behaviour: when the current thread
3085 * is changed in gdb, it assumes that the target can follow and also
3086 * make the thread current. This is an assumption that cannot hold
3087 * for a real target running a multi-threading OS. We just fake
3088 * the step to not trigger an internal error in gdb. See
3089 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3093 char sig_reply
[128];
3095 LOG_DEBUG("fake step thread %"PRIx64
, thread_id
);
3097 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
),
3098 "T05thread:%016"PRIx64
";", thread_id
);
3100 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
3101 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3106 /* support for gdb_sync command */
3107 if (gdb_connection
->sync
) {
3108 gdb_connection
->sync
= false;
3109 if (ct
->state
== TARGET_HALTED
) {
3110 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3111 "from the target.");
3112 gdb_sig_halted(connection
);
3113 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3115 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3119 retval
= target_step(ct
, current_pc
, 0, 0);
3120 if (retval
== ERROR_TARGET_NOT_HALTED
)
3121 LOG_INFO("target %s was not halted when step was requested", target_name(ct
));
3123 /* if step was successful send a reply back to gdb */
3124 if (retval
== ERROR_OK
) {
3125 retval
= target_poll(ct
);
3126 if (retval
!= ERROR_OK
)
3127 LOG_DEBUG("error polling target %s after successful step", target_name(ct
));
3128 /* send back signal information */
3129 gdb_signal_reply(ct
, connection
);
3130 /* stop forwarding log packets! */
3131 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3133 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3136 LOG_ERROR("Unknown vCont packet");
3140 static char *next_hex_encoded_field(const char **str
, char sep
)
3143 const char *hex
= *str
;
3147 const char *end
= strchr(hex
, sep
);
3149 hexlen
= strlen(hex
);
3152 *str
= hex
+ hexlen
+ 1;
3154 if (hexlen
% 2 != 0) {
3155 /* Malformed hex data */
3159 size_t count
= hexlen
/ 2;
3160 char *decoded
= malloc(count
+ 1);
3164 size_t converted
= unhexify((void *)decoded
, hex
, count
);
3165 if (converted
!= count
) {
3170 decoded
[count
] = '\0';
3174 /* handle extended restart packet */
3175 static void gdb_restart_inferior(struct connection
*connection
, const char *packet
, int packet_size
)
3177 struct gdb_connection
*gdb_con
= connection
->priv
;
3178 struct target
*target
= get_target_from_connection(connection
);
3180 breakpoint_clear_target(target
);
3181 watchpoint_clear_target(target
);
3182 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
3183 target_name(target
));
3184 /* set connection as attached after reset */
3185 gdb_con
->attached
= true;
3186 /* info rtos parts */
3187 gdb_thread_packet(connection
, packet
, packet_size
);
3190 static bool gdb_handle_vrun_packet(struct connection
*connection
, const char *packet
, int packet_size
)
3192 struct target
*target
= get_target_from_connection(connection
);
3193 const char *parse
= packet
;
3198 if (parse
[0] != ';')
3202 /* Skip first field "filename"; don't know what to do with it. */
3203 free(next_hex_encoded_field(&parse
, ';'));
3205 char *cmdline
= next_hex_encoded_field(&parse
, ';');
3207 char *arg
= next_hex_encoded_field(&parse
, ';');
3210 char *new_cmdline
= alloc_printf("%s %s", cmdline
, arg
);
3213 cmdline
= new_cmdline
;
3217 if (target
->semihosting
) {
3218 LOG_INFO("GDB set inferior command line to '%s'", cmdline
);
3219 free(target
->semihosting
->cmdline
);
3220 target
->semihosting
->cmdline
= cmdline
;
3222 LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline
);
3227 gdb_restart_inferior(connection
, packet
, packet_size
);
3228 gdb_put_packet(connection
, "S00", 3);
3232 static int gdb_v_packet(struct connection
*connection
,
3233 char const *packet
, int packet_size
)
3235 struct gdb_connection
*gdb_connection
= connection
->priv
;
3238 struct target
*target
= get_target_from_connection(connection
);
3240 if (strncmp(packet
, "vCont", 5) == 0) {
3246 handled
= gdb_handle_vcont_packet(connection
, packet
, packet_size
);
3248 gdb_put_packet(connection
, "", 0);
3253 if (strncmp(packet
, "vRun", 4) == 0) {
3256 handled
= gdb_handle_vrun_packet(connection
, packet
, packet_size
);
3258 gdb_put_packet(connection
, "", 0);
3263 /* if flash programming disabled - send a empty reply */
3265 if (gdb_flash_program
== 0) {
3266 gdb_put_packet(connection
, "", 0);
3270 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
3272 unsigned long length
;
3274 char const *parse
= packet
+ 12;
3275 if (*parse
== '\0') {
3276 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3277 return ERROR_SERVER_REMOTE_CLOSED
;
3280 addr
= strtoul(parse
, (char **)&parse
, 16);
3282 if (*(parse
++) != ',' || *parse
== '\0') {
3283 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3284 return ERROR_SERVER_REMOTE_CLOSED
;
3287 length
= strtoul(parse
, (char **)&parse
, 16);
3289 if (*parse
!= '\0') {
3290 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3291 return ERROR_SERVER_REMOTE_CLOSED
;
3294 /* assume all sectors need erasing - stops any problems
3295 * when flash_write is called multiple times */
3298 /* perform any target specific operations before the erase */
3299 target_call_event_callbacks(target
,
3300 TARGET_EVENT_GDB_FLASH_ERASE_START
);
3302 /* vFlashErase:addr,length messages require region start and
3303 * end to be "block" aligned ... if padding is ever needed,
3304 * GDB will have become dangerously confused.
3306 result
= flash_erase_address_range(target
, false, addr
,
3309 /* perform any target specific operations after the erase */
3310 target_call_event_callbacks(target
,
3311 TARGET_EVENT_GDB_FLASH_ERASE_END
);
3314 if (result
!= ERROR_OK
) {
3315 /* GDB doesn't evaluate the actual error number returned,
3316 * treat a failed erase as an I/O error
3318 gdb_send_error(connection
, EIO
);
3319 LOG_ERROR("flash_erase returned %i", result
);
3321 gdb_put_packet(connection
, "OK", 2);
3326 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
3329 unsigned long length
;
3330 char const *parse
= packet
+ 12;
3332 if (*parse
== '\0') {
3333 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3334 return ERROR_SERVER_REMOTE_CLOSED
;
3336 addr
= strtoul(parse
, (char **)&parse
, 16);
3337 if (*(parse
++) != ':') {
3338 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3339 return ERROR_SERVER_REMOTE_CLOSED
;
3341 length
= packet_size
- (parse
- packet
);
3343 /* create a new image if there isn't already one */
3344 if (!gdb_connection
->vflash_image
) {
3345 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
3346 image_open(gdb_connection
->vflash_image
, "", "build");
3349 /* create new section with content from packet buffer */
3350 retval
= image_add_section(gdb_connection
->vflash_image
,
3351 addr
, length
, 0x0, (uint8_t const *)parse
);
3352 if (retval
!= ERROR_OK
)
3355 gdb_put_packet(connection
, "OK", 2);
3360 if (strncmp(packet
, "vFlashDone", 10) == 0) {
3363 /* process the flashing buffer. No need to erase as GDB
3364 * always issues a vFlashErase first. */
3365 target_call_event_callbacks(target
,
3366 TARGET_EVENT_GDB_FLASH_WRITE_START
);
3367 result
= flash_write(target
, gdb_connection
->vflash_image
,
3369 target_call_event_callbacks(target
,
3370 TARGET_EVENT_GDB_FLASH_WRITE_END
);
3371 if (result
!= ERROR_OK
) {
3372 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
3373 gdb_put_packet(connection
, "E.memtype", 9);
3375 gdb_send_error(connection
, EIO
);
3377 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
3378 gdb_put_packet(connection
, "OK", 2);
3381 image_close(gdb_connection
->vflash_image
);
3382 free(gdb_connection
->vflash_image
);
3383 gdb_connection
->vflash_image
= NULL
;
3388 gdb_put_packet(connection
, "", 0);
3392 static int gdb_detach(struct connection
*connection
)
3395 * Only reply "OK" to GDB
3396 * it will close the connection and this will trigger a call to
3397 * gdb_connection_closed() that will in turn trigger the event
3398 * TARGET_EVENT_GDB_DETACH
3400 return gdb_put_packet(connection
, "OK", 2);
3403 /* The format of 'F' response packet is
3404 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3406 static int gdb_fileio_response_packet(struct connection
*connection
,
3407 char const *packet
, int packet_size
)
3409 struct target
*target
= get_target_from_connection(connection
);
3411 char *parsing_point
;
3412 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
3413 int fileio_errno
= 0;
3414 bool fileio_ctrl_c
= false;
3419 if (*separator
== ',') {
3420 parsing_point
= separator
+ 1;
3421 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
3422 if (*separator
== ',') {
3423 if (*(separator
+ 1) == 'C') {
3424 /* TODO: process ctrl-c */
3425 fileio_ctrl_c
= true;
3430 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3431 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
3433 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
3434 if (retval
!= ERROR_OK
)
3437 /* After File-I/O ends, keep continue or step */
3438 if (gdb_running_type
== 'c')
3439 retval
= target_resume(target
, 1, 0x0, 0, 0);
3440 else if (gdb_running_type
== 's')
3441 retval
= target_step(target
, 1, 0x0, 0);
3443 retval
= ERROR_FAIL
;
3445 if (retval
!= ERROR_OK
)
3451 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
3452 const char *function
, const char *string
)
3454 struct connection
*connection
= priv
;
3455 struct gdb_connection
*gdb_con
= connection
->priv
;
3457 if (gdb_con
->output_flag
== GDB_OUTPUT_NO
)
3458 /* No out allowed */
3461 if (gdb_con
->busy
) {
3462 /* do not reply this using the O packet */
3466 gdb_output_con(connection
, string
);
3469 static void gdb_sig_halted(struct connection
*connection
)
3472 snprintf(sig_reply
, 4, "T%2.2x", 2);
3473 gdb_put_packet(connection
, sig_reply
, 3);
3476 static int gdb_input_inner(struct connection
*connection
)
3478 /* Do not allocate this on the stack */
3479 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for null-termination */
3481 struct target
*target
;
3482 char const *packet
= gdb_packet_buffer
;
3485 struct gdb_connection
*gdb_con
= connection
->priv
;
3486 static bool warn_use_ext
;
3488 target
= get_target_from_connection(connection
);
3490 /* drain input buffer. If one of the packets fail, then an error
3491 * packet is replied, if applicable.
3493 * This loop will terminate and the error code is returned.
3495 * The calling fn will check if this error is something that
3496 * can be recovered from, or if the connection must be closed.
3498 * If the error is recoverable, this fn is called again to
3499 * drain the rest of the buffer.
3502 packet_size
= GDB_BUFFER_SIZE
;
3503 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
3504 if (retval
!= ERROR_OK
)
3507 /* terminate with zero */
3508 gdb_packet_buffer
[packet_size
] = '\0';
3510 if (packet_size
> 0) {
3512 gdb_log_incoming_packet(connection
, gdb_packet_buffer
);
3515 switch (packet
[0]) {
3516 case 'T': /* Is thread alive? */
3517 gdb_thread_packet(connection
, packet
, packet_size
);
3519 case 'H': /* Set current thread ( 'c' for step and continue,
3520 * 'g' for all other operations ) */
3521 gdb_thread_packet(connection
, packet
, packet_size
);
3525 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
3526 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
3527 retval
= gdb_query_packet(connection
, packet
, packet_size
);
3530 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
3533 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
3536 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
3539 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
3542 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
3545 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
3549 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
3552 gdb_last_signal_packet(connection
, packet
, packet_size
);
3553 /* '?' is sent after the eventual '!' */
3554 if (!warn_use_ext
&& !gdb_con
->extended_protocol
) {
3555 warn_use_ext
= true;
3556 LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3557 connection
->service
->port
, connection
->service
->port
);
3563 gdb_thread_packet(connection
, packet
, packet_size
);
3564 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3566 if (gdb_con
->mem_write_error
) {
3567 LOG_ERROR("Memory write failure!");
3569 /* now that we have reported the memory write error,
3570 * we can clear the condition */
3571 gdb_con
->mem_write_error
= false;
3574 bool nostep
= false;
3575 bool already_running
= false;
3576 if (target
->state
== TARGET_RUNNING
) {
3577 LOG_WARNING("WARNING! The target is already running. "
3578 "All changes GDB did to registers will be discarded! "
3579 "Waiting for target to halt.");
3580 already_running
= true;
3581 } else if (target
->state
!= TARGET_HALTED
) {
3582 LOG_WARNING("The target is not in the halted nor running stated, "
3583 "stepi/continue ignored.");
3585 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
3586 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3587 * sent by GDB first to OpenOCD, thus defeating the check to
3588 * make only the single stepping have the sync feature...
3591 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3592 "from the target.");
3594 gdb_con
->sync
= false;
3596 if (!already_running
&& nostep
) {
3597 /* Either the target isn't in the halted state, then we can't
3598 * step/continue. This might be early setup, etc.
3600 * Or we want to allow GDB to pick up a fresh set of
3601 * register values without modifying the target state.
3604 gdb_sig_halted(connection
);
3606 /* stop forwarding log packets! */
3607 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
3609 /* We're running/stepping, in which case we can
3610 * forward log output until the target is halted
3612 gdb_con
->frontend_state
= TARGET_RUNNING
;
3613 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3615 if (!already_running
) {
3616 /* Here we don't want packet processing to stop even if this fails,
3617 * so we use a local variable instead of retval. */
3618 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
3619 if (retval
!= ERROR_OK
) {
3620 /* we'll never receive a halted
3621 * condition... issue a false one..
3623 gdb_frontend_halted(target
, connection
);
3630 retval
= gdb_v_packet(connection
, packet
, packet_size
);
3633 retval
= gdb_detach(connection
);
3636 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
3637 if (retval
!= ERROR_OK
)
3641 if (gdb_con
->extended_protocol
) {
3642 gdb_con
->attached
= false;
3645 gdb_put_packet(connection
, "OK", 2);
3646 return ERROR_SERVER_REMOTE_CLOSED
;
3648 /* handle extended remote protocol */
3649 gdb_con
->extended_protocol
= true;
3650 gdb_put_packet(connection
, "OK", 2);
3653 /* handle extended restart packet */
3654 gdb_restart_inferior(connection
, packet
, packet_size
);
3659 /* packet supported only by smp target i.e cortex_a.c*/
3660 /* handle smp packet replying coreid played to gbd */
3661 gdb_read_smp_packet(connection
, packet
, packet_size
);
3666 /* packet supported only by smp target i.e cortex_a.c */
3667 /* handle smp packet setting coreid to be played at next
3669 gdb_write_smp_packet(connection
, packet
, packet_size
);
3673 /* File-I/O extension */
3674 /* After gdb uses host-side syscall to complete target file
3675 * I/O, gdb sends host-side syscall return value to target
3677 * The format of 'F' response packet is
3678 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3680 gdb_con
->frontend_state
= TARGET_RUNNING
;
3681 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3682 gdb_fileio_response_packet(connection
, packet
, packet_size
);
3686 /* ignore unknown packets */
3687 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
3688 gdb_put_packet(connection
, "", 0);
3692 /* if a packet handler returned an error, exit input loop */
3693 if (retval
!= ERROR_OK
)
3697 if (gdb_con
->ctrl_c
) {
3698 if (target
->state
== TARGET_RUNNING
) {
3699 struct target
*t
= target
;
3701 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &t
);
3702 retval
= target_halt(t
);
3703 if (retval
== ERROR_OK
)
3704 retval
= target_poll(t
);
3705 if (retval
!= ERROR_OK
)
3706 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3707 gdb_con
->ctrl_c
= false;
3709 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3710 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3714 } while (gdb_con
->buf_cnt
> 0);
3719 static int gdb_input(struct connection
*connection
)
3721 int retval
= gdb_input_inner(connection
);
3722 struct gdb_connection
*gdb_con
= connection
->priv
;
3723 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3726 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3727 if (gdb_con
->closed
)
3728 return ERROR_SERVER_REMOTE_CLOSED
;
3730 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3734 static void gdb_keep_client_alive(struct connection
*connection
)
3736 struct gdb_connection
*gdb_con
= connection
->priv
;
3738 if (gdb_con
->busy
) {
3739 /* do not send packets, retry asap */
3743 switch (gdb_con
->output_flag
) {
3745 /* no need for keep-alive */
3747 case GDB_OUTPUT_ALL
:
3748 /* send an empty O packet */
3749 gdb_output_con(connection
, "");
3756 static const struct service_driver gdb_service_driver
= {
3758 .new_connection_during_keep_alive_handler
= NULL
,
3759 .new_connection_handler
= gdb_new_connection
,
3760 .input_handler
= gdb_input
,
3761 .connection_closed_handler
= gdb_connection_closed
,
3762 .keep_client_alive_handler
= gdb_keep_client_alive
,
3765 static int gdb_target_start(struct target
*target
, const char *port
)
3767 struct gdb_service
*gdb_service
;
3769 gdb_service
= malloc(sizeof(struct gdb_service
));
3774 LOG_INFO("starting gdb server for %s on %s", target_name(target
), port
);
3776 gdb_service
->target
= target
;
3777 gdb_service
->core
[0] = -1;
3778 gdb_service
->core
[1] = -1;
3779 target
->gdb_service
= gdb_service
;
3781 ret
= add_service(&gdb_service_driver
, port
, target
->gdb_max_connections
, gdb_service
);
3782 /* initialize all targets gdb service with the same pointer */
3784 struct target_list
*head
;
3785 foreach_smp_target(head
, target
->smp_targets
) {
3786 struct target
*curr
= head
->target
;
3788 curr
->gdb_service
= gdb_service
;
3794 static int gdb_target_add_one(struct target
*target
)
3796 /* one gdb instance per smp list */
3797 if ((target
->smp
) && (target
->gdb_service
))
3800 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3801 if (!target_supports_gdb_connection(target
)) {
3802 LOG_DEBUG("skip gdb server for target %s", target_name(target
));
3806 if (target
->gdb_port_override
) {
3807 if (strcmp(target
->gdb_port_override
, "disabled") == 0) {
3808 LOG_INFO("gdb port disabled");
3811 return gdb_target_start(target
, target
->gdb_port_override
);
3814 if (strcmp(gdb_port
, "disabled") == 0) {
3815 LOG_INFO("gdb port disabled");
3819 int retval
= gdb_target_start(target
, gdb_port_next
);
3820 if (retval
== ERROR_OK
) {
3821 /* save the port number so can be queried with
3822 * $target_name cget -gdb-port
3824 target
->gdb_port_override
= strdup(gdb_port_next
);
3827 /* If we can parse the port number
3828 * then we increment the port number for the next target.
3831 portnumber
= strtol(gdb_port_next
, &end
, 0);
3833 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3834 free(gdb_port_next
);
3836 gdb_port_next
= alloc_printf("%ld", portnumber
+1);
3838 /* Don't increment if gdb_port is 0, since we're just
3839 * trying to allocate an unused port. */
3840 gdb_port_next
= strdup("0");
3848 int gdb_target_add_all(struct target
*target
)
3851 LOG_WARNING("gdb services need one or more targets defined");
3856 int retval
= gdb_target_add_one(target
);
3857 if (retval
!= ERROR_OK
)
3860 target
= target
->next
;
3866 COMMAND_HANDLER(handle_gdb_sync_command
)
3869 return ERROR_COMMAND_SYNTAX_ERROR
;
3871 if (!current_gdb_connection
) {
3873 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3877 current_gdb_connection
->sync
= true;
3882 /* daemon configuration command gdb_port */
3883 COMMAND_HANDLER(handle_gdb_port_command
)
3885 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3886 if (retval
== ERROR_OK
) {
3887 free(gdb_port_next
);
3888 gdb_port_next
= strdup(gdb_port
);
3893 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3896 return ERROR_COMMAND_SYNTAX_ERROR
;
3898 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3902 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3905 return ERROR_COMMAND_SYNTAX_ERROR
;
3907 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3911 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3914 return ERROR_COMMAND_SYNTAX_ERROR
;
3916 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3920 COMMAND_HANDLER(handle_gdb_report_register_access_error
)
3923 return ERROR_COMMAND_SYNTAX_ERROR
;
3925 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_register_access_error
);
3929 /* gdb_breakpoint_override */
3930 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3932 if (CMD_ARGC
== 0) {
3934 } else if (CMD_ARGC
== 1) {
3935 gdb_breakpoint_override
= 1;
3936 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3937 gdb_breakpoint_override_type
= BKPT_HARD
;
3938 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3939 gdb_breakpoint_override_type
= BKPT_SOFT
;
3940 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3941 gdb_breakpoint_override
= 0;
3943 return ERROR_COMMAND_SYNTAX_ERROR
;
3944 if (gdb_breakpoint_override
)
3945 LOG_USER("force %s breakpoints",
3946 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3948 LOG_USER("breakpoint type is not overridden");
3953 COMMAND_HANDLER(handle_gdb_target_description_command
)
3956 return ERROR_COMMAND_SYNTAX_ERROR
;
3958 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3962 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3965 uint32_t tdesc_length
;
3966 struct target
*target
= get_current_target(CMD_CTX
);
3968 int retval
= gdb_generate_target_description(target
, &tdesc
);
3969 if (retval
!= ERROR_OK
) {
3970 LOG_ERROR("Unable to Generate Target Description");
3974 tdesc_length
= strlen(tdesc
);
3976 struct fileio
*fileio
;
3977 size_t size_written
;
3979 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3980 if (!tdesc_filename
) {
3981 retval
= ERROR_FAIL
;
3985 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
3987 if (retval
!= ERROR_OK
) {
3988 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
3992 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
3994 fileio_close(fileio
);
3996 if (retval
!= ERROR_OK
)
3997 LOG_ERROR("Error while writing the tdesc file");
4000 free(tdesc_filename
);
4006 static const struct command_registration gdb_command_handlers
[] = {
4009 .handler
= handle_gdb_sync_command
,
4010 .mode
= COMMAND_ANY
,
4011 .help
= "next stepi will return immediately allowing "
4012 "GDB to fetch register state without affecting "
4018 .handler
= handle_gdb_port_command
,
4019 .mode
= COMMAND_CONFIG
,
4020 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4021 "server listens for the next port number after the "
4022 "base port number specified. "
4023 "No arguments reports GDB port. \"pipe\" means listen to stdin "
4024 "output to stdout, an integer is base port number, \"disabled\" disables "
4025 "port. Any other string is are interpreted as named pipe to listen to. "
4026 "Output pipe is the same name as input pipe, but with 'o' appended.",
4027 .usage
= "[port_num]",
4030 .name
= "gdb_memory_map",
4031 .handler
= handle_gdb_memory_map_command
,
4032 .mode
= COMMAND_CONFIG
,
4033 .help
= "enable or disable memory map",
4034 .usage
= "('enable'|'disable')"
4037 .name
= "gdb_flash_program",
4038 .handler
= handle_gdb_flash_program_command
,
4039 .mode
= COMMAND_CONFIG
,
4040 .help
= "enable or disable flash program",
4041 .usage
= "('enable'|'disable')"
4044 .name
= "gdb_report_data_abort",
4045 .handler
= handle_gdb_report_data_abort_command
,
4046 .mode
= COMMAND_CONFIG
,
4047 .help
= "enable or disable reporting data aborts",
4048 .usage
= "('enable'|'disable')"
4051 .name
= "gdb_report_register_access_error",
4052 .handler
= handle_gdb_report_register_access_error
,
4053 .mode
= COMMAND_CONFIG
,
4054 .help
= "enable or disable reporting register access errors",
4055 .usage
= "('enable'|'disable')"
4058 .name
= "gdb_breakpoint_override",
4059 .handler
= handle_gdb_breakpoint_override_command
,
4060 .mode
= COMMAND_ANY
,
4061 .help
= "Display or specify type of breakpoint "
4062 "to be used by gdb 'break' commands.",
4063 .usage
= "('hard'|'soft'|'disable')"
4066 .name
= "gdb_target_description",
4067 .handler
= handle_gdb_target_description_command
,
4068 .mode
= COMMAND_CONFIG
,
4069 .help
= "enable or disable target description",
4070 .usage
= "('enable'|'disable')"
4073 .name
= "gdb_save_tdesc",
4074 .handler
= handle_gdb_save_tdesc_command
,
4075 .mode
= COMMAND_EXEC
,
4076 .help
= "Save the target description file",
4079 COMMAND_REGISTRATION_DONE
4082 int gdb_register_commands(struct command_context
*cmd_ctx
)
4084 gdb_port
= strdup("3333");
4085 gdb_port_next
= strdup("3333");
4086 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);
4089 void gdb_service_free(void)
4092 free(gdb_port_next
);
4095 int gdb_get_actual_connections(void)
4097 return gdb_actual_connections
;