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, write to the *
35 * Free Software Foundation, Inc., *
36 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
37 ***************************************************************************/
43 #include <target/breakpoints.h>
44 #include <target/target_request.h>
45 #include <target/register.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
;
98 #define _DEBUG_GDB_IO_
101 static struct gdb_connection
*current_gdb_connection
;
103 static int gdb_breakpoint_override
;
104 static enum breakpoint_type gdb_breakpoint_override_type
;
106 static int gdb_error(struct connection
*connection
, int retval
);
107 static char *gdb_port
;
108 static char *gdb_port_next
;
110 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
111 const char *function
, const char *string
);
113 /* number of gdb connections, mainly to suppress gdb related debugging spam
114 * in helper/log.c when no gdb connections are actually active */
115 int gdb_actual_connections
;
117 /* set if we are sending a memory map to gdb
118 * via qXfer:memory-map:read packet */
119 /* enabled by default*/
120 static int gdb_use_memory_map
= 1;
121 /* enabled by default*/
122 static int gdb_flash_program
= 1;
124 /* if set, data aborts cause an error to be reported in memory read packets
125 * see the code in gdb_read_memory_packet() for further explanations.
126 * Disabled by default.
128 static int gdb_report_data_abort
;
130 /* set if we are sending target descriptions to gdb
131 * via qXfer:features:read packet */
132 /* enabled by default */
133 static int gdb_use_target_description
= 1;
135 /* current processing free-run type, used by file-I/O */
136 static char gdb_running_type
;
138 static int gdb_last_signal(struct target
*target
)
140 switch (target
->debug_reason
) {
141 case DBG_REASON_DBGRQ
:
142 return 0x2; /* SIGINT */
143 case DBG_REASON_BREAKPOINT
:
144 case DBG_REASON_WATCHPOINT
:
145 case DBG_REASON_WPTANDBKPT
:
146 return 0x05; /* SIGTRAP */
147 case DBG_REASON_SINGLESTEP
:
148 return 0x05; /* SIGTRAP */
149 case DBG_REASON_NOTHALTED
:
150 return 0x0; /* no signal... shouldn't happen */
152 LOG_USER("undefined debug reason %d - target needs reset",
153 target
->debug_reason
);
158 static int check_pending(struct connection
*connection
,
159 int timeout_s
, int *got_data
)
161 /* a non-blocking socket will block if there is 0 bytes available on the socket,
162 * but return with as many bytes as are available immediately
166 struct gdb_connection
*gdb_con
= connection
->priv
;
168 if (got_data
== NULL
)
172 if (gdb_con
->buf_cnt
> 0) {
178 FD_SET(connection
->fd
, &read_fds
);
180 tv
.tv_sec
= timeout_s
;
182 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
183 /* This can typically be because a "monitor" command took too long
184 * before printing any progress messages
187 return ERROR_GDB_TIMEOUT
;
191 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
195 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
197 struct gdb_connection
*gdb_con
= connection
->priv
;
198 int retval
= ERROR_OK
;
200 #ifdef _DEBUG_GDB_IO_
204 if (connection
->service
->type
!= CONNECTION_TCP
)
205 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
207 retval
= check_pending(connection
, 1, NULL
);
208 if (retval
!= ERROR_OK
)
210 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
215 if (gdb_con
->buf_cnt
> 0)
217 if (gdb_con
->buf_cnt
== 0) {
219 return ERROR_SERVER_REMOTE_CLOSED
;
223 errno
= WSAGetLastError();
229 case WSAECONNABORTED
:
231 return ERROR_SERVER_REMOTE_CLOSED
;
234 return ERROR_SERVER_REMOTE_CLOSED
;
236 LOG_ERROR("read: %d", errno
);
246 return ERROR_SERVER_REMOTE_CLOSED
;
249 return ERROR_SERVER_REMOTE_CLOSED
;
251 LOG_ERROR("read: %s", strerror(errno
));
253 return ERROR_SERVER_REMOTE_CLOSED
;
258 #ifdef _DEBUG_GDB_IO_
259 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
260 LOG_DEBUG("received '%s'", debug_buffer
);
264 gdb_con
->buf_p
= gdb_con
->buffer
;
266 *next_char
= *(gdb_con
->buf_p
++);
267 if (gdb_con
->buf_cnt
> 0)
268 connection
->input_pending
= 1;
270 connection
->input_pending
= 0;
271 #ifdef _DEBUG_GDB_IO_
272 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
279 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
280 * held in registers in the inner loop.
282 * For small caches and embedded systems this is important!
284 static inline int gdb_get_char_fast(struct connection
*connection
,
285 int *next_char
, char **buf_p
, int *buf_cnt
)
287 int retval
= ERROR_OK
;
289 if ((*buf_cnt
)-- > 0) {
290 *next_char
= **buf_p
;
293 connection
->input_pending
= 1;
295 connection
->input_pending
= 0;
297 #ifdef _DEBUG_GDB_IO_
298 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
304 struct gdb_connection
*gdb_con
= connection
->priv
;
305 gdb_con
->buf_p
= *buf_p
;
306 gdb_con
->buf_cnt
= *buf_cnt
;
307 retval
= gdb_get_char_inner(connection
, next_char
);
308 *buf_p
= gdb_con
->buf_p
;
309 *buf_cnt
= gdb_con
->buf_cnt
;
314 static int gdb_get_char(struct connection
*connection
, int *next_char
)
316 struct gdb_connection
*gdb_con
= connection
->priv
;
317 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
320 static int gdb_putback_char(struct connection
*connection
, int last_char
)
322 struct gdb_connection
*gdb_con
= connection
->priv
;
324 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
325 *(--gdb_con
->buf_p
) = last_char
;
328 LOG_ERROR("BUG: couldn't put character back");
333 /* The only way we can detect that the socket is closed is the first time
334 * we write to it, we will fail. Subsequent write operations will
335 * succeed. Shudder! */
336 static int gdb_write(struct connection
*connection
, void *data
, int len
)
338 struct gdb_connection
*gdb_con
= connection
->priv
;
340 return ERROR_SERVER_REMOTE_CLOSED
;
342 if (connection_write(connection
, data
, len
) == len
)
345 return ERROR_SERVER_REMOTE_CLOSED
;
348 static int gdb_put_packet_inner(struct connection
*connection
,
349 char *buffer
, int len
)
352 unsigned char my_checksum
= 0;
353 #ifdef _DEBUG_GDB_IO_
358 struct gdb_connection
*gdb_con
= connection
->priv
;
360 for (i
= 0; i
< len
; i
++)
361 my_checksum
+= buffer
[i
];
363 #ifdef _DEBUG_GDB_IO_
365 * At this point we should have nothing in the input queue from GDB,
366 * however sometimes '-' is sent even though we've already received
367 * an ACK (+) for everything we've sent off.
371 retval
= check_pending(connection
, 0, &gotdata
);
372 if (retval
!= ERROR_OK
)
376 retval
= gdb_get_char(connection
, &reply
);
377 if (retval
!= ERROR_OK
)
380 /* fix a problem with some IAR tools */
381 gdb_putback_char(connection
, reply
);
382 LOG_DEBUG("Unexpected start of new packet");
386 LOG_WARNING("Discard unexpected char %c", reply
);
391 #ifdef _DEBUG_GDB_IO_
392 debug_buffer
= strndup(buffer
, len
);
393 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
397 char local_buffer
[1024];
398 local_buffer
[0] = '$';
399 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
400 /* performance gain on smaller packets by only a single call to gdb_write() */
401 memcpy(local_buffer
+ 1, buffer
, len
++);
402 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
403 retval
= gdb_write(connection
, local_buffer
, len
);
404 if (retval
!= ERROR_OK
)
407 /* larger packets are transmitted directly from caller supplied buffer
408 * by several calls to gdb_write() to avoid dynamic allocation */
409 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
410 retval
= gdb_write(connection
, local_buffer
, 1);
411 if (retval
!= ERROR_OK
)
413 retval
= gdb_write(connection
, buffer
, len
);
414 if (retval
!= ERROR_OK
)
416 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
417 if (retval
!= ERROR_OK
)
421 if (gdb_con
->noack_mode
)
424 retval
= gdb_get_char(connection
, &reply
);
425 if (retval
!= ERROR_OK
)
430 else if (reply
== '-') {
431 /* Stop sending output packets for now */
432 log_remove_callback(gdb_log_callback
, connection
);
433 LOG_WARNING("negative reply, retrying");
434 } else if (reply
== 0x3) {
436 retval
= gdb_get_char(connection
, &reply
);
437 if (retval
!= ERROR_OK
)
441 else if (reply
== '-') {
442 /* Stop sending output packets for now */
443 log_remove_callback(gdb_log_callback
, connection
);
444 LOG_WARNING("negative reply, retrying");
445 } else if (reply
== '$') {
446 LOG_ERROR("GDB missing ack(1) - assumed good");
447 gdb_putback_char(connection
, reply
);
450 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
452 return ERROR_SERVER_REMOTE_CLOSED
;
454 } else if (reply
== '$') {
455 LOG_ERROR("GDB missing ack(2) - assumed good");
456 gdb_putback_char(connection
, reply
);
459 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
462 return ERROR_SERVER_REMOTE_CLOSED
;
466 return ERROR_SERVER_REMOTE_CLOSED
;
471 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
473 struct gdb_connection
*gdb_con
= connection
->priv
;
475 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
478 /* we sent some data, reset timer for keep alive messages */
484 static inline int fetch_packet(struct connection
*connection
,
485 int *checksum_ok
, int noack
, int *len
, char *buffer
)
487 unsigned char my_checksum
= 0;
490 int retval
= ERROR_OK
;
492 struct gdb_connection
*gdb_con
= connection
->priv
;
497 /* move this over into local variables to use registers and give the
498 * more freedom to optimize */
499 char *buf_p
= gdb_con
->buf_p
;
500 int buf_cnt
= gdb_con
->buf_cnt
;
503 /* The common case is that we have an entire packet with no escape chars.
504 * We need to leave at least 2 bytes in the buffer to have
505 * gdb_get_char() update various bits and bobs correctly.
507 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
508 /* The compiler will struggle a bit with constant propagation and
509 * aliasing, so we help it by showing that these values do not
510 * change inside the loop
514 int run
= buf_cnt
- 2;
520 if (character
== '#') {
521 /* Danger! character can be '#' when esc is
522 * used so we need an explicit boolean for done here. */
527 if (character
== '}') {
528 /* data transmitted in binary mode (X packet)
529 * uses 0x7d as escape character */
530 my_checksum
+= character
& 0xff;
533 my_checksum
+= character
& 0xff;
534 buffer
[count
++] = (character
^ 0x20) & 0xff;
536 my_checksum
+= character
& 0xff;
537 buffer
[count
++] = character
& 0xff;
546 LOG_ERROR("packet buffer too small");
547 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
551 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
552 if (retval
!= ERROR_OK
)
555 if (character
== '#')
558 if (character
== '}') {
559 /* data transmitted in binary mode (X packet)
560 * uses 0x7d as escape character */
561 my_checksum
+= character
& 0xff;
563 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
564 if (retval
!= ERROR_OK
)
567 my_checksum
+= character
& 0xff;
568 buffer
[count
++] = (character
^ 0x20) & 0xff;
570 my_checksum
+= character
& 0xff;
571 buffer
[count
++] = character
& 0xff;
575 gdb_con
->buf_p
= buf_p
;
576 gdb_con
->buf_cnt
= buf_cnt
;
578 if (retval
!= ERROR_OK
)
583 retval
= gdb_get_char(connection
, &character
);
584 if (retval
!= ERROR_OK
)
586 checksum
[0] = character
;
587 retval
= gdb_get_char(connection
, &character
);
588 if (retval
!= ERROR_OK
)
590 checksum
[1] = character
;
594 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
599 static int gdb_get_packet_inner(struct connection
*connection
,
600 char *buffer
, int *len
)
604 struct gdb_connection
*gdb_con
= connection
->priv
;
608 retval
= gdb_get_char(connection
, &character
);
609 if (retval
!= ERROR_OK
)
612 #ifdef _DEBUG_GDB_IO_
613 LOG_DEBUG("character: '%c'", character
);
620 /* According to the GDB documentation
621 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
622 * "gdb sends a final `+` acknowledgment of the stub's `OK`
623 * response, which can be safely ignored by the stub."
624 * However OpenOCD server already is in noack mode at this
625 * point and instead of ignoring this it was emitting a
626 * warning. This code makes server ignore the first ACK
627 * that will be received after going into noack mode,
628 * warning only about subsequent ACK's. */
629 if (gdb_con
->noack_mode
> 1) {
630 LOG_WARNING("acknowledgment received, but no packet pending");
632 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
633 gdb_con
->noack_mode
= 2;
637 LOG_WARNING("negative acknowledgment, but no packet pending");
644 LOG_WARNING("ignoring character 0x%x", character
);
647 } while (character
!= '$');
650 /* explicit code expansion here to get faster inlined code in -O3 by not
651 * calculating checksum */
652 if (gdb_con
->noack_mode
) {
653 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
654 if (retval
!= ERROR_OK
)
657 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
658 if (retval
!= ERROR_OK
)
662 if (gdb_con
->noack_mode
) {
663 /* checksum is not checked in noack mode */
667 retval
= gdb_write(connection
, "+", 1);
668 if (retval
!= ERROR_OK
)
674 return ERROR_SERVER_REMOTE_CLOSED
;
679 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
681 struct gdb_connection
*gdb_con
= connection
->priv
;
683 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
688 static int gdb_output_con(struct connection
*connection
, const char *line
)
693 bin_size
= strlen(line
);
695 hex_buffer
= malloc(bin_size
* 2 + 2);
696 if (hex_buffer
== NULL
)
697 return ERROR_GDB_BUFFER_TOO_SMALL
;
700 int pkt_len
= hexify(hex_buffer
+ 1, line
, bin_size
, bin_size
* 2 + 1);
701 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
707 static int gdb_output(struct command_context
*context
, const char *line
)
709 /* this will be dumped to the log and also sent as an O packet if possible */
710 LOG_USER_N("%s", line
);
714 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
716 struct gdb_connection
*gdb_connection
= connection
->priv
;
718 char stop_reason
[20];
722 if (target
->debug_reason
== DBG_REASON_EXIT
) {
723 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
725 if (gdb_connection
->ctrl_c
) {
727 gdb_connection
->ctrl_c
= 0;
729 signal_var
= gdb_last_signal(target
);
731 stop_reason
[0] = '\0';
732 if (target
->debug_reason
== DBG_REASON_WATCHPOINT
) {
733 enum watchpoint_rw hit_wp_type
;
734 uint32_t hit_wp_address
;
736 if (watchpoint_hit(target
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
738 switch (hit_wp_type
) {
740 snprintf(stop_reason
, sizeof(stop_reason
),
741 "watch:%08" PRIx32
";", hit_wp_address
);
744 snprintf(stop_reason
, sizeof(stop_reason
),
745 "rwatch:%08" PRIx32
";", hit_wp_address
);
748 snprintf(stop_reason
, sizeof(stop_reason
),
749 "awatch:%08" PRIx32
";", hit_wp_address
);
757 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s",
758 signal_var
, stop_reason
);
761 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
762 gdb_connection
->frontend_state
= TARGET_HALTED
;
763 rtos_update_threads(target
);
766 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
768 struct gdb_connection
*gdb_connection
= connection
->priv
;
769 char fileio_command
[256];
771 bool program_exited
= false;
773 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
774 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
",%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
775 target
->fileio_info
->param_1
,
776 target
->fileio_info
->param_2
,
777 target
->fileio_info
->param_3
,
778 target
->fileio_info
->param_4
);
779 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
780 sprintf(fileio_command
, "F%s,%" PRIx32
, target
->fileio_info
->identifier
,
781 target
->fileio_info
->param_1
);
782 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
783 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
784 target
->fileio_info
->param_1
,
785 target
->fileio_info
->param_2
,
786 target
->fileio_info
->param_3
);
787 else if (strcmp(target
->fileio_info
->identifier
, "write") == 0)
788 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
789 target
->fileio_info
->param_1
,
790 target
->fileio_info
->param_2
,
791 target
->fileio_info
->param_3
);
792 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
793 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
794 target
->fileio_info
->param_1
,
795 target
->fileio_info
->param_2
,
796 target
->fileio_info
->param_3
);
797 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
798 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
",%" PRIx32
"/%" PRIx32
, 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
, "unlink") == 0)
804 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
, target
->fileio_info
->identifier
,
805 target
->fileio_info
->param_1
,
806 target
->fileio_info
->param_2
);
807 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
808 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
809 target
->fileio_info
->param_1
,
810 target
->fileio_info
->param_2
,
811 target
->fileio_info
->param_3
);
812 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
813 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
814 target
->fileio_info
->param_1
,
815 target
->fileio_info
->param_2
);
816 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
817 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
818 target
->fileio_info
->param_1
,
819 target
->fileio_info
->param_2
);
820 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
821 sprintf(fileio_command
, "F%s,%" PRIx32
, target
->fileio_info
->identifier
,
822 target
->fileio_info
->param_1
);
823 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
824 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
, target
->fileio_info
->identifier
,
825 target
->fileio_info
->param_1
,
826 target
->fileio_info
->param_2
);
827 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
828 /* If target hits exit syscall, report to GDB the program is terminated.
829 * In addition, let target run its own exit syscall handler. */
830 program_exited
= true;
831 sprintf(fileio_command
, "W%02" PRIx32
, target
->fileio_info
->param_1
);
833 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
835 /* encounter unknown syscall, continue */
836 gdb_connection
->frontend_state
= TARGET_RUNNING
;
837 target_resume(target
, 1, 0x0, 0, 0);
841 command_len
= strlen(fileio_command
);
842 gdb_put_packet(connection
, fileio_command
, command_len
);
844 if (program_exited
) {
845 /* Use target_resume() to let target run its own exit syscall handler. */
846 gdb_connection
->frontend_state
= TARGET_RUNNING
;
847 target_resume(target
, 1, 0x0, 0, 0);
849 gdb_connection
->frontend_state
= TARGET_HALTED
;
850 rtos_update_threads(target
);
854 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
856 struct gdb_connection
*gdb_connection
= connection
->priv
;
858 /* In the GDB protocol when we are stepping or continuing execution,
859 * we have a lingering reply. Upon receiving a halted event
860 * when we have that lingering packet, we reply to the original
861 * step or continue packet.
863 * Executing monitor commands can bring the target in and
864 * out of the running state so we'll see lots of TARGET_EVENT_XXX
865 * that are to be ignored.
867 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
868 /* stop forwarding log packets! */
869 log_remove_callback(gdb_log_callback
, connection
);
871 /* check fileio first */
872 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
873 gdb_fileio_reply(target
, connection
);
875 gdb_signal_reply(target
, connection
);
879 static int gdb_target_callback_event_handler(struct target
*target
,
880 enum target_event event
, void *priv
)
883 struct connection
*connection
= priv
;
884 struct gdb_service
*gdb_service
= connection
->service
->priv
;
886 if (gdb_service
->target
!= target
)
890 case TARGET_EVENT_GDB_HALT
:
891 gdb_frontend_halted(target
, connection
);
893 case TARGET_EVENT_HALTED
:
894 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
896 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
897 retval
= jtag_execute_queue();
898 if (retval
!= ERROR_OK
)
908 static int gdb_new_connection(struct connection
*connection
)
910 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
911 struct gdb_service
*gdb_service
= connection
->service
->priv
;
915 connection
->priv
= gdb_connection
;
917 /* initialize gdb connection information */
918 gdb_connection
->buf_p
= gdb_connection
->buffer
;
919 gdb_connection
->buf_cnt
= 0;
920 gdb_connection
->ctrl_c
= 0;
921 gdb_connection
->frontend_state
= TARGET_HALTED
;
922 gdb_connection
->vflash_image
= NULL
;
923 gdb_connection
->closed
= 0;
924 gdb_connection
->busy
= 0;
925 gdb_connection
->noack_mode
= 0;
926 gdb_connection
->sync
= false;
927 gdb_connection
->mem_write_error
= false;
928 gdb_connection
->attached
= true;
929 gdb_connection
->target_desc
.tdesc
= NULL
;
930 gdb_connection
->target_desc
.tdesc_length
= 0;
932 /* send ACK to GDB for debug request */
933 gdb_write(connection
, "+", 1);
935 /* output goes through gdb connection */
936 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
938 /* we must remove all breakpoints registered to the target as a previous
939 * GDB session could leave dangling breakpoints if e.g. communication
942 breakpoint_clear_target(gdb_service
->target
);
943 watchpoint_clear_target(gdb_service
->target
);
945 /* clean previous rtos session if supported*/
946 if ((gdb_service
->target
->rtos
) && (gdb_service
->target
->rtos
->type
->clean
))
947 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
949 /* remove the initial ACK from the incoming buffer */
950 retval
= gdb_get_char(connection
, &initial_ack
);
951 if (retval
!= ERROR_OK
)
954 /* FIX!!!??? would we actually ever receive a + here???
957 if (initial_ack
!= '+')
958 gdb_putback_char(connection
, initial_ack
);
959 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
961 if (gdb_use_memory_map
) {
962 /* Connect must fail if the memory map can't be set up correctly.
964 * This will cause an auto_probe to be invoked, which is either
965 * a no-op or it will fail when the target isn't ready(e.g. not halted).
968 for (i
= 0; i
< flash_get_bank_count(); i
++) {
969 struct flash_bank
*p
;
970 p
= get_flash_bank_by_num_noprobe(i
);
971 if (p
->target
!= gdb_service
->target
)
973 retval
= get_flash_bank_by_num(i
, &p
);
974 if (retval
!= ERROR_OK
) {
975 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
976 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
982 gdb_actual_connections
++;
983 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
984 gdb_actual_connections
,
985 target_name(gdb_service
->target
),
986 target_state_name(gdb_service
->target
));
988 /* DANGER! If we fail subsequently, we must remove this handler,
989 * otherwise we occasionally see crashes as the timer can invoke the
992 * register callback to be informed about target events */
993 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
998 static int gdb_connection_closed(struct connection
*connection
)
1000 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1001 struct gdb_connection
*gdb_connection
= connection
->priv
;
1003 /* we're done forwarding messages. Tear down callback before
1004 * cleaning up connection.
1006 log_remove_callback(gdb_log_callback
, connection
);
1008 gdb_actual_connections
--;
1009 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1010 target_name(gdb_service
->target
),
1011 target_state_name(gdb_service
->target
),
1012 gdb_actual_connections
);
1014 /* see if an image built with vFlash commands is left */
1015 if (gdb_connection
->vflash_image
) {
1016 image_close(gdb_connection
->vflash_image
);
1017 free(gdb_connection
->vflash_image
);
1018 gdb_connection
->vflash_image
= NULL
;
1021 /* if this connection registered a debug-message receiver delete it */
1022 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
1024 if (connection
->priv
) {
1025 free(connection
->priv
);
1026 connection
->priv
= NULL
;
1028 LOG_ERROR("BUG: connection->priv == NULL");
1030 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1032 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
1034 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
1039 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1042 snprintf(err
, 4, "E%2.2X", the_error
);
1043 gdb_put_packet(connection
, err
, 3);
1046 static int gdb_last_signal_packet(struct connection
*connection
,
1047 char *packet
, int packet_size
)
1049 struct target
*target
= get_target_from_connection(connection
);
1050 struct gdb_connection
*gdb_con
= connection
->priv
;
1054 if (!gdb_con
->attached
) {
1055 /* if we are here we have received a kill packet
1056 * reply W stop reply otherwise gdb gets very unhappy */
1057 gdb_put_packet(connection
, "W00", 3);
1061 signal_var
= gdb_last_signal(target
);
1063 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1064 gdb_put_packet(connection
, sig_reply
, 3);
1069 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1071 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1074 return len
- 1 - pos
;
1077 /* Convert register to string of bytes. NB! The # of bits in the
1078 * register might be non-divisible by 8(a byte), in which
1079 * case an entire byte is shown.
1081 * NB! the format on the wire is the target endianness
1083 * The format of reg->value is little endian
1086 static void gdb_str_to_target(struct target
*target
,
1087 char *tstr
, struct reg
*reg
)
1094 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1096 for (i
= 0; i
< buf_len
; i
++) {
1097 int j
= gdb_reg_pos(target
, i
, buf_len
);
1098 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1102 /* copy over in register buffer */
1103 static void gdb_target_to_reg(struct target
*target
,
1104 char *tstr
, int str_len
, uint8_t *bin
)
1107 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1112 for (i
= 0; i
< str_len
; i
+= 2) {
1114 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1115 LOG_ERROR("BUG: unable to convert register value");
1119 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1124 static int gdb_get_registers_packet(struct connection
*connection
,
1125 char *packet
, int packet_size
)
1127 struct target
*target
= get_target_from_connection(connection
);
1128 struct reg
**reg_list
;
1131 int reg_packet_size
= 0;
1136 #ifdef _DEBUG_GDB_IO_
1140 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1143 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1145 if (retval
!= ERROR_OK
)
1146 return gdb_error(connection
, retval
);
1148 for (i
= 0; i
< reg_list_size
; i
++)
1149 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1151 assert(reg_packet_size
> 0);
1153 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1154 reg_packet_p
= reg_packet
;
1156 for (i
= 0; i
< reg_list_size
; i
++) {
1157 if (!reg_list
[i
]->valid
)
1158 reg_list
[i
]->type
->get(reg_list
[i
]);
1159 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1160 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1163 #ifdef _DEBUG_GDB_IO_
1165 char *reg_packet_p_debug
;
1166 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1167 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1168 free(reg_packet_p_debug
);
1172 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1180 static int gdb_set_registers_packet(struct connection
*connection
,
1181 char *packet
, int packet_size
)
1183 struct target
*target
= get_target_from_connection(connection
);
1185 struct reg
**reg_list
;
1190 #ifdef _DEBUG_GDB_IO_
1194 /* skip command character */
1198 if (packet_size
% 2) {
1199 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1200 return ERROR_SERVER_REMOTE_CLOSED
;
1203 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1205 if (retval
!= ERROR_OK
)
1206 return gdb_error(connection
, retval
);
1209 for (i
= 0; i
< reg_list_size
; i
++) {
1211 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1213 if (packet_p
+ chars
> packet
+ packet_size
)
1214 LOG_ERROR("BUG: register packet is too small for registers");
1216 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1217 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1219 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1221 /* advance packet pointer */
1227 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1230 gdb_put_packet(connection
, "OK", 2);
1235 static int gdb_get_register_packet(struct connection
*connection
,
1236 char *packet
, int packet_size
)
1238 struct target
*target
= get_target_from_connection(connection
);
1240 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1241 struct reg
**reg_list
;
1245 #ifdef _DEBUG_GDB_IO_
1249 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1251 if (retval
!= ERROR_OK
)
1252 return gdb_error(connection
, retval
);
1254 if (reg_list_size
<= reg_num
) {
1255 LOG_ERROR("gdb requested a non-existing register");
1256 return ERROR_SERVER_REMOTE_CLOSED
;
1259 if (!reg_list
[reg_num
]->valid
)
1260 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1262 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1); /* plus one for string termination null */
1264 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1266 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1274 static int gdb_set_register_packet(struct connection
*connection
,
1275 char *packet
, int packet_size
)
1277 struct target
*target
= get_target_from_connection(connection
);
1280 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1281 struct reg
**reg_list
;
1287 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1289 if (retval
!= ERROR_OK
)
1290 return gdb_error(connection
, retval
);
1292 if (reg_list_size
<= reg_num
) {
1293 LOG_ERROR("gdb requested a non-existing register");
1294 return ERROR_SERVER_REMOTE_CLOSED
;
1297 if (*separator
!= '=') {
1298 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1299 return ERROR_SERVER_REMOTE_CLOSED
;
1302 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1303 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1304 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1306 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1307 LOG_ERROR("gdb sent a packet with wrong register size");
1309 return ERROR_SERVER_REMOTE_CLOSED
;
1312 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1314 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1316 gdb_put_packet(connection
, "OK", 2);
1324 /* No attempt is made to translate the "retval" to
1325 * GDB speak. This has to be done at the calling
1326 * site as no mapping really exists.
1328 static int gdb_error(struct connection
*connection
, int retval
)
1330 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1331 gdb_send_error(connection
, EFAULT
);
1335 /* We don't have to worry about the default 2 second timeout for GDB packets,
1336 * because GDB breaks up large memory reads into smaller reads.
1338 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1340 static int gdb_read_memory_packet(struct connection
*connection
,
1341 char *packet
, int packet_size
)
1343 struct target
*target
= get_target_from_connection(connection
);
1351 int retval
= ERROR_OK
;
1353 /* skip command character */
1356 addr
= strtoul(packet
, &separator
, 16);
1358 if (*separator
!= ',') {
1359 LOG_ERROR("incomplete read memory packet received, dropping connection");
1360 return ERROR_SERVER_REMOTE_CLOSED
;
1363 len
= strtoul(separator
+ 1, NULL
, 16);
1366 LOG_WARNING("invalid read memory packet received (len == 0)");
1367 gdb_put_packet(connection
, NULL
, 0);
1371 buffer
= malloc(len
);
1373 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1375 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1377 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1378 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1379 * At some point this might be fixed in GDB, in which case this code can be removed.
1381 * OpenOCD developers are acutely aware of this problem, but there is nothing
1382 * gained by involving the user in this problem that hopefully will get resolved
1385 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1386 * cmd = view%20audit-trail&database = gdb&pr = 2395
1388 * For now, the default is to fix up things to make current GDB versions work.
1389 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1391 memset(buffer
, 0, len
);
1395 if (retval
== ERROR_OK
) {
1396 hex_buffer
= malloc(len
* 2 + 1);
1398 int pkt_len
= hexify(hex_buffer
, (char *)buffer
, len
, len
* 2 + 1);
1400 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1404 retval
= gdb_error(connection
, retval
);
1411 static int gdb_write_memory_packet(struct connection
*connection
,
1412 char *packet
, int packet_size
)
1414 struct target
*target
= get_target_from_connection(connection
);
1422 /* skip command character */
1425 addr
= strtoul(packet
, &separator
, 16);
1427 if (*separator
!= ',') {
1428 LOG_ERROR("incomplete write memory packet received, dropping connection");
1429 return ERROR_SERVER_REMOTE_CLOSED
;
1432 len
= strtoul(separator
+ 1, &separator
, 16);
1434 if (*(separator
++) != ':') {
1435 LOG_ERROR("incomplete write memory packet received, dropping connection");
1436 return ERROR_SERVER_REMOTE_CLOSED
;
1439 buffer
= malloc(len
);
1441 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1443 if (unhexify((char *)buffer
, separator
, len
) != (int)len
)
1444 LOG_ERROR("unable to decode memory packet");
1446 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1448 if (retval
== ERROR_OK
)
1449 gdb_put_packet(connection
, "OK", 2);
1451 retval
= gdb_error(connection
, retval
);
1458 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1459 char *packet
, int packet_size
)
1461 struct target
*target
= get_target_from_connection(connection
);
1466 int retval
= ERROR_OK
;
1468 /* skip command character */
1471 addr
= strtoul(packet
, &separator
, 16);
1473 if (*separator
!= ',') {
1474 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1475 return ERROR_SERVER_REMOTE_CLOSED
;
1478 len
= strtoul(separator
+ 1, &separator
, 16);
1480 if (*(separator
++) != ':') {
1481 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1482 return ERROR_SERVER_REMOTE_CLOSED
;
1485 struct gdb_connection
*gdb_connection
= connection
->priv
;
1487 if (gdb_connection
->mem_write_error
) {
1488 retval
= ERROR_FAIL
;
1489 /* now that we have reported the memory write error, we can clear the condition */
1490 gdb_connection
->mem_write_error
= false;
1493 /* By replying the packet *immediately* GDB will send us a new packet
1494 * while we write the last one to the target.
1496 if (retval
== ERROR_OK
)
1497 gdb_put_packet(connection
, "OK", 2);
1499 retval
= gdb_error(connection
, retval
);
1500 if (retval
!= ERROR_OK
)
1505 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1507 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1508 if (retval
!= ERROR_OK
)
1509 gdb_connection
->mem_write_error
= true;
1515 static int gdb_step_continue_packet(struct connection
*connection
,
1516 char *packet
, int packet_size
)
1518 struct target
*target
= get_target_from_connection(connection
);
1520 uint32_t address
= 0x0;
1521 int retval
= ERROR_OK
;
1525 if (packet_size
> 1) {
1526 packet
[packet_size
] = 0;
1527 address
= strtoul(packet
+ 1, NULL
, 16);
1531 gdb_running_type
= packet
[0];
1532 if (packet
[0] == 'c') {
1533 LOG_DEBUG("continue");
1534 /* resume at current address, don't handle breakpoints, not debugging */
1535 retval
= target_resume(target
, current
, address
, 0, 0);
1536 } else if (packet
[0] == 's') {
1538 /* step at current or address, don't handle breakpoints */
1539 retval
= target_step(target
, current
, address
, 0);
1544 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1545 char *packet
, int packet_size
)
1547 struct target
*target
= get_target_from_connection(connection
);
1549 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1550 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1558 type
= strtoul(packet
+ 1, &separator
, 16);
1560 if (type
== 0) /* memory breakpoint */
1561 bp_type
= BKPT_SOFT
;
1562 else if (type
== 1) /* hardware breakpoint */
1563 bp_type
= BKPT_HARD
;
1564 else if (type
== 2) /* write watchpoint */
1565 wp_type
= WPT_WRITE
;
1566 else if (type
== 3) /* read watchpoint */
1568 else if (type
== 4) /* access watchpoint */
1569 wp_type
= WPT_ACCESS
;
1571 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1572 return ERROR_SERVER_REMOTE_CLOSED
;
1575 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1576 bp_type
= gdb_breakpoint_override_type
;
1578 if (*separator
!= ',') {
1579 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1580 return ERROR_SERVER_REMOTE_CLOSED
;
1583 address
= strtoul(separator
+ 1, &separator
, 16);
1585 if (*separator
!= ',') {
1586 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1587 return ERROR_SERVER_REMOTE_CLOSED
;
1590 size
= strtoul(separator
+ 1, &separator
, 16);
1595 if (packet
[0] == 'Z') {
1596 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1597 if (retval
!= ERROR_OK
) {
1598 retval
= gdb_error(connection
, retval
);
1599 if (retval
!= ERROR_OK
)
1602 gdb_put_packet(connection
, "OK", 2);
1604 breakpoint_remove(target
, address
);
1605 gdb_put_packet(connection
, "OK", 2);
1612 if (packet
[0] == 'Z') {
1613 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1614 if (retval
!= ERROR_OK
) {
1615 retval
= gdb_error(connection
, retval
);
1616 if (retval
!= ERROR_OK
)
1619 gdb_put_packet(connection
, "OK", 2);
1621 watchpoint_remove(target
, address
);
1622 gdb_put_packet(connection
, "OK", 2);
1633 /* print out a string and allocate more space as needed,
1634 * mainly used for XML at this point
1636 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1637 const char *fmt
, ...)
1639 if (*retval
!= ERROR_OK
)
1644 if ((*xml
== NULL
) || (!first
)) {
1645 /* start by 0 to exercise all the code paths.
1646 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1648 *size
= *size
* 2 + 2;
1650 *xml
= realloc(*xml
, *size
);
1654 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1662 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1664 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1668 /* there was just enough or not enough space, allocate more. */
1673 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1677 /* Extract and NUL-terminate the annex. */
1679 while (*buf
&& *buf
!= ':')
1685 /* After the read marker and annex, qXfer looks like a
1686 * traditional 'm' packet. */
1688 *ofs
= strtoul(buf
, &separator
, 16);
1690 if (*separator
!= ',')
1693 *len
= strtoul(separator
+ 1, NULL
, 16);
1698 static int compare_bank(const void *a
, const void *b
)
1700 struct flash_bank
*b1
, *b2
;
1701 b1
= *((struct flash_bank
**)a
);
1702 b2
= *((struct flash_bank
**)b
);
1704 if (b1
->base
== b2
->base
)
1706 else if (b1
->base
> b2
->base
)
1712 static int gdb_memory_map(struct connection
*connection
,
1713 char *packet
, int packet_size
)
1715 /* We get away with only specifying flash here. Regions that are not
1716 * specified are treated as if we provided no memory map(if not we
1717 * could detect the holes and mark them as RAM).
1718 * Normally we only execute this code once, but no big deal if we
1719 * have to regenerate it a couple of times.
1722 struct target
*target
= get_target_from_connection(connection
);
1723 struct flash_bank
*p
;
1727 int retval
= ERROR_OK
;
1728 struct flash_bank
**banks
;
1732 uint32_t ram_start
= 0;
1734 int target_flash_banks
= 0;
1736 /* skip command character */
1739 offset
= strtoul(packet
, &separator
, 16);
1740 length
= strtoul(separator
+ 1, &separator
, 16);
1742 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1744 /* Sort banks in ascending order. We need to report non-flash
1745 * memory as ram (or rather read/write) by default for GDB, since
1746 * it has no concept of non-cacheable read/write memory (i/o etc).
1748 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1749 * Current versions of GDB assume unlisted addresses are RAM...
1751 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1753 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1754 p
= get_flash_bank_by_num_noprobe(i
);
1755 if (p
->target
!= target
)
1757 retval
= get_flash_bank_by_num(i
, &p
);
1758 if (retval
!= ERROR_OK
) {
1760 gdb_error(connection
, retval
);
1763 banks
[target_flash_banks
++] = p
;
1766 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1769 for (i
= 0; i
< target_flash_banks
; i
++) {
1771 unsigned sector_size
= 0;
1777 if (ram_start
< p
->base
)
1778 xml_printf(&retval
, &xml
, &pos
, &size
,
1779 "<memory type=\"ram\" start=\"0x%x\" "
1780 "length=\"0x%x\"/>\n",
1781 ram_start
, p
->base
- ram_start
);
1783 /* Report adjacent groups of same-size sectors. So for
1784 * example top boot CFI flash will list an initial region
1785 * with several large sectors (maybe 128KB) and several
1786 * smaller ones at the end (maybe 32KB). STR7 will have
1787 * regions with 8KB, 32KB, and 64KB sectors; etc.
1789 for (j
= 0; j
< p
->num_sectors
; j
++) {
1792 /* Maybe start a new group of sectors. */
1793 if (sector_size
== 0) {
1794 start
= p
->base
+ p
->sectors
[j
].offset
;
1795 xml_printf(&retval
, &xml
, &pos
, &size
,
1796 "<memory type=\"flash\" "
1799 sector_size
= p
->sectors
[j
].size
;
1802 /* Does this finish a group of sectors?
1803 * If not, continue an already-started group.
1805 if (j
== p
->num_sectors
- 1)
1806 group_len
= (p
->base
+ p
->size
) - start
;
1807 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1808 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1813 xml_printf(&retval
, &xml
, &pos
, &size
,
1814 "length=\"0x%x\">\n"
1815 "<property name=\"blocksize\">"
1823 ram_start
= p
->base
+ p
->size
;
1827 xml_printf(&retval
, &xml
, &pos
, &size
,
1828 "<memory type=\"ram\" start=\"0x%x\" "
1829 "length=\"0x%x\"/>\n",
1830 ram_start
, 0-ram_start
);
1831 /* ELSE a flash chip could be at the very end of the 32 bit address
1832 * space, in which case ram_start will be precisely 0
1838 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1840 if (retval
!= ERROR_OK
) {
1841 gdb_error(connection
, retval
);
1845 if (offset
+ length
> pos
)
1846 length
= pos
- offset
;
1848 char *t
= malloc(length
+ 1);
1850 memcpy(t
+ 1, xml
+ offset
, length
);
1851 gdb_put_packet(connection
, t
, length
+ 1);
1858 static const char *gdb_get_reg_type_name(enum reg_type type
)
1865 case REG_TYPE_INT16
:
1867 case REG_TYPE_INT32
:
1869 case REG_TYPE_INT64
:
1871 case REG_TYPE_INT128
:
1873 case REG_TYPE_UINT8
:
1875 case REG_TYPE_UINT16
:
1877 case REG_TYPE_UINT32
:
1879 case REG_TYPE_UINT64
:
1881 case REG_TYPE_UINT128
:
1883 case REG_TYPE_CODE_PTR
:
1885 case REG_TYPE_DATA_PTR
:
1887 case REG_TYPE_FLOAT
:
1889 case REG_TYPE_IEEE_SINGLE
:
1890 return "ieee_single";
1891 case REG_TYPE_IEEE_DOUBLE
:
1892 return "ieee_double";
1893 case REG_TYPE_ARCH_DEFINED
:
1894 return "int"; /* return arbitrary string to avoid compile warning. */
1897 return "int"; /* "int" as default value */
1900 static int gdb_generate_reg_type_description(struct target
*target
,
1901 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
)
1903 int retval
= ERROR_OK
;
1905 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
1906 /* <vector id="id" type="type" count="count"/> */
1907 xml_printf(&retval
, tdesc
, pos
, size
,
1908 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
1909 type
->id
, type
->reg_type_vector
->type
->id
,
1910 type
->reg_type_vector
->count
);
1912 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
1914 * <field name="name" type="type"/> ...
1916 xml_printf(&retval
, tdesc
, pos
, size
,
1917 "<union id=\"%s\">\n",
1920 struct reg_data_type_union_field
*field
;
1921 field
= type
->reg_type_union
->fields
;
1922 while (field
!= NULL
) {
1923 xml_printf(&retval
, tdesc
, pos
, size
,
1924 "<field name=\"%s\" type=\"%s\"/>\n",
1925 field
->name
, field
->type
->id
);
1927 field
= field
->next
;
1930 xml_printf(&retval
, tdesc
, pos
, size
,
1933 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
1934 struct reg_data_type_struct_field
*field
;
1935 field
= type
->reg_type_struct
->fields
;
1937 if (field
->use_bitfields
) {
1938 /* <struct id="id" size="size">
1939 * <field name="name" start="start" end="end"/> ...
1941 xml_printf(&retval
, tdesc
, pos
, size
,
1942 "<struct id=\"%s\" size=\"%d\">\n",
1943 type
->id
, type
->reg_type_struct
->size
);
1944 while (field
!= NULL
) {
1945 xml_printf(&retval
, tdesc
, pos
, size
,
1946 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1947 field
->name
, field
->bitfield
->start
,
1948 field
->bitfield
->end
);
1950 field
= field
->next
;
1954 * <field name="name" type="type"/> ...
1956 xml_printf(&retval
, tdesc
, pos
, size
,
1957 "<struct id=\"%s\">\n",
1959 while (field
!= NULL
) {
1960 xml_printf(&retval
, tdesc
, pos
, size
,
1961 "<field name=\"%s\" type=\"%s\"/>\n",
1962 field
->name
, field
->type
->id
);
1964 field
= field
->next
;
1968 xml_printf(&retval
, tdesc
, pos
, size
,
1971 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
1972 /* <flags id="id" size="size">
1973 * <field name="name" start="start" end="end"/> ...
1975 xml_printf(&retval
, tdesc
, pos
, size
,
1976 "<flags id=\"%s\" size=\"%d\">\n",
1977 type
->id
, type
->reg_type_flags
->size
);
1979 struct reg_data_type_flags_field
*field
;
1980 field
= type
->reg_type_flags
->fields
;
1981 while (field
!= NULL
) {
1982 xml_printf(&retval
, tdesc
, pos
, size
,
1983 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1984 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
);
1986 field
= field
->next
;
1989 xml_printf(&retval
, tdesc
, pos
, size
,
1997 /* Get a list of available target registers features. feature_list must
1998 * be freed by caller.
2000 static int get_reg_features_list(struct target
*target
, char **feature_list
[], int *feature_list_size
,
2001 struct reg
**reg_list
, int reg_list_size
)
2005 /* Start with only one element */
2006 *feature_list
= calloc(1, sizeof(char *));
2008 for (int i
= 0; i
< reg_list_size
; i
++) {
2009 if (reg_list
[i
]->exist
== false)
2012 if (reg_list
[i
]->feature
!= NULL
2013 && reg_list
[i
]->feature
->name
!= NULL
2014 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2015 /* We found a feature, check if the feature is already in the
2016 * table. If not, allocate a new entry for the table and
2017 * put the new feature in it.
2019 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2020 if (!((*feature_list
)[j
])) {
2021 (*feature_list
)[tbl_sz
++] = strdup(reg_list
[i
]->feature
->name
);
2022 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2023 (*feature_list
)[tbl_sz
] = NULL
;
2026 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2033 if (feature_list_size
)
2034 *feature_list_size
= tbl_sz
;
2039 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2041 int retval
= ERROR_OK
;
2042 struct reg
**reg_list
;
2048 retval
= target_get_gdb_reg_list(target
, ®_list
,
2049 ®_list_size
, REG_CLASS_ALL
);
2051 if (retval
!= ERROR_OK
) {
2052 LOG_ERROR("get register list failed");
2056 if (reg_list_size
<= 0) {
2061 char **features
= NULL
;
2062 /* Get a list of available target registers features */
2063 retval
= get_reg_features_list(target
, &features
, NULL
, reg_list
, reg_list_size
);
2064 if (retval
!= ERROR_OK
) {
2065 LOG_ERROR("Can't get the registers feature list");
2070 /* If we found some features associated with registers, create sections */
2071 int current_feature
= 0;
2073 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2074 "<?xml version=\"1.0\"?>\n"
2075 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2076 "<target version=\"1.0\">\n");
2078 /* generate target description according to register list */
2079 if (features
!= NULL
) {
2080 while (features
[current_feature
]) {
2082 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2083 "<feature name=\"%s\">\n",
2084 features
[current_feature
]);
2087 for (i
= 0; i
< reg_list_size
; i
++) {
2089 if (reg_list
[i
]->exist
== false)
2092 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2095 const char *type_str
;
2096 if (reg_list
[i
]->reg_data_type
!= NULL
) {
2097 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2098 /* generate <type... first, if there are architecture-defined types. */
2099 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2100 reg_list
[i
]->reg_data_type
);
2102 type_str
= reg_list
[i
]->reg_data_type
->id
;
2104 /* predefined type */
2105 type_str
= gdb_get_reg_type_name(
2106 reg_list
[i
]->reg_data_type
->type
);
2109 /* Default type is "int" */
2113 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2114 "<reg name=\"%s\"", reg_list
[i
]->name
);
2115 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2116 " bitsize=\"%d\"", reg_list
[i
]->size
);
2117 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2118 " regnum=\"%d\"", reg_list
[i
]->number
);
2119 if (reg_list
[i
]->caller_save
)
2120 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2121 " save-restore=\"yes\"");
2123 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2124 " save-restore=\"no\"");
2126 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2127 " type=\"%s\"", type_str
);
2129 if (reg_list
[i
]->group
!= NULL
)
2130 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2131 " group=\"%s\"", reg_list
[i
]->group
);
2133 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2137 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2144 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2150 if (retval
== ERROR_OK
)
2158 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2159 char **chunk
, int32_t offset
, uint32_t length
)
2161 if (target_desc
== NULL
) {
2162 LOG_ERROR("Unable to Generate Target Description");
2166 char *tdesc
= target_desc
->tdesc
;
2167 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2169 if (tdesc
== NULL
) {
2170 int retval
= gdb_generate_target_description(target
, &tdesc
);
2171 if (retval
!= ERROR_OK
) {
2172 LOG_ERROR("Unable to Generate Target Description");
2176 tdesc_length
= strlen(tdesc
);
2181 if (length
< (tdesc_length
- offset
))
2182 transfer_type
= 'm';
2184 transfer_type
= 'l';
2186 *chunk
= malloc(length
+ 2);
2187 if (*chunk
== NULL
) {
2188 LOG_ERROR("Unable to allocate memory");
2192 (*chunk
)[0] = transfer_type
;
2193 if (transfer_type
== 'm') {
2194 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2195 (*chunk
)[1 + length
] = '\0';
2197 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2198 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2200 /* After gdb-server sends out last chunk, invalidate tdesc. */
2206 target_desc
->tdesc
= tdesc
;
2207 target_desc
->tdesc_length
= tdesc_length
;
2212 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2214 int retval
= ERROR_OK
;
2215 struct reg
**reg_list
= NULL
;
2216 int reg_list_size
= 0;
2217 int feature_list_size
= 0;
2218 char **features
= NULL
;
2220 retval
= target_get_gdb_reg_list(target
, ®_list
,
2221 ®_list_size
, REG_CLASS_ALL
);
2222 if (retval
!= ERROR_OK
) {
2223 LOG_ERROR("get register list failed");
2227 if (reg_list_size
<= 0) {
2228 retval
= ERROR_FAIL
;
2232 /* Get a list of available target registers features */
2233 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2234 if (retval
!= ERROR_OK
) {
2235 LOG_ERROR("Can't get the registers feature list");
2240 if (feature_list_size
)
2247 if (reg_list
!= NULL
)
2250 if (features
!= NULL
)
2256 static int gdb_query_packet(struct connection
*connection
,
2257 char *packet
, int packet_size
)
2259 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2260 struct gdb_connection
*gdb_connection
= connection
->priv
;
2261 struct target
*target
= get_target_from_connection(connection
);
2263 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2264 if (packet_size
> 6) {
2266 cmd
= malloc((packet_size
- 6) / 2 + 1);
2267 int len
= unhexify(cmd
, packet
+ 6, (packet_size
- 6) / 2);
2270 /* We want to print all debug output to GDB connection */
2271 log_add_callback(gdb_log_callback
, connection
);
2272 target_call_timer_callbacks_now();
2273 /* some commands need to know the GDB connection, make note of current
2274 * GDB connection. */
2275 current_gdb_connection
= gdb_connection
;
2276 command_run_line(cmd_ctx
, cmd
);
2277 current_gdb_connection
= NULL
;
2278 target_call_timer_callbacks_now();
2279 log_remove_callback(gdb_log_callback
, connection
);
2282 gdb_put_packet(connection
, "OK", 2);
2284 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2285 if (packet_size
> 5) {
2293 /* skip command character */
2296 addr
= strtoul(packet
, &separator
, 16);
2298 if (*separator
!= ',') {
2299 LOG_ERROR("incomplete read memory packet received, dropping connection");
2300 return ERROR_SERVER_REMOTE_CLOSED
;
2303 len
= strtoul(separator
+ 1, NULL
, 16);
2305 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2307 if (retval
== ERROR_OK
) {
2308 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2309 gdb_put_packet(connection
, gdb_reply
, 9);
2311 retval
= gdb_error(connection
, retval
);
2312 if (retval
!= ERROR_OK
)
2318 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2319 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2320 * qXfer:features:read is supported for some targets */
2321 int retval
= ERROR_OK
;
2322 char *buffer
= NULL
;
2325 int gdb_target_desc_supported
= 0;
2327 /* we need to test that the target supports target descriptions */
2328 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2329 if (retval
!= ERROR_OK
) {
2330 LOG_INFO("Failed detecting Target Description Support, disabling");
2331 gdb_target_desc_supported
= 0;
2334 /* support may be disabled globally */
2335 if (gdb_use_target_description
== 0) {
2336 if (gdb_target_desc_supported
)
2337 LOG_WARNING("Target Descriptions Supported, but disabled");
2338 gdb_target_desc_supported
= 0;
2345 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;QStartNoAckMode+",
2346 (GDB_BUFFER_SIZE
- 1),
2347 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2348 (gdb_target_desc_supported
== 1) ? '+' : '-');
2350 if (retval
!= ERROR_OK
) {
2351 gdb_send_error(connection
, 01);
2355 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2359 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2360 && (flash_get_bank_count() > 0))
2361 return gdb_memory_map(connection
, packet
, packet_size
);
2362 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2364 int retval
= ERROR_OK
;
2367 unsigned int length
;
2370 /* skip command character */
2373 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0) {
2374 gdb_send_error(connection
, 01);
2378 /* Target should prepare correct target description for annex.
2379 * The first character of returned xml is 'm' or 'l'. 'm' for
2380 * there are *more* chunks to transfer. 'l' for it is the *last*
2381 * chunk of target description.
2383 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2384 &xml
, offset
, length
);
2385 if (retval
!= ERROR_OK
) {
2386 gdb_error(connection
, retval
);
2390 gdb_put_packet(connection
, xml
, strlen(xml
));
2394 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2395 gdb_connection
->noack_mode
= 1;
2396 gdb_put_packet(connection
, "OK", 2);
2400 gdb_put_packet(connection
, "", 0);
2404 static int gdb_v_packet(struct connection
*connection
,
2405 char *packet
, int packet_size
)
2407 struct gdb_connection
*gdb_connection
= connection
->priv
;
2408 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2411 /* if flash programming disabled - send a empty reply */
2413 if (gdb_flash_program
== 0) {
2414 gdb_put_packet(connection
, "", 0);
2418 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
2420 unsigned long length
;
2422 char *parse
= packet
+ 12;
2423 if (*parse
== '\0') {
2424 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2425 return ERROR_SERVER_REMOTE_CLOSED
;
2428 addr
= strtoul(parse
, &parse
, 16);
2430 if (*(parse
++) != ',' || *parse
== '\0') {
2431 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2432 return ERROR_SERVER_REMOTE_CLOSED
;
2435 length
= strtoul(parse
, &parse
, 16);
2437 if (*parse
!= '\0') {
2438 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2439 return ERROR_SERVER_REMOTE_CLOSED
;
2442 /* assume all sectors need erasing - stops any problems
2443 * when flash_write is called multiple times */
2446 /* perform any target specific operations before the erase */
2447 target_call_event_callbacks(gdb_service
->target
,
2448 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2450 /* vFlashErase:addr,length messages require region start and
2451 * end to be "block" aligned ... if padding is ever needed,
2452 * GDB will have become dangerously confused.
2454 result
= flash_erase_address_range(gdb_service
->target
,
2455 false, addr
, length
);
2457 /* perform any target specific operations after the erase */
2458 target_call_event_callbacks(gdb_service
->target
,
2459 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2462 if (result
!= ERROR_OK
) {
2463 /* GDB doesn't evaluate the actual error number returned,
2464 * treat a failed erase as an I/O error
2466 gdb_send_error(connection
, EIO
);
2467 LOG_ERROR("flash_erase returned %i", result
);
2469 gdb_put_packet(connection
, "OK", 2);
2474 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
2477 unsigned long length
;
2478 char *parse
= packet
+ 12;
2480 if (*parse
== '\0') {
2481 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2482 return ERROR_SERVER_REMOTE_CLOSED
;
2484 addr
= strtoul(parse
, &parse
, 16);
2485 if (*(parse
++) != ':') {
2486 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2487 return ERROR_SERVER_REMOTE_CLOSED
;
2489 length
= packet_size
- (parse
- packet
);
2491 /* create a new image if there isn't already one */
2492 if (gdb_connection
->vflash_image
== NULL
) {
2493 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2494 image_open(gdb_connection
->vflash_image
, "", "build");
2497 /* create new section with content from packet buffer */
2498 retval
= image_add_section(gdb_connection
->vflash_image
,
2499 addr
, length
, 0x0, (uint8_t *)parse
);
2500 if (retval
!= ERROR_OK
)
2503 gdb_put_packet(connection
, "OK", 2);
2508 if (strncmp(packet
, "vFlashDone", 10) == 0) {
2511 /* process the flashing buffer. No need to erase as GDB
2512 * always issues a vFlashErase first. */
2513 target_call_event_callbacks(gdb_service
->target
,
2514 TARGET_EVENT_GDB_FLASH_WRITE_START
);
2515 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2516 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2517 if (result
!= ERROR_OK
) {
2518 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2519 gdb_put_packet(connection
, "E.memtype", 9);
2521 gdb_send_error(connection
, EIO
);
2523 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2524 gdb_put_packet(connection
, "OK", 2);
2527 image_close(gdb_connection
->vflash_image
);
2528 free(gdb_connection
->vflash_image
);
2529 gdb_connection
->vflash_image
= NULL
;
2534 gdb_put_packet(connection
, "", 0);
2538 static int gdb_detach(struct connection
*connection
)
2540 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2542 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
2544 return gdb_put_packet(connection
, "OK", 2);
2547 /* The format of 'F' response packet is
2548 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2550 static int gdb_fileio_response_packet(struct connection
*connection
,
2551 char *packet
, int packet_size
)
2553 struct target
*target
= get_target_from_connection(connection
);
2555 char *parsing_point
;
2556 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
2557 int fileio_errno
= 0;
2558 bool fileio_ctrl_c
= false;
2563 if (*separator
== ',') {
2564 parsing_point
= separator
+ 1;
2565 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
2566 if (*separator
== ',') {
2567 if (*(separator
+ 1) == 'C') {
2568 /* TODO: process ctrl-c */
2569 fileio_ctrl_c
= true;
2574 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
2575 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
2577 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
2578 if (retval
!= ERROR_OK
)
2581 /* After File-I/O ends, keep continue or step */
2582 if (gdb_running_type
== 'c')
2583 retval
= target_resume(target
, 1, 0x0, 0, 0);
2584 else if (gdb_running_type
== 's')
2585 retval
= target_step(target
, 1, 0x0, 0);
2587 retval
= ERROR_FAIL
;
2589 if (retval
!= ERROR_OK
)
2595 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2596 const char *function
, const char *string
)
2598 struct connection
*connection
= priv
;
2599 struct gdb_connection
*gdb_con
= connection
->priv
;
2601 if (gdb_con
->busy
) {
2602 /* do not reply this using the O packet */
2606 gdb_output_con(connection
, string
);
2609 static void gdb_sig_halted(struct connection
*connection
)
2612 snprintf(sig_reply
, 4, "T%2.2x", 2);
2613 gdb_put_packet(connection
, sig_reply
, 3);
2616 static int gdb_input_inner(struct connection
*connection
)
2618 /* Do not allocate this on the stack */
2619 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2621 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2622 struct target
*target
= gdb_service
->target
;
2623 char *packet
= gdb_packet_buffer
;
2626 struct gdb_connection
*gdb_con
= connection
->priv
;
2627 static int extended_protocol
;
2629 /* drain input buffer. If one of the packets fail, then an error
2630 * packet is replied, if applicable.
2632 * This loop will terminate and the error code is returned.
2634 * The calling fn will check if this error is something that
2635 * can be recovered from, or if the connection must be closed.
2637 * If the error is recoverable, this fn is called again to
2638 * drain the rest of the buffer.
2641 packet_size
= GDB_BUFFER_SIZE
-1;
2642 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2643 if (retval
!= ERROR_OK
)
2646 /* terminate with zero */
2647 packet
[packet_size
] = 0;
2649 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2650 if (packet
[0] == 'X') {
2651 /* binary packets spew junk into the debug log stream */
2654 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
2657 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2659 LOG_DEBUG("received packet: '%s'", packet
);
2662 if (packet_size
> 0) {
2664 switch (packet
[0]) {
2665 case 'T': /* Is thread alive? */
2666 gdb_thread_packet(connection
, packet
, packet_size
);
2668 case 'H': /* Set current thread ( 'c' for step and continue,
2669 * 'g' for all other operations ) */
2670 gdb_thread_packet(connection
, packet
, packet_size
);
2674 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2675 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2676 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2679 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2682 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2685 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2688 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2691 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2694 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2698 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2701 gdb_last_signal_packet(connection
, packet
, packet_size
);
2706 gdb_thread_packet(connection
, packet
, packet_size
);
2707 log_add_callback(gdb_log_callback
, connection
);
2709 if (gdb_con
->mem_write_error
) {
2710 LOG_ERROR("Memory write failure!");
2712 /* now that we have reported the memory write error,
2713 * we can clear the condition */
2714 gdb_con
->mem_write_error
= false;
2717 bool nostep
= false;
2718 bool already_running
= false;
2719 if (target
->state
== TARGET_RUNNING
) {
2720 LOG_WARNING("WARNING! The target is already running. "
2721 "All changes GDB did to registers will be discarded! "
2722 "Waiting for target to halt.");
2723 already_running
= true;
2724 } else if (target
->state
!= TARGET_HALTED
) {
2725 LOG_WARNING("The target is not in the halted nor running stated, " \
2726 "stepi/continue ignored.");
2728 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
2729 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2730 * sent by GDB first to OpenOCD, thus defeating the check to
2731 * make only the single stepping have the sync feature...
2734 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2735 "from the target.");
2737 gdb_con
->sync
= false;
2739 if (!already_running
&& nostep
) {
2740 /* Either the target isn't in the halted state, then we can't
2741 * step/continue. This might be early setup, etc.
2743 * Or we want to allow GDB to pick up a fresh set of
2744 * register values without modifying the target state.
2747 gdb_sig_halted(connection
);
2749 /* stop forwarding log packets! */
2750 log_remove_callback(gdb_log_callback
, connection
);
2752 /* We're running/stepping, in which case we can
2753 * forward log output until the target is halted
2755 gdb_con
->frontend_state
= TARGET_RUNNING
;
2756 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2758 if (!already_running
) {
2759 /* Here we don't want packet processing to stop even if this fails,
2760 * so we use a local variable instead of retval. */
2761 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2762 if (retval
!= ERROR_OK
) {
2763 /* we'll never receive a halted
2764 * condition... issue a false one..
2766 gdb_frontend_halted(target
, connection
);
2773 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2776 retval
= gdb_detach(connection
);
2777 extended_protocol
= 0;
2780 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2781 if (retval
!= ERROR_OK
)
2785 if (extended_protocol
!= 0) {
2786 gdb_con
->attached
= false;
2789 gdb_put_packet(connection
, "OK", 2);
2790 return ERROR_SERVER_REMOTE_CLOSED
;
2792 /* handle extended remote protocol */
2793 extended_protocol
= 1;
2794 gdb_put_packet(connection
, "OK", 2);
2797 /* handle extended restart packet */
2798 breakpoint_clear_target(gdb_service
->target
);
2799 watchpoint_clear_target(gdb_service
->target
);
2800 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
2801 target_name(target
));
2802 /* set connection as attached after reset */
2803 gdb_con
->attached
= true;
2804 /* info rtos parts */
2805 gdb_thread_packet(connection
, packet
, packet_size
);
2809 /* packet supported only by smp target i.e cortex_a.c*/
2810 /* handle smp packet replying coreid played to gbd */
2811 gdb_read_smp_packet(connection
, packet
, packet_size
);
2815 /* packet supported only by smp target i.e cortex_a.c */
2816 /* handle smp packet setting coreid to be played at next
2818 gdb_write_smp_packet(connection
, packet
, packet_size
);
2822 /* File-I/O extension */
2823 /* After gdb uses host-side syscall to complete target file
2824 * I/O, gdb sends host-side syscall return value to target
2826 * The format of 'F' response packet is
2827 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2829 gdb_con
->frontend_state
= TARGET_RUNNING
;
2830 log_add_callback(gdb_log_callback
, connection
);
2831 gdb_fileio_response_packet(connection
, packet
, packet_size
);
2835 /* ignore unknown packets */
2836 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2837 gdb_put_packet(connection
, NULL
, 0);
2841 /* if a packet handler returned an error, exit input loop */
2842 if (retval
!= ERROR_OK
)
2846 if (gdb_con
->ctrl_c
) {
2847 if (target
->state
== TARGET_RUNNING
) {
2848 retval
= target_halt(target
);
2849 if (retval
!= ERROR_OK
)
2850 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2851 gdb_con
->ctrl_c
= 0;
2853 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2854 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2858 } while (gdb_con
->buf_cnt
> 0);
2863 static int gdb_input(struct connection
*connection
)
2865 int retval
= gdb_input_inner(connection
);
2866 struct gdb_connection
*gdb_con
= connection
->priv
;
2867 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2870 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2871 if (gdb_con
->closed
)
2872 return ERROR_SERVER_REMOTE_CLOSED
;
2874 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2878 static int gdb_target_start(struct target
*target
, const char *port
)
2880 struct gdb_service
*gdb_service
;
2882 gdb_service
= malloc(sizeof(struct gdb_service
));
2884 if (NULL
== gdb_service
)
2887 gdb_service
->target
= target
;
2888 gdb_service
->core
[0] = -1;
2889 gdb_service
->core
[1] = -1;
2890 target
->gdb_service
= gdb_service
;
2892 ret
= add_service("gdb",
2893 port
, 1, &gdb_new_connection
, &gdb_input
,
2894 &gdb_connection_closed
, gdb_service
);
2895 /* initialialize all targets gdb service with the same pointer */
2897 struct target_list
*head
;
2898 struct target
*curr
;
2899 head
= target
->head
;
2900 while (head
!= (struct target_list
*)NULL
) {
2901 curr
= head
->target
;
2903 curr
->gdb_service
= gdb_service
;
2910 static int gdb_target_add_one(struct target
*target
)
2912 /* one gdb instance per smp list */
2913 if ((target
->smp
) && (target
->gdb_service
))
2915 int retval
= gdb_target_start(target
, gdb_port_next
);
2916 if (retval
== ERROR_OK
) {
2918 /* If we can parse the port number
2919 * then we increment the port number for the next target.
2922 portnumber
= strtol(gdb_port_next
, &end
, 0);
2924 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
2925 free(gdb_port_next
);
2926 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2933 int gdb_target_add_all(struct target
*target
)
2935 if (NULL
== target
) {
2936 LOG_WARNING("gdb services need one or more targets defined");
2940 while (NULL
!= target
) {
2941 int retval
= gdb_target_add_one(target
);
2942 if (ERROR_OK
!= retval
)
2945 target
= target
->next
;
2951 COMMAND_HANDLER(handle_gdb_sync_command
)
2954 return ERROR_COMMAND_SYNTAX_ERROR
;
2956 if (current_gdb_connection
== NULL
) {
2957 command_print(CMD_CTX
,
2958 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2962 current_gdb_connection
->sync
= true;
2967 /* daemon configuration command gdb_port */
2968 COMMAND_HANDLER(handle_gdb_port_command
)
2970 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2971 if (ERROR_OK
== retval
) {
2972 free(gdb_port_next
);
2973 gdb_port_next
= strdup(gdb_port
);
2978 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2981 return ERROR_COMMAND_SYNTAX_ERROR
;
2983 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2987 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2990 return ERROR_COMMAND_SYNTAX_ERROR
;
2992 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2996 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2999 return ERROR_COMMAND_SYNTAX_ERROR
;
3001 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3005 /* gdb_breakpoint_override */
3006 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3008 if (CMD_ARGC
== 0) {
3010 } else if (CMD_ARGC
== 1) {
3011 gdb_breakpoint_override
= 1;
3012 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3013 gdb_breakpoint_override_type
= BKPT_HARD
;
3014 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3015 gdb_breakpoint_override_type
= BKPT_SOFT
;
3016 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3017 gdb_breakpoint_override
= 0;
3019 return ERROR_COMMAND_SYNTAX_ERROR
;
3020 if (gdb_breakpoint_override
)
3021 LOG_USER("force %s breakpoints",
3022 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3024 LOG_USER("breakpoint type is not overridden");
3029 COMMAND_HANDLER(handle_gdb_target_description_command
)
3032 return ERROR_COMMAND_SYNTAX_ERROR
;
3034 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3038 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3041 uint32_t tdesc_length
;
3042 struct target
*target
= get_current_target(CMD_CTX
);
3044 int retval
= gdb_generate_target_description(target
, &tdesc
);
3045 if (retval
!= ERROR_OK
) {
3046 LOG_ERROR("Unable to Generate Target Description");
3050 tdesc_length
= strlen(tdesc
);
3052 struct fileio fileio
;
3053 size_t size_written
;
3055 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3056 if (tdesc_filename
== NULL
) {
3057 retval
= ERROR_FAIL
;
3061 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
3063 if (retval
!= ERROR_OK
) {
3064 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
3068 retval
= fileio_write(&fileio
, tdesc_length
, tdesc
, &size_written
);
3070 fileio_close(&fileio
);
3072 if (retval
!= ERROR_OK
)
3073 LOG_ERROR("Error while writing the tdesc file");
3076 free(tdesc_filename
);
3082 static const struct command_registration gdb_command_handlers
[] = {
3085 .handler
= handle_gdb_sync_command
,
3086 .mode
= COMMAND_ANY
,
3087 .help
= "next stepi will return immediately allowing "
3088 "GDB to fetch register state without affecting "
3094 .handler
= handle_gdb_port_command
,
3095 .mode
= COMMAND_ANY
,
3096 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
3097 "server listens for the next port number after the "
3098 "base port number specified. "
3099 "No arguments reports GDB port. \"pipe\" means listen to stdin "
3100 "output to stdout, an integer is base port number, \"disable\" disables "
3101 "port. Any other string is are interpreted as named pipe to listen to. "
3102 "Output pipe is the same name as input pipe, but with 'o' appended.",
3103 .usage
= "[port_num]",
3106 .name
= "gdb_memory_map",
3107 .handler
= handle_gdb_memory_map_command
,
3108 .mode
= COMMAND_CONFIG
,
3109 .help
= "enable or disable memory map",
3110 .usage
= "('enable'|'disable')"
3113 .name
= "gdb_flash_program",
3114 .handler
= handle_gdb_flash_program_command
,
3115 .mode
= COMMAND_CONFIG
,
3116 .help
= "enable or disable flash program",
3117 .usage
= "('enable'|'disable')"
3120 .name
= "gdb_report_data_abort",
3121 .handler
= handle_gdb_report_data_abort_command
,
3122 .mode
= COMMAND_CONFIG
,
3123 .help
= "enable or disable reporting data aborts",
3124 .usage
= "('enable'|'disable')"
3127 .name
= "gdb_breakpoint_override",
3128 .handler
= handle_gdb_breakpoint_override_command
,
3129 .mode
= COMMAND_ANY
,
3130 .help
= "Display or specify type of breakpoint "
3131 "to be used by gdb 'break' commands.",
3132 .usage
= "('hard'|'soft'|'disable')"
3135 .name
= "gdb_target_description",
3136 .handler
= handle_gdb_target_description_command
,
3137 .mode
= COMMAND_CONFIG
,
3138 .help
= "enable or disable target description",
3139 .usage
= "('enable'|'disable')"
3142 .name
= "gdb_save_tdesc",
3143 .handler
= handle_gdb_save_tdesc_command
,
3144 .mode
= COMMAND_EXEC
,
3145 .help
= "Save the target description file",
3147 COMMAND_REGISTRATION_DONE
3150 int gdb_register_commands(struct command_context
*cmd_ctx
)
3152 gdb_port
= strdup("3333");
3153 gdb_port_next
= strdup("3333");
3154 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);