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 /* output goes through gdb connection */
1005 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
1007 /* we must remove all breakpoints registered to the target as a previous
1008 * GDB session could leave dangling breakpoints if e.g. communication
1011 breakpoint_clear_target(target
);
1012 watchpoint_clear_target(target
);
1014 /* Since version 3.95 (gdb-19990504), with the exclusion of 6.5~6.8, GDB
1015 * sends an ACK at connection with the following comment in its source code:
1016 * "Ack any packet which the remote side has already sent."
1017 * LLDB does the same since the first gdb-remote implementation.
1018 * Remove the initial ACK from the incoming buffer.
1020 retval
= gdb_get_char(connection
, &initial_ack
);
1021 if (retval
!= ERROR_OK
)
1024 if (initial_ack
!= '+')
1025 gdb_putback_char(connection
, initial_ack
);
1027 target_call_event_callbacks(target
, TARGET_EVENT_GDB_ATTACH
);
1030 /* clean previous rtos session if supported*/
1031 if (target
->rtos
->type
->clean
)
1032 target
->rtos
->type
->clean(target
);
1034 /* update threads */
1035 rtos_update_threads(target
);
1038 if (gdb_use_memory_map
) {
1039 /* Connect must fail if the memory map can't be set up correctly.
1041 * This will cause an auto_probe to be invoked, which is either
1042 * a no-op or it will fail when the target isn't ready(e.g. not halted).
1044 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1045 struct flash_bank
*p
;
1046 p
= get_flash_bank_by_num_noprobe(i
);
1047 if (p
->target
!= target
)
1049 retval
= get_flash_bank_by_num(i
, &p
);
1050 if (retval
!= ERROR_OK
) {
1051 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target "
1052 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1058 log_printf_lf(all_targets
->next
? LOG_LVL_INFO
: LOG_LVL_DEBUG
,
1059 __FILE__
, __LINE__
, __func__
,
1060 "New GDB Connection: %d, Target %s, state: %s",
1061 gdb_connection
->unique_index
,
1062 target_name(target
),
1063 target_state_name(target
));
1065 if (!target_was_examined(target
)) {
1066 LOG_ERROR("Target %s not examined yet, refuse gdb connection %d!",
1067 target_name(target
), gdb_connection
->unique_index
);
1068 return ERROR_TARGET_NOT_EXAMINED
;
1070 gdb_actual_connections
++;
1072 if (target
->state
!= TARGET_HALTED
)
1073 LOG_WARNING("GDB connection %d on target %s not halted",
1074 gdb_actual_connections
, target_name(target
));
1076 /* DANGER! If we fail subsequently, we must remove this handler,
1077 * otherwise we occasionally see crashes as the timer can invoke the
1080 * register callback to be informed about target events */
1081 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1083 log_add_callback(gdb_log_callback
, connection
);
1088 static int gdb_connection_closed(struct connection
*connection
)
1090 struct target
*target
;
1091 struct gdb_connection
*gdb_connection
= connection
->priv
;
1093 target
= get_target_from_connection(connection
);
1095 /* we're done forwarding messages. Tear down callback before
1096 * cleaning up connection.
1098 log_remove_callback(gdb_log_callback
, connection
);
1100 gdb_actual_connections
--;
1101 LOG_DEBUG("{%d} GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1102 gdb_connection
->unique_index
,
1103 target_name(target
),
1104 target_state_name(target
),
1105 gdb_actual_connections
);
1107 /* see if an image built with vFlash commands is left */
1108 if (gdb_connection
->vflash_image
) {
1109 image_close(gdb_connection
->vflash_image
);
1110 free(gdb_connection
->vflash_image
);
1111 gdb_connection
->vflash_image
= NULL
;
1114 /* if this connection registered a debug-message receiver delete it */
1115 delete_debug_msg_receiver(connection
->cmd_ctx
, target
);
1117 free(connection
->priv
);
1118 connection
->priv
= NULL
;
1120 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1122 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
1124 target_call_event_callbacks(target
, TARGET_EVENT_GDB_DETACH
);
1129 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1132 snprintf(err
, 4, "E%2.2X", the_error
);
1133 gdb_put_packet(connection
, err
, 3);
1136 static int gdb_last_signal_packet(struct connection
*connection
,
1137 char const *packet
, int packet_size
)
1139 struct target
*target
= get_target_from_connection(connection
);
1140 struct gdb_connection
*gdb_con
= connection
->priv
;
1144 if (!gdb_con
->attached
) {
1145 /* if we are here we have received a kill packet
1146 * reply W stop reply otherwise gdb gets very unhappy */
1147 gdb_put_packet(connection
, "W00", 3);
1151 signal_var
= gdb_last_signal(target
);
1153 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1154 gdb_put_packet(connection
, sig_reply
, 3);
1159 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1161 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1164 return len
- 1 - pos
;
1167 /* Convert register to string of bytes. NB! The # of bits in the
1168 * register might be non-divisible by 8(a byte), in which
1169 * case an entire byte is shown.
1171 * NB! the format on the wire is the target endianness
1173 * The format of reg->value is little endian
1176 static void gdb_str_to_target(struct target
*target
,
1177 char *tstr
, struct reg
*reg
)
1184 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1186 for (i
= 0; i
< buf_len
; i
++) {
1187 int j
= gdb_reg_pos(target
, i
, buf_len
);
1188 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1192 /* copy over in register buffer */
1193 static void gdb_target_to_reg(struct target
*target
,
1194 char const *tstr
, int str_len
, uint8_t *bin
)
1197 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1202 for (i
= 0; i
< str_len
; i
+= 2) {
1204 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1205 LOG_ERROR("BUG: unable to convert register value");
1209 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1214 /* get register value if needed and fill the buffer accordingly */
1215 static int gdb_get_reg_value_as_str(struct target
*target
, char *tstr
, struct reg
*reg
)
1217 int retval
= ERROR_OK
;
1220 retval
= reg
->type
->get(reg
);
1222 const unsigned int len
= DIV_ROUND_UP(reg
->size
, 8) * 2;
1225 gdb_str_to_target(target
, tstr
, reg
);
1227 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE
:
1228 memset(tstr
, 'x', len
);
1235 static int gdb_get_registers_packet(struct connection
*connection
,
1236 char const *packet
, int packet_size
)
1238 struct target
*target
= get_target_from_connection(connection
);
1239 struct reg
**reg_list
;
1242 int reg_packet_size
= 0;
1247 #ifdef _DEBUG_GDB_IO_
1251 if ((target
->rtos
) && (rtos_get_gdb_reg_list(connection
) == ERROR_OK
))
1254 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1256 if (retval
!= ERROR_OK
)
1257 return gdb_error(connection
, retval
);
1259 for (i
= 0; i
< reg_list_size
; i
++) {
1260 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1262 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1265 assert(reg_packet_size
> 0);
1267 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1271 reg_packet_p
= reg_packet
;
1273 for (i
= 0; i
< reg_list_size
; i
++) {
1274 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1276 if (gdb_get_reg_value_as_str(target
, reg_packet_p
, reg_list
[i
]) != ERROR_OK
) {
1279 return gdb_error(connection
, retval
);
1281 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1284 #ifdef _DEBUG_GDB_IO_
1286 char *reg_packet_p_debug
;
1287 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1288 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1289 free(reg_packet_p_debug
);
1293 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1301 static int gdb_set_registers_packet(struct connection
*connection
,
1302 char const *packet
, int packet_size
)
1304 struct target
*target
= get_target_from_connection(connection
);
1306 struct reg
**reg_list
;
1309 char const *packet_p
;
1311 #ifdef _DEBUG_GDB_IO_
1315 /* skip command character */
1319 if (packet_size
% 2) {
1320 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1321 return ERROR_SERVER_REMOTE_CLOSED
;
1324 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1326 if (retval
!= ERROR_OK
)
1327 return gdb_error(connection
, retval
);
1330 for (i
= 0; i
< reg_list_size
; i
++) {
1332 if (!reg_list
[i
] || !reg_list
[i
]->exist
|| reg_list
[i
]->hidden
)
1334 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1336 if (packet_p
+ chars
> packet
+ packet_size
)
1337 LOG_ERROR("BUG: register packet is too small for registers");
1339 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1340 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1342 retval
= reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1343 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1344 LOG_DEBUG("Couldn't set register %s.", reg_list
[i
]->name
);
1347 return gdb_error(connection
, retval
);
1350 /* advance packet pointer */
1356 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1359 gdb_put_packet(connection
, "OK", 2);
1364 static int gdb_get_register_packet(struct connection
*connection
,
1365 char const *packet
, int packet_size
)
1367 struct target
*target
= get_target_from_connection(connection
);
1369 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1370 struct reg
**reg_list
;
1374 #ifdef _DEBUG_GDB_IO_
1378 if ((target
->rtos
) && (rtos_get_gdb_reg(connection
, reg_num
) == ERROR_OK
))
1381 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1383 if (retval
!= ERROR_OK
)
1384 return gdb_error(connection
, retval
);
1386 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1387 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1388 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1389 return ERROR_SERVER_REMOTE_CLOSED
;
1392 reg_packet
= calloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1, 1); /* plus one for string termination null */
1394 if (gdb_get_reg_value_as_str(target
, reg_packet
, reg_list
[reg_num
]) != ERROR_OK
) {
1397 return gdb_error(connection
, retval
);
1400 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1408 static int gdb_set_register_packet(struct connection
*connection
,
1409 char const *packet
, int packet_size
)
1411 struct target
*target
= get_target_from_connection(connection
);
1413 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1414 struct reg
**reg_list
;
1418 #ifdef _DEBUG_GDB_IO_
1422 if (*separator
!= '=') {
1423 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1424 return ERROR_SERVER_REMOTE_CLOSED
;
1426 size_t chars
= strlen(separator
+ 1);
1427 uint8_t *bin_buf
= malloc(chars
/ 2);
1428 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1430 if ((target
->rtos
) &&
1431 (rtos_set_reg(connection
, reg_num
, bin_buf
) == ERROR_OK
)) {
1433 gdb_put_packet(connection
, "OK", 2);
1437 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1439 if (retval
!= ERROR_OK
) {
1441 return gdb_error(connection
, retval
);
1444 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1445 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1446 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1449 return ERROR_SERVER_REMOTE_CLOSED
;
1452 if (chars
!= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2)) {
1453 LOG_ERROR("gdb sent %zu bits for a %" PRIu32
"-bit register (%s)",
1454 chars
* 4, reg_list
[reg_num
]->size
, reg_list
[reg_num
]->name
);
1457 return ERROR_SERVER_REMOTE_CLOSED
;
1460 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1462 retval
= reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1463 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1464 LOG_DEBUG("Couldn't set register %s.", reg_list
[reg_num
]->name
);
1467 return gdb_error(connection
, retval
);
1470 gdb_put_packet(connection
, "OK", 2);
1478 /* No attempt is made to translate the "retval" to
1479 * GDB speak. This has to be done at the calling
1480 * site as no mapping really exists.
1482 static int gdb_error(struct connection
*connection
, int retval
)
1484 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1485 gdb_send_error(connection
, EFAULT
);
1489 /* We don't have to worry about the default 2 second timeout for GDB packets,
1490 * because GDB breaks up large memory reads into smaller reads.
1492 static int gdb_read_memory_packet(struct connection
*connection
,
1493 char const *packet
, int packet_size
)
1495 struct target
*target
= get_target_from_connection(connection
);
1503 int retval
= ERROR_OK
;
1505 /* skip command character */
1508 addr
= strtoull(packet
, &separator
, 16);
1510 if (*separator
!= ',') {
1511 LOG_ERROR("incomplete read memory packet received, dropping connection");
1512 return ERROR_SERVER_REMOTE_CLOSED
;
1515 len
= strtoul(separator
+ 1, NULL
, 16);
1518 LOG_WARNING("invalid read memory packet received (len == 0)");
1519 gdb_put_packet(connection
, "", 0);
1523 buffer
= malloc(len
);
1525 LOG_DEBUG("addr: 0x%16.16" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1527 retval
= ERROR_NOT_IMPLEMENTED
;
1529 retval
= rtos_read_buffer(target
, addr
, len
, buffer
);
1530 if (retval
== ERROR_NOT_IMPLEMENTED
)
1531 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1533 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1534 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1535 * At some point this might be fixed in GDB, in which case this code can be removed.
1537 * OpenOCD developers are acutely aware of this problem, but there is nothing
1538 * gained by involving the user in this problem that hopefully will get resolved
1541 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1542 * cmd = view%20audit-trail&database = gdb&pr = 2395
1544 * For now, the default is to fix up things to make current GDB versions work.
1545 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1547 memset(buffer
, 0, len
);
1551 if (retval
== ERROR_OK
) {
1552 hex_buffer
= malloc(len
* 2 + 1);
1554 size_t pkt_len
= hexify(hex_buffer
, buffer
, len
, len
* 2 + 1);
1556 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1560 retval
= gdb_error(connection
, retval
);
1567 static int gdb_write_memory_packet(struct connection
*connection
,
1568 char const *packet
, int packet_size
)
1570 struct target
*target
= get_target_from_connection(connection
);
1578 /* skip command character */
1581 addr
= strtoull(packet
, &separator
, 16);
1583 if (*separator
!= ',') {
1584 LOG_ERROR("incomplete write memory packet received, dropping connection");
1585 return ERROR_SERVER_REMOTE_CLOSED
;
1588 len
= strtoul(separator
+ 1, &separator
, 16);
1590 if (*(separator
++) != ':') {
1591 LOG_ERROR("incomplete write memory packet received, dropping connection");
1592 return ERROR_SERVER_REMOTE_CLOSED
;
1595 buffer
= malloc(len
);
1597 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1599 if (unhexify(buffer
, separator
, len
) != len
)
1600 LOG_ERROR("unable to decode memory packet");
1602 retval
= ERROR_NOT_IMPLEMENTED
;
1604 retval
= rtos_write_buffer(target
, addr
, len
, buffer
);
1605 if (retval
== ERROR_NOT_IMPLEMENTED
)
1606 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1608 if (retval
== ERROR_OK
)
1609 gdb_put_packet(connection
, "OK", 2);
1611 retval
= gdb_error(connection
, retval
);
1618 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1619 char const *packet
, int packet_size
)
1621 struct target
*target
= get_target_from_connection(connection
);
1626 int retval
= ERROR_OK
;
1627 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1628 * the assumption that we're in a download and it's important to go as fast
1630 uint32_t fast_limit
= 8;
1632 /* skip command character */
1635 addr
= strtoull(packet
, &separator
, 16);
1637 if (*separator
!= ',') {
1638 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1639 return ERROR_SERVER_REMOTE_CLOSED
;
1642 len
= strtoul(separator
+ 1, &separator
, 16);
1644 if (*(separator
++) != ':') {
1645 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1646 return ERROR_SERVER_REMOTE_CLOSED
;
1649 struct gdb_connection
*gdb_connection
= connection
->priv
;
1651 if (gdb_connection
->mem_write_error
)
1652 retval
= ERROR_FAIL
;
1654 if (retval
== ERROR_OK
) {
1655 if (len
>= fast_limit
) {
1656 /* By replying the packet *immediately* GDB will send us a new packet
1657 * while we write the last one to the target.
1658 * We only do this for larger writes, so that users who do something like:
1659 * p *((int*)0xdeadbeef)=8675309
1660 * will get immediate feedback that that write failed.
1662 gdb_put_packet(connection
, "OK", 2);
1665 retval
= gdb_error(connection
, retval
);
1666 /* now that we have reported the memory write error, we can clear the condition */
1667 gdb_connection
->mem_write_error
= false;
1668 if (retval
!= ERROR_OK
)
1673 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1675 retval
= ERROR_NOT_IMPLEMENTED
;
1677 retval
= rtos_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1678 if (retval
== ERROR_NOT_IMPLEMENTED
)
1679 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1681 if (retval
!= ERROR_OK
)
1682 gdb_connection
->mem_write_error
= true;
1685 if (len
< fast_limit
) {
1686 if (retval
!= ERROR_OK
) {
1687 gdb_error(connection
, retval
);
1688 gdb_connection
->mem_write_error
= false;
1690 gdb_put_packet(connection
, "OK", 2);
1697 static int gdb_step_continue_packet(struct connection
*connection
,
1698 char const *packet
, int packet_size
)
1700 struct target
*target
= get_target_from_connection(connection
);
1702 uint64_t address
= 0x0;
1703 int retval
= ERROR_OK
;
1707 if (packet_size
> 1)
1708 address
= strtoull(packet
+ 1, NULL
, 16);
1712 gdb_running_type
= packet
[0];
1713 if (packet
[0] == 'c') {
1714 LOG_DEBUG("continue");
1715 /* resume at current address, don't handle breakpoints, not debugging */
1716 retval
= target_resume(target
, current
, address
, 0, 0);
1717 } else if (packet
[0] == 's') {
1719 /* step at current or address, don't handle breakpoints */
1720 retval
= target_step(target
, current
, address
, 0);
1725 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1726 char const *packet
, int packet_size
)
1728 struct target
*target
= get_target_from_connection(connection
);
1730 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1731 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1737 LOG_DEBUG("[%s]", target_name(target
));
1739 type
= strtoul(packet
+ 1, &separator
, 16);
1741 if (type
== 0) /* memory breakpoint */
1742 bp_type
= BKPT_SOFT
;
1743 else if (type
== 1) /* hardware breakpoint */
1744 bp_type
= BKPT_HARD
;
1745 else if (type
== 2) /* write watchpoint */
1746 wp_type
= WPT_WRITE
;
1747 else if (type
== 3) /* read watchpoint */
1749 else if (type
== 4) /* access watchpoint */
1750 wp_type
= WPT_ACCESS
;
1752 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1753 return ERROR_SERVER_REMOTE_CLOSED
;
1756 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1757 bp_type
= gdb_breakpoint_override_type
;
1759 if (*separator
!= ',') {
1760 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1761 return ERROR_SERVER_REMOTE_CLOSED
;
1764 address
= strtoull(separator
+ 1, &separator
, 16);
1766 if (*separator
!= ',') {
1767 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1768 return ERROR_SERVER_REMOTE_CLOSED
;
1771 size
= strtoul(separator
+ 1, &separator
, 16);
1776 if (packet
[0] == 'Z') {
1777 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1778 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1779 /* Send empty reply to report that breakpoints of this type are not supported */
1780 gdb_put_packet(connection
, "", 0);
1781 } else if (retval
!= ERROR_OK
) {
1782 retval
= gdb_error(connection
, retval
);
1783 if (retval
!= ERROR_OK
)
1786 gdb_put_packet(connection
, "OK", 2);
1788 breakpoint_remove(target
, address
);
1789 gdb_put_packet(connection
, "OK", 2);
1796 if (packet
[0] == 'Z') {
1797 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, WATCHPOINT_IGNORE_DATA_VALUE_MASK
);
1798 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1799 /* Send empty reply to report that watchpoints of this type are not supported */
1800 gdb_put_packet(connection
, "", 0);
1801 } else if (retval
!= ERROR_OK
) {
1802 retval
= gdb_error(connection
, retval
);
1803 if (retval
!= ERROR_OK
)
1806 gdb_put_packet(connection
, "OK", 2);
1808 watchpoint_remove(target
, address
);
1809 gdb_put_packet(connection
, "OK", 2);
1820 /* print out a string and allocate more space as needed,
1821 * mainly used for XML at this point
1823 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 5, 6))) void xml_printf(int *retval
,
1824 char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1826 if (*retval
!= ERROR_OK
)
1831 if ((!*xml
) || (!first
)) {
1832 /* start by 0 to exercise all the code paths.
1833 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1835 *size
= *size
* 2 + 2;
1837 *xml
= realloc(*xml
, *size
);
1840 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1848 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1850 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1854 /* there was just enough or not enough space, allocate more. */
1859 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1861 /* Locate the annex. */
1862 const char *annex_end
= strchr(buf
, ':');
1866 /* After the read marker and annex, qXfer looks like a
1867 * traditional 'm' packet. */
1869 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1871 if (*separator
!= ',')
1874 *len
= strtoul(separator
+ 1, NULL
, 16);
1876 /* Extract the annex if needed */
1878 *annex
= strndup(buf
, annex_end
- buf
);
1886 static int compare_bank(const void *a
, const void *b
)
1888 struct flash_bank
*b1
, *b2
;
1889 b1
= *((struct flash_bank
**)a
);
1890 b2
= *((struct flash_bank
**)b
);
1892 if (b1
->base
== b2
->base
)
1894 else if (b1
->base
> b2
->base
)
1900 static int gdb_memory_map(struct connection
*connection
,
1901 char const *packet
, int packet_size
)
1903 /* We get away with only specifying flash here. Regions that are not
1904 * specified are treated as if we provided no memory map(if not we
1905 * could detect the holes and mark them as RAM).
1906 * Normally we only execute this code once, but no big deal if we
1907 * have to regenerate it a couple of times.
1910 struct target
*target
= get_target_from_connection(connection
);
1911 struct flash_bank
*p
;
1915 int retval
= ERROR_OK
;
1916 struct flash_bank
**banks
;
1920 target_addr_t ram_start
= 0;
1921 unsigned int target_flash_banks
= 0;
1923 /* skip command character */
1926 offset
= strtoul(packet
, &separator
, 16);
1927 length
= strtoul(separator
+ 1, &separator
, 16);
1929 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1931 /* Sort banks in ascending order. We need to report non-flash
1932 * memory as ram (or rather read/write) by default for GDB, since
1933 * it has no concept of non-cacheable read/write memory (i/o etc).
1935 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1937 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1938 p
= get_flash_bank_by_num_noprobe(i
);
1939 if (p
->target
!= target
)
1941 retval
= get_flash_bank_by_num(i
, &p
);
1942 if (retval
!= ERROR_OK
) {
1944 gdb_error(connection
, retval
);
1947 banks
[target_flash_banks
++] = p
;
1950 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1953 for (unsigned int i
= 0; i
< target_flash_banks
; i
++) {
1954 unsigned sector_size
= 0;
1955 unsigned group_len
= 0;
1959 if (ram_start
< p
->base
)
1960 xml_printf(&retval
, &xml
, &pos
, &size
,
1961 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1962 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
1963 ram_start
, p
->base
- ram_start
);
1965 /* Report adjacent groups of same-size sectors. So for
1966 * example top boot CFI flash will list an initial region
1967 * with several large sectors (maybe 128KB) and several
1968 * smaller ones at the end (maybe 32KB). STR7 will have
1969 * regions with 8KB, 32KB, and 64KB sectors; etc.
1971 for (unsigned int j
= 0; j
< p
->num_sectors
; j
++) {
1973 /* Maybe start a new group of sectors. */
1974 if (sector_size
== 0) {
1975 if (p
->sectors
[j
].offset
+ p
->sectors
[j
].size
> p
->size
) {
1976 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1977 " overflows the end of %s bank.",
1978 p
->sectors
[j
].offset
, p
->name
);
1979 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1982 target_addr_t start
;
1983 start
= p
->base
+ p
->sectors
[j
].offset
;
1984 xml_printf(&retval
, &xml
, &pos
, &size
,
1985 "<memory type=\"flash\" "
1986 "start=\"" TARGET_ADDR_FMT
"\" ",
1988 sector_size
= p
->sectors
[j
].size
;
1989 group_len
= sector_size
;
1991 group_len
+= sector_size
; /* equal to p->sectors[j].size */
1994 /* Does this finish a group of sectors?
1995 * If not, continue an already-started group.
1997 if (j
< p
->num_sectors
- 1
1998 && p
->sectors
[j
+ 1].size
== sector_size
1999 && p
->sectors
[j
+ 1].offset
== p
->sectors
[j
].offset
+ sector_size
2000 && p
->sectors
[j
+ 1].offset
+ p
->sectors
[j
+ 1].size
<= p
->size
)
2003 xml_printf(&retval
, &xml
, &pos
, &size
,
2004 "length=\"0x%x\">\n"
2005 "<property name=\"blocksize\">"
2013 ram_start
= p
->base
+ p
->size
;
2017 xml_printf(&retval
, &xml
, &pos
, &size
,
2018 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
2019 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
2020 ram_start
, target_address_max(target
) - ram_start
+ 1);
2021 /* ELSE a flash chip could be at the very end of the address space, in
2022 * which case ram_start will be precisely 0 */
2026 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
2028 if (retval
!= ERROR_OK
) {
2030 gdb_error(connection
, retval
);
2034 if (offset
+ length
> pos
)
2035 length
= pos
- offset
;
2037 char *t
= malloc(length
+ 1);
2039 memcpy(t
+ 1, xml
+ offset
, length
);
2040 gdb_put_packet(connection
, t
, length
+ 1);
2047 static const char *gdb_get_reg_type_name(enum reg_type type
)
2056 case REG_TYPE_INT16
:
2058 case REG_TYPE_INT32
:
2060 case REG_TYPE_INT64
:
2062 case REG_TYPE_INT128
:
2066 case REG_TYPE_UINT8
:
2068 case REG_TYPE_UINT16
:
2070 case REG_TYPE_UINT32
:
2072 case REG_TYPE_UINT64
:
2074 case REG_TYPE_UINT128
:
2076 case REG_TYPE_CODE_PTR
:
2078 case REG_TYPE_DATA_PTR
:
2080 case REG_TYPE_FLOAT
:
2082 case REG_TYPE_IEEE_SINGLE
:
2083 return "ieee_single";
2084 case REG_TYPE_IEEE_DOUBLE
:
2085 return "ieee_double";
2086 case REG_TYPE_ARCH_DEFINED
:
2087 return "int"; /* return arbitrary string to avoid compile warning. */
2090 return "int"; /* "int" as default value */
2093 static int lookup_add_arch_defined_types(char const **arch_defined_types_list
[], const char *type_id
,
2094 int *num_arch_defined_types
)
2096 int tbl_sz
= *num_arch_defined_types
;
2098 if (type_id
&& (strcmp(type_id
, ""))) {
2099 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2100 if (!((*arch_defined_types_list
)[j
])) {
2101 (*arch_defined_types_list
)[tbl_sz
++] = type_id
;
2102 *arch_defined_types_list
= realloc(*arch_defined_types_list
,
2103 sizeof(char *) * (tbl_sz
+ 1));
2104 (*arch_defined_types_list
)[tbl_sz
] = NULL
;
2105 *num_arch_defined_types
= tbl_sz
;
2108 if (!strcmp((*arch_defined_types_list
)[j
], type_id
))
2117 static int gdb_generate_reg_type_description(struct target
*target
,
2118 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
,
2119 char const **arch_defined_types_list
[], int *num_arch_defined_types
)
2121 int retval
= ERROR_OK
;
2123 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
2124 struct reg_data_type
*data_type
= type
->reg_type_vector
->type
;
2125 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2126 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2127 num_arch_defined_types
))
2128 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2129 arch_defined_types_list
,
2130 num_arch_defined_types
);
2132 /* <vector id="id" type="type" count="count"/> */
2133 xml_printf(&retval
, tdesc
, pos
, size
,
2134 "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32
"\"/>\n",
2135 type
->id
, type
->reg_type_vector
->type
->id
,
2136 type
->reg_type_vector
->count
);
2138 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
2139 struct reg_data_type_union_field
*field
;
2140 field
= type
->reg_type_union
->fields
;
2142 struct reg_data_type
*data_type
= field
->type
;
2143 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2144 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2145 num_arch_defined_types
))
2146 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2147 arch_defined_types_list
,
2148 num_arch_defined_types
);
2151 field
= field
->next
;
2154 * <field name="name" type="type"/> ...
2156 xml_printf(&retval
, tdesc
, pos
, size
,
2157 "<union id=\"%s\">\n",
2160 field
= type
->reg_type_union
->fields
;
2162 xml_printf(&retval
, tdesc
, pos
, size
,
2163 "<field name=\"%s\" type=\"%s\"/>\n",
2164 field
->name
, field
->type
->id
);
2166 field
= field
->next
;
2169 xml_printf(&retval
, tdesc
, pos
, size
,
2172 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
2173 struct reg_data_type_struct_field
*field
;
2174 field
= type
->reg_type_struct
->fields
;
2176 if (field
->use_bitfields
) {
2177 /* <struct id="id" size="size">
2178 * <field name="name" start="start" end="end"/> ...
2180 xml_printf(&retval
, tdesc
, pos
, size
,
2181 "<struct id=\"%s\" size=\"%" PRIu32
"\">\n",
2182 type
->id
, type
->reg_type_struct
->size
);
2184 xml_printf(&retval
, tdesc
, pos
, size
,
2185 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2186 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2187 gdb_get_reg_type_name(field
->bitfield
->type
));
2189 field
= field
->next
;
2193 struct reg_data_type
*data_type
= field
->type
;
2194 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2195 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2196 num_arch_defined_types
))
2197 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2198 arch_defined_types_list
,
2199 num_arch_defined_types
);
2204 * <field name="name" type="type"/> ...
2206 xml_printf(&retval
, tdesc
, pos
, size
,
2207 "<struct id=\"%s\">\n",
2210 xml_printf(&retval
, tdesc
, pos
, size
,
2211 "<field name=\"%s\" type=\"%s\"/>\n",
2212 field
->name
, field
->type
->id
);
2214 field
= field
->next
;
2218 xml_printf(&retval
, tdesc
, pos
, size
,
2221 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2222 /* <flags id="id" size="size">
2223 * <field name="name" start="start" end="end"/> ...
2225 xml_printf(&retval
, tdesc
, pos
, size
,
2226 "<flags id=\"%s\" size=\"%" PRIu32
"\">\n",
2227 type
->id
, type
->reg_type_flags
->size
);
2229 struct reg_data_type_flags_field
*field
;
2230 field
= type
->reg_type_flags
->fields
;
2232 xml_printf(&retval
, tdesc
, pos
, size
,
2233 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2234 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2235 gdb_get_reg_type_name(field
->bitfield
->type
));
2237 field
= field
->next
;
2240 xml_printf(&retval
, tdesc
, pos
, size
,
2248 /* Get a list of available target registers features. feature_list must
2249 * be freed by caller.
2251 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2252 struct reg
**reg_list
, int reg_list_size
)
2256 /* Start with only one element */
2257 *feature_list
= calloc(1, sizeof(char *));
2259 for (int i
= 0; i
< reg_list_size
; i
++) {
2260 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2263 if (reg_list
[i
]->feature
2264 && reg_list
[i
]->feature
->name
2265 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2266 /* We found a feature, check if the feature is already in the
2267 * table. If not, allocate a new entry for the table and
2268 * put the new feature in it.
2270 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2271 if (!((*feature_list
)[j
])) {
2272 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2273 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2274 (*feature_list
)[tbl_sz
] = NULL
;
2277 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2284 if (feature_list_size
)
2285 *feature_list_size
= tbl_sz
;
2290 /* Create a register list that's the union of all the registers of the SMP
2291 * group this target is in. If the target is not part of an SMP group, this
2292 * returns the same as target_get_gdb_reg_list_noread().
2294 static int smp_reg_list_noread(struct target
*target
,
2295 struct reg
**combined_list
[], int *combined_list_size
,
2296 enum target_register_class reg_class
)
2299 return target_get_gdb_reg_list_noread(target
, combined_list
,
2300 combined_list_size
, REG_CLASS_ALL
);
2302 unsigned int combined_allocated
= 256;
2303 struct reg
**local_list
= malloc(combined_allocated
* sizeof(struct reg
*));
2305 LOG_ERROR("malloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2308 unsigned int local_list_size
= 0;
2310 struct target_list
*head
;
2311 foreach_smp_target(head
, target
->smp_targets
) {
2312 if (!target_was_examined(head
->target
))
2315 struct reg
**reg_list
= NULL
;
2317 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2318 ®_list_size
, reg_class
);
2319 if (result
!= ERROR_OK
) {
2323 for (int i
= 0; i
< reg_list_size
; i
++) {
2325 struct reg
*a
= reg_list
[i
];
2327 /* Nested loop makes this O(n^2), but this entire function with
2328 * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2330 for (unsigned int j
= 0; j
< local_list_size
; j
++) {
2331 struct reg
*b
= local_list
[j
];
2332 if (!strcmp(a
->name
, b
->name
)) {
2334 if (a
->size
!= b
->size
) {
2335 LOG_ERROR("SMP register %s is %d bits on one "
2336 "target, but %d bits on another target.",
2337 a
->name
, a
->size
, b
->size
);
2346 LOG_DEBUG("[%s] %s not found in combined list", target_name(target
), a
->name
);
2347 if (local_list_size
>= combined_allocated
) {
2348 combined_allocated
*= 2;
2349 local_list
= realloc(local_list
, combined_allocated
* sizeof(struct reg
*));
2351 LOG_ERROR("realloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2356 local_list
[local_list_size
] = a
;
2364 if (local_list_size
== 0) {
2365 LOG_ERROR("Unable to get register list");
2370 /* Now warn the user about any registers that weren't found in every target. */
2371 foreach_smp_target(head
, target
->smp_targets
) {
2372 if (!target_was_examined(head
->target
))
2375 struct reg
**reg_list
= NULL
;
2377 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2378 ®_list_size
, reg_class
);
2379 if (result
!= ERROR_OK
) {
2383 for (unsigned int i
= 0; i
< local_list_size
; i
++) {
2385 struct reg
*a
= local_list
[i
];
2386 for (int j
= 0; j
< reg_list_size
; j
++) {
2387 struct reg
*b
= reg_list
[j
];
2388 if (b
->exist
&& !strcmp(a
->name
, b
->name
)) {
2394 LOG_WARNING("Register %s does not exist in %s, which is part of an SMP group where "
2395 "this register does exist.",
2396 a
->name
, target_name(head
->target
));
2402 *combined_list
= local_list
;
2403 *combined_list_size
= local_list_size
;
2407 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2409 int retval
= ERROR_OK
;
2410 struct reg
**reg_list
= NULL
;
2412 char const *architecture
;
2413 char const **features
= NULL
;
2414 int feature_list_size
= 0;
2420 retval
= smp_reg_list_noread(target
, ®_list
, ®_list_size
,
2423 if (retval
!= ERROR_OK
) {
2424 LOG_ERROR("get register list failed");
2425 retval
= ERROR_FAIL
;
2429 if (reg_list_size
<= 0) {
2430 LOG_ERROR("get register list failed");
2431 retval
= ERROR_FAIL
;
2435 /* Get a list of available target registers features */
2436 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2437 if (retval
!= ERROR_OK
) {
2438 LOG_ERROR("Can't get the registers feature list");
2439 retval
= ERROR_FAIL
;
2443 /* If we found some features associated with registers, create sections */
2444 int current_feature
= 0;
2446 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2447 "<?xml version=\"1.0\"?>\n"
2448 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2449 "<target version=\"1.0\">\n");
2451 /* generate architecture element if supported by target */
2452 architecture
= target_get_gdb_arch(target
);
2454 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2455 "<architecture>%s</architecture>\n", architecture
);
2457 /* generate target description according to register list */
2459 while (features
[current_feature
]) {
2460 char const **arch_defined_types
= NULL
;
2461 int num_arch_defined_types
= 0;
2463 arch_defined_types
= calloc(1, sizeof(char *));
2464 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2465 "<feature name=\"%s\">\n",
2466 features
[current_feature
]);
2469 for (i
= 0; i
< reg_list_size
; i
++) {
2471 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2474 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2477 const char *type_str
;
2478 if (reg_list
[i
]->reg_data_type
) {
2479 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2480 /* generate <type... first, if there are architecture-defined types. */
2481 if (lookup_add_arch_defined_types(&arch_defined_types
,
2482 reg_list
[i
]->reg_data_type
->id
,
2483 &num_arch_defined_types
))
2484 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2485 reg_list
[i
]->reg_data_type
,
2486 &arch_defined_types
,
2487 &num_arch_defined_types
);
2489 type_str
= reg_list
[i
]->reg_data_type
->id
;
2491 /* predefined type */
2492 type_str
= gdb_get_reg_type_name(
2493 reg_list
[i
]->reg_data_type
->type
);
2496 /* Default type is "int" */
2500 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2501 "<reg name=\"%s\"", reg_list
[i
]->name
);
2502 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2503 " bitsize=\"%" PRIu32
"\"", reg_list
[i
]->size
);
2504 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2505 " regnum=\"%" PRIu32
"\"", reg_list
[i
]->number
);
2506 if (reg_list
[i
]->caller_save
)
2507 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2508 " save-restore=\"yes\"");
2510 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2511 " save-restore=\"no\"");
2513 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2514 " type=\"%s\"", type_str
);
2516 if (reg_list
[i
]->group
)
2517 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2518 " group=\"%s\"", reg_list
[i
]->group
);
2520 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2524 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2528 free(arch_defined_types
);
2532 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2539 if (retval
== ERROR_OK
)
2547 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2548 char **chunk
, int32_t offset
, uint32_t length
)
2551 LOG_ERROR("Unable to Generate Target Description");
2555 char *tdesc
= target_desc
->tdesc
;
2556 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2559 int retval
= gdb_generate_target_description(target
, &tdesc
);
2560 if (retval
!= ERROR_OK
) {
2561 LOG_ERROR("Unable to Generate Target Description");
2565 tdesc_length
= strlen(tdesc
);
2570 if (length
< (tdesc_length
- offset
))
2571 transfer_type
= 'm';
2573 transfer_type
= 'l';
2575 *chunk
= malloc(length
+ 2);
2577 LOG_ERROR("Unable to allocate memory");
2581 (*chunk
)[0] = transfer_type
;
2582 if (transfer_type
== 'm') {
2583 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2584 (*chunk
)[1 + length
] = '\0';
2586 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2587 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2589 /* After gdb-server sends out last chunk, invalidate tdesc. */
2595 target_desc
->tdesc
= tdesc
;
2596 target_desc
->tdesc_length
= tdesc_length
;
2601 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2603 int retval
= ERROR_OK
;
2604 struct reg
**reg_list
= NULL
;
2605 int reg_list_size
= 0;
2606 char const **features
= NULL
;
2607 int feature_list_size
= 0;
2609 char const *architecture
= target_get_gdb_arch(target
);
2611 retval
= target_get_gdb_reg_list_noread(target
, ®_list
,
2612 ®_list_size
, REG_CLASS_ALL
);
2613 if (retval
!= ERROR_OK
) {
2614 LOG_ERROR("get register list failed");
2618 if (reg_list_size
<= 0) {
2619 LOG_ERROR("get register list failed");
2620 retval
= ERROR_FAIL
;
2624 /* Get a list of available target registers features */
2625 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2626 if (retval
!= ERROR_OK
) {
2627 LOG_ERROR("Can't get the registers feature list");
2632 if (architecture
|| feature_list_size
)
2646 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2648 struct rtos
*rtos
= target
->rtos
;
2649 int retval
= ERROR_OK
;
2650 char *thread_list
= NULL
;
2654 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2655 "<?xml version=\"1.0\"?>\n"
2659 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2660 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2662 if (!thread_detail
->exists
)
2665 if (thread_detail
->thread_name_str
)
2666 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2667 "<thread id=\"%" PRIx64
"\" name=\"%s\">",
2668 thread_detail
->threadid
,
2669 thread_detail
->thread_name_str
);
2671 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2672 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2674 if (thread_detail
->thread_name_str
)
2675 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2676 "Name: %s", thread_detail
->thread_name_str
);
2678 if (thread_detail
->extra_info_str
) {
2679 if (thread_detail
->thread_name_str
)
2680 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2682 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2683 "%s", thread_detail
->extra_info_str
);
2686 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2691 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2694 if (retval
== ERROR_OK
)
2695 *thread_list_out
= thread_list
;
2702 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2703 char **chunk
, int32_t offset
, uint32_t length
)
2705 if (!*thread_list
) {
2706 int retval
= gdb_generate_thread_list(target
, thread_list
);
2707 if (retval
!= ERROR_OK
) {
2708 LOG_ERROR("Unable to Generate Thread List");
2713 size_t thread_list_length
= strlen(*thread_list
);
2716 length
= MIN(length
, thread_list_length
- offset
);
2717 if (length
< (thread_list_length
- offset
))
2718 transfer_type
= 'm';
2720 transfer_type
= 'l';
2722 *chunk
= malloc(length
+ 2 + 3);
2723 /* Allocating extra 3 bytes prevents false positive valgrind report
2724 * of strlen(chunk) word access:
2725 * Invalid read of size 4
2726 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2728 LOG_ERROR("Unable to allocate memory");
2732 (*chunk
)[0] = transfer_type
;
2733 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2734 (*chunk
)[1 + length
] = '\0';
2736 /* After gdb-server sends out last chunk, invalidate thread list. */
2737 if (transfer_type
== 'l') {
2739 *thread_list
= NULL
;
2745 static int gdb_query_packet(struct connection
*connection
,
2746 char const *packet
, int packet_size
)
2748 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2749 struct gdb_connection
*gdb_connection
= connection
->priv
;
2750 struct target
*target
= get_target_from_connection(connection
);
2752 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2753 if (packet_size
> 6) {
2754 Jim_Interp
*interp
= cmd_ctx
->interp
;
2756 cmd
= malloc((packet_size
- 6) / 2 + 1);
2757 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2760 /* We want to print all debug output to GDB connection */
2761 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
2762 target_call_timer_callbacks_now();
2763 /* some commands need to know the GDB connection, make note of current
2764 * GDB connection. */
2765 current_gdb_connection
= gdb_connection
;
2767 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
2768 cmd_ctx
->current_target_override
= NULL
;
2770 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
2771 Jim_DeleteAssocData(interp
, "context");
2772 int retval
= Jim_SetAssocData(interp
, "context", NULL
, cmd_ctx
);
2773 if (retval
== JIM_OK
) {
2774 retval
= Jim_EvalObj(interp
, Jim_NewStringObj(interp
, cmd
, -1));
2775 Jim_DeleteAssocData(interp
, "context");
2777 int inner_retval
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
2778 if (retval
== JIM_OK
)
2779 retval
= inner_retval
;
2781 cmd_ctx
->current_target_override
= saved_target_override
;
2783 current_gdb_connection
= NULL
;
2784 target_call_timer_callbacks_now();
2785 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
2787 if (retval
== JIM_RETURN
)
2788 retval
= interp
->returnCode
;
2790 const char *cretmsg
= Jim_GetString(Jim_GetResult(interp
), &lenmsg
);
2792 if (lenmsg
&& cretmsg
[lenmsg
- 1] != '\n') {
2793 retmsg
= alloc_printf("%s\n", cretmsg
);
2796 retmsg
= strdup(cretmsg
);
2799 return ERROR_GDB_BUFFER_TOO_SMALL
;
2801 if (retval
== JIM_OK
) {
2803 char *hex_buffer
= malloc(lenmsg
* 2 + 1);
2806 return ERROR_GDB_BUFFER_TOO_SMALL
;
2809 size_t pkt_len
= hexify(hex_buffer
, (const uint8_t *)retmsg
, lenmsg
,
2811 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
2814 gdb_put_packet(connection
, "OK", 2);
2818 gdb_output_con(connection
, retmsg
);
2819 gdb_send_error(connection
, retval
);
2824 gdb_put_packet(connection
, "OK", 2);
2826 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2827 if (packet_size
> 5) {
2832 target_addr_t addr
= 0;
2835 /* skip command character */
2838 addr
= strtoull(packet
, &separator
, 16);
2840 if (*separator
!= ',') {
2841 LOG_ERROR("incomplete read memory packet received, dropping connection");
2842 return ERROR_SERVER_REMOTE_CLOSED
;
2845 len
= strtoul(separator
+ 1, NULL
, 16);
2847 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2849 if (retval
== ERROR_OK
) {
2850 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2851 gdb_put_packet(connection
, gdb_reply
, 9);
2853 retval
= gdb_error(connection
, retval
);
2854 if (retval
!= ERROR_OK
)
2860 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2861 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2862 * qXfer:features:read is supported for some targets */
2863 int retval
= ERROR_OK
;
2864 char *buffer
= NULL
;
2867 int gdb_target_desc_supported
= 0;
2869 /* we need to test that the target supports target descriptions */
2870 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2871 if (retval
!= ERROR_OK
) {
2872 LOG_INFO("Failed detecting Target Description Support, disabling");
2873 gdb_target_desc_supported
= 0;
2876 /* support may be disabled globally */
2877 if (gdb_use_target_description
== 0) {
2878 if (gdb_target_desc_supported
)
2879 LOG_WARNING("Target Descriptions Supported, but disabled");
2880 gdb_target_desc_supported
= 0;
2887 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2889 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2890 (gdb_target_desc_supported
== 1) ? '+' : '-');
2892 if (retval
!= ERROR_OK
) {
2893 gdb_send_error(connection
, 01);
2897 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2901 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2902 && (flash_get_bank_count() > 0))
2903 return gdb_memory_map(connection
, packet
, packet_size
);
2904 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2906 int retval
= ERROR_OK
;
2909 unsigned int length
;
2911 /* skip command character */
2914 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2915 gdb_send_error(connection
, 01);
2919 /* Target should prepare correct target description for annex.
2920 * The first character of returned xml is 'm' or 'l'. 'm' for
2921 * there are *more* chunks to transfer. 'l' for it is the *last*
2922 * chunk of target description.
2924 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2925 &xml
, offset
, length
);
2926 if (retval
!= ERROR_OK
) {
2927 gdb_error(connection
, retval
);
2931 gdb_put_packet(connection
, xml
, strlen(xml
));
2935 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2937 int retval
= ERROR_OK
;
2940 unsigned int length
;
2942 /* skip command character */
2945 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2946 gdb_send_error(connection
, 01);
2950 /* Target should prepare correct thread list for annex.
2951 * The first character of returned xml is 'm' or 'l'. 'm' for
2952 * there are *more* chunks to transfer. 'l' for it is the *last*
2953 * chunk of target description.
2955 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2956 &xml
, offset
, length
);
2957 if (retval
!= ERROR_OK
) {
2958 gdb_error(connection
, retval
);
2962 gdb_put_packet(connection
, xml
, strlen(xml
));
2966 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2967 gdb_connection
->noack_mode
= 1;
2968 gdb_put_packet(connection
, "OK", 2);
2970 } else if (target
->type
->gdb_query_custom
) {
2971 char *buffer
= NULL
;
2972 int ret
= target
->type
->gdb_query_custom(target
, packet
, &buffer
);
2973 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2977 gdb_put_packet(connection
, "", 0);
2981 static bool gdb_handle_vcont_packet(struct connection
*connection
, const char *packet
,
2982 __attribute__((unused
)) int packet_size
)
2984 struct gdb_connection
*gdb_connection
= connection
->priv
;
2985 struct target
*target
= get_target_from_connection(connection
);
2986 const char *parse
= packet
;
2989 /* query for vCont supported */
2990 if (parse
[0] == '?') {
2991 if (target
->type
->step
) {
2992 /* gdb doesn't accept c without C and s without S */
2993 gdb_put_packet(connection
, "vCont;c;C;s;S", 13);
2999 if (parse
[0] == ';') {
3003 /* simple case, a continue packet */
3004 if (parse
[0] == 'c') {
3005 gdb_running_type
= 'c';
3006 LOG_DEBUG("target %s continue", target_name(target
));
3007 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3008 retval
= target_resume(target
, 1, 0, 0, 0);
3009 if (retval
== ERROR_TARGET_NOT_HALTED
)
3010 LOG_INFO("target %s was not halted when resume was requested", target_name(target
));
3012 /* poll target in an attempt to make its internal state consistent */
3013 if (retval
!= ERROR_OK
) {
3014 retval
= target_poll(target
);
3015 if (retval
!= ERROR_OK
)
3016 LOG_DEBUG("error polling target %s after failed resume", target_name(target
));
3020 * We don't report errors to gdb here, move frontend_state to
3021 * TARGET_RUNNING to stay in sync with gdb's expectation of the
3024 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3025 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3030 /* single-step or step-over-breakpoint */
3031 if (parse
[0] == 's') {
3032 gdb_running_type
= 's';
3033 bool fake_step
= false;
3035 struct target
*ct
= target
;
3039 if (parse
[0] == ':') {
3042 thread_id
= strtoll(parse
, &endp
, 16);
3051 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
3052 rtos_update_threads(target
);
3054 target
->rtos
->gdb_target_for_threadid(connection
, thread_id
, &ct
);
3057 * check if the thread to be stepped is the current rtos thread
3058 * if not, we must fake the step
3060 if (target
->rtos
->current_thread
!= thread_id
)
3064 if (parse
[0] == ';') {
3067 if (parse
[0] == 'c') {
3070 /* check if thread-id follows */
3071 if (parse
[0] == ':') {
3075 tid
= strtoll(parse
, NULL
, 16);
3076 if (tid
== thread_id
) {
3078 * Special case: only step a single thread (core),
3079 * keep the other threads halted. Currently, only
3080 * aarch64 target understands it. Other target types don't
3081 * care (nobody checks the actual value of 'current')
3082 * and it doesn't really matter. This deserves
3083 * a symbolic constant and a formal interface documentation
3086 LOG_DEBUG("request to step current core only");
3087 /* uncomment after checking that indeed other targets are safe */
3094 LOG_DEBUG("target %s single-step thread %"PRIx64
, target_name(ct
), thread_id
);
3095 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3096 target_call_event_callbacks(ct
, TARGET_EVENT_GDB_START
);
3099 * work around an annoying gdb behaviour: when the current thread
3100 * is changed in gdb, it assumes that the target can follow and also
3101 * make the thread current. This is an assumption that cannot hold
3102 * for a real target running a multi-threading OS. We just fake
3103 * the step to not trigger an internal error in gdb. See
3104 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3108 char sig_reply
[128];
3110 LOG_DEBUG("fake step thread %"PRIx64
, thread_id
);
3112 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
),
3113 "T05thread:%016"PRIx64
";", thread_id
);
3115 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
3116 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3121 /* support for gdb_sync command */
3122 if (gdb_connection
->sync
) {
3123 gdb_connection
->sync
= false;
3124 if (ct
->state
== TARGET_HALTED
) {
3125 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3126 "from the target.");
3127 gdb_sig_halted(connection
);
3128 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3130 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3134 retval
= target_step(ct
, current_pc
, 0, 0);
3135 if (retval
== ERROR_TARGET_NOT_HALTED
)
3136 LOG_INFO("target %s was not halted when step was requested", target_name(ct
));
3138 /* if step was successful send a reply back to gdb */
3139 if (retval
== ERROR_OK
) {
3140 retval
= target_poll(ct
);
3141 if (retval
!= ERROR_OK
)
3142 LOG_DEBUG("error polling target %s after successful step", target_name(ct
));
3143 /* send back signal information */
3144 gdb_signal_reply(ct
, connection
);
3145 /* stop forwarding log packets! */
3146 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3148 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3151 LOG_ERROR("Unknown vCont packet");
3155 static char *next_hex_encoded_field(const char **str
, char sep
)
3158 const char *hex
= *str
;
3162 const char *end
= strchr(hex
, sep
);
3164 hexlen
= strlen(hex
);
3167 *str
= hex
+ hexlen
+ 1;
3169 if (hexlen
% 2 != 0) {
3170 /* Malformed hex data */
3174 size_t count
= hexlen
/ 2;
3175 char *decoded
= malloc(count
+ 1);
3179 size_t converted
= unhexify((void *)decoded
, hex
, count
);
3180 if (converted
!= count
) {
3185 decoded
[count
] = '\0';
3189 /* handle extended restart packet */
3190 static void gdb_restart_inferior(struct connection
*connection
, const char *packet
, int packet_size
)
3192 struct gdb_connection
*gdb_con
= connection
->priv
;
3193 struct target
*target
= get_target_from_connection(connection
);
3195 breakpoint_clear_target(target
);
3196 watchpoint_clear_target(target
);
3197 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
3198 target_name(target
));
3199 /* set connection as attached after reset */
3200 gdb_con
->attached
= true;
3201 /* info rtos parts */
3202 gdb_thread_packet(connection
, packet
, packet_size
);
3205 static bool gdb_handle_vrun_packet(struct connection
*connection
, const char *packet
, int packet_size
)
3207 struct target
*target
= get_target_from_connection(connection
);
3208 const char *parse
= packet
;
3213 if (parse
[0] != ';')
3217 /* Skip first field "filename"; don't know what to do with it. */
3218 free(next_hex_encoded_field(&parse
, ';'));
3220 char *cmdline
= next_hex_encoded_field(&parse
, ';');
3222 char *arg
= next_hex_encoded_field(&parse
, ';');
3225 char *new_cmdline
= alloc_printf("%s %s", cmdline
, arg
);
3228 cmdline
= new_cmdline
;
3232 if (target
->semihosting
) {
3233 LOG_INFO("GDB set inferior command line to '%s'", cmdline
);
3234 free(target
->semihosting
->cmdline
);
3235 target
->semihosting
->cmdline
= cmdline
;
3237 LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline
);
3242 gdb_restart_inferior(connection
, packet
, packet_size
);
3243 gdb_put_packet(connection
, "S00", 3);
3247 static int gdb_v_packet(struct connection
*connection
,
3248 char const *packet
, int packet_size
)
3250 struct gdb_connection
*gdb_connection
= connection
->priv
;
3253 struct target
*target
= get_target_from_connection(connection
);
3255 if (strncmp(packet
, "vCont", 5) == 0) {
3261 handled
= gdb_handle_vcont_packet(connection
, packet
, packet_size
);
3263 gdb_put_packet(connection
, "", 0);
3268 if (strncmp(packet
, "vRun", 4) == 0) {
3271 handled
= gdb_handle_vrun_packet(connection
, packet
, packet_size
);
3273 gdb_put_packet(connection
, "", 0);
3278 /* if flash programming disabled - send a empty reply */
3280 if (gdb_flash_program
== 0) {
3281 gdb_put_packet(connection
, "", 0);
3285 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
3287 unsigned long length
;
3289 char const *parse
= packet
+ 12;
3290 if (*parse
== '\0') {
3291 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3292 return ERROR_SERVER_REMOTE_CLOSED
;
3295 addr
= strtoull(parse
, (char **)&parse
, 16);
3297 if (*(parse
++) != ',' || *parse
== '\0') {
3298 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3299 return ERROR_SERVER_REMOTE_CLOSED
;
3302 length
= strtoul(parse
, (char **)&parse
, 16);
3304 if (*parse
!= '\0') {
3305 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3306 return ERROR_SERVER_REMOTE_CLOSED
;
3309 /* assume all sectors need erasing - stops any problems
3310 * when flash_write is called multiple times */
3313 /* perform any target specific operations before the erase */
3314 target_call_event_callbacks(target
,
3315 TARGET_EVENT_GDB_FLASH_ERASE_START
);
3317 /* vFlashErase:addr,length messages require region start and
3318 * end to be "block" aligned ... if padding is ever needed,
3319 * GDB will have become dangerously confused.
3321 result
= flash_erase_address_range(target
, false, addr
,
3324 /* perform any target specific operations after the erase */
3325 target_call_event_callbacks(target
,
3326 TARGET_EVENT_GDB_FLASH_ERASE_END
);
3329 if (result
!= ERROR_OK
) {
3330 /* GDB doesn't evaluate the actual error number returned,
3331 * treat a failed erase as an I/O error
3333 gdb_send_error(connection
, EIO
);
3334 LOG_ERROR("flash_erase returned %i", result
);
3336 gdb_put_packet(connection
, "OK", 2);
3341 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
3344 unsigned long length
;
3345 char const *parse
= packet
+ 12;
3347 if (*parse
== '\0') {
3348 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3349 return ERROR_SERVER_REMOTE_CLOSED
;
3352 addr
= strtoull(parse
, (char **)&parse
, 16);
3353 if (*(parse
++) != ':') {
3354 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3355 return ERROR_SERVER_REMOTE_CLOSED
;
3357 length
= packet_size
- (parse
- packet
);
3359 /* create a new image if there isn't already one */
3360 if (!gdb_connection
->vflash_image
) {
3361 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
3362 image_open(gdb_connection
->vflash_image
, "", "build");
3365 /* create new section with content from packet buffer */
3366 retval
= image_add_section(gdb_connection
->vflash_image
,
3367 addr
, length
, 0x0, (uint8_t const *)parse
);
3368 if (retval
!= ERROR_OK
)
3371 gdb_put_packet(connection
, "OK", 2);
3376 if (strncmp(packet
, "vFlashDone", 10) == 0) {
3379 /* process the flashing buffer. No need to erase as GDB
3380 * always issues a vFlashErase first. */
3381 target_call_event_callbacks(target
,
3382 TARGET_EVENT_GDB_FLASH_WRITE_START
);
3383 result
= flash_write(target
, gdb_connection
->vflash_image
,
3385 target_call_event_callbacks(target
,
3386 TARGET_EVENT_GDB_FLASH_WRITE_END
);
3387 if (result
!= ERROR_OK
) {
3388 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
3389 gdb_put_packet(connection
, "E.memtype", 9);
3391 gdb_send_error(connection
, EIO
);
3393 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
3394 gdb_put_packet(connection
, "OK", 2);
3397 image_close(gdb_connection
->vflash_image
);
3398 free(gdb_connection
->vflash_image
);
3399 gdb_connection
->vflash_image
= NULL
;
3404 gdb_put_packet(connection
, "", 0);
3408 static int gdb_detach(struct connection
*connection
)
3411 * Only reply "OK" to GDB
3412 * it will close the connection and this will trigger a call to
3413 * gdb_connection_closed() that will in turn trigger the event
3414 * TARGET_EVENT_GDB_DETACH
3416 return gdb_put_packet(connection
, "OK", 2);
3419 /* The format of 'F' response packet is
3420 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3422 static int gdb_fileio_response_packet(struct connection
*connection
,
3423 char const *packet
, int packet_size
)
3425 struct target
*target
= get_target_from_connection(connection
);
3427 char *parsing_point
;
3428 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
3429 int fileio_errno
= 0;
3430 bool fileio_ctrl_c
= false;
3435 if (*separator
== ',') {
3436 parsing_point
= separator
+ 1;
3437 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
3438 if (*separator
== ',') {
3439 if (*(separator
+ 1) == 'C') {
3440 /* TODO: process ctrl-c */
3441 fileio_ctrl_c
= true;
3446 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3447 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
3449 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
3450 if (retval
!= ERROR_OK
)
3453 /* After File-I/O ends, keep continue or step */
3454 if (gdb_running_type
== 'c')
3455 retval
= target_resume(target
, 1, 0x0, 0, 0);
3456 else if (gdb_running_type
== 's')
3457 retval
= target_step(target
, 1, 0x0, 0);
3459 retval
= ERROR_FAIL
;
3461 if (retval
!= ERROR_OK
)
3467 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
3468 const char *function
, const char *string
)
3470 struct connection
*connection
= priv
;
3471 struct gdb_connection
*gdb_con
= connection
->priv
;
3473 if (gdb_con
->output_flag
== GDB_OUTPUT_NO
)
3474 /* No out allowed */
3477 if (gdb_con
->busy
) {
3478 /* do not reply this using the O packet */
3482 gdb_output_con(connection
, string
);
3485 static void gdb_sig_halted(struct connection
*connection
)
3488 snprintf(sig_reply
, 4, "T%2.2x", 2);
3489 gdb_put_packet(connection
, sig_reply
, 3);
3492 static int gdb_input_inner(struct connection
*connection
)
3494 /* Do not allocate this on the stack */
3495 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for null-termination */
3497 struct target
*target
;
3498 char const *packet
= gdb_packet_buffer
;
3501 struct gdb_connection
*gdb_con
= connection
->priv
;
3502 static bool warn_use_ext
;
3504 target
= get_target_from_connection(connection
);
3506 /* drain input buffer. If one of the packets fail, then an error
3507 * packet is replied, if applicable.
3509 * This loop will terminate and the error code is returned.
3511 * The calling fn will check if this error is something that
3512 * can be recovered from, or if the connection must be closed.
3514 * If the error is recoverable, this fn is called again to
3515 * drain the rest of the buffer.
3518 packet_size
= GDB_BUFFER_SIZE
;
3519 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
3520 if (retval
!= ERROR_OK
)
3523 /* terminate with zero */
3524 gdb_packet_buffer
[packet_size
] = '\0';
3526 if (packet_size
> 0) {
3528 gdb_log_incoming_packet(connection
, gdb_packet_buffer
);
3531 switch (packet
[0]) {
3532 case 'T': /* Is thread alive? */
3533 gdb_thread_packet(connection
, packet
, packet_size
);
3535 case 'H': /* Set current thread ( 'c' for step and continue,
3536 * 'g' for all other operations ) */
3537 gdb_thread_packet(connection
, packet
, packet_size
);
3541 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
3542 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
3543 retval
= gdb_query_packet(connection
, packet
, packet_size
);
3546 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
3549 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
3552 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
3555 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
3558 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
3561 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
3565 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
3568 gdb_last_signal_packet(connection
, packet
, packet_size
);
3569 /* '?' is sent after the eventual '!' */
3570 if (!warn_use_ext
&& !gdb_con
->extended_protocol
) {
3571 warn_use_ext
= true;
3572 LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3573 connection
->service
->port
, connection
->service
->port
);
3579 gdb_thread_packet(connection
, packet
, packet_size
);
3580 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3582 if (gdb_con
->mem_write_error
) {
3583 LOG_ERROR("Memory write failure!");
3585 /* now that we have reported the memory write error,
3586 * we can clear the condition */
3587 gdb_con
->mem_write_error
= false;
3590 bool nostep
= false;
3591 bool already_running
= false;
3592 if (target
->state
== TARGET_RUNNING
) {
3593 LOG_WARNING("WARNING! The target is already running. "
3594 "All changes GDB did to registers will be discarded! "
3595 "Waiting for target to halt.");
3596 already_running
= true;
3597 } else if (target
->state
!= TARGET_HALTED
) {
3598 LOG_WARNING("The target is not in the halted nor running stated, "
3599 "stepi/continue ignored.");
3601 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
3602 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3603 * sent by GDB first to OpenOCD, thus defeating the check to
3604 * make only the single stepping have the sync feature...
3607 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3608 "from the target.");
3610 gdb_con
->sync
= false;
3612 if (!already_running
&& nostep
) {
3613 /* Either the target isn't in the halted state, then we can't
3614 * step/continue. This might be early setup, etc.
3616 * Or we want to allow GDB to pick up a fresh set of
3617 * register values without modifying the target state.
3620 gdb_sig_halted(connection
);
3622 /* stop forwarding log packets! */
3623 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
3625 /* We're running/stepping, in which case we can
3626 * forward log output until the target is halted
3628 gdb_con
->frontend_state
= TARGET_RUNNING
;
3629 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3631 if (!already_running
) {
3632 /* Here we don't want packet processing to stop even if this fails,
3633 * so we use a local variable instead of retval. */
3634 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
3635 if (retval
!= ERROR_OK
) {
3636 /* we'll never receive a halted
3637 * condition... issue a false one..
3639 gdb_frontend_halted(target
, connection
);
3646 retval
= gdb_v_packet(connection
, packet
, packet_size
);
3649 retval
= gdb_detach(connection
);
3652 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
3653 if (retval
!= ERROR_OK
)
3657 if (gdb_con
->extended_protocol
) {
3658 gdb_con
->attached
= false;
3661 gdb_put_packet(connection
, "OK", 2);
3662 return ERROR_SERVER_REMOTE_CLOSED
;
3664 /* handle extended remote protocol */
3665 gdb_con
->extended_protocol
= true;
3666 gdb_put_packet(connection
, "OK", 2);
3669 /* handle extended restart packet */
3670 gdb_restart_inferior(connection
, packet
, packet_size
);
3675 /* packet supported only by smp target i.e cortex_a.c*/
3676 /* handle smp packet replying coreid played to gbd */
3677 gdb_read_smp_packet(connection
, packet
, packet_size
);
3682 /* packet supported only by smp target i.e cortex_a.c */
3683 /* handle smp packet setting coreid to be played at next
3685 gdb_write_smp_packet(connection
, packet
, packet_size
);
3689 /* File-I/O extension */
3690 /* After gdb uses host-side syscall to complete target file
3691 * I/O, gdb sends host-side syscall return value to target
3693 * The format of 'F' response packet is
3694 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3696 gdb_con
->frontend_state
= TARGET_RUNNING
;
3697 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3698 gdb_fileio_response_packet(connection
, packet
, packet_size
);
3702 /* ignore unknown packets */
3703 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
3704 gdb_put_packet(connection
, "", 0);
3708 /* if a packet handler returned an error, exit input loop */
3709 if (retval
!= ERROR_OK
)
3713 if (gdb_con
->ctrl_c
) {
3714 if (target
->state
== TARGET_RUNNING
) {
3715 struct target
*t
= target
;
3717 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &t
);
3718 retval
= target_halt(t
);
3719 if (retval
== ERROR_OK
)
3720 retval
= target_poll(t
);
3721 if (retval
!= ERROR_OK
)
3722 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3723 gdb_con
->ctrl_c
= false;
3725 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3726 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3730 } while (gdb_con
->buf_cnt
> 0);
3735 static int gdb_input(struct connection
*connection
)
3737 int retval
= gdb_input_inner(connection
);
3738 struct gdb_connection
*gdb_con
= connection
->priv
;
3739 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3742 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3743 if (gdb_con
->closed
)
3744 return ERROR_SERVER_REMOTE_CLOSED
;
3746 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3750 static void gdb_keep_client_alive(struct connection
*connection
)
3752 struct gdb_connection
*gdb_con
= connection
->priv
;
3754 if (gdb_con
->busy
) {
3755 /* do not send packets, retry asap */
3759 switch (gdb_con
->output_flag
) {
3761 /* no need for keep-alive */
3763 case GDB_OUTPUT_ALL
:
3764 /* send an empty O packet */
3765 gdb_output_con(connection
, "");
3772 static const struct service_driver gdb_service_driver
= {
3774 .new_connection_during_keep_alive_handler
= NULL
,
3775 .new_connection_handler
= gdb_new_connection
,
3776 .input_handler
= gdb_input
,
3777 .connection_closed_handler
= gdb_connection_closed
,
3778 .keep_client_alive_handler
= gdb_keep_client_alive
,
3781 static int gdb_target_start(struct target
*target
, const char *port
)
3783 struct gdb_service
*gdb_service
;
3785 gdb_service
= malloc(sizeof(struct gdb_service
));
3790 LOG_INFO("starting gdb server for %s on %s", target_name(target
), port
);
3792 gdb_service
->target
= target
;
3793 gdb_service
->core
[0] = -1;
3794 gdb_service
->core
[1] = -1;
3795 target
->gdb_service
= gdb_service
;
3797 ret
= add_service(&gdb_service_driver
, port
, target
->gdb_max_connections
, gdb_service
);
3798 /* initialize all targets gdb service with the same pointer */
3800 struct target_list
*head
;
3801 foreach_smp_target(head
, target
->smp_targets
) {
3802 struct target
*curr
= head
->target
;
3804 curr
->gdb_service
= gdb_service
;
3810 static int gdb_target_add_one(struct target
*target
)
3812 /* one gdb instance per smp list */
3813 if ((target
->smp
) && (target
->gdb_service
))
3816 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3817 if (!target_supports_gdb_connection(target
)) {
3818 LOG_DEBUG("skip gdb server for target %s", target_name(target
));
3822 if (target
->gdb_port_override
) {
3823 if (strcmp(target
->gdb_port_override
, "disabled") == 0) {
3824 LOG_INFO("gdb port disabled");
3827 return gdb_target_start(target
, target
->gdb_port_override
);
3830 if (strcmp(gdb_port
, "disabled") == 0) {
3831 LOG_INFO("gdb port disabled");
3835 int retval
= gdb_target_start(target
, gdb_port_next
);
3836 if (retval
== ERROR_OK
) {
3837 /* save the port number so can be queried with
3838 * $target_name cget -gdb-port
3840 target
->gdb_port_override
= strdup(gdb_port_next
);
3843 /* If we can parse the port number
3844 * then we increment the port number for the next target.
3847 portnumber
= strtol(gdb_port_next
, &end
, 0);
3849 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3850 free(gdb_port_next
);
3852 gdb_port_next
= alloc_printf("%ld", portnumber
+1);
3854 /* Don't increment if gdb_port is 0, since we're just
3855 * trying to allocate an unused port. */
3856 gdb_port_next
= strdup("0");
3864 int gdb_target_add_all(struct target
*target
)
3867 LOG_WARNING("gdb services need one or more targets defined");
3872 int retval
= gdb_target_add_one(target
);
3873 if (retval
!= ERROR_OK
)
3876 target
= target
->next
;
3882 COMMAND_HANDLER(handle_gdb_sync_command
)
3885 return ERROR_COMMAND_SYNTAX_ERROR
;
3887 if (!current_gdb_connection
) {
3889 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3893 current_gdb_connection
->sync
= true;
3898 /* daemon configuration command gdb_port */
3899 COMMAND_HANDLER(handle_gdb_port_command
)
3901 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3902 if (retval
== ERROR_OK
) {
3903 free(gdb_port_next
);
3904 gdb_port_next
= strdup(gdb_port
);
3909 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3912 return ERROR_COMMAND_SYNTAX_ERROR
;
3914 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3918 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3921 return ERROR_COMMAND_SYNTAX_ERROR
;
3923 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3927 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3930 return ERROR_COMMAND_SYNTAX_ERROR
;
3932 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3936 COMMAND_HANDLER(handle_gdb_report_register_access_error
)
3939 return ERROR_COMMAND_SYNTAX_ERROR
;
3941 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_register_access_error
);
3945 /* gdb_breakpoint_override */
3946 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3948 if (CMD_ARGC
== 0) {
3950 } else if (CMD_ARGC
== 1) {
3951 gdb_breakpoint_override
= 1;
3952 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3953 gdb_breakpoint_override_type
= BKPT_HARD
;
3954 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3955 gdb_breakpoint_override_type
= BKPT_SOFT
;
3956 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3957 gdb_breakpoint_override
= 0;
3959 return ERROR_COMMAND_SYNTAX_ERROR
;
3960 if (gdb_breakpoint_override
)
3961 LOG_USER("force %s breakpoints",
3962 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3964 LOG_USER("breakpoint type is not overridden");
3969 COMMAND_HANDLER(handle_gdb_target_description_command
)
3972 return ERROR_COMMAND_SYNTAX_ERROR
;
3974 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3978 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3981 uint32_t tdesc_length
;
3982 struct target
*target
= get_current_target(CMD_CTX
);
3984 int retval
= gdb_generate_target_description(target
, &tdesc
);
3985 if (retval
!= ERROR_OK
) {
3986 LOG_ERROR("Unable to Generate Target Description");
3990 tdesc_length
= strlen(tdesc
);
3992 struct fileio
*fileio
;
3993 size_t size_written
;
3995 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3996 if (!tdesc_filename
) {
3997 retval
= ERROR_FAIL
;
4001 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
4003 if (retval
!= ERROR_OK
) {
4004 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
4008 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
4010 fileio_close(fileio
);
4012 if (retval
!= ERROR_OK
)
4013 LOG_ERROR("Error while writing the tdesc file");
4016 free(tdesc_filename
);
4022 static const struct command_registration gdb_command_handlers
[] = {
4025 .handler
= handle_gdb_sync_command
,
4026 .mode
= COMMAND_ANY
,
4027 .help
= "next stepi will return immediately allowing "
4028 "GDB to fetch register state without affecting "
4034 .handler
= handle_gdb_port_command
,
4035 .mode
= COMMAND_CONFIG
,
4036 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4037 "server listens for the next port number after the "
4038 "base port number specified. "
4039 "No arguments reports GDB port. \"pipe\" means listen to stdin "
4040 "output to stdout, an integer is base port number, \"disabled\" disables "
4041 "port. Any other string is are interpreted as named pipe to listen to. "
4042 "Output pipe is the same name as input pipe, but with 'o' appended.",
4043 .usage
= "[port_num]",
4046 .name
= "gdb_memory_map",
4047 .handler
= handle_gdb_memory_map_command
,
4048 .mode
= COMMAND_CONFIG
,
4049 .help
= "enable or disable memory map",
4050 .usage
= "('enable'|'disable')"
4053 .name
= "gdb_flash_program",
4054 .handler
= handle_gdb_flash_program_command
,
4055 .mode
= COMMAND_CONFIG
,
4056 .help
= "enable or disable flash program",
4057 .usage
= "('enable'|'disable')"
4060 .name
= "gdb_report_data_abort",
4061 .handler
= handle_gdb_report_data_abort_command
,
4062 .mode
= COMMAND_CONFIG
,
4063 .help
= "enable or disable reporting data aborts",
4064 .usage
= "('enable'|'disable')"
4067 .name
= "gdb_report_register_access_error",
4068 .handler
= handle_gdb_report_register_access_error
,
4069 .mode
= COMMAND_CONFIG
,
4070 .help
= "enable or disable reporting register access errors",
4071 .usage
= "('enable'|'disable')"
4074 .name
= "gdb_breakpoint_override",
4075 .handler
= handle_gdb_breakpoint_override_command
,
4076 .mode
= COMMAND_ANY
,
4077 .help
= "Display or specify type of breakpoint "
4078 "to be used by gdb 'break' commands.",
4079 .usage
= "('hard'|'soft'|'disable')"
4082 .name
= "gdb_target_description",
4083 .handler
= handle_gdb_target_description_command
,
4084 .mode
= COMMAND_CONFIG
,
4085 .help
= "enable or disable target description",
4086 .usage
= "('enable'|'disable')"
4089 .name
= "gdb_save_tdesc",
4090 .handler
= handle_gdb_save_tdesc_command
,
4091 .mode
= COMMAND_EXEC
,
4092 .help
= "Save the target description file",
4095 COMMAND_REGISTRATION_DONE
4098 int gdb_register_commands(struct command_context
*cmd_ctx
)
4100 gdb_port
= strdup("3333");
4101 gdb_port_next
= strdup("3333");
4102 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);
4105 void gdb_service_free(void)
4108 free(gdb_port_next
);
4111 int gdb_get_actual_connections(void)
4113 return gdb_actual_connections
;