1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008 by Spencer Oliver *
11 * spen@spen-soft.co.uk *
13 * Copyright (C) 2011 by Broadcom Corporation *
14 * Evan Hunter - ehunter@broadcom.com *
16 * Copyright (C) ST-Ericsson SA 2011 *
17 * michel.jaouen@stericsson.com : smp minimum support *
19 * Copyright (C) 2013 Andes Technology *
20 * Hsiangkai Wang <hkwang@andestech.com> *
22 * Copyright (C) 2013 Franck Jullien *
23 * elec4fun@gmail.com *
24 ***************************************************************************/
30 #include <target/breakpoints.h>
31 #include <target/target_request.h>
32 #include <target/register.h>
33 #include <target/target.h>
34 #include <target/target_type.h>
35 #include <target/semihosting_common.h>
37 #include <flash/nor/core.h>
38 #include "gdb_server.h"
39 #include <target/image.h>
40 #include <jtag/jtag.h>
41 #include "rtos/rtos.h"
42 #include "target/smp.h"
46 * GDB server implementation.
48 * This implements the GDB Remote Serial Protocol, over TCP connections,
49 * giving GDB access to the JTAG or other hardware debugging facilities
50 * found in most modern embedded processors.
53 enum gdb_output_flag
{
54 /* GDB doesn't accept 'O' packets */
56 /* GDB accepts 'O' packets */
60 struct target_desc_format
{
62 uint32_t tdesc_length
;
65 /* private connection data for GDB */
66 struct gdb_connection
{
67 char buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for null-termination */
71 enum target_state frontend_state
;
72 struct image
*vflash_image
;
76 /* set flag to true if you want the next stepi to return immediately.
77 * allowing GDB to pick up a fresh set of register values from the target
78 * without modifying the target state. */
80 /* We delay reporting memory write errors until next step/continue or memory
81 * write. This improves performance of gdb load significantly as the GDB packet
82 * can be replied immediately and a new GDB packet will be ready without delay
83 * (ca. 10% or so...). */
85 /* with extended-remote it seems we need to better emulate attach/detach.
86 * what this means is we reply with a W stop reply after a kill packet,
87 * normally we reply with a S reply via gdb_last_signal_packet.
88 * as a side note this behaviour only effects gdb > 6.8 */
90 /* set when extended protocol is used */
91 bool extended_protocol
;
92 /* temporarily used for target description support */
93 struct target_desc_format target_desc
;
94 /* temporarily used for thread list support */
96 /* flag to mask the output from gdb_log_callback() */
97 enum gdb_output_flag output_flag
;
101 #define _DEBUG_GDB_IO_
104 static struct gdb_connection
*current_gdb_connection
;
106 static int gdb_breakpoint_override
;
107 static enum breakpoint_type gdb_breakpoint_override_type
;
109 static int gdb_error(struct connection
*connection
, int retval
);
110 static char *gdb_port
;
111 static char *gdb_port_next
;
113 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
114 const char *function
, const char *string
);
116 static void gdb_sig_halted(struct connection
*connection
);
118 /* number of gdb connections, mainly to suppress gdb related debugging spam
119 * in helper/log.c when no gdb connections are actually active */
120 static int gdb_actual_connections
;
122 /* set if we are sending a memory map to gdb
123 * via qXfer:memory-map:read packet */
124 /* enabled by default*/
125 static int gdb_use_memory_map
= 1;
126 /* enabled by default*/
127 static int gdb_flash_program
= 1;
129 /* if set, data aborts cause an error to be reported in memory read packets
130 * see the code in gdb_read_memory_packet() for further explanations.
131 * Disabled by default.
133 static int gdb_report_data_abort
;
134 /* If set, errors when accessing registers are reported to gdb. Disabled by
136 static int gdb_report_register_access_error
;
138 /* set if we are sending target descriptions to gdb
139 * via qXfer:features:read packet */
140 /* enabled by default */
141 static int gdb_use_target_description
= 1;
143 /* current processing free-run type, used by file-I/O */
144 static char gdb_running_type
;
146 static int gdb_last_signal(struct target
*target
)
148 LOG_TARGET_DEBUG(target
, "Debug reason is: %s",
149 target_debug_reason_str(target
->debug_reason
));
151 switch (target
->debug_reason
) {
152 case DBG_REASON_DBGRQ
:
153 return 0x2; /* SIGINT */
154 case DBG_REASON_BREAKPOINT
:
155 case DBG_REASON_WATCHPOINT
:
156 case DBG_REASON_WPTANDBKPT
:
157 return 0x05; /* SIGTRAP */
158 case DBG_REASON_SINGLESTEP
:
159 return 0x05; /* SIGTRAP */
160 case DBG_REASON_EXC_CATCH
:
162 case DBG_REASON_NOTHALTED
:
163 return 0x0; /* no signal... shouldn't happen */
165 LOG_USER("undefined debug reason %d (%s) - target needs reset",
166 target
->debug_reason
,
167 target_debug_reason_str(target
->debug_reason
));
172 static int check_pending(struct connection
*connection
,
173 int timeout_s
, int *got_data
)
175 /* a non-blocking socket will block if there is 0 bytes available on the socket,
176 * but return with as many bytes as are available immediately
180 struct gdb_connection
*gdb_con
= connection
->priv
;
186 if (gdb_con
->buf_cnt
> 0) {
192 FD_SET(connection
->fd
, &read_fds
);
194 tv
.tv_sec
= timeout_s
;
196 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
197 /* This can typically be because a "monitor" command took too long
198 * before printing any progress messages
201 return ERROR_GDB_TIMEOUT
;
205 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
209 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
211 struct gdb_connection
*gdb_con
= connection
->priv
;
212 int retval
= ERROR_OK
;
214 #ifdef _DEBUG_GDB_IO_
218 if (connection
->service
->type
!= CONNECTION_TCP
)
219 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
221 retval
= check_pending(connection
, 1, NULL
);
222 if (retval
!= ERROR_OK
)
224 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
229 if (gdb_con
->buf_cnt
> 0)
231 if (gdb_con
->buf_cnt
== 0) {
232 LOG_DEBUG("GDB connection closed by the remote client");
233 gdb_con
->closed
= true;
234 return ERROR_SERVER_REMOTE_CLOSED
;
238 errno
= WSAGetLastError();
244 case WSAECONNABORTED
:
245 gdb_con
->closed
= true;
246 return ERROR_SERVER_REMOTE_CLOSED
;
248 gdb_con
->closed
= true;
249 return ERROR_SERVER_REMOTE_CLOSED
;
251 LOG_ERROR("read: %d", errno
);
260 gdb_con
->closed
= true;
261 return ERROR_SERVER_REMOTE_CLOSED
;
263 gdb_con
->closed
= true;
264 return ERROR_SERVER_REMOTE_CLOSED
;
266 LOG_ERROR("read: %s", strerror(errno
));
267 gdb_con
->closed
= true;
268 return ERROR_SERVER_REMOTE_CLOSED
;
273 #ifdef _DEBUG_GDB_IO_
274 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
275 LOG_DEBUG("received '%s'", debug_buffer
);
279 gdb_con
->buf_p
= gdb_con
->buffer
;
281 *next_char
= *(gdb_con
->buf_p
++);
282 if (gdb_con
->buf_cnt
> 0)
283 connection
->input_pending
= true;
285 connection
->input_pending
= false;
286 #ifdef _DEBUG_GDB_IO_
287 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
294 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
295 * held in registers in the inner loop.
297 * For small caches and embedded systems this is important!
299 static inline int gdb_get_char_fast(struct connection
*connection
,
300 int *next_char
, char **buf_p
, int *buf_cnt
)
302 int retval
= ERROR_OK
;
304 if ((*buf_cnt
)-- > 0) {
305 *next_char
= **buf_p
;
308 connection
->input_pending
= true;
310 connection
->input_pending
= false;
312 #ifdef _DEBUG_GDB_IO_
313 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
319 struct gdb_connection
*gdb_con
= connection
->priv
;
320 gdb_con
->buf_p
= *buf_p
;
321 gdb_con
->buf_cnt
= *buf_cnt
;
322 retval
= gdb_get_char_inner(connection
, next_char
);
323 *buf_p
= gdb_con
->buf_p
;
324 *buf_cnt
= gdb_con
->buf_cnt
;
329 static int gdb_get_char(struct connection
*connection
, int *next_char
)
331 struct gdb_connection
*gdb_con
= connection
->priv
;
332 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
335 static int gdb_putback_char(struct connection
*connection
, int last_char
)
337 struct gdb_connection
*gdb_con
= connection
->priv
;
339 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
340 *(--gdb_con
->buf_p
) = last_char
;
343 LOG_ERROR("BUG: couldn't put character back");
348 /* The only way we can detect that the socket is closed is the first time
349 * we write to it, we will fail. Subsequent write operations will
350 * succeed. Shudder! */
351 static int gdb_write(struct connection
*connection
, void *data
, int len
)
353 struct gdb_connection
*gdb_con
= connection
->priv
;
354 if (gdb_con
->closed
) {
355 LOG_DEBUG("GDB socket marked as closed, cannot write to it.");
356 return ERROR_SERVER_REMOTE_CLOSED
;
359 if (connection_write(connection
, data
, len
) == len
)
362 LOG_WARNING("Error writing to GDB socket. Dropping the connection.");
363 gdb_con
->closed
= true;
364 return ERROR_SERVER_REMOTE_CLOSED
;
367 static void gdb_log_incoming_packet(struct connection
*connection
, char *packet
)
369 if (!LOG_LEVEL_IS(LOG_LVL_DEBUG
))
372 struct target
*target
= get_target_from_connection(connection
);
374 /* Avoid dumping non-printable characters to the terminal */
375 const unsigned packet_len
= strlen(packet
);
376 const char *nonprint
= find_nonprint_char(packet
, packet_len
);
378 /* Does packet at least have a prefix that is printable?
379 * Look within the first 50 chars of the packet. */
380 const char *colon
= memchr(packet
, ':', MIN(50, packet_len
));
381 const bool packet_has_prefix
= (colon
);
382 const bool packet_prefix_printable
= (packet_has_prefix
&& nonprint
> colon
);
384 if (packet_prefix_printable
) {
385 const unsigned int prefix_len
= colon
- packet
+ 1; /* + 1 to include the ':' */
386 const unsigned int payload_len
= packet_len
- prefix_len
;
387 LOG_TARGET_DEBUG(target
, "received packet: %.*s<binary-data-%u-bytes>", prefix_len
,
388 packet
, payload_len
);
390 LOG_TARGET_DEBUG(target
, "received packet: <binary-data-%u-bytes>", packet_len
);
393 /* All chars printable, dump the packet as is */
394 LOG_TARGET_DEBUG(target
, "received packet: %s", packet
);
398 static void gdb_log_outgoing_packet(struct connection
*connection
, char *packet_buf
,
399 unsigned int packet_len
, unsigned char checksum
)
401 if (!LOG_LEVEL_IS(LOG_LVL_DEBUG
))
404 struct target
*target
= get_target_from_connection(connection
);
406 if (find_nonprint_char(packet_buf
, packet_len
))
407 LOG_TARGET_DEBUG(target
, "sending packet: $<binary-data-%u-bytes>#%2.2x",
408 packet_len
, checksum
);
410 LOG_TARGET_DEBUG(target
, "sending packet: $%.*s#%2.2x", packet_len
, packet_buf
,
414 static int gdb_put_packet_inner(struct connection
*connection
,
415 char *buffer
, int len
)
418 unsigned char my_checksum
= 0;
421 struct gdb_connection
*gdb_con
= connection
->priv
;
423 for (i
= 0; i
< len
; i
++)
424 my_checksum
+= buffer
[i
];
426 #ifdef _DEBUG_GDB_IO_
428 * At this point we should have nothing in the input queue from GDB,
429 * however sometimes '-' is sent even though we've already received
430 * an ACK (+) for everything we've sent off.
434 retval
= check_pending(connection
, 0, &gotdata
);
435 if (retval
!= ERROR_OK
)
439 retval
= gdb_get_char(connection
, &reply
);
440 if (retval
!= ERROR_OK
)
443 /* fix a problem with some IAR tools */
444 gdb_putback_char(connection
, reply
);
445 LOG_DEBUG("Unexpected start of new packet");
449 LOG_WARNING("Discard unexpected char %c", reply
);
454 gdb_log_outgoing_packet(connection
, buffer
, len
, my_checksum
);
456 char local_buffer
[1024];
457 local_buffer
[0] = '$';
458 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
459 /* performance gain on smaller packets by only a single call to gdb_write() */
460 memcpy(local_buffer
+ 1, buffer
, len
++);
461 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
462 retval
= gdb_write(connection
, local_buffer
, len
);
463 if (retval
!= ERROR_OK
)
466 /* larger packets are transmitted directly from caller supplied buffer
467 * by several calls to gdb_write() to avoid dynamic allocation */
468 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
469 retval
= gdb_write(connection
, local_buffer
, 1);
470 if (retval
!= ERROR_OK
)
472 retval
= gdb_write(connection
, buffer
, len
);
473 if (retval
!= ERROR_OK
)
475 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
476 if (retval
!= ERROR_OK
)
480 if (gdb_con
->noack_mode
)
483 retval
= gdb_get_char(connection
, &reply
);
484 if (retval
!= ERROR_OK
)
488 gdb_log_incoming_packet(connection
, "+");
490 } else if (reply
== '-') {
491 /* Stop sending output packets for now */
492 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
493 gdb_log_incoming_packet(connection
, "-");
494 LOG_WARNING("negative reply, retrying");
495 } else if (reply
== 0x3) {
496 gdb_con
->ctrl_c
= true;
497 gdb_log_incoming_packet(connection
, "<Ctrl-C>");
498 retval
= gdb_get_char(connection
, &reply
);
499 if (retval
!= ERROR_OK
)
502 gdb_log_incoming_packet(connection
, "+");
504 } else if (reply
== '-') {
505 /* Stop sending output packets for now */
506 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
507 gdb_log_incoming_packet(connection
, "-");
508 LOG_WARNING("negative reply, retrying");
509 } else if (reply
== '$') {
510 LOG_ERROR("GDB missing ack(1) - assumed good");
511 gdb_putback_char(connection
, reply
);
514 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
515 gdb_con
->closed
= true;
516 return ERROR_SERVER_REMOTE_CLOSED
;
518 } else if (reply
== '$') {
519 LOG_ERROR("GDB missing ack(2) - assumed good");
520 gdb_putback_char(connection
, reply
);
523 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
525 gdb_con
->closed
= true;
526 return ERROR_SERVER_REMOTE_CLOSED
;
530 return ERROR_SERVER_REMOTE_CLOSED
;
535 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
537 struct gdb_connection
*gdb_con
= connection
->priv
;
538 gdb_con
->busy
= true;
539 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
540 gdb_con
->busy
= false;
542 /* we sent some data, reset timer for keep alive messages */
548 static inline int fetch_packet(struct connection
*connection
,
549 int *checksum_ok
, int noack
, int *len
, char *buffer
)
551 unsigned char my_checksum
= 0;
554 int retval
= ERROR_OK
;
556 struct gdb_connection
*gdb_con
= connection
->priv
;
561 /* move this over into local variables to use registers and give the
562 * more freedom to optimize */
563 char *buf_p
= gdb_con
->buf_p
;
564 int buf_cnt
= gdb_con
->buf_cnt
;
567 /* The common case is that we have an entire packet with no escape chars.
568 * We need to leave at least 2 bytes in the buffer to have
569 * gdb_get_char() update various bits and bobs correctly.
571 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
572 /* The compiler will struggle a bit with constant propagation and
573 * aliasing, so we help it by showing that these values do not
574 * change inside the loop
578 int run
= buf_cnt
- 2;
584 if (character
== '#') {
585 /* Danger! character can be '#' when esc is
586 * used so we need an explicit boolean for done here. */
591 if (character
== '}') {
592 /* data transmitted in binary mode (X packet)
593 * uses 0x7d as escape character */
594 my_checksum
+= character
& 0xff;
597 my_checksum
+= character
& 0xff;
598 buffer
[count
++] = (character
^ 0x20) & 0xff;
600 my_checksum
+= character
& 0xff;
601 buffer
[count
++] = character
& 0xff;
610 LOG_ERROR("packet buffer too small");
611 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
615 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
616 if (retval
!= ERROR_OK
)
619 if (character
== '#')
622 if (character
== '}') {
623 /* data transmitted in binary mode (X packet)
624 * uses 0x7d as escape character */
625 my_checksum
+= character
& 0xff;
627 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
628 if (retval
!= ERROR_OK
)
631 my_checksum
+= character
& 0xff;
632 buffer
[count
++] = (character
^ 0x20) & 0xff;
634 my_checksum
+= character
& 0xff;
635 buffer
[count
++] = character
& 0xff;
639 gdb_con
->buf_p
= buf_p
;
640 gdb_con
->buf_cnt
= buf_cnt
;
642 if (retval
!= ERROR_OK
)
647 retval
= gdb_get_char(connection
, &character
);
648 if (retval
!= ERROR_OK
)
650 checksum
[0] = character
;
651 retval
= gdb_get_char(connection
, &character
);
652 if (retval
!= ERROR_OK
)
654 checksum
[1] = character
;
658 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
663 static int gdb_get_packet_inner(struct connection
*connection
,
664 char *buffer
, int *len
)
668 struct gdb_connection
*gdb_con
= connection
->priv
;
672 retval
= gdb_get_char(connection
, &character
);
673 if (retval
!= ERROR_OK
)
676 #ifdef _DEBUG_GDB_IO_
677 LOG_DEBUG("character: '%c'", character
);
684 gdb_log_incoming_packet(connection
, "+");
685 /* According to the GDB documentation
686 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
687 * "gdb sends a final `+` acknowledgment of the stub's `OK`
688 * response, which can be safely ignored by the stub."
689 * However OpenOCD server already is in noack mode at this
690 * point and instead of ignoring this it was emitting a
691 * warning. This code makes server ignore the first ACK
692 * that will be received after going into noack mode,
693 * warning only about subsequent ACK's. */
694 if (gdb_con
->noack_mode
> 1) {
695 LOG_WARNING("acknowledgment received, but no packet pending");
696 } else if (gdb_con
->noack_mode
) {
697 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
698 gdb_con
->noack_mode
= 2;
702 gdb_log_incoming_packet(connection
, "-");
703 LOG_WARNING("negative acknowledgment, but no packet pending");
706 gdb_log_incoming_packet(connection
, "<Ctrl-C>");
707 gdb_con
->ctrl_c
= true;
711 LOG_WARNING("ignoring character 0x%x", character
);
714 } while (character
!= '$');
717 /* explicit code expansion here to get faster inlined code in -O3 by not
718 * calculating checksum */
719 if (gdb_con
->noack_mode
) {
720 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
721 if (retval
!= ERROR_OK
)
724 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
725 if (retval
!= ERROR_OK
)
729 if (gdb_con
->noack_mode
) {
730 /* checksum is not checked in noack mode */
734 retval
= gdb_write(connection
, "+", 1);
735 if (retval
!= ERROR_OK
)
741 return ERROR_SERVER_REMOTE_CLOSED
;
746 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
748 struct gdb_connection
*gdb_con
= connection
->priv
;
749 gdb_con
->busy
= true;
750 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
751 gdb_con
->busy
= false;
755 static int gdb_output_con(struct connection
*connection
, const char *line
)
760 bin_size
= strlen(line
);
762 hex_buffer
= malloc(bin_size
* 2 + 2);
764 return ERROR_GDB_BUFFER_TOO_SMALL
;
767 size_t pkt_len
= hexify(hex_buffer
+ 1, (const uint8_t *)line
, bin_size
,
769 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
775 static int gdb_output(struct command_context
*context
, const char *line
)
777 /* this will be dumped to the log and also sent as an O packet if possible */
778 LOG_USER_N("%s", line
);
782 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
784 struct gdb_connection
*gdb_connection
= connection
->priv
;
786 char stop_reason
[32];
787 char current_thread
[25];
791 rtos_update_threads(target
);
793 if (target
->debug_reason
== DBG_REASON_EXIT
) {
794 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
798 target
->rtos
->current_threadid
= target
->rtos
->current_thread
;
799 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &ct
);
804 if (gdb_connection
->ctrl_c
) {
805 LOG_TARGET_DEBUG(target
, "Responding with signal 2 (SIGINT) to debugger due to Ctrl-C");
808 signal_var
= gdb_last_signal(ct
);
810 stop_reason
[0] = '\0';
811 if (ct
->debug_reason
== DBG_REASON_WATCHPOINT
) {
812 enum watchpoint_rw hit_wp_type
;
813 target_addr_t hit_wp_address
;
815 if (watchpoint_hit(ct
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
817 switch (hit_wp_type
) {
819 snprintf(stop_reason
, sizeof(stop_reason
),
820 "watch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
823 snprintf(stop_reason
, sizeof(stop_reason
),
824 "rwatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
827 snprintf(stop_reason
, sizeof(stop_reason
),
828 "awatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
836 current_thread
[0] = '\0';
838 snprintf(current_thread
, sizeof(current_thread
), "thread:%" PRIx64
";",
839 target
->rtos
->current_thread
);
841 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s%s",
842 signal_var
, stop_reason
, current_thread
);
844 gdb_connection
->ctrl_c
= false;
847 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
848 gdb_connection
->frontend_state
= TARGET_HALTED
;
851 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
853 struct gdb_connection
*gdb_connection
= connection
->priv
;
854 char fileio_command
[256];
856 bool program_exited
= false;
858 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
859 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
860 target
->fileio_info
->param_1
,
861 target
->fileio_info
->param_2
+ 1, /* len + trailing zero */
862 target
->fileio_info
->param_3
,
863 target
->fileio_info
->param_4
);
864 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
865 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
866 target
->fileio_info
->param_1
);
867 else if (strcmp(target
->fileio_info
->identifier
, "read") == 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
, "write") == 0)
873 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
874 target
->fileio_info
->param_1
,
875 target
->fileio_info
->param_2
,
876 target
->fileio_info
->param_3
);
877 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
878 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
879 target
->fileio_info
->param_1
,
880 target
->fileio_info
->param_2
,
881 target
->fileio_info
->param_3
);
882 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
883 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
884 target
->fileio_info
->param_1
,
885 target
->fileio_info
->param_2
+ 1, /* len + trailing zero */
886 target
->fileio_info
->param_3
,
887 target
->fileio_info
->param_4
+ 1); /* len + trailing zero */
888 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
889 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
890 target
->fileio_info
->param_1
,
891 target
->fileio_info
->param_2
+ 1); /* len + trailing zero */
892 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
893 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
894 target
->fileio_info
->param_1
,
895 target
->fileio_info
->param_2
,
896 target
->fileio_info
->param_3
);
897 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
898 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
899 target
->fileio_info
->param_1
,
900 target
->fileio_info
->param_2
);
901 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
902 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
903 target
->fileio_info
->param_1
,
904 target
->fileio_info
->param_2
);
905 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
906 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
907 target
->fileio_info
->param_1
);
908 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
909 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
910 target
->fileio_info
->param_1
,
911 target
->fileio_info
->param_2
+ 1); /* len + trailing zero */
912 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
913 /* If target hits exit syscall, report to GDB the program is terminated.
914 * In addition, let target run its own exit syscall handler. */
915 program_exited
= true;
916 sprintf(fileio_command
, "W%02" PRIx64
, target
->fileio_info
->param_1
);
918 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
920 /* encounter unknown syscall, continue */
921 gdb_connection
->frontend_state
= TARGET_RUNNING
;
922 target_resume(target
, 1, 0x0, 0, 0);
926 command_len
= strlen(fileio_command
);
927 gdb_put_packet(connection
, fileio_command
, command_len
);
929 if (program_exited
) {
930 /* Use target_resume() to let target run its own exit syscall handler. */
931 gdb_connection
->frontend_state
= TARGET_RUNNING
;
932 target_resume(target
, 1, 0x0, 0, 0);
934 gdb_connection
->frontend_state
= TARGET_HALTED
;
935 rtos_update_threads(target
);
939 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
941 struct gdb_connection
*gdb_connection
= connection
->priv
;
943 /* In the GDB protocol when we are stepping or continuing execution,
944 * we have a lingering reply. Upon receiving a halted event
945 * when we have that lingering packet, we reply to the original
946 * step or continue packet.
948 * Executing monitor commands can bring the target in and
949 * out of the running state so we'll see lots of TARGET_EVENT_XXX
950 * that are to be ignored.
952 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
953 /* stop forwarding log packets! */
954 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
956 /* check fileio first */
957 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
958 gdb_fileio_reply(target
, connection
);
960 gdb_signal_reply(target
, connection
);
964 static int gdb_target_callback_event_handler(struct target
*target
,
965 enum target_event event
, void *priv
)
967 struct connection
*connection
= priv
;
968 struct gdb_service
*gdb_service
= connection
->service
->priv
;
970 if (gdb_service
->target
!= target
)
974 case TARGET_EVENT_GDB_HALT
:
975 gdb_frontend_halted(target
, connection
);
977 case TARGET_EVENT_HALTED
:
978 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
987 static int gdb_new_connection(struct connection
*connection
)
989 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
990 struct target
*target
;
994 target
= get_target_from_connection(connection
);
995 connection
->priv
= gdb_connection
;
996 connection
->cmd_ctx
->current_target
= target
;
998 /* initialize gdb connection information */
999 gdb_connection
->buf_p
= gdb_connection
->buffer
;
1000 gdb_connection
->buf_cnt
= 0;
1001 gdb_connection
->ctrl_c
= false;
1002 gdb_connection
->frontend_state
= TARGET_HALTED
;
1003 gdb_connection
->vflash_image
= NULL
;
1004 gdb_connection
->closed
= false;
1005 gdb_connection
->busy
= false;
1006 gdb_connection
->noack_mode
= 0;
1007 gdb_connection
->sync
= false;
1008 gdb_connection
->mem_write_error
= false;
1009 gdb_connection
->attached
= true;
1010 gdb_connection
->extended_protocol
= false;
1011 gdb_connection
->target_desc
.tdesc
= NULL
;
1012 gdb_connection
->target_desc
.tdesc_length
= 0;
1013 gdb_connection
->thread_list
= NULL
;
1014 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
1016 /* send ACK to GDB for debug request */
1017 gdb_write(connection
, "+", 1);
1019 /* output goes through gdb connection */
1020 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
1022 /* we must remove all breakpoints registered to the target as a previous
1023 * GDB session could leave dangling breakpoints if e.g. communication
1026 breakpoint_clear_target(target
);
1027 watchpoint_clear_target(target
);
1029 /* Since version 3.95 (gdb-19990504), with the exclusion of 6.5~6.8, GDB
1030 * sends an ACK at connection with the following comment in its source code:
1031 * "Ack any packet which the remote side has already sent."
1032 * LLDB does the same since the first gdb-remote implementation.
1033 * Remove the initial ACK from the incoming buffer.
1035 retval
= gdb_get_char(connection
, &initial_ack
);
1036 if (retval
!= ERROR_OK
)
1039 if (initial_ack
!= '+')
1040 gdb_putback_char(connection
, initial_ack
);
1042 target_call_event_callbacks(target
, TARGET_EVENT_GDB_ATTACH
);
1045 /* clean previous rtos session if supported*/
1046 if (target
->rtos
->type
->clean
)
1047 target
->rtos
->type
->clean(target
);
1049 /* update threads */
1050 rtos_update_threads(target
);
1053 if (gdb_use_memory_map
) {
1054 /* Connect must fail if the memory map can't be set up correctly.
1056 * This will cause an auto_probe to be invoked, which is either
1057 * a no-op or it will fail when the target isn't ready(e.g. not halted).
1059 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1060 struct flash_bank
*p
;
1061 p
= get_flash_bank_by_num_noprobe(i
);
1062 if (p
->target
!= target
)
1064 retval
= get_flash_bank_by_num(i
, &p
);
1065 if (retval
!= ERROR_OK
) {
1066 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target "
1067 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1073 gdb_actual_connections
++;
1074 log_printf_lf(all_targets
->next
? LOG_LVL_INFO
: LOG_LVL_DEBUG
,
1075 __FILE__
, __LINE__
, __func__
,
1076 "New GDB Connection: %d, Target %s, state: %s",
1077 gdb_actual_connections
,
1078 target_name(target
),
1079 target_state_name(target
));
1081 if (!target_was_examined(target
)) {
1082 LOG_ERROR("Target %s not examined yet, refuse gdb connection %d!",
1083 target_name(target
), gdb_actual_connections
);
1084 gdb_actual_connections
--;
1085 return ERROR_TARGET_NOT_EXAMINED
;
1088 if (target
->state
!= TARGET_HALTED
)
1089 LOG_WARNING("GDB connection %d on target %s not halted",
1090 gdb_actual_connections
, target_name(target
));
1092 /* DANGER! If we fail subsequently, we must remove this handler,
1093 * otherwise we occasionally see crashes as the timer can invoke the
1096 * register callback to be informed about target events */
1097 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1099 log_add_callback(gdb_log_callback
, connection
);
1104 static int gdb_connection_closed(struct connection
*connection
)
1106 struct target
*target
;
1107 struct gdb_connection
*gdb_connection
= connection
->priv
;
1109 target
= get_target_from_connection(connection
);
1111 /* we're done forwarding messages. Tear down callback before
1112 * cleaning up connection.
1114 log_remove_callback(gdb_log_callback
, connection
);
1116 gdb_actual_connections
--;
1117 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1118 target_name(target
),
1119 target_state_name(target
),
1120 gdb_actual_connections
);
1122 /* see if an image built with vFlash commands is left */
1123 if (gdb_connection
->vflash_image
) {
1124 image_close(gdb_connection
->vflash_image
);
1125 free(gdb_connection
->vflash_image
);
1126 gdb_connection
->vflash_image
= NULL
;
1129 /* if this connection registered a debug-message receiver delete it */
1130 delete_debug_msg_receiver(connection
->cmd_ctx
, target
);
1132 free(connection
->priv
);
1133 connection
->priv
= NULL
;
1135 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1137 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
1139 target_call_event_callbacks(target
, TARGET_EVENT_GDB_DETACH
);
1144 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1147 snprintf(err
, 4, "E%2.2X", the_error
);
1148 gdb_put_packet(connection
, err
, 3);
1151 static int gdb_last_signal_packet(struct connection
*connection
,
1152 char const *packet
, int packet_size
)
1154 struct target
*target
= get_target_from_connection(connection
);
1155 struct gdb_connection
*gdb_con
= connection
->priv
;
1159 if (!gdb_con
->attached
) {
1160 /* if we are here we have received a kill packet
1161 * reply W stop reply otherwise gdb gets very unhappy */
1162 gdb_put_packet(connection
, "W00", 3);
1166 signal_var
= gdb_last_signal(target
);
1168 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1169 gdb_put_packet(connection
, sig_reply
, 3);
1174 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1176 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1179 return len
- 1 - pos
;
1182 /* Convert register to string of bytes. NB! The # of bits in the
1183 * register might be non-divisible by 8(a byte), in which
1184 * case an entire byte is shown.
1186 * NB! the format on the wire is the target endianness
1188 * The format of reg->value is little endian
1191 static void gdb_str_to_target(struct target
*target
,
1192 char *tstr
, struct reg
*reg
)
1199 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1201 for (i
= 0; i
< buf_len
; i
++) {
1202 int j
= gdb_reg_pos(target
, i
, buf_len
);
1203 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1207 /* copy over in register buffer */
1208 static void gdb_target_to_reg(struct target
*target
,
1209 char const *tstr
, int str_len
, uint8_t *bin
)
1212 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1217 for (i
= 0; i
< str_len
; i
+= 2) {
1219 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1220 LOG_ERROR("BUG: unable to convert register value");
1224 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1229 static int gdb_get_registers_packet(struct connection
*connection
,
1230 char const *packet
, int packet_size
)
1232 struct target
*target
= get_target_from_connection(connection
);
1233 struct reg
**reg_list
;
1236 int reg_packet_size
= 0;
1241 #ifdef _DEBUG_GDB_IO_
1245 if ((target
->rtos
) && (rtos_get_gdb_reg_list(connection
) == ERROR_OK
))
1248 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1250 if (retval
!= ERROR_OK
)
1251 return gdb_error(connection
, retval
);
1253 for (i
= 0; i
< reg_list_size
; i
++) {
1254 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1256 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1259 assert(reg_packet_size
> 0);
1261 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1265 reg_packet_p
= reg_packet
;
1267 for (i
= 0; i
< reg_list_size
; i
++) {
1268 if (!reg_list
[i
] || reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
1270 if (!reg_list
[i
]->valid
) {
1271 retval
= reg_list
[i
]->type
->get(reg_list
[i
]);
1272 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1273 LOG_DEBUG("Couldn't get register %s.", reg_list
[i
]->name
);
1276 return gdb_error(connection
, retval
);
1279 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1280 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1283 #ifdef _DEBUG_GDB_IO_
1285 char *reg_packet_p_debug
;
1286 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1287 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1288 free(reg_packet_p_debug
);
1292 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1300 static int gdb_set_registers_packet(struct connection
*connection
,
1301 char const *packet
, int packet_size
)
1303 struct target
*target
= get_target_from_connection(connection
);
1305 struct reg
**reg_list
;
1308 char const *packet_p
;
1310 #ifdef _DEBUG_GDB_IO_
1314 /* skip command character */
1318 if (packet_size
% 2) {
1319 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1320 return ERROR_SERVER_REMOTE_CLOSED
;
1323 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1325 if (retval
!= ERROR_OK
)
1326 return gdb_error(connection
, retval
);
1329 for (i
= 0; i
< reg_list_size
; i
++) {
1331 if (!reg_list
[i
] || !reg_list
[i
]->exist
|| reg_list
[i
]->hidden
)
1333 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1335 if (packet_p
+ chars
> packet
+ packet_size
)
1336 LOG_ERROR("BUG: register packet is too small for registers");
1338 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1339 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1341 retval
= reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1342 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1343 LOG_DEBUG("Couldn't set register %s.", reg_list
[i
]->name
);
1346 return gdb_error(connection
, retval
);
1349 /* advance packet pointer */
1355 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1358 gdb_put_packet(connection
, "OK", 2);
1363 static int gdb_get_register_packet(struct connection
*connection
,
1364 char const *packet
, int packet_size
)
1366 struct target
*target
= get_target_from_connection(connection
);
1368 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1369 struct reg
**reg_list
;
1373 #ifdef _DEBUG_GDB_IO_
1377 if ((target
->rtos
) && (rtos_get_gdb_reg(connection
, reg_num
) == ERROR_OK
))
1380 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1382 if (retval
!= ERROR_OK
)
1383 return gdb_error(connection
, retval
);
1385 if ((reg_list_size
<= reg_num
) || !reg_list
[reg_num
] ||
1386 !reg_list
[reg_num
]->exist
|| reg_list
[reg_num
]->hidden
) {
1387 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num
);
1388 return ERROR_SERVER_REMOTE_CLOSED
;
1391 if (!reg_list
[reg_num
]->valid
) {
1392 retval
= reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1393 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1394 LOG_DEBUG("Couldn't get register %s.", reg_list
[reg_num
]->name
);
1396 return gdb_error(connection
, retval
);
1400 reg_packet
= calloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1, 1); /* plus one for string termination null */
1402 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
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 /* We don't have to worry about the default 2 second timeout for GDB packets,
1494 * because GDB breaks up large memory reads into smaller reads.
1496 static int gdb_read_memory_packet(struct connection
*connection
,
1497 char const *packet
, int packet_size
)
1499 struct target
*target
= get_target_from_connection(connection
);
1507 int retval
= ERROR_OK
;
1509 /* skip command character */
1512 addr
= strtoull(packet
, &separator
, 16);
1514 if (*separator
!= ',') {
1515 LOG_ERROR("incomplete read memory packet received, dropping connection");
1516 return ERROR_SERVER_REMOTE_CLOSED
;
1519 len
= strtoul(separator
+ 1, NULL
, 16);
1522 LOG_WARNING("invalid read memory packet received (len == 0)");
1523 gdb_put_packet(connection
, "", 0);
1527 buffer
= malloc(len
);
1529 LOG_DEBUG("addr: 0x%16.16" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1531 retval
= ERROR_NOT_IMPLEMENTED
;
1533 retval
= rtos_read_buffer(target
, addr
, len
, buffer
);
1534 if (retval
== ERROR_NOT_IMPLEMENTED
)
1535 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1537 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1538 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1539 * At some point this might be fixed in GDB, in which case this code can be removed.
1541 * OpenOCD developers are acutely aware of this problem, but there is nothing
1542 * gained by involving the user in this problem that hopefully will get resolved
1545 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1546 * cmd = view%20audit-trail&database = gdb&pr = 2395
1548 * For now, the default is to fix up things to make current GDB versions work.
1549 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1551 memset(buffer
, 0, len
);
1555 if (retval
== ERROR_OK
) {
1556 hex_buffer
= malloc(len
* 2 + 1);
1558 size_t pkt_len
= hexify(hex_buffer
, buffer
, len
, len
* 2 + 1);
1560 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1564 retval
= gdb_error(connection
, retval
);
1571 static int gdb_write_memory_packet(struct connection
*connection
,
1572 char const *packet
, int packet_size
)
1574 struct target
*target
= get_target_from_connection(connection
);
1582 /* skip command character */
1585 addr
= strtoull(packet
, &separator
, 16);
1587 if (*separator
!= ',') {
1588 LOG_ERROR("incomplete write memory packet received, dropping connection");
1589 return ERROR_SERVER_REMOTE_CLOSED
;
1592 len
= strtoul(separator
+ 1, &separator
, 16);
1594 if (*(separator
++) != ':') {
1595 LOG_ERROR("incomplete write memory packet received, dropping connection");
1596 return ERROR_SERVER_REMOTE_CLOSED
;
1599 buffer
= malloc(len
);
1601 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1603 if (unhexify(buffer
, separator
, len
) != len
)
1604 LOG_ERROR("unable to decode memory packet");
1606 retval
= ERROR_NOT_IMPLEMENTED
;
1608 retval
= rtos_write_buffer(target
, addr
, len
, buffer
);
1609 if (retval
== ERROR_NOT_IMPLEMENTED
)
1610 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1612 if (retval
== ERROR_OK
)
1613 gdb_put_packet(connection
, "OK", 2);
1615 retval
= gdb_error(connection
, retval
);
1622 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1623 char const *packet
, int packet_size
)
1625 struct target
*target
= get_target_from_connection(connection
);
1630 int retval
= ERROR_OK
;
1631 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1632 * the assumption that we're in a download and it's important to go as fast
1634 uint32_t fast_limit
= 8;
1636 /* skip command character */
1639 addr
= strtoull(packet
, &separator
, 16);
1641 if (*separator
!= ',') {
1642 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1643 return ERROR_SERVER_REMOTE_CLOSED
;
1646 len
= strtoul(separator
+ 1, &separator
, 16);
1648 if (*(separator
++) != ':') {
1649 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1650 return ERROR_SERVER_REMOTE_CLOSED
;
1653 struct gdb_connection
*gdb_connection
= connection
->priv
;
1655 if (gdb_connection
->mem_write_error
)
1656 retval
= ERROR_FAIL
;
1658 if (retval
== ERROR_OK
) {
1659 if (len
>= fast_limit
) {
1660 /* By replying the packet *immediately* GDB will send us a new packet
1661 * while we write the last one to the target.
1662 * We only do this for larger writes, so that users who do something like:
1663 * p *((int*)0xdeadbeef)=8675309
1664 * will get immediate feedback that that write failed.
1666 gdb_put_packet(connection
, "OK", 2);
1669 retval
= gdb_error(connection
, retval
);
1670 /* now that we have reported the memory write error, we can clear the condition */
1671 gdb_connection
->mem_write_error
= false;
1672 if (retval
!= ERROR_OK
)
1677 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1679 retval
= ERROR_NOT_IMPLEMENTED
;
1681 retval
= rtos_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1682 if (retval
== ERROR_NOT_IMPLEMENTED
)
1683 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1685 if (retval
!= ERROR_OK
)
1686 gdb_connection
->mem_write_error
= true;
1689 if (len
< fast_limit
) {
1690 if (retval
!= ERROR_OK
) {
1691 gdb_error(connection
, retval
);
1692 gdb_connection
->mem_write_error
= false;
1694 gdb_put_packet(connection
, "OK", 2);
1701 static int gdb_step_continue_packet(struct connection
*connection
,
1702 char const *packet
, int packet_size
)
1704 struct target
*target
= get_target_from_connection(connection
);
1706 uint64_t address
= 0x0;
1707 int retval
= ERROR_OK
;
1711 if (packet_size
> 1)
1712 address
= strtoull(packet
+ 1, NULL
, 16);
1716 gdb_running_type
= packet
[0];
1717 if (packet
[0] == 'c') {
1718 LOG_DEBUG("continue");
1719 /* resume at current address, don't handle breakpoints, not debugging */
1720 retval
= target_resume(target
, current
, address
, 0, 0);
1721 } else if (packet
[0] == 's') {
1723 /* step at current or address, don't handle breakpoints */
1724 retval
= target_step(target
, current
, address
, 0);
1729 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1730 char const *packet
, int packet_size
)
1732 struct target
*target
= get_target_from_connection(connection
);
1734 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1735 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1741 LOG_DEBUG("[%s]", target_name(target
));
1743 type
= strtoul(packet
+ 1, &separator
, 16);
1745 if (type
== 0) /* memory breakpoint */
1746 bp_type
= BKPT_SOFT
;
1747 else if (type
== 1) /* hardware breakpoint */
1748 bp_type
= BKPT_HARD
;
1749 else if (type
== 2) /* write watchpoint */
1750 wp_type
= WPT_WRITE
;
1751 else if (type
== 3) /* read watchpoint */
1753 else if (type
== 4) /* access watchpoint */
1754 wp_type
= WPT_ACCESS
;
1756 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1757 return ERROR_SERVER_REMOTE_CLOSED
;
1760 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1761 bp_type
= gdb_breakpoint_override_type
;
1763 if (*separator
!= ',') {
1764 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1765 return ERROR_SERVER_REMOTE_CLOSED
;
1768 address
= strtoull(separator
+ 1, &separator
, 16);
1770 if (*separator
!= ',') {
1771 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1772 return ERROR_SERVER_REMOTE_CLOSED
;
1775 size
= strtoul(separator
+ 1, &separator
, 16);
1780 if (packet
[0] == 'Z') {
1781 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1782 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1783 /* Send empty reply to report that breakpoints of this type are not supported */
1784 gdb_put_packet(connection
, "", 0);
1785 } else if (retval
!= ERROR_OK
) {
1786 retval
= gdb_error(connection
, retval
);
1787 if (retval
!= ERROR_OK
)
1790 gdb_put_packet(connection
, "OK", 2);
1792 breakpoint_remove(target
, address
);
1793 gdb_put_packet(connection
, "OK", 2);
1800 if (packet
[0] == 'Z') {
1801 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1802 if (retval
== ERROR_NOT_IMPLEMENTED
) {
1803 /* Send empty reply to report that watchpoints of this type are not supported */
1804 gdb_put_packet(connection
, "", 0);
1805 } else if (retval
!= ERROR_OK
) {
1806 retval
= gdb_error(connection
, retval
);
1807 if (retval
!= ERROR_OK
)
1810 gdb_put_packet(connection
, "OK", 2);
1812 watchpoint_remove(target
, address
);
1813 gdb_put_packet(connection
, "OK", 2);
1824 /* print out a string and allocate more space as needed,
1825 * mainly used for XML at this point
1827 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT
, 5, 6))) void xml_printf(int *retval
,
1828 char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1830 if (*retval
!= ERROR_OK
)
1835 if ((!*xml
) || (!first
)) {
1836 /* start by 0 to exercise all the code paths.
1837 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1839 *size
= *size
* 2 + 2;
1841 *xml
= realloc(*xml
, *size
);
1844 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1852 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1854 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1858 /* there was just enough or not enough space, allocate more. */
1863 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1865 /* Locate the annex. */
1866 const char *annex_end
= strchr(buf
, ':');
1870 /* After the read marker and annex, qXfer looks like a
1871 * traditional 'm' packet. */
1873 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1875 if (*separator
!= ',')
1878 *len
= strtoul(separator
+ 1, NULL
, 16);
1880 /* Extract the annex if needed */
1882 *annex
= strndup(buf
, annex_end
- buf
);
1890 static int compare_bank(const void *a
, const void *b
)
1892 struct flash_bank
*b1
, *b2
;
1893 b1
= *((struct flash_bank
**)a
);
1894 b2
= *((struct flash_bank
**)b
);
1896 if (b1
->base
== b2
->base
)
1898 else if (b1
->base
> b2
->base
)
1904 static int gdb_memory_map(struct connection
*connection
,
1905 char const *packet
, int packet_size
)
1907 /* We get away with only specifying flash here. Regions that are not
1908 * specified are treated as if we provided no memory map(if not we
1909 * could detect the holes and mark them as RAM).
1910 * Normally we only execute this code once, but no big deal if we
1911 * have to regenerate it a couple of times.
1914 struct target
*target
= get_target_from_connection(connection
);
1915 struct flash_bank
*p
;
1919 int retval
= ERROR_OK
;
1920 struct flash_bank
**banks
;
1924 target_addr_t ram_start
= 0;
1925 unsigned int target_flash_banks
= 0;
1927 /* skip command character */
1930 offset
= strtoul(packet
, &separator
, 16);
1931 length
= strtoul(separator
+ 1, &separator
, 16);
1933 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1935 /* Sort banks in ascending order. We need to report non-flash
1936 * memory as ram (or rather read/write) by default for GDB, since
1937 * it has no concept of non-cacheable read/write memory (i/o etc).
1939 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1941 for (unsigned int i
= 0; i
< flash_get_bank_count(); i
++) {
1942 p
= get_flash_bank_by_num_noprobe(i
);
1943 if (p
->target
!= target
)
1945 retval
= get_flash_bank_by_num(i
, &p
);
1946 if (retval
!= ERROR_OK
) {
1948 gdb_error(connection
, retval
);
1951 banks
[target_flash_banks
++] = p
;
1954 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1957 for (unsigned int i
= 0; i
< target_flash_banks
; i
++) {
1958 unsigned sector_size
= 0;
1959 unsigned group_len
= 0;
1963 if (ram_start
< p
->base
)
1964 xml_printf(&retval
, &xml
, &pos
, &size
,
1965 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1966 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
1967 ram_start
, p
->base
- ram_start
);
1969 /* Report adjacent groups of same-size sectors. So for
1970 * example top boot CFI flash will list an initial region
1971 * with several large sectors (maybe 128KB) and several
1972 * smaller ones at the end (maybe 32KB). STR7 will have
1973 * regions with 8KB, 32KB, and 64KB sectors; etc.
1975 for (unsigned int j
= 0; j
< p
->num_sectors
; j
++) {
1977 /* Maybe start a new group of sectors. */
1978 if (sector_size
== 0) {
1979 if (p
->sectors
[j
].offset
+ p
->sectors
[j
].size
> p
->size
) {
1980 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1981 " overflows the end of %s bank.",
1982 p
->sectors
[j
].offset
, p
->name
);
1983 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1986 target_addr_t start
;
1987 start
= p
->base
+ p
->sectors
[j
].offset
;
1988 xml_printf(&retval
, &xml
, &pos
, &size
,
1989 "<memory type=\"flash\" "
1990 "start=\"" TARGET_ADDR_FMT
"\" ",
1992 sector_size
= p
->sectors
[j
].size
;
1993 group_len
= sector_size
;
1995 group_len
+= sector_size
; /* equal to p->sectors[j].size */
1998 /* Does this finish a group of sectors?
1999 * If not, continue an already-started group.
2001 if (j
< p
->num_sectors
- 1
2002 && p
->sectors
[j
+ 1].size
== sector_size
2003 && p
->sectors
[j
+ 1].offset
== p
->sectors
[j
].offset
+ sector_size
2004 && p
->sectors
[j
+ 1].offset
+ p
->sectors
[j
+ 1].size
<= p
->size
)
2007 xml_printf(&retval
, &xml
, &pos
, &size
,
2008 "length=\"0x%x\">\n"
2009 "<property name=\"blocksize\">"
2017 ram_start
= p
->base
+ p
->size
;
2021 xml_printf(&retval
, &xml
, &pos
, &size
,
2022 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
2023 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
2024 ram_start
, target_address_max(target
) - ram_start
+ 1);
2025 /* ELSE a flash chip could be at the very end of the address space, in
2026 * which case ram_start will be precisely 0 */
2030 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
2032 if (retval
!= ERROR_OK
) {
2034 gdb_error(connection
, retval
);
2038 if (offset
+ length
> pos
)
2039 length
= pos
- offset
;
2041 char *t
= malloc(length
+ 1);
2043 memcpy(t
+ 1, xml
+ offset
, length
);
2044 gdb_put_packet(connection
, t
, length
+ 1);
2051 static const char *gdb_get_reg_type_name(enum reg_type type
)
2060 case REG_TYPE_INT16
:
2062 case REG_TYPE_INT32
:
2064 case REG_TYPE_INT64
:
2066 case REG_TYPE_INT128
:
2070 case REG_TYPE_UINT8
:
2072 case REG_TYPE_UINT16
:
2074 case REG_TYPE_UINT32
:
2076 case REG_TYPE_UINT64
:
2078 case REG_TYPE_UINT128
:
2080 case REG_TYPE_CODE_PTR
:
2082 case REG_TYPE_DATA_PTR
:
2084 case REG_TYPE_FLOAT
:
2086 case REG_TYPE_IEEE_SINGLE
:
2087 return "ieee_single";
2088 case REG_TYPE_IEEE_DOUBLE
:
2089 return "ieee_double";
2090 case REG_TYPE_ARCH_DEFINED
:
2091 return "int"; /* return arbitrary string to avoid compile warning. */
2094 return "int"; /* "int" as default value */
2097 static int lookup_add_arch_defined_types(char const **arch_defined_types_list
[], const char *type_id
,
2098 int *num_arch_defined_types
)
2100 int tbl_sz
= *num_arch_defined_types
;
2102 if (type_id
&& (strcmp(type_id
, ""))) {
2103 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2104 if (!((*arch_defined_types_list
)[j
])) {
2105 (*arch_defined_types_list
)[tbl_sz
++] = type_id
;
2106 *arch_defined_types_list
= realloc(*arch_defined_types_list
,
2107 sizeof(char *) * (tbl_sz
+ 1));
2108 (*arch_defined_types_list
)[tbl_sz
] = NULL
;
2109 *num_arch_defined_types
= tbl_sz
;
2112 if (!strcmp((*arch_defined_types_list
)[j
], type_id
))
2121 static int gdb_generate_reg_type_description(struct target
*target
,
2122 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
,
2123 char const **arch_defined_types_list
[], int *num_arch_defined_types
)
2125 int retval
= ERROR_OK
;
2127 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
2128 struct reg_data_type
*data_type
= type
->reg_type_vector
->type
;
2129 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2130 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2131 num_arch_defined_types
))
2132 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2133 arch_defined_types_list
,
2134 num_arch_defined_types
);
2136 /* <vector id="id" type="type" count="count"/> */
2137 xml_printf(&retval
, tdesc
, pos
, size
,
2138 "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32
"\"/>\n",
2139 type
->id
, type
->reg_type_vector
->type
->id
,
2140 type
->reg_type_vector
->count
);
2142 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
2143 struct reg_data_type_union_field
*field
;
2144 field
= type
->reg_type_union
->fields
;
2146 struct reg_data_type
*data_type
= field
->type
;
2147 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2148 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2149 num_arch_defined_types
))
2150 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2151 arch_defined_types_list
,
2152 num_arch_defined_types
);
2155 field
= field
->next
;
2158 * <field name="name" type="type"/> ...
2160 xml_printf(&retval
, tdesc
, pos
, size
,
2161 "<union id=\"%s\">\n",
2164 field
= type
->reg_type_union
->fields
;
2166 xml_printf(&retval
, tdesc
, pos
, size
,
2167 "<field name=\"%s\" type=\"%s\"/>\n",
2168 field
->name
, field
->type
->id
);
2170 field
= field
->next
;
2173 xml_printf(&retval
, tdesc
, pos
, size
,
2176 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
2177 struct reg_data_type_struct_field
*field
;
2178 field
= type
->reg_type_struct
->fields
;
2180 if (field
->use_bitfields
) {
2181 /* <struct id="id" size="size">
2182 * <field name="name" start="start" end="end"/> ...
2184 xml_printf(&retval
, tdesc
, pos
, size
,
2185 "<struct id=\"%s\" size=\"%" PRIu32
"\">\n",
2186 type
->id
, type
->reg_type_struct
->size
);
2188 xml_printf(&retval
, tdesc
, pos
, size
,
2189 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2190 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2191 gdb_get_reg_type_name(field
->bitfield
->type
));
2193 field
= field
->next
;
2197 struct reg_data_type
*data_type
= field
->type
;
2198 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2199 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2200 num_arch_defined_types
))
2201 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2202 arch_defined_types_list
,
2203 num_arch_defined_types
);
2208 * <field name="name" type="type"/> ...
2210 xml_printf(&retval
, tdesc
, pos
, size
,
2211 "<struct id=\"%s\">\n",
2214 xml_printf(&retval
, tdesc
, pos
, size
,
2215 "<field name=\"%s\" type=\"%s\"/>\n",
2216 field
->name
, field
->type
->id
);
2218 field
= field
->next
;
2222 xml_printf(&retval
, tdesc
, pos
, size
,
2225 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2226 /* <flags id="id" size="size">
2227 * <field name="name" start="start" end="end"/> ...
2229 xml_printf(&retval
, tdesc
, pos
, size
,
2230 "<flags id=\"%s\" size=\"%" PRIu32
"\">\n",
2231 type
->id
, type
->reg_type_flags
->size
);
2233 struct reg_data_type_flags_field
*field
;
2234 field
= type
->reg_type_flags
->fields
;
2236 xml_printf(&retval
, tdesc
, pos
, size
,
2237 "<field name=\"%s\" start=\"%" PRIu32
"\" end=\"%" PRIu32
"\" type=\"%s\" />\n",
2238 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2239 gdb_get_reg_type_name(field
->bitfield
->type
));
2241 field
= field
->next
;
2244 xml_printf(&retval
, tdesc
, pos
, size
,
2252 /* Get a list of available target registers features. feature_list must
2253 * be freed by caller.
2255 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2256 struct reg
**reg_list
, int reg_list_size
)
2260 /* Start with only one element */
2261 *feature_list
= calloc(1, sizeof(char *));
2263 for (int i
= 0; i
< reg_list_size
; i
++) {
2264 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2267 if (reg_list
[i
]->feature
2268 && reg_list
[i
]->feature
->name
2269 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2270 /* We found a feature, check if the feature is already in the
2271 * table. If not, allocate a new entry for the table and
2272 * put the new feature in it.
2274 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2275 if (!((*feature_list
)[j
])) {
2276 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2277 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2278 (*feature_list
)[tbl_sz
] = NULL
;
2281 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2288 if (feature_list_size
)
2289 *feature_list_size
= tbl_sz
;
2294 /* Create a register list that's the union of all the registers of the SMP
2295 * group this target is in. If the target is not part of an SMP group, this
2296 * returns the same as target_get_gdb_reg_list_noread().
2298 static int smp_reg_list_noread(struct target
*target
,
2299 struct reg
**combined_list
[], int *combined_list_size
,
2300 enum target_register_class reg_class
)
2303 return target_get_gdb_reg_list_noread(target
, combined_list
,
2304 combined_list_size
, REG_CLASS_ALL
);
2306 unsigned int combined_allocated
= 256;
2307 struct reg
**local_list
= malloc(combined_allocated
* sizeof(struct reg
*));
2309 LOG_ERROR("malloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2312 unsigned int local_list_size
= 0;
2314 struct target_list
*head
;
2315 foreach_smp_target(head
, target
->smp_targets
) {
2316 if (!target_was_examined(head
->target
))
2319 struct reg
**reg_list
= NULL
;
2321 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2322 ®_list_size
, reg_class
);
2323 if (result
!= ERROR_OK
) {
2327 for (int i
= 0; i
< reg_list_size
; i
++) {
2329 struct reg
*a
= reg_list
[i
];
2331 /* Nested loop makes this O(n^2), but this entire function with
2332 * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2334 for (unsigned int j
= 0; j
< local_list_size
; j
++) {
2335 struct reg
*b
= local_list
[j
];
2336 if (!strcmp(a
->name
, b
->name
)) {
2338 if (a
->size
!= b
->size
) {
2339 LOG_ERROR("SMP register %s is %d bits on one "
2340 "target, but %d bits on another target.",
2341 a
->name
, a
->size
, b
->size
);
2350 LOG_DEBUG("[%s] %s not found in combined list", target_name(target
), a
->name
);
2351 if (local_list_size
>= combined_allocated
) {
2352 combined_allocated
*= 2;
2353 local_list
= realloc(local_list
, combined_allocated
* sizeof(struct reg
*));
2355 LOG_ERROR("realloc(%zu) failed", combined_allocated
* sizeof(struct reg
*));
2360 local_list
[local_list_size
] = a
;
2368 if (local_list_size
== 0) {
2369 LOG_ERROR("Unable to get register list");
2374 /* Now warn the user about any registers that weren't found in every target. */
2375 foreach_smp_target(head
, target
->smp_targets
) {
2376 if (!target_was_examined(head
->target
))
2379 struct reg
**reg_list
= NULL
;
2381 int result
= target_get_gdb_reg_list_noread(head
->target
, ®_list
,
2382 ®_list_size
, reg_class
);
2383 if (result
!= ERROR_OK
) {
2387 for (unsigned int i
= 0; i
< local_list_size
; i
++) {
2389 struct reg
*a
= local_list
[i
];
2390 for (int j
= 0; j
< reg_list_size
; j
++) {
2391 struct reg
*b
= reg_list
[j
];
2392 if (b
->exist
&& !strcmp(a
->name
, b
->name
)) {
2398 LOG_WARNING("Register %s does not exist in %s, which is part of an SMP group where "
2399 "this register does exist.",
2400 a
->name
, target_name(head
->target
));
2406 *combined_list
= local_list
;
2407 *combined_list_size
= local_list_size
;
2411 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2413 int retval
= ERROR_OK
;
2414 struct reg
**reg_list
= NULL
;
2416 char const *architecture
;
2417 char const **features
= NULL
;
2418 int feature_list_size
= 0;
2424 retval
= smp_reg_list_noread(target
, ®_list
, ®_list_size
,
2427 if (retval
!= ERROR_OK
) {
2428 LOG_ERROR("get register list failed");
2429 retval
= ERROR_FAIL
;
2433 if (reg_list_size
<= 0) {
2434 LOG_ERROR("get register list failed");
2435 retval
= ERROR_FAIL
;
2439 /* Get a list of available target registers features */
2440 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2441 if (retval
!= ERROR_OK
) {
2442 LOG_ERROR("Can't get the registers feature list");
2443 retval
= ERROR_FAIL
;
2447 /* If we found some features associated with registers, create sections */
2448 int current_feature
= 0;
2450 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2451 "<?xml version=\"1.0\"?>\n"
2452 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2453 "<target version=\"1.0\">\n");
2455 /* generate architecture element if supported by target */
2456 architecture
= target_get_gdb_arch(target
);
2458 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2459 "<architecture>%s</architecture>\n", architecture
);
2461 /* generate target description according to register list */
2463 while (features
[current_feature
]) {
2464 char const **arch_defined_types
= NULL
;
2465 int num_arch_defined_types
= 0;
2467 arch_defined_types
= calloc(1, sizeof(char *));
2468 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2469 "<feature name=\"%s\">\n",
2470 features
[current_feature
]);
2473 for (i
= 0; i
< reg_list_size
; i
++) {
2475 if (reg_list
[i
]->exist
== false || reg_list
[i
]->hidden
)
2478 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2481 const char *type_str
;
2482 if (reg_list
[i
]->reg_data_type
) {
2483 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2484 /* generate <type... first, if there are architecture-defined types. */
2485 if (lookup_add_arch_defined_types(&arch_defined_types
,
2486 reg_list
[i
]->reg_data_type
->id
,
2487 &num_arch_defined_types
))
2488 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2489 reg_list
[i
]->reg_data_type
,
2490 &arch_defined_types
,
2491 &num_arch_defined_types
);
2493 type_str
= reg_list
[i
]->reg_data_type
->id
;
2495 /* predefined type */
2496 type_str
= gdb_get_reg_type_name(
2497 reg_list
[i
]->reg_data_type
->type
);
2500 /* Default type is "int" */
2504 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2505 "<reg name=\"%s\"", reg_list
[i
]->name
);
2506 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2507 " bitsize=\"%" PRIu32
"\"", reg_list
[i
]->size
);
2508 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2509 " regnum=\"%" PRIu32
"\"", reg_list
[i
]->number
);
2510 if (reg_list
[i
]->caller_save
)
2511 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2512 " save-restore=\"yes\"");
2514 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2515 " save-restore=\"no\"");
2517 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2518 " type=\"%s\"", type_str
);
2520 if (reg_list
[i
]->group
)
2521 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2522 " group=\"%s\"", reg_list
[i
]->group
);
2524 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2528 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2532 free(arch_defined_types
);
2536 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2543 if (retval
== ERROR_OK
)
2551 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2552 char **chunk
, int32_t offset
, uint32_t length
)
2555 LOG_ERROR("Unable to Generate Target Description");
2559 char *tdesc
= target_desc
->tdesc
;
2560 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2563 int retval
= gdb_generate_target_description(target
, &tdesc
);
2564 if (retval
!= ERROR_OK
) {
2565 LOG_ERROR("Unable to Generate Target Description");
2569 tdesc_length
= strlen(tdesc
);
2574 if (length
< (tdesc_length
- offset
))
2575 transfer_type
= 'm';
2577 transfer_type
= 'l';
2579 *chunk
= malloc(length
+ 2);
2581 LOG_ERROR("Unable to allocate memory");
2585 (*chunk
)[0] = transfer_type
;
2586 if (transfer_type
== 'm') {
2587 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2588 (*chunk
)[1 + length
] = '\0';
2590 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2591 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2593 /* After gdb-server sends out last chunk, invalidate tdesc. */
2599 target_desc
->tdesc
= tdesc
;
2600 target_desc
->tdesc_length
= tdesc_length
;
2605 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2607 int retval
= ERROR_OK
;
2608 struct reg
**reg_list
= NULL
;
2609 int reg_list_size
= 0;
2610 char const **features
= NULL
;
2611 int feature_list_size
= 0;
2613 char const *architecture
= target_get_gdb_arch(target
);
2615 retval
= target_get_gdb_reg_list_noread(target
, ®_list
,
2616 ®_list_size
, REG_CLASS_ALL
);
2617 if (retval
!= ERROR_OK
) {
2618 LOG_ERROR("get register list failed");
2622 if (reg_list_size
<= 0) {
2623 LOG_ERROR("get register list failed");
2624 retval
= ERROR_FAIL
;
2628 /* Get a list of available target registers features */
2629 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2630 if (retval
!= ERROR_OK
) {
2631 LOG_ERROR("Can't get the registers feature list");
2636 if (architecture
|| feature_list_size
)
2650 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2652 struct rtos
*rtos
= target
->rtos
;
2653 int retval
= ERROR_OK
;
2654 char *thread_list
= NULL
;
2658 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2659 "<?xml version=\"1.0\"?>\n"
2663 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2664 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2666 if (!thread_detail
->exists
)
2669 if (thread_detail
->thread_name_str
)
2670 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2671 "<thread id=\"%" PRIx64
"\" name=\"%s\">",
2672 thread_detail
->threadid
,
2673 thread_detail
->thread_name_str
);
2675 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2676 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2678 if (thread_detail
->thread_name_str
)
2679 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2680 "Name: %s", thread_detail
->thread_name_str
);
2682 if (thread_detail
->extra_info_str
) {
2683 if (thread_detail
->thread_name_str
)
2684 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2686 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2687 "%s", thread_detail
->extra_info_str
);
2690 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2695 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2698 if (retval
== ERROR_OK
)
2699 *thread_list_out
= thread_list
;
2706 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2707 char **chunk
, int32_t offset
, uint32_t length
)
2709 if (!*thread_list
) {
2710 int retval
= gdb_generate_thread_list(target
, thread_list
);
2711 if (retval
!= ERROR_OK
) {
2712 LOG_ERROR("Unable to Generate Thread List");
2717 size_t thread_list_length
= strlen(*thread_list
);
2720 length
= MIN(length
, thread_list_length
- offset
);
2721 if (length
< (thread_list_length
- offset
))
2722 transfer_type
= 'm';
2724 transfer_type
= 'l';
2726 *chunk
= malloc(length
+ 2 + 3);
2727 /* Allocating extra 3 bytes prevents false positive valgrind report
2728 * of strlen(chunk) word access:
2729 * Invalid read of size 4
2730 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2732 LOG_ERROR("Unable to allocate memory");
2736 (*chunk
)[0] = transfer_type
;
2737 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2738 (*chunk
)[1 + length
] = '\0';
2740 /* After gdb-server sends out last chunk, invalidate thread list. */
2741 if (transfer_type
== 'l') {
2743 *thread_list
= NULL
;
2749 static int gdb_query_packet(struct connection
*connection
,
2750 char const *packet
, int packet_size
)
2752 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2753 struct gdb_connection
*gdb_connection
= connection
->priv
;
2754 struct target
*target
= get_target_from_connection(connection
);
2756 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2757 if (packet_size
> 6) {
2758 Jim_Interp
*interp
= cmd_ctx
->interp
;
2760 cmd
= malloc((packet_size
- 6) / 2 + 1);
2761 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2764 /* We want to print all debug output to GDB connection */
2765 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
2766 target_call_timer_callbacks_now();
2767 /* some commands need to know the GDB connection, make note of current
2768 * GDB connection. */
2769 current_gdb_connection
= gdb_connection
;
2771 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
2772 cmd_ctx
->current_target_override
= NULL
;
2774 struct command_context
*old_context
= Jim_GetAssocData(interp
, "context");
2775 Jim_DeleteAssocData(interp
, "context");
2776 int retval
= Jim_SetAssocData(interp
, "context", NULL
, cmd_ctx
);
2777 if (retval
== JIM_OK
) {
2778 retval
= Jim_EvalObj(interp
, Jim_NewStringObj(interp
, cmd
, -1));
2779 Jim_DeleteAssocData(interp
, "context");
2781 int inner_retval
= Jim_SetAssocData(interp
, "context", NULL
, old_context
);
2782 if (retval
== JIM_OK
)
2783 retval
= inner_retval
;
2785 cmd_ctx
->current_target_override
= saved_target_override
;
2787 current_gdb_connection
= NULL
;
2788 target_call_timer_callbacks_now();
2789 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
2791 if (retval
== JIM_RETURN
)
2792 retval
= interp
->returnCode
;
2794 const char *cretmsg
= Jim_GetString(Jim_GetResult(interp
), &lenmsg
);
2796 if (lenmsg
&& cretmsg
[lenmsg
- 1] != '\n') {
2797 retmsg
= alloc_printf("%s\n", cretmsg
);
2800 retmsg
= strdup(cretmsg
);
2803 return ERROR_GDB_BUFFER_TOO_SMALL
;
2805 if (retval
== JIM_OK
) {
2807 char *hex_buffer
= malloc(lenmsg
* 2 + 1);
2810 return ERROR_GDB_BUFFER_TOO_SMALL
;
2813 size_t pkt_len
= hexify(hex_buffer
, (const uint8_t *)retmsg
, lenmsg
,
2815 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
2818 gdb_put_packet(connection
, "OK", 2);
2822 gdb_output_con(connection
, retmsg
);
2823 gdb_send_error(connection
, retval
);
2828 gdb_put_packet(connection
, "OK", 2);
2830 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2831 if (packet_size
> 5) {
2836 target_addr_t addr
= 0;
2839 /* skip command character */
2842 addr
= strtoull(packet
, &separator
, 16);
2844 if (*separator
!= ',') {
2845 LOG_ERROR("incomplete read memory packet received, dropping connection");
2846 return ERROR_SERVER_REMOTE_CLOSED
;
2849 len
= strtoul(separator
+ 1, NULL
, 16);
2851 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2853 if (retval
== ERROR_OK
) {
2854 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2855 gdb_put_packet(connection
, gdb_reply
, 9);
2857 retval
= gdb_error(connection
, retval
);
2858 if (retval
!= ERROR_OK
)
2864 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2865 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2866 * qXfer:features:read is supported for some targets */
2867 int retval
= ERROR_OK
;
2868 char *buffer
= NULL
;
2871 int gdb_target_desc_supported
= 0;
2873 /* we need to test that the target supports target descriptions */
2874 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2875 if (retval
!= ERROR_OK
) {
2876 LOG_INFO("Failed detecting Target Description Support, disabling");
2877 gdb_target_desc_supported
= 0;
2880 /* support may be disabled globally */
2881 if (gdb_use_target_description
== 0) {
2882 if (gdb_target_desc_supported
)
2883 LOG_WARNING("Target Descriptions Supported, but disabled");
2884 gdb_target_desc_supported
= 0;
2891 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2893 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2894 (gdb_target_desc_supported
== 1) ? '+' : '-');
2896 if (retval
!= ERROR_OK
) {
2897 gdb_send_error(connection
, 01);
2901 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2905 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2906 && (flash_get_bank_count() > 0))
2907 return gdb_memory_map(connection
, packet
, packet_size
);
2908 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2910 int retval
= ERROR_OK
;
2913 unsigned int length
;
2915 /* skip command character */
2918 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2919 gdb_send_error(connection
, 01);
2923 /* Target should prepare correct target description for annex.
2924 * The first character of returned xml is 'm' or 'l'. 'm' for
2925 * there are *more* chunks to transfer. 'l' for it is the *last*
2926 * chunk of target description.
2928 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2929 &xml
, offset
, length
);
2930 if (retval
!= ERROR_OK
) {
2931 gdb_error(connection
, retval
);
2935 gdb_put_packet(connection
, xml
, strlen(xml
));
2939 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2941 int retval
= ERROR_OK
;
2944 unsigned int length
;
2946 /* skip command character */
2949 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2950 gdb_send_error(connection
, 01);
2954 /* Target should prepare correct thread list for annex.
2955 * The first character of returned xml is 'm' or 'l'. 'm' for
2956 * there are *more* chunks to transfer. 'l' for it is the *last*
2957 * chunk of target description.
2959 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2960 &xml
, offset
, length
);
2961 if (retval
!= ERROR_OK
) {
2962 gdb_error(connection
, retval
);
2966 gdb_put_packet(connection
, xml
, strlen(xml
));
2970 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2971 gdb_connection
->noack_mode
= 1;
2972 gdb_put_packet(connection
, "OK", 2);
2974 } else if (target
->type
->gdb_query_custom
) {
2975 char *buffer
= NULL
;
2976 int ret
= target
->type
->gdb_query_custom(target
, packet
, &buffer
);
2977 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2981 gdb_put_packet(connection
, "", 0);
2985 static bool gdb_handle_vcont_packet(struct connection
*connection
, const char *packet
,
2986 __attribute__((unused
)) int packet_size
)
2988 struct gdb_connection
*gdb_connection
= connection
->priv
;
2989 struct target
*target
= get_target_from_connection(connection
);
2990 const char *parse
= packet
;
2993 /* query for vCont supported */
2994 if (parse
[0] == '?') {
2995 if (target
->type
->step
) {
2996 /* gdb doesn't accept c without C and s without S */
2997 gdb_put_packet(connection
, "vCont;c;C;s;S", 13);
3003 if (parse
[0] == ';') {
3007 /* simple case, a continue packet */
3008 if (parse
[0] == 'c') {
3009 gdb_running_type
= 'c';
3010 LOG_DEBUG("target %s continue", target_name(target
));
3011 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3012 retval
= target_resume(target
, 1, 0, 0, 0);
3013 if (retval
== ERROR_TARGET_NOT_HALTED
)
3014 LOG_INFO("target %s was not halted when resume was requested", target_name(target
));
3016 /* poll target in an attempt to make its internal state consistent */
3017 if (retval
!= ERROR_OK
) {
3018 retval
= target_poll(target
);
3019 if (retval
!= ERROR_OK
)
3020 LOG_DEBUG("error polling target %s after failed resume", target_name(target
));
3024 * We don't report errors to gdb here, move frontend_state to
3025 * TARGET_RUNNING to stay in sync with gdb's expectation of the
3028 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3029 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3034 /* single-step or step-over-breakpoint */
3035 if (parse
[0] == 's') {
3036 gdb_running_type
= 's';
3037 bool fake_step
= false;
3039 struct target
*ct
= target
;
3043 if (parse
[0] == ':') {
3046 thread_id
= strtoll(parse
, &endp
, 16);
3055 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
3056 rtos_update_threads(target
);
3058 target
->rtos
->gdb_target_for_threadid(connection
, thread_id
, &ct
);
3061 * check if the thread to be stepped is the current rtos thread
3062 * if not, we must fake the step
3064 if (target
->rtos
->current_thread
!= thread_id
)
3068 if (parse
[0] == ';') {
3071 if (parse
[0] == 'c') {
3074 /* check if thread-id follows */
3075 if (parse
[0] == ':') {
3079 tid
= strtoll(parse
, NULL
, 16);
3080 if (tid
== thread_id
) {
3082 * Special case: only step a single thread (core),
3083 * keep the other threads halted. Currently, only
3084 * aarch64 target understands it. Other target types don't
3085 * care (nobody checks the actual value of 'current')
3086 * and it doesn't really matter. This deserves
3087 * a symbolic constant and a formal interface documentation
3090 LOG_DEBUG("request to step current core only");
3091 /* uncomment after checking that indeed other targets are safe */
3098 LOG_DEBUG("target %s single-step thread %"PRIx64
, target_name(ct
), thread_id
);
3099 gdb_connection
->output_flag
= GDB_OUTPUT_ALL
;
3100 target_call_event_callbacks(ct
, TARGET_EVENT_GDB_START
);
3103 * work around an annoying gdb behaviour: when the current thread
3104 * is changed in gdb, it assumes that the target can follow and also
3105 * make the thread current. This is an assumption that cannot hold
3106 * for a real target running a multi-threading OS. We just fake
3107 * the step to not trigger an internal error in gdb. See
3108 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3112 char sig_reply
[128];
3114 LOG_DEBUG("fake step thread %"PRIx64
, thread_id
);
3116 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
),
3117 "T05thread:%016"PRIx64
";", thread_id
);
3119 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
3120 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3125 /* support for gdb_sync command */
3126 if (gdb_connection
->sync
) {
3127 gdb_connection
->sync
= false;
3128 if (ct
->state
== TARGET_HALTED
) {
3129 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3130 "from the target.");
3131 gdb_sig_halted(connection
);
3132 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3134 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3138 retval
= target_step(ct
, current_pc
, 0, 0);
3139 if (retval
== ERROR_TARGET_NOT_HALTED
)
3140 LOG_INFO("target %s was not halted when step was requested", target_name(ct
));
3142 /* if step was successful send a reply back to gdb */
3143 if (retval
== ERROR_OK
) {
3144 retval
= target_poll(ct
);
3145 if (retval
!= ERROR_OK
)
3146 LOG_DEBUG("error polling target %s after successful step", target_name(ct
));
3147 /* send back signal information */
3148 gdb_signal_reply(ct
, connection
);
3149 /* stop forwarding log packets! */
3150 gdb_connection
->output_flag
= GDB_OUTPUT_NO
;
3152 gdb_connection
->frontend_state
= TARGET_RUNNING
;
3155 LOG_ERROR("Unknown vCont packet");
3159 static char *next_hex_encoded_field(const char **str
, char sep
)
3162 const char *hex
= *str
;
3166 const char *end
= strchr(hex
, sep
);
3168 hexlen
= strlen(hex
);
3171 *str
= hex
+ hexlen
+ 1;
3173 if (hexlen
% 2 != 0) {
3174 /* Malformed hex data */
3178 size_t count
= hexlen
/ 2;
3179 char *decoded
= malloc(count
+ 1);
3183 size_t converted
= unhexify((void *)decoded
, hex
, count
);
3184 if (converted
!= count
) {
3189 decoded
[count
] = '\0';
3193 /* handle extended restart packet */
3194 static void gdb_restart_inferior(struct connection
*connection
, const char *packet
, int packet_size
)
3196 struct gdb_connection
*gdb_con
= connection
->priv
;
3197 struct target
*target
= get_target_from_connection(connection
);
3199 breakpoint_clear_target(target
);
3200 watchpoint_clear_target(target
);
3201 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
3202 target_name(target
));
3203 /* set connection as attached after reset */
3204 gdb_con
->attached
= true;
3205 /* info rtos parts */
3206 gdb_thread_packet(connection
, packet
, packet_size
);
3209 static bool gdb_handle_vrun_packet(struct connection
*connection
, const char *packet
, int packet_size
)
3211 struct target
*target
= get_target_from_connection(connection
);
3212 const char *parse
= packet
;
3217 if (parse
[0] != ';')
3221 /* Skip first field "filename"; don't know what to do with it. */
3222 free(next_hex_encoded_field(&parse
, ';'));
3224 char *cmdline
= next_hex_encoded_field(&parse
, ';');
3226 char *arg
= next_hex_encoded_field(&parse
, ';');
3229 char *new_cmdline
= alloc_printf("%s %s", cmdline
, arg
);
3232 cmdline
= new_cmdline
;
3236 if (target
->semihosting
) {
3237 LOG_INFO("GDB set inferior command line to '%s'", cmdline
);
3238 free(target
->semihosting
->cmdline
);
3239 target
->semihosting
->cmdline
= cmdline
;
3241 LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline
);
3246 gdb_restart_inferior(connection
, packet
, packet_size
);
3247 gdb_put_packet(connection
, "S00", 3);
3251 static int gdb_v_packet(struct connection
*connection
,
3252 char const *packet
, int packet_size
)
3254 struct gdb_connection
*gdb_connection
= connection
->priv
;
3257 struct target
*target
= get_target_from_connection(connection
);
3259 if (strncmp(packet
, "vCont", 5) == 0) {
3265 handled
= gdb_handle_vcont_packet(connection
, packet
, packet_size
);
3267 gdb_put_packet(connection
, "", 0);
3272 if (strncmp(packet
, "vRun", 4) == 0) {
3275 handled
= gdb_handle_vrun_packet(connection
, packet
, packet_size
);
3277 gdb_put_packet(connection
, "", 0);
3282 /* if flash programming disabled - send a empty reply */
3284 if (gdb_flash_program
== 0) {
3285 gdb_put_packet(connection
, "", 0);
3289 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
3291 unsigned long length
;
3293 char const *parse
= packet
+ 12;
3294 if (*parse
== '\0') {
3295 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3296 return ERROR_SERVER_REMOTE_CLOSED
;
3299 addr
= strtoul(parse
, (char **)&parse
, 16);
3301 if (*(parse
++) != ',' || *parse
== '\0') {
3302 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3303 return ERROR_SERVER_REMOTE_CLOSED
;
3306 length
= strtoul(parse
, (char **)&parse
, 16);
3308 if (*parse
!= '\0') {
3309 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3310 return ERROR_SERVER_REMOTE_CLOSED
;
3313 /* assume all sectors need erasing - stops any problems
3314 * when flash_write is called multiple times */
3317 /* perform any target specific operations before the erase */
3318 target_call_event_callbacks(target
,
3319 TARGET_EVENT_GDB_FLASH_ERASE_START
);
3321 /* vFlashErase:addr,length messages require region start and
3322 * end to be "block" aligned ... if padding is ever needed,
3323 * GDB will have become dangerously confused.
3325 result
= flash_erase_address_range(target
, false, addr
,
3328 /* perform any target specific operations after the erase */
3329 target_call_event_callbacks(target
,
3330 TARGET_EVENT_GDB_FLASH_ERASE_END
);
3333 if (result
!= ERROR_OK
) {
3334 /* GDB doesn't evaluate the actual error number returned,
3335 * treat a failed erase as an I/O error
3337 gdb_send_error(connection
, EIO
);
3338 LOG_ERROR("flash_erase returned %i", result
);
3340 gdb_put_packet(connection
, "OK", 2);
3345 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
3348 unsigned long length
;
3349 char const *parse
= packet
+ 12;
3351 if (*parse
== '\0') {
3352 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3353 return ERROR_SERVER_REMOTE_CLOSED
;
3355 addr
= strtoul(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 /* process the flashing buffer. No need to erase as GDB
3383 * always issues a vFlashErase first. */
3384 target_call_event_callbacks(target
,
3385 TARGET_EVENT_GDB_FLASH_WRITE_START
);
3386 result
= flash_write(target
, gdb_connection
->vflash_image
,
3388 target_call_event_callbacks(target
,
3389 TARGET_EVENT_GDB_FLASH_WRITE_END
);
3390 if (result
!= ERROR_OK
) {
3391 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
3392 gdb_put_packet(connection
, "E.memtype", 9);
3394 gdb_send_error(connection
, EIO
);
3396 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
3397 gdb_put_packet(connection
, "OK", 2);
3400 image_close(gdb_connection
->vflash_image
);
3401 free(gdb_connection
->vflash_image
);
3402 gdb_connection
->vflash_image
= NULL
;
3407 gdb_put_packet(connection
, "", 0);
3411 static int gdb_detach(struct connection
*connection
)
3414 * Only reply "OK" to GDB
3415 * it will close the connection and this will trigger a call to
3416 * gdb_connection_closed() that will in turn trigger the event
3417 * TARGET_EVENT_GDB_DETACH
3419 return gdb_put_packet(connection
, "OK", 2);
3422 /* The format of 'F' response packet is
3423 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3425 static int gdb_fileio_response_packet(struct connection
*connection
,
3426 char const *packet
, int packet_size
)
3428 struct target
*target
= get_target_from_connection(connection
);
3430 char *parsing_point
;
3431 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
3432 int fileio_errno
= 0;
3433 bool fileio_ctrl_c
= false;
3438 if (*separator
== ',') {
3439 parsing_point
= separator
+ 1;
3440 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
3441 if (*separator
== ',') {
3442 if (*(separator
+ 1) == 'C') {
3443 /* TODO: process ctrl-c */
3444 fileio_ctrl_c
= true;
3449 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3450 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
3452 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
3453 if (retval
!= ERROR_OK
)
3456 /* After File-I/O ends, keep continue or step */
3457 if (gdb_running_type
== 'c')
3458 retval
= target_resume(target
, 1, 0x0, 0, 0);
3459 else if (gdb_running_type
== 's')
3460 retval
= target_step(target
, 1, 0x0, 0);
3462 retval
= ERROR_FAIL
;
3464 if (retval
!= ERROR_OK
)
3470 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
3471 const char *function
, const char *string
)
3473 struct connection
*connection
= priv
;
3474 struct gdb_connection
*gdb_con
= connection
->priv
;
3476 if (gdb_con
->output_flag
== GDB_OUTPUT_NO
)
3477 /* No out allowed */
3480 if (gdb_con
->busy
) {
3481 /* do not reply this using the O packet */
3485 gdb_output_con(connection
, string
);
3488 static void gdb_sig_halted(struct connection
*connection
)
3491 snprintf(sig_reply
, 4, "T%2.2x", 2);
3492 gdb_put_packet(connection
, sig_reply
, 3);
3495 static int gdb_input_inner(struct connection
*connection
)
3497 /* Do not allocate this on the stack */
3498 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for null-termination */
3500 struct target
*target
;
3501 char const *packet
= gdb_packet_buffer
;
3504 struct gdb_connection
*gdb_con
= connection
->priv
;
3505 static bool warn_use_ext
;
3507 target
= get_target_from_connection(connection
);
3509 /* drain input buffer. If one of the packets fail, then an error
3510 * packet is replied, if applicable.
3512 * This loop will terminate and the error code is returned.
3514 * The calling fn will check if this error is something that
3515 * can be recovered from, or if the connection must be closed.
3517 * If the error is recoverable, this fn is called again to
3518 * drain the rest of the buffer.
3521 packet_size
= GDB_BUFFER_SIZE
;
3522 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
3523 if (retval
!= ERROR_OK
)
3526 /* terminate with zero */
3527 gdb_packet_buffer
[packet_size
] = '\0';
3529 if (packet_size
> 0) {
3531 gdb_log_incoming_packet(connection
, gdb_packet_buffer
);
3534 switch (packet
[0]) {
3535 case 'T': /* Is thread alive? */
3536 gdb_thread_packet(connection
, packet
, packet_size
);
3538 case 'H': /* Set current thread ( 'c' for step and continue,
3539 * 'g' for all other operations ) */
3540 gdb_thread_packet(connection
, packet
, packet_size
);
3544 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
3545 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
3546 retval
= gdb_query_packet(connection
, packet
, packet_size
);
3549 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
3552 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
3555 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
3558 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
3561 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
3564 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
3568 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
3571 gdb_last_signal_packet(connection
, packet
, packet_size
);
3572 /* '?' is sent after the eventual '!' */
3573 if (!warn_use_ext
&& !gdb_con
->extended_protocol
) {
3574 warn_use_ext
= true;
3575 LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3576 connection
->service
->port
, connection
->service
->port
);
3582 gdb_thread_packet(connection
, packet
, packet_size
);
3583 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3585 if (gdb_con
->mem_write_error
) {
3586 LOG_ERROR("Memory write failure!");
3588 /* now that we have reported the memory write error,
3589 * we can clear the condition */
3590 gdb_con
->mem_write_error
= false;
3593 bool nostep
= false;
3594 bool already_running
= false;
3595 if (target
->state
== TARGET_RUNNING
) {
3596 LOG_WARNING("WARNING! The target is already running. "
3597 "All changes GDB did to registers will be discarded! "
3598 "Waiting for target to halt.");
3599 already_running
= true;
3600 } else if (target
->state
!= TARGET_HALTED
) {
3601 LOG_WARNING("The target is not in the halted nor running stated, "
3602 "stepi/continue ignored.");
3604 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
3605 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3606 * sent by GDB first to OpenOCD, thus defeating the check to
3607 * make only the single stepping have the sync feature...
3610 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3611 "from the target.");
3613 gdb_con
->sync
= false;
3615 if (!already_running
&& nostep
) {
3616 /* Either the target isn't in the halted state, then we can't
3617 * step/continue. This might be early setup, etc.
3619 * Or we want to allow GDB to pick up a fresh set of
3620 * register values without modifying the target state.
3623 gdb_sig_halted(connection
);
3625 /* stop forwarding log packets! */
3626 gdb_con
->output_flag
= GDB_OUTPUT_NO
;
3628 /* We're running/stepping, in which case we can
3629 * forward log output until the target is halted
3631 gdb_con
->frontend_state
= TARGET_RUNNING
;
3632 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3634 if (!already_running
) {
3635 /* Here we don't want packet processing to stop even if this fails,
3636 * so we use a local variable instead of retval. */
3637 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
3638 if (retval
!= ERROR_OK
) {
3639 /* we'll never receive a halted
3640 * condition... issue a false one..
3642 gdb_frontend_halted(target
, connection
);
3649 retval
= gdb_v_packet(connection
, packet
, packet_size
);
3652 retval
= gdb_detach(connection
);
3655 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
3656 if (retval
!= ERROR_OK
)
3660 if (gdb_con
->extended_protocol
) {
3661 gdb_con
->attached
= false;
3664 gdb_put_packet(connection
, "OK", 2);
3665 return ERROR_SERVER_REMOTE_CLOSED
;
3667 /* handle extended remote protocol */
3668 gdb_con
->extended_protocol
= true;
3669 gdb_put_packet(connection
, "OK", 2);
3672 /* handle extended restart packet */
3673 gdb_restart_inferior(connection
, packet
, packet_size
);
3678 /* packet supported only by smp target i.e cortex_a.c*/
3679 /* handle smp packet replying coreid played to gbd */
3680 gdb_read_smp_packet(connection
, packet
, packet_size
);
3685 /* packet supported only by smp target i.e cortex_a.c */
3686 /* handle smp packet setting coreid to be played at next
3688 gdb_write_smp_packet(connection
, packet
, packet_size
);
3692 /* File-I/O extension */
3693 /* After gdb uses host-side syscall to complete target file
3694 * I/O, gdb sends host-side syscall return value to target
3696 * The format of 'F' response packet is
3697 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3699 gdb_con
->frontend_state
= TARGET_RUNNING
;
3700 gdb_con
->output_flag
= GDB_OUTPUT_ALL
;
3701 gdb_fileio_response_packet(connection
, packet
, packet_size
);
3705 /* ignore unknown packets */
3706 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
3707 gdb_put_packet(connection
, "", 0);
3711 /* if a packet handler returned an error, exit input loop */
3712 if (retval
!= ERROR_OK
)
3716 if (gdb_con
->ctrl_c
) {
3717 if (target
->state
== TARGET_RUNNING
) {
3718 struct target
*t
= target
;
3720 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &t
);
3721 retval
= target_halt(t
);
3722 if (retval
== ERROR_OK
)
3723 retval
= target_poll(t
);
3724 if (retval
!= ERROR_OK
)
3725 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3726 gdb_con
->ctrl_c
= false;
3728 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3729 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3733 } while (gdb_con
->buf_cnt
> 0);
3738 static int gdb_input(struct connection
*connection
)
3740 int retval
= gdb_input_inner(connection
);
3741 struct gdb_connection
*gdb_con
= connection
->priv
;
3742 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3745 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3746 if (gdb_con
->closed
)
3747 return ERROR_SERVER_REMOTE_CLOSED
;
3749 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3753 static void gdb_keep_client_alive(struct connection
*connection
)
3755 struct gdb_connection
*gdb_con
= connection
->priv
;
3757 if (gdb_con
->busy
) {
3758 /* do not send packets, retry asap */
3762 switch (gdb_con
->output_flag
) {
3764 /* no need for keep-alive */
3766 case GDB_OUTPUT_ALL
:
3767 /* send an empty O packet */
3768 gdb_output_con(connection
, "");
3775 static const struct service_driver gdb_service_driver
= {
3777 .new_connection_during_keep_alive_handler
= NULL
,
3778 .new_connection_handler
= gdb_new_connection
,
3779 .input_handler
= gdb_input
,
3780 .connection_closed_handler
= gdb_connection_closed
,
3781 .keep_client_alive_handler
= gdb_keep_client_alive
,
3784 static int gdb_target_start(struct target
*target
, const char *port
)
3786 struct gdb_service
*gdb_service
;
3788 gdb_service
= malloc(sizeof(struct gdb_service
));
3793 LOG_INFO("starting gdb server for %s on %s", target_name(target
), port
);
3795 gdb_service
->target
= target
;
3796 gdb_service
->core
[0] = -1;
3797 gdb_service
->core
[1] = -1;
3798 target
->gdb_service
= gdb_service
;
3800 ret
= add_service(&gdb_service_driver
, port
, target
->gdb_max_connections
, gdb_service
);
3801 /* initialize all targets gdb service with the same pointer */
3803 struct target_list
*head
;
3804 foreach_smp_target(head
, target
->smp_targets
) {
3805 struct target
*curr
= head
->target
;
3807 curr
->gdb_service
= gdb_service
;
3813 static int gdb_target_add_one(struct target
*target
)
3815 /* one gdb instance per smp list */
3816 if ((target
->smp
) && (target
->gdb_service
))
3819 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3820 if (!target_supports_gdb_connection(target
)) {
3821 LOG_DEBUG("skip gdb server for target %s", target_name(target
));
3825 if (target
->gdb_port_override
) {
3826 if (strcmp(target
->gdb_port_override
, "disabled") == 0) {
3827 LOG_INFO("gdb port disabled");
3830 return gdb_target_start(target
, target
->gdb_port_override
);
3833 if (strcmp(gdb_port
, "disabled") == 0) {
3834 LOG_INFO("gdb port disabled");
3838 int retval
= gdb_target_start(target
, gdb_port_next
);
3839 if (retval
== ERROR_OK
) {
3840 /* save the port number so can be queried with
3841 * $target_name cget -gdb-port
3843 target
->gdb_port_override
= strdup(gdb_port_next
);
3846 /* If we can parse the port number
3847 * then we increment the port number for the next target.
3850 portnumber
= strtol(gdb_port_next
, &end
, 0);
3852 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3853 free(gdb_port_next
);
3855 gdb_port_next
= alloc_printf("%ld", portnumber
+1);
3857 /* Don't increment if gdb_port is 0, since we're just
3858 * trying to allocate an unused port. */
3859 gdb_port_next
= strdup("0");
3867 int gdb_target_add_all(struct target
*target
)
3870 LOG_WARNING("gdb services need one or more targets defined");
3875 int retval
= gdb_target_add_one(target
);
3876 if (retval
!= ERROR_OK
)
3879 target
= target
->next
;
3885 COMMAND_HANDLER(handle_gdb_sync_command
)
3888 return ERROR_COMMAND_SYNTAX_ERROR
;
3890 if (!current_gdb_connection
) {
3892 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3896 current_gdb_connection
->sync
= true;
3901 /* daemon configuration command gdb_port */
3902 COMMAND_HANDLER(handle_gdb_port_command
)
3904 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3905 if (retval
== ERROR_OK
) {
3906 free(gdb_port_next
);
3907 gdb_port_next
= strdup(gdb_port
);
3912 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3915 return ERROR_COMMAND_SYNTAX_ERROR
;
3917 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3921 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3924 return ERROR_COMMAND_SYNTAX_ERROR
;
3926 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3930 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3933 return ERROR_COMMAND_SYNTAX_ERROR
;
3935 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3939 COMMAND_HANDLER(handle_gdb_report_register_access_error
)
3942 return ERROR_COMMAND_SYNTAX_ERROR
;
3944 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_register_access_error
);
3948 /* gdb_breakpoint_override */
3949 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3951 if (CMD_ARGC
== 0) {
3953 } else if (CMD_ARGC
== 1) {
3954 gdb_breakpoint_override
= 1;
3955 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3956 gdb_breakpoint_override_type
= BKPT_HARD
;
3957 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3958 gdb_breakpoint_override_type
= BKPT_SOFT
;
3959 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3960 gdb_breakpoint_override
= 0;
3962 return ERROR_COMMAND_SYNTAX_ERROR
;
3963 if (gdb_breakpoint_override
)
3964 LOG_USER("force %s breakpoints",
3965 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3967 LOG_USER("breakpoint type is not overridden");
3972 COMMAND_HANDLER(handle_gdb_target_description_command
)
3975 return ERROR_COMMAND_SYNTAX_ERROR
;
3977 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3981 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3984 uint32_t tdesc_length
;
3985 struct target
*target
= get_current_target(CMD_CTX
);
3987 int retval
= gdb_generate_target_description(target
, &tdesc
);
3988 if (retval
!= ERROR_OK
) {
3989 LOG_ERROR("Unable to Generate Target Description");
3993 tdesc_length
= strlen(tdesc
);
3995 struct fileio
*fileio
;
3996 size_t size_written
;
3998 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3999 if (!tdesc_filename
) {
4000 retval
= ERROR_FAIL
;
4004 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
4006 if (retval
!= ERROR_OK
) {
4007 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
4011 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
4013 fileio_close(fileio
);
4015 if (retval
!= ERROR_OK
)
4016 LOG_ERROR("Error while writing the tdesc file");
4019 free(tdesc_filename
);
4025 static const struct command_registration gdb_command_handlers
[] = {
4028 .handler
= handle_gdb_sync_command
,
4029 .mode
= COMMAND_ANY
,
4030 .help
= "next stepi will return immediately allowing "
4031 "GDB to fetch register state without affecting "
4037 .handler
= handle_gdb_port_command
,
4038 .mode
= COMMAND_CONFIG
,
4039 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4040 "server listens for the next port number after the "
4041 "base port number specified. "
4042 "No arguments reports GDB port. \"pipe\" means listen to stdin "
4043 "output to stdout, an integer is base port number, \"disabled\" disables "
4044 "port. Any other string is are interpreted as named pipe to listen to. "
4045 "Output pipe is the same name as input pipe, but with 'o' appended.",
4046 .usage
= "[port_num]",
4049 .name
= "gdb_memory_map",
4050 .handler
= handle_gdb_memory_map_command
,
4051 .mode
= COMMAND_CONFIG
,
4052 .help
= "enable or disable memory map",
4053 .usage
= "('enable'|'disable')"
4056 .name
= "gdb_flash_program",
4057 .handler
= handle_gdb_flash_program_command
,
4058 .mode
= COMMAND_CONFIG
,
4059 .help
= "enable or disable flash program",
4060 .usage
= "('enable'|'disable')"
4063 .name
= "gdb_report_data_abort",
4064 .handler
= handle_gdb_report_data_abort_command
,
4065 .mode
= COMMAND_CONFIG
,
4066 .help
= "enable or disable reporting data aborts",
4067 .usage
= "('enable'|'disable')"
4070 .name
= "gdb_report_register_access_error",
4071 .handler
= handle_gdb_report_register_access_error
,
4072 .mode
= COMMAND_CONFIG
,
4073 .help
= "enable or disable reporting register access errors",
4074 .usage
= "('enable'|'disable')"
4077 .name
= "gdb_breakpoint_override",
4078 .handler
= handle_gdb_breakpoint_override_command
,
4079 .mode
= COMMAND_ANY
,
4080 .help
= "Display or specify type of breakpoint "
4081 "to be used by gdb 'break' commands.",
4082 .usage
= "('hard'|'soft'|'disable')"
4085 .name
= "gdb_target_description",
4086 .handler
= handle_gdb_target_description_command
,
4087 .mode
= COMMAND_CONFIG
,
4088 .help
= "enable or disable target description",
4089 .usage
= "('enable'|'disable')"
4092 .name
= "gdb_save_tdesc",
4093 .handler
= handle_gdb_save_tdesc_command
,
4094 .mode
= COMMAND_EXEC
,
4095 .help
= "Save the target description file",
4098 COMMAND_REGISTRATION_DONE
4101 int gdb_register_commands(struct command_context
*cmd_ctx
)
4103 gdb_port
= strdup("3333");
4104 gdb_port_next
= strdup("3333");
4105 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);
4108 void gdb_service_free(void)
4111 free(gdb_port_next
);
4114 int gdb_get_actual_connections(void)
4116 return gdb_actual_connections
;