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 doesn't accept 'O' packets but accepts notifications */
58 /* GDB accepts 'O' packets */
62 struct target_desc_format
{
64 uint32_t tdesc_length
;
67 /* private connection data for GDB */
68 struct gdb_connection
{
69 char buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for null-termination */
73 enum target_state frontend_state
;
74 struct image
*vflash_image
;
76 /* set to prevent re-entrance from log messages during gdb_get_packet()
77 * and gdb_put_packet(). */
80 /* set flag to true if you want the next stepi to return immediately.
81 * allowing GDB to pick up a fresh set of register values from the target
82 * without modifying the target state. */
84 /* We delay reporting memory write errors until next step/continue or memory
85 * write. This improves performance of gdb load significantly as the GDB packet
86 * can be replied immediately and a new GDB packet will be ready without delay
87 * (ca. 10% or so...). */
89 /* with extended-remote it seems we need to better emulate attach/detach.
90 * what this means is we reply with a W stop reply after a kill packet,
91 * normally we reply with a S reply via gdb_last_signal_packet.
92 * as a side note this behaviour only effects gdb > 6.8 */
94 /* set when extended protocol is used */
95 bool extended_protocol
;
96 /* temporarily used for target description support */
97 struct target_desc_format target_desc
;
98 /* temporarily used for thread list support */
100 /* flag to mask the output from gdb_log_callback() */
101 enum gdb_output_flag output_flag
;
102 /* Unique index for this GDB connection. */
103 unsigned int unique_index
;
107 #define _DEBUG_GDB_IO_
110 static struct gdb_connection
*current_gdb_connection
;
112 static int gdb_breakpoint_override
;
113 static enum breakpoint_type gdb_breakpoint_override_type
;
115 static int gdb_error(struct connection
*connection
, int retval
);
116 static char *gdb_port
;
117 static char *gdb_port_next
;
119 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
120 const char *function
, const char *string
);
122 static void gdb_sig_halted(struct connection
*connection
);
124 /* number of gdb connections, mainly to suppress gdb related debugging spam
125 * in helper/log.c when no gdb connections are actually active */
126 static int gdb_actual_connections
;
128 /* set if we are sending a memory map to gdb
129 * via qXfer:memory-map:read packet */
130 /* enabled by default*/
131 static int gdb_use_memory_map
= 1;
132 /* enabled by default*/
133 static int gdb_flash_program
= 1;
135 /* if set, data aborts cause an error to be reported in memory read packets
136 * see the code in gdb_read_memory_packet() for further explanations.
137 * Disabled by default.
139 static int gdb_report_data_abort
;
140 /* If set, errors when accessing registers are reported to gdb. Disabled by
142 static int gdb_report_register_access_error
;
144 /* set if we are sending target descriptions to gdb
145 * via qXfer:features:read packet */
146 /* enabled by default */
147 static int gdb_use_target_description
= 1;
149 /* current processing free-run type, used by file-I/O */
150 static char gdb_running_type
;
152 static int gdb_last_signal(struct target
*target
)
154 LOG_TARGET_DEBUG(target
, "Debug reason is: %s",
155 target_debug_reason_str(target
->debug_reason
));
157 switch (target
->debug_reason
) {
158 case DBG_REASON_DBGRQ
:
159 return 0x2; /* SIGINT */
160 case DBG_REASON_BREAKPOINT
:
161 case DBG_REASON_WATCHPOINT
:
162 case DBG_REASON_WPTANDBKPT
:
163 return 0x05; /* SIGTRAP */
164 case DBG_REASON_SINGLESTEP
:
165 return 0x05; /* SIGTRAP */
166 case DBG_REASON_EXC_CATCH
:
168 case DBG_REASON_NOTHALTED
:
169 return 0x0; /* no signal... shouldn't happen */
171 LOG_USER("undefined debug reason %d (%s) - target needs reset",
172 target
->debug_reason
,
173 target_debug_reason_str(target
->debug_reason
));
178 static int check_pending(struct connection
*connection
,
179 int timeout_s
, int *got_data
)
181 /* a non-blocking socket will block if there is 0 bytes available on the socket,
182 * but return with as many bytes as are available immediately
186 struct gdb_connection
*gdb_con
= connection
->priv
;
192 if (gdb_con
->buf_cnt
> 0) {
198 FD_SET(connection
->fd
, &read_fds
);
200 tv
.tv_sec
= timeout_s
;
202 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
203 /* This can typically be because a "monitor" command took too long
204 * before printing any progress messages
207 return ERROR_GDB_TIMEOUT
;
211 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
215 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
217 struct gdb_connection
*gdb_con
= connection
->priv
;
218 int retval
= ERROR_OK
;
220 #ifdef _DEBUG_GDB_IO_
224 if (connection
->service
->type
!= CONNECTION_TCP
)
225 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
227 retval
= check_pending(connection
, 1, NULL
);
228 if (retval
!= ERROR_OK
)
230 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
235 if (gdb_con
->buf_cnt
> 0)
237 if (gdb_con
->buf_cnt
== 0) {
238 LOG_DEBUG("GDB connection closed by the remote client");
239 gdb_con
->closed
= true;
240 return ERROR_SERVER_REMOTE_CLOSED
;
244 bool retry
= (WSAGetLastError() == WSAEWOULDBLOCK
);
246 bool retry
= (errno
== EAGAIN
);
250 // Try again after a delay
253 // Print error and close the socket
254 log_socket_error("GDB");
255 gdb_con
->closed
= true;
256 return ERROR_SERVER_REMOTE_CLOSED
;
260 #ifdef _DEBUG_GDB_IO_
261 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
262 LOG_DEBUG("received '%s'", debug_buffer
);
266 gdb_con
->buf_p
= gdb_con
->buffer
;
268 *next_char
= *(gdb_con
->buf_p
++);
269 if (gdb_con
->buf_cnt
> 0)
270 connection
->input_pending
= true;
272 connection
->input_pending
= false;
273 #ifdef _DEBUG_GDB_IO_
274 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
281 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
282 * held in registers in the inner loop.
284 * For small caches and embedded systems this is important!
286 static inline int gdb_get_char_fast(struct connection
*connection
,
287 int *next_char
, char **buf_p
, int *buf_cnt
)
289 int retval
= ERROR_OK
;
291 if ((*buf_cnt
)-- > 0) {
292 *next_char
= **buf_p
;
295 connection
->input_pending
= true;
297 connection
->input_pending
= false;
299 #ifdef _DEBUG_GDB_IO_
300 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
306 struct gdb_connection
*gdb_con
= connection
->priv
;
307 gdb_con
->buf_p
= *buf_p
;
308 gdb_con
->buf_cnt
= *buf_cnt
;
309 retval
= gdb_get_char_inner(connection
, next_char
);
310 *buf_p
= gdb_con
->buf_p
;
311 *buf_cnt
= gdb_con
->buf_cnt
;
316 static int gdb_get_char(struct connection
*connection
, int *next_char
)
318 struct gdb_connection
*gdb_con
= connection
->priv
;
319 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
322 static int gdb_putback_char(struct connection
*connection
, int last_char
)
324 struct gdb_connection
*gdb_con
= connection
->priv
;
326 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
327 *(--gdb_con
->buf_p
) = last_char
;
330 LOG_ERROR("BUG: couldn't put character back");
335 /* The only way we can detect that the socket is closed is the first time
336 * we write to it, we will fail. Subsequent write operations will
337 * succeed. Shudder! */
338 static int gdb_write(struct connection
*connection
, void *data
, int len
)
340 struct gdb_connection
*gdb_con
= connection
->priv
;
341 if (gdb_con
->closed
) {
342 LOG_DEBUG("GDB socket marked as closed, cannot write to it.");
343 return ERROR_SERVER_REMOTE_CLOSED
;
346 if (connection_write(connection
, data
, len
) == len
)
349 LOG_WARNING("Error writing to GDB socket. Dropping the connection.");
350 gdb_con
->closed
= true;
351 return ERROR_SERVER_REMOTE_CLOSED
;
354 static void gdb_log_incoming_packet(struct connection
*connection
, char *packet
)
356 if (!LOG_LEVEL_IS(LOG_LVL_DEBUG
))
359 struct target
*target
= get_target_from_connection(connection
);
360 struct gdb_connection
*gdb_connection
= connection
->priv
;
362 /* Avoid dumping non-printable characters to the terminal */
363 const unsigned packet_len
= strlen(packet
);
364 const char *nonprint
= find_nonprint_char(packet
, packet_len
);
366 /* Does packet at least have a prefix that is printable?
367 * Look within the first 50 chars of the packet. */
368 const char *colon
= memchr(packet
, ':', MIN(50, packet_len
));
369 const bool packet_has_prefix
= (colon
);
370 const bool packet_prefix_printable
= (packet_has_prefix
&& nonprint
> colon
);
372 if (packet_prefix_printable
) {
373 const unsigned int prefix_len
= colon
- packet
+ 1; /* + 1 to include the ':' */
374 const unsigned int payload_len
= packet_len
- prefix_len
;
375 LOG_TARGET_DEBUG(target
, "{%d} received packet: %.*s<binary-data-%u-bytes>",
376 gdb_connection
->unique_index
, prefix_len
, packet
, payload_len
);
378 LOG_TARGET_DEBUG(target
, "{%d} received packet: <binary-data-%u-bytes>",
379 gdb_connection
->unique_index
, packet_len
);
382 /* All chars printable, dump the packet as is */
383 LOG_TARGET_DEBUG(target
, "{%d} received packet: %s", gdb_connection
->unique_index
, packet
);
387 static void gdb_log_outgoing_packet(struct connection
*connection
, char *packet_buf
,
388 unsigned int packet_len
, unsigned char checksum
)
390 if (!LOG_LEVEL_IS(LOG_LVL_DEBUG
))
393 struct target
*target
= get_target_from_connection(connection
);
394 struct gdb_connection
*gdb_connection
= connection
->priv
;
396 if (find_nonprint_char(packet_buf
, packet_len
))
397 LOG_TARGET_DEBUG(target
, "{%d} sending packet: $<binary-data-%u-bytes>#%2.2x",
398 gdb_connection
->unique_index
, packet_len
, checksum
);
400 LOG_TARGET_DEBUG(target
, "{%d} sending packet: $%.*s#%2.2x",
401 gdb_connection
->unique_index
, packet_len
, packet_buf
, checksum
);
404 static int gdb_put_packet_inner(struct connection
*connection
,
405 char *buffer
, int len
)
408 unsigned char my_checksum
= 0;
411 struct gdb_connection
*gdb_con
= connection
->priv
;
413 for (i
= 0; i
< len
; i
++)
414 my_checksum
+= buffer
[i
];
416 #ifdef _DEBUG_GDB_IO_
418 * At this point we should have nothing in the input queue from GDB,
419 * however sometimes '-' is sent even though we've already received
420 * an ACK (+) for everything we've sent off.
424 retval
= check_pending(connection
, 0, &gotdata
);
425 if (retval
!= ERROR_OK
)
429 retval
= gdb_get_char(connection
, &reply
);
430 if (retval
!= ERROR_OK
)
433 /* fix a problem with some IAR tools */
434 gdb_putback_char(connection
, reply
);
435 LOG_DEBUG("Unexpected start of new packet");
439 LOG_WARNING("Discard unexpected char %c", reply
);
444 gdb_log_outgoing_packet(connection
, buffer
, len
, my_checksum
);
446 char local_buffer
[1024];
447 local_buffer
[0] = '$';
448 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
449 /* performance gain on smaller packets by only a single call to gdb_write() */
450 memcpy(local_buffer
+ 1, buffer
, len
++);
451 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
452 retval
= gdb_write(connection
, local_buffer
, len
);
453 if (retval
!= ERROR_OK
)
456 /* larger packets are transmitted directly from caller supplied buffer
457 * by several calls to gdb_write() to avoid dynamic allocation */
458 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
459 retval
= gdb_write(connection
, local_buffer
, 1);
460 if (retval
!= ERROR_OK
)
462 retval
= gdb_write(connection
, buffer
, len
);
463 if (retval
!= ERROR_OK
)
465 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
466 if (retval
!= ERROR_OK
)
470 if (gdb_con
->noack_mode
)
473 retval
= gdb_get_char(connection
, &reply
);
474 if (retval
!= ERROR_OK
)
478 gdb_log_incoming_packet(connection
, "+");
480 } else if (reply
== '-') {
481 /* Stop sending output packets for now */
482 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
483 gdb_log_incoming_packet(connection
, "-");
484 LOG_WARNING("negative reply, retrying");
485 } else if (reply
== 0x3) {
486 gdb_con
->ctrl_c
= true;
487 gdb_log_incoming_packet(connection
, "<Ctrl-C>");
488 retval
= gdb_get_char(connection
, &reply
);
489 if (retval
!= ERROR_OK
)
492 gdb_log_incoming_packet(connection
, "+");
494 } else if (reply
== '-') {
495 /* Stop sending output packets for now */
496 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
497 gdb_log_incoming_packet(connection
, "-");
498 LOG_WARNING("negative reply, retrying");
499 } else if (reply
== '$') {
500 LOG_ERROR("GDB missing ack(1) - assumed good");
501 gdb_putback_char(connection
, reply
);
504 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
505 gdb_con
->closed
= true;
506 return ERROR_SERVER_REMOTE_CLOSED
;
508 } else if (reply
== '$') {
509 LOG_ERROR("GDB missing ack(2) - assumed good");
510 gdb_putback_char(connection
, reply
);
513 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
515 gdb_con
->closed
= true;
516 return ERROR_SERVER_REMOTE_CLOSED
;
520 return ERROR_SERVER_REMOTE_CLOSED
;
525 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
527 struct gdb_connection
*gdb_con
= connection
->priv
;
528 gdb_con
->busy
= true;
529 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
530 gdb_con
->busy
= false;
532 /* we sent some data, reset timer for keep alive messages */
538 static inline int fetch_packet(struct connection
*connection
,
539 int *checksum_ok
, int noack
, int *len
, char *buffer
)
541 unsigned char my_checksum
= 0;
544 int retval
= ERROR_OK
;
546 struct gdb_connection
*gdb_con
= connection
->priv
;
551 /* move this over into local variables to use registers and give the
552 * more freedom to optimize */
553 char *buf_p
= gdb_con
->buf_p
;
554 int buf_cnt
= gdb_con
->buf_cnt
;
557 /* The common case is that we have an entire packet with no escape chars.
558 * We need to leave at least 2 bytes in the buffer to have
559 * gdb_get_char() update various bits and bobs correctly.
561 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
562 /* The compiler will struggle a bit with constant propagation and
563 * aliasing, so we help it by showing that these values do not
564 * change inside the loop
568 int run
= buf_cnt
- 2;
574 if (character
== '#') {
575 /* Danger! character can be '#' when esc is
576 * used so we need an explicit boolean for done here. */
581 if (character
== '}') {
582 /* data transmitted in binary mode (X packet)
583 * uses 0x7d as escape character */
584 my_checksum
+= character
& 0xff;
587 my_checksum
+= character
& 0xff;
588 buffer
[count
++] = (character
^ 0x20) & 0xff;
590 my_checksum
+= character
& 0xff;
591 buffer
[count
++] = character
& 0xff;
600 LOG_ERROR("packet buffer too small");
601 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
605 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
606 if (retval
!= ERROR_OK
)
609 if (character
== '#')
612 if (character
== '}') {
613 /* data transmitted in binary mode (X packet)
614 * uses 0x7d as escape character */
615 my_checksum
+= character
& 0xff;
617 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
618 if (retval
!= ERROR_OK
)
621 my_checksum
+= character
& 0xff;
622 buffer
[count
++] = (character
^ 0x20) & 0xff;
624 my_checksum
+= character
& 0xff;
625 buffer
[count
++] = character
& 0xff;
629 gdb_con
->buf_p
= buf_p
;
630 gdb_con
->buf_cnt
= buf_cnt
;
632 if (retval
!= ERROR_OK
)
637 retval
= gdb_get_char(connection
, &character
);
638 if (retval
!= ERROR_OK
)
640 checksum
[0] = character
;
641 retval
= gdb_get_char(connection
, &character
);
642 if (retval
!= ERROR_OK
)
644 checksum
[1] = character
;
648 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
653 static int gdb_get_packet_inner(struct connection
*connection
,
654 char *buffer
, int *len
)
658 struct gdb_connection
*gdb_con
= connection
->priv
;
662 retval
= gdb_get_char(connection
, &character
);
663 if (retval
!= ERROR_OK
)
666 #ifdef _DEBUG_GDB_IO_
667 LOG_DEBUG("character: '%c'", character
);
674 gdb_log_incoming_packet(connection
, "+");
675 /* According to the GDB documentation
676 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
677 * "gdb sends a final `+` acknowledgment of the stub's `OK`
678 * response, which can be safely ignored by the stub."
679 * However OpenOCD server already is in noack mode at this
680 * point and instead of ignoring this it was emitting a
681 * warning. This code makes server ignore the first ACK
682 * that will be received after going into noack mode,
683 * warning only about subsequent ACK's. */
684 if (gdb_con
->noack_mode
> 1) {
685 LOG_WARNING("acknowledgment received, but no packet pending");
686 } else if (gdb_con
->noack_mode
) {
687 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
688 gdb_con
->noack_mode
= 2;
692 gdb_log_incoming_packet(connection
, "-");
693 LOG_WARNING("negative acknowledgment, but no packet pending");
696 gdb_log_incoming_packet(connection
, "<Ctrl-C>");
697 gdb_con
->ctrl_c
= true;
701 LOG_WARNING("ignoring character 0x%x", character
);
704 } while (character
!= '$');
707 /* explicit code expansion here to get faster inlined code in -O3 by not
708 * calculating checksum */
709 if (gdb_con
->noack_mode
) {
710 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
711 if (retval
!= ERROR_OK
)
714 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
715 if (retval
!= ERROR_OK
)
719 if (gdb_con
->noack_mode
) {
720 /* checksum is not checked in noack mode */
724 retval
= gdb_write(connection
, "+", 1);
725 if (retval
!= ERROR_OK
)
731 return ERROR_SERVER_REMOTE_CLOSED
;
736 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
738 struct gdb_connection
*gdb_con
= connection
->priv
;
739 gdb_con
->busy
= true;
740 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
741 gdb_con
->busy
= false;
745 static int gdb_output_con(struct connection
*connection
, const char *line
)
750 bin_size
= strlen(line
);
752 hex_buffer
= malloc(bin_size
* 2 + 2);
754 return ERROR_GDB_BUFFER_TOO_SMALL
;
757 size_t pkt_len
= hexify(hex_buffer
+ 1, (const uint8_t *)line
, bin_size
,
759 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
765 static int gdb_output(struct command_context
*context
, const char *line
)
767 /* this will be dumped to the log and also sent as an O packet if possible */
768 LOG_USER_N("%s", line
);
772 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
774 struct gdb_connection
*gdb_connection
= connection
->priv
;
776 char stop_reason
[32];
777 char current_thread
[25];
781 rtos_update_threads(target
);
783 if (target
->debug_reason
== DBG_REASON_EXIT
) {
784 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
788 target
->rtos
->current_threadid
= target
->rtos
->current_thread
;
789 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &ct
);
794 if (gdb_connection
->ctrl_c
) {
795 LOG_TARGET_DEBUG(target
, "Responding with signal 2 (SIGINT) to debugger due to Ctrl-C");
798 signal_var
= gdb_last_signal(ct
);
800 stop_reason
[0] = '\0';
801 if (ct
->debug_reason
== DBG_REASON_WATCHPOINT
) {
802 enum watchpoint_rw hit_wp_type
;
803 target_addr_t hit_wp_address
;
805 if (watchpoint_hit(ct
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
807 switch (hit_wp_type
) {
809 snprintf(stop_reason
, sizeof(stop_reason
),
810 "watch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
813 snprintf(stop_reason
, sizeof(stop_reason
),
814 "rwatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
817 snprintf(stop_reason
, sizeof(stop_reason
),
818 "awatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
826 current_thread
[0] = '\0';
828 snprintf(current_thread
, sizeof(current_thread
), "thread:%" PRIx64
";",
829 target
->rtos
->current_thread
);
831 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s%s",
832 signal_var
, stop_reason
, current_thread
);
834 gdb_connection
->ctrl_c
= false;
837 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
838 gdb_connection
->frontend_state
= TARGET_HALTED
;
841 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
843 struct gdb_connection
*gdb_connection
= connection
->priv
;
844 char fileio_command
[256];
846 bool program_exited
= false;
848 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
849 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
850 target
->fileio_info
->param_1
,
851 target
->fileio_info
->param_2
+ 1, /* len + trailing zero */
852 target
->fileio_info
->param_3
,
853 target
->fileio_info
->param_4
);
854 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
855 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
856 target
->fileio_info
->param_1
);
857 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
858 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
859 target
->fileio_info
->param_1
,
860 target
->fileio_info
->param_2
,
861 target
->fileio_info
->param_3
);
862 else if (strcmp(target
->fileio_info
->identifier
, "write") == 0)
863 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
864 target
->fileio_info
->param_1
,
865 target
->fileio_info
->param_2
,
866 target
->fileio_info
->param_3
);
867 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
868 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
869 target
->fileio_info
->param_1
,
870 target
->fileio_info
->param_2
,
871 target
->fileio_info
->param_3
);
872 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
873 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
874 target
->fileio_info
->param_1
,
875 target
->fileio_info
->param_2
+ 1, /* len + trailing zero */
876 target
->fileio_info
->param_3
,
877 target
->fileio_info
->param_4
+ 1); /* len + trailing zero */
878 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
879 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
880 target
->fileio_info
->param_1
,
881 target
->fileio_info
->param_2
+ 1); /* len + trailing zero */
882 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
883 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
884 target
->fileio_info
->param_1
,
885 target
->fileio_info
->param_2
,
886 target
->fileio_info
->param_3
);
887 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 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
, "gettimeofday") == 0)
892 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
893 target
->fileio_info
->param_1
,
894 target
->fileio_info
->param_2
);
895 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
896 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
897 target
->fileio_info
->param_1
);
898 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
899 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
900 target
->fileio_info
->param_1
,
901 target
->fileio_info
->param_2
+ 1); /* len + trailing zero */
902 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
903 /* If target hits exit syscall, report to GDB the program is terminated.
904 * In addition, let target run its own exit syscall handler. */
905 program_exited
= true;
906 sprintf(fileio_command
, "W%02" PRIx64
, target
->fileio_info
->param_1
);
908 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
910 /* encounter unknown syscall, continue */
911 gdb_connection
->frontend_state
= TARGET_RUNNING
;
912 target_resume(target
, 1, 0x0, 0, 0);
916 command_len
= strlen(fileio_command
);
917 gdb_put_packet(connection
, fileio_command
, command_len
);
919 if (program_exited
) {
920 /* Use target_resume() to let target run its own exit syscall handler. */
921 gdb_connection
->frontend_state
= TARGET_RUNNING
;
922 target_resume(target
, 1, 0x0, 0, 0);
924 gdb_connection
->frontend_state
= TARGET_HALTED
;
925 rtos_update_threads(target
);
929 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
931 struct gdb_connection
*gdb_connection
= connection
->priv
;
933 /* In the GDB protocol when we are stepping or continuing execution,
934 * we have a lingering reply. Upon receiving a halted event
935 * when we have that lingering packet, we reply to the original
936 * step or continue packet.
938 * Executing monitor commands can bring the target in and
939 * out of the running state so we'll see lots of TARGET_EVENT_XXX
940 * that are to be ignored.
942 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
943 /* stop forwarding log packets! */
944 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
946 /* check fileio first */
947 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
948 gdb_fileio_reply(target
, connection
);
950 gdb_signal_reply(target
, connection
);
954 static int gdb_target_callback_event_handler(struct target
*target
,
955 enum target_event event
, void *priv
)
957 struct connection
*connection
= priv
;
958 struct gdb_service
*gdb_service
= connection
->service
->priv
;
960 if (gdb_service
->target
!= target
)
964 case TARGET_EVENT_GDB_HALT
:
965 gdb_frontend_halted(target
, connection
);
967 case TARGET_EVENT_HALTED
:
968 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
977 static int gdb_new_connection(struct connection
*connection
)
979 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
980 struct target
*target
;
983 static unsigned int next_unique_id
= 1;
985 target
= get_target_from_connection(connection
);
986 connection
->priv
= gdb_connection
;
987 connection
->cmd_ctx
->current_target
= target
;
989 /* initialize gdb connection information */
990 gdb_connection
->buf_p
= gdb_connection
->buffer
;
991 gdb_connection
->buf_cnt
= 0;
992 gdb_connection
->ctrl_c
= false;
993 gdb_connection
->frontend_state
= TARGET_HALTED
;
994 gdb_connection
->vflash_image
= NULL
;
995 gdb_connection
->closed
= false;
996 gdb_connection
->busy
= false;
997 gdb_connection
->noack_mode
= 0;
998 gdb_connection
->sync
= false;
999 gdb_connection
->mem_write_error
= false;
1000 gdb_connection
->attached
= true;
1001 gdb_connection
->extended_protocol
= false;
1002 gdb_connection
->target_desc
.tdesc
= NULL
;
1003 gdb_connection
->target_desc
.tdesc_length
= 0;
1004 gdb_connection
->thread_list
= NULL
;
1005 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
1006 gdb_connection
->unique_index
= next_unique_id
++;
1008 /* output goes through gdb connection */
1009 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
1011 /* we must remove all breakpoints registered to the target as a previous
1012 * GDB session could leave dangling breakpoints if e.g. communication
1015 breakpoint_clear_target(target
);
1016 watchpoint_clear_target(target
);
1018 /* Since version 3.95 (gdb-19990504), with the exclusion of 6.5~6.8, GDB
1019 * sends an ACK at connection with the following comment in its source code:
1020 * "Ack any packet which the remote side has already sent."
1021 * LLDB does the same since the first gdb-remote implementation.
1022 * Remove the initial ACK from the incoming buffer.
1024 retval
= gdb_get_char(connection
, &initial_ack
);
1025 if (retval
!= ERROR_OK
)
1028 if (initial_ack
!= '+')
1029 gdb_putback_char(connection
, initial_ack
);
1031 target_call_event_callbacks(target
, TARGET_EVENT_GDB_ATTACH
);
1034 /* clean previous rtos session if supported*/
1035 if (target
->rtos
->type
->clean
)
1036 target
->rtos
->type
->clean(target
);
1038 /* update threads */
1039 rtos_update_threads(target
);
1042 if (gdb_use_memory_map
) {
1043 /* Connect must fail if the memory map can't be set up correctly.
1045 * This will cause an auto_probe to be invoked, which is either
1046 * a no-op or it will fail when the target isn't ready(e.g. not halted).
1048 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1049 struct flash_bank
*p
;
1050 p
= get_flash_bank_by_num_noprobe(i
);
1051 if (p
->target
!= target
)
1053 retval
= get_flash_bank_by_num(i
, &p
);
1054 if (retval
!= ERROR_OK
) {
1055 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target "
1056 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1062 log_printf_lf(all_targets
->next
? LOG_LVL_INFO
: LOG_LVL_DEBUG
,
1063 __FILE__
, __LINE__
, __func__
,
1064 "New GDB Connection: %d, Target %s, state: %s",
1065 gdb_connection
->unique_index
,
1066 target_name(target
),
1067 target_state_name(target
));
1069 if (!target_was_examined(target
)) {
1070 LOG_ERROR("Target %s not examined yet, refuse gdb connection %d!",
1071 target_name(target
), gdb_connection
->unique_index
);
1072 return ERROR_TARGET_NOT_EXAMINED
;
1074 gdb_actual_connections
++;
1076 if (target
->state
!= TARGET_HALTED
)
1077 LOG_WARNING("GDB connection %d on target %s not halted",
1078 gdb_actual_connections
, target_name(target
));
1080 /* DANGER! If we fail subsequently, we must remove this handler,
1081 * otherwise we occasionally see crashes as the timer can invoke the
1084 * register callback to be informed about target events */
1085 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1087 log_add_callback(gdb_log_callback
, connection
);
1092 static int gdb_connection_closed(struct connection
*connection
)
1094 struct target
*target
;
1095 struct gdb_connection
*gdb_connection
= connection
->priv
;
1097 target
= get_target_from_connection(connection
);
1099 /* we're done forwarding messages. Tear down callback before
1100 * cleaning up connection.
1102 log_remove_callback(gdb_log_callback
, connection
);
1104 gdb_actual_connections
--;
1105 LOG_DEBUG("{%d} GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1106 gdb_connection
->unique_index
,
1107 target_name(target
),
1108 target_state_name(target
),
1109 gdb_actual_connections
);
1111 /* see if an image built with vFlash commands is left */
1112 if (gdb_connection
->vflash_image
) {
1113 image_close(gdb_connection
->vflash_image
);
1114 free(gdb_connection
->vflash_image
);
1115 gdb_connection
->vflash_image
= NULL
;
1118 /* if this connection registered a debug-message receiver delete it */
1119 delete_debug_msg_receiver(connection
->cmd_ctx
, target
);
1121 free(connection
->priv
);
1122 connection
->priv
= NULL
;
1124 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1126 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
1128 target_call_event_callbacks(target
, TARGET_EVENT_GDB_DETACH
);
1133 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1136 snprintf(err
, 4, "E%2.2X", the_error
);
1137 gdb_put_packet(connection
, err
, 3);
1140 static int gdb_last_signal_packet(struct connection
*connection
,
1141 char const *packet
, int packet_size
)
1143 struct target
*target
= get_target_from_connection(connection
);
1144 struct gdb_connection
*gdb_con
= connection
->priv
;
1148 if (!gdb_con
->attached
) {
1149 /* if we are here we have received a kill packet
1150 * reply W stop reply otherwise gdb gets very unhappy */
1151 gdb_put_packet(connection
, "W00", 3);
1155 signal_var
= gdb_last_signal(target
);
1157 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1158 gdb_put_packet(connection
, sig_reply
, 3);
1163 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1165 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1168 return len
- 1 - pos
;
1171 /* Convert register to string of bytes. NB! The # of bits in the
1172 * register might be non-divisible by 8(a byte), in which
1173 * case an entire byte is shown.
1175 * NB! the format on the wire is the target endianness
1177 * The format of reg->value is little endian
1180 static void gdb_str_to_target(struct target
*target
,
1181 char *tstr
, struct reg
*reg
)
1188 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1190 for (i
= 0; i
< buf_len
; i
++) {
1191 int j
= gdb_reg_pos(target
, i
, buf_len
);
1192 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1196 /* copy over in register buffer */
1197 static void gdb_target_to_reg(struct target
*target
,
1198 char const *tstr
, int str_len
, uint8_t *bin
)
1201 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1206 for (i
= 0; i
< str_len
; i
+= 2) {
1208 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1209 LOG_ERROR("BUG: unable to convert register value");
1213 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1218 /* get register value if needed and fill the buffer accordingly */
1219 static int gdb_get_reg_value_as_str(struct target
*target
, char *tstr
, struct reg
*reg
)
1221 int retval
= ERROR_OK
;
1224 retval
= reg
->type
->get(reg
);
1226 const unsigned int len
= DIV_ROUND_UP(reg
->size
, 8) * 2;
1229 gdb_str_to_target(target
, tstr
, reg
);
1231 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE
:
1232 memset(tstr
, 'x', len
);
1239 static int gdb_get_registers_packet(struct connection
*connection
,
1240 char const *packet
, int packet_size
)
1242 struct target
*target
= get_target_from_connection(connection
);
1243 struct reg
**reg_list
;
1246 int reg_packet_size
= 0;
1251 #ifdef _DEBUG_GDB_IO_
1255 if ((target
->rtos
) && (rtos_get_gdb_reg_list(connection
) == ERROR_OK
))
1258 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1260 if (retval
!= ERROR_OK
)
1261 return gdb_error(connection
, retval
);
1263 for (i
= 0; i
< reg_list_size
; i
++) {
1264 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1266 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1269 assert(reg_packet_size
> 0);
1271 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1275 reg_packet_p
= reg_packet
;
1277 for (i
= 0; i
< reg_list_size
; i
++) {
1278 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1280 if (gdb_get_reg_value_as_str(target
, reg_packet_p
, reg_list
[i
]) != ERROR_OK
) {
1283 return gdb_error(connection
, retval
);
1285 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1288 #ifdef _DEBUG_GDB_IO_
1290 char *reg_packet_p_debug
;
1291 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1292 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1293 free(reg_packet_p_debug
);
1297 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1305 static int gdb_set_registers_packet(struct connection
*connection
,
1306 char const *packet
, int packet_size
)
1308 struct target
*target
= get_target_from_connection(connection
);
1310 struct reg
**reg_list
;
1313 char const *packet_p
;
1315 #ifdef _DEBUG_GDB_IO_
1319 /* skip command character */
1323 if (packet_size
% 2) {
1324 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1325 return ERROR_SERVER_REMOTE_CLOSED
;
1328 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1330 if (retval
!= ERROR_OK
)
1331 return gdb_error(connection
, retval
);
1334 for (i
= 0; i
< reg_list_size
; i
++) {
1336 if (!reg_list
[i
] || !reg_list
[i
]->exist
|| reg_list
[i
]->hidden
)
1338 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1340 if (packet_p
+ chars
> packet
+ packet_size
)
1341 LOG_ERROR("BUG: register packet is too small for registers");
1343 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1344 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1346 retval
= reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1347 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1348 LOG_DEBUG("Couldn't set register %s.", reg_list
[i
]->name
);
1351 return gdb_error(connection
, retval
);
1354 /* advance packet pointer */
1360 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1363 gdb_put_packet(connection
, "OK", 2);
1368 static int gdb_get_register_packet(struct connection
*connection
,
1369 char const *packet
, int packet_size
)
1371 struct target
*target
= get_target_from_connection(connection
);
1373 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1374 struct reg
**reg_list
;
1378 #ifdef _DEBUG_GDB_IO_
1382 if ((target
->rtos
) && (rtos_get_gdb_reg(connection
, reg_num
) == ERROR_OK
))
1385 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1387 if (retval
!= ERROR_OK
)
1388 return gdb_error(connection
, retval
);
1390 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1391 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1392 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1393 return ERROR_SERVER_REMOTE_CLOSED
;
1396 reg_packet
= calloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1, 1); /* plus one for string termination null */
1398 if (gdb_get_reg_value_as_str(target
, reg_packet
, reg_list
[reg_num
]) != ERROR_OK
) {
1401 return gdb_error(connection
, retval
);
1404 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1412 static int gdb_set_register_packet(struct connection
*connection
,
1413 char const *packet
, int packet_size
)
1415 struct target
*target
= get_target_from_connection(connection
);
1417 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1418 struct reg
**reg_list
;
1422 #ifdef _DEBUG_GDB_IO_
1426 if (*separator
!= '=') {
1427 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1428 return ERROR_SERVER_REMOTE_CLOSED
;
1430 size_t chars
= strlen(separator
+ 1);
1431 uint8_t *bin_buf
= malloc(chars
/ 2);
1432 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1434 if ((target
->rtos
) &&
1435 (rtos_set_reg(connection
, reg_num
, bin_buf
) == ERROR_OK
)) {
1437 gdb_put_packet(connection
, "OK", 2);
1441 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1443 if (retval
!= ERROR_OK
) {
1445 return gdb_error(connection
, retval
);
1448 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1449 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1450 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1453 return ERROR_SERVER_REMOTE_CLOSED
;
1456 if (chars
!= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2)) {
1457 LOG_ERROR("gdb sent %zu bits for a %" PRIu32
"-bit register (%s)",
1458 chars
* 4, reg_list
[reg_num
]->size
, reg_list
[reg_num
]->name
);
1461 return ERROR_SERVER_REMOTE_CLOSED
;
1464 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1466 retval
= reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1467 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1468 LOG_DEBUG("Couldn't set register %s.", reg_list
[reg_num
]->name
);
1471 return gdb_error(connection
, retval
);
1474 gdb_put_packet(connection
, "OK", 2);
1482 /* No attempt is made to translate the "retval" to
1483 * GDB speak. This has to be done at the calling
1484 * site as no mapping really exists.
1486 static int gdb_error(struct connection
*connection
, int retval
)
1488 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1489 gdb_send_error(connection
, EFAULT
);
1493 static int gdb_read_memory_packet(struct connection
*connection
,
1494 char const *packet
, int packet_size
)
1496 struct target
*target
= get_target_from_connection(connection
);
1504 int retval
= ERROR_OK
;
1506 /* skip command character */
1509 addr
= strtoull(packet
, &separator
, 16);
1511 if (*separator
!= ',') {
1512 LOG_ERROR("incomplete read memory packet received, dropping connection");
1513 return ERROR_SERVER_REMOTE_CLOSED
;
1516 len
= strtoul(separator
+ 1, NULL
, 16);
1519 LOG_WARNING("invalid read memory packet received (len == 0)");
1520 gdb_put_packet(connection
, "", 0);
1524 buffer
= malloc(len
);
1526 LOG_DEBUG("addr: 0x%16.16" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1528 retval
= ERROR_NOT_IMPLEMENTED
;
1530 retval
= rtos_read_buffer(target
, addr
, len
, buffer
);
1531 if (retval
== ERROR_NOT_IMPLEMENTED
)
1532 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1534 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1535 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1536 * At some point this might be fixed in GDB, in which case this code can be removed.
1538 * OpenOCD developers are acutely aware of this problem, but there is nothing
1539 * gained by involving the user in this problem that hopefully will get resolved
1542 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1543 * cmd = view%20audit-trail&database = gdb&pr = 2395
1545 * For now, the default is to fix up things to make current GDB versions work.
1546 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1548 memset(buffer
, 0, len
);
1552 if (retval
== ERROR_OK
) {
1553 hex_buffer
= malloc(len
* 2 + 1);
1555 size_t pkt_len
= hexify(hex_buffer
, buffer
, len
, len
* 2 + 1);
1557 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1561 retval
= gdb_error(connection
, retval
);
1568 static int gdb_write_memory_packet(struct connection
*connection
,
1569 char const *packet
, int packet_size
)
1571 struct target
*target
= get_target_from_connection(connection
);
1579 /* skip command character */
1582 addr
= strtoull(packet
, &separator
, 16);
1584 if (*separator
!= ',') {
1585 LOG_ERROR("incomplete write memory packet received, dropping connection");
1586 return ERROR_SERVER_REMOTE_CLOSED
;
1589 len
= strtoul(separator
+ 1, &separator
, 16);
1591 if (*(separator
++) != ':') {
1592 LOG_ERROR("incomplete write memory packet received, dropping connection");
1593 return ERROR_SERVER_REMOTE_CLOSED
;
1596 buffer
= malloc(len
);
1598 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1600 if (unhexify(buffer
, separator
, len
) != len
)
1601 LOG_ERROR("unable to decode memory packet");
1603 retval
= ERROR_NOT_IMPLEMENTED
;
1605 retval
= rtos_write_buffer(target
, addr
, len
, buffer
);
1606 if (retval
== ERROR_NOT_IMPLEMENTED
)
1607 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1609 if (retval
== ERROR_OK
)
1610 gdb_put_packet(connection
, "OK", 2);
1612 retval
= gdb_error(connection
, retval
);
1619 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1620 char const *packet
, int packet_size
)
1622 struct target
*target
= get_target_from_connection(connection
);
1627 int retval
= ERROR_OK
;
1628 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1629 * the assumption that we're in a download and it's important to go as fast
1631 uint32_t fast_limit
= 8;
1633 /* skip command character */
1636 addr
= strtoull(packet
, &separator
, 16);
1638 if (*separator
!= ',') {
1639 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1640 return ERROR_SERVER_REMOTE_CLOSED
;
1643 len
= strtoul(separator
+ 1, &separator
, 16);
1645 if (*(separator
++) != ':') {
1646 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1647 return ERROR_SERVER_REMOTE_CLOSED
;
1650 struct gdb_connection
*gdb_connection
= connection
->priv
;
1652 if (gdb_connection
->mem_write_error
)
1653 retval
= ERROR_FAIL
;
1655 if (retval
== ERROR_OK
) {
1656 if (len
>= fast_limit
) {
1657 /* By replying the packet *immediately* GDB will send us a new packet
1658 * while we write the last one to the target.
1659 * We only do this for larger writes, so that users who do something like:
1660 * p *((int*)0xdeadbeef)=8675309
1661 * will get immediate feedback that that write failed.
1663 gdb_put_packet(connection
, "OK", 2);
1666 retval
= gdb_error(connection
, retval
);
1667 /* now that we have reported the memory write error, we can clear the condition */
1668 gdb_connection
->mem_write_error
= false;
1669 if (retval
!= ERROR_OK
)
1674 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1676 retval
= ERROR_NOT_IMPLEMENTED
;
1678 retval
= rtos_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1679 if (retval
== ERROR_NOT_IMPLEMENTED
)
1680 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1682 if (retval
!= ERROR_OK
)
1683 gdb_connection
->mem_write_error
= true;
1686 if (len
< fast_limit
) {
1687 if (retval
!= ERROR_OK
) {
1688 gdb_error(connection
, retval
);
1689 gdb_connection
->mem_write_error
= false;
1691 gdb_put_packet(connection
, "OK", 2);
1698 static int gdb_step_continue_packet(struct connection
*connection
,
1699 char const *packet
, int packet_size
)
1701 struct target
*target
= get_target_from_connection(connection
);
1703 uint64_t address
= 0x0;
1704 int retval
= ERROR_OK
;
1708 if (packet_size
> 1)
1709 address
= strtoull(packet
+ 1, NULL
, 16);
1713 gdb_running_type
= packet
[0];
1714 if (packet
[0] == 'c') {
1715 LOG_DEBUG("continue");
1716 /* resume at current address, don't handle breakpoints, not debugging */
1717 retval
= target_resume(target
, current
, address
, 0, 0);
1718 } else if (packet
[0] == 's') {
1720 /* step at current or address, don't handle breakpoints */
1721 retval
= target_step(target
, current
, address
, 0);
1726 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1727 char const *packet
, int packet_size
)
1729 struct target
*target
= get_target_from_connection(connection
);
1731 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1732 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1738 LOG_DEBUG("[%s]", target_name(target
));
1740 type
= strtoul(packet
+ 1, &separator
, 16);
1742 if (type
== 0) /* memory breakpoint */
1743 bp_type
= BKPT_SOFT
;
1744 else if (type
== 1) /* hardware breakpoint */
1745 bp_type
= BKPT_HARD
;
1746 else if (type
== 2) /* write watchpoint */
1747 wp_type
= WPT_WRITE
;
1748 else if (type
== 3) /* read watchpoint */
1750 else if (type
== 4) /* access watchpoint */
1751 wp_type
= WPT_ACCESS
;
1753 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1754 return ERROR_SERVER_REMOTE_CLOSED
;
1757 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1758 bp_type
= gdb_breakpoint_override_type
;
1760 if (*separator
!= ',') {
1761 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1762 return ERROR_SERVER_REMOTE_CLOSED
;
1765 address
= strtoull(separator
+ 1, &separator
, 16);
1767 if (*separator
!= ',') {
1768 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1769 return ERROR_SERVER_REMOTE_CLOSED
;
1772 size
= strtoul(separator
+ 1, &separator
, 16);
1777 if (packet
[0] == 'Z') {
1778 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1779 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1780 /* Send empty reply to report that breakpoints of this type are not supported */
1781 gdb_put_packet(connection
, "", 0);
1782 } else if (retval
!= ERROR_OK
) {
1783 retval
= gdb_error(connection
, retval
);
1784 if (retval
!= ERROR_OK
)
1787 gdb_put_packet(connection
, "OK", 2);
1789 breakpoint_remove(target
, address
);
1790 gdb_put_packet(connection
, "OK", 2);
1797 if (packet
[0] == 'Z') {
1798 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, WATCHPOINT_IGNORE_DATA_VALUE_MASK
);
1799 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1800 /* Send empty reply to report that watchpoints of this type are not supported */
1801 gdb_put_packet(connection
, "", 0);
1802 } else if (retval
!= ERROR_OK
) {
1803 retval
= gdb_error(connection
, retval
);
1804 if (retval
!= ERROR_OK
)
1807 gdb_put_packet(connection
, "OK", 2);
1809 watchpoint_remove(target
, address
);
1810 gdb_put_packet(connection
, "OK", 2);
1821 /* print out a string and allocate more space as needed,
1822 * mainly used for XML at this point
1824 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 5, 6))) void xml_printf(int *retval
,
1825 char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1827 if (*retval
!= ERROR_OK
)
1832 if ((!*xml
) || (!first
)) {
1833 /* start by 0 to exercise all the code paths.
1834 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1836 *size
= *size
* 2 + 2;
1838 *xml
= realloc(*xml
, *size
);
1841 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1849 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1851 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1855 /* there was just enough or not enough space, allocate more. */
1860 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1862 /* Locate the annex. */
1863 const char *annex_end
= strchr(buf
, ':');
1867 /* After the read marker and annex, qXfer looks like a
1868 * traditional 'm' packet. */
1870 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1872 if (*separator
!= ',')
1875 *len
= strtoul(separator
+ 1, NULL
, 16);
1877 /* Extract the annex if needed */
1879 *annex
= strndup(buf
, annex_end
- buf
);
1887 static int compare_bank(const void *a
, const void *b
)
1889 struct flash_bank
*b1
, *b2
;
1890 b1
= *((struct flash_bank
**)a
);
1891 b2
= *((struct flash_bank
**)b
);
1893 if (b1
->base
== b2
->base
)
1895 else if (b1
->base
> b2
->base
)
1901 static int gdb_memory_map(struct connection
*connection
,
1902 char const *packet
, int packet_size
)
1904 /* We get away with only specifying flash here. Regions that are not
1905 * specified are treated as if we provided no memory map(if not we
1906 * could detect the holes and mark them as RAM).
1907 * Normally we only execute this code once, but no big deal if we
1908 * have to regenerate it a couple of times.
1911 struct target
*target
= get_target_from_connection(connection
);
1912 struct flash_bank
*p
;
1916 int retval
= ERROR_OK
;
1917 struct flash_bank
**banks
;
1921 target_addr_t ram_start
= 0;
1922 unsigned int target_flash_banks
= 0;
1924 /* skip command character */
1927 offset
= strtoul(packet
, &separator
, 16);
1928 length
= strtoul(separator
+ 1, &separator
, 16);
1930 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1932 /* Sort banks in ascending order. We need to report non-flash
1933 * memory as ram (or rather read/write) by default for GDB, since
1934 * it has no concept of non-cacheable read/write memory (i/o etc).
1936 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1938 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1939 p
= get_flash_bank_by_num_noprobe(i
);
1940 if (p
->target
!= target
)
1942 retval
= get_flash_bank_by_num(i
, &p
);
1943 if (retval
!= ERROR_OK
) {
1945 gdb_error(connection
, retval
);
1948 banks
[target_flash_banks
++] = p
;
1951 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1954 for (unsigned int i
= 0; i
< target_flash_banks
; i
++) {
1955 unsigned sector_size
= 0;
1956 unsigned group_len
= 0;
1960 if (ram_start
< p
->base
)
1961 xml_printf(&retval
, &xml
, &pos
, &size
,
1962 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1963 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
1964 ram_start
, p
->base
- ram_start
);
1966 /* Report adjacent groups of same-size sectors. So for
1967 * example top boot CFI flash will list an initial region
1968 * with several large sectors (maybe 128KB) and several
1969 * smaller ones at the end (maybe 32KB). STR7 will have
1970 * regions with 8KB, 32KB, and 64KB sectors; etc.
1972 for (unsigned int j
= 0; j
< p
->num_sectors
; j
++) {
1974 /* Maybe start a new group of sectors. */
1975 if (sector_size
== 0) {
1976 if (p
->sectors
[j
].offset
+ p
->sectors
[j
].size
> p
->size
) {
1977 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1978 " overflows the end of %s bank.",
1979 p
->sectors
[j
].offset
, p
->name
);
1980 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1983 target_addr_t start
;
1984 start
= p
->base
+ p
->sectors
[j
].offset
;
1985 xml_printf(&retval
, &xml
, &pos
, &size
,
1986 "<memory type=\"flash\" "
1987 "start=\"" TARGET_ADDR_FMT
"\" ",
1989 sector_size
= p
->sectors
[j
].size
;
1990 group_len
= sector_size
;
1992 group_len
+= sector_size
; /* equal to p->sectors[j].size */
1995 /* Does this finish a group of sectors?
1996 * If not, continue an already-started group.
1998 if (j
< p
->num_sectors
- 1
1999 && p
->sectors
[j
+ 1].size
== sector_size
2000 && p
->sectors
[j
+ 1].offset
== p
->sectors
[j
].offset
+ sector_size
2001 && p
->sectors
[j
+ 1].offset
+ p
->sectors
[j
+ 1].size
<= p
->size
)
2004 xml_printf(&retval
, &xml
, &pos
, &size
,
2005 "length=\"0x%x\">\n"
2006 "<property name=\"blocksize\">"
2014 ram_start
= p
->base
+ p
->size
;
2018 xml_printf(&retval
, &xml
, &pos
, &size
,
2019 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
2020 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
2021 ram_start
, target_address_max(target
) - ram_start
+ 1);
2022 /* ELSE a flash chip could be at the very end of the address space, in
2023 * which case ram_start will be precisely 0 */
2027 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
2029 if (retval
!= ERROR_OK
) {
2031 gdb_error(connection
, retval
);
2035 if (offset
+ length
> pos
)
2036 length
= pos
- offset
;
2038 char *t
= malloc(length
+ 1);
2040 memcpy(t
+ 1, xml
+ offset
, length
);
2041 gdb_put_packet(connection
, t
, length
+ 1);
2048 static const char *gdb_get_reg_type_name(enum reg_type type
)
2057 case REG_TYPE_INT16
:
2059 case REG_TYPE_INT32
:
2061 case REG_TYPE_INT64
:
2063 case REG_TYPE_INT128
:
2067 case REG_TYPE_UINT8
:
2069 case REG_TYPE_UINT16
:
2071 case REG_TYPE_UINT32
:
2073 case REG_TYPE_UINT64
:
2075 case REG_TYPE_UINT128
:
2077 case REG_TYPE_CODE_PTR
:
2079 case REG_TYPE_DATA_PTR
:
2081 case REG_TYPE_FLOAT
:
2083 case REG_TYPE_IEEE_SINGLE
:
2084 return "ieee_single";
2085 case REG_TYPE_IEEE_DOUBLE
:
2086 return "ieee_double";
2087 case REG_TYPE_ARCH_DEFINED
:
2088 return "int"; /* return arbitrary string to avoid compile warning. */
2091 return "int"; /* "int" as default value */
2094 static int lookup_add_arch_defined_types(char const **arch_defined_types_list
[], const char *type_id
,
2095 int *num_arch_defined_types
)
2097 int tbl_sz
= *num_arch_defined_types
;
2099 if (type_id
&& (strcmp(type_id
, ""))) {
2100 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2101 if (!((*arch_defined_types_list
)[j
])) {
2102 (*arch_defined_types_list
)[tbl_sz
++] = type_id
;
2103 *arch_defined_types_list
= realloc(*arch_defined_types_list
,
2104 sizeof(char *) * (tbl_sz
+ 1));
2105 (*arch_defined_types_list
)[tbl_sz
] = NULL
;
2106 *num_arch_defined_types
= tbl_sz
;
2109 if (!strcmp((*arch_defined_types_list
)[j
], type_id
))
2118 static int gdb_generate_reg_type_description(struct target
*target
,
2119 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
,
2120 char const **arch_defined_types_list
[], int *num_arch_defined_types
)
2122 int retval
= ERROR_OK
;
2124 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
2125 struct reg_data_type
*data_type
= type
->reg_type_vector
->type
;
2126 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2127 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2128 num_arch_defined_types
))
2129 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2130 arch_defined_types_list
,
2131 num_arch_defined_types
);
2133 /* <vector id="id" type="type" count="count"/> */
2134 xml_printf(&retval
, tdesc
, pos
, size
,
2135 "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32
"\"/>\n",
2136 type
->id
, type
->reg_type_vector
->type
->id
,
2137 type
->reg_type_vector
->count
);
2139 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
2140 struct reg_data_type_union_field
*field
;
2141 field
= type
->reg_type_union
->fields
;
2143 struct reg_data_type
*data_type
= field
->type
;
2144 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2145 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2146 num_arch_defined_types
))
2147 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2148 arch_defined_types_list
,
2149 num_arch_defined_types
);
2152 field
= field
->next
;
2155 * <field name="name" type="type"/> ...
2157 xml_printf(&retval
, tdesc
, pos
, size
,
2158 "<union id=\"%s\">\n",
2161 field
= type
->reg_type_union
->fields
;
2163 xml_printf(&retval
, tdesc
, pos
, size
,
2164 "<field name=\"%s\" type=\"%s\"/>\n",
2165 field
->name
, field
->type
->id
);
2167 field
= field
->next
;
2170 xml_printf(&retval
, tdesc
, pos
, size
,
2173 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
2174 struct reg_data_type_struct_field
*field
;
2175 field
= type
->reg_type_struct
->fields
;
2177 if (field
->use_bitfields
) {
2178 /* <struct id="id" size="size">
2179 * <field name="name" start="start" end="end"/> ...
2181 xml_printf(&retval
, tdesc
, pos
, size
,
2182 "<struct id=\"%s\" size=\"%" PRIu32
"\">\n",
2183 type
->id
, type
->reg_type_struct
->size
);
2185 xml_printf(&retval
, tdesc
, pos
, size
,
2186 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2187 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2188 gdb_get_reg_type_name(field
->bitfield
->type
));
2190 field
= field
->next
;
2194 struct reg_data_type
*data_type
= field
->type
;
2195 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2196 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2197 num_arch_defined_types
))
2198 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2199 arch_defined_types_list
,
2200 num_arch_defined_types
);
2205 * <field name="name" type="type"/> ...
2207 xml_printf(&retval
, tdesc
, pos
, size
,
2208 "<struct id=\"%s\">\n",
2211 xml_printf(&retval
, tdesc
, pos
, size
,
2212 "<field name=\"%s\" type=\"%s\"/>\n",
2213 field
->name
, field
->type
->id
);
2215 field
= field
->next
;
2219 xml_printf(&retval
, tdesc
, pos
, size
,
2222 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2223 /* <flags id="id" size="size">
2224 * <field name="name" start="start" end="end"/> ...
2226 xml_printf(&retval
, tdesc
, pos
, size
,
2227 "<flags id=\"%s\" size=\"%" PRIu32
"\">\n",
2228 type
->id
, type
->reg_type_flags
->size
);
2230 struct reg_data_type_flags_field
*field
;
2231 field
= type
->reg_type_flags
->fields
;
2233 xml_printf(&retval
, tdesc
, pos
, size
,
2234 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2235 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2236 gdb_get_reg_type_name(field
->bitfield
->type
));
2238 field
= field
->next
;
2241 xml_printf(&retval
, tdesc
, pos
, size
,
2249 /* Get a list of available target registers features. feature_list must
2250 * be freed by caller.
2252 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2253 struct reg
**reg_list
, int reg_list_size
)
2257 /* Start with only one element */
2258 *feature_list
= calloc(1, sizeof(char *));
2260 for (int i
= 0; i
< reg_list_size
; i
++) {
2261 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2264 if (reg_list
[i
]->feature
2265 && reg_list
[i
]->feature
->name
2266 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2267 /* We found a feature, check if the feature is already in the
2268 * table. If not, allocate a new entry for the table and
2269 * put the new feature in it.
2271 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2272 if (!((*feature_list
)[j
])) {
2273 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2274 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2275 (*feature_list
)[tbl_sz
] = NULL
;
2278 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2285 if (feature_list_size
)
2286 *feature_list_size
= tbl_sz
;
2291 /* Create a register list that's the union of all the registers of the SMP
2292 * group this target is in. If the target is not part of an SMP group, this
2293 * returns the same as target_get_gdb_reg_list_noread().
2295 static int smp_reg_list_noread(struct target
*target
,
2296 struct reg
**combined_list
[], int *combined_list_size
,
2297 enum target_register_class reg_class
)
2300 return target_get_gdb_reg_list_noread(target
, combined_list
,
2301 combined_list_size
, REG_CLASS_ALL
);
2303 unsigned int combined_allocated
= 256;
2304 struct reg
**local_list
= malloc(combined_allocated
* sizeof(struct reg
*));
2306 LOG_ERROR("malloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2309 unsigned int local_list_size
= 0;
2311 struct target_list
*head
;
2312 foreach_smp_target(head
, target
->smp_targets
) {
2313 if (!target_was_examined(head
->target
))
2316 struct reg
**reg_list
= NULL
;
2318 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2319 ®_list_size
, reg_class
);
2320 if (result
!= ERROR_OK
) {
2324 for (int i
= 0; i
< reg_list_size
; i
++) {
2326 struct reg
*a
= reg_list
[i
];
2328 /* Nested loop makes this O(n^2), but this entire function with
2329 * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2331 for (unsigned int j
= 0; j
< local_list_size
; j
++) {
2332 struct reg
*b
= local_list
[j
];
2333 if (!strcmp(a
->name
, b
->name
)) {
2335 if (a
->size
!= b
->size
) {
2336 LOG_ERROR("SMP register %s is %d bits on one "
2337 "target, but %d bits on another target.",
2338 a
->name
, a
->size
, b
->size
);
2347 LOG_DEBUG("[%s] %s not found in combined list", target_name(target
), a
->name
);
2348 if (local_list_size
>= combined_allocated
) {
2349 combined_allocated
*= 2;
2350 local_list
= realloc(local_list
, combined_allocated
* sizeof(struct reg
*));
2352 LOG_ERROR("realloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2357 local_list
[local_list_size
] = a
;
2365 if (local_list_size
== 0) {
2366 LOG_ERROR("Unable to get register list");
2371 /* Now warn the user about any registers that weren't found in every target. */
2372 foreach_smp_target(head
, target
->smp_targets
) {
2373 if (!target_was_examined(head
->target
))
2376 struct reg
**reg_list
= NULL
;
2378 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2379 ®_list_size
, reg_class
);
2380 if (result
!= ERROR_OK
) {
2384 for (unsigned int i
= 0; i
< local_list_size
; i
++) {
2386 struct reg
*a
= local_list
[i
];
2387 for (int j
= 0; j
< reg_list_size
; j
++) {
2388 struct reg
*b
= reg_list
[j
];
2389 if (b
->exist
&& !strcmp(a
->name
, b
->name
)) {
2395 LOG_WARNING("Register %s does not exist in %s, which is part of an SMP group where "
2396 "this register does exist.",
2397 a
->name
, target_name(head
->target
));
2403 *combined_list
= local_list
;
2404 *combined_list_size
= local_list_size
;
2408 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2410 int retval
= ERROR_OK
;
2411 struct reg
**reg_list
= NULL
;
2413 char const *architecture
;
2414 char const **features
= NULL
;
2415 int feature_list_size
= 0;
2421 retval
= smp_reg_list_noread(target
, ®_list
, ®_list_size
,
2424 if (retval
!= ERROR_OK
) {
2425 LOG_ERROR("get register list failed");
2426 retval
= ERROR_FAIL
;
2430 if (reg_list_size
<= 0) {
2431 LOG_ERROR("get register list failed");
2432 retval
= ERROR_FAIL
;
2436 /* Get a list of available target registers features */
2437 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2438 if (retval
!= ERROR_OK
) {
2439 LOG_ERROR("Can't get the registers feature list");
2440 retval
= ERROR_FAIL
;
2444 /* If we found some features associated with registers, create sections */
2445 int current_feature
= 0;
2447 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2448 "<?xml version=\"1.0\"?>\n"
2449 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2450 "<target version=\"1.0\">\n");
2452 /* generate architecture element if supported by target */
2453 architecture
= target_get_gdb_arch(target
);
2455 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2456 "<architecture>%s</architecture>\n", architecture
);
2458 /* generate target description according to register list */
2460 while (features
[current_feature
]) {
2461 char const **arch_defined_types
= NULL
;
2462 int num_arch_defined_types
= 0;
2464 arch_defined_types
= calloc(1, sizeof(char *));
2465 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2466 "<feature name=\"%s\">\n",
2467 features
[current_feature
]);
2470 for (i
= 0; i
< reg_list_size
; i
++) {
2472 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2475 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2478 const char *type_str
;
2479 if (reg_list
[i
]->reg_data_type
) {
2480 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2481 /* generate <type... first, if there are architecture-defined types. */
2482 if (lookup_add_arch_defined_types(&arch_defined_types
,
2483 reg_list
[i
]->reg_data_type
->id
,
2484 &num_arch_defined_types
))
2485 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2486 reg_list
[i
]->reg_data_type
,
2487 &arch_defined_types
,
2488 &num_arch_defined_types
);
2490 type_str
= reg_list
[i
]->reg_data_type
->id
;
2492 /* predefined type */
2493 type_str
= gdb_get_reg_type_name(
2494 reg_list
[i
]->reg_data_type
->type
);
2497 /* Default type is "int" */
2501 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2502 "<reg name=\"%s\"", reg_list
[i
]->name
);
2503 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2504 " bitsize=\"%" PRIu32
"\"", reg_list
[i
]->size
);
2505 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2506 " regnum=\"%" PRIu32
"\"", reg_list
[i
]->number
);
2507 if (reg_list
[i
]->caller_save
)
2508 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2509 " save-restore=\"yes\"");
2511 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2512 " save-restore=\"no\"");
2514 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2515 " type=\"%s\"", type_str
);
2517 if (reg_list
[i
]->group
)
2518 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2519 " group=\"%s\"", reg_list
[i
]->group
);
2521 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2525 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2529 free(arch_defined_types
);
2533 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2540 if (retval
== ERROR_OK
)
2548 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2549 char **chunk
, int32_t offset
, uint32_t length
)
2552 LOG_ERROR("Unable to Generate Target Description");
2556 char *tdesc
= target_desc
->tdesc
;
2557 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2560 int retval
= gdb_generate_target_description(target
, &tdesc
);
2561 if (retval
!= ERROR_OK
) {
2562 LOG_ERROR("Unable to Generate Target Description");
2566 tdesc_length
= strlen(tdesc
);
2571 if (length
< (tdesc_length
- offset
))
2572 transfer_type
= 'm';
2574 transfer_type
= 'l';
2576 *chunk
= malloc(length
+ 2);
2578 LOG_ERROR("Unable to allocate memory");
2582 (*chunk
)[0] = transfer_type
;
2583 if (transfer_type
== 'm') {
2584 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2585 (*chunk
)[1 + length
] = '\0';
2587 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2588 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2590 /* After gdb-server sends out last chunk, invalidate tdesc. */
2596 target_desc
->tdesc
= tdesc
;
2597 target_desc
->tdesc_length
= tdesc_length
;
2602 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2604 int retval
= ERROR_OK
;
2605 struct reg
**reg_list
= NULL
;
2606 int reg_list_size
= 0;
2607 char const **features
= NULL
;
2608 int feature_list_size
= 0;
2610 char const *architecture
= target_get_gdb_arch(target
);
2612 retval
= target_get_gdb_reg_list_noread(target
, ®_list
,
2613 ®_list_size
, REG_CLASS_ALL
);
2614 if (retval
!= ERROR_OK
) {
2615 LOG_ERROR("get register list failed");
2619 if (reg_list_size
<= 0) {
2620 LOG_ERROR("get register list failed");
2621 retval
= ERROR_FAIL
;
2625 /* Get a list of available target registers features */
2626 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2627 if (retval
!= ERROR_OK
) {
2628 LOG_ERROR("Can't get the registers feature list");
2633 if (architecture
|| feature_list_size
)
2647 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2649 struct rtos
*rtos
= target
->rtos
;
2650 int retval
= ERROR_OK
;
2651 char *thread_list
= NULL
;
2655 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2656 "<?xml version=\"1.0\"?>\n"
2660 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2661 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2663 if (!thread_detail
->exists
)
2666 if (thread_detail
->thread_name_str
)
2667 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2668 "<thread id=\"%" PRIx64
"\" name=\"%s\">",
2669 thread_detail
->threadid
,
2670 thread_detail
->thread_name_str
);
2672 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2673 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2675 if (thread_detail
->thread_name_str
)
2676 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2677 "Name: %s", thread_detail
->thread_name_str
);
2679 if (thread_detail
->extra_info_str
) {
2680 if (thread_detail
->thread_name_str
)
2681 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2683 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2684 "%s", thread_detail
->extra_info_str
);
2687 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2692 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2695 if (retval
== ERROR_OK
)
2696 *thread_list_out
= thread_list
;
2703 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2704 char **chunk
, int32_t offset
, uint32_t length
)
2706 if (!*thread_list
) {
2707 int retval
= gdb_generate_thread_list(target
, thread_list
);
2708 if (retval
!= ERROR_OK
) {
2709 LOG_ERROR("Unable to Generate Thread List");
2714 size_t thread_list_length
= strlen(*thread_list
);
2717 length
= MIN(length
, thread_list_length
- offset
);
2718 if (length
< (thread_list_length
- offset
))
2719 transfer_type
= 'm';
2721 transfer_type
= 'l';
2723 *chunk
= malloc(length
+ 2 + 3);
2724 /* Allocating extra 3 bytes prevents false positive valgrind report
2725 * of strlen(chunk) word access:
2726 * Invalid read of size 4
2727 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2729 LOG_ERROR("Unable to allocate memory");
2733 (*chunk
)[0] = transfer_type
;
2734 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2735 (*chunk
)[1 + length
] = '\0';
2737 /* After gdb-server sends out last chunk, invalidate thread list. */
2738 if (transfer_type
== 'l') {
2740 *thread_list
= NULL
;
2746 static int gdb_query_packet(struct connection
*connection
,
2747 char const *packet
, int packet_size
)
2749 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2750 struct gdb_connection
*gdb_connection
= connection
->priv
;
2751 struct target
*target
= get_target_from_connection(connection
);
2753 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2754 if (packet_size
> 6) {
2755 Jim_Interp
*interp
= cmd_ctx
->interp
;
2757 cmd
= malloc((packet_size
- 6) / 2 + 1);
2758 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2761 /* We want to print all debug output to GDB connection */
2762 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
2763 target_call_timer_callbacks_now();
2764 /* some commands need to know the GDB connection, make note of current
2765 * GDB connection. */
2766 current_gdb_connection
= gdb_connection
;
2768 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
2769 cmd_ctx
->current_target_override
= NULL
;
2771 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
2772 Jim_DeleteAssocData(interp
, "context");
2773 int retval
= Jim_SetAssocData(interp
, "context", NULL
, cmd_ctx
);
2774 if (retval
== JIM_OK
) {
2775 retval
= Jim_EvalObj(interp
, Jim_NewStringObj(interp
, cmd
, -1));
2776 Jim_DeleteAssocData(interp
, "context");
2778 int inner_retval
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
2779 if (retval
== JIM_OK
)
2780 retval
= inner_retval
;
2782 cmd_ctx
->current_target_override
= saved_target_override
;
2784 current_gdb_connection
= NULL
;
2785 target_call_timer_callbacks_now();
2786 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
2788 if (retval
== JIM_RETURN
)
2789 retval
= interp
->returnCode
;
2791 const char *cretmsg
= Jim_GetString(Jim_GetResult(interp
), &lenmsg
);
2793 if (lenmsg
&& cretmsg
[lenmsg
- 1] != '\n') {
2794 retmsg
= alloc_printf("%s\n", cretmsg
);
2797 retmsg
= strdup(cretmsg
);
2800 return ERROR_GDB_BUFFER_TOO_SMALL
;
2802 if (retval
== JIM_OK
) {
2804 char *hex_buffer
= malloc(lenmsg
* 2 + 1);
2807 return ERROR_GDB_BUFFER_TOO_SMALL
;
2810 size_t pkt_len
= hexify(hex_buffer
, (const uint8_t *)retmsg
, lenmsg
,
2812 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
2815 gdb_put_packet(connection
, "OK", 2);
2819 gdb_output_con(connection
, retmsg
);
2820 gdb_send_error(connection
, retval
);
2825 gdb_put_packet(connection
, "OK", 2);
2827 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2828 if (packet_size
> 5) {
2833 target_addr_t addr
= 0;
2836 /* skip command character */
2839 addr
= strtoull(packet
, &separator
, 16);
2841 if (*separator
!= ',') {
2842 LOG_ERROR("incomplete read memory packet received, dropping connection");
2843 return ERROR_SERVER_REMOTE_CLOSED
;
2846 len
= strtoul(separator
+ 1, NULL
, 16);
2848 gdb_connection
->output_flag
= GDB_OUTPUT_NOTIF
;
2849 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2850 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
2852 if (retval
== ERROR_OK
) {
2853 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2854 gdb_put_packet(connection
, gdb_reply
, 9);
2856 retval
= gdb_error(connection
, retval
);
2857 if (retval
!= ERROR_OK
)
2863 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2864 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2865 * qXfer:features:read is supported for some targets */
2866 int retval
= ERROR_OK
;
2867 char *buffer
= NULL
;
2870 int gdb_target_desc_supported
= 0;
2872 /* we need to test that the target supports target descriptions */
2873 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2874 if (retval
!= ERROR_OK
) {
2875 LOG_INFO("Failed detecting Target Description Support, disabling");
2876 gdb_target_desc_supported
= 0;
2879 /* support may be disabled globally */
2880 if (gdb_use_target_description
== 0) {
2881 if (gdb_target_desc_supported
)
2882 LOG_WARNING("Target Descriptions Supported, but disabled");
2883 gdb_target_desc_supported
= 0;
2890 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2892 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2893 (gdb_target_desc_supported
== 1) ? '+' : '-');
2895 if (retval
!= ERROR_OK
) {
2896 gdb_send_error(connection
, 01);
2900 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2904 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2905 && (flash_get_bank_count() > 0))
2906 return gdb_memory_map(connection
, packet
, packet_size
);
2907 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2909 int retval
= ERROR_OK
;
2912 unsigned int length
;
2914 /* skip command character */
2917 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2918 gdb_send_error(connection
, 01);
2922 /* Target should prepare correct target description for annex.
2923 * The first character of returned xml is 'm' or 'l'. 'm' for
2924 * there are *more* chunks to transfer. 'l' for it is the *last*
2925 * chunk of target description.
2927 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2928 &xml
, offset
, length
);
2929 if (retval
!= ERROR_OK
) {
2930 gdb_error(connection
, retval
);
2934 gdb_put_packet(connection
, xml
, strlen(xml
));
2938 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2940 int retval
= ERROR_OK
;
2943 unsigned int length
;
2945 /* skip command character */
2948 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2949 gdb_send_error(connection
, 01);
2953 /* Target should prepare correct thread list for annex.
2954 * The first character of returned xml is 'm' or 'l'. 'm' for
2955 * there are *more* chunks to transfer. 'l' for it is the *last*
2956 * chunk of target description.
2958 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2959 &xml
, offset
, length
);
2960 if (retval
!= ERROR_OK
) {
2961 gdb_error(connection
, retval
);
2965 gdb_put_packet(connection
, xml
, strlen(xml
));
2969 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2970 gdb_connection
->noack_mode
= 1;
2971 gdb_put_packet(connection
, "OK", 2);
2973 } else if (target
->type
->gdb_query_custom
) {
2974 char *buffer
= NULL
;
2975 int ret
= target
->type
->gdb_query_custom(target
, packet
, &buffer
);
2976 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2980 gdb_put_packet(connection
, "", 0);
2984 static bool gdb_handle_vcont_packet(struct connection
*connection
, const char *packet
,
2985 __attribute__((unused
)) int packet_size
)
2987 struct gdb_connection
*gdb_connection
= connection
->priv
;
2988 struct target
*target
= get_target_from_connection(connection
);
2989 const char *parse
= packet
;
2992 /* query for vCont supported */
2993 if (parse
[0] == '?') {
2994 if (target
->type
->step
) {
2995 /* gdb doesn't accept c without C and s without S */
2996 gdb_put_packet(connection
, "vCont;c;C;s;S", 13);
3002 if (parse
[0] == ';') {
3006 /* simple case, a continue packet */
3007 if (parse
[0] == 'c') {
3008 gdb_running_type
= 'c';
3009 LOG_DEBUG("target %s continue", target_name(target
));
3010 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3011 retval
= target_resume(target
, 1, 0, 0, 0);
3012 if (retval
== ERROR_TARGET_NOT_HALTED
)
3013 LOG_INFO("target %s was not halted when resume was requested", target_name(target
));
3015 /* poll target in an attempt to make its internal state consistent */
3016 if (retval
!= ERROR_OK
) {
3017 retval
= target_poll(target
);
3018 if (retval
!= ERROR_OK
)
3019 LOG_DEBUG("error polling target %s after failed resume", target_name(target
));
3023 * We don't report errors to gdb here, move frontend_state to
3024 * TARGET_RUNNING to stay in sync with gdb's expectation of the
3027 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3028 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3033 /* single-step or step-over-breakpoint */
3034 if (parse
[0] == 's') {
3035 gdb_running_type
= 's';
3036 bool fake_step
= false;
3038 struct target
*ct
= target
;
3042 if (parse
[0] == ':') {
3045 thread_id
= strtoll(parse
, &endp
, 16);
3054 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
3055 rtos_update_threads(target
);
3057 target
->rtos
->gdb_target_for_threadid(connection
, thread_id
, &ct
);
3060 * check if the thread to be stepped is the current rtos thread
3061 * if not, we must fake the step
3063 if (target
->rtos
->current_thread
!= thread_id
)
3067 if (parse
[0] == ';') {
3070 if (parse
[0] == 'c') {
3073 /* check if thread-id follows */
3074 if (parse
[0] == ':') {
3078 tid
= strtoll(parse
, NULL
, 16);
3079 if (tid
== thread_id
) {
3081 * Special case: only step a single thread (core),
3082 * keep the other threads halted. Currently, only
3083 * aarch64 target understands it. Other target types don't
3084 * care (nobody checks the actual value of 'current')
3085 * and it doesn't really matter. This deserves
3086 * a symbolic constant and a formal interface documentation
3089 LOG_DEBUG("request to step current core only");
3090 /* uncomment after checking that indeed other targets are safe */
3097 LOG_DEBUG("target %s single-step thread %"PRIx64
, target_name(ct
), thread_id
);
3098 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3099 target_call_event_callbacks(ct
, TARGET_EVENT_GDB_START
);
3102 * work around an annoying gdb behaviour: when the current thread
3103 * is changed in gdb, it assumes that the target can follow and also
3104 * make the thread current. This is an assumption that cannot hold
3105 * for a real target running a multi-threading OS. We just fake
3106 * the step to not trigger an internal error in gdb. See
3107 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3111 char sig_reply
[128];
3113 LOG_DEBUG("fake step thread %"PRIx64
, thread_id
);
3115 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
),
3116 "T05thread:%016"PRIx64
";", thread_id
);
3118 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
3119 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3124 /* support for gdb_sync command */
3125 if (gdb_connection
->sync
) {
3126 gdb_connection
->sync
= false;
3127 if (ct
->state
== TARGET_HALTED
) {
3128 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3129 "from the target.");
3130 gdb_sig_halted(connection
);
3131 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3133 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3137 retval
= target_step(ct
, current_pc
, 0, 0);
3138 if (retval
== ERROR_TARGET_NOT_HALTED
)
3139 LOG_INFO("target %s was not halted when step was requested", target_name(ct
));
3141 /* if step was successful send a reply back to gdb */
3142 if (retval
== ERROR_OK
) {
3143 retval
= target_poll(ct
);
3144 if (retval
!= ERROR_OK
)
3145 LOG_DEBUG("error polling target %s after successful step", target_name(ct
));
3146 /* send back signal information */
3147 gdb_signal_reply(ct
, connection
);
3148 /* stop forwarding log packets! */
3149 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3151 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3154 LOG_ERROR("Unknown vCont packet");
3158 static char *next_hex_encoded_field(const char **str
, char sep
)
3161 const char *hex
= *str
;
3165 const char *end
= strchr(hex
, sep
);
3167 hexlen
= strlen(hex
);
3170 *str
= hex
+ hexlen
+ 1;
3172 if (hexlen
% 2 != 0) {
3173 /* Malformed hex data */
3177 size_t count
= hexlen
/ 2;
3178 char *decoded
= malloc(count
+ 1);
3182 size_t converted
= unhexify((void *)decoded
, hex
, count
);
3183 if (converted
!= count
) {
3188 decoded
[count
] = '\0';
3192 /* handle extended restart packet */
3193 static void gdb_restart_inferior(struct connection
*connection
, const char *packet
, int packet_size
)
3195 struct gdb_connection
*gdb_con
= connection
->priv
;
3196 struct target
*target
= get_target_from_connection(connection
);
3198 breakpoint_clear_target(target
);
3199 watchpoint_clear_target(target
);
3200 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
3201 target_name(target
));
3202 /* set connection as attached after reset */
3203 gdb_con
->attached
= true;
3204 /* info rtos parts */
3205 gdb_thread_packet(connection
, packet
, packet_size
);
3208 static bool gdb_handle_vrun_packet(struct connection
*connection
, const char *packet
, int packet_size
)
3210 struct target
*target
= get_target_from_connection(connection
);
3211 const char *parse
= packet
;
3216 if (parse
[0] != ';')
3220 /* Skip first field "filename"; don't know what to do with it. */
3221 free(next_hex_encoded_field(&parse
, ';'));
3223 char *cmdline
= next_hex_encoded_field(&parse
, ';');
3225 char *arg
= next_hex_encoded_field(&parse
, ';');
3228 char *new_cmdline
= alloc_printf("%s %s", cmdline
, arg
);
3231 cmdline
= new_cmdline
;
3235 if (target
->semihosting
) {
3236 LOG_INFO("GDB set inferior command line to '%s'", cmdline
);
3237 free(target
->semihosting
->cmdline
);
3238 target
->semihosting
->cmdline
= cmdline
;
3240 LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline
);
3245 gdb_restart_inferior(connection
, packet
, packet_size
);
3246 gdb_put_packet(connection
, "S00", 3);
3250 static int gdb_v_packet(struct connection
*connection
,
3251 char const *packet
, int packet_size
)
3253 struct gdb_connection
*gdb_connection
= connection
->priv
;
3256 struct target
*target
= get_target_from_connection(connection
);
3258 if (strncmp(packet
, "vCont", 5) == 0) {
3264 handled
= gdb_handle_vcont_packet(connection
, packet
, packet_size
);
3266 gdb_put_packet(connection
, "", 0);
3271 if (strncmp(packet
, "vRun", 4) == 0) {
3274 handled
= gdb_handle_vrun_packet(connection
, packet
, packet_size
);
3276 gdb_put_packet(connection
, "", 0);
3281 /* if flash programming disabled - send a empty reply */
3283 if (gdb_flash_program
== 0) {
3284 gdb_put_packet(connection
, "", 0);
3288 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
3290 unsigned long length
;
3292 char const *parse
= packet
+ 12;
3293 if (*parse
== '\0') {
3294 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3295 return ERROR_SERVER_REMOTE_CLOSED
;
3298 addr
= strtoull(parse
, (char **)&parse
, 16);
3300 if (*(parse
++) != ',' || *parse
== '\0') {
3301 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3302 return ERROR_SERVER_REMOTE_CLOSED
;
3305 length
= strtoul(parse
, (char **)&parse
, 16);
3307 if (*parse
!= '\0') {
3308 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3309 return ERROR_SERVER_REMOTE_CLOSED
;
3312 /* assume all sectors need erasing - stops any problems
3313 * when flash_write is called multiple times */
3316 /* perform any target specific operations before the erase */
3317 target_call_event_callbacks(target
,
3318 TARGET_EVENT_GDB_FLASH_ERASE_START
);
3320 /* vFlashErase:addr,length messages require region start and
3321 * end to be "block" aligned ... if padding is ever needed,
3322 * GDB will have become dangerously confused.
3324 result
= flash_erase_address_range(target
, false, addr
,
3327 /* perform any target specific operations after the erase */
3328 target_call_event_callbacks(target
,
3329 TARGET_EVENT_GDB_FLASH_ERASE_END
);
3332 if (result
!= ERROR_OK
) {
3333 /* GDB doesn't evaluate the actual error number returned,
3334 * treat a failed erase as an I/O error
3336 gdb_send_error(connection
, EIO
);
3337 LOG_ERROR("flash_erase returned %i", result
);
3339 gdb_put_packet(connection
, "OK", 2);
3344 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
3347 unsigned long length
;
3348 char const *parse
= packet
+ 12;
3350 if (*parse
== '\0') {
3351 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3352 return ERROR_SERVER_REMOTE_CLOSED
;
3355 addr
= strtoull(parse
, (char **)&parse
, 16);
3356 if (*(parse
++) != ':') {
3357 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3358 return ERROR_SERVER_REMOTE_CLOSED
;
3360 length
= packet_size
- (parse
- packet
);
3362 /* create a new image if there isn't already one */
3363 if (!gdb_connection
->vflash_image
) {
3364 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
3365 image_open(gdb_connection
->vflash_image
, "", "build");
3368 /* create new section with content from packet buffer */
3369 retval
= image_add_section(gdb_connection
->vflash_image
,
3370 addr
, length
, 0x0, (uint8_t const *)parse
);
3371 if (retval
!= ERROR_OK
)
3374 gdb_put_packet(connection
, "OK", 2);
3379 if (strncmp(packet
, "vFlashDone", 10) == 0) {
3382 /* GDB command 'flash-erase' does not send a vFlashWrite,
3383 * so nothing to write here. */
3384 if (!gdb_connection
->vflash_image
) {
3385 gdb_put_packet(connection
, "OK", 2);
3389 /* process the flashing buffer. No need to erase as GDB
3390 * always issues a vFlashErase first. */
3391 target_call_event_callbacks(target
,
3392 TARGET_EVENT_GDB_FLASH_WRITE_START
);
3393 result
= flash_write(target
, gdb_connection
->vflash_image
,
3395 target_call_event_callbacks(target
,
3396 TARGET_EVENT_GDB_FLASH_WRITE_END
);
3397 if (result
!= ERROR_OK
) {
3398 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
3399 gdb_put_packet(connection
, "E.memtype", 9);
3401 gdb_send_error(connection
, EIO
);
3403 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
3404 gdb_put_packet(connection
, "OK", 2);
3407 image_close(gdb_connection
->vflash_image
);
3408 free(gdb_connection
->vflash_image
);
3409 gdb_connection
->vflash_image
= NULL
;
3414 gdb_put_packet(connection
, "", 0);
3418 static int gdb_detach(struct connection
*connection
)
3421 * Only reply "OK" to GDB
3422 * it will close the connection and this will trigger a call to
3423 * gdb_connection_closed() that will in turn trigger the event
3424 * TARGET_EVENT_GDB_DETACH
3426 return gdb_put_packet(connection
, "OK", 2);
3429 /* The format of 'F' response packet is
3430 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3432 static int gdb_fileio_response_packet(struct connection
*connection
,
3433 char const *packet
, int packet_size
)
3435 struct target
*target
= get_target_from_connection(connection
);
3437 char *parsing_point
;
3438 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
3439 int fileio_errno
= 0;
3440 bool fileio_ctrl_c
= false;
3445 if (*separator
== ',') {
3446 parsing_point
= separator
+ 1;
3447 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
3448 if (*separator
== ',') {
3449 if (*(separator
+ 1) == 'C') {
3450 /* TODO: process ctrl-c */
3451 fileio_ctrl_c
= true;
3456 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3457 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
3459 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
3460 if (retval
!= ERROR_OK
)
3463 /* After File-I/O ends, keep continue or step */
3464 if (gdb_running_type
== 'c')
3465 retval
= target_resume(target
, 1, 0x0, 0, 0);
3466 else if (gdb_running_type
== 's')
3467 retval
= target_step(target
, 1, 0x0, 0);
3469 retval
= ERROR_FAIL
;
3471 if (retval
!= ERROR_OK
)
3477 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
3478 const char *function
, const char *string
)
3480 struct connection
*connection
= priv
;
3481 struct gdb_connection
*gdb_con
= connection
->priv
;
3483 if (gdb_con
->output_flag
!= GDB_OUTPUT_ALL
)
3484 /* No out allowed */
3487 if (gdb_con
->busy
) {
3488 /* do not reply this using the O packet */
3492 gdb_output_con(connection
, string
);
3495 static void gdb_sig_halted(struct connection
*connection
)
3498 snprintf(sig_reply
, 4, "T%2.2x", 2);
3499 gdb_put_packet(connection
, sig_reply
, 3);
3502 static int gdb_input_inner(struct connection
*connection
)
3504 /* Do not allocate this on the stack */
3505 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for null-termination */
3507 struct target
*target
;
3508 char const *packet
= gdb_packet_buffer
;
3511 struct gdb_connection
*gdb_con
= connection
->priv
;
3512 static bool warn_use_ext
;
3514 target
= get_target_from_connection(connection
);
3516 /* drain input buffer. If one of the packets fail, then an error
3517 * packet is replied, if applicable.
3519 * This loop will terminate and the error code is returned.
3521 * The calling fn will check if this error is something that
3522 * can be recovered from, or if the connection must be closed.
3524 * If the error is recoverable, this fn is called again to
3525 * drain the rest of the buffer.
3528 packet_size
= GDB_BUFFER_SIZE
;
3529 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
3530 if (retval
!= ERROR_OK
)
3533 /* terminate with zero */
3534 gdb_packet_buffer
[packet_size
] = '\0';
3536 if (packet_size
> 0) {
3538 gdb_log_incoming_packet(connection
, gdb_packet_buffer
);
3541 switch (packet
[0]) {
3542 case 'T': /* Is thread alive? */
3543 gdb_thread_packet(connection
, packet
, packet_size
);
3545 case 'H': /* Set current thread ( 'c' for step and continue,
3546 * 'g' for all other operations ) */
3547 gdb_thread_packet(connection
, packet
, packet_size
);
3551 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
3552 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
3553 retval
= gdb_query_packet(connection
, packet
, packet_size
);
3556 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
3559 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
3562 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
3565 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
3568 gdb_con
->output_flag
= GDB_OUTPUT_NOTIF
;
3569 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
3570 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
3573 gdb_con
->output_flag
= GDB_OUTPUT_NOTIF
;
3574 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
3575 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
3579 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
3582 gdb_last_signal_packet(connection
, packet
, packet_size
);
3583 /* '?' is sent after the eventual '!' */
3584 if (!warn_use_ext
&& !gdb_con
->extended_protocol
) {
3585 warn_use_ext
= true;
3586 LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3587 connection
->service
->port
, connection
->service
->port
);
3593 gdb_thread_packet(connection
, packet
, packet_size
);
3594 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3596 if (gdb_con
->mem_write_error
) {
3597 LOG_ERROR("Memory write failure!");
3599 /* now that we have reported the memory write error,
3600 * we can clear the condition */
3601 gdb_con
->mem_write_error
= false;
3604 bool nostep
= false;
3605 bool already_running
= false;
3606 if (target
->state
== TARGET_RUNNING
) {
3607 LOG_WARNING("WARNING! The target is already running. "
3608 "All changes GDB did to registers will be discarded! "
3609 "Waiting for target to halt.");
3610 already_running
= true;
3611 } else if (target
->state
!= TARGET_HALTED
) {
3612 LOG_WARNING("The target is not in the halted nor running stated, "
3613 "stepi/continue ignored.");
3615 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
3616 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3617 * sent by GDB first to OpenOCD, thus defeating the check to
3618 * make only the single stepping have the sync feature...
3621 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3622 "from the target.");
3624 gdb_con
->sync
= false;
3626 if (!already_running
&& nostep
) {
3627 /* Either the target isn't in the halted state, then we can't
3628 * step/continue. This might be early setup, etc.
3630 * Or we want to allow GDB to pick up a fresh set of
3631 * register values without modifying the target state.
3634 gdb_sig_halted(connection
);
3636 /* stop forwarding log packets! */
3637 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
3639 /* We're running/stepping, in which case we can
3640 * forward log output until the target is halted
3642 gdb_con
->frontend_state
= TARGET_RUNNING
;
3643 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3645 if (!already_running
) {
3646 /* Here we don't want packet processing to stop even if this fails,
3647 * so we use a local variable instead of retval. */
3648 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
3649 if (retval
!= ERROR_OK
) {
3650 /* we'll never receive a halted
3651 * condition... issue a false one..
3653 gdb_frontend_halted(target
, connection
);
3660 retval
= gdb_v_packet(connection
, packet
, packet_size
);
3663 retval
= gdb_detach(connection
);
3666 gdb_con
->output_flag
= GDB_OUTPUT_NOTIF
;
3667 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
3668 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
3671 if (gdb_con
->extended_protocol
) {
3672 gdb_con
->attached
= false;
3675 gdb_put_packet(connection
, "OK", 2);
3676 return ERROR_SERVER_REMOTE_CLOSED
;
3678 /* handle extended remote protocol */
3679 gdb_con
->extended_protocol
= true;
3680 gdb_put_packet(connection
, "OK", 2);
3683 /* handle extended restart packet */
3684 gdb_restart_inferior(connection
, packet
, packet_size
);
3689 /* packet supported only by smp target i.e cortex_a.c*/
3690 /* handle smp packet replying coreid played to gbd */
3691 gdb_read_smp_packet(connection
, packet
, packet_size
);
3696 /* packet supported only by smp target i.e cortex_a.c */
3697 /* handle smp packet setting coreid to be played at next
3699 gdb_write_smp_packet(connection
, packet
, packet_size
);
3703 /* File-I/O extension */
3704 /* After gdb uses host-side syscall to complete target file
3705 * I/O, gdb sends host-side syscall return value to target
3707 * The format of 'F' response packet is
3708 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3710 gdb_con
->frontend_state
= TARGET_RUNNING
;
3711 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3712 gdb_fileio_response_packet(connection
, packet
, packet_size
);
3716 /* ignore unknown packets */
3717 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
3718 gdb_put_packet(connection
, "", 0);
3722 /* if a packet handler returned an error, exit input loop */
3723 if (retval
!= ERROR_OK
)
3727 if (gdb_con
->ctrl_c
) {
3728 if (target
->state
== TARGET_RUNNING
) {
3729 struct target
*t
= target
;
3731 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &t
);
3732 retval
= target_halt(t
);
3733 if (retval
== ERROR_OK
)
3734 retval
= target_poll(t
);
3735 if (retval
!= ERROR_OK
)
3736 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3737 gdb_con
->ctrl_c
= false;
3739 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3740 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3744 } while (gdb_con
->buf_cnt
> 0);
3749 static int gdb_input(struct connection
*connection
)
3751 int retval
= gdb_input_inner(connection
);
3752 struct gdb_connection
*gdb_con
= connection
->priv
;
3753 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3756 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3757 if (gdb_con
->closed
)
3758 return ERROR_SERVER_REMOTE_CLOSED
;
3760 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3765 * Send custom notification packet as keep-alive during memory read/write.
3767 * From gdb 7.0 (released 2009-10-06) an unknown notification received during
3768 * memory read/write would be silently dropped.
3769 * Before gdb 7.0 any character, with exclusion of "+-$", would be considered
3770 * as junk and ignored.
3771 * In both cases the reception will reset the timeout counter in gdb, thus
3772 * working as a keep-alive.
3773 * Check putpkt_binary() and getpkt_sane() in gdb commit
3774 * 74531fed1f2d662debc2c209b8b3faddceb55960
3776 * Enable remote debug in gdb with 'set debug remote 1' to either dump the junk
3777 * characters in gdb pre-7.0 and the notification from gdb 7.0.
3779 static void gdb_async_notif(struct connection
*connection
)
3781 static unsigned char count
;
3782 unsigned char checksum
= 0;
3785 int len
= sprintf(buf
, "%%oocd_keepalive:%2.2x", count
++);
3786 for (int i
= 1; i
< len
; i
++)
3788 len
+= sprintf(buf
+ len
, "#%2.2x", checksum
);
3790 #ifdef _DEBUG_GDB_IO_
3791 LOG_DEBUG("sending packet '%s'", buf
);
3794 gdb_write(connection
, buf
, len
);
3797 static void gdb_keep_client_alive(struct connection
*connection
)
3799 struct gdb_connection
*gdb_con
= connection
->priv
;
3801 switch (gdb_con
->output_flag
) {
3803 /* no need for keep-alive */
3805 case GDB_OUTPUT_NOTIF
:
3806 /* send asynchronous notification */
3807 gdb_async_notif(connection
);
3809 case GDB_OUTPUT_ALL
:
3810 /* send an empty O packet */
3811 gdb_output_con(connection
, "");
3818 static const struct service_driver gdb_service_driver
= {
3820 .new_connection_during_keep_alive_handler
= NULL
,
3821 .new_connection_handler
= gdb_new_connection
,
3822 .input_handler
= gdb_input
,
3823 .connection_closed_handler
= gdb_connection_closed
,
3824 .keep_client_alive_handler
= gdb_keep_client_alive
,
3827 static int gdb_target_start(struct target
*target
, const char *port
)
3829 struct gdb_service
*gdb_service
;
3831 gdb_service
= malloc(sizeof(struct gdb_service
));
3836 LOG_INFO("starting gdb server for %s on %s", target_name(target
), port
);
3838 gdb_service
->target
= target
;
3839 gdb_service
->core
[0] = -1;
3840 gdb_service
->core
[1] = -1;
3841 target
->gdb_service
= gdb_service
;
3843 ret
= add_service(&gdb_service_driver
, port
, target
->gdb_max_connections
, gdb_service
);
3844 /* initialize all targets gdb service with the same pointer */
3846 struct target_list
*head
;
3847 foreach_smp_target(head
, target
->smp_targets
) {
3848 struct target
*curr
= head
->target
;
3850 curr
->gdb_service
= gdb_service
;
3856 static int gdb_target_add_one(struct target
*target
)
3858 /* one gdb instance per smp list */
3859 if ((target
->smp
) && (target
->gdb_service
))
3862 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3863 if (!target_supports_gdb_connection(target
)) {
3864 LOG_DEBUG("skip gdb server for target %s", target_name(target
));
3868 if (target
->gdb_port_override
) {
3869 if (strcmp(target
->gdb_port_override
, "disabled") == 0) {
3870 LOG_INFO("gdb port disabled");
3873 return gdb_target_start(target
, target
->gdb_port_override
);
3876 if (strcmp(gdb_port
, "disabled") == 0) {
3877 LOG_INFO("gdb port disabled");
3881 int retval
= gdb_target_start(target
, gdb_port_next
);
3882 if (retval
== ERROR_OK
) {
3883 /* save the port number so can be queried with
3884 * $target_name cget -gdb-port
3886 target
->gdb_port_override
= strdup(gdb_port_next
);
3889 /* If we can parse the port number
3890 * then we increment the port number for the next target.
3893 portnumber
= strtol(gdb_port_next
, &end
, 0);
3895 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3896 free(gdb_port_next
);
3898 gdb_port_next
= alloc_printf("%ld", portnumber
+1);
3900 /* Don't increment if gdb_port is 0, since we're just
3901 * trying to allocate an unused port. */
3902 gdb_port_next
= strdup("0");
3910 int gdb_target_add_all(struct target
*target
)
3913 LOG_WARNING("gdb services need one or more targets defined");
3918 int retval
= gdb_target_add_one(target
);
3919 if (retval
!= ERROR_OK
)
3922 target
= target
->next
;
3928 COMMAND_HANDLER(handle_gdb_sync_command
)
3931 return ERROR_COMMAND_SYNTAX_ERROR
;
3933 if (!current_gdb_connection
) {
3935 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3939 current_gdb_connection
->sync
= true;
3944 /* daemon configuration command gdb_port */
3945 COMMAND_HANDLER(handle_gdb_port_command
)
3947 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3948 if (retval
== ERROR_OK
) {
3949 free(gdb_port_next
);
3950 gdb_port_next
= strdup(gdb_port
);
3955 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3958 return ERROR_COMMAND_SYNTAX_ERROR
;
3960 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3964 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3967 return ERROR_COMMAND_SYNTAX_ERROR
;
3969 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3973 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3976 return ERROR_COMMAND_SYNTAX_ERROR
;
3978 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3982 COMMAND_HANDLER(handle_gdb_report_register_access_error
)
3985 return ERROR_COMMAND_SYNTAX_ERROR
;
3987 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_register_access_error
);
3991 /* gdb_breakpoint_override */
3992 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3994 if (CMD_ARGC
== 0) {
3996 } else if (CMD_ARGC
== 1) {
3997 gdb_breakpoint_override
= 1;
3998 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3999 gdb_breakpoint_override_type
= BKPT_HARD
;
4000 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
4001 gdb_breakpoint_override_type
= BKPT_SOFT
;
4002 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
4003 gdb_breakpoint_override
= 0;
4005 return ERROR_COMMAND_SYNTAX_ERROR
;
4006 if (gdb_breakpoint_override
)
4007 LOG_USER("force %s breakpoints",
4008 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
4010 LOG_USER("breakpoint type is not overridden");
4015 COMMAND_HANDLER(handle_gdb_target_description_command
)
4018 return ERROR_COMMAND_SYNTAX_ERROR
;
4020 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
4024 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
4027 uint32_t tdesc_length
;
4028 struct target
*target
= get_current_target(CMD_CTX
);
4030 int retval
= gdb_generate_target_description(target
, &tdesc
);
4031 if (retval
!= ERROR_OK
) {
4032 LOG_ERROR("Unable to Generate Target Description");
4036 tdesc_length
= strlen(tdesc
);
4038 struct fileio
*fileio
;
4039 size_t size_written
;
4041 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
4042 if (!tdesc_filename
) {
4043 retval
= ERROR_FAIL
;
4047 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
4049 if (retval
!= ERROR_OK
) {
4050 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
4054 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
4056 fileio_close(fileio
);
4058 if (retval
!= ERROR_OK
)
4059 LOG_ERROR("Error while writing the tdesc file");
4062 free(tdesc_filename
);
4068 static const struct command_registration gdb_command_handlers
[] = {
4071 .handler
= handle_gdb_sync_command
,
4072 .mode
= COMMAND_ANY
,
4073 .help
= "next stepi will return immediately allowing "
4074 "GDB to fetch register state without affecting "
4080 .handler
= handle_gdb_port_command
,
4081 .mode
= COMMAND_CONFIG
,
4082 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4083 "server listens for the next port number after the "
4084 "base port number specified. "
4085 "No arguments reports GDB port. \"pipe\" means listen to stdin "
4086 "output to stdout, an integer is base port number, \"disabled\" disables "
4087 "port. Any other string is are interpreted as named pipe to listen to. "
4088 "Output pipe is the same name as input pipe, but with 'o' appended.",
4089 .usage
= "[port_num]",
4092 .name
= "gdb_memory_map",
4093 .handler
= handle_gdb_memory_map_command
,
4094 .mode
= COMMAND_CONFIG
,
4095 .help
= "enable or disable memory map",
4096 .usage
= "('enable'|'disable')"
4099 .name
= "gdb_flash_program",
4100 .handler
= handle_gdb_flash_program_command
,
4101 .mode
= COMMAND_CONFIG
,
4102 .help
= "enable or disable flash program",
4103 .usage
= "('enable'|'disable')"
4106 .name
= "gdb_report_data_abort",
4107 .handler
= handle_gdb_report_data_abort_command
,
4108 .mode
= COMMAND_CONFIG
,
4109 .help
= "enable or disable reporting data aborts",
4110 .usage
= "('enable'|'disable')"
4113 .name
= "gdb_report_register_access_error",
4114 .handler
= handle_gdb_report_register_access_error
,
4115 .mode
= COMMAND_CONFIG
,
4116 .help
= "enable or disable reporting register access errors",
4117 .usage
= "('enable'|'disable')"
4120 .name
= "gdb_breakpoint_override",
4121 .handler
= handle_gdb_breakpoint_override_command
,
4122 .mode
= COMMAND_ANY
,
4123 .help
= "Display or specify type of breakpoint "
4124 "to be used by gdb 'break' commands.",
4125 .usage
= "('hard'|'soft'|'disable')"
4128 .name
= "gdb_target_description",
4129 .handler
= handle_gdb_target_description_command
,
4130 .mode
= COMMAND_CONFIG
,
4131 .help
= "enable or disable target description",
4132 .usage
= "('enable'|'disable')"
4135 .name
= "gdb_save_tdesc",
4136 .handler
= handle_gdb_save_tdesc_command
,
4137 .mode
= COMMAND_EXEC
,
4138 .help
= "Save the target description file",
4141 COMMAND_REGISTRATION_DONE
4144 int gdb_register_commands(struct command_context
*cmd_ctx
)
4146 gdb_port
= strdup("3333");
4147 gdb_port_next
= strdup("3333");
4148 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);
4151 void gdb_service_free(void)
4154 free(gdb_port_next
);
4157 int gdb_get_actual_connections(void)
4159 return gdb_actual_connections
;