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_EXC_CATCH
:
158 case DBG_REASON_NOTHALTED
:
159 return 0x0; /* no signal... shouldn't happen */
161 LOG_USER("undefined debug reason %d - target needs reset",
162 target
->debug_reason
);
167 static int check_pending(struct connection
*connection
,
168 int timeout_s
, int *got_data
)
170 /* a non-blocking socket will block if there is 0 bytes available on the socket,
171 * but return with as many bytes as are available immediately
175 struct gdb_connection
*gdb_con
= connection
->priv
;
177 if (got_data
== NULL
)
181 if (gdb_con
->buf_cnt
> 0) {
187 FD_SET(connection
->fd
, &read_fds
);
189 tv
.tv_sec
= timeout_s
;
191 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
192 /* This can typically be because a "monitor" command took too long
193 * before printing any progress messages
196 return ERROR_GDB_TIMEOUT
;
200 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
204 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
206 struct gdb_connection
*gdb_con
= connection
->priv
;
207 int retval
= ERROR_OK
;
209 #ifdef _DEBUG_GDB_IO_
213 if (connection
->service
->type
!= CONNECTION_TCP
)
214 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
216 retval
= check_pending(connection
, 1, NULL
);
217 if (retval
!= ERROR_OK
)
219 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
224 if (gdb_con
->buf_cnt
> 0)
226 if (gdb_con
->buf_cnt
== 0) {
227 gdb_con
->closed
= true;
228 return ERROR_SERVER_REMOTE_CLOSED
;
232 errno
= WSAGetLastError();
238 case WSAECONNABORTED
:
239 gdb_con
->closed
= true;
240 return ERROR_SERVER_REMOTE_CLOSED
;
242 gdb_con
->closed
= true;
243 return ERROR_SERVER_REMOTE_CLOSED
;
245 LOG_ERROR("read: %d", errno
);
254 gdb_con
->closed
= true;
255 return ERROR_SERVER_REMOTE_CLOSED
;
257 gdb_con
->closed
= true;
258 return ERROR_SERVER_REMOTE_CLOSED
;
260 LOG_ERROR("read: %s", strerror(errno
));
261 gdb_con
->closed
= true;
262 return ERROR_SERVER_REMOTE_CLOSED
;
267 #ifdef _DEBUG_GDB_IO_
268 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
269 LOG_DEBUG("received '%s'", debug_buffer
);
273 gdb_con
->buf_p
= gdb_con
->buffer
;
275 *next_char
= *(gdb_con
->buf_p
++);
276 if (gdb_con
->buf_cnt
> 0)
277 connection
->input_pending
= 1;
279 connection
->input_pending
= 0;
280 #ifdef _DEBUG_GDB_IO_
281 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
288 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
289 * held in registers in the inner loop.
291 * For small caches and embedded systems this is important!
293 static inline int gdb_get_char_fast(struct connection
*connection
,
294 int *next_char
, char **buf_p
, int *buf_cnt
)
296 int retval
= ERROR_OK
;
298 if ((*buf_cnt
)-- > 0) {
299 *next_char
= **buf_p
;
302 connection
->input_pending
= 1;
304 connection
->input_pending
= 0;
306 #ifdef _DEBUG_GDB_IO_
307 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
313 struct gdb_connection
*gdb_con
= connection
->priv
;
314 gdb_con
->buf_p
= *buf_p
;
315 gdb_con
->buf_cnt
= *buf_cnt
;
316 retval
= gdb_get_char_inner(connection
, next_char
);
317 *buf_p
= gdb_con
->buf_p
;
318 *buf_cnt
= gdb_con
->buf_cnt
;
323 static int gdb_get_char(struct connection
*connection
, int *next_char
)
325 struct gdb_connection
*gdb_con
= connection
->priv
;
326 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
329 static int gdb_putback_char(struct connection
*connection
, int last_char
)
331 struct gdb_connection
*gdb_con
= connection
->priv
;
333 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
334 *(--gdb_con
->buf_p
) = last_char
;
337 LOG_ERROR("BUG: couldn't put character back");
342 /* The only way we can detect that the socket is closed is the first time
343 * we write to it, we will fail. Subsequent write operations will
344 * succeed. Shudder! */
345 static int gdb_write(struct connection
*connection
, void *data
, int len
)
347 struct gdb_connection
*gdb_con
= connection
->priv
;
349 return ERROR_SERVER_REMOTE_CLOSED
;
351 if (connection_write(connection
, data
, len
) == len
)
353 gdb_con
->closed
= true;
354 return ERROR_SERVER_REMOTE_CLOSED
;
357 static int gdb_put_packet_inner(struct connection
*connection
,
358 char *buffer
, int len
)
361 unsigned char my_checksum
= 0;
362 #ifdef _DEBUG_GDB_IO_
367 struct gdb_connection
*gdb_con
= connection
->priv
;
369 for (i
= 0; i
< len
; i
++)
370 my_checksum
+= buffer
[i
];
372 #ifdef _DEBUG_GDB_IO_
374 * At this point we should have nothing in the input queue from GDB,
375 * however sometimes '-' is sent even though we've already received
376 * an ACK (+) for everything we've sent off.
380 retval
= check_pending(connection
, 0, &gotdata
);
381 if (retval
!= ERROR_OK
)
385 retval
= gdb_get_char(connection
, &reply
);
386 if (retval
!= ERROR_OK
)
389 /* fix a problem with some IAR tools */
390 gdb_putback_char(connection
, reply
);
391 LOG_DEBUG("Unexpected start of new packet");
395 LOG_WARNING("Discard unexpected char %c", reply
);
400 #ifdef _DEBUG_GDB_IO_
401 debug_buffer
= strndup(buffer
, len
);
402 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
406 char local_buffer
[1024];
407 local_buffer
[0] = '$';
408 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
409 /* performance gain on smaller packets by only a single call to gdb_write() */
410 memcpy(local_buffer
+ 1, buffer
, len
++);
411 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
412 retval
= gdb_write(connection
, local_buffer
, len
);
413 if (retval
!= ERROR_OK
)
416 /* larger packets are transmitted directly from caller supplied buffer
417 * by several calls to gdb_write() to avoid dynamic allocation */
418 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
419 retval
= gdb_write(connection
, local_buffer
, 1);
420 if (retval
!= ERROR_OK
)
422 retval
= gdb_write(connection
, buffer
, len
);
423 if (retval
!= ERROR_OK
)
425 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
426 if (retval
!= ERROR_OK
)
430 if (gdb_con
->noack_mode
)
433 retval
= gdb_get_char(connection
, &reply
);
434 if (retval
!= ERROR_OK
)
439 else if (reply
== '-') {
440 /* Stop sending output packets for now */
441 log_remove_callback(gdb_log_callback
, connection
);
442 LOG_WARNING("negative reply, retrying");
443 } else if (reply
== 0x3) {
445 retval
= gdb_get_char(connection
, &reply
);
446 if (retval
!= ERROR_OK
)
450 else if (reply
== '-') {
451 /* Stop sending output packets for now */
452 log_remove_callback(gdb_log_callback
, connection
);
453 LOG_WARNING("negative reply, retrying");
454 } else if (reply
== '$') {
455 LOG_ERROR("GDB missing ack(1) - assumed good");
456 gdb_putback_char(connection
, reply
);
459 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
460 gdb_con
->closed
= true;
461 return ERROR_SERVER_REMOTE_CLOSED
;
463 } else if (reply
== '$') {
464 LOG_ERROR("GDB missing ack(2) - assumed good");
465 gdb_putback_char(connection
, reply
);
468 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
470 gdb_con
->closed
= true;
471 return ERROR_SERVER_REMOTE_CLOSED
;
475 return ERROR_SERVER_REMOTE_CLOSED
;
480 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
482 struct gdb_connection
*gdb_con
= connection
->priv
;
483 gdb_con
->busy
= true;
484 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
485 gdb_con
->busy
= false;
487 /* we sent some data, reset timer for keep alive messages */
493 static inline int fetch_packet(struct connection
*connection
,
494 int *checksum_ok
, int noack
, int *len
, char *buffer
)
496 unsigned char my_checksum
= 0;
499 int retval
= ERROR_OK
;
501 struct gdb_connection
*gdb_con
= connection
->priv
;
506 /* move this over into local variables to use registers and give the
507 * more freedom to optimize */
508 char *buf_p
= gdb_con
->buf_p
;
509 int buf_cnt
= gdb_con
->buf_cnt
;
512 /* The common case is that we have an entire packet with no escape chars.
513 * We need to leave at least 2 bytes in the buffer to have
514 * gdb_get_char() update various bits and bobs correctly.
516 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
517 /* The compiler will struggle a bit with constant propagation and
518 * aliasing, so we help it by showing that these values do not
519 * change inside the loop
523 int run
= buf_cnt
- 2;
529 if (character
== '#') {
530 /* Danger! character can be '#' when esc is
531 * used so we need an explicit boolean for done here. */
536 if (character
== '}') {
537 /* data transmitted in binary mode (X packet)
538 * uses 0x7d as escape character */
539 my_checksum
+= character
& 0xff;
542 my_checksum
+= character
& 0xff;
543 buffer
[count
++] = (character
^ 0x20) & 0xff;
545 my_checksum
+= character
& 0xff;
546 buffer
[count
++] = character
& 0xff;
555 LOG_ERROR("packet buffer too small");
556 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
560 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
561 if (retval
!= ERROR_OK
)
564 if (character
== '#')
567 if (character
== '}') {
568 /* data transmitted in binary mode (X packet)
569 * uses 0x7d as escape character */
570 my_checksum
+= character
& 0xff;
572 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
573 if (retval
!= ERROR_OK
)
576 my_checksum
+= character
& 0xff;
577 buffer
[count
++] = (character
^ 0x20) & 0xff;
579 my_checksum
+= character
& 0xff;
580 buffer
[count
++] = character
& 0xff;
584 gdb_con
->buf_p
= buf_p
;
585 gdb_con
->buf_cnt
= buf_cnt
;
587 if (retval
!= ERROR_OK
)
592 retval
= gdb_get_char(connection
, &character
);
593 if (retval
!= ERROR_OK
)
595 checksum
[0] = character
;
596 retval
= gdb_get_char(connection
, &character
);
597 if (retval
!= ERROR_OK
)
599 checksum
[1] = character
;
603 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
608 static int gdb_get_packet_inner(struct connection
*connection
,
609 char *buffer
, int *len
)
613 struct gdb_connection
*gdb_con
= connection
->priv
;
617 retval
= gdb_get_char(connection
, &character
);
618 if (retval
!= ERROR_OK
)
621 #ifdef _DEBUG_GDB_IO_
622 LOG_DEBUG("character: '%c'", character
);
629 /* According to the GDB documentation
630 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
631 * "gdb sends a final `+` acknowledgment of the stub's `OK`
632 * response, which can be safely ignored by the stub."
633 * However OpenOCD server already is in noack mode at this
634 * point and instead of ignoring this it was emitting a
635 * warning. This code makes server ignore the first ACK
636 * that will be received after going into noack mode,
637 * warning only about subsequent ACK's. */
638 if (gdb_con
->noack_mode
> 1) {
639 LOG_WARNING("acknowledgment received, but no packet pending");
640 } else if (gdb_con
->noack_mode
) {
641 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
642 gdb_con
->noack_mode
= 2;
646 LOG_WARNING("negative acknowledgment, but no packet pending");
653 LOG_WARNING("ignoring character 0x%x", character
);
656 } while (character
!= '$');
659 /* explicit code expansion here to get faster inlined code in -O3 by not
660 * calculating checksum */
661 if (gdb_con
->noack_mode
) {
662 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
663 if (retval
!= ERROR_OK
)
666 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
667 if (retval
!= ERROR_OK
)
671 if (gdb_con
->noack_mode
) {
672 /* checksum is not checked in noack mode */
676 retval
= gdb_write(connection
, "+", 1);
677 if (retval
!= ERROR_OK
)
683 return ERROR_SERVER_REMOTE_CLOSED
;
688 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
690 struct gdb_connection
*gdb_con
= connection
->priv
;
691 gdb_con
->busy
= true;
692 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
693 gdb_con
->busy
= false;
697 static int gdb_output_con(struct connection
*connection
, const char *line
)
702 bin_size
= strlen(line
);
704 hex_buffer
= malloc(bin_size
* 2 + 2);
705 if (hex_buffer
== NULL
)
706 return ERROR_GDB_BUFFER_TOO_SMALL
;
709 size_t pkt_len
= hexify(hex_buffer
+ 1, (const uint8_t *)line
, bin_size
,
711 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
717 static int gdb_output(struct command_context
*context
, const char *line
)
719 /* this will be dumped to the log and also sent as an O packet if possible */
720 LOG_USER_N("%s", line
);
724 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
726 struct gdb_connection
*gdb_connection
= connection
->priv
;
728 char stop_reason
[20];
729 char current_thread
[25];
733 rtos_update_threads(target
);
735 if (target
->debug_reason
== DBG_REASON_EXIT
) {
736 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
738 if (gdb_connection
->ctrl_c
) {
741 signal_var
= gdb_last_signal(target
);
743 stop_reason
[0] = '\0';
744 if (target
->debug_reason
== DBG_REASON_WATCHPOINT
) {
745 enum watchpoint_rw hit_wp_type
;
746 target_addr_t hit_wp_address
;
748 if (watchpoint_hit(target
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
750 switch (hit_wp_type
) {
752 snprintf(stop_reason
, sizeof(stop_reason
),
753 "watch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
756 snprintf(stop_reason
, sizeof(stop_reason
),
757 "rwatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
760 snprintf(stop_reason
, sizeof(stop_reason
),
761 "awatch:%08" TARGET_PRIxADDR
";", hit_wp_address
);
769 current_thread
[0] = '\0';
770 if (target
->rtos
!= NULL
) {
772 snprintf(current_thread
, sizeof(current_thread
), "thread:%016" PRIx64
";",
773 target
->rtos
->current_thread
);
774 target
->rtos
->current_threadid
= target
->rtos
->current_thread
;
775 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &ct
);
776 if (!gdb_connection
->ctrl_c
)
777 signal_var
= gdb_last_signal(ct
);
780 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s%s",
781 signal_var
, stop_reason
, current_thread
);
783 gdb_connection
->ctrl_c
= 0;
786 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
787 gdb_connection
->frontend_state
= TARGET_HALTED
;
790 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
792 struct gdb_connection
*gdb_connection
= connection
->priv
;
793 char fileio_command
[256];
795 bool program_exited
= false;
797 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
798 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
799 target
->fileio_info
->param_1
,
800 target
->fileio_info
->param_2
,
801 target
->fileio_info
->param_3
,
802 target
->fileio_info
->param_4
);
803 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
804 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
805 target
->fileio_info
->param_1
);
806 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
807 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
808 target
->fileio_info
->param_1
,
809 target
->fileio_info
->param_2
,
810 target
->fileio_info
->param_3
);
811 else if (strcmp(target
->fileio_info
->identifier
, "write") == 0)
812 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
813 target
->fileio_info
->param_1
,
814 target
->fileio_info
->param_2
,
815 target
->fileio_info
->param_3
);
816 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
817 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
818 target
->fileio_info
->param_1
,
819 target
->fileio_info
->param_2
,
820 target
->fileio_info
->param_3
);
821 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
822 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
823 target
->fileio_info
->param_1
,
824 target
->fileio_info
->param_2
,
825 target
->fileio_info
->param_3
,
826 target
->fileio_info
->param_4
);
827 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
828 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
829 target
->fileio_info
->param_1
,
830 target
->fileio_info
->param_2
);
831 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
832 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
833 target
->fileio_info
->param_1
,
834 target
->fileio_info
->param_2
,
835 target
->fileio_info
->param_3
);
836 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
837 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
838 target
->fileio_info
->param_1
,
839 target
->fileio_info
->param_2
);
840 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
841 sprintf(fileio_command
, "F%s,%" PRIx64
",%" PRIx64
, target
->fileio_info
->identifier
,
842 target
->fileio_info
->param_1
,
843 target
->fileio_info
->param_2
);
844 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
845 sprintf(fileio_command
, "F%s,%" PRIx64
, target
->fileio_info
->identifier
,
846 target
->fileio_info
->param_1
);
847 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
848 sprintf(fileio_command
, "F%s,%" PRIx64
"/%" PRIx64
, target
->fileio_info
->identifier
,
849 target
->fileio_info
->param_1
,
850 target
->fileio_info
->param_2
);
851 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
852 /* If target hits exit syscall, report to GDB the program is terminated.
853 * In addition, let target run its own exit syscall handler. */
854 program_exited
= true;
855 sprintf(fileio_command
, "W%02" PRIx64
, target
->fileio_info
->param_1
);
857 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
859 /* encounter unknown syscall, continue */
860 gdb_connection
->frontend_state
= TARGET_RUNNING
;
861 target_resume(target
, 1, 0x0, 0, 0);
865 command_len
= strlen(fileio_command
);
866 gdb_put_packet(connection
, fileio_command
, command_len
);
868 if (program_exited
) {
869 /* Use target_resume() to let target run its own exit syscall handler. */
870 gdb_connection
->frontend_state
= TARGET_RUNNING
;
871 target_resume(target
, 1, 0x0, 0, 0);
873 gdb_connection
->frontend_state
= TARGET_HALTED
;
874 rtos_update_threads(target
);
878 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
880 struct gdb_connection
*gdb_connection
= connection
->priv
;
882 /* In the GDB protocol when we are stepping or continuing execution,
883 * we have a lingering reply. Upon receiving a halted event
884 * when we have that lingering packet, we reply to the original
885 * step or continue packet.
887 * Executing monitor commands can bring the target in and
888 * out of the running state so we'll see lots of TARGET_EVENT_XXX
889 * that are to be ignored.
891 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
892 /* stop forwarding log packets! */
893 log_remove_callback(gdb_log_callback
, connection
);
895 /* check fileio first */
896 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
897 gdb_fileio_reply(target
, connection
);
899 gdb_signal_reply(target
, connection
);
903 static int gdb_target_callback_event_handler(struct target
*target
,
904 enum target_event event
, void *priv
)
907 struct connection
*connection
= priv
;
908 struct gdb_service
*gdb_service
= connection
->service
->priv
;
910 if (gdb_service
->target
!= target
)
914 case TARGET_EVENT_GDB_HALT
:
915 gdb_frontend_halted(target
, connection
);
917 case TARGET_EVENT_HALTED
:
918 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
920 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
921 retval
= jtag_execute_queue();
922 if (retval
!= ERROR_OK
)
932 static int gdb_new_connection(struct connection
*connection
)
934 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
935 struct target
*target
;
939 target
= get_target_from_connection(connection
);
940 connection
->priv
= gdb_connection
;
941 connection
->cmd_ctx
->current_target
= target
;
943 /* initialize gdb connection information */
944 gdb_connection
->buf_p
= gdb_connection
->buffer
;
945 gdb_connection
->buf_cnt
= 0;
946 gdb_connection
->ctrl_c
= 0;
947 gdb_connection
->frontend_state
= TARGET_HALTED
;
948 gdb_connection
->vflash_image
= NULL
;
949 gdb_connection
->closed
= false;
950 gdb_connection
->busy
= false;
951 gdb_connection
->noack_mode
= 0;
952 gdb_connection
->sync
= false;
953 gdb_connection
->mem_write_error
= false;
954 gdb_connection
->attached
= true;
955 gdb_connection
->target_desc
.tdesc
= NULL
;
956 gdb_connection
->target_desc
.tdesc_length
= 0;
957 gdb_connection
->thread_list
= NULL
;
959 /* send ACK to GDB for debug request */
960 gdb_write(connection
, "+", 1);
962 /* output goes through gdb connection */
963 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
965 /* we must remove all breakpoints registered to the target as a previous
966 * GDB session could leave dangling breakpoints if e.g. communication
969 breakpoint_clear_target(target
);
970 watchpoint_clear_target(target
);
973 /* clean previous rtos session if supported*/
974 if (target
->rtos
->type
->clean
)
975 target
->rtos
->type
->clean(target
);
978 rtos_update_threads(target
);
981 /* remove the initial ACK from the incoming buffer */
982 retval
= gdb_get_char(connection
, &initial_ack
);
983 if (retval
!= ERROR_OK
)
986 /* FIX!!!??? would we actually ever receive a + here???
989 if (initial_ack
!= '+')
990 gdb_putback_char(connection
, initial_ack
);
991 target_call_event_callbacks(target
, TARGET_EVENT_GDB_ATTACH
);
993 if (gdb_use_memory_map
) {
994 /* Connect must fail if the memory map can't be set up correctly.
996 * This will cause an auto_probe to be invoked, which is either
997 * a no-op or it will fail when the target isn't ready(e.g. not halted).
1000 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1001 struct flash_bank
*p
;
1002 p
= get_flash_bank_by_num_noprobe(i
);
1003 if (p
->target
!= target
)
1005 retval
= get_flash_bank_by_num(i
, &p
);
1006 if (retval
!= ERROR_OK
) {
1007 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
1008 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1014 gdb_actual_connections
++;
1015 log_printf_lf(all_targets
->next
!= NULL
? LOG_LVL_INFO
: LOG_LVL_DEBUG
,
1016 __FILE__
, __LINE__
, __func__
,
1017 "New GDB Connection: %d, Target %s, state: %s",
1018 gdb_actual_connections
,
1019 target_name(target
),
1020 target_state_name(target
));
1022 /* DANGER! If we fail subsequently, we must remove this handler,
1023 * otherwise we occasionally see crashes as the timer can invoke the
1026 * register callback to be informed about target events */
1027 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1032 static int gdb_connection_closed(struct connection
*connection
)
1034 struct target
*target
;
1035 struct gdb_connection
*gdb_connection
= connection
->priv
;
1037 target
= get_target_from_connection(connection
);
1039 /* we're done forwarding messages. Tear down callback before
1040 * cleaning up connection.
1042 log_remove_callback(gdb_log_callback
, connection
);
1044 gdb_actual_connections
--;
1045 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1046 target_name(target
),
1047 target_state_name(target
),
1048 gdb_actual_connections
);
1050 /* see if an image built with vFlash commands is left */
1051 if (gdb_connection
->vflash_image
) {
1052 image_close(gdb_connection
->vflash_image
);
1053 free(gdb_connection
->vflash_image
);
1054 gdb_connection
->vflash_image
= NULL
;
1057 /* if this connection registered a debug-message receiver delete it */
1058 delete_debug_msg_receiver(connection
->cmd_ctx
, target
);
1060 if (connection
->priv
) {
1061 free(connection
->priv
);
1062 connection
->priv
= NULL
;
1064 LOG_ERROR("BUG: connection->priv == NULL");
1066 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1068 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
1070 target_call_event_callbacks(target
, TARGET_EVENT_GDB_DETACH
);
1075 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1078 snprintf(err
, 4, "E%2.2X", the_error
);
1079 gdb_put_packet(connection
, err
, 3);
1082 static int gdb_last_signal_packet(struct connection
*connection
,
1083 char const *packet
, int packet_size
)
1085 struct target
*target
= get_target_from_connection(connection
);
1086 struct gdb_connection
*gdb_con
= connection
->priv
;
1090 if (!gdb_con
->attached
) {
1091 /* if we are here we have received a kill packet
1092 * reply W stop reply otherwise gdb gets very unhappy */
1093 gdb_put_packet(connection
, "W00", 3);
1097 signal_var
= gdb_last_signal(target
);
1099 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1100 gdb_put_packet(connection
, sig_reply
, 3);
1105 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1107 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1110 return len
- 1 - pos
;
1113 /* Convert register to string of bytes. NB! The # of bits in the
1114 * register might be non-divisible by 8(a byte), in which
1115 * case an entire byte is shown.
1117 * NB! the format on the wire is the target endianness
1119 * The format of reg->value is little endian
1122 static void gdb_str_to_target(struct target
*target
,
1123 char *tstr
, struct reg
*reg
)
1130 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1132 for (i
= 0; i
< buf_len
; i
++) {
1133 int j
= gdb_reg_pos(target
, i
, buf_len
);
1134 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1138 /* copy over in register buffer */
1139 static void gdb_target_to_reg(struct target
*target
,
1140 char const *tstr
, int str_len
, uint8_t *bin
)
1143 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1148 for (i
= 0; i
< str_len
; i
+= 2) {
1150 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1151 LOG_ERROR("BUG: unable to convert register value");
1155 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1160 static int gdb_get_registers_packet(struct connection
*connection
,
1161 char const *packet
, int packet_size
)
1163 struct target
*target
= get_target_from_connection(connection
);
1164 struct reg
**reg_list
;
1167 int reg_packet_size
= 0;
1172 #ifdef _DEBUG_GDB_IO_
1176 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1179 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1181 if (retval
!= ERROR_OK
)
1182 return gdb_error(connection
, retval
);
1184 for (i
= 0; i
< reg_list_size
; i
++) {
1185 if (reg_list
[i
] == NULL
|| reg_list
[i
]->exist
== false)
1187 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1190 assert(reg_packet_size
> 0);
1192 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1193 if (reg_packet
== NULL
)
1196 reg_packet_p
= reg_packet
;
1198 for (i
= 0; i
< reg_list_size
; i
++) {
1199 if (reg_list
[i
] == NULL
|| reg_list
[i
]->exist
== false)
1201 if (!reg_list
[i
]->valid
) {
1202 retval
= reg_list
[i
]->type
->get(reg_list
[i
]);
1203 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1204 LOG_DEBUG("Couldn't get register %s.", reg_list
[i
]->name
);
1207 return gdb_error(connection
, retval
);
1210 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1211 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1214 #ifdef _DEBUG_GDB_IO_
1216 char *reg_packet_p_debug
;
1217 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1218 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1219 free(reg_packet_p_debug
);
1223 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1231 static int gdb_set_registers_packet(struct connection
*connection
,
1232 char const *packet
, int packet_size
)
1234 struct target
*target
= get_target_from_connection(connection
);
1236 struct reg
**reg_list
;
1239 char const *packet_p
;
1241 #ifdef _DEBUG_GDB_IO_
1245 /* skip command character */
1249 if (packet_size
% 2) {
1250 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1251 return ERROR_SERVER_REMOTE_CLOSED
;
1254 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1256 if (retval
!= ERROR_OK
)
1257 return gdb_error(connection
, retval
);
1260 for (i
= 0; i
< reg_list_size
; i
++) {
1262 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1264 if (packet_p
+ chars
> packet
+ packet_size
)
1265 LOG_ERROR("BUG: register packet is too small for registers");
1267 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1268 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1270 retval
= reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1271 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1272 LOG_DEBUG("Couldn't set register %s.", reg_list
[i
]->name
);
1275 return gdb_error(connection
, retval
);
1278 /* advance packet pointer */
1284 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1287 gdb_put_packet(connection
, "OK", 2);
1292 static int gdb_get_register_packet(struct connection
*connection
,
1293 char const *packet
, int packet_size
)
1295 struct target
*target
= get_target_from_connection(connection
);
1297 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1298 struct reg
**reg_list
;
1302 #ifdef _DEBUG_GDB_IO_
1306 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg(connection
, reg_num
)))
1309 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1311 if (retval
!= ERROR_OK
)
1312 return gdb_error(connection
, retval
);
1314 if (reg_list_size
<= reg_num
) {
1315 LOG_ERROR("gdb requested a non-existing register");
1316 return ERROR_SERVER_REMOTE_CLOSED
;
1319 if (!reg_list
[reg_num
]->valid
) {
1320 retval
= reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1321 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1322 LOG_DEBUG("Couldn't get register %s.", reg_list
[reg_num
]->name
);
1324 return gdb_error(connection
, retval
);
1328 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1); /* plus one for string termination null */
1330 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1332 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1340 static int gdb_set_register_packet(struct connection
*connection
,
1341 char const *packet
, int packet_size
)
1343 struct target
*target
= get_target_from_connection(connection
);
1346 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1347 struct reg
**reg_list
;
1353 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1355 if (retval
!= ERROR_OK
)
1356 return gdb_error(connection
, retval
);
1358 if (reg_list_size
<= reg_num
) {
1359 LOG_ERROR("gdb requested a non-existing register");
1360 return ERROR_SERVER_REMOTE_CLOSED
;
1363 if (*separator
!= '=') {
1364 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1365 return ERROR_SERVER_REMOTE_CLOSED
;
1368 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1369 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1370 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1372 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1373 LOG_ERROR("gdb sent %zu bits for a %d-bit register (%s)",
1374 strlen(separator
+ 1) * 4, chars
* 4, reg_list
[reg_num
]->name
);
1376 return ERROR_SERVER_REMOTE_CLOSED
;
1379 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1381 retval
= reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1382 if (retval
!= ERROR_OK
&& gdb_report_register_access_error
) {
1383 LOG_DEBUG("Couldn't set register %s.", reg_list
[reg_num
]->name
);
1386 return gdb_error(connection
, retval
);
1389 gdb_put_packet(connection
, "OK", 2);
1397 /* No attempt is made to translate the "retval" to
1398 * GDB speak. This has to be done at the calling
1399 * site as no mapping really exists.
1401 static int gdb_error(struct connection
*connection
, int retval
)
1403 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1404 gdb_send_error(connection
, EFAULT
);
1408 /* We don't have to worry about the default 2 second timeout for GDB packets,
1409 * because GDB breaks up large memory reads into smaller reads.
1411 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1413 static int gdb_read_memory_packet(struct connection
*connection
,
1414 char const *packet
, int packet_size
)
1416 struct target
*target
= get_target_from_connection(connection
);
1424 int retval
= ERROR_OK
;
1426 /* skip command character */
1429 addr
= strtoull(packet
, &separator
, 16);
1431 if (*separator
!= ',') {
1432 LOG_ERROR("incomplete read memory packet received, dropping connection");
1433 return ERROR_SERVER_REMOTE_CLOSED
;
1436 len
= strtoul(separator
+ 1, NULL
, 16);
1439 LOG_WARNING("invalid read memory packet received (len == 0)");
1440 gdb_put_packet(connection
, NULL
, 0);
1444 buffer
= malloc(len
);
1446 LOG_DEBUG("addr: 0x%16.16" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1448 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1450 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1451 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1452 * At some point this might be fixed in GDB, in which case this code can be removed.
1454 * OpenOCD developers are acutely aware of this problem, but there is nothing
1455 * gained by involving the user in this problem that hopefully will get resolved
1458 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1459 * cmd = view%20audit-trail&database = gdb&pr = 2395
1461 * For now, the default is to fix up things to make current GDB versions work.
1462 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1464 memset(buffer
, 0, len
);
1468 if (retval
== ERROR_OK
) {
1469 hex_buffer
= malloc(len
* 2 + 1);
1471 size_t pkt_len
= hexify(hex_buffer
, buffer
, len
, len
* 2 + 1);
1473 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1477 retval
= gdb_error(connection
, retval
);
1484 static int gdb_write_memory_packet(struct connection
*connection
,
1485 char const *packet
, int packet_size
)
1487 struct target
*target
= get_target_from_connection(connection
);
1495 /* skip command character */
1498 addr
= strtoull(packet
, &separator
, 16);
1500 if (*separator
!= ',') {
1501 LOG_ERROR("incomplete write memory packet received, dropping connection");
1502 return ERROR_SERVER_REMOTE_CLOSED
;
1505 len
= strtoul(separator
+ 1, &separator
, 16);
1507 if (*(separator
++) != ':') {
1508 LOG_ERROR("incomplete write memory packet received, dropping connection");
1509 return ERROR_SERVER_REMOTE_CLOSED
;
1512 buffer
= malloc(len
);
1514 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1516 if (unhexify(buffer
, separator
, len
) != len
)
1517 LOG_ERROR("unable to decode memory packet");
1519 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1521 if (retval
== ERROR_OK
)
1522 gdb_put_packet(connection
, "OK", 2);
1524 retval
= gdb_error(connection
, retval
);
1531 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1532 char const *packet
, int packet_size
)
1534 struct target
*target
= get_target_from_connection(connection
);
1539 int retval
= ERROR_OK
;
1540 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1541 * the assumption that we're in a download and it's important to go as fast
1543 uint32_t fast_limit
= 8;
1545 /* skip command character */
1548 addr
= strtoull(packet
, &separator
, 16);
1550 if (*separator
!= ',') {
1551 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1552 return ERROR_SERVER_REMOTE_CLOSED
;
1555 len
= strtoul(separator
+ 1, &separator
, 16);
1557 if (*(separator
++) != ':') {
1558 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1559 return ERROR_SERVER_REMOTE_CLOSED
;
1562 struct gdb_connection
*gdb_connection
= connection
->priv
;
1564 if (gdb_connection
->mem_write_error
)
1565 retval
= ERROR_FAIL
;
1567 if (retval
== ERROR_OK
) {
1568 if (len
>= fast_limit
) {
1569 /* By replying the packet *immediately* GDB will send us a new packet
1570 * while we write the last one to the target.
1571 * We only do this for larger writes, so that users who do something like:
1572 * p *((int*)0xdeadbeef)=8675309
1573 * will get immediate feedback that that write failed.
1575 gdb_put_packet(connection
, "OK", 2);
1578 retval
= gdb_error(connection
, retval
);
1579 /* now that we have reported the memory write error, we can clear the condition */
1580 gdb_connection
->mem_write_error
= false;
1581 if (retval
!= ERROR_OK
)
1586 LOG_DEBUG("addr: 0x%" PRIx64
", len: 0x%8.8" PRIx32
"", addr
, len
);
1588 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1589 if (retval
!= ERROR_OK
)
1590 gdb_connection
->mem_write_error
= true;
1593 if (len
< fast_limit
) {
1594 if (retval
!= ERROR_OK
) {
1595 gdb_error(connection
, retval
);
1596 gdb_connection
->mem_write_error
= false;
1598 gdb_put_packet(connection
, "OK", 2);
1605 static int gdb_step_continue_packet(struct connection
*connection
,
1606 char const *packet
, int packet_size
)
1608 struct target
*target
= get_target_from_connection(connection
);
1610 uint64_t address
= 0x0;
1611 int retval
= ERROR_OK
;
1615 if (packet_size
> 1)
1616 address
= strtoull(packet
+ 1, NULL
, 16);
1620 gdb_running_type
= packet
[0];
1621 if (packet
[0] == 'c') {
1622 LOG_DEBUG("continue");
1623 /* resume at current address, don't handle breakpoints, not debugging */
1624 retval
= target_resume(target
, current
, address
, 0, 0);
1625 } else if (packet
[0] == 's') {
1627 /* step at current or address, don't handle breakpoints */
1628 retval
= target_step(target
, current
, address
, 0);
1633 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1634 char const *packet
, int packet_size
)
1636 struct target
*target
= get_target_from_connection(connection
);
1638 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1639 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1647 type
= strtoul(packet
+ 1, &separator
, 16);
1649 if (type
== 0) /* memory breakpoint */
1650 bp_type
= BKPT_SOFT
;
1651 else if (type
== 1) /* hardware breakpoint */
1652 bp_type
= BKPT_HARD
;
1653 else if (type
== 2) /* write watchpoint */
1654 wp_type
= WPT_WRITE
;
1655 else if (type
== 3) /* read watchpoint */
1657 else if (type
== 4) /* access watchpoint */
1658 wp_type
= WPT_ACCESS
;
1660 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1661 return ERROR_SERVER_REMOTE_CLOSED
;
1664 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1665 bp_type
= gdb_breakpoint_override_type
;
1667 if (*separator
!= ',') {
1668 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1669 return ERROR_SERVER_REMOTE_CLOSED
;
1672 address
= strtoull(separator
+ 1, &separator
, 16);
1674 if (*separator
!= ',') {
1675 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1676 return ERROR_SERVER_REMOTE_CLOSED
;
1679 size
= strtoul(separator
+ 1, &separator
, 16);
1684 if (packet
[0] == 'Z') {
1685 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1686 if (retval
!= ERROR_OK
) {
1687 retval
= gdb_error(connection
, retval
);
1688 if (retval
!= ERROR_OK
)
1691 gdb_put_packet(connection
, "OK", 2);
1693 breakpoint_remove(target
, address
);
1694 gdb_put_packet(connection
, "OK", 2);
1701 if (packet
[0] == 'Z') {
1702 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1703 if (retval
!= ERROR_OK
) {
1704 retval
= gdb_error(connection
, retval
);
1705 if (retval
!= ERROR_OK
)
1708 gdb_put_packet(connection
, "OK", 2);
1710 watchpoint_remove(target
, address
);
1711 gdb_put_packet(connection
, "OK", 2);
1722 /* print out a string and allocate more space as needed,
1723 * mainly used for XML at this point
1725 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1726 const char *fmt
, ...)
1728 if (*retval
!= ERROR_OK
)
1733 if ((*xml
== NULL
) || (!first
)) {
1734 /* start by 0 to exercise all the code paths.
1735 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1737 *size
= *size
* 2 + 2;
1739 *xml
= realloc(*xml
, *size
);
1743 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1751 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1753 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1757 /* there was just enough or not enough space, allocate more. */
1762 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1764 /* Locate the annex. */
1765 const char *annex_end
= strchr(buf
, ':');
1766 if (annex_end
== NULL
)
1769 /* After the read marker and annex, qXfer looks like a
1770 * traditional 'm' packet. */
1772 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1774 if (*separator
!= ',')
1777 *len
= strtoul(separator
+ 1, NULL
, 16);
1779 /* Extract the annex if needed */
1780 if (annex
!= NULL
) {
1781 *annex
= strndup(buf
, annex_end
- buf
);
1789 static int compare_bank(const void *a
, const void *b
)
1791 struct flash_bank
*b1
, *b2
;
1792 b1
= *((struct flash_bank
**)a
);
1793 b2
= *((struct flash_bank
**)b
);
1795 if (b1
->base
== b2
->base
)
1797 else if (b1
->base
> b2
->base
)
1803 static int gdb_memory_map(struct connection
*connection
,
1804 char const *packet
, int packet_size
)
1806 /* We get away with only specifying flash here. Regions that are not
1807 * specified are treated as if we provided no memory map(if not we
1808 * could detect the holes and mark them as RAM).
1809 * Normally we only execute this code once, but no big deal if we
1810 * have to regenerate it a couple of times.
1813 struct target
*target
= get_target_from_connection(connection
);
1814 struct flash_bank
*p
;
1818 int retval
= ERROR_OK
;
1819 struct flash_bank
**banks
;
1823 target_addr_t ram_start
= 0;
1825 int target_flash_banks
= 0;
1827 /* skip command character */
1830 offset
= strtoul(packet
, &separator
, 16);
1831 length
= strtoul(separator
+ 1, &separator
, 16);
1833 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1835 /* Sort banks in ascending order. We need to report non-flash
1836 * memory as ram (or rather read/write) by default for GDB, since
1837 * it has no concept of non-cacheable read/write memory (i/o etc).
1839 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1841 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1842 p
= get_flash_bank_by_num_noprobe(i
);
1843 if (p
->target
!= target
)
1845 retval
= get_flash_bank_by_num(i
, &p
);
1846 if (retval
!= ERROR_OK
) {
1848 gdb_error(connection
, retval
);
1851 banks
[target_flash_banks
++] = p
;
1854 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1857 for (i
= 0; i
< target_flash_banks
; i
++) {
1859 unsigned sector_size
= 0;
1860 unsigned group_len
= 0;
1864 if (ram_start
< p
->base
)
1865 xml_printf(&retval
, &xml
, &pos
, &size
,
1866 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1867 "length=\"0x%x\"/>\n",
1868 ram_start
, p
->base
- ram_start
);
1870 /* Report adjacent groups of same-size sectors. So for
1871 * example top boot CFI flash will list an initial region
1872 * with several large sectors (maybe 128KB) and several
1873 * smaller ones at the end (maybe 32KB). STR7 will have
1874 * regions with 8KB, 32KB, and 64KB sectors; etc.
1876 for (j
= 0; j
< p
->num_sectors
; j
++) {
1878 /* Maybe start a new group of sectors. */
1879 if (sector_size
== 0) {
1880 if (p
->sectors
[j
].offset
+ p
->sectors
[j
].size
> p
->size
) {
1881 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1882 " overflows the end of %s bank.",
1883 p
->sectors
[j
].offset
, p
->name
);
1884 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1887 target_addr_t start
;
1888 start
= p
->base
+ p
->sectors
[j
].offset
;
1889 xml_printf(&retval
, &xml
, &pos
, &size
,
1890 "<memory type=\"flash\" "
1891 "start=\"" TARGET_ADDR_FMT
"\" ",
1893 sector_size
= p
->sectors
[j
].size
;
1894 group_len
= sector_size
;
1896 group_len
+= sector_size
; /* equal to p->sectors[j].size */
1899 /* Does this finish a group of sectors?
1900 * If not, continue an already-started group.
1902 if (j
< p
->num_sectors
- 1
1903 && p
->sectors
[j
+ 1].size
== sector_size
1904 && p
->sectors
[j
+ 1].offset
== p
->sectors
[j
].offset
+ sector_size
1905 && p
->sectors
[j
+ 1].offset
+ p
->sectors
[j
+ 1].size
<= p
->size
)
1908 xml_printf(&retval
, &xml
, &pos
, &size
,
1909 "length=\"0x%x\">\n"
1910 "<property name=\"blocksize\">"
1918 ram_start
= p
->base
+ p
->size
;
1922 xml_printf(&retval
, &xml
, &pos
, &size
,
1923 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT
"\" "
1924 "length=\"0x%x\"/>\n",
1925 ram_start
, 0-ram_start
);
1926 /* ELSE a flash chip could be at the very end of the 32 bit address
1927 * space, in which case ram_start will be precisely 0
1932 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1934 if (retval
!= ERROR_OK
) {
1936 gdb_error(connection
, retval
);
1940 if (offset
+ length
> pos
)
1941 length
= pos
- offset
;
1943 char *t
= malloc(length
+ 1);
1945 memcpy(t
+ 1, xml
+ offset
, length
);
1946 gdb_put_packet(connection
, t
, length
+ 1);
1953 static const char *gdb_get_reg_type_name(enum reg_type type
)
1962 case REG_TYPE_INT16
:
1964 case REG_TYPE_INT32
:
1966 case REG_TYPE_INT64
:
1968 case REG_TYPE_INT128
:
1972 case REG_TYPE_UINT8
:
1974 case REG_TYPE_UINT16
:
1976 case REG_TYPE_UINT32
:
1978 case REG_TYPE_UINT64
:
1980 case REG_TYPE_UINT128
:
1982 case REG_TYPE_CODE_PTR
:
1984 case REG_TYPE_DATA_PTR
:
1986 case REG_TYPE_FLOAT
:
1988 case REG_TYPE_IEEE_SINGLE
:
1989 return "ieee_single";
1990 case REG_TYPE_IEEE_DOUBLE
:
1991 return "ieee_double";
1992 case REG_TYPE_ARCH_DEFINED
:
1993 return "int"; /* return arbitrary string to avoid compile warning. */
1996 return "int"; /* "int" as default value */
1999 static int lookup_add_arch_defined_types(char const **arch_defined_types_list
[], const char *type_id
,
2000 int *num_arch_defined_types
)
2002 int tbl_sz
= *num_arch_defined_types
;
2004 if (type_id
!= NULL
&& (strcmp(type_id
, ""))) {
2005 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2006 if (!((*arch_defined_types_list
)[j
])) {
2007 (*arch_defined_types_list
)[tbl_sz
++] = type_id
;
2008 *arch_defined_types_list
= realloc(*arch_defined_types_list
,
2009 sizeof(char *) * (tbl_sz
+ 1));
2010 (*arch_defined_types_list
)[tbl_sz
] = NULL
;
2011 *num_arch_defined_types
= tbl_sz
;
2014 if (!strcmp((*arch_defined_types_list
)[j
], type_id
))
2023 static int gdb_generate_reg_type_description(struct target
*target
,
2024 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
,
2025 char const **arch_defined_types_list
[], int * num_arch_defined_types
)
2027 int retval
= ERROR_OK
;
2029 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
2030 struct reg_data_type
*data_type
= type
->reg_type_vector
->type
;
2031 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2032 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2033 num_arch_defined_types
))
2034 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2035 arch_defined_types_list
,
2036 num_arch_defined_types
);
2038 /* <vector id="id" type="type" count="count"/> */
2039 xml_printf(&retval
, tdesc
, pos
, size
,
2040 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
2041 type
->id
, type
->reg_type_vector
->type
->id
,
2042 type
->reg_type_vector
->count
);
2044 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
2045 struct reg_data_type_union_field
*field
;
2046 field
= type
->reg_type_union
->fields
;
2047 while (field
!= NULL
) {
2048 struct reg_data_type
*data_type
= field
->type
;
2049 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2050 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2051 num_arch_defined_types
))
2052 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2053 arch_defined_types_list
,
2054 num_arch_defined_types
);
2057 field
= field
->next
;
2060 * <field name="name" type="type"/> ...
2062 xml_printf(&retval
, tdesc
, pos
, size
,
2063 "<union id=\"%s\">\n",
2066 field
= type
->reg_type_union
->fields
;
2067 while (field
!= NULL
) {
2068 xml_printf(&retval
, tdesc
, pos
, size
,
2069 "<field name=\"%s\" type=\"%s\"/>\n",
2070 field
->name
, field
->type
->id
);
2072 field
= field
->next
;
2075 xml_printf(&retval
, tdesc
, pos
, size
,
2078 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
2079 struct reg_data_type_struct_field
*field
;
2080 field
= type
->reg_type_struct
->fields
;
2082 if (field
->use_bitfields
) {
2083 /* <struct id="id" size="size">
2084 * <field name="name" start="start" end="end"/> ...
2086 xml_printf(&retval
, tdesc
, pos
, size
,
2087 "<struct id=\"%s\" size=\"%d\">\n",
2088 type
->id
, type
->reg_type_struct
->size
);
2089 while (field
!= NULL
) {
2090 xml_printf(&retval
, tdesc
, pos
, size
,
2091 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2092 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2093 gdb_get_reg_type_name(field
->bitfield
->type
));
2095 field
= field
->next
;
2098 while (field
!= NULL
) {
2099 struct reg_data_type
*data_type
= field
->type
;
2100 if (data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2101 if (lookup_add_arch_defined_types(arch_defined_types_list
, data_type
->id
,
2102 num_arch_defined_types
))
2103 gdb_generate_reg_type_description(target
, tdesc
, pos
, size
, data_type
,
2104 arch_defined_types_list
,
2105 num_arch_defined_types
);
2110 * <field name="name" type="type"/> ...
2112 xml_printf(&retval
, tdesc
, pos
, size
,
2113 "<struct id=\"%s\">\n",
2115 while (field
!= NULL
) {
2116 xml_printf(&retval
, tdesc
, pos
, size
,
2117 "<field name=\"%s\" type=\"%s\"/>\n",
2118 field
->name
, field
->type
->id
);
2120 field
= field
->next
;
2124 xml_printf(&retval
, tdesc
, pos
, size
,
2127 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2128 /* <flags id="id" size="size">
2129 * <field name="name" start="start" end="end"/> ...
2131 xml_printf(&retval
, tdesc
, pos
, size
,
2132 "<flags id=\"%s\" size=\"%d\">\n",
2133 type
->id
, type
->reg_type_flags
->size
);
2135 struct reg_data_type_flags_field
*field
;
2136 field
= type
->reg_type_flags
->fields
;
2137 while (field
!= NULL
) {
2138 xml_printf(&retval
, tdesc
, pos
, size
,
2139 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2140 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
,
2141 gdb_get_reg_type_name(field
->bitfield
->type
));
2143 field
= field
->next
;
2146 xml_printf(&retval
, tdesc
, pos
, size
,
2154 /* Get a list of available target registers features. feature_list must
2155 * be freed by caller.
2157 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2158 struct reg
**reg_list
, int reg_list_size
)
2162 /* Start with only one element */
2163 *feature_list
= calloc(1, sizeof(char *));
2165 for (int i
= 0; i
< reg_list_size
; i
++) {
2166 if (reg_list
[i
]->exist
== false)
2169 if (reg_list
[i
]->feature
!= NULL
2170 && reg_list
[i
]->feature
->name
!= NULL
2171 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2172 /* We found a feature, check if the feature is already in the
2173 * table. If not, allocate a new entry for the table and
2174 * put the new feature in it.
2176 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2177 if (!((*feature_list
)[j
])) {
2178 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2179 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2180 (*feature_list
)[tbl_sz
] = NULL
;
2183 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2190 if (feature_list_size
)
2191 *feature_list_size
= tbl_sz
;
2196 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2198 int retval
= ERROR_OK
;
2199 struct reg
**reg_list
= NULL
;
2201 char const *architecture
;
2202 char const **features
= NULL
;
2203 char const **arch_defined_types
= NULL
;
2204 int feature_list_size
= 0;
2205 int num_arch_defined_types
= 0;
2210 arch_defined_types
= calloc(1, sizeof(char *));
2212 retval
= target_get_gdb_reg_list(target
, ®_list
,
2213 ®_list_size
, REG_CLASS_ALL
);
2215 if (retval
!= ERROR_OK
) {
2216 LOG_ERROR("get register list failed");
2217 retval
= ERROR_FAIL
;
2221 if (reg_list_size
<= 0) {
2222 LOG_ERROR("get register list failed");
2223 retval
= ERROR_FAIL
;
2227 /* Get a list of available target registers features */
2228 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2229 if (retval
!= ERROR_OK
) {
2230 LOG_ERROR("Can't get the registers feature list");
2231 retval
= ERROR_FAIL
;
2235 /* If we found some features associated with registers, create sections */
2236 int current_feature
= 0;
2238 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2239 "<?xml version=\"1.0\"?>\n"
2240 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2241 "<target version=\"1.0\">\n");
2243 /* generate architecture element if supported by target */
2244 architecture
= target_get_gdb_arch(target
);
2245 if (architecture
!= NULL
)
2246 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2247 "<architecture>%s</architecture>\n", architecture
);
2249 /* generate target description according to register list */
2250 if (features
!= NULL
) {
2251 while (features
[current_feature
]) {
2253 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2254 "<feature name=\"%s\">\n",
2255 features
[current_feature
]);
2258 for (i
= 0; i
< reg_list_size
; i
++) {
2260 if (reg_list
[i
]->exist
== false)
2263 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2266 const char *type_str
;
2267 if (reg_list
[i
]->reg_data_type
!= NULL
) {
2268 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2269 /* generate <type... first, if there are architecture-defined types. */
2270 if (lookup_add_arch_defined_types(&arch_defined_types
,
2271 reg_list
[i
]->reg_data_type
->id
,
2272 &num_arch_defined_types
))
2273 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2274 reg_list
[i
]->reg_data_type
,
2275 &arch_defined_types
,
2276 &num_arch_defined_types
);
2278 type_str
= reg_list
[i
]->reg_data_type
->id
;
2280 /* predefined type */
2281 type_str
= gdb_get_reg_type_name(
2282 reg_list
[i
]->reg_data_type
->type
);
2285 /* Default type is "int" */
2289 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2290 "<reg name=\"%s\"", reg_list
[i
]->name
);
2291 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2292 " bitsize=\"%d\"", reg_list
[i
]->size
);
2293 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2294 " regnum=\"%d\"", reg_list
[i
]->number
);
2295 if (reg_list
[i
]->caller_save
)
2296 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2297 " save-restore=\"yes\"");
2299 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2300 " save-restore=\"no\"");
2302 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2303 " type=\"%s\"", type_str
);
2305 if (reg_list
[i
]->group
!= NULL
)
2306 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2307 " group=\"%s\"", reg_list
[i
]->group
);
2309 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2313 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2320 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2326 free(arch_defined_types
);
2328 if (retval
== ERROR_OK
)
2336 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2337 char **chunk
, int32_t offset
, uint32_t length
)
2339 if (target_desc
== NULL
) {
2340 LOG_ERROR("Unable to Generate Target Description");
2344 char *tdesc
= target_desc
->tdesc
;
2345 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2347 if (tdesc
== NULL
) {
2348 int retval
= gdb_generate_target_description(target
, &tdesc
);
2349 if (retval
!= ERROR_OK
) {
2350 LOG_ERROR("Unable to Generate Target Description");
2354 tdesc_length
= strlen(tdesc
);
2359 if (length
< (tdesc_length
- offset
))
2360 transfer_type
= 'm';
2362 transfer_type
= 'l';
2364 *chunk
= malloc(length
+ 2);
2365 if (*chunk
== NULL
) {
2366 LOG_ERROR("Unable to allocate memory");
2370 (*chunk
)[0] = transfer_type
;
2371 if (transfer_type
== 'm') {
2372 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2373 (*chunk
)[1 + length
] = '\0';
2375 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2376 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2378 /* After gdb-server sends out last chunk, invalidate tdesc. */
2384 target_desc
->tdesc
= tdesc
;
2385 target_desc
->tdesc_length
= tdesc_length
;
2390 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2392 int retval
= ERROR_OK
;
2393 struct reg
**reg_list
= NULL
;
2394 int reg_list_size
= 0;
2395 char const **features
= NULL
;
2396 int feature_list_size
= 0;
2398 char const *architecture
= target_get_gdb_arch(target
);
2400 retval
= target_get_gdb_reg_list(target
, ®_list
,
2401 ®_list_size
, REG_CLASS_ALL
);
2402 if (retval
!= ERROR_OK
) {
2403 LOG_ERROR("get register list failed");
2407 if (reg_list_size
<= 0) {
2408 LOG_ERROR("get register list failed");
2409 retval
= ERROR_FAIL
;
2413 /* Get a list of available target registers features */
2414 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2415 if (retval
!= ERROR_OK
) {
2416 LOG_ERROR("Can't get the registers feature list");
2421 if (architecture
|| feature_list_size
)
2435 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2437 struct rtos
*rtos
= target
->rtos
;
2438 int retval
= ERROR_OK
;
2439 char *thread_list
= NULL
;
2443 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2444 "<?xml version=\"1.0\"?>\n"
2448 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2449 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2451 if (!thread_detail
->exists
)
2454 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2455 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2457 if (thread_detail
->thread_name_str
!= NULL
)
2458 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2459 "Name: %s", thread_detail
->thread_name_str
);
2461 if (thread_detail
->extra_info_str
!= NULL
) {
2462 if (thread_detail
->thread_name_str
!= NULL
)
2463 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2465 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2466 thread_detail
->extra_info_str
);
2469 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2474 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2477 if (retval
== ERROR_OK
)
2478 *thread_list_out
= thread_list
;
2485 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2486 char **chunk
, int32_t offset
, uint32_t length
)
2488 if (*thread_list
== NULL
) {
2489 int retval
= gdb_generate_thread_list(target
, thread_list
);
2490 if (retval
!= ERROR_OK
) {
2491 LOG_ERROR("Unable to Generate Thread List");
2496 size_t thread_list_length
= strlen(*thread_list
);
2499 length
= MIN(length
, thread_list_length
- offset
);
2500 if (length
< (thread_list_length
- offset
))
2501 transfer_type
= 'm';
2503 transfer_type
= 'l';
2505 *chunk
= malloc(length
+ 2 + 3);
2506 /* Allocating extra 3 bytes prevents false positive valgrind report
2507 * of strlen(chunk) word access:
2508 * Invalid read of size 4
2509 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2510 if (*chunk
== NULL
) {
2511 LOG_ERROR("Unable to allocate memory");
2515 (*chunk
)[0] = transfer_type
;
2516 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2517 (*chunk
)[1 + length
] = '\0';
2519 /* After gdb-server sends out last chunk, invalidate thread list. */
2520 if (transfer_type
== 'l') {
2522 *thread_list
= NULL
;
2528 static int gdb_query_packet(struct connection
*connection
,
2529 char const *packet
, int packet_size
)
2531 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2532 struct gdb_connection
*gdb_connection
= connection
->priv
;
2533 struct target
*target
= get_target_from_connection(connection
);
2535 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2536 if (packet_size
> 6) {
2538 cmd
= malloc((packet_size
- 6) / 2 + 1);
2539 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2542 /* We want to print all debug output to GDB connection */
2543 log_add_callback(gdb_log_callback
, connection
);
2544 target_call_timer_callbacks_now();
2545 /* some commands need to know the GDB connection, make note of current
2546 * GDB connection. */
2547 current_gdb_connection
= gdb_connection
;
2548 command_run_line(cmd_ctx
, cmd
);
2549 current_gdb_connection
= NULL
;
2550 target_call_timer_callbacks_now();
2551 log_remove_callback(gdb_log_callback
, connection
);
2554 gdb_put_packet(connection
, "OK", 2);
2556 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2557 if (packet_size
> 5) {
2562 target_addr_t addr
= 0;
2565 /* skip command character */
2568 addr
= strtoull(packet
, &separator
, 16);
2570 if (*separator
!= ',') {
2571 LOG_ERROR("incomplete read memory packet received, dropping connection");
2572 return ERROR_SERVER_REMOTE_CLOSED
;
2575 len
= strtoul(separator
+ 1, NULL
, 16);
2577 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2579 if (retval
== ERROR_OK
) {
2580 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2581 gdb_put_packet(connection
, gdb_reply
, 9);
2583 retval
= gdb_error(connection
, retval
);
2584 if (retval
!= ERROR_OK
)
2590 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2591 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2592 * qXfer:features:read is supported for some targets */
2593 int retval
= ERROR_OK
;
2594 char *buffer
= NULL
;
2597 int gdb_target_desc_supported
= 0;
2599 /* we need to test that the target supports target descriptions */
2600 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2601 if (retval
!= ERROR_OK
) {
2602 LOG_INFO("Failed detecting Target Description Support, disabling");
2603 gdb_target_desc_supported
= 0;
2606 /* support may be disabled globally */
2607 if (gdb_use_target_description
== 0) {
2608 if (gdb_target_desc_supported
)
2609 LOG_WARNING("Target Descriptions Supported, but disabled");
2610 gdb_target_desc_supported
= 0;
2617 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2618 (GDB_BUFFER_SIZE
- 1),
2619 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2620 (gdb_target_desc_supported
== 1) ? '+' : '-');
2622 if (retval
!= ERROR_OK
) {
2623 gdb_send_error(connection
, 01);
2627 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2631 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2632 && (flash_get_bank_count() > 0))
2633 return gdb_memory_map(connection
, packet
, packet_size
);
2634 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2636 int retval
= ERROR_OK
;
2639 unsigned int length
;
2641 /* skip command character */
2644 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2645 gdb_send_error(connection
, 01);
2649 /* Target should prepare correct target description for annex.
2650 * The first character of returned xml is 'm' or 'l'. 'm' for
2651 * there are *more* chunks to transfer. 'l' for it is the *last*
2652 * chunk of target description.
2654 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2655 &xml
, offset
, length
);
2656 if (retval
!= ERROR_OK
) {
2657 gdb_error(connection
, retval
);
2661 gdb_put_packet(connection
, xml
, strlen(xml
));
2665 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2667 int retval
= ERROR_OK
;
2670 unsigned int length
;
2672 /* skip command character */
2675 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2676 gdb_send_error(connection
, 01);
2680 /* Target should prepare correct thread list for annex.
2681 * The first character of returned xml is 'm' or 'l'. 'm' for
2682 * there are *more* chunks to transfer. 'l' for it is the *last*
2683 * chunk of target description.
2685 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2686 &xml
, offset
, length
);
2687 if (retval
!= ERROR_OK
) {
2688 gdb_error(connection
, retval
);
2692 gdb_put_packet(connection
, xml
, strlen(xml
));
2696 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2697 gdb_connection
->noack_mode
= 1;
2698 gdb_put_packet(connection
, "OK", 2);
2702 gdb_put_packet(connection
, "", 0);
2706 static bool gdb_handle_vcont_packet(struct connection
*connection
, const char *packet
, int packet_size
)
2708 struct gdb_connection
*gdb_connection
= connection
->priv
;
2709 struct target
*target
= get_target_from_connection(connection
);
2710 const char *parse
= packet
;
2713 /* query for vCont supported */
2714 if (parse
[0] == '?') {
2715 if (target
->type
->step
!= NULL
) {
2716 /* gdb doesn't accept c without C and s without S */
2717 gdb_put_packet(connection
, "vCont;c;C;s;S", 13);
2723 if (parse
[0] == ';') {
2728 /* simple case, a continue packet */
2729 if (parse
[0] == 'c') {
2730 gdb_running_type
= 'c';
2731 LOG_DEBUG("target %s continue", target_name(target
));
2732 log_add_callback(gdb_log_callback
, connection
);
2733 retval
= target_resume(target
, 1, 0, 0, 0);
2734 if (retval
== ERROR_TARGET_NOT_HALTED
)
2735 LOG_INFO("target %s was not halted when resume was requested", target_name(target
));
2737 /* poll target in an attempt to make its internal state consistent */
2738 if (retval
!= ERROR_OK
) {
2739 retval
= target_poll(target
);
2740 if (retval
!= ERROR_OK
)
2741 LOG_DEBUG("error polling target %s after failed resume", target_name(target
));
2745 * We don't report errors to gdb here, move frontend_state to
2746 * TARGET_RUNNING to stay in sync with gdb's expectation of the
2749 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2750 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2755 /* single-step or step-over-breakpoint */
2756 if (parse
[0] == 's') {
2757 gdb_running_type
= 's';
2758 bool fake_step
= false;
2760 if (strncmp(parse
, "s:", 2) == 0) {
2761 struct target
*ct
= target
;
2769 thread_id
= strtoll(parse
, &endp
, 16);
2771 packet_size
-= endp
- parse
;
2775 if (target
->rtos
!= NULL
) {
2776 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
2777 rtos_update_threads(target
);
2779 target
->rtos
->gdb_target_for_threadid(connection
, thread_id
, &ct
);
2782 * check if the thread to be stepped is the current rtos thread
2783 * if not, we must fake the step
2785 if (target
->rtos
->current_thread
!= thread_id
)
2789 if (parse
[0] == ';') {
2793 if (parse
[0] == 'c') {
2797 /* check if thread-id follows */
2798 if (parse
[0] == ':') {
2803 tid
= strtoll(parse
, &endp
, 16);
2804 if (tid
== thread_id
) {
2806 * Special case: only step a single thread (core),
2807 * keep the other threads halted. Currently, only
2808 * aarch64 target understands it. Other target types don't
2809 * care (nobody checks the actual value of 'current')
2810 * and it doesn't really matter. This deserves
2811 * a symbolic constant and a formal interface documentation
2814 LOG_DEBUG("request to step current core only");
2815 /* uncomment after checking that indeed other targets are safe */
2822 LOG_DEBUG("target %s single-step thread %"PRIx64
, target_name(ct
), thread_id
);
2823 log_add_callback(gdb_log_callback
, connection
);
2824 target_call_event_callbacks(ct
, TARGET_EVENT_GDB_START
);
2827 * work around an annoying gdb behaviour: when the current thread
2828 * is changed in gdb, it assumes that the target can follow and also
2829 * make the thread current. This is an assumption that cannot hold
2830 * for a real target running a multi-threading OS. We just fake
2831 * the step to not trigger an internal error in gdb. See
2832 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
2836 char sig_reply
[128];
2838 LOG_DEBUG("fake step thread %"PRIx64
, thread_id
);
2840 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
),
2841 "T05thread:%016"PRIx64
";", thread_id
);
2843 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
2844 log_remove_callback(gdb_log_callback
, connection
);
2849 /* support for gdb_sync command */
2850 if (gdb_connection
->sync
) {
2851 gdb_connection
->sync
= false;
2852 if (ct
->state
== TARGET_HALTED
) {
2853 LOG_DEBUG("stepi ignored. GDB will now fetch the register state " \
2854 "from the target.");
2855 gdb_sig_halted(connection
);
2856 log_remove_callback(gdb_log_callback
, connection
);
2858 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2862 retval
= target_step(ct
, current_pc
, 0, 0);
2863 if (retval
== ERROR_TARGET_NOT_HALTED
)
2864 LOG_INFO("target %s was not halted when step was requested", target_name(ct
));
2866 /* if step was successful send a reply back to gdb */
2867 if (retval
== ERROR_OK
) {
2868 retval
= target_poll(ct
);
2869 if (retval
!= ERROR_OK
)
2870 LOG_DEBUG("error polling target %s after successful step", target_name(ct
));
2871 /* send back signal information */
2872 gdb_signal_reply(ct
, connection
);
2873 /* stop forwarding log packets! */
2874 log_remove_callback(gdb_log_callback
, connection
);
2876 gdb_connection
->frontend_state
= TARGET_RUNNING
;
2878 LOG_ERROR("Unknown vCont packet");
2887 static int gdb_v_packet(struct connection
*connection
,
2888 char const *packet
, int packet_size
)
2890 struct gdb_connection
*gdb_connection
= connection
->priv
;
2891 struct target
*target
;
2894 target
= get_target_from_connection(connection
);
2896 if (strncmp(packet
, "vCont", 5) == 0) {
2902 handled
= gdb_handle_vcont_packet(connection
, packet
, packet_size
);
2904 gdb_put_packet(connection
, "", 0);
2909 /* if flash programming disabled - send a empty reply */
2911 if (gdb_flash_program
== 0) {
2912 gdb_put_packet(connection
, "", 0);
2916 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
2918 unsigned long length
;
2920 char const *parse
= packet
+ 12;
2921 if (*parse
== '\0') {
2922 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2923 return ERROR_SERVER_REMOTE_CLOSED
;
2926 addr
= strtoul(parse
, (char **)&parse
, 16);
2928 if (*(parse
++) != ',' || *parse
== '\0') {
2929 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2930 return ERROR_SERVER_REMOTE_CLOSED
;
2933 length
= strtoul(parse
, (char **)&parse
, 16);
2935 if (*parse
!= '\0') {
2936 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2937 return ERROR_SERVER_REMOTE_CLOSED
;
2940 /* assume all sectors need erasing - stops any problems
2941 * when flash_write is called multiple times */
2944 /* perform any target specific operations before the erase */
2945 target_call_event_callbacks(target
,
2946 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2948 /* vFlashErase:addr,length messages require region start and
2949 * end to be "block" aligned ... if padding is ever needed,
2950 * GDB will have become dangerously confused.
2952 result
= flash_erase_address_range(target
, false, addr
,
2955 /* perform any target specific operations after the erase */
2956 target_call_event_callbacks(target
,
2957 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2960 if (result
!= ERROR_OK
) {
2961 /* GDB doesn't evaluate the actual error number returned,
2962 * treat a failed erase as an I/O error
2964 gdb_send_error(connection
, EIO
);
2965 LOG_ERROR("flash_erase returned %i", result
);
2967 gdb_put_packet(connection
, "OK", 2);
2972 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
2975 unsigned long length
;
2976 char const *parse
= packet
+ 12;
2978 if (*parse
== '\0') {
2979 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2980 return ERROR_SERVER_REMOTE_CLOSED
;
2982 addr
= strtoul(parse
, (char **)&parse
, 16);
2983 if (*(parse
++) != ':') {
2984 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2985 return ERROR_SERVER_REMOTE_CLOSED
;
2987 length
= packet_size
- (parse
- packet
);
2989 /* create a new image if there isn't already one */
2990 if (gdb_connection
->vflash_image
== NULL
) {
2991 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2992 image_open(gdb_connection
->vflash_image
, "", "build");
2995 /* create new section with content from packet buffer */
2996 retval
= image_add_section(gdb_connection
->vflash_image
,
2997 addr
, length
, 0x0, (uint8_t const *)parse
);
2998 if (retval
!= ERROR_OK
)
3001 gdb_put_packet(connection
, "OK", 2);
3006 if (strncmp(packet
, "vFlashDone", 10) == 0) {
3009 /* process the flashing buffer. No need to erase as GDB
3010 * always issues a vFlashErase first. */
3011 target_call_event_callbacks(target
,
3012 TARGET_EVENT_GDB_FLASH_WRITE_START
);
3013 result
= flash_write(target
, gdb_connection
->vflash_image
,
3015 target_call_event_callbacks(target
,
3016 TARGET_EVENT_GDB_FLASH_WRITE_END
);
3017 if (result
!= ERROR_OK
) {
3018 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
3019 gdb_put_packet(connection
, "E.memtype", 9);
3021 gdb_send_error(connection
, EIO
);
3023 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
3024 gdb_put_packet(connection
, "OK", 2);
3027 image_close(gdb_connection
->vflash_image
);
3028 free(gdb_connection
->vflash_image
);
3029 gdb_connection
->vflash_image
= NULL
;
3034 gdb_put_packet(connection
, "", 0);
3038 static int gdb_detach(struct connection
*connection
)
3041 * Only reply "OK" to GDB
3042 * it will close the connection and this will trigger a call to
3043 * gdb_connection_closed() that will in turn trigger the event
3044 * TARGET_EVENT_GDB_DETACH
3046 return gdb_put_packet(connection
, "OK", 2);
3049 /* The format of 'F' response packet is
3050 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3052 static int gdb_fileio_response_packet(struct connection
*connection
,
3053 char const *packet
, int packet_size
)
3055 struct target
*target
= get_target_from_connection(connection
);
3057 char *parsing_point
;
3058 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
3059 int fileio_errno
= 0;
3060 bool fileio_ctrl_c
= false;
3065 if (*separator
== ',') {
3066 parsing_point
= separator
+ 1;
3067 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
3068 if (*separator
== ',') {
3069 if (*(separator
+ 1) == 'C') {
3070 /* TODO: process ctrl-c */
3071 fileio_ctrl_c
= true;
3076 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3077 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
3079 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
3080 if (retval
!= ERROR_OK
)
3083 /* After File-I/O ends, keep continue or step */
3084 if (gdb_running_type
== 'c')
3085 retval
= target_resume(target
, 1, 0x0, 0, 0);
3086 else if (gdb_running_type
== 's')
3087 retval
= target_step(target
, 1, 0x0, 0);
3089 retval
= ERROR_FAIL
;
3091 if (retval
!= ERROR_OK
)
3097 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
3098 const char *function
, const char *string
)
3100 struct connection
*connection
= priv
;
3101 struct gdb_connection
*gdb_con
= connection
->priv
;
3103 if (gdb_con
->busy
) {
3104 /* do not reply this using the O packet */
3108 gdb_output_con(connection
, string
);
3111 static void gdb_sig_halted(struct connection
*connection
)
3114 snprintf(sig_reply
, 4, "T%2.2x", 2);
3115 gdb_put_packet(connection
, sig_reply
, 3);
3118 static int gdb_input_inner(struct connection
*connection
)
3120 /* Do not allocate this on the stack */
3121 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
3123 struct target
*target
;
3124 char const *packet
= gdb_packet_buffer
;
3127 struct gdb_connection
*gdb_con
= connection
->priv
;
3128 static int extended_protocol
;
3130 target
= get_target_from_connection(connection
);
3132 /* drain input buffer. If one of the packets fail, then an error
3133 * packet is replied, if applicable.
3135 * This loop will terminate and the error code is returned.
3137 * The calling fn will check if this error is something that
3138 * can be recovered from, or if the connection must be closed.
3140 * If the error is recoverable, this fn is called again to
3141 * drain the rest of the buffer.
3144 packet_size
= GDB_BUFFER_SIZE
-1;
3145 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
3146 if (retval
!= ERROR_OK
)
3149 /* terminate with zero */
3150 gdb_packet_buffer
[packet_size
] = '\0';
3152 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
3153 if (packet
[0] == 'X') {
3154 /* binary packets spew junk into the debug log stream */
3157 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
3160 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
3162 LOG_DEBUG("received packet: '%s'", packet
);
3165 if (packet_size
> 0) {
3167 switch (packet
[0]) {
3168 case 'T': /* Is thread alive? */
3169 gdb_thread_packet(connection
, packet
, packet_size
);
3171 case 'H': /* Set current thread ( 'c' for step and continue,
3172 * 'g' for all other operations ) */
3173 gdb_thread_packet(connection
, packet
, packet_size
);
3177 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
3178 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
3179 retval
= gdb_query_packet(connection
, packet
, packet_size
);
3182 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
3185 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
3188 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
3191 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
3194 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
3197 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
3201 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
3204 gdb_last_signal_packet(connection
, packet
, packet_size
);
3209 gdb_thread_packet(connection
, packet
, packet_size
);
3210 log_add_callback(gdb_log_callback
, connection
);
3212 if (gdb_con
->mem_write_error
) {
3213 LOG_ERROR("Memory write failure!");
3215 /* now that we have reported the memory write error,
3216 * we can clear the condition */
3217 gdb_con
->mem_write_error
= false;
3220 bool nostep
= false;
3221 bool already_running
= false;
3222 if (target
->state
== TARGET_RUNNING
) {
3223 LOG_WARNING("WARNING! The target is already running. "
3224 "All changes GDB did to registers will be discarded! "
3225 "Waiting for target to halt.");
3226 already_running
= true;
3227 } else if (target
->state
!= TARGET_HALTED
) {
3228 LOG_WARNING("The target is not in the halted nor running stated, " \
3229 "stepi/continue ignored.");
3231 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
3232 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3233 * sent by GDB first to OpenOCD, thus defeating the check to
3234 * make only the single stepping have the sync feature...
3237 LOG_DEBUG("stepi ignored. GDB will now fetch the register state " \
3238 "from the target.");
3240 gdb_con
->sync
= false;
3242 if (!already_running
&& nostep
) {
3243 /* Either the target isn't in the halted state, then we can't
3244 * step/continue. This might be early setup, etc.
3246 * Or we want to allow GDB to pick up a fresh set of
3247 * register values without modifying the target state.
3250 gdb_sig_halted(connection
);
3252 /* stop forwarding log packets! */
3253 log_remove_callback(gdb_log_callback
, connection
);
3255 /* We're running/stepping, in which case we can
3256 * forward log output until the target is halted
3258 gdb_con
->frontend_state
= TARGET_RUNNING
;
3259 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
3261 if (!already_running
) {
3262 /* Here we don't want packet processing to stop even if this fails,
3263 * so we use a local variable instead of retval. */
3264 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
3265 if (retval
!= ERROR_OK
) {
3266 /* we'll never receive a halted
3267 * condition... issue a false one..
3269 gdb_frontend_halted(target
, connection
);
3276 retval
= gdb_v_packet(connection
, packet
, packet_size
);
3279 retval
= gdb_detach(connection
);
3280 extended_protocol
= 0;
3283 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
3284 if (retval
!= ERROR_OK
)
3288 if (extended_protocol
!= 0) {
3289 gdb_con
->attached
= false;
3292 gdb_put_packet(connection
, "OK", 2);
3293 return ERROR_SERVER_REMOTE_CLOSED
;
3295 /* handle extended remote protocol */
3296 extended_protocol
= 1;
3297 gdb_put_packet(connection
, "OK", 2);
3300 /* handle extended restart packet */
3301 breakpoint_clear_target(target
);
3302 watchpoint_clear_target(target
);
3303 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
3304 target_name(target
));
3305 /* set connection as attached after reset */
3306 gdb_con
->attached
= true;
3307 /* info rtos parts */
3308 gdb_thread_packet(connection
, packet
, packet_size
);
3312 /* packet supported only by smp target i.e cortex_a.c*/
3313 /* handle smp packet replying coreid played to gbd */
3314 gdb_read_smp_packet(connection
, packet
, packet_size
);
3318 /* packet supported only by smp target i.e cortex_a.c */
3319 /* handle smp packet setting coreid to be played at next
3321 gdb_write_smp_packet(connection
, packet
, packet_size
);
3325 /* File-I/O extension */
3326 /* After gdb uses host-side syscall to complete target file
3327 * I/O, gdb sends host-side syscall return value to target
3329 * The format of 'F' response packet is
3330 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3332 gdb_con
->frontend_state
= TARGET_RUNNING
;
3333 log_add_callback(gdb_log_callback
, connection
);
3334 gdb_fileio_response_packet(connection
, packet
, packet_size
);
3338 /* ignore unknown packets */
3339 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
3340 gdb_put_packet(connection
, NULL
, 0);
3344 /* if a packet handler returned an error, exit input loop */
3345 if (retval
!= ERROR_OK
)
3349 if (gdb_con
->ctrl_c
) {
3350 if (target
->state
== TARGET_RUNNING
) {
3351 struct target
*t
= target
;
3353 target
->rtos
->gdb_target_for_threadid(connection
, target
->rtos
->current_threadid
, &t
);
3354 retval
= target_halt(t
);
3355 if (retval
== ERROR_OK
)
3356 retval
= target_poll(t
);
3357 if (retval
!= ERROR_OK
)
3358 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3359 gdb_con
->ctrl_c
= 0;
3361 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3362 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3366 } while (gdb_con
->buf_cnt
> 0);
3371 static int gdb_input(struct connection
*connection
)
3373 int retval
= gdb_input_inner(connection
);
3374 struct gdb_connection
*gdb_con
= connection
->priv
;
3375 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3378 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3379 if (gdb_con
->closed
)
3380 return ERROR_SERVER_REMOTE_CLOSED
;
3382 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3386 static int gdb_target_start(struct target
*target
, const char *port
)
3388 struct gdb_service
*gdb_service
;
3390 gdb_service
= malloc(sizeof(struct gdb_service
));
3392 if (NULL
== gdb_service
)
3395 LOG_DEBUG("starting gdb server for %s on %s", target_name(target
), port
);
3397 gdb_service
->target
= target
;
3398 gdb_service
->core
[0] = -1;
3399 gdb_service
->core
[1] = -1;
3400 target
->gdb_service
= gdb_service
;
3402 ret
= add_service("gdb",
3403 port
, 1, &gdb_new_connection
, &gdb_input
,
3404 &gdb_connection_closed
, gdb_service
);
3405 /* initialialize all targets gdb service with the same pointer */
3407 struct target_list
*head
;
3408 struct target
*curr
;
3409 head
= target
->head
;
3410 while (head
!= (struct target_list
*)NULL
) {
3411 curr
= head
->target
;
3413 curr
->gdb_service
= gdb_service
;
3420 static int gdb_target_add_one(struct target
*target
)
3422 /* one gdb instance per smp list */
3423 if ((target
->smp
) && (target
->gdb_service
))
3426 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3427 if (!target_supports_gdb_connection(target
)) {
3428 LOG_DEBUG("skip gdb server for target %s", target_name(target
));
3432 if (target
->gdb_port_override
) {
3433 if (strcmp(target
->gdb_port_override
, "disabled") == 0) {
3434 LOG_INFO("gdb port disabled");
3437 return gdb_target_start(target
, target
->gdb_port_override
);
3440 if (strcmp(gdb_port
, "disabled") == 0) {
3441 LOG_INFO("gdb port disabled");
3445 int retval
= gdb_target_start(target
, gdb_port_next
);
3446 if (retval
== ERROR_OK
) {
3447 /* save the port number so can be queried with
3448 * $target_name cget -gdb-port
3450 target
->gdb_port_override
= strdup(gdb_port_next
);
3453 /* If we can parse the port number
3454 * then we increment the port number for the next target.
3457 portnumber
= strtol(gdb_port_next
, &end
, 0);
3459 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3460 free(gdb_port_next
);
3462 gdb_port_next
= alloc_printf("%d", portnumber
+1);
3464 /* Don't increment if gdb_port is 0, since we're just
3465 * trying to allocate an unused port. */
3466 gdb_port_next
= strdup("0");
3474 int gdb_target_add_all(struct target
*target
)
3476 if (NULL
== target
) {
3477 LOG_WARNING("gdb services need one or more targets defined");
3481 while (NULL
!= target
) {
3482 int retval
= gdb_target_add_one(target
);
3483 if (ERROR_OK
!= retval
)
3486 target
= target
->next
;
3492 COMMAND_HANDLER(handle_gdb_sync_command
)
3495 return ERROR_COMMAND_SYNTAX_ERROR
;
3497 if (current_gdb_connection
== NULL
) {
3498 command_print(CMD_CTX
,
3499 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3503 current_gdb_connection
->sync
= true;
3508 /* daemon configuration command gdb_port */
3509 COMMAND_HANDLER(handle_gdb_port_command
)
3511 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3512 if (ERROR_OK
== retval
) {
3513 free(gdb_port_next
);
3514 gdb_port_next
= strdup(gdb_port
);
3519 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3522 return ERROR_COMMAND_SYNTAX_ERROR
;
3524 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3528 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3531 return ERROR_COMMAND_SYNTAX_ERROR
;
3533 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3537 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3540 return ERROR_COMMAND_SYNTAX_ERROR
;
3542 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3546 COMMAND_HANDLER(handle_gdb_report_register_access_error
)
3549 return ERROR_COMMAND_SYNTAX_ERROR
;
3551 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_register_access_error
);
3555 /* gdb_breakpoint_override */
3556 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3558 if (CMD_ARGC
== 0) {
3560 } else if (CMD_ARGC
== 1) {
3561 gdb_breakpoint_override
= 1;
3562 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3563 gdb_breakpoint_override_type
= BKPT_HARD
;
3564 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3565 gdb_breakpoint_override_type
= BKPT_SOFT
;
3566 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3567 gdb_breakpoint_override
= 0;
3569 return ERROR_COMMAND_SYNTAX_ERROR
;
3570 if (gdb_breakpoint_override
)
3571 LOG_USER("force %s breakpoints",
3572 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3574 LOG_USER("breakpoint type is not overridden");
3579 COMMAND_HANDLER(handle_gdb_target_description_command
)
3582 return ERROR_COMMAND_SYNTAX_ERROR
;
3584 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3588 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3591 uint32_t tdesc_length
;
3592 struct target
*target
= get_current_target(CMD_CTX
);
3594 int retval
= gdb_generate_target_description(target
, &tdesc
);
3595 if (retval
!= ERROR_OK
) {
3596 LOG_ERROR("Unable to Generate Target Description");
3600 tdesc_length
= strlen(tdesc
);
3602 struct fileio
*fileio
;
3603 size_t size_written
;
3605 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3606 if (tdesc_filename
== NULL
) {
3607 retval
= ERROR_FAIL
;
3611 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
3613 if (retval
!= ERROR_OK
) {
3614 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
3618 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
3620 fileio_close(fileio
);
3622 if (retval
!= ERROR_OK
)
3623 LOG_ERROR("Error while writing the tdesc file");
3626 free(tdesc_filename
);
3632 static const struct command_registration gdb_command_handlers
[] = {
3635 .handler
= handle_gdb_sync_command
,
3636 .mode
= COMMAND_ANY
,
3637 .help
= "next stepi will return immediately allowing "
3638 "GDB to fetch register state without affecting "
3644 .handler
= handle_gdb_port_command
,
3645 .mode
= COMMAND_ANY
,
3646 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
3647 "server listens for the next port number after the "
3648 "base port number specified. "
3649 "No arguments reports GDB port. \"pipe\" means listen to stdin "
3650 "output to stdout, an integer is base port number, \"disabled\" disables "
3651 "port. Any other string is are interpreted as named pipe to listen to. "
3652 "Output pipe is the same name as input pipe, but with 'o' appended.",
3653 .usage
= "[port_num]",
3656 .name
= "gdb_memory_map",
3657 .handler
= handle_gdb_memory_map_command
,
3658 .mode
= COMMAND_CONFIG
,
3659 .help
= "enable or disable memory map",
3660 .usage
= "('enable'|'disable')"
3663 .name
= "gdb_flash_program",
3664 .handler
= handle_gdb_flash_program_command
,
3665 .mode
= COMMAND_CONFIG
,
3666 .help
= "enable or disable flash program",
3667 .usage
= "('enable'|'disable')"
3670 .name
= "gdb_report_data_abort",
3671 .handler
= handle_gdb_report_data_abort_command
,
3672 .mode
= COMMAND_CONFIG
,
3673 .help
= "enable or disable reporting data aborts",
3674 .usage
= "('enable'|'disable')"
3677 .name
= "gdb_report_register_access_error",
3678 .handler
= handle_gdb_report_register_access_error
,
3679 .mode
= COMMAND_CONFIG
,
3680 .help
= "enable or disable reporting register access errors",
3681 .usage
= "('enable'|'disable')"
3684 .name
= "gdb_breakpoint_override",
3685 .handler
= handle_gdb_breakpoint_override_command
,
3686 .mode
= COMMAND_ANY
,
3687 .help
= "Display or specify type of breakpoint "
3688 "to be used by gdb 'break' commands.",
3689 .usage
= "('hard'|'soft'|'disable')"
3692 .name
= "gdb_target_description",
3693 .handler
= handle_gdb_target_description_command
,
3694 .mode
= COMMAND_CONFIG
,
3695 .help
= "enable or disable target description",
3696 .usage
= "('enable'|'disable')"
3699 .name
= "gdb_save_tdesc",
3700 .handler
= handle_gdb_save_tdesc_command
,
3701 .mode
= COMMAND_EXEC
,
3702 .help
= "Save the target description file",
3704 COMMAND_REGISTRATION_DONE
3707 int gdb_register_commands(struct command_context
*cmd_ctx
)
3709 gdb_port
= strdup("3333");
3710 gdb_port_next
= strdup("3333");
3711 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);
3714 void gdb_service_free(void)
3717 free(gdb_port_next
);