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>
47 #include <flash/nor/core.h>
48 #include "gdb_server.h"
49 #include <target/image.h>
50 #include <jtag/jtag.h>
51 #include "rtos/rtos.h"
52 #include "target/smp.h"
56 * GDB server implementation.
58 * This implements the GDB Remote Serial Protocol, over TCP connections,
59 * giving GDB access to the JTAG or other hardware debugging facilities
60 * found in most modern embedded processors.
63 struct target_desc_format
{
65 uint32_t tdesc_length
;
68 /* private connection data for GDB */
69 struct gdb_connection
{
70 char buffer
[GDB_BUFFER_SIZE
];
74 enum target_state frontend_state
;
75 struct image
*vflash_image
;
79 /* set flag to true if you want the next stepi to return immediately.
80 * allowing GDB to pick up a fresh set of register values from the target
81 * without modifying the target state. */
83 /* We delay reporting memory write errors until next step/continue or memory
84 * write. This improves performance of gdb load significantly as the GDB packet
85 * can be replied immediately and a new GDB packet will be ready without delay
86 * (ca. 10% or so...). */
88 /* with extended-remote it seems we need to better emulate attach/detach.
89 * what this means is we reply with a W stop reply after a kill packet,
90 * normally we reply with a S reply via gdb_last_signal_packet.
91 * as a side note this behaviour only effects gdb > 6.8 */
93 /* temporarily used for target description support */
94 struct target_desc_format target_desc
;
95 /* temporarily used for thread list support */
100 #define _DEBUG_GDB_IO_
103 static struct gdb_connection
*current_gdb_connection
;
105 static int gdb_breakpoint_override
;
106 static enum breakpoint_type gdb_breakpoint_override_type
;
108 static int gdb_error(struct connection
*connection
, int retval
);
109 static char *gdb_port
;
110 static char *gdb_port_next
;
112 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
113 const char *function
, const char *string
);
115 static void gdb_sig_halted(struct connection
*connection
);
117 /* number of gdb connections, mainly to suppress gdb related debugging spam
118 * in helper/log.c when no gdb connections are actually active */
119 int gdb_actual_connections
;
121 /* set if we are sending a memory map to gdb
122 * via qXfer:memory-map:read packet */
123 /* enabled by default*/
124 static int gdb_use_memory_map
= 1;
125 /* enabled by default*/
126 static int gdb_flash_program
= 1;
128 /* if set, data aborts cause an error to be reported in memory read packets
129 * see the code in gdb_read_memory_packet() for further explanations.
130 * Disabled by default.
132 static int gdb_report_data_abort
;
133 /* If set, errors when accessing registers are reported to gdb. Disabled by
135 static int gdb_report_register_access_error
;
137 /* set if we are sending target descriptions to gdb
138 * via qXfer:features:read packet */
139 /* enabled by default */
140 static int gdb_use_target_description
= 1;
142 /* current processing free-run type, used by file-I/O */
143 static char gdb_running_type
;
145 static int gdb_last_signal(struct target
*target
)
147 switch (target
->debug_reason
) {
148 case DBG_REASON_DBGRQ
:
149 return 0x2; /* SIGINT */
150 case DBG_REASON_BREAKPOINT
:
151 case DBG_REASON_WATCHPOINT
:
152 case DBG_REASON_WPTANDBKPT
:
153 return 0x05; /* SIGTRAP */
154 case DBG_REASON_SINGLESTEP
:
155 return 0x05; /* SIGTRAP */
156 case DBG_REASON_NOTHALTED
:
157 return 0x0; /* no signal... shouldn't happen */
159 LOG_USER("undefined debug reason %d - target needs reset",
160 target
->debug_reason
);
165 static int check_pending(struct connection
*connection
,
166 int timeout_s
, int *got_data
)
168 /* a non-blocking socket will block if there is 0 bytes available on the socket,
169 * but return with as many bytes as are available immediately
173 struct gdb_connection
*gdb_con
= connection
->priv
;
175 if (got_data
== NULL
)
179 if (gdb_con
->buf_cnt
> 0) {
185 FD_SET(connection
->fd
, &read_fds
);
187 tv
.tv_sec
= timeout_s
;
189 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
190 /* This can typically be because a "monitor" command took too long
191 * before printing any progress messages
194 return ERROR_GDB_TIMEOUT
;
198 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
202 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
204 struct gdb_connection
*gdb_con
= connection
->priv
;
205 int retval
= ERROR_OK
;
207 #ifdef _DEBUG_GDB_IO_
211 if (connection
->service
->type
!= CONNECTION_TCP
)
212 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
214 retval
= check_pending(connection
, 1, NULL
);
215 if (retval
!= ERROR_OK
)
217 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
222 if (gdb_con
->buf_cnt
> 0)
224 if (gdb_con
->buf_cnt
== 0) {
225 gdb_con
->closed
= true;
226 return ERROR_SERVER_REMOTE_CLOSED
;
230 errno
= WSAGetLastError();
236 case WSAECONNABORTED
:
237 gdb_con
->closed
= true;
238 return ERROR_SERVER_REMOTE_CLOSED
;
240 gdb_con
->closed
= true;
241 return ERROR_SERVER_REMOTE_CLOSED
;
243 LOG_ERROR("read: %d", errno
);
252 gdb_con
->closed
= true;
253 return ERROR_SERVER_REMOTE_CLOSED
;
255 gdb_con
->closed
= true;
256 return ERROR_SERVER_REMOTE_CLOSED
;
258 LOG_ERROR("read: %s", strerror(errno
));
259 gdb_con
->closed
= true;
260 return ERROR_SERVER_REMOTE_CLOSED
;
265 #ifdef _DEBUG_GDB_IO_
266 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
267 LOG_DEBUG("received '%s'", debug_buffer
);
271 gdb_con
->buf_p
= gdb_con
->buffer
;
273 *next_char
= *(gdb_con
->buf_p
++);
274 if (gdb_con
->buf_cnt
> 0)
275 connection
->input_pending
= 1;
277 connection
->input_pending
= 0;
278 #ifdef _DEBUG_GDB_IO_
279 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
286 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
287 * held in registers in the inner loop.
289 * For small caches and embedded systems this is important!
291 static inline int gdb_get_char_fast(struct connection
*connection
,
292 int *next_char
, char **buf_p
, int *buf_cnt
)
294 int retval
= ERROR_OK
;
296 if ((*buf_cnt
)-- > 0) {
297 *next_char
= **buf_p
;
300 connection
->input_pending
= 1;
302 connection
->input_pending
= 0;
304 #ifdef _DEBUG_GDB_IO_
305 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
311 struct gdb_connection
*gdb_con
= connection
->priv
;
312 gdb_con
->buf_p
= *buf_p
;
313 gdb_con
->buf_cnt
= *buf_cnt
;
314 retval
= gdb_get_char_inner(connection
, next_char
);
315 *buf_p
= gdb_con
->buf_p
;
316 *buf_cnt
= gdb_con
->buf_cnt
;
321 static int gdb_get_char(struct connection
*connection
, int *next_char
)
323 struct gdb_connection
*gdb_con
= connection
->priv
;
324 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
327 static int gdb_putback_char(struct connection
*connection
, int last_char
)
329 struct gdb_connection
*gdb_con
= connection
->priv
;
331 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
332 *(--gdb_con
->buf_p
) = last_char
;
335 LOG_ERROR("BUG: couldn't put character back");
340 /* The only way we can detect that the socket is closed is the first time
341 * we write to it, we will fail. Subsequent write operations will
342 * succeed. Shudder! */
343 static int gdb_write(struct connection
*connection
, void *data
, int len
)
345 struct gdb_connection
*gdb_con
= connection
->priv
;
347 return ERROR_SERVER_REMOTE_CLOSED
;
349 if (connection_write(connection
, data
, len
) == len
)
351 gdb_con
->closed
= true;
352 return ERROR_SERVER_REMOTE_CLOSED
;
355 static int gdb_put_packet_inner(struct connection
*connection
,
356 char *buffer
, int len
)
359 unsigned char my_checksum
= 0;
360 #ifdef _DEBUG_GDB_IO_
365 struct gdb_connection
*gdb_con
= connection
->priv
;
367 for (i
= 0; i
< len
; i
++)
368 my_checksum
+= buffer
[i
];
370 #ifdef _DEBUG_GDB_IO_
372 * At this point we should have nothing in the input queue from GDB,
373 * however sometimes '-' is sent even though we've already received
374 * an ACK (+) for everything we've sent off.
378 retval
= check_pending(connection
, 0, &gotdata
);
379 if (retval
!= ERROR_OK
)
383 retval
= gdb_get_char(connection
, &reply
);
384 if (retval
!= ERROR_OK
)
387 /* fix a problem with some IAR tools */
388 gdb_putback_char(connection
, reply
);
389 LOG_DEBUG("Unexpected start of new packet");
393 LOG_WARNING("Discard unexpected char %c", reply
);
398 #ifdef _DEBUG_GDB_IO_
399 debug_buffer
= strndup(buffer
, len
);
400 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
404 char local_buffer
[1024];
405 local_buffer
[0] = '$';
406 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
407 /* performance gain on smaller packets by only a single call to gdb_write() */
408 memcpy(local_buffer
+ 1, buffer
, len
++);
409 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
410 retval
= gdb_write(connection
, local_buffer
, len
);
411 if (retval
!= ERROR_OK
)
414 /* larger packets are transmitted directly from caller supplied buffer
415 * by several calls to gdb_write() to avoid dynamic allocation */
416 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
417 retval
= gdb_write(connection
, local_buffer
, 1);
418 if (retval
!= ERROR_OK
)
420 retval
= gdb_write(connection
, buffer
, len
);
421 if (retval
!= ERROR_OK
)
423 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
424 if (retval
!= ERROR_OK
)
428 if (gdb_con
->noack_mode
)
431 retval
= gdb_get_char(connection
, &reply
);
432 if (retval
!= ERROR_OK
)
437 else if (reply
== '-') {
438 /* Stop sending output packets for now */
439 log_remove_callback(gdb_log_callback
, connection
);
440 LOG_WARNING("negative reply, retrying");
441 } else if (reply
== 0x3) {
443 retval
= gdb_get_char(connection
, &reply
);
444 if (retval
!= ERROR_OK
)
448 else if (reply
== '-') {
449 /* Stop sending output packets for now */
450 log_remove_callback(gdb_log_callback
, connection
);
451 LOG_WARNING("negative reply, retrying");
452 } else if (reply
== '$') {
453 LOG_ERROR("GDB missing ack(1) - assumed good");
454 gdb_putback_char(connection
, reply
);
457 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
458 gdb_con
->closed
= true;
459 return ERROR_SERVER_REMOTE_CLOSED
;
461 } else if (reply
== '$') {
462 LOG_ERROR("GDB missing ack(2) - assumed good");
463 gdb_putback_char(connection
, reply
);
466 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
468 gdb_con
->closed
= true;
469 return ERROR_SERVER_REMOTE_CLOSED
;
473 return ERROR_SERVER_REMOTE_CLOSED
;
478 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
480 struct gdb_connection
*gdb_con
= connection
->priv
;
481 gdb_con
->busy
= true;
482 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
483 gdb_con
->busy
= false;
485 /* we sent some data, reset timer for keep alive messages */
491 static inline int fetch_packet(struct connection
*connection
,
492 int *checksum_ok
, int noack
, int *len
, char *buffer
)
494 unsigned char my_checksum
= 0;
497 int retval
= ERROR_OK
;
499 struct gdb_connection
*gdb_con
= connection
->priv
;
504 /* move this over into local variables to use registers and give the
505 * more freedom to optimize */
506 char *buf_p
= gdb_con
->buf_p
;
507 int buf_cnt
= gdb_con
->buf_cnt
;
510 /* The common case is that we have an entire packet with no escape chars.
511 * We need to leave at least 2 bytes in the buffer to have
512 * gdb_get_char() update various bits and bobs correctly.
514 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
515 /* The compiler will struggle a bit with constant propagation and
516 * aliasing, so we help it by showing that these values do not
517 * change inside the loop
521 int run
= buf_cnt
- 2;
527 if (character
== '#') {
528 /* Danger! character can be '#' when esc is
529 * used so we need an explicit boolean for done here. */
534 if (character
== '}') {
535 /* data transmitted in binary mode (X packet)
536 * uses 0x7d as escape character */
537 my_checksum
+= character
& 0xff;
540 my_checksum
+= character
& 0xff;
541 buffer
[count
++] = (character
^ 0x20) & 0xff;
543 my_checksum
+= character
& 0xff;
544 buffer
[count
++] = character
& 0xff;
553 LOG_ERROR("packet buffer too small");
554 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
558 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
559 if (retval
!= ERROR_OK
)
562 if (character
== '#')
565 if (character
== '}') {
566 /* data transmitted in binary mode (X packet)
567 * uses 0x7d as escape character */
568 my_checksum
+= character
& 0xff;
570 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
571 if (retval
!= ERROR_OK
)
574 my_checksum
+= character
& 0xff;
575 buffer
[count
++] = (character
^ 0x20) & 0xff;
577 my_checksum
+= character
& 0xff;
578 buffer
[count
++] = character
& 0xff;
582 gdb_con
->buf_p
= buf_p
;
583 gdb_con
->buf_cnt
= buf_cnt
;
585 if (retval
!= ERROR_OK
)
590 retval
= gdb_get_char(connection
, &character
);
591 if (retval
!= ERROR_OK
)
593 checksum
[0] = character
;
594 retval
= gdb_get_char(connection
, &character
);
595 if (retval
!= ERROR_OK
)
597 checksum
[1] = character
;
601 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
606 static int gdb_get_packet_inner(struct connection
*connection
,
607 char *buffer
, int *len
)
611 struct gdb_connection
*gdb_con
= connection
->priv
;
615 retval
= gdb_get_char(connection
, &character
);
616 if (retval
!= ERROR_OK
)
619 #ifdef _DEBUG_GDB_IO_
620 LOG_DEBUG("character: '%c'", character
);
627 /* According to the GDB documentation
628 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
629 * "gdb sends a final `+` acknowledgment of the stub's `OK`
630 * response, which can be safely ignored by the stub."
631 * However OpenOCD server already is in noack mode at this
632 * point and instead of ignoring this it was emitting a
633 * warning. This code makes server ignore the first ACK
634 * that will be received after going into noack mode,
635 * warning only about subsequent ACK's. */
636 if (gdb_con
->noack_mode
> 1) {
637 LOG_WARNING("acknowledgment received, but no packet pending");
638 } else if (gdb_con
->noack_mode
) {
639 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
640 gdb_con
->noack_mode
= 2;
644 LOG_WARNING("negative acknowledgment, but no packet pending");
651 LOG_WARNING("ignoring character 0x%x", character
);
654 } while (character
!= '$');
657 /* explicit code expansion here to get faster inlined code in -O3 by not
658 * calculating checksum */
659 if (gdb_con
->noack_mode
) {
660 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
661 if (retval
!= ERROR_OK
)
664 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
665 if (retval
!= ERROR_OK
)
669 if (gdb_con
->noack_mode
) {
670 /* checksum is not checked in noack mode */
674 retval
= gdb_write(connection
, "+", 1);
675 if (retval
!= ERROR_OK
)
681 return ERROR_SERVER_REMOTE_CLOSED
;
686 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
688 struct gdb_connection
*gdb_con
= connection
->priv
;
689 gdb_con
->busy
= true;
690 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
691 gdb_con
->busy
= false;
695 static int gdb_output_con(struct connection
*connection
, const char *line
)
700 bin_size
= strlen(line
);
702 hex_buffer
= malloc(bin_size
* 2 + 2);
703 if (hex_buffer
== NULL
)
704 return ERROR_GDB_BUFFER_TOO_SMALL
;
707 size_t pkt_len
= hexify(hex_buffer
+ 1, (const uint8_t *)line
, bin_size
,
709 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
715 static int gdb_output(struct command_context
*context
, const char *line
)
717 /* this will be dumped to the log and also sent as an O packet if possible */
718 LOG_USER_N("%s", line
);
722 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
724 struct gdb_connection
*gdb_connection
= connection
->priv
;
726 char stop_reason
[20];
727 char current_thread
[25];
731 rtos_update_threads(target
);
733 if (target
->debug_reason
== DBG_REASON_EXIT
) {
734 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
736 if (gdb_connection
->ctrl_c
) {
739 signal_var
= gdb_last_signal(target
);
741 stop_reason
[0] = '\0';
742 if (target
->debug_reason
== DBG_REASON_WATCHPOINT
) {
743 enum watchpoint_rw hit_wp_type
;
744 target_addr_t hit_wp_address
;
746 if (watchpoint_hit(target
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
748 switch (hit_wp_type
) {
750 snprintf(stop_reason
, sizeof(stop_reason
),
751 "watch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
754 snprintf(stop_reason
, sizeof(stop_reason
),
755 "rwatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
758 snprintf(stop_reason
, sizeof(stop_reason
),
759 "awatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
767 current_thread
[0] = '\0';
768 if (target
->rtos
!= NULL
) {
770 snprintf(current_thread
, sizeof(current_thread
), "thread:%016" PRIx64
";",
771 target
->rtos
->current_thread
);
772 target
->rtos
->current_threadid
= target
->rtos
->current_thread
;
773 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &ct
);
774 if (!gdb_connection
->ctrl_c
)
775 signal_var
= gdb_last_signal(ct
);
778 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s%s",
779 signal_var
, stop_reason
, current_thread
);
781 gdb_connection
->ctrl_c
= 0;
784 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
785 gdb_connection
->frontend_state
= TARGET_HALTED
;
788 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
790 struct gdb_connection
*gdb_connection
= connection
->priv
;
791 char fileio_command
[256];
793 bool program_exited
= false;
795 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
796 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
797 target
->fileio_info
->param_1
,
798 target
->fileio_info
->param_2
,
799 target
->fileio_info
->param_3
,
800 target
->fileio_info
->param_4
);
801 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
802 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
803 target
->fileio_info
->param_1
);
804 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
805 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
806 target
->fileio_info
->param_1
,
807 target
->fileio_info
->param_2
,
808 target
->fileio_info
->param_3
);
809 else if (strcmp(target
->fileio_info
->identifier
, "write") == 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
, "lseek") == 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
, "rename") == 0)
820 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" 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 target
->fileio_info
->param_4
);
825 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
826 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
827 target
->fileio_info
->param_1
,
828 target
->fileio_info
->param_2
);
829 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
830 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
831 target
->fileio_info
->param_1
,
832 target
->fileio_info
->param_2
,
833 target
->fileio_info
->param_3
);
834 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
835 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
836 target
->fileio_info
->param_1
,
837 target
->fileio_info
->param_2
);
838 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
839 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
840 target
->fileio_info
->param_1
,
841 target
->fileio_info
->param_2
);
842 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
843 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
844 target
->fileio_info
->param_1
);
845 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
846 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
847 target
->fileio_info
->param_1
,
848 target
->fileio_info
->param_2
);
849 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
850 /* If target hits exit syscall, report to GDB the program is terminated.
851 * In addition, let target run its own exit syscall handler. */
852 program_exited
= true;
853 sprintf(fileio_command
, "W%02" PRIx64
, target
->fileio_info
->param_1
);
855 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
857 /* encounter unknown syscall, continue */
858 gdb_connection
->frontend_state
= TARGET_RUNNING
;
859 target_resume(target
, 1, 0x0, 0, 0);
863 command_len
= strlen(fileio_command
);
864 gdb_put_packet(connection
, fileio_command
, command_len
);
866 if (program_exited
) {
867 /* Use target_resume() to let target run its own exit syscall handler. */
868 gdb_connection
->frontend_state
= TARGET_RUNNING
;
869 target_resume(target
, 1, 0x0, 0, 0);
871 gdb_connection
->frontend_state
= TARGET_HALTED
;
872 rtos_update_threads(target
);
876 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
878 struct gdb_connection
*gdb_connection
= connection
->priv
;
880 /* In the GDB protocol when we are stepping or continuing execution,
881 * we have a lingering reply. Upon receiving a halted event
882 * when we have that lingering packet, we reply to the original
883 * step or continue packet.
885 * Executing monitor commands can bring the target in and
886 * out of the running state so we'll see lots of TARGET_EVENT_XXX
887 * that are to be ignored.
889 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
890 /* stop forwarding log packets! */
891 log_remove_callback(gdb_log_callback
, connection
);
893 /* check fileio first */
894 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
895 gdb_fileio_reply(target
, connection
);
897 gdb_signal_reply(target
, connection
);
901 static int gdb_target_callback_event_handler(struct target
*target
,
902 enum target_event event
, void *priv
)
905 struct connection
*connection
= priv
;
906 struct gdb_service
*gdb_service
= connection
->service
->priv
;
908 if (gdb_service
->target
!= target
)
912 case TARGET_EVENT_GDB_HALT
:
913 gdb_frontend_halted(target
, connection
);
915 case TARGET_EVENT_HALTED
:
916 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
918 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
919 retval
= jtag_execute_queue();
920 if (retval
!= ERROR_OK
)
930 static int gdb_new_connection(struct connection
*connection
)
932 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
933 struct target
*target
;
937 target
= get_target_from_connection(connection
);
938 connection
->priv
= gdb_connection
;
939 connection
->cmd_ctx
->current_target
= target
;
941 /* initialize gdb connection information */
942 gdb_connection
->buf_p
= gdb_connection
->buffer
;
943 gdb_connection
->buf_cnt
= 0;
944 gdb_connection
->ctrl_c
= 0;
945 gdb_connection
->frontend_state
= TARGET_HALTED
;
946 gdb_connection
->vflash_image
= NULL
;
947 gdb_connection
->closed
= false;
948 gdb_connection
->busy
= false;
949 gdb_connection
->noack_mode
= 0;
950 gdb_connection
->sync
= false;
951 gdb_connection
->mem_write_error
= false;
952 gdb_connection
->attached
= true;
953 gdb_connection
->target_desc
.tdesc
= NULL
;
954 gdb_connection
->target_desc
.tdesc_length
= 0;
955 gdb_connection
->thread_list
= NULL
;
957 /* send ACK to GDB for debug request */
958 gdb_write(connection
, "+", 1);
960 /* output goes through gdb connection */
961 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
963 /* we must remove all breakpoints registered to the target as a previous
964 * GDB session could leave dangling breakpoints if e.g. communication
967 breakpoint_clear_target(target
);
968 watchpoint_clear_target(target
);
971 /* clean previous rtos session if supported*/
972 if (target
->rtos
->type
->clean
)
973 target
->rtos
->type
->clean(target
);
976 rtos_update_threads(target
);
979 /* remove the initial ACK from the incoming buffer */
980 retval
= gdb_get_char(connection
, &initial_ack
);
981 if (retval
!= ERROR_OK
)
984 /* FIX!!!??? would we actually ever receive a + here???
987 if (initial_ack
!= '+')
988 gdb_putback_char(connection
, initial_ack
);
989 target_call_event_callbacks(target
, TARGET_EVENT_GDB_ATTACH
);
991 if (gdb_use_memory_map
) {
992 /* Connect must fail if the memory map can't be set up correctly.
994 * This will cause an auto_probe to be invoked, which is either
995 * a no-op or it will fail when the target isn't ready(e.g. not halted).
998 for (i
= 0; i
< flash_get_bank_count(); i
++) {
999 struct flash_bank
*p
;
1000 p
= get_flash_bank_by_num_noprobe(i
);
1001 if (p
->target
!= target
)
1003 retval
= get_flash_bank_by_num(i
, &p
);
1004 if (retval
!= ERROR_OK
) {
1005 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
1006 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1012 gdb_actual_connections
++;
1013 log_printf_lf(all_targets
->next
!= NULL
? LOG_LVL_INFO
: LOG_LVL_DEBUG
,
1014 __FILE__
, __LINE__
, __func__
,
1015 "New GDB Connection: %d, Target %s, state: %s",
1016 gdb_actual_connections
,
1017 target_name(target
),
1018 target_state_name(target
));
1020 /* DANGER! If we fail subsequently, we must remove this handler,
1021 * otherwise we occasionally see crashes as the timer can invoke the
1024 * register callback to be informed about target events */
1025 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1030 static int gdb_connection_closed(struct connection
*connection
)
1032 struct target
*target
;
1033 struct gdb_connection
*gdb_connection
= connection
->priv
;
1035 target
= get_target_from_connection(connection
);
1037 /* we're done forwarding messages. Tear down callback before
1038 * cleaning up connection.
1040 log_remove_callback(gdb_log_callback
, connection
);
1042 gdb_actual_connections
--;
1043 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1044 target_name(target
),
1045 target_state_name(target
),
1046 gdb_actual_connections
);
1048 /* see if an image built with vFlash commands is left */
1049 if (gdb_connection
->vflash_image
) {
1050 image_close(gdb_connection
->vflash_image
);
1051 free(gdb_connection
->vflash_image
);
1052 gdb_connection
->vflash_image
= NULL
;
1055 /* if this connection registered a debug-message receiver delete it */
1056 delete_debug_msg_receiver(connection
->cmd_ctx
, target
);
1058 if (connection
->priv
) {
1059 free(connection
->priv
);
1060 connection
->priv
= NULL
;
1062 LOG_ERROR("BUG: connection->priv == NULL");
1064 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1066 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
1068 target_call_event_callbacks(target
, TARGET_EVENT_GDB_DETACH
);
1073 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1076 snprintf(err
, 4, "E%2.2X", the_error
);
1077 gdb_put_packet(connection
, err
, 3);
1080 static int gdb_last_signal_packet(struct connection
*connection
,
1081 char const *packet
, int packet_size
)
1083 struct target
*target
= get_target_from_connection(connection
);
1084 struct gdb_connection
*gdb_con
= connection
->priv
;
1088 if (!gdb_con
->attached
) {
1089 /* if we are here we have received a kill packet
1090 * reply W stop reply otherwise gdb gets very unhappy */
1091 gdb_put_packet(connection
, "W00", 3);
1095 signal_var
= gdb_last_signal(target
);
1097 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1098 gdb_put_packet(connection
, sig_reply
, 3);
1103 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1105 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1108 return len
- 1 - pos
;
1111 /* Convert register to string of bytes. NB! The # of bits in the
1112 * register might be non-divisible by 8(a byte), in which
1113 * case an entire byte is shown.
1115 * NB! the format on the wire is the target endianness
1117 * The format of reg->value is little endian
1120 static void gdb_str_to_target(struct target
*target
,
1121 char *tstr
, struct reg
*reg
)
1128 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1130 for (i
= 0; i
< buf_len
; i
++) {
1131 int j
= gdb_reg_pos(target
, i
, buf_len
);
1132 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1136 /* copy over in register buffer */
1137 static void gdb_target_to_reg(struct target
*target
,
1138 char const *tstr
, int str_len
, uint8_t *bin
)
1141 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1146 for (i
= 0; i
< str_len
; i
+= 2) {
1148 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1149 LOG_ERROR("BUG: unable to convert register value");
1153 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1158 static int gdb_get_registers_packet(struct connection
*connection
,
1159 char const *packet
, int packet_size
)
1161 struct target
*target
= get_target_from_connection(connection
);
1162 struct reg
**reg_list
;
1165 int reg_packet_size
= 0;
1170 #ifdef _DEBUG_GDB_IO_
1174 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1177 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1179 if (retval
!= ERROR_OK
)
1180 return gdb_error(connection
, retval
);
1182 for (i
= 0; i
< reg_list_size
; i
++) {
1183 if (reg_list
[i
] == NULL
|| reg_list
[i
]->exist
== false)
1185 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1188 assert(reg_packet_size
> 0);
1190 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1191 if (reg_packet
== NULL
)
1194 reg_packet_p
= reg_packet
;
1196 for (i
= 0; i
< reg_list_size
; i
++) {
1197 if (reg_list
[i
] == NULL
|| reg_list
[i
]->exist
== false)
1199 if (!reg_list
[i
]->valid
) {
1200 retval
= reg_list
[i
]->type
->get(reg_list
[i
]);
1201 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1202 LOG_DEBUG("Couldn't get register %s.", reg_list
[i
]->name
);
1205 return gdb_error(connection
, retval
);
1208 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1209 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1212 #ifdef _DEBUG_GDB_IO_
1214 char *reg_packet_p_debug
;
1215 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1216 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1217 free(reg_packet_p_debug
);
1221 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1229 static int gdb_set_registers_packet(struct connection
*connection
,
1230 char const *packet
, int packet_size
)
1232 struct target
*target
= get_target_from_connection(connection
);
1234 struct reg
**reg_list
;
1237 char const *packet_p
;
1239 #ifdef _DEBUG_GDB_IO_
1243 /* skip command character */
1247 if (packet_size
% 2) {
1248 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1249 return ERROR_SERVER_REMOTE_CLOSED
;
1252 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1254 if (retval
!= ERROR_OK
)
1255 return gdb_error(connection
, retval
);
1258 for (i
= 0; i
< reg_list_size
; i
++) {
1260 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1262 if (packet_p
+ chars
> packet
+ packet_size
)
1263 LOG_ERROR("BUG: register packet is too small for registers");
1265 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1266 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1268 retval
= reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1269 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1270 LOG_DEBUG("Couldn't set register %s.", reg_list
[i
]->name
);
1273 return gdb_error(connection
, retval
);
1276 /* advance packet pointer */
1282 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1285 gdb_put_packet(connection
, "OK", 2);
1290 static int gdb_get_register_packet(struct connection
*connection
,
1291 char const *packet
, int packet_size
)
1293 struct target
*target
= get_target_from_connection(connection
);
1295 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1296 struct reg
**reg_list
;
1300 #ifdef _DEBUG_GDB_IO_
1304 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg(connection
, reg_num
)))
1307 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1309 if (retval
!= ERROR_OK
)
1310 return gdb_error(connection
, retval
);
1312 if (reg_list_size
<= reg_num
) {
1313 LOG_ERROR("gdb requested a non-existing register");
1314 return ERROR_SERVER_REMOTE_CLOSED
;
1317 if (!reg_list
[reg_num
]->valid
) {
1318 retval
= reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1319 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1320 LOG_DEBUG("Couldn't get register %s.", reg_list
[reg_num
]->name
);
1322 return gdb_error(connection
, retval
);
1326 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1); /* plus one for string termination null */
1328 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1330 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1338 static int gdb_set_register_packet(struct connection
*connection
,
1339 char const *packet
, int packet_size
)
1341 struct target
*target
= get_target_from_connection(connection
);
1344 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1345 struct reg
**reg_list
;
1351 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1353 if (retval
!= ERROR_OK
)
1354 return gdb_error(connection
, retval
);
1356 if (reg_list_size
<= reg_num
) {
1357 LOG_ERROR("gdb requested a non-existing register");
1358 return ERROR_SERVER_REMOTE_CLOSED
;
1361 if (*separator
!= '=') {
1362 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1363 return ERROR_SERVER_REMOTE_CLOSED
;
1366 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1367 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1368 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1370 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1371 LOG_ERROR("gdb sent %zu bits for a %d-bit register (%s)",
1372 strlen(separator
+ 1) * 4, chars
* 4, reg_list
[reg_num
]->name
);
1374 return ERROR_SERVER_REMOTE_CLOSED
;
1377 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1379 retval
= reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1380 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1381 LOG_DEBUG("Couldn't set register %s.", reg_list
[reg_num
]->name
);
1384 return gdb_error(connection
, retval
);
1387 gdb_put_packet(connection
, "OK", 2);
1395 /* No attempt is made to translate the "retval" to
1396 * GDB speak. This has to be done at the calling
1397 * site as no mapping really exists.
1399 static int gdb_error(struct connection
*connection
, int retval
)
1401 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1402 gdb_send_error(connection
, EFAULT
);
1406 /* We don't have to worry about the default 2 second timeout for GDB packets,
1407 * because GDB breaks up large memory reads into smaller reads.
1409 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1411 static int gdb_read_memory_packet(struct connection
*connection
,
1412 char const *packet
, int packet_size
)
1414 struct target
*target
= get_target_from_connection(connection
);
1422 int retval
= ERROR_OK
;
1424 /* skip command character */
1427 addr
= strtoull(packet
, &separator
, 16);
1429 if (*separator
!= ',') {
1430 LOG_ERROR("incomplete read memory packet received, dropping connection");
1431 return ERROR_SERVER_REMOTE_CLOSED
;
1434 len
= strtoul(separator
+ 1, NULL
, 16);
1437 LOG_WARNING("invalid read memory packet received (len == 0)");
1438 gdb_put_packet(connection
, NULL
, 0);
1442 buffer
= malloc(len
);
1444 LOG_DEBUG("addr: 0x%16.16" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1446 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1448 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1449 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1450 * At some point this might be fixed in GDB, in which case this code can be removed.
1452 * OpenOCD developers are acutely aware of this problem, but there is nothing
1453 * gained by involving the user in this problem that hopefully will get resolved
1456 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1457 * cmd = view%20audit-trail&database = gdb&pr = 2395
1459 * For now, the default is to fix up things to make current GDB versions work.
1460 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1462 memset(buffer
, 0, len
);
1466 if (retval
== ERROR_OK
) {
1467 hex_buffer
= malloc(len
* 2 + 1);
1469 size_t pkt_len
= hexify(hex_buffer
, buffer
, len
, len
* 2 + 1);
1471 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1475 retval
= gdb_error(connection
, retval
);
1482 static int gdb_write_memory_packet(struct connection
*connection
,
1483 char const *packet
, int packet_size
)
1485 struct target
*target
= get_target_from_connection(connection
);
1493 /* skip command character */
1496 addr
= strtoull(packet
, &separator
, 16);
1498 if (*separator
!= ',') {
1499 LOG_ERROR("incomplete write memory packet received, dropping connection");
1500 return ERROR_SERVER_REMOTE_CLOSED
;
1503 len
= strtoul(separator
+ 1, &separator
, 16);
1505 if (*(separator
++) != ':') {
1506 LOG_ERROR("incomplete write memory packet received, dropping connection");
1507 return ERROR_SERVER_REMOTE_CLOSED
;
1510 buffer
= malloc(len
);
1512 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1514 if (unhexify(buffer
, separator
, len
) != len
)
1515 LOG_ERROR("unable to decode memory packet");
1517 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1519 if (retval
== ERROR_OK
)
1520 gdb_put_packet(connection
, "OK", 2);
1522 retval
= gdb_error(connection
, retval
);
1529 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1530 char const *packet
, int packet_size
)
1532 struct target
*target
= get_target_from_connection(connection
);
1537 int retval
= ERROR_OK
;
1538 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1539 * the assumption that we're in a download and it's important to go as fast
1541 uint32_t fast_limit
= 8;
1543 /* skip command character */
1546 addr
= strtoull(packet
, &separator
, 16);
1548 if (*separator
!= ',') {
1549 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1550 return ERROR_SERVER_REMOTE_CLOSED
;
1553 len
= strtoul(separator
+ 1, &separator
, 16);
1555 if (*(separator
++) != ':') {
1556 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1557 return ERROR_SERVER_REMOTE_CLOSED
;
1560 struct gdb_connection
*gdb_connection
= connection
->priv
;
1562 if (gdb_connection
->mem_write_error
)
1563 retval
= ERROR_FAIL
;
1565 if (retval
== ERROR_OK
) {
1566 if (len
>= fast_limit
) {
1567 /* By replying the packet *immediately* GDB will send us a new packet
1568 * while we write the last one to the target.
1569 * We only do this for larger writes, so that users who do something like:
1570 * p *((int*)0xdeadbeef)=8675309
1571 * will get immediate feedback that that write failed.
1573 gdb_put_packet(connection
, "OK", 2);
1576 retval
= gdb_error(connection
, retval
);
1577 /* now that we have reported the memory write error, we can clear the condition */
1578 gdb_connection
->mem_write_error
= false;
1579 if (retval
!= ERROR_OK
)
1584 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1586 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1587 if (retval
!= ERROR_OK
)
1588 gdb_connection
->mem_write_error
= true;
1591 if (len
< fast_limit
) {
1592 if (retval
!= ERROR_OK
) {
1593 gdb_error(connection
, retval
);
1594 gdb_connection
->mem_write_error
= false;
1596 gdb_put_packet(connection
, "OK", 2);
1603 static int gdb_step_continue_packet(struct connection
*connection
,
1604 char const *packet
, int packet_size
)
1606 struct target
*target
= get_target_from_connection(connection
);
1608 uint64_t address
= 0x0;
1609 int retval
= ERROR_OK
;
1613 if (packet_size
> 1)
1614 address
= strtoull(packet
+ 1, NULL
, 16);
1618 gdb_running_type
= packet
[0];
1619 if (packet
[0] == 'c') {
1620 LOG_DEBUG("continue");
1621 /* resume at current address, don't handle breakpoints, not debugging */
1622 retval
= target_resume(target
, current
, address
, 0, 0);
1623 } else if (packet
[0] == 's') {
1625 /* step at current or address, don't handle breakpoints */
1626 retval
= target_step(target
, current
, address
, 0);
1631 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1632 char const *packet
, int packet_size
)
1634 struct target
*target
= get_target_from_connection(connection
);
1636 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1637 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1645 type
= strtoul(packet
+ 1, &separator
, 16);
1647 if (type
== 0) /* memory breakpoint */
1648 bp_type
= BKPT_SOFT
;
1649 else if (type
== 1) /* hardware breakpoint */
1650 bp_type
= BKPT_HARD
;
1651 else if (type
== 2) /* write watchpoint */
1652 wp_type
= WPT_WRITE
;
1653 else if (type
== 3) /* read watchpoint */
1655 else if (type
== 4) /* access watchpoint */
1656 wp_type
= WPT_ACCESS
;
1658 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1659 return ERROR_SERVER_REMOTE_CLOSED
;
1662 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1663 bp_type
= gdb_breakpoint_override_type
;
1665 if (*separator
!= ',') {
1666 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1667 return ERROR_SERVER_REMOTE_CLOSED
;
1670 address
= strtoull(separator
+ 1, &separator
, 16);
1672 if (*separator
!= ',') {
1673 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1674 return ERROR_SERVER_REMOTE_CLOSED
;
1677 size
= strtoul(separator
+ 1, &separator
, 16);
1682 if (packet
[0] == 'Z') {
1683 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1684 if (retval
!= ERROR_OK
) {
1685 retval
= gdb_error(connection
, retval
);
1686 if (retval
!= ERROR_OK
)
1689 gdb_put_packet(connection
, "OK", 2);
1691 breakpoint_remove(target
, address
);
1692 gdb_put_packet(connection
, "OK", 2);
1699 if (packet
[0] == 'Z') {
1700 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1701 if (retval
!= ERROR_OK
) {
1702 retval
= gdb_error(connection
, retval
);
1703 if (retval
!= ERROR_OK
)
1706 gdb_put_packet(connection
, "OK", 2);
1708 watchpoint_remove(target
, address
);
1709 gdb_put_packet(connection
, "OK", 2);
1720 /* print out a string and allocate more space as needed,
1721 * mainly used for XML at this point
1723 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1724 const char *fmt
, ...)
1726 if (*retval
!= ERROR_OK
)
1731 if ((*xml
== NULL
) || (!first
)) {
1732 /* start by 0 to exercise all the code paths.
1733 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1735 *size
= *size
* 2 + 2;
1737 *xml
= realloc(*xml
, *size
);
1741 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1749 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1751 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1755 /* there was just enough or not enough space, allocate more. */
1760 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1762 /* Locate the annex. */
1763 const char *annex_end
= strchr(buf
, ':');
1764 if (annex_end
== NULL
)
1767 /* After the read marker and annex, qXfer looks like a
1768 * traditional 'm' packet. */
1770 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1772 if (*separator
!= ',')
1775 *len
= strtoul(separator
+ 1, NULL
, 16);
1777 /* Extract the annex if needed */
1778 if (annex
!= NULL
) {
1779 *annex
= strndup(buf
, annex_end
- buf
);
1787 static int compare_bank(const void *a
, const void *b
)
1789 struct flash_bank
*b1
, *b2
;
1790 b1
= *((struct flash_bank
**)a
);
1791 b2
= *((struct flash_bank
**)b
);
1793 if (b1
->base
== b2
->base
)
1795 else if (b1
->base
> b2
->base
)
1801 static int gdb_memory_map(struct connection
*connection
,
1802 char const *packet
, int packet_size
)
1804 /* We get away with only specifying flash here. Regions that are not
1805 * specified are treated as if we provided no memory map(if not we
1806 * could detect the holes and mark them as RAM).
1807 * Normally we only execute this code once, but no big deal if we
1808 * have to regenerate it a couple of times.
1811 struct target
*target
= get_target_from_connection(connection
);
1812 struct flash_bank
*p
;
1816 int retval
= ERROR_OK
;
1817 struct flash_bank
**banks
;
1821 target_addr_t ram_start
= 0;
1823 int target_flash_banks
= 0;
1825 /* skip command character */
1828 offset
= strtoul(packet
, &separator
, 16);
1829 length
= strtoul(separator
+ 1, &separator
, 16);
1831 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1833 /* Sort banks in ascending order. We need to report non-flash
1834 * memory as ram (or rather read/write) by default for GDB, since
1835 * it has no concept of non-cacheable read/write memory (i/o etc).
1837 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1839 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1840 p
= get_flash_bank_by_num_noprobe(i
);
1841 if (p
->target
!= target
)
1843 retval
= get_flash_bank_by_num(i
, &p
);
1844 if (retval
!= ERROR_OK
) {
1846 gdb_error(connection
, retval
);
1849 banks
[target_flash_banks
++] = p
;
1852 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1855 for (i
= 0; i
< target_flash_banks
; i
++) {
1857 unsigned sector_size
= 0;
1858 unsigned group_len
= 0;
1862 if (ram_start
< p
->base
)
1863 xml_printf(&retval
, &xml
, &pos
, &size
,
1864 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1865 "length=\"0x%x\"/>\n",
1866 ram_start
, p
->base
- ram_start
);
1868 /* Report adjacent groups of same-size sectors. So for
1869 * example top boot CFI flash will list an initial region
1870 * with several large sectors (maybe 128KB) and several
1871 * smaller ones at the end (maybe 32KB). STR7 will have
1872 * regions with 8KB, 32KB, and 64KB sectors; etc.
1874 for (j
= 0; j
< p
->num_sectors
; j
++) {
1876 /* Maybe start a new group of sectors. */
1877 if (sector_size
== 0) {
1878 if (p
->sectors
[j
].offset
+ p
->sectors
[j
].size
> p
->size
) {
1879 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1880 " overflows the end of %s bank.",
1881 p
->sectors
[j
].offset
, p
->name
);
1882 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1885 target_addr_t start
;
1886 start
= p
->base
+ p
->sectors
[j
].offset
;
1887 xml_printf(&retval
, &xml
, &pos
, &size
,
1888 "<memory type=\"flash\" "
1889 "start=\"" TARGET_ADDR_FMT
"\" ",
1891 sector_size
= p
->sectors
[j
].size
;
1892 group_len
= sector_size
;
1894 group_len
+= sector_size
; /* equal to p->sectors[j].size */
1897 /* Does this finish a group of sectors?
1898 * If not, continue an already-started group.
1900 if (j
< p
->num_sectors
- 1
1901 && p
->sectors
[j
+ 1].size
== sector_size
1902 && p
->sectors
[j
+ 1].offset
== p
->sectors
[j
].offset
+ sector_size
1903 && p
->sectors
[j
+ 1].offset
+ p
->sectors
[j
+ 1].size
<= p
->size
)
1906 xml_printf(&retval
, &xml
, &pos
, &size
,
1907 "length=\"0x%x\">\n"
1908 "<property name=\"blocksize\">"
1916 ram_start
= p
->base
+ p
->size
;
1920 xml_printf(&retval
, &xml
, &pos
, &size
,
1921 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1922 "length=\"0x%x\"/>\n",
1923 ram_start
, 0-ram_start
);
1924 /* ELSE a flash chip could be at the very end of the 32 bit address
1925 * space, in which case ram_start will be precisely 0
1930 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1932 if (retval
!= ERROR_OK
) {
1934 gdb_error(connection
, retval
);
1938 if (offset
+ length
> pos
)
1939 length
= pos
- offset
;
1941 char *t
= malloc(length
+ 1);
1943 memcpy(t
+ 1, xml
+ offset
, length
);
1944 gdb_put_packet(connection
, t
, length
+ 1);
1951 static const char *gdb_get_reg_type_name(enum reg_type type
)
1960 case REG_TYPE_INT16
:
1962 case REG_TYPE_INT32
:
1964 case REG_TYPE_INT64
:
1966 case REG_TYPE_INT128
:
1970 case REG_TYPE_UINT8
:
1972 case REG_TYPE_UINT16
:
1974 case REG_TYPE_UINT32
:
1976 case REG_TYPE_UINT64
:
1978 case REG_TYPE_UINT128
:
1980 case REG_TYPE_CODE_PTR
:
1982 case REG_TYPE_DATA_PTR
:
1984 case REG_TYPE_FLOAT
:
1986 case REG_TYPE_IEEE_SINGLE
:
1987 return "ieee_single";
1988 case REG_TYPE_IEEE_DOUBLE
:
1989 return "ieee_double";
1990 case REG_TYPE_ARCH_DEFINED
:
1991 return "int"; /* return arbitrary string to avoid compile warning. */
1994 return "int"; /* "int" as default value */
1997 static int lookup_add_arch_defined_types(char const **arch_defined_types_list
[], const char *type_id
,
1998 int *num_arch_defined_types
)
2000 int tbl_sz
= *num_arch_defined_types
;
2002 if (type_id
!= NULL
&& (strcmp(type_id
, ""))) {
2003 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2004 if (!((*arch_defined_types_list
)[j
])) {
2005 (*arch_defined_types_list
)[tbl_sz
++] = type_id
;
2006 *arch_defined_types_list
= realloc(*arch_defined_types_list
,
2007 sizeof(char *) * (tbl_sz
+ 1));
2008 (*arch_defined_types_list
)[tbl_sz
] = NULL
;
2009 *num_arch_defined_types
= tbl_sz
;
2012 if (!strcmp((*arch_defined_types_list
)[j
], type_id
))
2021 static int gdb_generate_reg_type_description(struct target
*target
,
2022 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
,
2023 char const **arch_defined_types_list
[], int * num_arch_defined_types
)
2025 int retval
= ERROR_OK
;
2027 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
2028 struct reg_data_type
*data_type
= type
->reg_type_vector
->type
;
2029 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2030 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2031 num_arch_defined_types
))
2032 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2033 arch_defined_types_list
,
2034 num_arch_defined_types
);
2036 /* <vector id="id" type="type" count="count"/> */
2037 xml_printf(&retval
, tdesc
, pos
, size
,
2038 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
2039 type
->id
, type
->reg_type_vector
->type
->id
,
2040 type
->reg_type_vector
->count
);
2042 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
2043 struct reg_data_type_union_field
*field
;
2044 field
= type
->reg_type_union
->fields
;
2045 while (field
!= NULL
) {
2046 struct reg_data_type
*data_type
= field
->type
;
2047 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2048 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2049 num_arch_defined_types
))
2050 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2051 arch_defined_types_list
,
2052 num_arch_defined_types
);
2055 field
= field
->next
;
2058 * <field name="name" type="type"/> ...
2060 xml_printf(&retval
, tdesc
, pos
, size
,
2061 "<union id=\"%s\">\n",
2064 field
= type
->reg_type_union
->fields
;
2065 while (field
!= NULL
) {
2066 xml_printf(&retval
, tdesc
, pos
, size
,
2067 "<field name=\"%s\" type=\"%s\"/>\n",
2068 field
->name
, field
->type
->id
);
2070 field
= field
->next
;
2073 xml_printf(&retval
, tdesc
, pos
, size
,
2076 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
2077 struct reg_data_type_struct_field
*field
;
2078 field
= type
->reg_type_struct
->fields
;
2080 if (field
->use_bitfields
) {
2081 /* <struct id="id" size="size">
2082 * <field name="name" start="start" end="end"/> ...
2084 xml_printf(&retval
, tdesc
, pos
, size
,
2085 "<struct id=\"%s\" size=\"%d\">\n",
2086 type
->id
, type
->reg_type_struct
->size
);
2087 while (field
!= NULL
) {
2088 xml_printf(&retval
, tdesc
, pos
, size
,
2089 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2090 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2091 gdb_get_reg_type_name(field
->bitfield
->type
));
2093 field
= field
->next
;
2096 while (field
!= NULL
) {
2097 struct reg_data_type
*data_type
= field
->type
;
2098 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2099 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2100 num_arch_defined_types
))
2101 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2102 arch_defined_types_list
,
2103 num_arch_defined_types
);
2108 * <field name="name" type="type"/> ...
2110 xml_printf(&retval
, tdesc
, pos
, size
,
2111 "<struct id=\"%s\">\n",
2113 while (field
!= NULL
) {
2114 xml_printf(&retval
, tdesc
, pos
, size
,
2115 "<field name=\"%s\" type=\"%s\"/>\n",
2116 field
->name
, field
->type
->id
);
2118 field
= field
->next
;
2122 xml_printf(&retval
, tdesc
, pos
, size
,
2125 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2126 /* <flags id="id" size="size">
2127 * <field name="name" start="start" end="end"/> ...
2129 xml_printf(&retval
, tdesc
, pos
, size
,
2130 "<flags id=\"%s\" size=\"%d\">\n",
2131 type
->id
, type
->reg_type_flags
->size
);
2133 struct reg_data_type_flags_field
*field
;
2134 field
= type
->reg_type_flags
->fields
;
2135 while (field
!= NULL
) {
2136 xml_printf(&retval
, tdesc
, pos
, size
,
2137 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2138 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2139 gdb_get_reg_type_name(field
->bitfield
->type
));
2141 field
= field
->next
;
2144 xml_printf(&retval
, tdesc
, pos
, size
,
2152 /* Get a list of available target registers features. feature_list must
2153 * be freed by caller.
2155 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2156 struct reg
**reg_list
, int reg_list_size
)
2160 /* Start with only one element */
2161 *feature_list
= calloc(1, sizeof(char *));
2163 for (int i
= 0; i
< reg_list_size
; i
++) {
2164 if (reg_list
[i
]->exist
== false)
2167 if (reg_list
[i
]->feature
!= NULL
2168 && reg_list
[i
]->feature
->name
!= NULL
2169 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2170 /* We found a feature, check if the feature is already in the
2171 * table. If not, allocate a new entry for the table and
2172 * put the new feature in it.
2174 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2175 if (!((*feature_list
)[j
])) {
2176 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2177 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2178 (*feature_list
)[tbl_sz
] = NULL
;
2181 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2188 if (feature_list_size
)
2189 *feature_list_size
= tbl_sz
;
2194 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2196 int retval
= ERROR_OK
;
2197 struct reg
**reg_list
= NULL
;
2199 char const **features
= NULL
;
2200 char const **arch_defined_types
= NULL
;
2201 int feature_list_size
= 0;
2202 int num_arch_defined_types
= 0;
2207 arch_defined_types
= calloc(1, sizeof(char *));
2209 retval
= target_get_gdb_reg_list(target
, ®_list
,
2210 ®_list_size
, REG_CLASS_ALL
);
2212 if (retval
!= ERROR_OK
) {
2213 LOG_ERROR("get register list failed");
2214 retval
= ERROR_FAIL
;
2218 if (reg_list_size
<= 0) {
2219 LOG_ERROR("get register list failed");
2220 retval
= ERROR_FAIL
;
2224 /* Get a list of available target registers features */
2225 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2226 if (retval
!= ERROR_OK
) {
2227 LOG_ERROR("Can't get the registers feature list");
2228 retval
= ERROR_FAIL
;
2232 /* If we found some features associated with registers, create sections */
2233 int current_feature
= 0;
2235 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2236 "<?xml version=\"1.0\"?>\n"
2237 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2238 "<target version=\"1.0\">\n");
2240 /* generate target description according to register list */
2241 if (features
!= NULL
) {
2242 while (features
[current_feature
]) {
2244 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2245 "<feature name=\"%s\">\n",
2246 features
[current_feature
]);
2249 for (i
= 0; i
< reg_list_size
; i
++) {
2251 if (reg_list
[i
]->exist
== false)
2254 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2257 const char *type_str
;
2258 if (reg_list
[i
]->reg_data_type
!= NULL
) {
2259 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2260 /* generate <type... first, if there are architecture-defined types. */
2261 if (lookup_add_arch_defined_types(&arch_defined_types
,
2262 reg_list
[i
]->reg_data_type
->id
,
2263 &num_arch_defined_types
))
2264 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2265 reg_list
[i
]->reg_data_type
,
2266 &arch_defined_types
,
2267 &num_arch_defined_types
);
2269 type_str
= reg_list
[i
]->reg_data_type
->id
;
2271 /* predefined type */
2272 type_str
= gdb_get_reg_type_name(
2273 reg_list
[i
]->reg_data_type
->type
);
2276 /* Default type is "int" */
2280 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2281 "<reg name=\"%s\"", reg_list
[i
]->name
);
2282 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2283 " bitsize=\"%d\"", reg_list
[i
]->size
);
2284 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2285 " regnum=\"%d\"", reg_list
[i
]->number
);
2286 if (reg_list
[i
]->caller_save
)
2287 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2288 " save-restore=\"yes\"");
2290 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2291 " save-restore=\"no\"");
2293 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2294 " type=\"%s\"", type_str
);
2296 if (reg_list
[i
]->group
!= NULL
)
2297 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2298 " group=\"%s\"", reg_list
[i
]->group
);
2300 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2304 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2311 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2317 free(arch_defined_types
);
2319 if (retval
== ERROR_OK
)
2327 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2328 char **chunk
, int32_t offset
, uint32_t length
)
2330 if (target_desc
== NULL
) {
2331 LOG_ERROR("Unable to Generate Target Description");
2335 char *tdesc
= target_desc
->tdesc
;
2336 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2338 if (tdesc
== NULL
) {
2339 int retval
= gdb_generate_target_description(target
, &tdesc
);
2340 if (retval
!= ERROR_OK
) {
2341 LOG_ERROR("Unable to Generate Target Description");
2345 tdesc_length
= strlen(tdesc
);
2350 if (length
< (tdesc_length
- offset
))
2351 transfer_type
= 'm';
2353 transfer_type
= 'l';
2355 *chunk
= malloc(length
+ 2);
2356 if (*chunk
== NULL
) {
2357 LOG_ERROR("Unable to allocate memory");
2361 (*chunk
)[0] = transfer_type
;
2362 if (transfer_type
== 'm') {
2363 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2364 (*chunk
)[1 + length
] = '\0';
2366 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2367 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2369 /* After gdb-server sends out last chunk, invalidate tdesc. */
2375 target_desc
->tdesc
= tdesc
;
2376 target_desc
->tdesc_length
= tdesc_length
;
2381 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2383 int retval
= ERROR_OK
;
2384 struct reg
**reg_list
= NULL
;
2385 int reg_list_size
= 0;
2386 char const **features
= NULL
;
2387 int feature_list_size
= 0;
2389 retval
= target_get_gdb_reg_list(target
, ®_list
,
2390 ®_list_size
, REG_CLASS_ALL
);
2391 if (retval
!= ERROR_OK
) {
2392 LOG_ERROR("get register list failed");
2396 if (reg_list_size
<= 0) {
2397 LOG_ERROR("get register list failed");
2398 retval
= ERROR_FAIL
;
2402 /* Get a list of available target registers features */
2403 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2404 if (retval
!= ERROR_OK
) {
2405 LOG_ERROR("Can't get the registers feature list");
2410 if (feature_list_size
)
2424 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2426 struct rtos
*rtos
= target
->rtos
;
2427 int retval
= ERROR_OK
;
2428 char *thread_list
= NULL
;
2432 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2433 "<?xml version=\"1.0\"?>\n"
2437 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2438 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2440 if (!thread_detail
->exists
)
2443 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2444 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2446 if (thread_detail
->thread_name_str
!= NULL
)
2447 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2448 "Name: %s", thread_detail
->thread_name_str
);
2450 if (thread_detail
->extra_info_str
!= NULL
) {
2451 if (thread_detail
->thread_name_str
!= NULL
)
2452 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2454 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2455 thread_detail
->extra_info_str
);
2458 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2463 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2466 if (retval
== ERROR_OK
)
2467 *thread_list_out
= thread_list
;
2474 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2475 char **chunk
, int32_t offset
, uint32_t length
)
2477 if (*thread_list
== NULL
) {
2478 int retval
= gdb_generate_thread_list(target
, thread_list
);
2479 if (retval
!= ERROR_OK
) {
2480 LOG_ERROR("Unable to Generate Thread List");
2485 size_t thread_list_length
= strlen(*thread_list
);
2488 length
= MIN(length
, thread_list_length
- offset
);
2489 if (length
< (thread_list_length
- offset
))
2490 transfer_type
= 'm';
2492 transfer_type
= 'l';
2494 *chunk
= malloc(length
+ 2 + 3);
2495 /* Allocating extra 3 bytes prevents false positive valgrind report
2496 * of strlen(chunk) word access:
2497 * Invalid read of size 4
2498 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2499 if (*chunk
== NULL
) {
2500 LOG_ERROR("Unable to allocate memory");
2504 (*chunk
)[0] = transfer_type
;
2505 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2506 (*chunk
)[1 + length
] = '\0';
2508 /* After gdb-server sends out last chunk, invalidate thread list. */
2509 if (transfer_type
== 'l') {
2511 *thread_list
= NULL
;
2517 static int gdb_query_packet(struct connection
*connection
,
2518 char const *packet
, int packet_size
)
2520 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2521 struct gdb_connection
*gdb_connection
= connection
->priv
;
2522 struct target
*target
= get_target_from_connection(connection
);
2524 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2525 if (packet_size
> 6) {
2527 cmd
= malloc((packet_size
- 6) / 2 + 1);
2528 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2531 /* We want to print all debug output to GDB connection */
2532 log_add_callback(gdb_log_callback
, connection
);
2533 target_call_timer_callbacks_now();
2534 /* some commands need to know the GDB connection, make note of current
2535 * GDB connection. */
2536 current_gdb_connection
= gdb_connection
;
2537 command_run_line(cmd_ctx
, cmd
);
2538 current_gdb_connection
= NULL
;
2539 target_call_timer_callbacks_now();
2540 log_remove_callback(gdb_log_callback
, connection
);
2543 gdb_put_packet(connection
, "OK", 2);
2545 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2546 if (packet_size
> 5) {
2551 target_addr_t addr
= 0;
2554 /* skip command character */
2557 addr
= strtoull(packet
, &separator
, 16);
2559 if (*separator
!= ',') {
2560 LOG_ERROR("incomplete read memory packet received, dropping connection");
2561 return ERROR_SERVER_REMOTE_CLOSED
;
2564 len
= strtoul(separator
+ 1, NULL
, 16);
2566 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2568 if (retval
== ERROR_OK
) {
2569 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2570 gdb_put_packet(connection
, gdb_reply
, 9);
2572 retval
= gdb_error(connection
, retval
);
2573 if (retval
!= ERROR_OK
)
2579 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2580 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2581 * qXfer:features:read is supported for some targets */
2582 int retval
= ERROR_OK
;
2583 char *buffer
= NULL
;
2586 int gdb_target_desc_supported
= 0;
2588 /* we need to test that the target supports target descriptions */
2589 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2590 if (retval
!= ERROR_OK
) {
2591 LOG_INFO("Failed detecting Target Description Support, disabling");
2592 gdb_target_desc_supported
= 0;
2595 /* support may be disabled globally */
2596 if (gdb_use_target_description
== 0) {
2597 if (gdb_target_desc_supported
)
2598 LOG_WARNING("Target Descriptions Supported, but disabled");
2599 gdb_target_desc_supported
= 0;
2606 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2607 (GDB_BUFFER_SIZE
- 1),
2608 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2609 (gdb_target_desc_supported
== 1) ? '+' : '-');
2611 if (retval
!= ERROR_OK
) {
2612 gdb_send_error(connection
, 01);
2616 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2620 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2621 && (flash_get_bank_count() > 0))
2622 return gdb_memory_map(connection
, packet
, packet_size
);
2623 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2625 int retval
= ERROR_OK
;
2628 unsigned int length
;
2630 /* skip command character */
2633 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2634 gdb_send_error(connection
, 01);
2638 /* Target should prepare correct target description for annex.
2639 * The first character of returned xml is 'm' or 'l'. 'm' for
2640 * there are *more* chunks to transfer. 'l' for it is the *last*
2641 * chunk of target description.
2643 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2644 &xml
, offset
, length
);
2645 if (retval
!= ERROR_OK
) {
2646 gdb_error(connection
, retval
);
2650 gdb_put_packet(connection
, xml
, strlen(xml
));
2654 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2656 int retval
= ERROR_OK
;
2659 unsigned int length
;
2661 /* skip command character */
2664 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2665 gdb_send_error(connection
, 01);
2669 /* Target should prepare correct thread list for annex.
2670 * The first character of returned xml is 'm' or 'l'. 'm' for
2671 * there are *more* chunks to transfer. 'l' for it is the *last*
2672 * chunk of target description.
2674 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2675 &xml
, offset
, length
);
2676 if (retval
!= ERROR_OK
) {
2677 gdb_error(connection
, retval
);
2681 gdb_put_packet(connection
, xml
, strlen(xml
));
2685 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2686 gdb_connection
->noack_mode
= 1;
2687 gdb_put_packet(connection
, "OK", 2);
2691 gdb_put_packet(connection
, "", 0);
2695 static bool gdb_handle_vcont_packet(struct connection
*connection
, const char *packet
, int packet_size
)
2697 struct gdb_connection
*gdb_connection
= connection
->priv
;
2698 struct target
*target
= get_target_from_connection(connection
);
2699 const char *parse
= packet
;
2702 /* query for vCont supported */
2703 if (parse
[0] == '?') {
2704 if (target
->type
->step
!= NULL
) {
2705 /* gdb doesn't accept c without C and s without S */
2706 gdb_put_packet(connection
, "vCont;c;C;s;S", 13);
2712 if (parse
[0] == ';') {
2717 /* simple case, a continue packet */
2718 if (parse
[0] == 'c') {
2719 gdb_running_type
= 'c';
2720 LOG_DEBUG("target %s continue", target_name(target
));
2721 log_add_callback(gdb_log_callback
, connection
);
2722 retval
= target_resume(target
, 1, 0, 0, 0);
2723 if (retval
== ERROR_TARGET_NOT_HALTED
)
2724 LOG_INFO("target %s was not halted when resume was requested", target_name(target
));
2726 /* poll target in an attempt to make its internal state consistent */
2727 if (retval
!= ERROR_OK
) {
2728 retval
= target_poll(target
);
2729 if (retval
!= ERROR_OK
)
2730 LOG_DEBUG("error polling target %s after failed resume", target_name(target
));
2734 * We don't report errors to gdb here, move frontend_state to
2735 * TARGET_RUNNING to stay in sync with gdb's expectation of the
2738 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2739 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2744 /* single-step or step-over-breakpoint */
2745 if (parse
[0] == 's') {
2746 gdb_running_type
= 's';
2747 bool fake_step
= false;
2749 if (strncmp(parse
, "s:", 2) == 0) {
2750 struct target
*ct
= target
;
2758 thread_id
= strtoll(parse
, &endp
, 16);
2760 packet_size
-= endp
- parse
;
2764 if (target
->rtos
!= NULL
) {
2765 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
2766 rtos_update_threads(target
);
2768 target
->rtos
->gdb_target_for_threadid(connection
, thread_id
, &ct
);
2771 * check if the thread to be stepped is the current rtos thread
2772 * if not, we must fake the step
2774 if (target
->rtos
->current_thread
!= thread_id
)
2778 if (parse
[0] == ';') {
2782 if (parse
[0] == 'c') {
2786 /* check if thread-id follows */
2787 if (parse
[0] == ':') {
2792 tid
= strtoll(parse
, &endp
, 16);
2793 if (tid
== thread_id
) {
2795 * Special case: only step a single thread (core),
2796 * keep the other threads halted. Currently, only
2797 * aarch64 target understands it. Other target types don't
2798 * care (nobody checks the actual value of 'current')
2799 * and it doesn't really matter. This deserves
2800 * a symbolic constant and a formal interface documentation
2803 LOG_DEBUG("request to step current core only");
2804 /* uncomment after checking that indeed other targets are safe */
2811 LOG_DEBUG("target %s single-step thread %"PRIx64
, target_name(ct
), thread_id
);
2812 log_add_callback(gdb_log_callback
, connection
);
2813 target_call_event_callbacks(ct
, TARGET_EVENT_GDB_START
);
2816 * work around an annoying gdb behaviour: when the current thread
2817 * is changed in gdb, it assumes that the target can follow and also
2818 * make the thread current. This is an assumption that cannot hold
2819 * for a real target running a multi-threading OS. We just fake
2820 * the step to not trigger an internal error in gdb. See
2821 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
2825 char sig_reply
[128];
2827 LOG_DEBUG("fake step thread %"PRIx64
, thread_id
);
2829 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
),
2830 "T05thread:%016"PRIx64
";", thread_id
);
2832 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
2833 log_remove_callback(gdb_log_callback
, connection
);
2838 /* support for gdb_sync command */
2839 if (gdb_connection
->sync
) {
2840 gdb_connection
->sync
= false;
2841 if (ct
->state
== TARGET_HALTED
) {
2842 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2843 "from the target.");
2844 gdb_sig_halted(connection
);
2845 log_remove_callback(gdb_log_callback
, connection
);
2847 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2851 retval
= target_step(ct
, current_pc
, 0, 0);
2852 if (retval
== ERROR_TARGET_NOT_HALTED
)
2853 LOG_INFO("target %s was not halted when step was requested", target_name(ct
));
2855 /* if step was successful send a reply back to gdb */
2856 if (retval
== ERROR_OK
) {
2857 retval
= target_poll(ct
);
2858 if (retval
!= ERROR_OK
)
2859 LOG_DEBUG("error polling target %s after successful step", target_name(ct
));
2860 /* send back signal information */
2861 gdb_signal_reply(ct
, connection
);
2862 /* stop forwarding log packets! */
2863 log_remove_callback(gdb_log_callback
, connection
);
2865 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2867 LOG_ERROR("Unknown vCont packet");
2876 static int gdb_v_packet(struct connection
*connection
,
2877 char const *packet
, int packet_size
)
2879 struct gdb_connection
*gdb_connection
= connection
->priv
;
2880 struct target
*target
;
2883 target
= get_target_from_connection(connection
);
2885 if (strncmp(packet
, "vCont", 5) == 0) {
2891 handled
= gdb_handle_vcont_packet(connection
, packet
, packet_size
);
2893 gdb_put_packet(connection
, "", 0);
2898 /* if flash programming disabled - send a empty reply */
2900 if (gdb_flash_program
== 0) {
2901 gdb_put_packet(connection
, "", 0);
2905 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
2907 unsigned long length
;
2909 char const *parse
= packet
+ 12;
2910 if (*parse
== '\0') {
2911 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2912 return ERROR_SERVER_REMOTE_CLOSED
;
2915 addr
= strtoul(parse
, (char **)&parse
, 16);
2917 if (*(parse
++) != ',' || *parse
== '\0') {
2918 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2919 return ERROR_SERVER_REMOTE_CLOSED
;
2922 length
= strtoul(parse
, (char **)&parse
, 16);
2924 if (*parse
!= '\0') {
2925 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2926 return ERROR_SERVER_REMOTE_CLOSED
;
2929 /* assume all sectors need erasing - stops any problems
2930 * when flash_write is called multiple times */
2933 /* perform any target specific operations before the erase */
2934 target_call_event_callbacks(target
,
2935 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2937 /* vFlashErase:addr,length messages require region start and
2938 * end to be "block" aligned ... if padding is ever needed,
2939 * GDB will have become dangerously confused.
2941 result
= flash_erase_address_range(target
, false, addr
,
2944 /* perform any target specific operations after the erase */
2945 target_call_event_callbacks(target
,
2946 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2949 if (result
!= ERROR_OK
) {
2950 /* GDB doesn't evaluate the actual error number returned,
2951 * treat a failed erase as an I/O error
2953 gdb_send_error(connection
, EIO
);
2954 LOG_ERROR("flash_erase returned %i", result
);
2956 gdb_put_packet(connection
, "OK", 2);
2961 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
2964 unsigned long length
;
2965 char const *parse
= packet
+ 12;
2967 if (*parse
== '\0') {
2968 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2969 return ERROR_SERVER_REMOTE_CLOSED
;
2971 addr
= strtoul(parse
, (char **)&parse
, 16);
2972 if (*(parse
++) != ':') {
2973 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2974 return ERROR_SERVER_REMOTE_CLOSED
;
2976 length
= packet_size
- (parse
- packet
);
2978 /* create a new image if there isn't already one */
2979 if (gdb_connection
->vflash_image
== NULL
) {
2980 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2981 image_open(gdb_connection
->vflash_image
, "", "build");
2984 /* create new section with content from packet buffer */
2985 retval
= image_add_section(gdb_connection
->vflash_image
,
2986 addr
, length
, 0x0, (uint8_t const *)parse
);
2987 if (retval
!= ERROR_OK
)
2990 gdb_put_packet(connection
, "OK", 2);
2995 if (strncmp(packet
, "vFlashDone", 10) == 0) {
2998 /* process the flashing buffer. No need to erase as GDB
2999 * always issues a vFlashErase first. */
3000 target_call_event_callbacks(target
,
3001 TARGET_EVENT_GDB_FLASH_WRITE_START
);
3002 result
= flash_write(target
, gdb_connection
->vflash_image
,
3004 target_call_event_callbacks(target
,
3005 TARGET_EVENT_GDB_FLASH_WRITE_END
);
3006 if (result
!= ERROR_OK
) {
3007 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
3008 gdb_put_packet(connection
, "E.memtype", 9);
3010 gdb_send_error(connection
, EIO
);
3012 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
3013 gdb_put_packet(connection
, "OK", 2);
3016 image_close(gdb_connection
->vflash_image
);
3017 free(gdb_connection
->vflash_image
);
3018 gdb_connection
->vflash_image
= NULL
;
3023 gdb_put_packet(connection
, "", 0);
3027 static int gdb_detach(struct connection
*connection
)
3030 * Only reply "OK" to GDB
3031 * it will close the connection and this will trigger a call to
3032 * gdb_connection_closed() that will in turn trigger the event
3033 * TARGET_EVENT_GDB_DETACH
3035 return gdb_put_packet(connection
, "OK", 2);
3038 /* The format of 'F' response packet is
3039 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3041 static int gdb_fileio_response_packet(struct connection
*connection
,
3042 char const *packet
, int packet_size
)
3044 struct target
*target
= get_target_from_connection(connection
);
3046 char *parsing_point
;
3047 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
3048 int fileio_errno
= 0;
3049 bool fileio_ctrl_c
= false;
3054 if (*separator
== ',') {
3055 parsing_point
= separator
+ 1;
3056 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
3057 if (*separator
== ',') {
3058 if (*(separator
+ 1) == 'C') {
3059 /* TODO: process ctrl-c */
3060 fileio_ctrl_c
= true;
3065 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3066 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
3068 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
3069 if (retval
!= ERROR_OK
)
3072 /* After File-I/O ends, keep continue or step */
3073 if (gdb_running_type
== 'c')
3074 retval
= target_resume(target
, 1, 0x0, 0, 0);
3075 else if (gdb_running_type
== 's')
3076 retval
= target_step(target
, 1, 0x0, 0);
3078 retval
= ERROR_FAIL
;
3080 if (retval
!= ERROR_OK
)
3086 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
3087 const char *function
, const char *string
)
3089 struct connection
*connection
= priv
;
3090 struct gdb_connection
*gdb_con
= connection
->priv
;
3092 if (gdb_con
->busy
) {
3093 /* do not reply this using the O packet */
3097 gdb_output_con(connection
, string
);
3100 static void gdb_sig_halted(struct connection
*connection
)
3103 snprintf(sig_reply
, 4, "T%2.2x", 2);
3104 gdb_put_packet(connection
, sig_reply
, 3);
3107 static int gdb_input_inner(struct connection
*connection
)
3109 /* Do not allocate this on the stack */
3110 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
3112 struct target
*target
;
3113 char const *packet
= gdb_packet_buffer
;
3116 struct gdb_connection
*gdb_con
= connection
->priv
;
3117 static int extended_protocol
;
3119 target
= get_target_from_connection(connection
);
3121 /* drain input buffer. If one of the packets fail, then an error
3122 * packet is replied, if applicable.
3124 * This loop will terminate and the error code is returned.
3126 * The calling fn will check if this error is something that
3127 * can be recovered from, or if the connection must be closed.
3129 * If the error is recoverable, this fn is called again to
3130 * drain the rest of the buffer.
3133 packet_size
= GDB_BUFFER_SIZE
-1;
3134 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
3135 if (retval
!= ERROR_OK
)
3138 /* terminate with zero */
3139 gdb_packet_buffer
[packet_size
] = '\0';
3141 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
3142 if (packet
[0] == 'X') {
3143 /* binary packets spew junk into the debug log stream */
3146 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
3149 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
3151 LOG_DEBUG("received packet: '%s'", packet
);
3154 if (packet_size
> 0) {
3156 switch (packet
[0]) {
3157 case 'T': /* Is thread alive? */
3158 gdb_thread_packet(connection
, packet
, packet_size
);
3160 case 'H': /* Set current thread ( 'c' for step and continue,
3161 * 'g' for all other operations ) */
3162 gdb_thread_packet(connection
, packet
, packet_size
);
3166 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
3167 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
3168 retval
= gdb_query_packet(connection
, packet
, packet_size
);
3171 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
3174 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
3177 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
3180 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
3183 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
3186 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
3190 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
3193 gdb_last_signal_packet(connection
, packet
, packet_size
);
3198 gdb_thread_packet(connection
, packet
, packet_size
);
3199 log_add_callback(gdb_log_callback
, connection
);
3201 if (gdb_con
->mem_write_error
) {
3202 LOG_ERROR("Memory write failure!");
3204 /* now that we have reported the memory write error,
3205 * we can clear the condition */
3206 gdb_con
->mem_write_error
= false;
3209 bool nostep
= false;
3210 bool already_running
= false;
3211 if (target
->state
== TARGET_RUNNING
) {
3212 LOG_WARNING("WARNING! The target is already running. "
3213 "All changes GDB did to registers will be discarded! "
3214 "Waiting for target to halt.");
3215 already_running
= true;
3216 } else if (target
->state
!= TARGET_HALTED
) {
3217 LOG_WARNING("The target is not in the halted nor running stated, " \
3218 "stepi/continue ignored.");
3220 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
3221 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3222 * sent by GDB first to OpenOCD, thus defeating the check to
3223 * make only the single stepping have the sync feature...
3226 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
3227 "from the target.");
3229 gdb_con
->sync
= false;
3231 if (!already_running
&& nostep
) {
3232 /* Either the target isn't in the halted state, then we can't
3233 * step/continue. This might be early setup, etc.
3235 * Or we want to allow GDB to pick up a fresh set of
3236 * register values without modifying the target state.
3239 gdb_sig_halted(connection
);
3241 /* stop forwarding log packets! */
3242 log_remove_callback(gdb_log_callback
, connection
);
3244 /* We're running/stepping, in which case we can
3245 * forward log output until the target is halted
3247 gdb_con
->frontend_state
= TARGET_RUNNING
;
3248 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3250 if (!already_running
) {
3251 /* Here we don't want packet processing to stop even if this fails,
3252 * so we use a local variable instead of retval. */
3253 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
3254 if (retval
!= ERROR_OK
) {
3255 /* we'll never receive a halted
3256 * condition... issue a false one..
3258 gdb_frontend_halted(target
, connection
);
3265 retval
= gdb_v_packet(connection
, packet
, packet_size
);
3268 retval
= gdb_detach(connection
);
3269 extended_protocol
= 0;
3272 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
3273 if (retval
!= ERROR_OK
)
3277 if (extended_protocol
!= 0) {
3278 gdb_con
->attached
= false;
3281 gdb_put_packet(connection
, "OK", 2);
3282 return ERROR_SERVER_REMOTE_CLOSED
;
3284 /* handle extended remote protocol */
3285 extended_protocol
= 1;
3286 gdb_put_packet(connection
, "OK", 2);
3289 /* handle extended restart packet */
3290 breakpoint_clear_target(target
);
3291 watchpoint_clear_target(target
);
3292 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
3293 target_name(target
));
3294 /* set connection as attached after reset */
3295 gdb_con
->attached
= true;
3296 /* info rtos parts */
3297 gdb_thread_packet(connection
, packet
, packet_size
);
3301 /* packet supported only by smp target i.e cortex_a.c*/
3302 /* handle smp packet replying coreid played to gbd */
3303 gdb_read_smp_packet(connection
, packet
, packet_size
);
3307 /* packet supported only by smp target i.e cortex_a.c */
3308 /* handle smp packet setting coreid to be played at next
3310 gdb_write_smp_packet(connection
, packet
, packet_size
);
3314 /* File-I/O extension */
3315 /* After gdb uses host-side syscall to complete target file
3316 * I/O, gdb sends host-side syscall return value to target
3318 * The format of 'F' response packet is
3319 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3321 gdb_con
->frontend_state
= TARGET_RUNNING
;
3322 log_add_callback(gdb_log_callback
, connection
);
3323 gdb_fileio_response_packet(connection
, packet
, packet_size
);
3327 /* ignore unknown packets */
3328 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
3329 gdb_put_packet(connection
, NULL
, 0);
3333 /* if a packet handler returned an error, exit input loop */
3334 if (retval
!= ERROR_OK
)
3338 if (gdb_con
->ctrl_c
) {
3339 if (target
->state
== TARGET_RUNNING
) {
3340 struct target
*t
= target
;
3342 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &t
);
3343 retval
= target_halt(t
);
3344 if (retval
== ERROR_OK
)
3345 retval
= target_poll(t
);
3346 if (retval
!= ERROR_OK
)
3347 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3348 gdb_con
->ctrl_c
= 0;
3350 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3351 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3355 } while (gdb_con
->buf_cnt
> 0);
3360 static int gdb_input(struct connection
*connection
)
3362 int retval
= gdb_input_inner(connection
);
3363 struct gdb_connection
*gdb_con
= connection
->priv
;
3364 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3367 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3368 if (gdb_con
->closed
)
3369 return ERROR_SERVER_REMOTE_CLOSED
;
3371 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3375 static int gdb_target_start(struct target
*target
, const char *port
)
3377 struct gdb_service
*gdb_service
;
3379 gdb_service
= malloc(sizeof(struct gdb_service
));
3381 if (NULL
== gdb_service
)
3384 LOG_DEBUG("starting gdb server for %s on %s", target_name(target
), port
);
3386 gdb_service
->target
= target
;
3387 gdb_service
->core
[0] = -1;
3388 gdb_service
->core
[1] = -1;
3389 target
->gdb_service
= gdb_service
;
3391 ret
= add_service("gdb",
3392 port
, 1, &gdb_new_connection
, &gdb_input
,
3393 &gdb_connection_closed
, gdb_service
);
3394 /* initialialize all targets gdb service with the same pointer */
3396 struct target_list
*head
;
3397 struct target
*curr
;
3398 head
= target
->head
;
3399 while (head
!= (struct target_list
*)NULL
) {
3400 curr
= head
->target
;
3402 curr
->gdb_service
= gdb_service
;
3409 static int gdb_target_add_one(struct target
*target
)
3411 /* one gdb instance per smp list */
3412 if ((target
->smp
) && (target
->gdb_service
))
3415 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3416 if (!target_supports_gdb_connection(target
)) {
3417 LOG_DEBUG("skip gdb server for target %s", target_name(target
));
3421 if (target
->gdb_port_override
) {
3422 if (strcmp(target
->gdb_port_override
, "disabled") == 0) {
3423 LOG_INFO("gdb port disabled");
3426 return gdb_target_start(target
, target
->gdb_port_override
);
3429 if (strcmp(gdb_port
, "disabled") == 0) {
3430 LOG_INFO("gdb port disabled");
3434 int retval
= gdb_target_start(target
, gdb_port_next
);
3435 if (retval
== ERROR_OK
) {
3436 /* save the port number so can be queried with
3437 * $target_name cget -gdb-port
3439 target
->gdb_port_override
= strdup(gdb_port_next
);
3442 /* If we can parse the port number
3443 * then we increment the port number for the next target.
3446 portnumber
= strtol(gdb_port_next
, &end
, 0);
3448 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3449 free(gdb_port_next
);
3451 gdb_port_next
= alloc_printf("%d", portnumber
+1);
3453 /* Don't increment if gdb_port is 0, since we're just
3454 * trying to allocate an unused port. */
3455 gdb_port_next
= strdup("0");
3463 int gdb_target_add_all(struct target
*target
)
3465 if (NULL
== target
) {
3466 LOG_WARNING("gdb services need one or more targets defined");
3470 while (NULL
!= target
) {
3471 int retval
= gdb_target_add_one(target
);
3472 if (ERROR_OK
!= retval
)
3475 target
= target
->next
;
3481 COMMAND_HANDLER(handle_gdb_sync_command
)
3484 return ERROR_COMMAND_SYNTAX_ERROR
;
3486 if (current_gdb_connection
== NULL
) {
3487 command_print(CMD_CTX
,
3488 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3492 current_gdb_connection
->sync
= true;
3497 /* daemon configuration command gdb_port */
3498 COMMAND_HANDLER(handle_gdb_port_command
)
3500 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3501 if (ERROR_OK
== retval
) {
3502 free(gdb_port_next
);
3503 gdb_port_next
= strdup(gdb_port
);
3508 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3511 return ERROR_COMMAND_SYNTAX_ERROR
;
3513 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3517 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3520 return ERROR_COMMAND_SYNTAX_ERROR
;
3522 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3526 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3529 return ERROR_COMMAND_SYNTAX_ERROR
;
3531 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3535 COMMAND_HANDLER(handle_gdb_report_register_access_error
)
3538 return ERROR_COMMAND_SYNTAX_ERROR
;
3540 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_register_access_error
);
3544 /* gdb_breakpoint_override */
3545 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3547 if (CMD_ARGC
== 0) {
3549 } else if (CMD_ARGC
== 1) {
3550 gdb_breakpoint_override
= 1;
3551 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3552 gdb_breakpoint_override_type
= BKPT_HARD
;
3553 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3554 gdb_breakpoint_override_type
= BKPT_SOFT
;
3555 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3556 gdb_breakpoint_override
= 0;
3558 return ERROR_COMMAND_SYNTAX_ERROR
;
3559 if (gdb_breakpoint_override
)
3560 LOG_USER("force %s breakpoints",
3561 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3563 LOG_USER("breakpoint type is not overridden");
3568 COMMAND_HANDLER(handle_gdb_target_description_command
)
3571 return ERROR_COMMAND_SYNTAX_ERROR
;
3573 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3577 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3580 uint32_t tdesc_length
;
3581 struct target
*target
= get_current_target(CMD_CTX
);
3583 int retval
= gdb_generate_target_description(target
, &tdesc
);
3584 if (retval
!= ERROR_OK
) {
3585 LOG_ERROR("Unable to Generate Target Description");
3589 tdesc_length
= strlen(tdesc
);
3591 struct fileio
*fileio
;
3592 size_t size_written
;
3594 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3595 if (tdesc_filename
== NULL
) {
3596 retval
= ERROR_FAIL
;
3600 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
3602 if (retval
!= ERROR_OK
) {
3603 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
3607 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
3609 fileio_close(fileio
);
3611 if (retval
!= ERROR_OK
)
3612 LOG_ERROR("Error while writing the tdesc file");
3615 free(tdesc_filename
);
3621 static const struct command_registration gdb_command_handlers
[] = {
3624 .handler
= handle_gdb_sync_command
,
3625 .mode
= COMMAND_ANY
,
3626 .help
= "next stepi will return immediately allowing "
3627 "GDB to fetch register state without affecting "
3633 .handler
= handle_gdb_port_command
,
3634 .mode
= COMMAND_ANY
,
3635 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
3636 "server listens for the next port number after the "
3637 "base port number specified. "
3638 "No arguments reports GDB port. \"pipe\" means listen to stdin "
3639 "output to stdout, an integer is base port number, \"disabled\" disables "
3640 "port. Any other string is are interpreted as named pipe to listen to. "
3641 "Output pipe is the same name as input pipe, but with 'o' appended.",
3642 .usage
= "[port_num]",
3645 .name
= "gdb_memory_map",
3646 .handler
= handle_gdb_memory_map_command
,
3647 .mode
= COMMAND_CONFIG
,
3648 .help
= "enable or disable memory map",
3649 .usage
= "('enable'|'disable')"
3652 .name
= "gdb_flash_program",
3653 .handler
= handle_gdb_flash_program_command
,
3654 .mode
= COMMAND_CONFIG
,
3655 .help
= "enable or disable flash program",
3656 .usage
= "('enable'|'disable')"
3659 .name
= "gdb_report_data_abort",
3660 .handler
= handle_gdb_report_data_abort_command
,
3661 .mode
= COMMAND_CONFIG
,
3662 .help
= "enable or disable reporting data aborts",
3663 .usage
= "('enable'|'disable')"
3666 .name
= "gdb_report_register_access_error",
3667 .handler
= handle_gdb_report_register_access_error
,
3668 .mode
= COMMAND_CONFIG
,
3669 .help
= "enable or disable reporting register access errors",
3670 .usage
= "('enable'|'disable')"
3673 .name
= "gdb_breakpoint_override",
3674 .handler
= handle_gdb_breakpoint_override_command
,
3675 .mode
= COMMAND_ANY
,
3676 .help
= "Display or specify type of breakpoint "
3677 "to be used by gdb 'break' commands.",
3678 .usage
= "('hard'|'soft'|'disable')"
3681 .name
= "gdb_target_description",
3682 .handler
= handle_gdb_target_description_command
,
3683 .mode
= COMMAND_CONFIG
,
3684 .help
= "enable or disable target description",
3685 .usage
= "('enable'|'disable')"
3688 .name
= "gdb_save_tdesc",
3689 .handler
= handle_gdb_save_tdesc_command
,
3690 .mode
= COMMAND_EXEC
,
3691 .help
= "Save the target description file",
3693 COMMAND_REGISTRATION_DONE
3696 int gdb_register_commands(struct command_context
*cmd_ctx
)
3698 gdb_port
= strdup("3333");
3699 gdb_port_next
= strdup("3333");
3700 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);
3703 void gdb_service_free(void)
3706 free(gdb_port_next
);