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
;
98 /* Unique index for this GDB connection. */
99 unsigned int unique_index
;
103 #define _DEBUG_GDB_IO_
106 static struct gdb_connection
*current_gdb_connection
;
108 static int gdb_breakpoint_override
;
109 static enum breakpoint_type gdb_breakpoint_override_type
;
111 static int gdb_error(struct connection
*connection
, int retval
);
112 static char *gdb_port
;
113 static char *gdb_port_next
;
115 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
116 const char *function
, const char *string
);
118 static void gdb_sig_halted(struct connection
*connection
);
120 /* number of gdb connections, mainly to suppress gdb related debugging spam
121 * in helper/log.c when no gdb connections are actually active */
122 static int gdb_actual_connections
;
124 /* set if we are sending a memory map to gdb
125 * via qXfer:memory-map:read packet */
126 /* enabled by default*/
127 static int gdb_use_memory_map
= 1;
128 /* enabled by default*/
129 static int gdb_flash_program
= 1;
131 /* if set, data aborts cause an error to be reported in memory read packets
132 * see the code in gdb_read_memory_packet() for further explanations.
133 * Disabled by default.
135 static int gdb_report_data_abort
;
136 /* If set, errors when accessing registers are reported to gdb. Disabled by
138 static int gdb_report_register_access_error
;
140 /* set if we are sending target descriptions to gdb
141 * via qXfer:features:read packet */
142 /* enabled by default */
143 static int gdb_use_target_description
= 1;
145 /* current processing free-run type, used by file-I/O */
146 static char gdb_running_type
;
148 static int gdb_last_signal(struct target
*target
)
150 LOG_TARGET_DEBUG(target
, "Debug reason is: %s",
151 target_debug_reason_str(target
->debug_reason
));
153 switch (target
->debug_reason
) {
154 case DBG_REASON_DBGRQ
:
155 return 0x2; /* SIGINT */
156 case DBG_REASON_BREAKPOINT
:
157 case DBG_REASON_WATCHPOINT
:
158 case DBG_REASON_WPTANDBKPT
:
159 return 0x05; /* SIGTRAP */
160 case DBG_REASON_SINGLESTEP
:
161 return 0x05; /* SIGTRAP */
162 case DBG_REASON_EXC_CATCH
:
164 case DBG_REASON_NOTHALTED
:
165 return 0x0; /* no signal... shouldn't happen */
167 LOG_USER("undefined debug reason %d (%s) - target needs reset",
168 target
->debug_reason
,
169 target_debug_reason_str(target
->debug_reason
));
174 static int check_pending(struct connection
*connection
,
175 int timeout_s
, int *got_data
)
177 /* a non-blocking socket will block if there is 0 bytes available on the socket,
178 * but return with as many bytes as are available immediately
182 struct gdb_connection
*gdb_con
= connection
->priv
;
188 if (gdb_con
->buf_cnt
> 0) {
194 FD_SET(connection
->fd
, &read_fds
);
196 tv
.tv_sec
= timeout_s
;
198 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
199 /* This can typically be because a "monitor" command took too long
200 * before printing any progress messages
203 return ERROR_GDB_TIMEOUT
;
207 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
211 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
213 struct gdb_connection
*gdb_con
= connection
->priv
;
214 int retval
= ERROR_OK
;
216 #ifdef _DEBUG_GDB_IO_
220 if (connection
->service
->type
!= CONNECTION_TCP
)
221 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
223 retval
= check_pending(connection
, 1, NULL
);
224 if (retval
!= ERROR_OK
)
226 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
231 if (gdb_con
->buf_cnt
> 0)
233 if (gdb_con
->buf_cnt
== 0) {
234 LOG_DEBUG("GDB connection closed by the remote client");
235 gdb_con
->closed
= true;
236 return ERROR_SERVER_REMOTE_CLOSED
;
240 bool retry
= (WSAGetLastError() == WSAEWOULDBLOCK
);
242 bool retry
= (errno
== EAGAIN
);
246 // Try again after a delay
249 // Print error and close the socket
250 log_socket_error("GDB");
251 gdb_con
->closed
= true;
252 return ERROR_SERVER_REMOTE_CLOSED
;
256 #ifdef _DEBUG_GDB_IO_
257 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
258 LOG_DEBUG("received '%s'", debug_buffer
);
262 gdb_con
->buf_p
= gdb_con
->buffer
;
264 *next_char
= *(gdb_con
->buf_p
++);
265 if (gdb_con
->buf_cnt
> 0)
266 connection
->input_pending
= true;
268 connection
->input_pending
= false;
269 #ifdef _DEBUG_GDB_IO_
270 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
277 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
278 * held in registers in the inner loop.
280 * For small caches and embedded systems this is important!
282 static inline int gdb_get_char_fast(struct connection
*connection
,
283 int *next_char
, char **buf_p
, int *buf_cnt
)
285 int retval
= ERROR_OK
;
287 if ((*buf_cnt
)-- > 0) {
288 *next_char
= **buf_p
;
291 connection
->input_pending
= true;
293 connection
->input_pending
= false;
295 #ifdef _DEBUG_GDB_IO_
296 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
302 struct gdb_connection
*gdb_con
= connection
->priv
;
303 gdb_con
->buf_p
= *buf_p
;
304 gdb_con
->buf_cnt
= *buf_cnt
;
305 retval
= gdb_get_char_inner(connection
, next_char
);
306 *buf_p
= gdb_con
->buf_p
;
307 *buf_cnt
= gdb_con
->buf_cnt
;
312 static int gdb_get_char(struct connection
*connection
, int *next_char
)
314 struct gdb_connection
*gdb_con
= connection
->priv
;
315 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
318 static int gdb_putback_char(struct connection
*connection
, int last_char
)
320 struct gdb_connection
*gdb_con
= connection
->priv
;
322 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
323 *(--gdb_con
->buf_p
) = last_char
;
326 LOG_ERROR("BUG: couldn't put character back");
331 /* The only way we can detect that the socket is closed is the first time
332 * we write to it, we will fail. Subsequent write operations will
333 * succeed. Shudder! */
334 static int gdb_write(struct connection
*connection
, void *data
, int len
)
336 struct gdb_connection
*gdb_con
= connection
->priv
;
337 if (gdb_con
->closed
) {
338 LOG_DEBUG("GDB socket marked as closed, cannot write to it.");
339 return ERROR_SERVER_REMOTE_CLOSED
;
342 if (connection_write(connection
, data
, len
) == len
)
345 LOG_WARNING("Error writing to GDB socket. Dropping the connection.");
346 gdb_con
->closed
= true;
347 return ERROR_SERVER_REMOTE_CLOSED
;
350 static void gdb_log_incoming_packet(struct connection
*connection
, char *packet
)
352 if (!LOG_LEVEL_IS(LOG_LVL_DEBUG
))
355 struct target
*target
= get_target_from_connection(connection
);
356 struct gdb_connection
*gdb_connection
= connection
->priv
;
358 /* Avoid dumping non-printable characters to the terminal */
359 const unsigned packet_len
= strlen(packet
);
360 const char *nonprint
= find_nonprint_char(packet
, packet_len
);
362 /* Does packet at least have a prefix that is printable?
363 * Look within the first 50 chars of the packet. */
364 const char *colon
= memchr(packet
, ':', MIN(50, packet_len
));
365 const bool packet_has_prefix
= (colon
);
366 const bool packet_prefix_printable
= (packet_has_prefix
&& nonprint
> colon
);
368 if (packet_prefix_printable
) {
369 const unsigned int prefix_len
= colon
- packet
+ 1; /* + 1 to include the ':' */
370 const unsigned int payload_len
= packet_len
- prefix_len
;
371 LOG_TARGET_DEBUG(target
, "{%d} received packet: %.*s<binary-data-%u-bytes>",
372 gdb_connection
->unique_index
, prefix_len
, packet
, payload_len
);
374 LOG_TARGET_DEBUG(target
, "{%d} received packet: <binary-data-%u-bytes>",
375 gdb_connection
->unique_index
, packet_len
);
378 /* All chars printable, dump the packet as is */
379 LOG_TARGET_DEBUG(target
, "{%d} received packet: %s", gdb_connection
->unique_index
, packet
);
383 static void gdb_log_outgoing_packet(struct connection
*connection
, char *packet_buf
,
384 unsigned int packet_len
, unsigned char checksum
)
386 if (!LOG_LEVEL_IS(LOG_LVL_DEBUG
))
389 struct target
*target
= get_target_from_connection(connection
);
390 struct gdb_connection
*gdb_connection
= connection
->priv
;
392 if (find_nonprint_char(packet_buf
, packet_len
))
393 LOG_TARGET_DEBUG(target
, "{%d} sending packet: $<binary-data-%u-bytes>#%2.2x",
394 gdb_connection
->unique_index
, packet_len
, checksum
);
396 LOG_TARGET_DEBUG(target
, "{%d} sending packet: $%.*s#%2.2x",
397 gdb_connection
->unique_index
, packet_len
, packet_buf
, checksum
);
400 static int gdb_put_packet_inner(struct connection
*connection
,
401 char *buffer
, int len
)
404 unsigned char my_checksum
= 0;
407 struct gdb_connection
*gdb_con
= connection
->priv
;
409 for (i
= 0; i
< len
; i
++)
410 my_checksum
+= buffer
[i
];
412 #ifdef _DEBUG_GDB_IO_
414 * At this point we should have nothing in the input queue from GDB,
415 * however sometimes '-' is sent even though we've already received
416 * an ACK (+) for everything we've sent off.
420 retval
= check_pending(connection
, 0, &gotdata
);
421 if (retval
!= ERROR_OK
)
425 retval
= gdb_get_char(connection
, &reply
);
426 if (retval
!= ERROR_OK
)
429 /* fix a problem with some IAR tools */
430 gdb_putback_char(connection
, reply
);
431 LOG_DEBUG("Unexpected start of new packet");
435 LOG_WARNING("Discard unexpected char %c", reply
);
440 gdb_log_outgoing_packet(connection
, buffer
, len
, my_checksum
);
442 char local_buffer
[1024];
443 local_buffer
[0] = '$';
444 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
445 /* performance gain on smaller packets by only a single call to gdb_write() */
446 memcpy(local_buffer
+ 1, buffer
, len
++);
447 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
448 retval
= gdb_write(connection
, local_buffer
, len
);
449 if (retval
!= ERROR_OK
)
452 /* larger packets are transmitted directly from caller supplied buffer
453 * by several calls to gdb_write() to avoid dynamic allocation */
454 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
455 retval
= gdb_write(connection
, local_buffer
, 1);
456 if (retval
!= ERROR_OK
)
458 retval
= gdb_write(connection
, buffer
, len
);
459 if (retval
!= ERROR_OK
)
461 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
462 if (retval
!= ERROR_OK
)
466 if (gdb_con
->noack_mode
)
469 retval
= gdb_get_char(connection
, &reply
);
470 if (retval
!= ERROR_OK
)
474 gdb_log_incoming_packet(connection
, "+");
476 } else if (reply
== '-') {
477 /* Stop sending output packets for now */
478 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
479 gdb_log_incoming_packet(connection
, "-");
480 LOG_WARNING("negative reply, retrying");
481 } else if (reply
== 0x3) {
482 gdb_con
->ctrl_c
= true;
483 gdb_log_incoming_packet(connection
, "<Ctrl-C>");
484 retval
= gdb_get_char(connection
, &reply
);
485 if (retval
!= ERROR_OK
)
488 gdb_log_incoming_packet(connection
, "+");
490 } else if (reply
== '-') {
491 /* Stop sending output packets for now */
492 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
493 gdb_log_incoming_packet(connection
, "-");
494 LOG_WARNING("negative reply, retrying");
495 } else if (reply
== '$') {
496 LOG_ERROR("GDB missing ack(1) - assumed good");
497 gdb_putback_char(connection
, reply
);
500 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
501 gdb_con
->closed
= true;
502 return ERROR_SERVER_REMOTE_CLOSED
;
504 } else if (reply
== '$') {
505 LOG_ERROR("GDB missing ack(2) - assumed good");
506 gdb_putback_char(connection
, reply
);
509 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
511 gdb_con
->closed
= true;
512 return ERROR_SERVER_REMOTE_CLOSED
;
516 return ERROR_SERVER_REMOTE_CLOSED
;
521 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
523 struct gdb_connection
*gdb_con
= connection
->priv
;
524 gdb_con
->busy
= true;
525 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
526 gdb_con
->busy
= false;
528 /* we sent some data, reset timer for keep alive messages */
534 static inline int fetch_packet(struct connection
*connection
,
535 int *checksum_ok
, int noack
, int *len
, char *buffer
)
537 unsigned char my_checksum
= 0;
540 int retval
= ERROR_OK
;
542 struct gdb_connection
*gdb_con
= connection
->priv
;
547 /* move this over into local variables to use registers and give the
548 * more freedom to optimize */
549 char *buf_p
= gdb_con
->buf_p
;
550 int buf_cnt
= gdb_con
->buf_cnt
;
553 /* The common case is that we have an entire packet with no escape chars.
554 * We need to leave at least 2 bytes in the buffer to have
555 * gdb_get_char() update various bits and bobs correctly.
557 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
558 /* The compiler will struggle a bit with constant propagation and
559 * aliasing, so we help it by showing that these values do not
560 * change inside the loop
564 int run
= buf_cnt
- 2;
570 if (character
== '#') {
571 /* Danger! character can be '#' when esc is
572 * used so we need an explicit boolean for done here. */
577 if (character
== '}') {
578 /* data transmitted in binary mode (X packet)
579 * uses 0x7d as escape character */
580 my_checksum
+= character
& 0xff;
583 my_checksum
+= character
& 0xff;
584 buffer
[count
++] = (character
^ 0x20) & 0xff;
586 my_checksum
+= character
& 0xff;
587 buffer
[count
++] = character
& 0xff;
596 LOG_ERROR("packet buffer too small");
597 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
601 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
602 if (retval
!= ERROR_OK
)
605 if (character
== '#')
608 if (character
== '}') {
609 /* data transmitted in binary mode (X packet)
610 * uses 0x7d as escape character */
611 my_checksum
+= character
& 0xff;
613 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
614 if (retval
!= ERROR_OK
)
617 my_checksum
+= character
& 0xff;
618 buffer
[count
++] = (character
^ 0x20) & 0xff;
620 my_checksum
+= character
& 0xff;
621 buffer
[count
++] = character
& 0xff;
625 gdb_con
->buf_p
= buf_p
;
626 gdb_con
->buf_cnt
= buf_cnt
;
628 if (retval
!= ERROR_OK
)
633 retval
= gdb_get_char(connection
, &character
);
634 if (retval
!= ERROR_OK
)
636 checksum
[0] = character
;
637 retval
= gdb_get_char(connection
, &character
);
638 if (retval
!= ERROR_OK
)
640 checksum
[1] = character
;
644 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
649 static int gdb_get_packet_inner(struct connection
*connection
,
650 char *buffer
, int *len
)
654 struct gdb_connection
*gdb_con
= connection
->priv
;
658 retval
= gdb_get_char(connection
, &character
);
659 if (retval
!= ERROR_OK
)
662 #ifdef _DEBUG_GDB_IO_
663 LOG_DEBUG("character: '%c'", character
);
670 gdb_log_incoming_packet(connection
, "+");
671 /* According to the GDB documentation
672 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
673 * "gdb sends a final `+` acknowledgment of the stub's `OK`
674 * response, which can be safely ignored by the stub."
675 * However OpenOCD server already is in noack mode at this
676 * point and instead of ignoring this it was emitting a
677 * warning. This code makes server ignore the first ACK
678 * that will be received after going into noack mode,
679 * warning only about subsequent ACK's. */
680 if (gdb_con
->noack_mode
> 1) {
681 LOG_WARNING("acknowledgment received, but no packet pending");
682 } else if (gdb_con
->noack_mode
) {
683 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
684 gdb_con
->noack_mode
= 2;
688 gdb_log_incoming_packet(connection
, "-");
689 LOG_WARNING("negative acknowledgment, but no packet pending");
692 gdb_log_incoming_packet(connection
, "<Ctrl-C>");
693 gdb_con
->ctrl_c
= true;
697 LOG_WARNING("ignoring character 0x%x", character
);
700 } while (character
!= '$');
703 /* explicit code expansion here to get faster inlined code in -O3 by not
704 * calculating checksum */
705 if (gdb_con
->noack_mode
) {
706 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
707 if (retval
!= ERROR_OK
)
710 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
711 if (retval
!= ERROR_OK
)
715 if (gdb_con
->noack_mode
) {
716 /* checksum is not checked in noack mode */
720 retval
= gdb_write(connection
, "+", 1);
721 if (retval
!= ERROR_OK
)
727 return ERROR_SERVER_REMOTE_CLOSED
;
732 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
734 struct gdb_connection
*gdb_con
= connection
->priv
;
735 gdb_con
->busy
= true;
736 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
737 gdb_con
->busy
= false;
741 static int gdb_output_con(struct connection
*connection
, const char *line
)
746 bin_size
= strlen(line
);
748 hex_buffer
= malloc(bin_size
* 2 + 2);
750 return ERROR_GDB_BUFFER_TOO_SMALL
;
753 size_t pkt_len
= hexify(hex_buffer
+ 1, (const uint8_t *)line
, bin_size
,
755 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
761 static int gdb_output(struct command_context
*context
, const char *line
)
763 /* this will be dumped to the log and also sent as an O packet if possible */
764 LOG_USER_N("%s", line
);
768 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
770 struct gdb_connection
*gdb_connection
= connection
->priv
;
772 char stop_reason
[32];
773 char current_thread
[25];
777 rtos_update_threads(target
);
779 if (target
->debug_reason
== DBG_REASON_EXIT
) {
780 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
784 target
->rtos
->current_threadid
= target
->rtos
->current_thread
;
785 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &ct
);
790 if (gdb_connection
->ctrl_c
) {
791 LOG_TARGET_DEBUG(target
, "Responding with signal 2 (SIGINT) to debugger due to Ctrl-C");
794 signal_var
= gdb_last_signal(ct
);
796 stop_reason
[0] = '\0';
797 if (ct
->debug_reason
== DBG_REASON_WATCHPOINT
) {
798 enum watchpoint_rw hit_wp_type
;
799 target_addr_t hit_wp_address
;
801 if (watchpoint_hit(ct
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
803 switch (hit_wp_type
) {
805 snprintf(stop_reason
, sizeof(stop_reason
),
806 "watch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
809 snprintf(stop_reason
, sizeof(stop_reason
),
810 "rwatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
813 snprintf(stop_reason
, sizeof(stop_reason
),
814 "awatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
822 current_thread
[0] = '\0';
824 snprintf(current_thread
, sizeof(current_thread
), "thread:%" PRIx64
";",
825 target
->rtos
->current_thread
);
827 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s%s",
828 signal_var
, stop_reason
, current_thread
);
830 gdb_connection
->ctrl_c
= false;
833 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
834 gdb_connection
->frontend_state
= TARGET_HALTED
;
837 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
839 struct gdb_connection
*gdb_connection
= connection
->priv
;
840 char fileio_command
[256];
842 bool program_exited
= false;
844 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
845 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
846 target
->fileio_info
->param_1
,
847 target
->fileio_info
->param_2
+ 1, /* len + trailing zero */
848 target
->fileio_info
->param_3
,
849 target
->fileio_info
->param_4
);
850 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
851 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
852 target
->fileio_info
->param_1
);
853 else if (strcmp(target
->fileio_info
->identifier
, "read") == 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
, "write") == 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
, "lseek") == 0)
864 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
865 target
->fileio_info
->param_1
,
866 target
->fileio_info
->param_2
,
867 target
->fileio_info
->param_3
);
868 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
869 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
870 target
->fileio_info
->param_1
,
871 target
->fileio_info
->param_2
+ 1, /* len + trailing zero */
872 target
->fileio_info
->param_3
,
873 target
->fileio_info
->param_4
+ 1); /* len + trailing zero */
874 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
875 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
876 target
->fileio_info
->param_1
,
877 target
->fileio_info
->param_2
+ 1); /* len + trailing zero */
878 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
879 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
880 target
->fileio_info
->param_1
,
881 target
->fileio_info
->param_2
,
882 target
->fileio_info
->param_3
);
883 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
884 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
885 target
->fileio_info
->param_1
,
886 target
->fileio_info
->param_2
);
887 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
888 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
889 target
->fileio_info
->param_1
,
890 target
->fileio_info
->param_2
);
891 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
892 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
893 target
->fileio_info
->param_1
);
894 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
895 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
896 target
->fileio_info
->param_1
,
897 target
->fileio_info
->param_2
+ 1); /* len + trailing zero */
898 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
899 /* If target hits exit syscall, report to GDB the program is terminated.
900 * In addition, let target run its own exit syscall handler. */
901 program_exited
= true;
902 sprintf(fileio_command
, "W%02" PRIx64
, target
->fileio_info
->param_1
);
904 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
906 /* encounter unknown syscall, continue */
907 gdb_connection
->frontend_state
= TARGET_RUNNING
;
908 target_resume(target
, 1, 0x0, 0, 0);
912 command_len
= strlen(fileio_command
);
913 gdb_put_packet(connection
, fileio_command
, command_len
);
915 if (program_exited
) {
916 /* Use target_resume() to let target run its own exit syscall handler. */
917 gdb_connection
->frontend_state
= TARGET_RUNNING
;
918 target_resume(target
, 1, 0x0, 0, 0);
920 gdb_connection
->frontend_state
= TARGET_HALTED
;
921 rtos_update_threads(target
);
925 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
927 struct gdb_connection
*gdb_connection
= connection
->priv
;
929 /* In the GDB protocol when we are stepping or continuing execution,
930 * we have a lingering reply. Upon receiving a halted event
931 * when we have that lingering packet, we reply to the original
932 * step or continue packet.
934 * Executing monitor commands can bring the target in and
935 * out of the running state so we'll see lots of TARGET_EVENT_XXX
936 * that are to be ignored.
938 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
939 /* stop forwarding log packets! */
940 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
942 /* check fileio first */
943 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
944 gdb_fileio_reply(target
, connection
);
946 gdb_signal_reply(target
, connection
);
950 static int gdb_target_callback_event_handler(struct target
*target
,
951 enum target_event event
, void *priv
)
953 struct connection
*connection
= priv
;
954 struct gdb_service
*gdb_service
= connection
->service
->priv
;
956 if (gdb_service
->target
!= target
)
960 case TARGET_EVENT_GDB_HALT
:
961 gdb_frontend_halted(target
, connection
);
963 case TARGET_EVENT_HALTED
:
964 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
973 static int gdb_new_connection(struct connection
*connection
)
975 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
976 struct target
*target
;
979 static unsigned int next_unique_id
= 1;
981 target
= get_target_from_connection(connection
);
982 connection
->priv
= gdb_connection
;
983 connection
->cmd_ctx
->current_target
= target
;
985 /* initialize gdb connection information */
986 gdb_connection
->buf_p
= gdb_connection
->buffer
;
987 gdb_connection
->buf_cnt
= 0;
988 gdb_connection
->ctrl_c
= false;
989 gdb_connection
->frontend_state
= TARGET_HALTED
;
990 gdb_connection
->vflash_image
= NULL
;
991 gdb_connection
->closed
= false;
992 gdb_connection
->busy
= false;
993 gdb_connection
->noack_mode
= 0;
994 gdb_connection
->sync
= false;
995 gdb_connection
->mem_write_error
= false;
996 gdb_connection
->attached
= true;
997 gdb_connection
->extended_protocol
= false;
998 gdb_connection
->target_desc
.tdesc
= NULL
;
999 gdb_connection
->target_desc
.tdesc_length
= 0;
1000 gdb_connection
->thread_list
= NULL
;
1001 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
1002 gdb_connection
->unique_index
= next_unique_id
++;
1004 /* send ACK to GDB for debug request */
1005 gdb_write(connection
, "+", 1);
1007 /* output goes through gdb connection */
1008 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
1010 /* we must remove all breakpoints registered to the target as a previous
1011 * GDB session could leave dangling breakpoints if e.g. communication
1014 breakpoint_clear_target(target
);
1015 watchpoint_clear_target(target
);
1017 /* Since version 3.95 (gdb-19990504), with the exclusion of 6.5~6.8, GDB
1018 * sends an ACK at connection with the following comment in its source code:
1019 * "Ack any packet which the remote side has already sent."
1020 * LLDB does the same since the first gdb-remote implementation.
1021 * Remove the initial ACK from the incoming buffer.
1023 retval
= gdb_get_char(connection
, &initial_ack
);
1024 if (retval
!= ERROR_OK
)
1027 if (initial_ack
!= '+')
1028 gdb_putback_char(connection
, initial_ack
);
1030 target_call_event_callbacks(target
, TARGET_EVENT_GDB_ATTACH
);
1033 /* clean previous rtos session if supported*/
1034 if (target
->rtos
->type
->clean
)
1035 target
->rtos
->type
->clean(target
);
1037 /* update threads */
1038 rtos_update_threads(target
);
1041 if (gdb_use_memory_map
) {
1042 /* Connect must fail if the memory map can't be set up correctly.
1044 * This will cause an auto_probe to be invoked, which is either
1045 * a no-op or it will fail when the target isn't ready(e.g. not halted).
1047 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1048 struct flash_bank
*p
;
1049 p
= get_flash_bank_by_num_noprobe(i
);
1050 if (p
->target
!= target
)
1052 retval
= get_flash_bank_by_num(i
, &p
);
1053 if (retval
!= ERROR_OK
) {
1054 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target "
1055 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1061 log_printf_lf(all_targets
->next
? LOG_LVL_INFO
: LOG_LVL_DEBUG
,
1062 __FILE__
, __LINE__
, __func__
,
1063 "New GDB Connection: %d, Target %s, state: %s",
1064 gdb_connection
->unique_index
,
1065 target_name(target
),
1066 target_state_name(target
));
1068 if (!target_was_examined(target
)) {
1069 LOG_ERROR("Target %s not examined yet, refuse gdb connection %d!",
1070 target_name(target
), gdb_connection
->unique_index
);
1071 return ERROR_TARGET_NOT_EXAMINED
;
1073 gdb_actual_connections
++;
1075 if (target
->state
!= TARGET_HALTED
)
1076 LOG_WARNING("GDB connection %d on target %s not halted",
1077 gdb_actual_connections
, target_name(target
));
1079 /* DANGER! If we fail subsequently, we must remove this handler,
1080 * otherwise we occasionally see crashes as the timer can invoke the
1083 * register callback to be informed about target events */
1084 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1086 log_add_callback(gdb_log_callback
, connection
);
1091 static int gdb_connection_closed(struct connection
*connection
)
1093 struct target
*target
;
1094 struct gdb_connection
*gdb_connection
= connection
->priv
;
1096 target
= get_target_from_connection(connection
);
1098 /* we're done forwarding messages. Tear down callback before
1099 * cleaning up connection.
1101 log_remove_callback(gdb_log_callback
, connection
);
1103 gdb_actual_connections
--;
1104 LOG_DEBUG("{%d} GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1105 gdb_connection
->unique_index
,
1106 target_name(target
),
1107 target_state_name(target
),
1108 gdb_actual_connections
);
1110 /* see if an image built with vFlash commands is left */
1111 if (gdb_connection
->vflash_image
) {
1112 image_close(gdb_connection
->vflash_image
);
1113 free(gdb_connection
->vflash_image
);
1114 gdb_connection
->vflash_image
= NULL
;
1117 /* if this connection registered a debug-message receiver delete it */
1118 delete_debug_msg_receiver(connection
->cmd_ctx
, target
);
1120 free(connection
->priv
);
1121 connection
->priv
= NULL
;
1123 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1125 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
1127 target_call_event_callbacks(target
, TARGET_EVENT_GDB_DETACH
);
1132 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1135 snprintf(err
, 4, "E%2.2X", the_error
);
1136 gdb_put_packet(connection
, err
, 3);
1139 static int gdb_last_signal_packet(struct connection
*connection
,
1140 char const *packet
, int packet_size
)
1142 struct target
*target
= get_target_from_connection(connection
);
1143 struct gdb_connection
*gdb_con
= connection
->priv
;
1147 if (!gdb_con
->attached
) {
1148 /* if we are here we have received a kill packet
1149 * reply W stop reply otherwise gdb gets very unhappy */
1150 gdb_put_packet(connection
, "W00", 3);
1154 signal_var
= gdb_last_signal(target
);
1156 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1157 gdb_put_packet(connection
, sig_reply
, 3);
1162 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1164 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1167 return len
- 1 - pos
;
1170 /* Convert register to string of bytes. NB! The # of bits in the
1171 * register might be non-divisible by 8(a byte), in which
1172 * case an entire byte is shown.
1174 * NB! the format on the wire is the target endianness
1176 * The format of reg->value is little endian
1179 static void gdb_str_to_target(struct target
*target
,
1180 char *tstr
, struct reg
*reg
)
1187 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1189 for (i
= 0; i
< buf_len
; i
++) {
1190 int j
= gdb_reg_pos(target
, i
, buf_len
);
1191 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1195 /* copy over in register buffer */
1196 static void gdb_target_to_reg(struct target
*target
,
1197 char const *tstr
, int str_len
, uint8_t *bin
)
1200 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1205 for (i
= 0; i
< str_len
; i
+= 2) {
1207 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1208 LOG_ERROR("BUG: unable to convert register value");
1212 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1217 /* get register value if needed and fill the buffer accordingly */
1218 static int gdb_get_reg_value_as_str(struct target
*target
, char *tstr
, struct reg
*reg
)
1220 int retval
= ERROR_OK
;
1223 retval
= reg
->type
->get(reg
);
1225 const unsigned int len
= DIV_ROUND_UP(reg
->size
, 8) * 2;
1228 gdb_str_to_target(target
, tstr
, reg
);
1230 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE
:
1231 memset(tstr
, 'x', len
);
1238 static int gdb_get_registers_packet(struct connection
*connection
,
1239 char const *packet
, int packet_size
)
1241 struct target
*target
= get_target_from_connection(connection
);
1242 struct reg
**reg_list
;
1245 int reg_packet_size
= 0;
1250 #ifdef _DEBUG_GDB_IO_
1254 if ((target
->rtos
) && (rtos_get_gdb_reg_list(connection
) == ERROR_OK
))
1257 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1259 if (retval
!= ERROR_OK
)
1260 return gdb_error(connection
, retval
);
1262 for (i
= 0; i
< reg_list_size
; i
++) {
1263 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1265 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1268 assert(reg_packet_size
> 0);
1270 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1274 reg_packet_p
= reg_packet
;
1276 for (i
= 0; i
< reg_list_size
; i
++) {
1277 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1279 if (gdb_get_reg_value_as_str(target
, reg_packet_p
, reg_list
[i
]) != ERROR_OK
) {
1282 return gdb_error(connection
, retval
);
1284 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1287 #ifdef _DEBUG_GDB_IO_
1289 char *reg_packet_p_debug
;
1290 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1291 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1292 free(reg_packet_p_debug
);
1296 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1304 static int gdb_set_registers_packet(struct connection
*connection
,
1305 char const *packet
, int packet_size
)
1307 struct target
*target
= get_target_from_connection(connection
);
1309 struct reg
**reg_list
;
1312 char const *packet_p
;
1314 #ifdef _DEBUG_GDB_IO_
1318 /* skip command character */
1322 if (packet_size
% 2) {
1323 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1324 return ERROR_SERVER_REMOTE_CLOSED
;
1327 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1329 if (retval
!= ERROR_OK
)
1330 return gdb_error(connection
, retval
);
1333 for (i
= 0; i
< reg_list_size
; i
++) {
1335 if (!reg_list
[i
] || !reg_list
[i
]->exist
|| reg_list
[i
]->hidden
)
1337 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1339 if (packet_p
+ chars
> packet
+ packet_size
)
1340 LOG_ERROR("BUG: register packet is too small for registers");
1342 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1343 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1345 retval
= reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1346 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1347 LOG_DEBUG("Couldn't set register %s.", reg_list
[i
]->name
);
1350 return gdb_error(connection
, retval
);
1353 /* advance packet pointer */
1359 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1362 gdb_put_packet(connection
, "OK", 2);
1367 static int gdb_get_register_packet(struct connection
*connection
,
1368 char const *packet
, int packet_size
)
1370 struct target
*target
= get_target_from_connection(connection
);
1372 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1373 struct reg
**reg_list
;
1377 #ifdef _DEBUG_GDB_IO_
1381 if ((target
->rtos
) && (rtos_get_gdb_reg(connection
, reg_num
) == ERROR_OK
))
1384 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1386 if (retval
!= ERROR_OK
)
1387 return gdb_error(connection
, retval
);
1389 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1390 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1391 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1392 return ERROR_SERVER_REMOTE_CLOSED
;
1395 reg_packet
= calloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1, 1); /* plus one for string termination null */
1397 if (gdb_get_reg_value_as_str(target
, reg_packet
, reg_list
[reg_num
]) != ERROR_OK
) {
1400 return gdb_error(connection
, retval
);
1403 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1411 static int gdb_set_register_packet(struct connection
*connection
,
1412 char const *packet
, int packet_size
)
1414 struct target
*target
= get_target_from_connection(connection
);
1416 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1417 struct reg
**reg_list
;
1421 #ifdef _DEBUG_GDB_IO_
1425 if (*separator
!= '=') {
1426 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1427 return ERROR_SERVER_REMOTE_CLOSED
;
1429 size_t chars
= strlen(separator
+ 1);
1430 uint8_t *bin_buf
= malloc(chars
/ 2);
1431 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1433 if ((target
->rtos
) &&
1434 (rtos_set_reg(connection
, reg_num
, bin_buf
) == ERROR_OK
)) {
1436 gdb_put_packet(connection
, "OK", 2);
1440 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1442 if (retval
!= ERROR_OK
) {
1444 return gdb_error(connection
, retval
);
1447 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1448 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1449 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1452 return ERROR_SERVER_REMOTE_CLOSED
;
1455 if (chars
!= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2)) {
1456 LOG_ERROR("gdb sent %zu bits for a %" PRIu32
"-bit register (%s)",
1457 chars
* 4, reg_list
[reg_num
]->size
, reg_list
[reg_num
]->name
);
1460 return ERROR_SERVER_REMOTE_CLOSED
;
1463 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1465 retval
= reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1466 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1467 LOG_DEBUG("Couldn't set register %s.", reg_list
[reg_num
]->name
);
1470 return gdb_error(connection
, retval
);
1473 gdb_put_packet(connection
, "OK", 2);
1481 /* No attempt is made to translate the "retval" to
1482 * GDB speak. This has to be done at the calling
1483 * site as no mapping really exists.
1485 static int gdb_error(struct connection
*connection
, int retval
)
1487 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1488 gdb_send_error(connection
, EFAULT
);
1492 /* We don't have to worry about the default 2 second timeout for GDB packets,
1493 * because GDB breaks up large memory reads into smaller reads.
1495 static int gdb_read_memory_packet(struct connection
*connection
,
1496 char const *packet
, int packet_size
)
1498 struct target
*target
= get_target_from_connection(connection
);
1506 int retval
= ERROR_OK
;
1508 /* skip command character */
1511 addr
= strtoull(packet
, &separator
, 16);
1513 if (*separator
!= ',') {
1514 LOG_ERROR("incomplete read memory packet received, dropping connection");
1515 return ERROR_SERVER_REMOTE_CLOSED
;
1518 len
= strtoul(separator
+ 1, NULL
, 16);
1521 LOG_WARNING("invalid read memory packet received (len == 0)");
1522 gdb_put_packet(connection
, "", 0);
1526 buffer
= malloc(len
);
1528 LOG_DEBUG("addr: 0x%16.16" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1530 retval
= ERROR_NOT_IMPLEMENTED
;
1532 retval
= rtos_read_buffer(target
, addr
, len
, buffer
);
1533 if (retval
== ERROR_NOT_IMPLEMENTED
)
1534 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1536 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1537 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1538 * At some point this might be fixed in GDB, in which case this code can be removed.
1540 * OpenOCD developers are acutely aware of this problem, but there is nothing
1541 * gained by involving the user in this problem that hopefully will get resolved
1544 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1545 * cmd = view%20audit-trail&database = gdb&pr = 2395
1547 * For now, the default is to fix up things to make current GDB versions work.
1548 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1550 memset(buffer
, 0, len
);
1554 if (retval
== ERROR_OK
) {
1555 hex_buffer
= malloc(len
* 2 + 1);
1557 size_t pkt_len
= hexify(hex_buffer
, buffer
, len
, len
* 2 + 1);
1559 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1563 retval
= gdb_error(connection
, retval
);
1570 static int gdb_write_memory_packet(struct connection
*connection
,
1571 char const *packet
, int packet_size
)
1573 struct target
*target
= get_target_from_connection(connection
);
1581 /* skip command character */
1584 addr
= strtoull(packet
, &separator
, 16);
1586 if (*separator
!= ',') {
1587 LOG_ERROR("incomplete write memory packet received, dropping connection");
1588 return ERROR_SERVER_REMOTE_CLOSED
;
1591 len
= strtoul(separator
+ 1, &separator
, 16);
1593 if (*(separator
++) != ':') {
1594 LOG_ERROR("incomplete write memory packet received, dropping connection");
1595 return ERROR_SERVER_REMOTE_CLOSED
;
1598 buffer
= malloc(len
);
1600 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1602 if (unhexify(buffer
, separator
, len
) != len
)
1603 LOG_ERROR("unable to decode memory packet");
1605 retval
= ERROR_NOT_IMPLEMENTED
;
1607 retval
= rtos_write_buffer(target
, addr
, len
, buffer
);
1608 if (retval
== ERROR_NOT_IMPLEMENTED
)
1609 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1611 if (retval
== ERROR_OK
)
1612 gdb_put_packet(connection
, "OK", 2);
1614 retval
= gdb_error(connection
, retval
);
1621 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1622 char const *packet
, int packet_size
)
1624 struct target
*target
= get_target_from_connection(connection
);
1629 int retval
= ERROR_OK
;
1630 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1631 * the assumption that we're in a download and it's important to go as fast
1633 uint32_t fast_limit
= 8;
1635 /* skip command character */
1638 addr
= strtoull(packet
, &separator
, 16);
1640 if (*separator
!= ',') {
1641 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1642 return ERROR_SERVER_REMOTE_CLOSED
;
1645 len
= strtoul(separator
+ 1, &separator
, 16);
1647 if (*(separator
++) != ':') {
1648 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1649 return ERROR_SERVER_REMOTE_CLOSED
;
1652 struct gdb_connection
*gdb_connection
= connection
->priv
;
1654 if (gdb_connection
->mem_write_error
)
1655 retval
= ERROR_FAIL
;
1657 if (retval
== ERROR_OK
) {
1658 if (len
>= fast_limit
) {
1659 /* By replying the packet *immediately* GDB will send us a new packet
1660 * while we write the last one to the target.
1661 * We only do this for larger writes, so that users who do something like:
1662 * p *((int*)0xdeadbeef)=8675309
1663 * will get immediate feedback that that write failed.
1665 gdb_put_packet(connection
, "OK", 2);
1668 retval
= gdb_error(connection
, retval
);
1669 /* now that we have reported the memory write error, we can clear the condition */
1670 gdb_connection
->mem_write_error
= false;
1671 if (retval
!= ERROR_OK
)
1676 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1678 retval
= ERROR_NOT_IMPLEMENTED
;
1680 retval
= rtos_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1681 if (retval
== ERROR_NOT_IMPLEMENTED
)
1682 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1684 if (retval
!= ERROR_OK
)
1685 gdb_connection
->mem_write_error
= true;
1688 if (len
< fast_limit
) {
1689 if (retval
!= ERROR_OK
) {
1690 gdb_error(connection
, retval
);
1691 gdb_connection
->mem_write_error
= false;
1693 gdb_put_packet(connection
, "OK", 2);
1700 static int gdb_step_continue_packet(struct connection
*connection
,
1701 char const *packet
, int packet_size
)
1703 struct target
*target
= get_target_from_connection(connection
);
1705 uint64_t address
= 0x0;
1706 int retval
= ERROR_OK
;
1710 if (packet_size
> 1)
1711 address
= strtoull(packet
+ 1, NULL
, 16);
1715 gdb_running_type
= packet
[0];
1716 if (packet
[0] == 'c') {
1717 LOG_DEBUG("continue");
1718 /* resume at current address, don't handle breakpoints, not debugging */
1719 retval
= target_resume(target
, current
, address
, 0, 0);
1720 } else if (packet
[0] == 's') {
1722 /* step at current or address, don't handle breakpoints */
1723 retval
= target_step(target
, current
, address
, 0);
1728 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1729 char const *packet
, int packet_size
)
1731 struct target
*target
= get_target_from_connection(connection
);
1733 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1734 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1740 LOG_DEBUG("[%s]", target_name(target
));
1742 type
= strtoul(packet
+ 1, &separator
, 16);
1744 if (type
== 0) /* memory breakpoint */
1745 bp_type
= BKPT_SOFT
;
1746 else if (type
== 1) /* hardware breakpoint */
1747 bp_type
= BKPT_HARD
;
1748 else if (type
== 2) /* write watchpoint */
1749 wp_type
= WPT_WRITE
;
1750 else if (type
== 3) /* read watchpoint */
1752 else if (type
== 4) /* access watchpoint */
1753 wp_type
= WPT_ACCESS
;
1755 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1756 return ERROR_SERVER_REMOTE_CLOSED
;
1759 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1760 bp_type
= gdb_breakpoint_override_type
;
1762 if (*separator
!= ',') {
1763 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1764 return ERROR_SERVER_REMOTE_CLOSED
;
1767 address
= strtoull(separator
+ 1, &separator
, 16);
1769 if (*separator
!= ',') {
1770 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1771 return ERROR_SERVER_REMOTE_CLOSED
;
1774 size
= strtoul(separator
+ 1, &separator
, 16);
1779 if (packet
[0] == 'Z') {
1780 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1781 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1782 /* Send empty reply to report that breakpoints of this type are not supported */
1783 gdb_put_packet(connection
, "", 0);
1784 } else if (retval
!= ERROR_OK
) {
1785 retval
= gdb_error(connection
, retval
);
1786 if (retval
!= ERROR_OK
)
1789 gdb_put_packet(connection
, "OK", 2);
1791 breakpoint_remove(target
, address
);
1792 gdb_put_packet(connection
, "OK", 2);
1799 if (packet
[0] == 'Z') {
1800 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, WATCHPOINT_IGNORE_DATA_VALUE_MASK
);
1801 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1802 /* Send empty reply to report that watchpoints of this type are not supported */
1803 gdb_put_packet(connection
, "", 0);
1804 } else if (retval
!= ERROR_OK
) {
1805 retval
= gdb_error(connection
, retval
);
1806 if (retval
!= ERROR_OK
)
1809 gdb_put_packet(connection
, "OK", 2);
1811 watchpoint_remove(target
, address
);
1812 gdb_put_packet(connection
, "OK", 2);
1823 /* print out a string and allocate more space as needed,
1824 * mainly used for XML at this point
1826 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 5, 6))) void xml_printf(int *retval
,
1827 char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1829 if (*retval
!= ERROR_OK
)
1834 if ((!*xml
) || (!first
)) {
1835 /* start by 0 to exercise all the code paths.
1836 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1838 *size
= *size
* 2 + 2;
1840 *xml
= realloc(*xml
, *size
);
1843 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1851 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1853 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1857 /* there was just enough or not enough space, allocate more. */
1862 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1864 /* Locate the annex. */
1865 const char *annex_end
= strchr(buf
, ':');
1869 /* After the read marker and annex, qXfer looks like a
1870 * traditional 'm' packet. */
1872 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1874 if (*separator
!= ',')
1877 *len
= strtoul(separator
+ 1, NULL
, 16);
1879 /* Extract the annex if needed */
1881 *annex
= strndup(buf
, annex_end
- buf
);
1889 static int compare_bank(const void *a
, const void *b
)
1891 struct flash_bank
*b1
, *b2
;
1892 b1
= *((struct flash_bank
**)a
);
1893 b2
= *((struct flash_bank
**)b
);
1895 if (b1
->base
== b2
->base
)
1897 else if (b1
->base
> b2
->base
)
1903 static int gdb_memory_map(struct connection
*connection
,
1904 char const *packet
, int packet_size
)
1906 /* We get away with only specifying flash here. Regions that are not
1907 * specified are treated as if we provided no memory map(if not we
1908 * could detect the holes and mark them as RAM).
1909 * Normally we only execute this code once, but no big deal if we
1910 * have to regenerate it a couple of times.
1913 struct target
*target
= get_target_from_connection(connection
);
1914 struct flash_bank
*p
;
1918 int retval
= ERROR_OK
;
1919 struct flash_bank
**banks
;
1923 target_addr_t ram_start
= 0;
1924 unsigned int target_flash_banks
= 0;
1926 /* skip command character */
1929 offset
= strtoul(packet
, &separator
, 16);
1930 length
= strtoul(separator
+ 1, &separator
, 16);
1932 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1934 /* Sort banks in ascending order. We need to report non-flash
1935 * memory as ram (or rather read/write) by default for GDB, since
1936 * it has no concept of non-cacheable read/write memory (i/o etc).
1938 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1940 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1941 p
= get_flash_bank_by_num_noprobe(i
);
1942 if (p
->target
!= target
)
1944 retval
= get_flash_bank_by_num(i
, &p
);
1945 if (retval
!= ERROR_OK
) {
1947 gdb_error(connection
, retval
);
1950 banks
[target_flash_banks
++] = p
;
1953 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1956 for (unsigned int i
= 0; i
< target_flash_banks
; i
++) {
1957 unsigned sector_size
= 0;
1958 unsigned group_len
= 0;
1962 if (ram_start
< p
->base
)
1963 xml_printf(&retval
, &xml
, &pos
, &size
,
1964 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1965 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
1966 ram_start
, p
->base
- ram_start
);
1968 /* Report adjacent groups of same-size sectors. So for
1969 * example top boot CFI flash will list an initial region
1970 * with several large sectors (maybe 128KB) and several
1971 * smaller ones at the end (maybe 32KB). STR7 will have
1972 * regions with 8KB, 32KB, and 64KB sectors; etc.
1974 for (unsigned int j
= 0; j
< p
->num_sectors
; j
++) {
1976 /* Maybe start a new group of sectors. */
1977 if (sector_size
== 0) {
1978 if (p
->sectors
[j
].offset
+ p
->sectors
[j
].size
> p
->size
) {
1979 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1980 " overflows the end of %s bank.",
1981 p
->sectors
[j
].offset
, p
->name
);
1982 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1985 target_addr_t start
;
1986 start
= p
->base
+ p
->sectors
[j
].offset
;
1987 xml_printf(&retval
, &xml
, &pos
, &size
,
1988 "<memory type=\"flash\" "
1989 "start=\"" TARGET_ADDR_FMT
"\" ",
1991 sector_size
= p
->sectors
[j
].size
;
1992 group_len
= sector_size
;
1994 group_len
+= sector_size
; /* equal to p->sectors[j].size */
1997 /* Does this finish a group of sectors?
1998 * If not, continue an already-started group.
2000 if (j
< p
->num_sectors
- 1
2001 && p
->sectors
[j
+ 1].size
== sector_size
2002 && p
->sectors
[j
+ 1].offset
== p
->sectors
[j
].offset
+ sector_size
2003 && p
->sectors
[j
+ 1].offset
+ p
->sectors
[j
+ 1].size
<= p
->size
)
2006 xml_printf(&retval
, &xml
, &pos
, &size
,
2007 "length=\"0x%x\">\n"
2008 "<property name=\"blocksize\">"
2016 ram_start
= p
->base
+ p
->size
;
2020 xml_printf(&retval
, &xml
, &pos
, &size
,
2021 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
2022 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
2023 ram_start
, target_address_max(target
) - ram_start
+ 1);
2024 /* ELSE a flash chip could be at the very end of the address space, in
2025 * which case ram_start will be precisely 0 */
2029 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
2031 if (retval
!= ERROR_OK
) {
2033 gdb_error(connection
, retval
);
2037 if (offset
+ length
> pos
)
2038 length
= pos
- offset
;
2040 char *t
= malloc(length
+ 1);
2042 memcpy(t
+ 1, xml
+ offset
, length
);
2043 gdb_put_packet(connection
, t
, length
+ 1);
2050 static const char *gdb_get_reg_type_name(enum reg_type type
)
2059 case REG_TYPE_INT16
:
2061 case REG_TYPE_INT32
:
2063 case REG_TYPE_INT64
:
2065 case REG_TYPE_INT128
:
2069 case REG_TYPE_UINT8
:
2071 case REG_TYPE_UINT16
:
2073 case REG_TYPE_UINT32
:
2075 case REG_TYPE_UINT64
:
2077 case REG_TYPE_UINT128
:
2079 case REG_TYPE_CODE_PTR
:
2081 case REG_TYPE_DATA_PTR
:
2083 case REG_TYPE_FLOAT
:
2085 case REG_TYPE_IEEE_SINGLE
:
2086 return "ieee_single";
2087 case REG_TYPE_IEEE_DOUBLE
:
2088 return "ieee_double";
2089 case REG_TYPE_ARCH_DEFINED
:
2090 return "int"; /* return arbitrary string to avoid compile warning. */
2093 return "int"; /* "int" as default value */
2096 static int lookup_add_arch_defined_types(char const **arch_defined_types_list
[], const char *type_id
,
2097 int *num_arch_defined_types
)
2099 int tbl_sz
= *num_arch_defined_types
;
2101 if (type_id
&& (strcmp(type_id
, ""))) {
2102 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2103 if (!((*arch_defined_types_list
)[j
])) {
2104 (*arch_defined_types_list
)[tbl_sz
++] = type_id
;
2105 *arch_defined_types_list
= realloc(*arch_defined_types_list
,
2106 sizeof(char *) * (tbl_sz
+ 1));
2107 (*arch_defined_types_list
)[tbl_sz
] = NULL
;
2108 *num_arch_defined_types
= tbl_sz
;
2111 if (!strcmp((*arch_defined_types_list
)[j
], type_id
))
2120 static int gdb_generate_reg_type_description(struct target
*target
,
2121 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
,
2122 char const **arch_defined_types_list
[], int *num_arch_defined_types
)
2124 int retval
= ERROR_OK
;
2126 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
2127 struct reg_data_type
*data_type
= type
->reg_type_vector
->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
);
2135 /* <vector id="id" type="type" count="count"/> */
2136 xml_printf(&retval
, tdesc
, pos
, size
,
2137 "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32
"\"/>\n",
2138 type
->id
, type
->reg_type_vector
->type
->id
,
2139 type
->reg_type_vector
->count
);
2141 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
2142 struct reg_data_type_union_field
*field
;
2143 field
= type
->reg_type_union
->fields
;
2145 struct reg_data_type
*data_type
= field
->type
;
2146 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2147 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2148 num_arch_defined_types
))
2149 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2150 arch_defined_types_list
,
2151 num_arch_defined_types
);
2154 field
= field
->next
;
2157 * <field name="name" type="type"/> ...
2159 xml_printf(&retval
, tdesc
, pos
, size
,
2160 "<union id=\"%s\">\n",
2163 field
= type
->reg_type_union
->fields
;
2165 xml_printf(&retval
, tdesc
, pos
, size
,
2166 "<field name=\"%s\" type=\"%s\"/>\n",
2167 field
->name
, field
->type
->id
);
2169 field
= field
->next
;
2172 xml_printf(&retval
, tdesc
, pos
, size
,
2175 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
2176 struct reg_data_type_struct_field
*field
;
2177 field
= type
->reg_type_struct
->fields
;
2179 if (field
->use_bitfields
) {
2180 /* <struct id="id" size="size">
2181 * <field name="name" start="start" end="end"/> ...
2183 xml_printf(&retval
, tdesc
, pos
, size
,
2184 "<struct id=\"%s\" size=\"%" PRIu32
"\">\n",
2185 type
->id
, type
->reg_type_struct
->size
);
2187 xml_printf(&retval
, tdesc
, pos
, size
,
2188 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2189 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2190 gdb_get_reg_type_name(field
->bitfield
->type
));
2192 field
= field
->next
;
2196 struct reg_data_type
*data_type
= field
->type
;
2197 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2198 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2199 num_arch_defined_types
))
2200 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2201 arch_defined_types_list
,
2202 num_arch_defined_types
);
2207 * <field name="name" type="type"/> ...
2209 xml_printf(&retval
, tdesc
, pos
, size
,
2210 "<struct id=\"%s\">\n",
2213 xml_printf(&retval
, tdesc
, pos
, size
,
2214 "<field name=\"%s\" type=\"%s\"/>\n",
2215 field
->name
, field
->type
->id
);
2217 field
= field
->next
;
2221 xml_printf(&retval
, tdesc
, pos
, size
,
2224 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2225 /* <flags id="id" size="size">
2226 * <field name="name" start="start" end="end"/> ...
2228 xml_printf(&retval
, tdesc
, pos
, size
,
2229 "<flags id=\"%s\" size=\"%" PRIu32
"\">\n",
2230 type
->id
, type
->reg_type_flags
->size
);
2232 struct reg_data_type_flags_field
*field
;
2233 field
= type
->reg_type_flags
->fields
;
2235 xml_printf(&retval
, tdesc
, pos
, size
,
2236 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2237 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2238 gdb_get_reg_type_name(field
->bitfield
->type
));
2240 field
= field
->next
;
2243 xml_printf(&retval
, tdesc
, pos
, size
,
2251 /* Get a list of available target registers features. feature_list must
2252 * be freed by caller.
2254 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2255 struct reg
**reg_list
, int reg_list_size
)
2259 /* Start with only one element */
2260 *feature_list
= calloc(1, sizeof(char *));
2262 for (int i
= 0; i
< reg_list_size
; i
++) {
2263 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2266 if (reg_list
[i
]->feature
2267 && reg_list
[i
]->feature
->name
2268 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2269 /* We found a feature, check if the feature is already in the
2270 * table. If not, allocate a new entry for the table and
2271 * put the new feature in it.
2273 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2274 if (!((*feature_list
)[j
])) {
2275 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2276 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2277 (*feature_list
)[tbl_sz
] = NULL
;
2280 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2287 if (feature_list_size
)
2288 *feature_list_size
= tbl_sz
;
2293 /* Create a register list that's the union of all the registers of the SMP
2294 * group this target is in. If the target is not part of an SMP group, this
2295 * returns the same as target_get_gdb_reg_list_noread().
2297 static int smp_reg_list_noread(struct target
*target
,
2298 struct reg
**combined_list
[], int *combined_list_size
,
2299 enum target_register_class reg_class
)
2302 return target_get_gdb_reg_list_noread(target
, combined_list
,
2303 combined_list_size
, REG_CLASS_ALL
);
2305 unsigned int combined_allocated
= 256;
2306 struct reg
**local_list
= malloc(combined_allocated
* sizeof(struct reg
*));
2308 LOG_ERROR("malloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2311 unsigned int local_list_size
= 0;
2313 struct target_list
*head
;
2314 foreach_smp_target(head
, target
->smp_targets
) {
2315 if (!target_was_examined(head
->target
))
2318 struct reg
**reg_list
= NULL
;
2320 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2321 ®_list_size
, reg_class
);
2322 if (result
!= ERROR_OK
) {
2326 for (int i
= 0; i
< reg_list_size
; i
++) {
2328 struct reg
*a
= reg_list
[i
];
2330 /* Nested loop makes this O(n^2), but this entire function with
2331 * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2333 for (unsigned int j
= 0; j
< local_list_size
; j
++) {
2334 struct reg
*b
= local_list
[j
];
2335 if (!strcmp(a
->name
, b
->name
)) {
2337 if (a
->size
!= b
->size
) {
2338 LOG_ERROR("SMP register %s is %d bits on one "
2339 "target, but %d bits on another target.",
2340 a
->name
, a
->size
, b
->size
);
2349 LOG_DEBUG("[%s] %s not found in combined list", target_name(target
), a
->name
);
2350 if (local_list_size
>= combined_allocated
) {
2351 combined_allocated
*= 2;
2352 local_list
= realloc(local_list
, combined_allocated
* sizeof(struct reg
*));
2354 LOG_ERROR("realloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2359 local_list
[local_list_size
] = a
;
2367 if (local_list_size
== 0) {
2368 LOG_ERROR("Unable to get register list");
2373 /* Now warn the user about any registers that weren't found in every target. */
2374 foreach_smp_target(head
, target
->smp_targets
) {
2375 if (!target_was_examined(head
->target
))
2378 struct reg
**reg_list
= NULL
;
2380 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2381 ®_list_size
, reg_class
);
2382 if (result
!= ERROR_OK
) {
2386 for (unsigned int i
= 0; i
< local_list_size
; i
++) {
2388 struct reg
*a
= local_list
[i
];
2389 for (int j
= 0; j
< reg_list_size
; j
++) {
2390 struct reg
*b
= reg_list
[j
];
2391 if (b
->exist
&& !strcmp(a
->name
, b
->name
)) {
2397 LOG_WARNING("Register %s does not exist in %s, which is part of an SMP group where "
2398 "this register does exist.",
2399 a
->name
, target_name(head
->target
));
2405 *combined_list
= local_list
;
2406 *combined_list_size
= local_list_size
;
2410 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2412 int retval
= ERROR_OK
;
2413 struct reg
**reg_list
= NULL
;
2415 char const *architecture
;
2416 char const **features
= NULL
;
2417 int feature_list_size
= 0;
2423 retval
= smp_reg_list_noread(target
, ®_list
, ®_list_size
,
2426 if (retval
!= ERROR_OK
) {
2427 LOG_ERROR("get register list failed");
2428 retval
= ERROR_FAIL
;
2432 if (reg_list_size
<= 0) {
2433 LOG_ERROR("get register list failed");
2434 retval
= ERROR_FAIL
;
2438 /* Get a list of available target registers features */
2439 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2440 if (retval
!= ERROR_OK
) {
2441 LOG_ERROR("Can't get the registers feature list");
2442 retval
= ERROR_FAIL
;
2446 /* If we found some features associated with registers, create sections */
2447 int current_feature
= 0;
2449 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2450 "<?xml version=\"1.0\"?>\n"
2451 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2452 "<target version=\"1.0\">\n");
2454 /* generate architecture element if supported by target */
2455 architecture
= target_get_gdb_arch(target
);
2457 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2458 "<architecture>%s</architecture>\n", architecture
);
2460 /* generate target description according to register list */
2462 while (features
[current_feature
]) {
2463 char const **arch_defined_types
= NULL
;
2464 int num_arch_defined_types
= 0;
2466 arch_defined_types
= calloc(1, sizeof(char *));
2467 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2468 "<feature name=\"%s\">\n",
2469 features
[current_feature
]);
2472 for (i
= 0; i
< reg_list_size
; i
++) {
2474 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2477 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2480 const char *type_str
;
2481 if (reg_list
[i
]->reg_data_type
) {
2482 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2483 /* generate <type... first, if there are architecture-defined types. */
2484 if (lookup_add_arch_defined_types(&arch_defined_types
,
2485 reg_list
[i
]->reg_data_type
->id
,
2486 &num_arch_defined_types
))
2487 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2488 reg_list
[i
]->reg_data_type
,
2489 &arch_defined_types
,
2490 &num_arch_defined_types
);
2492 type_str
= reg_list
[i
]->reg_data_type
->id
;
2494 /* predefined type */
2495 type_str
= gdb_get_reg_type_name(
2496 reg_list
[i
]->reg_data_type
->type
);
2499 /* Default type is "int" */
2503 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2504 "<reg name=\"%s\"", reg_list
[i
]->name
);
2505 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2506 " bitsize=\"%" PRIu32
"\"", reg_list
[i
]->size
);
2507 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2508 " regnum=\"%" PRIu32
"\"", reg_list
[i
]->number
);
2509 if (reg_list
[i
]->caller_save
)
2510 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2511 " save-restore=\"yes\"");
2513 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2514 " save-restore=\"no\"");
2516 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2517 " type=\"%s\"", type_str
);
2519 if (reg_list
[i
]->group
)
2520 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2521 " group=\"%s\"", reg_list
[i
]->group
);
2523 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2527 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2531 free(arch_defined_types
);
2535 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2542 if (retval
== ERROR_OK
)
2550 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2551 char **chunk
, int32_t offset
, uint32_t length
)
2554 LOG_ERROR("Unable to Generate Target Description");
2558 char *tdesc
= target_desc
->tdesc
;
2559 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2562 int retval
= gdb_generate_target_description(target
, &tdesc
);
2563 if (retval
!= ERROR_OK
) {
2564 LOG_ERROR("Unable to Generate Target Description");
2568 tdesc_length
= strlen(tdesc
);
2573 if (length
< (tdesc_length
- offset
))
2574 transfer_type
= 'm';
2576 transfer_type
= 'l';
2578 *chunk
= malloc(length
+ 2);
2580 LOG_ERROR("Unable to allocate memory");
2584 (*chunk
)[0] = transfer_type
;
2585 if (transfer_type
== 'm') {
2586 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2587 (*chunk
)[1 + length
] = '\0';
2589 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2590 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2592 /* After gdb-server sends out last chunk, invalidate tdesc. */
2598 target_desc
->tdesc
= tdesc
;
2599 target_desc
->tdesc_length
= tdesc_length
;
2604 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2606 int retval
= ERROR_OK
;
2607 struct reg
**reg_list
= NULL
;
2608 int reg_list_size
= 0;
2609 char const **features
= NULL
;
2610 int feature_list_size
= 0;
2612 char const *architecture
= target_get_gdb_arch(target
);
2614 retval
= target_get_gdb_reg_list_noread(target
, ®_list
,
2615 ®_list_size
, REG_CLASS_ALL
);
2616 if (retval
!= ERROR_OK
) {
2617 LOG_ERROR("get register list failed");
2621 if (reg_list_size
<= 0) {
2622 LOG_ERROR("get register list failed");
2623 retval
= ERROR_FAIL
;
2627 /* Get a list of available target registers features */
2628 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2629 if (retval
!= ERROR_OK
) {
2630 LOG_ERROR("Can't get the registers feature list");
2635 if (architecture
|| feature_list_size
)
2649 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2651 struct rtos
*rtos
= target
->rtos
;
2652 int retval
= ERROR_OK
;
2653 char *thread_list
= NULL
;
2657 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2658 "<?xml version=\"1.0\"?>\n"
2662 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2663 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2665 if (!thread_detail
->exists
)
2668 if (thread_detail
->thread_name_str
)
2669 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2670 "<thread id=\"%" PRIx64
"\" name=\"%s\">",
2671 thread_detail
->threadid
,
2672 thread_detail
->thread_name_str
);
2674 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2675 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2677 if (thread_detail
->thread_name_str
)
2678 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2679 "Name: %s", thread_detail
->thread_name_str
);
2681 if (thread_detail
->extra_info_str
) {
2682 if (thread_detail
->thread_name_str
)
2683 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2685 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2686 "%s", thread_detail
->extra_info_str
);
2689 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2694 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2697 if (retval
== ERROR_OK
)
2698 *thread_list_out
= thread_list
;
2705 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2706 char **chunk
, int32_t offset
, uint32_t length
)
2708 if (!*thread_list
) {
2709 int retval
= gdb_generate_thread_list(target
, thread_list
);
2710 if (retval
!= ERROR_OK
) {
2711 LOG_ERROR("Unable to Generate Thread List");
2716 size_t thread_list_length
= strlen(*thread_list
);
2719 length
= MIN(length
, thread_list_length
- offset
);
2720 if (length
< (thread_list_length
- offset
))
2721 transfer_type
= 'm';
2723 transfer_type
= 'l';
2725 *chunk
= malloc(length
+ 2 + 3);
2726 /* Allocating extra 3 bytes prevents false positive valgrind report
2727 * of strlen(chunk) word access:
2728 * Invalid read of size 4
2729 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2731 LOG_ERROR("Unable to allocate memory");
2735 (*chunk
)[0] = transfer_type
;
2736 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2737 (*chunk
)[1 + length
] = '\0';
2739 /* After gdb-server sends out last chunk, invalidate thread list. */
2740 if (transfer_type
== 'l') {
2742 *thread_list
= NULL
;
2748 static int gdb_query_packet(struct connection
*connection
,
2749 char const *packet
, int packet_size
)
2751 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2752 struct gdb_connection
*gdb_connection
= connection
->priv
;
2753 struct target
*target
= get_target_from_connection(connection
);
2755 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2756 if (packet_size
> 6) {
2757 Jim_Interp
*interp
= cmd_ctx
->interp
;
2759 cmd
= malloc((packet_size
- 6) / 2 + 1);
2760 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2763 /* We want to print all debug output to GDB connection */
2764 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
2765 target_call_timer_callbacks_now();
2766 /* some commands need to know the GDB connection, make note of current
2767 * GDB connection. */
2768 current_gdb_connection
= gdb_connection
;
2770 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
2771 cmd_ctx
->current_target_override
= NULL
;
2773 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
2774 Jim_DeleteAssocData(interp
, "context");
2775 int retval
= Jim_SetAssocData(interp
, "context", NULL
, cmd_ctx
);
2776 if (retval
== JIM_OK
) {
2777 retval
= Jim_EvalObj(interp
, Jim_NewStringObj(interp
, cmd
, -1));
2778 Jim_DeleteAssocData(interp
, "context");
2780 int inner_retval
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
2781 if (retval
== JIM_OK
)
2782 retval
= inner_retval
;
2784 cmd_ctx
->current_target_override
= saved_target_override
;
2786 current_gdb_connection
= NULL
;
2787 target_call_timer_callbacks_now();
2788 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
2790 if (retval
== JIM_RETURN
)
2791 retval
= interp
->returnCode
;
2793 const char *cretmsg
= Jim_GetString(Jim_GetResult(interp
), &lenmsg
);
2795 if (lenmsg
&& cretmsg
[lenmsg
- 1] != '\n') {
2796 retmsg
= alloc_printf("%s\n", cretmsg
);
2799 retmsg
= strdup(cretmsg
);
2802 return ERROR_GDB_BUFFER_TOO_SMALL
;
2804 if (retval
== JIM_OK
) {
2806 char *hex_buffer
= malloc(lenmsg
* 2 + 1);
2809 return ERROR_GDB_BUFFER_TOO_SMALL
;
2812 size_t pkt_len
= hexify(hex_buffer
, (const uint8_t *)retmsg
, lenmsg
,
2814 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
2817 gdb_put_packet(connection
, "OK", 2);
2821 gdb_output_con(connection
, retmsg
);
2822 gdb_send_error(connection
, retval
);
2827 gdb_put_packet(connection
, "OK", 2);
2829 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2830 if (packet_size
> 5) {
2835 target_addr_t addr
= 0;
2838 /* skip command character */
2841 addr
= strtoull(packet
, &separator
, 16);
2843 if (*separator
!= ',') {
2844 LOG_ERROR("incomplete read memory packet received, dropping connection");
2845 return ERROR_SERVER_REMOTE_CLOSED
;
2848 len
= strtoul(separator
+ 1, NULL
, 16);
2850 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2852 if (retval
== ERROR_OK
) {
2853 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2854 gdb_put_packet(connection
, gdb_reply
, 9);
2856 retval
= gdb_error(connection
, retval
);
2857 if (retval
!= ERROR_OK
)
2863 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2864 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2865 * qXfer:features:read is supported for some targets */
2866 int retval
= ERROR_OK
;
2867 char *buffer
= NULL
;
2870 int gdb_target_desc_supported
= 0;
2872 /* we need to test that the target supports target descriptions */
2873 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2874 if (retval
!= ERROR_OK
) {
2875 LOG_INFO("Failed detecting Target Description Support, disabling");
2876 gdb_target_desc_supported
= 0;
2879 /* support may be disabled globally */
2880 if (gdb_use_target_description
== 0) {
2881 if (gdb_target_desc_supported
)
2882 LOG_WARNING("Target Descriptions Supported, but disabled");
2883 gdb_target_desc_supported
= 0;
2890 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2892 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2893 (gdb_target_desc_supported
== 1) ? '+' : '-');
2895 if (retval
!= ERROR_OK
) {
2896 gdb_send_error(connection
, 01);
2900 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2904 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2905 && (flash_get_bank_count() > 0))
2906 return gdb_memory_map(connection
, packet
, packet_size
);
2907 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2909 int retval
= ERROR_OK
;
2912 unsigned int length
;
2914 /* skip command character */
2917 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2918 gdb_send_error(connection
, 01);
2922 /* Target should prepare correct target description for annex.
2923 * The first character of returned xml is 'm' or 'l'. 'm' for
2924 * there are *more* chunks to transfer. 'l' for it is the *last*
2925 * chunk of target description.
2927 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2928 &xml
, offset
, length
);
2929 if (retval
!= ERROR_OK
) {
2930 gdb_error(connection
, retval
);
2934 gdb_put_packet(connection
, xml
, strlen(xml
));
2938 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2940 int retval
= ERROR_OK
;
2943 unsigned int length
;
2945 /* skip command character */
2948 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2949 gdb_send_error(connection
, 01);
2953 /* Target should prepare correct thread list for annex.
2954 * The first character of returned xml is 'm' or 'l'. 'm' for
2955 * there are *more* chunks to transfer. 'l' for it is the *last*
2956 * chunk of target description.
2958 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2959 &xml
, offset
, length
);
2960 if (retval
!= ERROR_OK
) {
2961 gdb_error(connection
, retval
);
2965 gdb_put_packet(connection
, xml
, strlen(xml
));
2969 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2970 gdb_connection
->noack_mode
= 1;
2971 gdb_put_packet(connection
, "OK", 2);
2973 } else if (target
->type
->gdb_query_custom
) {
2974 char *buffer
= NULL
;
2975 int ret
= target
->type
->gdb_query_custom(target
, packet
, &buffer
);
2976 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2980 gdb_put_packet(connection
, "", 0);
2984 static bool gdb_handle_vcont_packet(struct connection
*connection
, const char *packet
,
2985 __attribute__((unused
)) int packet_size
)
2987 struct gdb_connection
*gdb_connection
= connection
->priv
;
2988 struct target
*target
= get_target_from_connection(connection
);
2989 const char *parse
= packet
;
2992 /* query for vCont supported */
2993 if (parse
[0] == '?') {
2994 if (target
->type
->step
) {
2995 /* gdb doesn't accept c without C and s without S */
2996 gdb_put_packet(connection
, "vCont;c;C;s;S", 13);
3002 if (parse
[0] == ';') {
3006 /* simple case, a continue packet */
3007 if (parse
[0] == 'c') {
3008 gdb_running_type
= 'c';
3009 LOG_DEBUG("target %s continue", target_name(target
));
3010 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3011 retval
= target_resume(target
, 1, 0, 0, 0);
3012 if (retval
== ERROR_TARGET_NOT_HALTED
)
3013 LOG_INFO("target %s was not halted when resume was requested", target_name(target
));
3015 /* poll target in an attempt to make its internal state consistent */
3016 if (retval
!= ERROR_OK
) {
3017 retval
= target_poll(target
);
3018 if (retval
!= ERROR_OK
)
3019 LOG_DEBUG("error polling target %s after failed resume", target_name(target
));
3023 * We don't report errors to gdb here, move frontend_state to
3024 * TARGET_RUNNING to stay in sync with gdb's expectation of the
3027 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3028 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3033 /* single-step or step-over-breakpoint */
3034 if (parse
[0] == 's') {
3035 gdb_running_type
= 's';
3036 bool fake_step
= false;
3038 struct target
*ct
= target
;
3042 if (parse
[0] == ':') {
3045 thread_id
= strtoll(parse
, &endp
, 16);
3054 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
3055 rtos_update_threads(target
);
3057 target
->rtos
->gdb_target_for_threadid(connection
, thread_id
, &ct
);
3060 * check if the thread to be stepped is the current rtos thread
3061 * if not, we must fake the step
3063 if (target
->rtos
->current_thread
!= thread_id
)
3067 if (parse
[0] == ';') {
3070 if (parse
[0] == 'c') {
3073 /* check if thread-id follows */
3074 if (parse
[0] == ':') {
3078 tid
= strtoll(parse
, NULL
, 16);
3079 if (tid
== thread_id
) {
3081 * Special case: only step a single thread (core),
3082 * keep the other threads halted. Currently, only
3083 * aarch64 target understands it. Other target types don't
3084 * care (nobody checks the actual value of 'current')
3085 * and it doesn't really matter. This deserves
3086 * a symbolic constant and a formal interface documentation
3089 LOG_DEBUG("request to step current core only");
3090 /* uncomment after checking that indeed other targets are safe */
3097 LOG_DEBUG("target %s single-step thread %"PRIx64
, target_name(ct
), thread_id
);
3098 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3099 target_call_event_callbacks(ct
, TARGET_EVENT_GDB_START
);
3102 * work around an annoying gdb behaviour: when the current thread
3103 * is changed in gdb, it assumes that the target can follow and also
3104 * make the thread current. This is an assumption that cannot hold
3105 * for a real target running a multi-threading OS. We just fake
3106 * the step to not trigger an internal error in gdb. See
3107 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3111 char sig_reply
[128];
3113 LOG_DEBUG("fake step thread %"PRIx64
, thread_id
);
3115 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
),
3116 "T05thread:%016"PRIx64
";", thread_id
);
3118 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
3119 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3124 /* support for gdb_sync command */
3125 if (gdb_connection
->sync
) {
3126 gdb_connection
->sync
= false;
3127 if (ct
->state
== TARGET_HALTED
) {
3128 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3129 "from the target.");
3130 gdb_sig_halted(connection
);
3131 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3133 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3137 retval
= target_step(ct
, current_pc
, 0, 0);
3138 if (retval
== ERROR_TARGET_NOT_HALTED
)
3139 LOG_INFO("target %s was not halted when step was requested", target_name(ct
));
3141 /* if step was successful send a reply back to gdb */
3142 if (retval
== ERROR_OK
) {
3143 retval
= target_poll(ct
);
3144 if (retval
!= ERROR_OK
)
3145 LOG_DEBUG("error polling target %s after successful step", target_name(ct
));
3146 /* send back signal information */
3147 gdb_signal_reply(ct
, connection
);
3148 /* stop forwarding log packets! */
3149 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3151 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3154 LOG_ERROR("Unknown vCont packet");
3158 static char *next_hex_encoded_field(const char **str
, char sep
)
3161 const char *hex
= *str
;
3165 const char *end
= strchr(hex
, sep
);
3167 hexlen
= strlen(hex
);
3170 *str
= hex
+ hexlen
+ 1;
3172 if (hexlen
% 2 != 0) {
3173 /* Malformed hex data */
3177 size_t count
= hexlen
/ 2;
3178 char *decoded
= malloc(count
+ 1);
3182 size_t converted
= unhexify((void *)decoded
, hex
, count
);
3183 if (converted
!= count
) {
3188 decoded
[count
] = '\0';
3192 /* handle extended restart packet */
3193 static void gdb_restart_inferior(struct connection
*connection
, const char *packet
, int packet_size
)
3195 struct gdb_connection
*gdb_con
= connection
->priv
;
3196 struct target
*target
= get_target_from_connection(connection
);
3198 breakpoint_clear_target(target
);
3199 watchpoint_clear_target(target
);
3200 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
3201 target_name(target
));
3202 /* set connection as attached after reset */
3203 gdb_con
->attached
= true;
3204 /* info rtos parts */
3205 gdb_thread_packet(connection
, packet
, packet_size
);
3208 static bool gdb_handle_vrun_packet(struct connection
*connection
, const char *packet
, int packet_size
)
3210 struct target
*target
= get_target_from_connection(connection
);
3211 const char *parse
= packet
;
3216 if (parse
[0] != ';')
3220 /* Skip first field "filename"; don't know what to do with it. */
3221 free(next_hex_encoded_field(&parse
, ';'));
3223 char *cmdline
= next_hex_encoded_field(&parse
, ';');
3225 char *arg
= next_hex_encoded_field(&parse
, ';');
3228 char *new_cmdline
= alloc_printf("%s %s", cmdline
, arg
);
3231 cmdline
= new_cmdline
;
3235 if (target
->semihosting
) {
3236 LOG_INFO("GDB set inferior command line to '%s'", cmdline
);
3237 free(target
->semihosting
->cmdline
);
3238 target
->semihosting
->cmdline
= cmdline
;
3240 LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline
);
3245 gdb_restart_inferior(connection
, packet
, packet_size
);
3246 gdb_put_packet(connection
, "S00", 3);
3250 static int gdb_v_packet(struct connection
*connection
,
3251 char const *packet
, int packet_size
)
3253 struct gdb_connection
*gdb_connection
= connection
->priv
;
3256 struct target
*target
= get_target_from_connection(connection
);
3258 if (strncmp(packet
, "vCont", 5) == 0) {
3264 handled
= gdb_handle_vcont_packet(connection
, packet
, packet_size
);
3266 gdb_put_packet(connection
, "", 0);
3271 if (strncmp(packet
, "vRun", 4) == 0) {
3274 handled
= gdb_handle_vrun_packet(connection
, packet
, packet_size
);
3276 gdb_put_packet(connection
, "", 0);
3281 /* if flash programming disabled - send a empty reply */
3283 if (gdb_flash_program
== 0) {
3284 gdb_put_packet(connection
, "", 0);
3288 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
3290 unsigned long length
;
3292 char const *parse
= packet
+ 12;
3293 if (*parse
== '\0') {
3294 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3295 return ERROR_SERVER_REMOTE_CLOSED
;
3298 addr
= strtoul(parse
, (char **)&parse
, 16);
3300 if (*(parse
++) != ',' || *parse
== '\0') {
3301 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3302 return ERROR_SERVER_REMOTE_CLOSED
;
3305 length
= strtoul(parse
, (char **)&parse
, 16);
3307 if (*parse
!= '\0') {
3308 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3309 return ERROR_SERVER_REMOTE_CLOSED
;
3312 /* assume all sectors need erasing - stops any problems
3313 * when flash_write is called multiple times */
3316 /* perform any target specific operations before the erase */
3317 target_call_event_callbacks(target
,
3318 TARGET_EVENT_GDB_FLASH_ERASE_START
);
3320 /* vFlashErase:addr,length messages require region start and
3321 * end to be "block" aligned ... if padding is ever needed,
3322 * GDB will have become dangerously confused.
3324 result
= flash_erase_address_range(target
, false, addr
,
3327 /* perform any target specific operations after the erase */
3328 target_call_event_callbacks(target
,
3329 TARGET_EVENT_GDB_FLASH_ERASE_END
);
3332 if (result
!= ERROR_OK
) {
3333 /* GDB doesn't evaluate the actual error number returned,
3334 * treat a failed erase as an I/O error
3336 gdb_send_error(connection
, EIO
);
3337 LOG_ERROR("flash_erase returned %i", result
);
3339 gdb_put_packet(connection
, "OK", 2);
3344 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
3347 unsigned long length
;
3348 char const *parse
= packet
+ 12;
3350 if (*parse
== '\0') {
3351 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3352 return ERROR_SERVER_REMOTE_CLOSED
;
3354 addr
= strtoul(parse
, (char **)&parse
, 16);
3355 if (*(parse
++) != ':') {
3356 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3357 return ERROR_SERVER_REMOTE_CLOSED
;
3359 length
= packet_size
- (parse
- packet
);
3361 /* create a new image if there isn't already one */
3362 if (!gdb_connection
->vflash_image
) {
3363 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
3364 image_open(gdb_connection
->vflash_image
, "", "build");
3367 /* create new section with content from packet buffer */
3368 retval
= image_add_section(gdb_connection
->vflash_image
,
3369 addr
, length
, 0x0, (uint8_t const *)parse
);
3370 if (retval
!= ERROR_OK
)
3373 gdb_put_packet(connection
, "OK", 2);
3378 if (strncmp(packet
, "vFlashDone", 10) == 0) {
3381 /* process the flashing buffer. No need to erase as GDB
3382 * always issues a vFlashErase first. */
3383 target_call_event_callbacks(target
,
3384 TARGET_EVENT_GDB_FLASH_WRITE_START
);
3385 result
= flash_write(target
, gdb_connection
->vflash_image
,
3387 target_call_event_callbacks(target
,
3388 TARGET_EVENT_GDB_FLASH_WRITE_END
);
3389 if (result
!= ERROR_OK
) {
3390 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
3391 gdb_put_packet(connection
, "E.memtype", 9);
3393 gdb_send_error(connection
, EIO
);
3395 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
3396 gdb_put_packet(connection
, "OK", 2);
3399 image_close(gdb_connection
->vflash_image
);
3400 free(gdb_connection
->vflash_image
);
3401 gdb_connection
->vflash_image
= NULL
;
3406 gdb_put_packet(connection
, "", 0);
3410 static int gdb_detach(struct connection
*connection
)
3413 * Only reply "OK" to GDB
3414 * it will close the connection and this will trigger a call to
3415 * gdb_connection_closed() that will in turn trigger the event
3416 * TARGET_EVENT_GDB_DETACH
3418 return gdb_put_packet(connection
, "OK", 2);
3421 /* The format of 'F' response packet is
3422 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3424 static int gdb_fileio_response_packet(struct connection
*connection
,
3425 char const *packet
, int packet_size
)
3427 struct target
*target
= get_target_from_connection(connection
);
3429 char *parsing_point
;
3430 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
3431 int fileio_errno
= 0;
3432 bool fileio_ctrl_c
= false;
3437 if (*separator
== ',') {
3438 parsing_point
= separator
+ 1;
3439 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
3440 if (*separator
== ',') {
3441 if (*(separator
+ 1) == 'C') {
3442 /* TODO: process ctrl-c */
3443 fileio_ctrl_c
= true;
3448 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3449 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
3451 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
3452 if (retval
!= ERROR_OK
)
3455 /* After File-I/O ends, keep continue or step */
3456 if (gdb_running_type
== 'c')
3457 retval
= target_resume(target
, 1, 0x0, 0, 0);
3458 else if (gdb_running_type
== 's')
3459 retval
= target_step(target
, 1, 0x0, 0);
3461 retval
= ERROR_FAIL
;
3463 if (retval
!= ERROR_OK
)
3469 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
3470 const char *function
, const char *string
)
3472 struct connection
*connection
= priv
;
3473 struct gdb_connection
*gdb_con
= connection
->priv
;
3475 if (gdb_con
->output_flag
== GDB_OUTPUT_NO
)
3476 /* No out allowed */
3479 if (gdb_con
->busy
) {
3480 /* do not reply this using the O packet */
3484 gdb_output_con(connection
, string
);
3487 static void gdb_sig_halted(struct connection
*connection
)
3490 snprintf(sig_reply
, 4, "T%2.2x", 2);
3491 gdb_put_packet(connection
, sig_reply
, 3);
3494 static int gdb_input_inner(struct connection
*connection
)
3496 /* Do not allocate this on the stack */
3497 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for null-termination */
3499 struct target
*target
;
3500 char const *packet
= gdb_packet_buffer
;
3503 struct gdb_connection
*gdb_con
= connection
->priv
;
3504 static bool warn_use_ext
;
3506 target
= get_target_from_connection(connection
);
3508 /* drain input buffer. If one of the packets fail, then an error
3509 * packet is replied, if applicable.
3511 * This loop will terminate and the error code is returned.
3513 * The calling fn will check if this error is something that
3514 * can be recovered from, or if the connection must be closed.
3516 * If the error is recoverable, this fn is called again to
3517 * drain the rest of the buffer.
3520 packet_size
= GDB_BUFFER_SIZE
;
3521 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
3522 if (retval
!= ERROR_OK
)
3525 /* terminate with zero */
3526 gdb_packet_buffer
[packet_size
] = '\0';
3528 if (packet_size
> 0) {
3530 gdb_log_incoming_packet(connection
, gdb_packet_buffer
);
3533 switch (packet
[0]) {
3534 case 'T': /* Is thread alive? */
3535 gdb_thread_packet(connection
, packet
, packet_size
);
3537 case 'H': /* Set current thread ( 'c' for step and continue,
3538 * 'g' for all other operations ) */
3539 gdb_thread_packet(connection
, packet
, packet_size
);
3543 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
3544 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
3545 retval
= gdb_query_packet(connection
, packet
, packet_size
);
3548 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
3551 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
3554 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
3557 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
3560 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
3563 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
3567 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
3570 gdb_last_signal_packet(connection
, packet
, packet_size
);
3571 /* '?' is sent after the eventual '!' */
3572 if (!warn_use_ext
&& !gdb_con
->extended_protocol
) {
3573 warn_use_ext
= true;
3574 LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3575 connection
->service
->port
, connection
->service
->port
);
3581 gdb_thread_packet(connection
, packet
, packet_size
);
3582 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3584 if (gdb_con
->mem_write_error
) {
3585 LOG_ERROR("Memory write failure!");
3587 /* now that we have reported the memory write error,
3588 * we can clear the condition */
3589 gdb_con
->mem_write_error
= false;
3592 bool nostep
= false;
3593 bool already_running
= false;
3594 if (target
->state
== TARGET_RUNNING
) {
3595 LOG_WARNING("WARNING! The target is already running. "
3596 "All changes GDB did to registers will be discarded! "
3597 "Waiting for target to halt.");
3598 already_running
= true;
3599 } else if (target
->state
!= TARGET_HALTED
) {
3600 LOG_WARNING("The target is not in the halted nor running stated, "
3601 "stepi/continue ignored.");
3603 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
3604 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3605 * sent by GDB first to OpenOCD, thus defeating the check to
3606 * make only the single stepping have the sync feature...
3609 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3610 "from the target.");
3612 gdb_con
->sync
= false;
3614 if (!already_running
&& nostep
) {
3615 /* Either the target isn't in the halted state, then we can't
3616 * step/continue. This might be early setup, etc.
3618 * Or we want to allow GDB to pick up a fresh set of
3619 * register values without modifying the target state.
3622 gdb_sig_halted(connection
);
3624 /* stop forwarding log packets! */
3625 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
3627 /* We're running/stepping, in which case we can
3628 * forward log output until the target is halted
3630 gdb_con
->frontend_state
= TARGET_RUNNING
;
3631 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3633 if (!already_running
) {
3634 /* Here we don't want packet processing to stop even if this fails,
3635 * so we use a local variable instead of retval. */
3636 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
3637 if (retval
!= ERROR_OK
) {
3638 /* we'll never receive a halted
3639 * condition... issue a false one..
3641 gdb_frontend_halted(target
, connection
);
3648 retval
= gdb_v_packet(connection
, packet
, packet_size
);
3651 retval
= gdb_detach(connection
);
3654 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
3655 if (retval
!= ERROR_OK
)
3659 if (gdb_con
->extended_protocol
) {
3660 gdb_con
->attached
= false;
3663 gdb_put_packet(connection
, "OK", 2);
3664 return ERROR_SERVER_REMOTE_CLOSED
;
3666 /* handle extended remote protocol */
3667 gdb_con
->extended_protocol
= true;
3668 gdb_put_packet(connection
, "OK", 2);
3671 /* handle extended restart packet */
3672 gdb_restart_inferior(connection
, packet
, packet_size
);
3677 /* packet supported only by smp target i.e cortex_a.c*/
3678 /* handle smp packet replying coreid played to gbd */
3679 gdb_read_smp_packet(connection
, packet
, packet_size
);
3684 /* packet supported only by smp target i.e cortex_a.c */
3685 /* handle smp packet setting coreid to be played at next
3687 gdb_write_smp_packet(connection
, packet
, packet_size
);
3691 /* File-I/O extension */
3692 /* After gdb uses host-side syscall to complete target file
3693 * I/O, gdb sends host-side syscall return value to target
3695 * The format of 'F' response packet is
3696 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3698 gdb_con
->frontend_state
= TARGET_RUNNING
;
3699 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3700 gdb_fileio_response_packet(connection
, packet
, packet_size
);
3704 /* ignore unknown packets */
3705 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
3706 gdb_put_packet(connection
, "", 0);
3710 /* if a packet handler returned an error, exit input loop */
3711 if (retval
!= ERROR_OK
)
3715 if (gdb_con
->ctrl_c
) {
3716 if (target
->state
== TARGET_RUNNING
) {
3717 struct target
*t
= target
;
3719 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &t
);
3720 retval
= target_halt(t
);
3721 if (retval
== ERROR_OK
)
3722 retval
= target_poll(t
);
3723 if (retval
!= ERROR_OK
)
3724 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3725 gdb_con
->ctrl_c
= false;
3727 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3728 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3732 } while (gdb_con
->buf_cnt
> 0);
3737 static int gdb_input(struct connection
*connection
)
3739 int retval
= gdb_input_inner(connection
);
3740 struct gdb_connection
*gdb_con
= connection
->priv
;
3741 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3744 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3745 if (gdb_con
->closed
)
3746 return ERROR_SERVER_REMOTE_CLOSED
;
3748 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3752 static void gdb_keep_client_alive(struct connection
*connection
)
3754 struct gdb_connection
*gdb_con
= connection
->priv
;
3756 if (gdb_con
->busy
) {
3757 /* do not send packets, retry asap */
3761 switch (gdb_con
->output_flag
) {
3763 /* no need for keep-alive */
3765 case GDB_OUTPUT_ALL
:
3766 /* send an empty O packet */
3767 gdb_output_con(connection
, "");
3774 static const struct service_driver gdb_service_driver
= {
3776 .new_connection_during_keep_alive_handler
= NULL
,
3777 .new_connection_handler
= gdb_new_connection
,
3778 .input_handler
= gdb_input
,
3779 .connection_closed_handler
= gdb_connection_closed
,
3780 .keep_client_alive_handler
= gdb_keep_client_alive
,
3783 static int gdb_target_start(struct target
*target
, const char *port
)
3785 struct gdb_service
*gdb_service
;
3787 gdb_service
= malloc(sizeof(struct gdb_service
));
3792 LOG_INFO("starting gdb server for %s on %s", target_name(target
), port
);
3794 gdb_service
->target
= target
;
3795 gdb_service
->core
[0] = -1;
3796 gdb_service
->core
[1] = -1;
3797 target
->gdb_service
= gdb_service
;
3799 ret
= add_service(&gdb_service_driver
, port
, target
->gdb_max_connections
, gdb_service
);
3800 /* initialize all targets gdb service with the same pointer */
3802 struct target_list
*head
;
3803 foreach_smp_target(head
, target
->smp_targets
) {
3804 struct target
*curr
= head
->target
;
3806 curr
->gdb_service
= gdb_service
;
3812 static int gdb_target_add_one(struct target
*target
)
3814 /* one gdb instance per smp list */
3815 if ((target
->smp
) && (target
->gdb_service
))
3818 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3819 if (!target_supports_gdb_connection(target
)) {
3820 LOG_DEBUG("skip gdb server for target %s", target_name(target
));
3824 if (target
->gdb_port_override
) {
3825 if (strcmp(target
->gdb_port_override
, "disabled") == 0) {
3826 LOG_INFO("gdb port disabled");
3829 return gdb_target_start(target
, target
->gdb_port_override
);
3832 if (strcmp(gdb_port
, "disabled") == 0) {
3833 LOG_INFO("gdb port disabled");
3837 int retval
= gdb_target_start(target
, gdb_port_next
);
3838 if (retval
== ERROR_OK
) {
3839 /* save the port number so can be queried with
3840 * $target_name cget -gdb-port
3842 target
->gdb_port_override
= strdup(gdb_port_next
);
3845 /* If we can parse the port number
3846 * then we increment the port number for the next target.
3849 portnumber
= strtol(gdb_port_next
, &end
, 0);
3851 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3852 free(gdb_port_next
);
3854 gdb_port_next
= alloc_printf("%ld", portnumber
+1);
3856 /* Don't increment if gdb_port is 0, since we're just
3857 * trying to allocate an unused port. */
3858 gdb_port_next
= strdup("0");
3866 int gdb_target_add_all(struct target
*target
)
3869 LOG_WARNING("gdb services need one or more targets defined");
3874 int retval
= gdb_target_add_one(target
);
3875 if (retval
!= ERROR_OK
)
3878 target
= target
->next
;
3884 COMMAND_HANDLER(handle_gdb_sync_command
)
3887 return ERROR_COMMAND_SYNTAX_ERROR
;
3889 if (!current_gdb_connection
) {
3891 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3895 current_gdb_connection
->sync
= true;
3900 /* daemon configuration command gdb_port */
3901 COMMAND_HANDLER(handle_gdb_port_command
)
3903 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3904 if (retval
== ERROR_OK
) {
3905 free(gdb_port_next
);
3906 gdb_port_next
= strdup(gdb_port
);
3911 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3914 return ERROR_COMMAND_SYNTAX_ERROR
;
3916 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3920 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3923 return ERROR_COMMAND_SYNTAX_ERROR
;
3925 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3929 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3932 return ERROR_COMMAND_SYNTAX_ERROR
;
3934 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3938 COMMAND_HANDLER(handle_gdb_report_register_access_error
)
3941 return ERROR_COMMAND_SYNTAX_ERROR
;
3943 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_register_access_error
);
3947 /* gdb_breakpoint_override */
3948 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3950 if (CMD_ARGC
== 0) {
3952 } else if (CMD_ARGC
== 1) {
3953 gdb_breakpoint_override
= 1;
3954 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3955 gdb_breakpoint_override_type
= BKPT_HARD
;
3956 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3957 gdb_breakpoint_override_type
= BKPT_SOFT
;
3958 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3959 gdb_breakpoint_override
= 0;
3961 return ERROR_COMMAND_SYNTAX_ERROR
;
3962 if (gdb_breakpoint_override
)
3963 LOG_USER("force %s breakpoints",
3964 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3966 LOG_USER("breakpoint type is not overridden");
3971 COMMAND_HANDLER(handle_gdb_target_description_command
)
3974 return ERROR_COMMAND_SYNTAX_ERROR
;
3976 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3980 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3983 uint32_t tdesc_length
;
3984 struct target
*target
= get_current_target(CMD_CTX
);
3986 int retval
= gdb_generate_target_description(target
, &tdesc
);
3987 if (retval
!= ERROR_OK
) {
3988 LOG_ERROR("Unable to Generate Target Description");
3992 tdesc_length
= strlen(tdesc
);
3994 struct fileio
*fileio
;
3995 size_t size_written
;
3997 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3998 if (!tdesc_filename
) {
3999 retval
= ERROR_FAIL
;
4003 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
4005 if (retval
!= ERROR_OK
) {
4006 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
4010 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
4012 fileio_close(fileio
);
4014 if (retval
!= ERROR_OK
)
4015 LOG_ERROR("Error while writing the tdesc file");
4018 free(tdesc_filename
);
4024 static const struct command_registration gdb_command_handlers
[] = {
4027 .handler
= handle_gdb_sync_command
,
4028 .mode
= COMMAND_ANY
,
4029 .help
= "next stepi will return immediately allowing "
4030 "GDB to fetch register state without affecting "
4036 .handler
= handle_gdb_port_command
,
4037 .mode
= COMMAND_CONFIG
,
4038 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4039 "server listens for the next port number after the "
4040 "base port number specified. "
4041 "No arguments reports GDB port. \"pipe\" means listen to stdin "
4042 "output to stdout, an integer is base port number, \"disabled\" disables "
4043 "port. Any other string is are interpreted as named pipe to listen to. "
4044 "Output pipe is the same name as input pipe, but with 'o' appended.",
4045 .usage
= "[port_num]",
4048 .name
= "gdb_memory_map",
4049 .handler
= handle_gdb_memory_map_command
,
4050 .mode
= COMMAND_CONFIG
,
4051 .help
= "enable or disable memory map",
4052 .usage
= "('enable'|'disable')"
4055 .name
= "gdb_flash_program",
4056 .handler
= handle_gdb_flash_program_command
,
4057 .mode
= COMMAND_CONFIG
,
4058 .help
= "enable or disable flash program",
4059 .usage
= "('enable'|'disable')"
4062 .name
= "gdb_report_data_abort",
4063 .handler
= handle_gdb_report_data_abort_command
,
4064 .mode
= COMMAND_CONFIG
,
4065 .help
= "enable or disable reporting data aborts",
4066 .usage
= "('enable'|'disable')"
4069 .name
= "gdb_report_register_access_error",
4070 .handler
= handle_gdb_report_register_access_error
,
4071 .mode
= COMMAND_CONFIG
,
4072 .help
= "enable or disable reporting register access errors",
4073 .usage
= "('enable'|'disable')"
4076 .name
= "gdb_breakpoint_override",
4077 .handler
= handle_gdb_breakpoint_override_command
,
4078 .mode
= COMMAND_ANY
,
4079 .help
= "Display or specify type of breakpoint "
4080 "to be used by gdb 'break' commands.",
4081 .usage
= "('hard'|'soft'|'disable')"
4084 .name
= "gdb_target_description",
4085 .handler
= handle_gdb_target_description_command
,
4086 .mode
= COMMAND_CONFIG
,
4087 .help
= "enable or disable target description",
4088 .usage
= "('enable'|'disable')"
4091 .name
= "gdb_save_tdesc",
4092 .handler
= handle_gdb_save_tdesc_command
,
4093 .mode
= COMMAND_EXEC
,
4094 .help
= "Save the target description file",
4097 COMMAND_REGISTRATION_DONE
4100 int gdb_register_commands(struct command_context
*cmd_ctx
)
4102 gdb_port
= strdup("3333");
4103 gdb_port_next
= strdup("3333");
4104 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);
4107 void gdb_service_free(void)
4110 free(gdb_port_next
);
4113 int gdb_get_actual_connections(void)
4115 return gdb_actual_connections
;