1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * Copyright (C) 2011 by Broadcom Corporation *
12 * Evan Hunter - ehunter@broadcom.com *
14 * Copyright (C) ST-Ericsson SA 2011 *
15 * michel.jaouen@stericsson.com : smp minimum support *
17 * Copyright (C) 2013 Andes Technology *
18 * Hsiangkai Wang <hkwang@andestech.com> *
20 * Copyright (C) 2013 Franck Jullien *
21 * elec4fun@gmail.com *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
35 ***************************************************************************/
41 #include <target/breakpoints.h>
42 #include <target/target_request.h>
43 #include <target/register.h>
44 #include <target/target.h>
45 #include <target/target_type.h>
46 #include <target/semihosting_common.h>
48 #include <flash/nor/core.h>
49 #include "gdb_server.h"
50 #include <target/image.h>
51 #include <jtag/jtag.h>
52 #include "rtos/rtos.h"
53 #include "target/smp.h"
57 * GDB server implementation.
59 * This implements the GDB Remote Serial Protocol, over TCP connections,
60 * giving GDB access to the JTAG or other hardware debugging facilities
61 * found in most modern embedded processors.
64 struct target_desc_format
{
66 uint32_t tdesc_length
;
69 /* private connection data for GDB */
70 struct gdb_connection
{
71 char buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for nul-termination */
75 enum target_state frontend_state
;
76 struct image
*vflash_image
;
80 /* set flag to true if you want the next stepi to return immediately.
81 * allowing GDB to pick up a fresh set of register values from the target
82 * without modifying the target state. */
84 /* We delay reporting memory write errors until next step/continue or memory
85 * write. This improves performance of gdb load significantly as the GDB packet
86 * can be replied immediately and a new GDB packet will be ready without delay
87 * (ca. 10% or so...). */
89 /* with extended-remote it seems we need to better emulate attach/detach.
90 * what this means is we reply with a W stop reply after a kill packet,
91 * normally we reply with a S reply via gdb_last_signal_packet.
92 * as a side note this behaviour only effects gdb > 6.8 */
94 /* temporarily used for target description support */
95 struct target_desc_format target_desc
;
96 /* temporarily used for thread list support */
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 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 switch (target
->debug_reason
) {
149 case DBG_REASON_DBGRQ
:
150 return 0x2; /* SIGINT */
151 case DBG_REASON_BREAKPOINT
:
152 case DBG_REASON_WATCHPOINT
:
153 case DBG_REASON_WPTANDBKPT
:
154 return 0x05; /* SIGTRAP */
155 case DBG_REASON_SINGLESTEP
:
156 return 0x05; /* SIGTRAP */
157 case DBG_REASON_EXC_CATCH
:
159 case DBG_REASON_NOTHALTED
:
160 return 0x0; /* no signal... shouldn't happen */
162 LOG_USER("undefined debug reason %d - target needs reset",
163 target
->debug_reason
);
168 static int check_pending(struct connection
*connection
,
169 int timeout_s
, int *got_data
)
171 /* a non-blocking socket will block if there is 0 bytes available on the socket,
172 * but return with as many bytes as are available immediately
176 struct gdb_connection
*gdb_con
= connection
->priv
;
178 if (got_data
== NULL
)
182 if (gdb_con
->buf_cnt
> 0) {
188 FD_SET(connection
->fd
, &read_fds
);
190 tv
.tv_sec
= timeout_s
;
192 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
193 /* This can typically be because a "monitor" command took too long
194 * before printing any progress messages
197 return ERROR_GDB_TIMEOUT
;
201 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
205 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
207 struct gdb_connection
*gdb_con
= connection
->priv
;
208 int retval
= ERROR_OK
;
210 #ifdef _DEBUG_GDB_IO_
214 if (connection
->service
->type
!= CONNECTION_TCP
)
215 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
217 retval
= check_pending(connection
, 1, NULL
);
218 if (retval
!= ERROR_OK
)
220 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
225 if (gdb_con
->buf_cnt
> 0)
227 if (gdb_con
->buf_cnt
== 0) {
228 gdb_con
->closed
= true;
229 return ERROR_SERVER_REMOTE_CLOSED
;
233 errno
= WSAGetLastError();
239 case WSAECONNABORTED
:
240 gdb_con
->closed
= true;
241 return ERROR_SERVER_REMOTE_CLOSED
;
243 gdb_con
->closed
= true;
244 return ERROR_SERVER_REMOTE_CLOSED
;
246 LOG_ERROR("read: %d", errno
);
255 gdb_con
->closed
= true;
256 return ERROR_SERVER_REMOTE_CLOSED
;
258 gdb_con
->closed
= true;
259 return ERROR_SERVER_REMOTE_CLOSED
;
261 LOG_ERROR("read: %s", strerror(errno
));
262 gdb_con
->closed
= true;
263 return ERROR_SERVER_REMOTE_CLOSED
;
268 #ifdef _DEBUG_GDB_IO_
269 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
270 LOG_DEBUG("received '%s'", debug_buffer
);
274 gdb_con
->buf_p
= gdb_con
->buffer
;
276 *next_char
= *(gdb_con
->buf_p
++);
277 if (gdb_con
->buf_cnt
> 0)
278 connection
->input_pending
= 1;
280 connection
->input_pending
= 0;
281 #ifdef _DEBUG_GDB_IO_
282 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
289 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
290 * held in registers in the inner loop.
292 * For small caches and embedded systems this is important!
294 static inline int gdb_get_char_fast(struct connection
*connection
,
295 int *next_char
, char **buf_p
, int *buf_cnt
)
297 int retval
= ERROR_OK
;
299 if ((*buf_cnt
)-- > 0) {
300 *next_char
= **buf_p
;
303 connection
->input_pending
= 1;
305 connection
->input_pending
= 0;
307 #ifdef _DEBUG_GDB_IO_
308 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
314 struct gdb_connection
*gdb_con
= connection
->priv
;
315 gdb_con
->buf_p
= *buf_p
;
316 gdb_con
->buf_cnt
= *buf_cnt
;
317 retval
= gdb_get_char_inner(connection
, next_char
);
318 *buf_p
= gdb_con
->buf_p
;
319 *buf_cnt
= gdb_con
->buf_cnt
;
324 static int gdb_get_char(struct connection
*connection
, int *next_char
)
326 struct gdb_connection
*gdb_con
= connection
->priv
;
327 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
330 static int gdb_putback_char(struct connection
*connection
, int last_char
)
332 struct gdb_connection
*gdb_con
= connection
->priv
;
334 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
335 *(--gdb_con
->buf_p
) = last_char
;
338 LOG_ERROR("BUG: couldn't put character back");
343 /* The only way we can detect that the socket is closed is the first time
344 * we write to it, we will fail. Subsequent write operations will
345 * succeed. Shudder! */
346 static int gdb_write(struct connection
*connection
, void *data
, int len
)
348 struct gdb_connection
*gdb_con
= connection
->priv
;
350 return ERROR_SERVER_REMOTE_CLOSED
;
352 if (connection_write(connection
, data
, len
) == len
)
354 gdb_con
->closed
= true;
355 return ERROR_SERVER_REMOTE_CLOSED
;
358 static int gdb_put_packet_inner(struct connection
*connection
,
359 char *buffer
, int len
)
362 unsigned char my_checksum
= 0;
363 #ifdef _DEBUG_GDB_IO_
368 struct gdb_connection
*gdb_con
= connection
->priv
;
370 for (i
= 0; i
< len
; i
++)
371 my_checksum
+= buffer
[i
];
373 #ifdef _DEBUG_GDB_IO_
375 * At this point we should have nothing in the input queue from GDB,
376 * however sometimes '-' is sent even though we've already received
377 * an ACK (+) for everything we've sent off.
381 retval
= check_pending(connection
, 0, &gotdata
);
382 if (retval
!= ERROR_OK
)
386 retval
= gdb_get_char(connection
, &reply
);
387 if (retval
!= ERROR_OK
)
390 /* fix a problem with some IAR tools */
391 gdb_putback_char(connection
, reply
);
392 LOG_DEBUG("Unexpected start of new packet");
396 LOG_WARNING("Discard unexpected char %c", reply
);
401 #ifdef _DEBUG_GDB_IO_
402 debug_buffer
= strndup(buffer
, len
);
403 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
407 char local_buffer
[1024];
408 local_buffer
[0] = '$';
409 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
410 /* performance gain on smaller packets by only a single call to gdb_write() */
411 memcpy(local_buffer
+ 1, buffer
, len
++);
412 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
413 retval
= gdb_write(connection
, local_buffer
, len
);
414 if (retval
!= ERROR_OK
)
417 /* larger packets are transmitted directly from caller supplied buffer
418 * by several calls to gdb_write() to avoid dynamic allocation */
419 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
420 retval
= gdb_write(connection
, local_buffer
, 1);
421 if (retval
!= ERROR_OK
)
423 retval
= gdb_write(connection
, buffer
, len
);
424 if (retval
!= ERROR_OK
)
426 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
427 if (retval
!= ERROR_OK
)
431 if (gdb_con
->noack_mode
)
434 retval
= gdb_get_char(connection
, &reply
);
435 if (retval
!= ERROR_OK
)
440 else if (reply
== '-') {
441 /* Stop sending output packets for now */
442 log_remove_callback(gdb_log_callback
, connection
);
443 LOG_WARNING("negative reply, retrying");
444 } else if (reply
== 0x3) {
446 retval
= gdb_get_char(connection
, &reply
);
447 if (retval
!= ERROR_OK
)
451 else if (reply
== '-') {
452 /* Stop sending output packets for now */
453 log_remove_callback(gdb_log_callback
, connection
);
454 LOG_WARNING("negative reply, retrying");
455 } else if (reply
== '$') {
456 LOG_ERROR("GDB missing ack(1) - assumed good");
457 gdb_putback_char(connection
, reply
);
460 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
461 gdb_con
->closed
= true;
462 return ERROR_SERVER_REMOTE_CLOSED
;
464 } else if (reply
== '$') {
465 LOG_ERROR("GDB missing ack(2) - assumed good");
466 gdb_putback_char(connection
, reply
);
469 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
471 gdb_con
->closed
= true;
472 return ERROR_SERVER_REMOTE_CLOSED
;
476 return ERROR_SERVER_REMOTE_CLOSED
;
481 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
483 struct gdb_connection
*gdb_con
= connection
->priv
;
484 gdb_con
->busy
= true;
485 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
486 gdb_con
->busy
= false;
488 /* we sent some data, reset timer for keep alive messages */
494 static inline int fetch_packet(struct connection
*connection
,
495 int *checksum_ok
, int noack
, int *len
, char *buffer
)
497 unsigned char my_checksum
= 0;
500 int retval
= ERROR_OK
;
502 struct gdb_connection
*gdb_con
= connection
->priv
;
507 /* move this over into local variables to use registers and give the
508 * more freedom to optimize */
509 char *buf_p
= gdb_con
->buf_p
;
510 int buf_cnt
= gdb_con
->buf_cnt
;
513 /* The common case is that we have an entire packet with no escape chars.
514 * We need to leave at least 2 bytes in the buffer to have
515 * gdb_get_char() update various bits and bobs correctly.
517 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
518 /* The compiler will struggle a bit with constant propagation and
519 * aliasing, so we help it by showing that these values do not
520 * change inside the loop
524 int run
= buf_cnt
- 2;
530 if (character
== '#') {
531 /* Danger! character can be '#' when esc is
532 * used so we need an explicit boolean for done here. */
537 if (character
== '}') {
538 /* data transmitted in binary mode (X packet)
539 * uses 0x7d as escape character */
540 my_checksum
+= character
& 0xff;
543 my_checksum
+= character
& 0xff;
544 buffer
[count
++] = (character
^ 0x20) & 0xff;
546 my_checksum
+= character
& 0xff;
547 buffer
[count
++] = character
& 0xff;
556 LOG_ERROR("packet buffer too small");
557 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
561 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
562 if (retval
!= ERROR_OK
)
565 if (character
== '#')
568 if (character
== '}') {
569 /* data transmitted in binary mode (X packet)
570 * uses 0x7d as escape character */
571 my_checksum
+= character
& 0xff;
573 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
574 if (retval
!= ERROR_OK
)
577 my_checksum
+= character
& 0xff;
578 buffer
[count
++] = (character
^ 0x20) & 0xff;
580 my_checksum
+= character
& 0xff;
581 buffer
[count
++] = character
& 0xff;
585 gdb_con
->buf_p
= buf_p
;
586 gdb_con
->buf_cnt
= buf_cnt
;
588 if (retval
!= ERROR_OK
)
593 retval
= gdb_get_char(connection
, &character
);
594 if (retval
!= ERROR_OK
)
596 checksum
[0] = character
;
597 retval
= gdb_get_char(connection
, &character
);
598 if (retval
!= ERROR_OK
)
600 checksum
[1] = character
;
604 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
609 static int gdb_get_packet_inner(struct connection
*connection
,
610 char *buffer
, int *len
)
614 struct gdb_connection
*gdb_con
= connection
->priv
;
618 retval
= gdb_get_char(connection
, &character
);
619 if (retval
!= ERROR_OK
)
622 #ifdef _DEBUG_GDB_IO_
623 LOG_DEBUG("character: '%c'", character
);
630 /* According to the GDB documentation
631 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
632 * "gdb sends a final `+` acknowledgment of the stub's `OK`
633 * response, which can be safely ignored by the stub."
634 * However OpenOCD server already is in noack mode at this
635 * point and instead of ignoring this it was emitting a
636 * warning. This code makes server ignore the first ACK
637 * that will be received after going into noack mode,
638 * warning only about subsequent ACK's. */
639 if (gdb_con
->noack_mode
> 1) {
640 LOG_WARNING("acknowledgment received, but no packet pending");
641 } else if (gdb_con
->noack_mode
) {
642 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
643 gdb_con
->noack_mode
= 2;
647 LOG_WARNING("negative acknowledgment, but no packet pending");
654 LOG_WARNING("ignoring character 0x%x", character
);
657 } while (character
!= '$');
660 /* explicit code expansion here to get faster inlined code in -O3 by not
661 * calculating checksum */
662 if (gdb_con
->noack_mode
) {
663 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
664 if (retval
!= ERROR_OK
)
667 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
668 if (retval
!= ERROR_OK
)
672 if (gdb_con
->noack_mode
) {
673 /* checksum is not checked in noack mode */
677 retval
= gdb_write(connection
, "+", 1);
678 if (retval
!= ERROR_OK
)
684 return ERROR_SERVER_REMOTE_CLOSED
;
689 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
691 struct gdb_connection
*gdb_con
= connection
->priv
;
692 gdb_con
->busy
= true;
693 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
694 gdb_con
->busy
= false;
698 static int gdb_output_con(struct connection
*connection
, const char *line
)
703 bin_size
= strlen(line
);
705 hex_buffer
= malloc(bin_size
* 2 + 2);
706 if (hex_buffer
== NULL
)
707 return ERROR_GDB_BUFFER_TOO_SMALL
;
710 size_t pkt_len
= hexify(hex_buffer
+ 1, (const uint8_t *)line
, bin_size
,
712 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
718 static int gdb_output(struct command_context
*context
, const char *line
)
720 /* this will be dumped to the log and also sent as an O packet if possible */
721 LOG_USER_N("%s", line
);
725 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
727 struct gdb_connection
*gdb_connection
= connection
->priv
;
729 char stop_reason
[20];
730 char current_thread
[25];
734 rtos_update_threads(target
);
736 if (target
->debug_reason
== DBG_REASON_EXIT
) {
737 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
740 if (target
->rtos
!= NULL
) {
741 target
->rtos
->current_threadid
= target
->rtos
->current_thread
;
742 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &ct
);
747 if (gdb_connection
->ctrl_c
) {
750 signal_var
= gdb_last_signal(ct
);
752 stop_reason
[0] = '\0';
753 if (ct
->debug_reason
== DBG_REASON_WATCHPOINT
) {
754 enum watchpoint_rw hit_wp_type
;
755 target_addr_t hit_wp_address
;
757 if (watchpoint_hit(ct
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
759 switch (hit_wp_type
) {
761 snprintf(stop_reason
, sizeof(stop_reason
),
762 "watch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
765 snprintf(stop_reason
, sizeof(stop_reason
),
766 "rwatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
769 snprintf(stop_reason
, sizeof(stop_reason
),
770 "awatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
778 current_thread
[0] = '\0';
779 if (target
->rtos
!= NULL
)
780 snprintf(current_thread
, sizeof(current_thread
), "thread:%" PRIx64
";",
781 target
->rtos
->current_thread
);
783 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s%s",
784 signal_var
, stop_reason
, current_thread
);
786 gdb_connection
->ctrl_c
= 0;
789 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
790 gdb_connection
->frontend_state
= TARGET_HALTED
;
793 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
795 struct gdb_connection
*gdb_connection
= connection
->priv
;
796 char fileio_command
[256];
798 bool program_exited
= false;
800 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
801 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
802 target
->fileio_info
->param_1
,
803 target
->fileio_info
->param_2
,
804 target
->fileio_info
->param_3
,
805 target
->fileio_info
->param_4
);
806 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
807 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
808 target
->fileio_info
->param_1
);
809 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
810 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
811 target
->fileio_info
->param_1
,
812 target
->fileio_info
->param_2
,
813 target
->fileio_info
->param_3
);
814 else if (strcmp(target
->fileio_info
->identifier
, "write") == 0)
815 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
816 target
->fileio_info
->param_1
,
817 target
->fileio_info
->param_2
,
818 target
->fileio_info
->param_3
);
819 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
820 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
821 target
->fileio_info
->param_1
,
822 target
->fileio_info
->param_2
,
823 target
->fileio_info
->param_3
);
824 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
825 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
826 target
->fileio_info
->param_1
,
827 target
->fileio_info
->param_2
,
828 target
->fileio_info
->param_3
,
829 target
->fileio_info
->param_4
);
830 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
831 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
832 target
->fileio_info
->param_1
,
833 target
->fileio_info
->param_2
);
834 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
835 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
836 target
->fileio_info
->param_1
,
837 target
->fileio_info
->param_2
,
838 target
->fileio_info
->param_3
);
839 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
840 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
841 target
->fileio_info
->param_1
,
842 target
->fileio_info
->param_2
);
843 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
844 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
845 target
->fileio_info
->param_1
,
846 target
->fileio_info
->param_2
);
847 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
848 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
849 target
->fileio_info
->param_1
);
850 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
851 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
852 target
->fileio_info
->param_1
,
853 target
->fileio_info
->param_2
);
854 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
855 /* If target hits exit syscall, report to GDB the program is terminated.
856 * In addition, let target run its own exit syscall handler. */
857 program_exited
= true;
858 sprintf(fileio_command
, "W%02" PRIx64
, target
->fileio_info
->param_1
);
860 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
862 /* encounter unknown syscall, continue */
863 gdb_connection
->frontend_state
= TARGET_RUNNING
;
864 target_resume(target
, 1, 0x0, 0, 0);
868 command_len
= strlen(fileio_command
);
869 gdb_put_packet(connection
, fileio_command
, command_len
);
871 if (program_exited
) {
872 /* Use target_resume() to let target run its own exit syscall handler. */
873 gdb_connection
->frontend_state
= TARGET_RUNNING
;
874 target_resume(target
, 1, 0x0, 0, 0);
876 gdb_connection
->frontend_state
= TARGET_HALTED
;
877 rtos_update_threads(target
);
881 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
883 struct gdb_connection
*gdb_connection
= connection
->priv
;
885 /* In the GDB protocol when we are stepping or continuing execution,
886 * we have a lingering reply. Upon receiving a halted event
887 * when we have that lingering packet, we reply to the original
888 * step or continue packet.
890 * Executing monitor commands can bring the target in and
891 * out of the running state so we'll see lots of TARGET_EVENT_XXX
892 * that are to be ignored.
894 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
895 /* stop forwarding log packets! */
896 log_remove_callback(gdb_log_callback
, connection
);
898 /* check fileio first */
899 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
900 gdb_fileio_reply(target
, connection
);
902 gdb_signal_reply(target
, connection
);
906 static int gdb_target_callback_event_handler(struct target
*target
,
907 enum target_event event
, void *priv
)
909 struct connection
*connection
= priv
;
910 struct gdb_service
*gdb_service
= connection
->service
->priv
;
912 if (gdb_service
->target
!= target
)
916 case TARGET_EVENT_GDB_HALT
:
917 gdb_frontend_halted(target
, connection
);
919 case TARGET_EVENT_HALTED
:
920 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
929 static int gdb_new_connection(struct connection
*connection
)
931 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
932 struct target
*target
;
936 target
= get_target_from_connection(connection
);
937 connection
->priv
= gdb_connection
;
938 connection
->cmd_ctx
->current_target
= target
;
940 /* initialize gdb connection information */
941 gdb_connection
->buf_p
= gdb_connection
->buffer
;
942 gdb_connection
->buf_cnt
= 0;
943 gdb_connection
->ctrl_c
= 0;
944 gdb_connection
->frontend_state
= TARGET_HALTED
;
945 gdb_connection
->vflash_image
= NULL
;
946 gdb_connection
->closed
= false;
947 gdb_connection
->busy
= false;
948 gdb_connection
->noack_mode
= 0;
949 gdb_connection
->sync
= false;
950 gdb_connection
->mem_write_error
= false;
951 gdb_connection
->attached
= true;
952 gdb_connection
->target_desc
.tdesc
= NULL
;
953 gdb_connection
->target_desc
.tdesc_length
= 0;
954 gdb_connection
->thread_list
= NULL
;
956 /* send ACK to GDB for debug request */
957 gdb_write(connection
, "+", 1);
959 /* output goes through gdb connection */
960 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
962 /* we must remove all breakpoints registered to the target as a previous
963 * GDB session could leave dangling breakpoints if e.g. communication
966 breakpoint_clear_target(target
);
967 watchpoint_clear_target(target
);
970 /* clean previous rtos session if supported*/
971 if (target
->rtos
->type
->clean
)
972 target
->rtos
->type
->clean(target
);
975 rtos_update_threads(target
);
978 /* remove the initial ACK from the incoming buffer */
979 retval
= gdb_get_char(connection
, &initial_ack
);
980 if (retval
!= ERROR_OK
)
983 /* FIX!!!??? would we actually ever receive a + here???
986 if (initial_ack
!= '+')
987 gdb_putback_char(connection
, initial_ack
);
988 target_call_event_callbacks(target
, TARGET_EVENT_GDB_ATTACH
);
990 if (gdb_use_memory_map
) {
991 /* Connect must fail if the memory map can't be set up correctly.
993 * This will cause an auto_probe to be invoked, which is either
994 * a no-op or it will fail when the target isn't ready(e.g. not halted).
997 for (i
= 0; i
< flash_get_bank_count(); i
++) {
998 struct flash_bank
*p
;
999 p
= get_flash_bank_by_num_noprobe(i
);
1000 if (p
->target
!= target
)
1002 retval
= get_flash_bank_by_num(i
, &p
);
1003 if (retval
!= ERROR_OK
) {
1004 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
1005 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1011 gdb_actual_connections
++;
1012 log_printf_lf(all_targets
->next
!= NULL
? LOG_LVL_INFO
: LOG_LVL_DEBUG
,
1013 __FILE__
, __LINE__
, __func__
,
1014 "New GDB Connection: %d, Target %s, state: %s",
1015 gdb_actual_connections
,
1016 target_name(target
),
1017 target_state_name(target
));
1019 /* DANGER! If we fail subsequently, we must remove this handler,
1020 * otherwise we occasionally see crashes as the timer can invoke the
1023 * register callback to be informed about target events */
1024 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1029 static int gdb_connection_closed(struct connection
*connection
)
1031 struct target
*target
;
1032 struct gdb_connection
*gdb_connection
= connection
->priv
;
1034 target
= get_target_from_connection(connection
);
1036 /* we're done forwarding messages. Tear down callback before
1037 * cleaning up connection.
1039 log_remove_callback(gdb_log_callback
, connection
);
1041 gdb_actual_connections
--;
1042 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1043 target_name(target
),
1044 target_state_name(target
),
1045 gdb_actual_connections
);
1047 /* see if an image built with vFlash commands is left */
1048 if (gdb_connection
->vflash_image
) {
1049 image_close(gdb_connection
->vflash_image
);
1050 free(gdb_connection
->vflash_image
);
1051 gdb_connection
->vflash_image
= NULL
;
1054 /* if this connection registered a debug-message receiver delete it */
1055 delete_debug_msg_receiver(connection
->cmd_ctx
, target
);
1057 if (connection
->priv
) {
1058 free(connection
->priv
);
1059 connection
->priv
= NULL
;
1061 LOG_ERROR("BUG: connection->priv == NULL");
1063 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1065 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
1067 target_call_event_callbacks(target
, TARGET_EVENT_GDB_DETACH
);
1072 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1075 snprintf(err
, 4, "E%2.2X", the_error
);
1076 gdb_put_packet(connection
, err
, 3);
1079 static int gdb_last_signal_packet(struct connection
*connection
,
1080 char const *packet
, int packet_size
)
1082 struct target
*target
= get_target_from_connection(connection
);
1083 struct gdb_connection
*gdb_con
= connection
->priv
;
1087 if (!gdb_con
->attached
) {
1088 /* if we are here we have received a kill packet
1089 * reply W stop reply otherwise gdb gets very unhappy */
1090 gdb_put_packet(connection
, "W00", 3);
1094 signal_var
= gdb_last_signal(target
);
1096 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1097 gdb_put_packet(connection
, sig_reply
, 3);
1102 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1104 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1107 return len
- 1 - pos
;
1110 /* Convert register to string of bytes. NB! The # of bits in the
1111 * register might be non-divisible by 8(a byte), in which
1112 * case an entire byte is shown.
1114 * NB! the format on the wire is the target endianness
1116 * The format of reg->value is little endian
1119 static void gdb_str_to_target(struct target
*target
,
1120 char *tstr
, struct reg
*reg
)
1127 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1129 for (i
= 0; i
< buf_len
; i
++) {
1130 int j
= gdb_reg_pos(target
, i
, buf_len
);
1131 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1135 /* copy over in register buffer */
1136 static void gdb_target_to_reg(struct target
*target
,
1137 char const *tstr
, int str_len
, uint8_t *bin
)
1140 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1145 for (i
= 0; i
< str_len
; i
+= 2) {
1147 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1148 LOG_ERROR("BUG: unable to convert register value");
1152 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1157 static int gdb_get_registers_packet(struct connection
*connection
,
1158 char const *packet
, int packet_size
)
1160 struct target
*target
= get_target_from_connection(connection
);
1161 struct reg
**reg_list
;
1164 int reg_packet_size
= 0;
1169 #ifdef _DEBUG_GDB_IO_
1173 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1176 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1178 if (retval
!= ERROR_OK
)
1179 return gdb_error(connection
, retval
);
1181 for (i
= 0; i
< reg_list_size
; i
++) {
1182 if (reg_list
[i
] == NULL
|| reg_list
[i
]->exist
== false)
1184 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1187 assert(reg_packet_size
> 0);
1189 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1190 if (reg_packet
== NULL
)
1193 reg_packet_p
= reg_packet
;
1195 for (i
= 0; i
< reg_list_size
; i
++) {
1196 if (reg_list
[i
] == NULL
|| reg_list
[i
]->exist
== false)
1198 if (!reg_list
[i
]->valid
) {
1199 retval
= reg_list
[i
]->type
->get(reg_list
[i
]);
1200 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1201 LOG_DEBUG("Couldn't get register %s.", reg_list
[i
]->name
);
1204 return gdb_error(connection
, retval
);
1207 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1208 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1211 #ifdef _DEBUG_GDB_IO_
1213 char *reg_packet_p_debug
;
1214 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1215 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1216 free(reg_packet_p_debug
);
1220 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1228 static int gdb_set_registers_packet(struct connection
*connection
,
1229 char const *packet
, int packet_size
)
1231 struct target
*target
= get_target_from_connection(connection
);
1233 struct reg
**reg_list
;
1236 char const *packet_p
;
1238 #ifdef _DEBUG_GDB_IO_
1242 /* skip command character */
1246 if (packet_size
% 2) {
1247 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1248 return ERROR_SERVER_REMOTE_CLOSED
;
1251 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1253 if (retval
!= ERROR_OK
)
1254 return gdb_error(connection
, retval
);
1257 for (i
= 0; i
< reg_list_size
; i
++) {
1259 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1261 if (packet_p
+ chars
> packet
+ packet_size
)
1262 LOG_ERROR("BUG: register packet is too small for registers");
1264 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1265 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1267 retval
= reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1268 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1269 LOG_DEBUG("Couldn't set register %s.", reg_list
[i
]->name
);
1272 return gdb_error(connection
, retval
);
1275 /* advance packet pointer */
1281 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1284 gdb_put_packet(connection
, "OK", 2);
1289 static int gdb_get_register_packet(struct connection
*connection
,
1290 char const *packet
, int packet_size
)
1292 struct target
*target
= get_target_from_connection(connection
);
1294 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1295 struct reg
**reg_list
;
1299 #ifdef _DEBUG_GDB_IO_
1303 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg(connection
, reg_num
)))
1306 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1308 if (retval
!= ERROR_OK
)
1309 return gdb_error(connection
, retval
);
1311 if (reg_list_size
<= reg_num
) {
1312 LOG_ERROR("gdb requested a non-existing register");
1313 return ERROR_SERVER_REMOTE_CLOSED
;
1316 if (!reg_list
[reg_num
]->valid
) {
1317 retval
= reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1318 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1319 LOG_DEBUG("Couldn't get register %s.", reg_list
[reg_num
]->name
);
1321 return gdb_error(connection
, retval
);
1325 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1); /* plus one for string termination null */
1327 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1329 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1337 static int gdb_set_register_packet(struct connection
*connection
,
1338 char const *packet
, int packet_size
)
1340 struct target
*target
= get_target_from_connection(connection
);
1342 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1343 struct reg
**reg_list
;
1347 #ifdef _DEBUG_GDB_IO_
1351 if (*separator
!= '=') {
1352 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1353 return ERROR_SERVER_REMOTE_CLOSED
;
1355 size_t chars
= strlen(separator
+ 1);
1356 uint8_t *bin_buf
= malloc(chars
/ 2);
1357 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1359 if ((target
->rtos
!= NULL
) &&
1360 (ERROR_OK
== rtos_set_reg(connection
, reg_num
, bin_buf
))) {
1362 gdb_put_packet(connection
, "OK", 2);
1366 retval
= target_get_gdb_reg_list_noread(target
, ®_list
, ®_list_size
,
1368 if (retval
!= ERROR_OK
) {
1370 return gdb_error(connection
, retval
);
1373 if (reg_list_size
<= reg_num
) {
1374 LOG_ERROR("gdb requested a non-existing register");
1377 return ERROR_SERVER_REMOTE_CLOSED
;
1380 if (chars
!= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2)) {
1381 LOG_ERROR("gdb sent %d bits for a %d-bit register (%s)",
1382 (int) chars
* 4, reg_list
[reg_num
]->size
, reg_list
[reg_num
]->name
);
1385 return ERROR_SERVER_REMOTE_CLOSED
;
1388 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1390 retval
= reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1391 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1392 LOG_DEBUG("Couldn't set register %s.", reg_list
[reg_num
]->name
);
1395 return gdb_error(connection
, retval
);
1398 gdb_put_packet(connection
, "OK", 2);
1406 /* No attempt is made to translate the "retval" to
1407 * GDB speak. This has to be done at the calling
1408 * site as no mapping really exists.
1410 static int gdb_error(struct connection
*connection
, int retval
)
1412 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1413 gdb_send_error(connection
, EFAULT
);
1417 /* We don't have to worry about the default 2 second timeout for GDB packets,
1418 * because GDB breaks up large memory reads into smaller reads.
1420 static int gdb_read_memory_packet(struct connection
*connection
,
1421 char const *packet
, int packet_size
)
1423 struct target
*target
= get_target_from_connection(connection
);
1431 int retval
= ERROR_OK
;
1433 /* skip command character */
1436 addr
= strtoull(packet
, &separator
, 16);
1438 if (*separator
!= ',') {
1439 LOG_ERROR("incomplete read memory packet received, dropping connection");
1440 return ERROR_SERVER_REMOTE_CLOSED
;
1443 len
= strtoul(separator
+ 1, NULL
, 16);
1446 LOG_WARNING("invalid read memory packet received (len == 0)");
1447 gdb_put_packet(connection
, "", 0);
1451 buffer
= malloc(len
);
1453 LOG_DEBUG("addr: 0x%16.16" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1455 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1457 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1458 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1459 * At some point this might be fixed in GDB, in which case this code can be removed.
1461 * OpenOCD developers are acutely aware of this problem, but there is nothing
1462 * gained by involving the user in this problem that hopefully will get resolved
1465 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1466 * cmd = view%20audit-trail&database = gdb&pr = 2395
1468 * For now, the default is to fix up things to make current GDB versions work.
1469 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1471 memset(buffer
, 0, len
);
1475 if (retval
== ERROR_OK
) {
1476 hex_buffer
= malloc(len
* 2 + 1);
1478 size_t pkt_len
= hexify(hex_buffer
, buffer
, len
, len
* 2 + 1);
1480 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1484 retval
= gdb_error(connection
, retval
);
1491 static int gdb_write_memory_packet(struct connection
*connection
,
1492 char const *packet
, int packet_size
)
1494 struct target
*target
= get_target_from_connection(connection
);
1502 /* skip command character */
1505 addr
= strtoull(packet
, &separator
, 16);
1507 if (*separator
!= ',') {
1508 LOG_ERROR("incomplete write memory packet received, dropping connection");
1509 return ERROR_SERVER_REMOTE_CLOSED
;
1512 len
= strtoul(separator
+ 1, &separator
, 16);
1514 if (*(separator
++) != ':') {
1515 LOG_ERROR("incomplete write memory packet received, dropping connection");
1516 return ERROR_SERVER_REMOTE_CLOSED
;
1519 buffer
= malloc(len
);
1521 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1523 if (unhexify(buffer
, separator
, len
) != len
)
1524 LOG_ERROR("unable to decode memory packet");
1526 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1528 if (retval
== ERROR_OK
)
1529 gdb_put_packet(connection
, "OK", 2);
1531 retval
= gdb_error(connection
, retval
);
1538 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1539 char const *packet
, int packet_size
)
1541 struct target
*target
= get_target_from_connection(connection
);
1546 int retval
= ERROR_OK
;
1547 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1548 * the assumption that we're in a download and it's important to go as fast
1550 uint32_t fast_limit
= 8;
1552 /* skip command character */
1555 addr
= strtoull(packet
, &separator
, 16);
1557 if (*separator
!= ',') {
1558 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1559 return ERROR_SERVER_REMOTE_CLOSED
;
1562 len
= strtoul(separator
+ 1, &separator
, 16);
1564 if (*(separator
++) != ':') {
1565 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1566 return ERROR_SERVER_REMOTE_CLOSED
;
1569 struct gdb_connection
*gdb_connection
= connection
->priv
;
1571 if (gdb_connection
->mem_write_error
)
1572 retval
= ERROR_FAIL
;
1574 if (retval
== ERROR_OK
) {
1575 if (len
>= fast_limit
) {
1576 /* By replying the packet *immediately* GDB will send us a new packet
1577 * while we write the last one to the target.
1578 * We only do this for larger writes, so that users who do something like:
1579 * p *((int*)0xdeadbeef)=8675309
1580 * will get immediate feedback that that write failed.
1582 gdb_put_packet(connection
, "OK", 2);
1585 retval
= gdb_error(connection
, retval
);
1586 /* now that we have reported the memory write error, we can clear the condition */
1587 gdb_connection
->mem_write_error
= false;
1588 if (retval
!= ERROR_OK
)
1593 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1595 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1596 if (retval
!= ERROR_OK
)
1597 gdb_connection
->mem_write_error
= true;
1600 if (len
< fast_limit
) {
1601 if (retval
!= ERROR_OK
) {
1602 gdb_error(connection
, retval
);
1603 gdb_connection
->mem_write_error
= false;
1605 gdb_put_packet(connection
, "OK", 2);
1612 static int gdb_step_continue_packet(struct connection
*connection
,
1613 char const *packet
, int packet_size
)
1615 struct target
*target
= get_target_from_connection(connection
);
1617 uint64_t address
= 0x0;
1618 int retval
= ERROR_OK
;
1622 if (packet_size
> 1)
1623 address
= strtoull(packet
+ 1, NULL
, 16);
1627 gdb_running_type
= packet
[0];
1628 if (packet
[0] == 'c') {
1629 LOG_DEBUG("continue");
1630 /* resume at current address, don't handle breakpoints, not debugging */
1631 retval
= target_resume(target
, current
, address
, 0, 0);
1632 } else if (packet
[0] == 's') {
1634 /* step at current or address, don't handle breakpoints */
1635 retval
= target_step(target
, current
, address
, 0);
1640 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1641 char const *packet
, int packet_size
)
1643 struct target
*target
= get_target_from_connection(connection
);
1645 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1646 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1652 LOG_DEBUG("[%s]", target_name(target
));
1654 type
= strtoul(packet
+ 1, &separator
, 16);
1656 if (type
== 0) /* memory breakpoint */
1657 bp_type
= BKPT_SOFT
;
1658 else if (type
== 1) /* hardware breakpoint */
1659 bp_type
= BKPT_HARD
;
1660 else if (type
== 2) /* write watchpoint */
1661 wp_type
= WPT_WRITE
;
1662 else if (type
== 3) /* read watchpoint */
1664 else if (type
== 4) /* access watchpoint */
1665 wp_type
= WPT_ACCESS
;
1667 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1668 return ERROR_SERVER_REMOTE_CLOSED
;
1671 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1672 bp_type
= gdb_breakpoint_override_type
;
1674 if (*separator
!= ',') {
1675 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1676 return ERROR_SERVER_REMOTE_CLOSED
;
1679 address
= strtoull(separator
+ 1, &separator
, 16);
1681 if (*separator
!= ',') {
1682 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1683 return ERROR_SERVER_REMOTE_CLOSED
;
1686 size
= strtoul(separator
+ 1, &separator
, 16);
1691 if (packet
[0] == 'Z') {
1692 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1693 if (retval
!= ERROR_OK
) {
1694 retval
= gdb_error(connection
, retval
);
1695 if (retval
!= ERROR_OK
)
1698 gdb_put_packet(connection
, "OK", 2);
1700 breakpoint_remove(target
, address
);
1701 gdb_put_packet(connection
, "OK", 2);
1708 if (packet
[0] == 'Z') {
1709 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1710 if (retval
!= ERROR_OK
) {
1711 retval
= gdb_error(connection
, retval
);
1712 if (retval
!= ERROR_OK
)
1715 gdb_put_packet(connection
, "OK", 2);
1717 watchpoint_remove(target
, address
);
1718 gdb_put_packet(connection
, "OK", 2);
1729 /* print out a string and allocate more space as needed,
1730 * mainly used for XML at this point
1732 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1733 const char *fmt
, ...)
1735 if (*retval
!= ERROR_OK
)
1740 if ((*xml
== NULL
) || (!first
)) {
1741 /* start by 0 to exercise all the code paths.
1742 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1744 *size
= *size
* 2 + 2;
1746 *xml
= realloc(*xml
, *size
);
1750 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1758 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1760 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1764 /* there was just enough or not enough space, allocate more. */
1769 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1771 /* Locate the annex. */
1772 const char *annex_end
= strchr(buf
, ':');
1773 if (annex_end
== NULL
)
1776 /* After the read marker and annex, qXfer looks like a
1777 * traditional 'm' packet. */
1779 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1781 if (*separator
!= ',')
1784 *len
= strtoul(separator
+ 1, NULL
, 16);
1786 /* Extract the annex if needed */
1787 if (annex
!= NULL
) {
1788 *annex
= strndup(buf
, annex_end
- buf
);
1796 static int compare_bank(const void *a
, const void *b
)
1798 struct flash_bank
*b1
, *b2
;
1799 b1
= *((struct flash_bank
**)a
);
1800 b2
= *((struct flash_bank
**)b
);
1802 if (b1
->base
== b2
->base
)
1804 else if (b1
->base
> b2
->base
)
1810 static int gdb_memory_map(struct connection
*connection
,
1811 char const *packet
, int packet_size
)
1813 /* We get away with only specifying flash here. Regions that are not
1814 * specified are treated as if we provided no memory map(if not we
1815 * could detect the holes and mark them as RAM).
1816 * Normally we only execute this code once, but no big deal if we
1817 * have to regenerate it a couple of times.
1820 struct target
*target
= get_target_from_connection(connection
);
1821 struct flash_bank
*p
;
1825 int retval
= ERROR_OK
;
1826 struct flash_bank
**banks
;
1830 target_addr_t ram_start
= 0;
1832 int target_flash_banks
= 0;
1834 /* skip command character */
1837 offset
= strtoul(packet
, &separator
, 16);
1838 length
= strtoul(separator
+ 1, &separator
, 16);
1840 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1842 /* Sort banks in ascending order. We need to report non-flash
1843 * memory as ram (or rather read/write) by default for GDB, since
1844 * it has no concept of non-cacheable read/write memory (i/o etc).
1846 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1848 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1849 p
= get_flash_bank_by_num_noprobe(i
);
1850 if (p
->target
!= target
)
1852 retval
= get_flash_bank_by_num(i
, &p
);
1853 if (retval
!= ERROR_OK
) {
1855 gdb_error(connection
, retval
);
1858 banks
[target_flash_banks
++] = p
;
1861 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1864 for (i
= 0; i
< target_flash_banks
; i
++) {
1866 unsigned sector_size
= 0;
1867 unsigned group_len
= 0;
1871 if (ram_start
< p
->base
)
1872 xml_printf(&retval
, &xml
, &pos
, &size
,
1873 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1874 "length=\"0x%x\"/>\n",
1875 ram_start
, p
->base
- ram_start
);
1877 /* Report adjacent groups of same-size sectors. So for
1878 * example top boot CFI flash will list an initial region
1879 * with several large sectors (maybe 128KB) and several
1880 * smaller ones at the end (maybe 32KB). STR7 will have
1881 * regions with 8KB, 32KB, and 64KB sectors; etc.
1883 for (j
= 0; j
< p
->num_sectors
; j
++) {
1885 /* Maybe start a new group of sectors. */
1886 if (sector_size
== 0) {
1887 if (p
->sectors
[j
].offset
+ p
->sectors
[j
].size
> p
->size
) {
1888 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1889 " overflows the end of %s bank.",
1890 p
->sectors
[j
].offset
, p
->name
);
1891 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1894 target_addr_t start
;
1895 start
= p
->base
+ p
->sectors
[j
].offset
;
1896 xml_printf(&retval
, &xml
, &pos
, &size
,
1897 "<memory type=\"flash\" "
1898 "start=\"" TARGET_ADDR_FMT
"\" ",
1900 sector_size
= p
->sectors
[j
].size
;
1901 group_len
= sector_size
;
1903 group_len
+= sector_size
; /* equal to p->sectors[j].size */
1906 /* Does this finish a group of sectors?
1907 * If not, continue an already-started group.
1909 if (j
< p
->num_sectors
- 1
1910 && p
->sectors
[j
+ 1].size
== sector_size
1911 && p
->sectors
[j
+ 1].offset
== p
->sectors
[j
].offset
+ sector_size
1912 && p
->sectors
[j
+ 1].offset
+ p
->sectors
[j
+ 1].size
<= p
->size
)
1915 xml_printf(&retval
, &xml
, &pos
, &size
,
1916 "length=\"0x%x\">\n"
1917 "<property name=\"blocksize\">"
1925 ram_start
= p
->base
+ p
->size
;
1929 xml_printf(&retval
, &xml
, &pos
, &size
,
1930 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1931 "length=\"" TARGET_ADDR_FMT
"\"/>\n",
1932 ram_start
, target_address_max(target
) - ram_start
+ 1);
1933 /* ELSE a flash chip could be at the very end of the address space, in
1934 * which case ram_start will be precisely 0 */
1938 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1940 if (retval
!= ERROR_OK
) {
1942 gdb_error(connection
, retval
);
1946 if (offset
+ length
> pos
)
1947 length
= pos
- offset
;
1949 char *t
= malloc(length
+ 1);
1951 memcpy(t
+ 1, xml
+ offset
, length
);
1952 gdb_put_packet(connection
, t
, length
+ 1);
1959 static const char *gdb_get_reg_type_name(enum reg_type type
)
1968 case REG_TYPE_INT16
:
1970 case REG_TYPE_INT32
:
1972 case REG_TYPE_INT64
:
1974 case REG_TYPE_INT128
:
1978 case REG_TYPE_UINT8
:
1980 case REG_TYPE_UINT16
:
1982 case REG_TYPE_UINT32
:
1984 case REG_TYPE_UINT64
:
1986 case REG_TYPE_UINT128
:
1988 case REG_TYPE_CODE_PTR
:
1990 case REG_TYPE_DATA_PTR
:
1992 case REG_TYPE_FLOAT
:
1994 case REG_TYPE_IEEE_SINGLE
:
1995 return "ieee_single";
1996 case REG_TYPE_IEEE_DOUBLE
:
1997 return "ieee_double";
1998 case REG_TYPE_ARCH_DEFINED
:
1999 return "int"; /* return arbitrary string to avoid compile warning. */
2002 return "int"; /* "int" as default value */
2005 static int lookup_add_arch_defined_types(char const **arch_defined_types_list
[], const char *type_id
,
2006 int *num_arch_defined_types
)
2008 int tbl_sz
= *num_arch_defined_types
;
2010 if (type_id
!= NULL
&& (strcmp(type_id
, ""))) {
2011 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2012 if (!((*arch_defined_types_list
)[j
])) {
2013 (*arch_defined_types_list
)[tbl_sz
++] = type_id
;
2014 *arch_defined_types_list
= realloc(*arch_defined_types_list
,
2015 sizeof(char *) * (tbl_sz
+ 1));
2016 (*arch_defined_types_list
)[tbl_sz
] = NULL
;
2017 *num_arch_defined_types
= tbl_sz
;
2020 if (!strcmp((*arch_defined_types_list
)[j
], type_id
))
2029 static int gdb_generate_reg_type_description(struct target
*target
,
2030 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
,
2031 char const **arch_defined_types_list
[], int * num_arch_defined_types
)
2033 int retval
= ERROR_OK
;
2035 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
2036 struct reg_data_type
*data_type
= type
->reg_type_vector
->type
;
2037 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2038 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2039 num_arch_defined_types
))
2040 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2041 arch_defined_types_list
,
2042 num_arch_defined_types
);
2044 /* <vector id="id" type="type" count="count"/> */
2045 xml_printf(&retval
, tdesc
, pos
, size
,
2046 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
2047 type
->id
, type
->reg_type_vector
->type
->id
,
2048 type
->reg_type_vector
->count
);
2050 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
2051 struct reg_data_type_union_field
*field
;
2052 field
= type
->reg_type_union
->fields
;
2053 while (field
!= NULL
) {
2054 struct reg_data_type
*data_type
= field
->type
;
2055 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2056 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2057 num_arch_defined_types
))
2058 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2059 arch_defined_types_list
,
2060 num_arch_defined_types
);
2063 field
= field
->next
;
2066 * <field name="name" type="type"/> ...
2068 xml_printf(&retval
, tdesc
, pos
, size
,
2069 "<union id=\"%s\">\n",
2072 field
= type
->reg_type_union
->fields
;
2073 while (field
!= NULL
) {
2074 xml_printf(&retval
, tdesc
, pos
, size
,
2075 "<field name=\"%s\" type=\"%s\"/>\n",
2076 field
->name
, field
->type
->id
);
2078 field
= field
->next
;
2081 xml_printf(&retval
, tdesc
, pos
, size
,
2084 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
2085 struct reg_data_type_struct_field
*field
;
2086 field
= type
->reg_type_struct
->fields
;
2088 if (field
->use_bitfields
) {
2089 /* <struct id="id" size="size">
2090 * <field name="name" start="start" end="end"/> ...
2092 xml_printf(&retval
, tdesc
, pos
, size
,
2093 "<struct id=\"%s\" size=\"%d\">\n",
2094 type
->id
, type
->reg_type_struct
->size
);
2095 while (field
!= NULL
) {
2096 xml_printf(&retval
, tdesc
, pos
, size
,
2097 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2098 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2099 gdb_get_reg_type_name(field
->bitfield
->type
));
2101 field
= field
->next
;
2104 while (field
!= NULL
) {
2105 struct reg_data_type
*data_type
= field
->type
;
2106 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2107 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2108 num_arch_defined_types
))
2109 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2110 arch_defined_types_list
,
2111 num_arch_defined_types
);
2116 * <field name="name" type="type"/> ...
2118 xml_printf(&retval
, tdesc
, pos
, size
,
2119 "<struct id=\"%s\">\n",
2121 while (field
!= NULL
) {
2122 xml_printf(&retval
, tdesc
, pos
, size
,
2123 "<field name=\"%s\" type=\"%s\"/>\n",
2124 field
->name
, field
->type
->id
);
2126 field
= field
->next
;
2130 xml_printf(&retval
, tdesc
, pos
, size
,
2133 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2134 /* <flags id="id" size="size">
2135 * <field name="name" start="start" end="end"/> ...
2137 xml_printf(&retval
, tdesc
, pos
, size
,
2138 "<flags id=\"%s\" size=\"%d\">\n",
2139 type
->id
, type
->reg_type_flags
->size
);
2141 struct reg_data_type_flags_field
*field
;
2142 field
= type
->reg_type_flags
->fields
;
2143 while (field
!= NULL
) {
2144 xml_printf(&retval
, tdesc
, pos
, size
,
2145 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2146 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2147 gdb_get_reg_type_name(field
->bitfield
->type
));
2149 field
= field
->next
;
2152 xml_printf(&retval
, tdesc
, pos
, size
,
2160 /* Get a list of available target registers features. feature_list must
2161 * be freed by caller.
2163 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2164 struct reg
**reg_list
, int reg_list_size
)
2168 /* Start with only one element */
2169 *feature_list
= calloc(1, sizeof(char *));
2171 for (int i
= 0; i
< reg_list_size
; i
++) {
2172 if (reg_list
[i
]->exist
== false)
2175 if (reg_list
[i
]->feature
!= NULL
2176 && reg_list
[i
]->feature
->name
!= NULL
2177 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2178 /* We found a feature, check if the feature is already in the
2179 * table. If not, allocate a new entry for the table and
2180 * put the new feature in it.
2182 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2183 if (!((*feature_list
)[j
])) {
2184 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2185 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2186 (*feature_list
)[tbl_sz
] = NULL
;
2189 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2196 if (feature_list_size
)
2197 *feature_list_size
= tbl_sz
;
2202 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2204 int retval
= ERROR_OK
;
2205 struct reg
**reg_list
= NULL
;
2207 char const *architecture
;
2208 char const **features
= NULL
;
2209 int feature_list_size
= 0;
2215 retval
= target_get_gdb_reg_list_noread(target
, ®_list
,
2216 ®_list_size
, REG_CLASS_ALL
);
2218 if (retval
!= ERROR_OK
) {
2219 LOG_ERROR("get register list failed");
2220 retval
= ERROR_FAIL
;
2224 if (reg_list_size
<= 0) {
2225 LOG_ERROR("get register list failed");
2226 retval
= ERROR_FAIL
;
2230 /* Get a list of available target registers features */
2231 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2232 if (retval
!= ERROR_OK
) {
2233 LOG_ERROR("Can't get the registers feature list");
2234 retval
= ERROR_FAIL
;
2238 /* If we found some features associated with registers, create sections */
2239 int current_feature
= 0;
2241 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2242 "<?xml version=\"1.0\"?>\n"
2243 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2244 "<target version=\"1.0\">\n");
2246 /* generate architecture element if supported by target */
2247 architecture
= target_get_gdb_arch(target
);
2248 if (architecture
!= NULL
)
2249 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2250 "<architecture>%s</architecture>\n", architecture
);
2252 /* generate target description according to register list */
2253 if (features
!= NULL
) {
2254 while (features
[current_feature
]) {
2255 char const **arch_defined_types
= NULL
;
2256 int num_arch_defined_types
= 0;
2258 arch_defined_types
= calloc(1, sizeof(char *));
2259 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2260 "<feature name=\"%s\">\n",
2261 features
[current_feature
]);
2264 for (i
= 0; i
< reg_list_size
; i
++) {
2266 if (reg_list
[i
]->exist
== false)
2269 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2272 const char *type_str
;
2273 if (reg_list
[i
]->reg_data_type
!= NULL
) {
2274 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2275 /* generate <type... first, if there are architecture-defined types. */
2276 if (lookup_add_arch_defined_types(&arch_defined_types
,
2277 reg_list
[i
]->reg_data_type
->id
,
2278 &num_arch_defined_types
))
2279 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2280 reg_list
[i
]->reg_data_type
,
2281 &arch_defined_types
,
2282 &num_arch_defined_types
);
2284 type_str
= reg_list
[i
]->reg_data_type
->id
;
2286 /* predefined type */
2287 type_str
= gdb_get_reg_type_name(
2288 reg_list
[i
]->reg_data_type
->type
);
2291 /* Default type is "int" */
2295 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2296 "<reg name=\"%s\"", reg_list
[i
]->name
);
2297 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2298 " bitsize=\"%d\"", reg_list
[i
]->size
);
2299 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2300 " regnum=\"%d\"", reg_list
[i
]->number
);
2301 if (reg_list
[i
]->caller_save
)
2302 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2303 " save-restore=\"yes\"");
2305 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2306 " save-restore=\"no\"");
2308 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2309 " type=\"%s\"", type_str
);
2311 if (reg_list
[i
]->group
!= NULL
)
2312 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2313 " group=\"%s\"", reg_list
[i
]->group
);
2315 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2319 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2323 free(arch_defined_types
);
2327 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2334 if (retval
== ERROR_OK
)
2342 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2343 char **chunk
, int32_t offset
, uint32_t length
)
2345 if (target_desc
== NULL
) {
2346 LOG_ERROR("Unable to Generate Target Description");
2350 char *tdesc
= target_desc
->tdesc
;
2351 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2353 if (tdesc
== NULL
) {
2354 int retval
= gdb_generate_target_description(target
, &tdesc
);
2355 if (retval
!= ERROR_OK
) {
2356 LOG_ERROR("Unable to Generate Target Description");
2360 tdesc_length
= strlen(tdesc
);
2365 if (length
< (tdesc_length
- offset
))
2366 transfer_type
= 'm';
2368 transfer_type
= 'l';
2370 *chunk
= malloc(length
+ 2);
2371 if (*chunk
== NULL
) {
2372 LOG_ERROR("Unable to allocate memory");
2376 (*chunk
)[0] = transfer_type
;
2377 if (transfer_type
== 'm') {
2378 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2379 (*chunk
)[1 + length
] = '\0';
2381 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2382 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2384 /* After gdb-server sends out last chunk, invalidate tdesc. */
2390 target_desc
->tdesc
= tdesc
;
2391 target_desc
->tdesc_length
= tdesc_length
;
2396 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2398 int retval
= ERROR_OK
;
2399 struct reg
**reg_list
= NULL
;
2400 int reg_list_size
= 0;
2401 char const **features
= NULL
;
2402 int feature_list_size
= 0;
2404 char const *architecture
= target_get_gdb_arch(target
);
2406 retval
= target_get_gdb_reg_list_noread(target
, ®_list
,
2407 ®_list_size
, REG_CLASS_ALL
);
2408 if (retval
!= ERROR_OK
) {
2409 LOG_ERROR("get register list failed");
2413 if (reg_list_size
<= 0) {
2414 LOG_ERROR("get register list failed");
2415 retval
= ERROR_FAIL
;
2419 /* Get a list of available target registers features */
2420 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2421 if (retval
!= ERROR_OK
) {
2422 LOG_ERROR("Can't get the registers feature list");
2427 if (architecture
|| feature_list_size
)
2441 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2443 struct rtos
*rtos
= target
->rtos
;
2444 int retval
= ERROR_OK
;
2445 char *thread_list
= NULL
;
2449 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2450 "<?xml version=\"1.0\"?>\n"
2454 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2455 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2457 if (!thread_detail
->exists
)
2460 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2461 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2463 if (thread_detail
->thread_name_str
!= NULL
)
2464 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2465 "Name: %s", thread_detail
->thread_name_str
);
2467 if (thread_detail
->extra_info_str
!= NULL
) {
2468 if (thread_detail
->thread_name_str
!= NULL
)
2469 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2471 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2472 thread_detail
->extra_info_str
);
2475 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2480 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2483 if (retval
== ERROR_OK
)
2484 *thread_list_out
= thread_list
;
2491 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2492 char **chunk
, int32_t offset
, uint32_t length
)
2494 if (*thread_list
== NULL
) {
2495 int retval
= gdb_generate_thread_list(target
, thread_list
);
2496 if (retval
!= ERROR_OK
) {
2497 LOG_ERROR("Unable to Generate Thread List");
2502 size_t thread_list_length
= strlen(*thread_list
);
2505 length
= MIN(length
, thread_list_length
- offset
);
2506 if (length
< (thread_list_length
- offset
))
2507 transfer_type
= 'm';
2509 transfer_type
= 'l';
2511 *chunk
= malloc(length
+ 2 + 3);
2512 /* Allocating extra 3 bytes prevents false positive valgrind report
2513 * of strlen(chunk) word access:
2514 * Invalid read of size 4
2515 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2516 if (*chunk
== NULL
) {
2517 LOG_ERROR("Unable to allocate memory");
2521 (*chunk
)[0] = transfer_type
;
2522 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2523 (*chunk
)[1 + length
] = '\0';
2525 /* After gdb-server sends out last chunk, invalidate thread list. */
2526 if (transfer_type
== 'l') {
2528 *thread_list
= NULL
;
2534 static int gdb_query_packet(struct connection
*connection
,
2535 char const *packet
, int packet_size
)
2537 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2538 struct gdb_connection
*gdb_connection
= connection
->priv
;
2539 struct target
*target
= get_target_from_connection(connection
);
2541 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2542 if (packet_size
> 6) {
2544 cmd
= malloc((packet_size
- 6) / 2 + 1);
2545 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2548 /* We want to print all debug output to GDB connection */
2549 log_add_callback(gdb_log_callback
, connection
);
2550 target_call_timer_callbacks_now();
2551 /* some commands need to know the GDB connection, make note of current
2552 * GDB connection. */
2553 current_gdb_connection
= gdb_connection
;
2554 command_run_line(cmd_ctx
, cmd
);
2555 current_gdb_connection
= NULL
;
2556 target_call_timer_callbacks_now();
2557 log_remove_callback(gdb_log_callback
, connection
);
2560 gdb_put_packet(connection
, "OK", 2);
2562 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2563 if (packet_size
> 5) {
2568 target_addr_t addr
= 0;
2571 /* skip command character */
2574 addr
= strtoull(packet
, &separator
, 16);
2576 if (*separator
!= ',') {
2577 LOG_ERROR("incomplete read memory packet received, dropping connection");
2578 return ERROR_SERVER_REMOTE_CLOSED
;
2581 len
= strtoul(separator
+ 1, NULL
, 16);
2583 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2585 if (retval
== ERROR_OK
) {
2586 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2587 gdb_put_packet(connection
, gdb_reply
, 9);
2589 retval
= gdb_error(connection
, retval
);
2590 if (retval
!= ERROR_OK
)
2596 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2597 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2598 * qXfer:features:read is supported for some targets */
2599 int retval
= ERROR_OK
;
2600 char *buffer
= NULL
;
2603 int gdb_target_desc_supported
= 0;
2605 /* we need to test that the target supports target descriptions */
2606 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2607 if (retval
!= ERROR_OK
) {
2608 LOG_INFO("Failed detecting Target Description Support, disabling");
2609 gdb_target_desc_supported
= 0;
2612 /* support may be disabled globally */
2613 if (gdb_use_target_description
== 0) {
2614 if (gdb_target_desc_supported
)
2615 LOG_WARNING("Target Descriptions Supported, but disabled");
2616 gdb_target_desc_supported
= 0;
2623 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2625 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2626 (gdb_target_desc_supported
== 1) ? '+' : '-');
2628 if (retval
!= ERROR_OK
) {
2629 gdb_send_error(connection
, 01);
2633 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2637 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2638 && (flash_get_bank_count() > 0))
2639 return gdb_memory_map(connection
, packet
, packet_size
);
2640 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2642 int retval
= ERROR_OK
;
2645 unsigned int length
;
2647 /* skip command character */
2650 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2651 gdb_send_error(connection
, 01);
2655 /* Target should prepare correct target description for annex.
2656 * The first character of returned xml is 'm' or 'l'. 'm' for
2657 * there are *more* chunks to transfer. 'l' for it is the *last*
2658 * chunk of target description.
2660 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2661 &xml
, offset
, length
);
2662 if (retval
!= ERROR_OK
) {
2663 gdb_error(connection
, retval
);
2667 gdb_put_packet(connection
, xml
, strlen(xml
));
2671 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2673 int retval
= ERROR_OK
;
2676 unsigned int length
;
2678 /* skip command character */
2681 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2682 gdb_send_error(connection
, 01);
2686 /* Target should prepare correct thread list for annex.
2687 * The first character of returned xml is 'm' or 'l'. 'm' for
2688 * there are *more* chunks to transfer. 'l' for it is the *last*
2689 * chunk of target description.
2691 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2692 &xml
, offset
, length
);
2693 if (retval
!= ERROR_OK
) {
2694 gdb_error(connection
, retval
);
2698 gdb_put_packet(connection
, xml
, strlen(xml
));
2702 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2703 gdb_connection
->noack_mode
= 1;
2704 gdb_put_packet(connection
, "OK", 2);
2708 gdb_put_packet(connection
, "", 0);
2712 static bool gdb_handle_vcont_packet(struct connection
*connection
, const char *packet
, int packet_size
)
2714 struct gdb_connection
*gdb_connection
= connection
->priv
;
2715 struct target
*target
= get_target_from_connection(connection
);
2716 const char *parse
= packet
;
2719 /* query for vCont supported */
2720 if (parse
[0] == '?') {
2721 if (target
->type
->step
!= NULL
) {
2722 /* gdb doesn't accept c without C and s without S */
2723 gdb_put_packet(connection
, "vCont;c;C;s;S", 13);
2729 if (parse
[0] == ';') {
2734 /* simple case, a continue packet */
2735 if (parse
[0] == 'c') {
2736 gdb_running_type
= 'c';
2737 LOG_DEBUG("target %s continue", target_name(target
));
2738 log_add_callback(gdb_log_callback
, connection
);
2739 retval
= target_resume(target
, 1, 0, 0, 0);
2740 if (retval
== ERROR_TARGET_NOT_HALTED
)
2741 LOG_INFO("target %s was not halted when resume was requested", target_name(target
));
2743 /* poll target in an attempt to make its internal state consistent */
2744 if (retval
!= ERROR_OK
) {
2745 retval
= target_poll(target
);
2746 if (retval
!= ERROR_OK
)
2747 LOG_DEBUG("error polling target %s after failed resume", target_name(target
));
2751 * We don't report errors to gdb here, move frontend_state to
2752 * TARGET_RUNNING to stay in sync with gdb's expectation of the
2755 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2756 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2761 /* single-step or step-over-breakpoint */
2762 if (parse
[0] == 's') {
2763 gdb_running_type
= 's';
2764 bool fake_step
= false;
2766 if (strncmp(parse
, "s:", 2) == 0) {
2767 struct target
*ct
= target
;
2775 thread_id
= strtoll(parse
, &endp
, 16);
2777 packet_size
-= endp
- parse
;
2781 if (target
->rtos
!= NULL
) {
2782 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
2783 rtos_update_threads(target
);
2785 target
->rtos
->gdb_target_for_threadid(connection
, thread_id
, &ct
);
2788 * check if the thread to be stepped is the current rtos thread
2789 * if not, we must fake the step
2791 if (target
->rtos
->current_thread
!= thread_id
)
2795 if (parse
[0] == ';') {
2799 if (parse
[0] == 'c') {
2802 /* check if thread-id follows */
2803 if (parse
[0] == ':') {
2807 tid
= strtoll(parse
, &endp
, 16);
2808 if (tid
== thread_id
) {
2810 * Special case: only step a single thread (core),
2811 * keep the other threads halted. Currently, only
2812 * aarch64 target understands it. Other target types don't
2813 * care (nobody checks the actual value of 'current')
2814 * and it doesn't really matter. This deserves
2815 * a symbolic constant and a formal interface documentation
2818 LOG_DEBUG("request to step current core only");
2819 /* uncomment after checking that indeed other targets are safe */
2826 LOG_DEBUG("target %s single-step thread %"PRIx64
, target_name(ct
), thread_id
);
2827 log_add_callback(gdb_log_callback
, connection
);
2828 target_call_event_callbacks(ct
, TARGET_EVENT_GDB_START
);
2831 * work around an annoying gdb behaviour: when the current thread
2832 * is changed in gdb, it assumes that the target can follow and also
2833 * make the thread current. This is an assumption that cannot hold
2834 * for a real target running a multi-threading OS. We just fake
2835 * the step to not trigger an internal error in gdb. See
2836 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
2840 char sig_reply
[128];
2842 LOG_DEBUG("fake step thread %"PRIx64
, thread_id
);
2844 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
),
2845 "T05thread:%016"PRIx64
";", thread_id
);
2847 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
2848 log_remove_callback(gdb_log_callback
, connection
);
2853 /* support for gdb_sync command */
2854 if (gdb_connection
->sync
) {
2855 gdb_connection
->sync
= false;
2856 if (ct
->state
== TARGET_HALTED
) {
2857 LOG_DEBUG("stepi ignored. GDB will now fetch the register state " \
2858 "from the target.");
2859 gdb_sig_halted(connection
);
2860 log_remove_callback(gdb_log_callback
, connection
);
2862 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2866 retval
= target_step(ct
, current_pc
, 0, 0);
2867 if (retval
== ERROR_TARGET_NOT_HALTED
)
2868 LOG_INFO("target %s was not halted when step was requested", target_name(ct
));
2870 /* if step was successful send a reply back to gdb */
2871 if (retval
== ERROR_OK
) {
2872 retval
= target_poll(ct
);
2873 if (retval
!= ERROR_OK
)
2874 LOG_DEBUG("error polling target %s after successful step", target_name(ct
));
2875 /* send back signal information */
2876 gdb_signal_reply(ct
, connection
);
2877 /* stop forwarding log packets! */
2878 log_remove_callback(gdb_log_callback
, connection
);
2880 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2882 LOG_ERROR("Unknown vCont packet");
2891 static char *next_hex_encoded_field(const char **str
, char sep
)
2894 const char *hex
= *str
;
2898 const char *end
= strchr(hex
, sep
);
2900 hexlen
= strlen(hex
);
2903 *str
= hex
+ hexlen
+ 1;
2905 if (hexlen
% 2 != 0) {
2906 /* Malformed hex data */
2910 size_t count
= hexlen
/ 2;
2911 char *decoded
= malloc(count
+ 1);
2912 if (decoded
== NULL
)
2915 size_t converted
= unhexify((void *)decoded
, hex
, count
);
2916 if (converted
!= count
) {
2921 decoded
[count
] = '\0';
2925 /* handle extended restart packet */
2926 static void gdb_restart_inferior(struct connection
*connection
, const char *packet
, int packet_size
)
2928 struct gdb_connection
*gdb_con
= connection
->priv
;
2929 struct target
*target
= get_target_from_connection(connection
);
2931 breakpoint_clear_target(target
);
2932 watchpoint_clear_target(target
);
2933 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
2934 target_name(target
));
2935 /* set connection as attached after reset */
2936 gdb_con
->attached
= true;
2937 /* info rtos parts */
2938 gdb_thread_packet(connection
, packet
, packet_size
);
2941 static bool gdb_handle_vrun_packet(struct connection
*connection
, const char *packet
, int packet_size
)
2943 struct target
*target
= get_target_from_connection(connection
);
2944 const char *parse
= packet
;
2949 if (parse
[0] != ';')
2953 /* Skip first field "filename"; don't know what to do with it. */
2954 free(next_hex_encoded_field(&parse
, ';'));
2956 char *cmdline
= next_hex_encoded_field(&parse
, ';');
2958 while (cmdline
!= NULL
&& (arg
= next_hex_encoded_field(&parse
, ';')) != NULL
) {
2959 char *new_cmdline
= alloc_printf("%s %s", cmdline
, arg
);
2962 cmdline
= new_cmdline
;
2965 if (cmdline
!= NULL
) {
2966 if (target
->semihosting
!= NULL
) {
2967 LOG_INFO("GDB set inferior command line to '%s'", cmdline
);
2968 free(target
->semihosting
->cmdline
);
2969 target
->semihosting
->cmdline
= cmdline
;
2971 LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline
);
2976 gdb_restart_inferior(connection
, packet
, packet_size
);
2977 gdb_put_packet(connection
, "S00", 3);
2981 static int gdb_v_packet(struct connection
*connection
,
2982 char const *packet
, int packet_size
)
2984 struct gdb_connection
*gdb_connection
= connection
->priv
;
2987 struct target
*target
= get_target_from_connection(connection
);
2989 if (strncmp(packet
, "vCont", 5) == 0) {
2995 handled
= gdb_handle_vcont_packet(connection
, packet
, packet_size
);
2997 gdb_put_packet(connection
, "", 0);
3002 if (strncmp(packet
, "vRun", 4) == 0) {
3005 handled
= gdb_handle_vrun_packet(connection
, packet
, packet_size
);
3007 gdb_put_packet(connection
, "", 0);
3012 /* if flash programming disabled - send a empty reply */
3014 if (gdb_flash_program
== 0) {
3015 gdb_put_packet(connection
, "", 0);
3019 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
3021 unsigned long length
;
3023 char const *parse
= packet
+ 12;
3024 if (*parse
== '\0') {
3025 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3026 return ERROR_SERVER_REMOTE_CLOSED
;
3029 addr
= strtoul(parse
, (char **)&parse
, 16);
3031 if (*(parse
++) != ',' || *parse
== '\0') {
3032 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3033 return ERROR_SERVER_REMOTE_CLOSED
;
3036 length
= strtoul(parse
, (char **)&parse
, 16);
3038 if (*parse
!= '\0') {
3039 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3040 return ERROR_SERVER_REMOTE_CLOSED
;
3043 /* assume all sectors need erasing - stops any problems
3044 * when flash_write is called multiple times */
3047 /* perform any target specific operations before the erase */
3048 target_call_event_callbacks(target
,
3049 TARGET_EVENT_GDB_FLASH_ERASE_START
);
3051 /* vFlashErase:addr,length messages require region start and
3052 * end to be "block" aligned ... if padding is ever needed,
3053 * GDB will have become dangerously confused.
3055 result
= flash_erase_address_range(target
, false, addr
,
3058 /* perform any target specific operations after the erase */
3059 target_call_event_callbacks(target
,
3060 TARGET_EVENT_GDB_FLASH_ERASE_END
);
3063 if (result
!= ERROR_OK
) {
3064 /* GDB doesn't evaluate the actual error number returned,
3065 * treat a failed erase as an I/O error
3067 gdb_send_error(connection
, EIO
);
3068 LOG_ERROR("flash_erase returned %i", result
);
3070 gdb_put_packet(connection
, "OK", 2);
3075 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
3078 unsigned long length
;
3079 char const *parse
= packet
+ 12;
3081 if (*parse
== '\0') {
3082 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3083 return ERROR_SERVER_REMOTE_CLOSED
;
3085 addr
= strtoul(parse
, (char **)&parse
, 16);
3086 if (*(parse
++) != ':') {
3087 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3088 return ERROR_SERVER_REMOTE_CLOSED
;
3090 length
= packet_size
- (parse
- packet
);
3092 /* create a new image if there isn't already one */
3093 if (gdb_connection
->vflash_image
== NULL
) {
3094 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
3095 image_open(gdb_connection
->vflash_image
, "", "build");
3098 /* create new section with content from packet buffer */
3099 retval
= image_add_section(gdb_connection
->vflash_image
,
3100 addr
, length
, 0x0, (uint8_t const *)parse
);
3101 if (retval
!= ERROR_OK
)
3104 gdb_put_packet(connection
, "OK", 2);
3109 if (strncmp(packet
, "vFlashDone", 10) == 0) {
3112 /* process the flashing buffer. No need to erase as GDB
3113 * always issues a vFlashErase first. */
3114 target_call_event_callbacks(target
,
3115 TARGET_EVENT_GDB_FLASH_WRITE_START
);
3116 result
= flash_write(target
, gdb_connection
->vflash_image
,
3118 target_call_event_callbacks(target
,
3119 TARGET_EVENT_GDB_FLASH_WRITE_END
);
3120 if (result
!= ERROR_OK
) {
3121 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
3122 gdb_put_packet(connection
, "E.memtype", 9);
3124 gdb_send_error(connection
, EIO
);
3126 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
3127 gdb_put_packet(connection
, "OK", 2);
3130 image_close(gdb_connection
->vflash_image
);
3131 free(gdb_connection
->vflash_image
);
3132 gdb_connection
->vflash_image
= NULL
;
3137 gdb_put_packet(connection
, "", 0);
3141 static int gdb_detach(struct connection
*connection
)
3144 * Only reply "OK" to GDB
3145 * it will close the connection and this will trigger a call to
3146 * gdb_connection_closed() that will in turn trigger the event
3147 * TARGET_EVENT_GDB_DETACH
3149 return gdb_put_packet(connection
, "OK", 2);
3152 /* The format of 'F' response packet is
3153 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3155 static int gdb_fileio_response_packet(struct connection
*connection
,
3156 char const *packet
, int packet_size
)
3158 struct target
*target
= get_target_from_connection(connection
);
3160 char *parsing_point
;
3161 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
3162 int fileio_errno
= 0;
3163 bool fileio_ctrl_c
= false;
3168 if (*separator
== ',') {
3169 parsing_point
= separator
+ 1;
3170 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
3171 if (*separator
== ',') {
3172 if (*(separator
+ 1) == 'C') {
3173 /* TODO: process ctrl-c */
3174 fileio_ctrl_c
= true;
3179 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3180 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
3182 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
3183 if (retval
!= ERROR_OK
)
3186 /* After File-I/O ends, keep continue or step */
3187 if (gdb_running_type
== 'c')
3188 retval
= target_resume(target
, 1, 0x0, 0, 0);
3189 else if (gdb_running_type
== 's')
3190 retval
= target_step(target
, 1, 0x0, 0);
3192 retval
= ERROR_FAIL
;
3194 if (retval
!= ERROR_OK
)
3200 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
3201 const char *function
, const char *string
)
3203 struct connection
*connection
= priv
;
3204 struct gdb_connection
*gdb_con
= connection
->priv
;
3206 if (gdb_con
->busy
) {
3207 /* do not reply this using the O packet */
3211 gdb_output_con(connection
, string
);
3214 static void gdb_sig_halted(struct connection
*connection
)
3217 snprintf(sig_reply
, 4, "T%2.2x", 2);
3218 gdb_put_packet(connection
, sig_reply
, 3);
3221 static int gdb_input_inner(struct connection
*connection
)
3223 /* Do not allocate this on the stack */
3224 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
+ 1]; /* Extra byte for nul-termination */
3226 struct target
*target
;
3227 char const *packet
= gdb_packet_buffer
;
3230 struct gdb_connection
*gdb_con
= connection
->priv
;
3231 static int extended_protocol
;
3233 target
= get_target_from_connection(connection
);
3235 /* drain input buffer. If one of the packets fail, then an error
3236 * packet is replied, if applicable.
3238 * This loop will terminate and the error code is returned.
3240 * The calling fn will check if this error is something that
3241 * can be recovered from, or if the connection must be closed.
3243 * If the error is recoverable, this fn is called again to
3244 * drain the rest of the buffer.
3247 packet_size
= GDB_BUFFER_SIZE
;
3248 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
3249 if (retval
!= ERROR_OK
)
3252 /* terminate with zero */
3253 gdb_packet_buffer
[packet_size
] = '\0';
3255 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
3256 if (packet
[0] == 'X') {
3257 /* binary packets spew junk into the debug log stream */
3260 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
3263 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
3265 LOG_DEBUG("received packet: '%s'", packet
);
3268 if (packet_size
> 0) {
3270 switch (packet
[0]) {
3271 case 'T': /* Is thread alive? */
3272 gdb_thread_packet(connection
, packet
, packet_size
);
3274 case 'H': /* Set current thread ( 'c' for step and continue,
3275 * 'g' for all other operations ) */
3276 gdb_thread_packet(connection
, packet
, packet_size
);
3280 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
3281 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
3282 retval
= gdb_query_packet(connection
, packet
, packet_size
);
3285 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
3288 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
3291 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
3294 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
3297 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
3300 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
3304 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
3307 gdb_last_signal_packet(connection
, packet
, packet_size
);
3312 gdb_thread_packet(connection
, packet
, packet_size
);
3313 log_add_callback(gdb_log_callback
, connection
);
3315 if (gdb_con
->mem_write_error
) {
3316 LOG_ERROR("Memory write failure!");
3318 /* now that we have reported the memory write error,
3319 * we can clear the condition */
3320 gdb_con
->mem_write_error
= false;
3323 bool nostep
= false;
3324 bool already_running
= false;
3325 if (target
->state
== TARGET_RUNNING
) {
3326 LOG_WARNING("WARNING! The target is already running. "
3327 "All changes GDB did to registers will be discarded! "
3328 "Waiting for target to halt.");
3329 already_running
= true;
3330 } else if (target
->state
!= TARGET_HALTED
) {
3331 LOG_WARNING("The target is not in the halted nor running stated, " \
3332 "stepi/continue ignored.");
3334 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
3335 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3336 * sent by GDB first to OpenOCD, thus defeating the check to
3337 * make only the single stepping have the sync feature...
3340 LOG_DEBUG("stepi ignored. GDB will now fetch the register state " \
3341 "from the target.");
3343 gdb_con
->sync
= false;
3345 if (!already_running
&& nostep
) {
3346 /* Either the target isn't in the halted state, then we can't
3347 * step/continue. This might be early setup, etc.
3349 * Or we want to allow GDB to pick up a fresh set of
3350 * register values without modifying the target state.
3353 gdb_sig_halted(connection
);
3355 /* stop forwarding log packets! */
3356 log_remove_callback(gdb_log_callback
, connection
);
3358 /* We're running/stepping, in which case we can
3359 * forward log output until the target is halted
3361 gdb_con
->frontend_state
= TARGET_RUNNING
;
3362 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3364 if (!already_running
) {
3365 /* Here we don't want packet processing to stop even if this fails,
3366 * so we use a local variable instead of retval. */
3367 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
3368 if (retval
!= ERROR_OK
) {
3369 /* we'll never receive a halted
3370 * condition... issue a false one..
3372 gdb_frontend_halted(target
, connection
);
3379 retval
= gdb_v_packet(connection
, packet
, packet_size
);
3382 retval
= gdb_detach(connection
);
3383 extended_protocol
= 0;
3386 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
3387 if (retval
!= ERROR_OK
)
3391 if (extended_protocol
!= 0) {
3392 gdb_con
->attached
= false;
3395 gdb_put_packet(connection
, "OK", 2);
3396 return ERROR_SERVER_REMOTE_CLOSED
;
3398 /* handle extended remote protocol */
3399 extended_protocol
= 1;
3400 gdb_put_packet(connection
, "OK", 2);
3403 /* handle extended restart packet */
3404 gdb_restart_inferior(connection
, packet
, packet_size
);
3408 /* packet supported only by smp target i.e cortex_a.c*/
3409 /* handle smp packet replying coreid played to gbd */
3410 gdb_read_smp_packet(connection
, packet
, packet_size
);
3414 /* packet supported only by smp target i.e cortex_a.c */
3415 /* handle smp packet setting coreid to be played at next
3417 gdb_write_smp_packet(connection
, packet
, packet_size
);
3421 /* File-I/O extension */
3422 /* After gdb uses host-side syscall to complete target file
3423 * I/O, gdb sends host-side syscall return value to target
3425 * The format of 'F' response packet is
3426 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3428 gdb_con
->frontend_state
= TARGET_RUNNING
;
3429 log_add_callback(gdb_log_callback
, connection
);
3430 gdb_fileio_response_packet(connection
, packet
, packet_size
);
3434 /* ignore unknown packets */
3435 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
3436 gdb_put_packet(connection
, "", 0);
3440 /* if a packet handler returned an error, exit input loop */
3441 if (retval
!= ERROR_OK
)
3445 if (gdb_con
->ctrl_c
) {
3446 if (target
->state
== TARGET_RUNNING
) {
3447 struct target
*t
= target
;
3449 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &t
);
3450 retval
= target_halt(t
);
3451 if (retval
== ERROR_OK
)
3452 retval
= target_poll(t
);
3453 if (retval
!= ERROR_OK
)
3454 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3455 gdb_con
->ctrl_c
= 0;
3457 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3458 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3462 } while (gdb_con
->buf_cnt
> 0);
3467 static int gdb_input(struct connection
*connection
)
3469 int retval
= gdb_input_inner(connection
);
3470 struct gdb_connection
*gdb_con
= connection
->priv
;
3471 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3474 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3475 if (gdb_con
->closed
)
3476 return ERROR_SERVER_REMOTE_CLOSED
;
3478 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3482 static int gdb_target_start(struct target
*target
, const char *port
)
3484 struct gdb_service
*gdb_service
;
3486 gdb_service
= malloc(sizeof(struct gdb_service
));
3488 if (NULL
== gdb_service
)
3491 LOG_DEBUG("starting gdb server for %s on %s", target_name(target
), port
);
3493 gdb_service
->target
= target
;
3494 gdb_service
->core
[0] = -1;
3495 gdb_service
->core
[1] = -1;
3496 target
->gdb_service
= gdb_service
;
3498 ret
= add_service("gdb",
3499 port
, 1, &gdb_new_connection
, &gdb_input
,
3500 &gdb_connection_closed
, gdb_service
);
3501 /* initialialize all targets gdb service with the same pointer */
3503 struct target_list
*head
;
3504 struct target
*curr
;
3505 head
= target
->head
;
3506 while (head
!= (struct target_list
*)NULL
) {
3507 curr
= head
->target
;
3509 curr
->gdb_service
= gdb_service
;
3516 static int gdb_target_add_one(struct target
*target
)
3518 /* one gdb instance per smp list */
3519 if ((target
->smp
) && (target
->gdb_service
))
3522 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3523 if (!target_supports_gdb_connection(target
)) {
3524 LOG_DEBUG("skip gdb server for target %s", target_name(target
));
3528 if (target
->gdb_port_override
) {
3529 if (strcmp(target
->gdb_port_override
, "disabled") == 0) {
3530 LOG_INFO("gdb port disabled");
3533 return gdb_target_start(target
, target
->gdb_port_override
);
3536 if (strcmp(gdb_port
, "disabled") == 0) {
3537 LOG_INFO("gdb port disabled");
3541 int retval
= gdb_target_start(target
, gdb_port_next
);
3542 if (retval
== ERROR_OK
) {
3543 /* save the port number so can be queried with
3544 * $target_name cget -gdb-port
3546 target
->gdb_port_override
= strdup(gdb_port_next
);
3549 /* If we can parse the port number
3550 * then we increment the port number for the next target.
3553 portnumber
= strtol(gdb_port_next
, &end
, 0);
3555 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3556 free(gdb_port_next
);
3558 gdb_port_next
= alloc_printf("%d", portnumber
+1);
3560 /* Don't increment if gdb_port is 0, since we're just
3561 * trying to allocate an unused port. */
3562 gdb_port_next
= strdup("0");
3570 int gdb_target_add_all(struct target
*target
)
3572 if (NULL
== target
) {
3573 LOG_WARNING("gdb services need one or more targets defined");
3577 while (NULL
!= target
) {
3578 int retval
= gdb_target_add_one(target
);
3579 if (ERROR_OK
!= retval
)
3582 target
= target
->next
;
3588 COMMAND_HANDLER(handle_gdb_sync_command
)
3591 return ERROR_COMMAND_SYNTAX_ERROR
;
3593 if (current_gdb_connection
== NULL
) {
3595 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3599 current_gdb_connection
->sync
= true;
3604 /* daemon configuration command gdb_port */
3605 COMMAND_HANDLER(handle_gdb_port_command
)
3607 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3608 if (ERROR_OK
== retval
) {
3609 free(gdb_port_next
);
3610 gdb_port_next
= strdup(gdb_port
);
3615 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3618 return ERROR_COMMAND_SYNTAX_ERROR
;
3620 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3624 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3627 return ERROR_COMMAND_SYNTAX_ERROR
;
3629 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3633 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3636 return ERROR_COMMAND_SYNTAX_ERROR
;
3638 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3642 COMMAND_HANDLER(handle_gdb_report_register_access_error
)
3645 return ERROR_COMMAND_SYNTAX_ERROR
;
3647 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_register_access_error
);
3651 /* gdb_breakpoint_override */
3652 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3654 if (CMD_ARGC
== 0) {
3656 } else if (CMD_ARGC
== 1) {
3657 gdb_breakpoint_override
= 1;
3658 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3659 gdb_breakpoint_override_type
= BKPT_HARD
;
3660 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3661 gdb_breakpoint_override_type
= BKPT_SOFT
;
3662 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3663 gdb_breakpoint_override
= 0;
3665 return ERROR_COMMAND_SYNTAX_ERROR
;
3666 if (gdb_breakpoint_override
)
3667 LOG_USER("force %s breakpoints",
3668 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3670 LOG_USER("breakpoint type is not overridden");
3675 COMMAND_HANDLER(handle_gdb_target_description_command
)
3678 return ERROR_COMMAND_SYNTAX_ERROR
;
3680 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3684 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3687 uint32_t tdesc_length
;
3688 struct target
*target
= get_current_target(CMD_CTX
);
3690 int retval
= gdb_generate_target_description(target
, &tdesc
);
3691 if (retval
!= ERROR_OK
) {
3692 LOG_ERROR("Unable to Generate Target Description");
3696 tdesc_length
= strlen(tdesc
);
3698 struct fileio
*fileio
;
3699 size_t size_written
;
3701 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3702 if (tdesc_filename
== NULL
) {
3703 retval
= ERROR_FAIL
;
3707 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
3709 if (retval
!= ERROR_OK
) {
3710 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
3714 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
3716 fileio_close(fileio
);
3718 if (retval
!= ERROR_OK
)
3719 LOG_ERROR("Error while writing the tdesc file");
3722 free(tdesc_filename
);
3728 static const struct command_registration gdb_command_handlers
[] = {
3731 .handler
= handle_gdb_sync_command
,
3732 .mode
= COMMAND_ANY
,
3733 .help
= "next stepi will return immediately allowing "
3734 "GDB to fetch register state without affecting "
3740 .handler
= handle_gdb_port_command
,
3741 .mode
= COMMAND_ANY
,
3742 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
3743 "server listens for the next port number after the "
3744 "base port number specified. "
3745 "No arguments reports GDB port. \"pipe\" means listen to stdin "
3746 "output to stdout, an integer is base port number, \"disabled\" disables "
3747 "port. Any other string is are interpreted as named pipe to listen to. "
3748 "Output pipe is the same name as input pipe, but with 'o' appended.",
3749 .usage
= "[port_num]",
3752 .name
= "gdb_memory_map",
3753 .handler
= handle_gdb_memory_map_command
,
3754 .mode
= COMMAND_CONFIG
,
3755 .help
= "enable or disable memory map",
3756 .usage
= "('enable'|'disable')"
3759 .name
= "gdb_flash_program",
3760 .handler
= handle_gdb_flash_program_command
,
3761 .mode
= COMMAND_CONFIG
,
3762 .help
= "enable or disable flash program",
3763 .usage
= "('enable'|'disable')"
3766 .name
= "gdb_report_data_abort",
3767 .handler
= handle_gdb_report_data_abort_command
,
3768 .mode
= COMMAND_CONFIG
,
3769 .help
= "enable or disable reporting data aborts",
3770 .usage
= "('enable'|'disable')"
3773 .name
= "gdb_report_register_access_error",
3774 .handler
= handle_gdb_report_register_access_error
,
3775 .mode
= COMMAND_CONFIG
,
3776 .help
= "enable or disable reporting register access errors",
3777 .usage
= "('enable'|'disable')"
3780 .name
= "gdb_breakpoint_override",
3781 .handler
= handle_gdb_breakpoint_override_command
,
3782 .mode
= COMMAND_ANY
,
3783 .help
= "Display or specify type of breakpoint "
3784 "to be used by gdb 'break' commands.",
3785 .usage
= "('hard'|'soft'|'disable')"
3788 .name
= "gdb_target_description",
3789 .handler
= handle_gdb_target_description_command
,
3790 .mode
= COMMAND_CONFIG
,
3791 .help
= "enable or disable target description",
3792 .usage
= "('enable'|'disable')"
3795 .name
= "gdb_save_tdesc",
3796 .handler
= handle_gdb_save_tdesc_command
,
3797 .mode
= COMMAND_EXEC
,
3798 .help
= "Save the target description file",
3801 COMMAND_REGISTRATION_DONE
3804 int gdb_register_commands(struct command_context
*cmd_ctx
)
3806 gdb_port
= strdup("3333");
3807 gdb_port_next
= strdup("3333");
3808 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);
3811 void gdb_service_free(void)
3814 free(gdb_port_next
);