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 /* private connection data for GDB */
64 struct gdb_connection
{
65 char buffer
[GDB_BUFFER_SIZE
];
69 enum target_state frontend_state
;
70 struct image
*vflash_image
;
74 /* set flag to true if you want the next stepi to return immediately.
75 * allowing GDB to pick up a fresh set of register values from the target
76 * without modifying the target state. */
78 /* We delay reporting memory write errors until next step/continue or memory
79 * write. This improves performance of gdb load significantly as the GDB packet
80 * can be replied immediately and a new GDB packet will be ready without delay
81 * (ca. 10% or so...). */
83 /* with extended-remote it seems we need to better emulate attach/detach.
84 * what this means is we reply with a W stop reply after a kill packet,
85 * normally we reply with a S reply via gdb_last_signal_packet.
86 * as a side note this behaviour only effects gdb > 6.8 */
91 #define _DEBUG_GDB_IO_
94 static struct gdb_connection
*current_gdb_connection
;
96 static int gdb_breakpoint_override
;
97 static enum breakpoint_type gdb_breakpoint_override_type
;
99 static int gdb_error(struct connection
*connection
, int retval
);
100 static const char *gdb_port
;
101 static const char *gdb_port_next
;
103 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
104 const char *function
, const char *string
);
106 /* number of gdb connections, mainly to suppress gdb related debugging spam
107 * in helper/log.c when no gdb connections are actually active */
108 int gdb_actual_connections
;
110 /* set if we are sending a memory map to gdb
111 * via qXfer:memory-map:read packet */
112 /* enabled by default*/
113 static int gdb_use_memory_map
= 1;
114 /* enabled by default*/
115 static int gdb_flash_program
= 1;
117 /* if set, data aborts cause an error to be reported in memory read packets
118 * see the code in gdb_read_memory_packet() for further explanations.
119 * Disabled by default.
121 static int gdb_report_data_abort
;
123 /* set if we are sending target descriptions to gdb
124 * via qXfer:features:read packet */
125 /* disabled by default */
126 static int gdb_use_target_description
;
128 /* current processing free-run type, used by file-I/O */
129 static char gdb_running_type
;
131 static int gdb_last_signal(struct target
*target
)
133 switch (target
->debug_reason
) {
134 case DBG_REASON_DBGRQ
:
135 return 0x2; /* SIGINT */
136 case DBG_REASON_BREAKPOINT
:
137 case DBG_REASON_WATCHPOINT
:
138 case DBG_REASON_WPTANDBKPT
:
139 return 0x05; /* SIGTRAP */
140 case DBG_REASON_SINGLESTEP
:
141 return 0x05; /* SIGTRAP */
142 case DBG_REASON_NOTHALTED
:
143 return 0x0; /* no signal... shouldn't happen */
145 LOG_USER("undefined debug reason %d - target needs reset",
146 target
->debug_reason
);
151 static int check_pending(struct connection
*connection
,
152 int timeout_s
, int *got_data
)
154 /* a non-blocking socket will block if there is 0 bytes available on the socket,
155 * but return with as many bytes as are available immediately
159 struct gdb_connection
*gdb_con
= connection
->priv
;
161 if (got_data
== NULL
)
165 if (gdb_con
->buf_cnt
> 0) {
171 FD_SET(connection
->fd
, &read_fds
);
173 tv
.tv_sec
= timeout_s
;
175 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
176 /* This can typically be because a "monitor" command took too long
177 * before printing any progress messages
180 return ERROR_GDB_TIMEOUT
;
184 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
188 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
190 struct gdb_connection
*gdb_con
= connection
->priv
;
191 int retval
= ERROR_OK
;
193 #ifdef _DEBUG_GDB_IO_
197 if (connection
->service
->type
!= CONNECTION_TCP
)
198 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
200 retval
= check_pending(connection
, 1, NULL
);
201 if (retval
!= ERROR_OK
)
203 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
208 if (gdb_con
->buf_cnt
> 0)
210 if (gdb_con
->buf_cnt
== 0) {
212 return ERROR_SERVER_REMOTE_CLOSED
;
216 errno
= WSAGetLastError();
222 case WSAECONNABORTED
:
224 return ERROR_SERVER_REMOTE_CLOSED
;
227 return ERROR_SERVER_REMOTE_CLOSED
;
229 LOG_ERROR("read: %d", errno
);
239 return ERROR_SERVER_REMOTE_CLOSED
;
242 return ERROR_SERVER_REMOTE_CLOSED
;
244 LOG_ERROR("read: %s", strerror(errno
));
246 return ERROR_SERVER_REMOTE_CLOSED
;
251 #ifdef _DEBUG_GDB_IO_
252 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
253 LOG_DEBUG("received '%s'", debug_buffer
);
257 gdb_con
->buf_p
= gdb_con
->buffer
;
259 *next_char
= *(gdb_con
->buf_p
++);
260 if (gdb_con
->buf_cnt
> 0)
261 connection
->input_pending
= 1;
263 connection
->input_pending
= 0;
264 #ifdef _DEBUG_GDB_IO_
265 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
272 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
273 * held in registers in the inner loop.
275 * For small caches and embedded systems this is important!
277 static inline int gdb_get_char_fast(struct connection
*connection
,
278 int *next_char
, char **buf_p
, int *buf_cnt
)
280 int retval
= ERROR_OK
;
282 if ((*buf_cnt
)-- > 0) {
283 *next_char
= **buf_p
;
286 connection
->input_pending
= 1;
288 connection
->input_pending
= 0;
290 #ifdef _DEBUG_GDB_IO_
291 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
297 struct gdb_connection
*gdb_con
= connection
->priv
;
298 gdb_con
->buf_p
= *buf_p
;
299 gdb_con
->buf_cnt
= *buf_cnt
;
300 retval
= gdb_get_char_inner(connection
, next_char
);
301 *buf_p
= gdb_con
->buf_p
;
302 *buf_cnt
= gdb_con
->buf_cnt
;
307 static int gdb_get_char(struct connection
*connection
, int *next_char
)
309 struct gdb_connection
*gdb_con
= connection
->priv
;
310 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
313 static int gdb_putback_char(struct connection
*connection
, int last_char
)
315 struct gdb_connection
*gdb_con
= connection
->priv
;
317 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
318 *(--gdb_con
->buf_p
) = last_char
;
321 LOG_ERROR("BUG: couldn't put character back");
326 /* The only way we can detect that the socket is closed is the first time
327 * we write to it, we will fail. Subsequent write operations will
328 * succeed. Shudder! */
329 static int gdb_write(struct connection
*connection
, void *data
, int len
)
331 struct gdb_connection
*gdb_con
= connection
->priv
;
333 return ERROR_SERVER_REMOTE_CLOSED
;
335 if (connection_write(connection
, data
, len
) == len
)
338 return ERROR_SERVER_REMOTE_CLOSED
;
341 static int gdb_put_packet_inner(struct connection
*connection
,
342 char *buffer
, int len
)
345 unsigned char my_checksum
= 0;
346 #ifdef _DEBUG_GDB_IO_
351 struct gdb_connection
*gdb_con
= connection
->priv
;
353 for (i
= 0; i
< len
; i
++)
354 my_checksum
+= buffer
[i
];
356 #ifdef _DEBUG_GDB_IO_
358 * At this point we should have nothing in the input queue from GDB,
359 * however sometimes '-' is sent even though we've already received
360 * an ACK (+) for everything we've sent off.
364 retval
= check_pending(connection
, 0, &gotdata
);
365 if (retval
!= ERROR_OK
)
369 retval
= gdb_get_char(connection
, &reply
);
370 if (retval
!= ERROR_OK
)
373 /* fix a problem with some IAR tools */
374 gdb_putback_char(connection
, reply
);
375 LOG_DEBUG("Unexpected start of new packet");
379 LOG_WARNING("Discard unexpected char %c", reply
);
384 #ifdef _DEBUG_GDB_IO_
385 debug_buffer
= strndup(buffer
, len
);
386 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
390 char local_buffer
[1024];
391 local_buffer
[0] = '$';
392 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
393 /* performance gain on smaller packets by only a single call to gdb_write() */
394 memcpy(local_buffer
+ 1, buffer
, len
++);
395 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
396 retval
= gdb_write(connection
, local_buffer
, len
);
397 if (retval
!= ERROR_OK
)
400 /* larger packets are transmitted directly from caller supplied buffer
401 * by several calls to gdb_write() to avoid dynamic allocation */
402 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
403 retval
= gdb_write(connection
, local_buffer
, 1);
404 if (retval
!= ERROR_OK
)
406 retval
= gdb_write(connection
, buffer
, len
);
407 if (retval
!= ERROR_OK
)
409 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
410 if (retval
!= ERROR_OK
)
414 if (gdb_con
->noack_mode
)
417 retval
= gdb_get_char(connection
, &reply
);
418 if (retval
!= ERROR_OK
)
423 else if (reply
== '-') {
424 /* Stop sending output packets for now */
425 log_remove_callback(gdb_log_callback
, connection
);
426 LOG_WARNING("negative reply, retrying");
427 } else if (reply
== 0x3) {
429 retval
= gdb_get_char(connection
, &reply
);
430 if (retval
!= ERROR_OK
)
434 else if (reply
== '-') {
435 /* Stop sending output packets for now */
436 log_remove_callback(gdb_log_callback
, connection
);
437 LOG_WARNING("negative reply, retrying");
438 } else if (reply
== '$') {
439 LOG_ERROR("GDB missing ack(1) - assumed good");
440 gdb_putback_char(connection
, reply
);
443 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
445 return ERROR_SERVER_REMOTE_CLOSED
;
447 } else if (reply
== '$') {
448 LOG_ERROR("GDB missing ack(2) - assumed good");
449 gdb_putback_char(connection
, reply
);
452 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
455 return ERROR_SERVER_REMOTE_CLOSED
;
459 return ERROR_SERVER_REMOTE_CLOSED
;
464 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
466 struct gdb_connection
*gdb_con
= connection
->priv
;
468 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
471 /* we sent some data, reset timer for keep alive messages */
477 static inline int fetch_packet(struct connection
*connection
,
478 int *checksum_ok
, int noack
, int *len
, char *buffer
)
480 unsigned char my_checksum
= 0;
483 int retval
= ERROR_OK
;
485 struct gdb_connection
*gdb_con
= connection
->priv
;
490 /* move this over into local variables to use registers and give the
491 * more freedom to optimize */
492 char *buf_p
= gdb_con
->buf_p
;
493 int buf_cnt
= gdb_con
->buf_cnt
;
496 /* The common case is that we have an entire packet with no escape chars.
497 * We need to leave at least 2 bytes in the buffer to have
498 * gdb_get_char() update various bits and bobs correctly.
500 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
501 /* The compiler will struggle a bit with constant propagation and
502 * aliasing, so we help it by showing that these values do not
503 * change inside the loop
507 int run
= buf_cnt
- 2;
513 if (character
== '#') {
514 /* Danger! character can be '#' when esc is
515 * used so we need an explicit boolean for done here. */
520 if (character
== '}') {
521 /* data transmitted in binary mode (X packet)
522 * uses 0x7d as escape character */
523 my_checksum
+= character
& 0xff;
526 my_checksum
+= character
& 0xff;
527 buffer
[count
++] = (character
^ 0x20) & 0xff;
529 my_checksum
+= character
& 0xff;
530 buffer
[count
++] = character
& 0xff;
539 LOG_ERROR("packet buffer too small");
540 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
544 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
545 if (retval
!= ERROR_OK
)
548 if (character
== '#')
551 if (character
== '}') {
552 /* data transmitted in binary mode (X packet)
553 * uses 0x7d as escape character */
554 my_checksum
+= character
& 0xff;
556 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
557 if (retval
!= ERROR_OK
)
560 my_checksum
+= character
& 0xff;
561 buffer
[count
++] = (character
^ 0x20) & 0xff;
563 my_checksum
+= character
& 0xff;
564 buffer
[count
++] = character
& 0xff;
568 gdb_con
->buf_p
= buf_p
;
569 gdb_con
->buf_cnt
= buf_cnt
;
571 if (retval
!= ERROR_OK
)
576 retval
= gdb_get_char(connection
, &character
);
577 if (retval
!= ERROR_OK
)
579 checksum
[0] = character
;
580 retval
= gdb_get_char(connection
, &character
);
581 if (retval
!= ERROR_OK
)
583 checksum
[1] = character
;
587 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
592 static int gdb_get_packet_inner(struct connection
*connection
,
593 char *buffer
, int *len
)
597 struct gdb_connection
*gdb_con
= connection
->priv
;
601 retval
= gdb_get_char(connection
, &character
);
602 if (retval
!= ERROR_OK
)
605 #ifdef _DEBUG_GDB_IO_
606 LOG_DEBUG("character: '%c'", character
);
613 /* gdb sends a dummy ack '+' at every remote connect - see
614 * remote_start_remote (remote.c)
615 * in case anyone tries to debug why they receive this
616 * warning every time */
617 LOG_WARNING("acknowledgment received, but no packet pending");
620 LOG_WARNING("negative acknowledgment, but no packet pending");
627 LOG_WARNING("ignoring character 0x%x", character
);
630 } while (character
!= '$');
633 /* explicit code expansion here to get faster inlined code in -O3 by not
634 * calculating checksum */
635 if (gdb_con
->noack_mode
) {
636 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
637 if (retval
!= ERROR_OK
)
640 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
641 if (retval
!= ERROR_OK
)
645 if (gdb_con
->noack_mode
) {
646 /* checksum is not checked in noack mode */
650 retval
= gdb_write(connection
, "+", 1);
651 if (retval
!= ERROR_OK
)
657 return ERROR_SERVER_REMOTE_CLOSED
;
662 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
664 struct gdb_connection
*gdb_con
= connection
->priv
;
666 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
671 static int gdb_output_con(struct connection
*connection
, const char *line
)
676 bin_size
= strlen(line
);
678 hex_buffer
= malloc(bin_size
* 2 + 2);
679 if (hex_buffer
== NULL
)
680 return ERROR_GDB_BUFFER_TOO_SMALL
;
683 int pkt_len
= hexify(hex_buffer
+ 1, line
, bin_size
, bin_size
* 2 + 1);
684 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
690 static int gdb_output(struct command_context
*context
, const char *line
)
692 /* this will be dumped to the log and also sent as an O packet if possible */
693 LOG_USER_N("%s", line
);
697 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
699 struct gdb_connection
*gdb_connection
= connection
->priv
;
701 char stop_reason
[20];
705 if (gdb_connection
->ctrl_c
) {
707 gdb_connection
->ctrl_c
= 0;
709 signal_var
= gdb_last_signal(target
);
711 stop_reason
[0] = '\0';
712 if (target
->debug_reason
== DBG_REASON_WATCHPOINT
) {
713 enum watchpoint_rw hit_wp_type
;
714 uint32_t hit_wp_address
;
716 if (watchpoint_hit(target
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
718 switch (hit_wp_type
) {
720 snprintf(stop_reason
, sizeof(stop_reason
),
721 "watch:%08x;", hit_wp_address
);
724 snprintf(stop_reason
, sizeof(stop_reason
),
725 "rwatch:%08x;", hit_wp_address
);
728 snprintf(stop_reason
, sizeof(stop_reason
),
729 "awatch:%08x;", hit_wp_address
);
737 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s",
738 signal_var
, stop_reason
);
740 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
741 gdb_connection
->frontend_state
= TARGET_HALTED
;
742 rtos_update_threads(target
);
745 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
747 struct gdb_connection
*gdb_connection
= connection
->priv
;
748 char fileio_command
[256];
750 bool program_exited
= false;
752 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
753 sprintf(fileio_command
, "F%s,%x/%x,%x,%x", target
->fileio_info
->identifier
,
754 target
->fileio_info
->param_1
,
755 target
->fileio_info
->param_2
,
756 target
->fileio_info
->param_3
,
757 target
->fileio_info
->param_4
);
758 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
759 sprintf(fileio_command
, "F%s,%x", target
->fileio_info
->identifier
,
760 target
->fileio_info
->param_1
);
761 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
762 sprintf(fileio_command
, "F%s,%x,%x,%x", target
->fileio_info
->identifier
,
763 target
->fileio_info
->param_1
,
764 target
->fileio_info
->param_2
,
765 target
->fileio_info
->param_3
);
766 else if (strcmp(target
->fileio_info
->identifier
, "write") == 0)
767 sprintf(fileio_command
, "F%s,%x,%x,%x", target
->fileio_info
->identifier
,
768 target
->fileio_info
->param_1
,
769 target
->fileio_info
->param_2
,
770 target
->fileio_info
->param_3
);
771 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
772 sprintf(fileio_command
, "F%s,%x,%x,%x", target
->fileio_info
->identifier
,
773 target
->fileio_info
->param_1
,
774 target
->fileio_info
->param_2
,
775 target
->fileio_info
->param_3
);
776 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
777 sprintf(fileio_command
, "F%s,%x/%x,%x/%x", target
->fileio_info
->identifier
,
778 target
->fileio_info
->param_1
,
779 target
->fileio_info
->param_2
,
780 target
->fileio_info
->param_3
,
781 target
->fileio_info
->param_4
);
782 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
783 sprintf(fileio_command
, "F%s,%x/%x", target
->fileio_info
->identifier
,
784 target
->fileio_info
->param_1
,
785 target
->fileio_info
->param_2
);
786 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
787 sprintf(fileio_command
, "F%s,%x/%x,%x", target
->fileio_info
->identifier
,
788 target
->fileio_info
->param_1
,
789 target
->fileio_info
->param_2
,
790 target
->fileio_info
->param_3
);
791 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
792 sprintf(fileio_command
, "F%s,%x,%x", target
->fileio_info
->identifier
,
793 target
->fileio_info
->param_1
,
794 target
->fileio_info
->param_2
);
795 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
796 sprintf(fileio_command
, "F%s,%x,%x", target
->fileio_info
->identifier
,
797 target
->fileio_info
->param_1
,
798 target
->fileio_info
->param_2
);
799 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
800 sprintf(fileio_command
, "F%s,%x", target
->fileio_info
->identifier
,
801 target
->fileio_info
->param_1
);
802 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
803 sprintf(fileio_command
, "F%s,%x/%x", target
->fileio_info
->identifier
,
804 target
->fileio_info
->param_1
,
805 target
->fileio_info
->param_2
);
806 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
807 /* If target hits exit syscall, report to GDB the program is terminated.
808 * In addition, let target run its own exit syscall handler. */
809 program_exited
= true;
810 sprintf(fileio_command
, "W%02x", target
->fileio_info
->param_1
);
812 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
814 /* encounter unknown syscall, continue */
815 gdb_connection
->frontend_state
= TARGET_RUNNING
;
816 target_resume(target
, 1, 0x0, 0, 0);
820 command_len
= strlen(fileio_command
);
821 gdb_put_packet(connection
, fileio_command
, command_len
);
823 if (program_exited
) {
824 /* Use target_resume() to let target run its own exit syscall handler. */
825 gdb_connection
->frontend_state
= TARGET_RUNNING
;
826 target_resume(target
, 1, 0x0, 0, 0);
828 gdb_connection
->frontend_state
= TARGET_HALTED
;
829 rtos_update_threads(target
);
833 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
835 struct gdb_connection
*gdb_connection
= connection
->priv
;
837 /* In the GDB protocol when we are stepping or continuing execution,
838 * we have a lingering reply. Upon receiving a halted event
839 * when we have that lingering packet, we reply to the original
840 * step or continue packet.
842 * Executing monitor commands can bring the target in and
843 * out of the running state so we'll see lots of TARGET_EVENT_XXX
844 * that are to be ignored.
846 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
847 /* stop forwarding log packets! */
848 log_remove_callback(gdb_log_callback
, connection
);
850 /* check fileio first */
851 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
852 gdb_fileio_reply(target
, connection
);
854 gdb_signal_reply(target
, connection
);
858 static int gdb_target_callback_event_handler(struct target
*target
,
859 enum target_event event
, void *priv
)
862 struct connection
*connection
= priv
;
865 case TARGET_EVENT_GDB_HALT
:
866 gdb_frontend_halted(target
, connection
);
868 case TARGET_EVENT_HALTED
:
869 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
871 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
872 retval
= jtag_execute_queue();
873 if (retval
!= ERROR_OK
)
883 static int gdb_new_connection(struct connection
*connection
)
885 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
886 struct gdb_service
*gdb_service
= connection
->service
->priv
;
890 connection
->priv
= gdb_connection
;
892 /* initialize gdb connection information */
893 gdb_connection
->buf_p
= gdb_connection
->buffer
;
894 gdb_connection
->buf_cnt
= 0;
895 gdb_connection
->ctrl_c
= 0;
896 gdb_connection
->frontend_state
= TARGET_HALTED
;
897 gdb_connection
->vflash_image
= NULL
;
898 gdb_connection
->closed
= 0;
899 gdb_connection
->busy
= 0;
900 gdb_connection
->noack_mode
= 0;
901 gdb_connection
->sync
= true;
902 gdb_connection
->mem_write_error
= false;
903 gdb_connection
->attached
= true;
905 /* send ACK to GDB for debug request */
906 gdb_write(connection
, "+", 1);
908 /* output goes through gdb connection */
909 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
911 /* we must remove all breakpoints registered to the target as a previous
912 * GDB session could leave dangling breakpoints if e.g. communication
915 breakpoint_clear_target(gdb_service
->target
);
916 watchpoint_clear_target(gdb_service
->target
);
918 /* clean previous rtos session if supported*/
919 if ((gdb_service
->target
->rtos
) && (gdb_service
->target
->rtos
->type
->clean
))
920 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
922 /* remove the initial ACK from the incoming buffer */
923 retval
= gdb_get_char(connection
, &initial_ack
);
924 if (retval
!= ERROR_OK
)
927 /* FIX!!!??? would we actually ever receive a + here???
930 if (initial_ack
!= '+')
931 gdb_putback_char(connection
, initial_ack
);
932 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
934 if (gdb_use_memory_map
) {
935 /* Connect must fail if the memory map can't be set up correctly.
937 * This will cause an auto_probe to be invoked, which is either
938 * a no-op or it will fail when the target isn't ready(e.g. not halted).
941 for (i
= 0; i
< flash_get_bank_count(); i
++) {
942 struct flash_bank
*p
;
943 retval
= get_flash_bank_by_num(i
, &p
);
944 if (retval
!= ERROR_OK
) {
945 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
946 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
952 gdb_actual_connections
++;
953 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
954 gdb_actual_connections
,
955 target_name(gdb_service
->target
),
956 target_state_name(gdb_service
->target
));
958 /* DANGER! If we fail subsequently, we must remove this handler,
959 * otherwise we occasionally see crashes as the timer can invoke the
962 * register callback to be informed about target events */
963 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
968 static int gdb_connection_closed(struct connection
*connection
)
970 struct gdb_service
*gdb_service
= connection
->service
->priv
;
971 struct gdb_connection
*gdb_connection
= connection
->priv
;
973 /* we're done forwarding messages. Tear down callback before
974 * cleaning up connection.
976 log_remove_callback(gdb_log_callback
, connection
);
978 gdb_actual_connections
--;
979 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
980 target_name(gdb_service
->target
),
981 target_state_name(gdb_service
->target
),
982 gdb_actual_connections
);
984 /* see if an image built with vFlash commands is left */
985 if (gdb_connection
->vflash_image
) {
986 image_close(gdb_connection
->vflash_image
);
987 free(gdb_connection
->vflash_image
);
988 gdb_connection
->vflash_image
= NULL
;
991 /* if this connection registered a debug-message receiver delete it */
992 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
994 if (connection
->priv
) {
995 free(connection
->priv
);
996 connection
->priv
= NULL
;
998 LOG_ERROR("BUG: connection->priv == NULL");
1000 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1002 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
1004 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
1009 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1012 snprintf(err
, 4, "E%2.2X", the_error
);
1013 gdb_put_packet(connection
, err
, 3);
1016 static int gdb_last_signal_packet(struct connection
*connection
,
1017 char *packet
, int packet_size
)
1019 struct target
*target
= get_target_from_connection(connection
);
1020 struct gdb_connection
*gdb_con
= connection
->priv
;
1024 if (!gdb_con
->attached
) {
1025 /* if we are here we have received a kill packet
1026 * reply W stop reply otherwise gdb gets very unhappy */
1027 gdb_put_packet(connection
, "W00", 3);
1031 signal_var
= gdb_last_signal(target
);
1033 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1034 gdb_put_packet(connection
, sig_reply
, 3);
1039 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1041 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1044 return len
- 1 - pos
;
1047 /* Convert register to string of bytes. NB! The # of bits in the
1048 * register might be non-divisible by 8(a byte), in which
1049 * case an entire byte is shown.
1051 * NB! the format on the wire is the target endianness
1053 * The format of reg->value is little endian
1056 static void gdb_str_to_target(struct target
*target
,
1057 char *tstr
, struct reg
*reg
)
1064 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1066 for (i
= 0; i
< buf_len
; i
++) {
1067 int j
= gdb_reg_pos(target
, i
, buf_len
);
1068 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1072 /* copy over in register buffer */
1073 static void gdb_target_to_reg(struct target
*target
,
1074 char *tstr
, int str_len
, uint8_t *bin
)
1077 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1082 for (i
= 0; i
< str_len
; i
+= 2) {
1084 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1085 LOG_ERROR("BUG: unable to convert register value");
1089 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1094 static int gdb_get_registers_packet(struct connection
*connection
,
1095 char *packet
, int packet_size
)
1097 struct target
*target
= get_target_from_connection(connection
);
1098 struct reg
**reg_list
;
1101 int reg_packet_size
= 0;
1106 #ifdef _DEBUG_GDB_IO_
1110 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1113 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1115 if (retval
!= ERROR_OK
)
1116 return gdb_error(connection
, retval
);
1118 for (i
= 0; i
< reg_list_size
; i
++)
1119 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1121 assert(reg_packet_size
> 0);
1123 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1124 reg_packet_p
= reg_packet
;
1126 for (i
= 0; i
< reg_list_size
; i
++) {
1127 if (!reg_list
[i
]->valid
)
1128 reg_list
[i
]->type
->get(reg_list
[i
]);
1129 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1130 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1133 #ifdef _DEBUG_GDB_IO_
1135 char *reg_packet_p_debug
;
1136 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1137 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1138 free(reg_packet_p_debug
);
1142 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1150 static int gdb_set_registers_packet(struct connection
*connection
,
1151 char *packet
, int packet_size
)
1153 struct target
*target
= get_target_from_connection(connection
);
1155 struct reg
**reg_list
;
1160 #ifdef _DEBUG_GDB_IO_
1164 /* skip command character */
1168 if (packet_size
% 2) {
1169 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1170 return ERROR_SERVER_REMOTE_CLOSED
;
1173 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1175 if (retval
!= ERROR_OK
)
1176 return gdb_error(connection
, retval
);
1179 for (i
= 0; i
< reg_list_size
; i
++) {
1181 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1183 if (packet_p
+ chars
> packet
+ packet_size
)
1184 LOG_ERROR("BUG: register packet is too small for registers");
1186 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1187 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1189 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1191 /* advance packet pointer */
1197 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1200 gdb_put_packet(connection
, "OK", 2);
1205 static int gdb_get_register_packet(struct connection
*connection
,
1206 char *packet
, int packet_size
)
1208 struct target
*target
= get_target_from_connection(connection
);
1210 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1211 struct reg
**reg_list
;
1215 #ifdef _DEBUG_GDB_IO_
1219 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1221 if (retval
!= ERROR_OK
)
1222 return gdb_error(connection
, retval
);
1224 if (reg_list_size
<= reg_num
) {
1225 LOG_ERROR("gdb requested a non-existing register");
1226 return ERROR_SERVER_REMOTE_CLOSED
;
1229 if (!reg_list
[reg_num
]->valid
)
1230 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1232 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1); /* plus one for string termination null */
1234 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1236 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1244 static int gdb_set_register_packet(struct connection
*connection
,
1245 char *packet
, int packet_size
)
1247 struct target
*target
= get_target_from_connection(connection
);
1250 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1251 struct reg
**reg_list
;
1257 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1259 if (retval
!= ERROR_OK
)
1260 return gdb_error(connection
, retval
);
1262 if (reg_list_size
<= reg_num
) {
1263 LOG_ERROR("gdb requested a non-existing register");
1264 return ERROR_SERVER_REMOTE_CLOSED
;
1267 if (*separator
!= '=') {
1268 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1269 return ERROR_SERVER_REMOTE_CLOSED
;
1272 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1273 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1274 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1276 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1277 LOG_ERROR("gdb sent a packet with wrong register size");
1279 return ERROR_SERVER_REMOTE_CLOSED
;
1282 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1284 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1286 gdb_put_packet(connection
, "OK", 2);
1294 /* No attempt is made to translate the "retval" to
1295 * GDB speak. This has to be done at the calling
1296 * site as no mapping really exists.
1298 static int gdb_error(struct connection
*connection
, int retval
)
1300 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1301 gdb_send_error(connection
, EFAULT
);
1305 /* We don't have to worry about the default 2 second timeout for GDB packets,
1306 * because GDB breaks up large memory reads into smaller reads.
1308 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1310 static int gdb_read_memory_packet(struct connection
*connection
,
1311 char *packet
, int packet_size
)
1313 struct target
*target
= get_target_from_connection(connection
);
1321 int retval
= ERROR_OK
;
1323 /* skip command character */
1326 addr
= strtoul(packet
, &separator
, 16);
1328 if (*separator
!= ',') {
1329 LOG_ERROR("incomplete read memory packet received, dropping connection");
1330 return ERROR_SERVER_REMOTE_CLOSED
;
1333 len
= strtoul(separator
+ 1, NULL
, 16);
1335 buffer
= malloc(len
);
1337 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1339 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1341 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1342 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1343 * At some point this might be fixed in GDB, in which case this code can be removed.
1345 * OpenOCD developers are acutely aware of this problem, but there is nothing
1346 * gained by involving the user in this problem that hopefully will get resolved
1349 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1350 * cmd = view%20audit-trail&database = gdb&pr = 2395
1352 * For now, the default is to fix up things to make current GDB versions work.
1353 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1355 memset(buffer
, 0, len
);
1359 if (retval
== ERROR_OK
) {
1360 hex_buffer
= malloc(len
* 2 + 1);
1362 int pkt_len
= hexify(hex_buffer
, (char *)buffer
, len
, len
* 2 + 1);
1364 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1368 retval
= gdb_error(connection
, retval
);
1375 static int gdb_write_memory_packet(struct connection
*connection
,
1376 char *packet
, int packet_size
)
1378 struct target
*target
= get_target_from_connection(connection
);
1386 /* skip command character */
1389 addr
= strtoul(packet
, &separator
, 16);
1391 if (*separator
!= ',') {
1392 LOG_ERROR("incomplete write memory packet received, dropping connection");
1393 return ERROR_SERVER_REMOTE_CLOSED
;
1396 len
= strtoul(separator
+ 1, &separator
, 16);
1398 if (*(separator
++) != ':') {
1399 LOG_ERROR("incomplete write memory packet received, dropping connection");
1400 return ERROR_SERVER_REMOTE_CLOSED
;
1403 buffer
= malloc(len
);
1405 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1407 if (unhexify((char *)buffer
, separator
, len
) != (int)len
)
1408 LOG_ERROR("unable to decode memory packet");
1410 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1412 if (retval
== ERROR_OK
)
1413 gdb_put_packet(connection
, "OK", 2);
1415 retval
= gdb_error(connection
, retval
);
1422 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1423 char *packet
, int packet_size
)
1425 struct target
*target
= get_target_from_connection(connection
);
1430 int retval
= ERROR_OK
;
1432 /* skip command character */
1435 addr
= strtoul(packet
, &separator
, 16);
1437 if (*separator
!= ',') {
1438 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1439 return ERROR_SERVER_REMOTE_CLOSED
;
1442 len
= strtoul(separator
+ 1, &separator
, 16);
1444 if (*(separator
++) != ':') {
1445 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1446 return ERROR_SERVER_REMOTE_CLOSED
;
1449 struct gdb_connection
*gdb_connection
= connection
->priv
;
1451 if (gdb_connection
->mem_write_error
) {
1452 retval
= ERROR_FAIL
;
1453 /* now that we have reported the memory write error, we can clear the condition */
1454 gdb_connection
->mem_write_error
= false;
1457 /* By replying the packet *immediately* GDB will send us a new packet
1458 * while we write the last one to the target.
1460 if (retval
== ERROR_OK
)
1461 gdb_put_packet(connection
, "OK", 2);
1463 retval
= gdb_error(connection
, retval
);
1464 if (retval
!= ERROR_OK
)
1469 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1471 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1472 if (retval
!= ERROR_OK
)
1473 gdb_connection
->mem_write_error
= true;
1479 static int gdb_step_continue_packet(struct connection
*connection
,
1480 char *packet
, int packet_size
)
1482 struct target
*target
= get_target_from_connection(connection
);
1484 uint32_t address
= 0x0;
1485 int retval
= ERROR_OK
;
1489 if (packet_size
> 1) {
1490 packet
[packet_size
] = 0;
1491 address
= strtoul(packet
+ 1, NULL
, 16);
1495 gdb_running_type
= packet
[0];
1496 if (packet
[0] == 'c') {
1497 LOG_DEBUG("continue");
1498 /* resume at current address, don't handle breakpoints, not debugging */
1499 retval
= target_resume(target
, current
, address
, 0, 0);
1500 } else if (packet
[0] == 's') {
1502 /* step at current or address, don't handle breakpoints */
1503 retval
= target_step(target
, current
, address
, 0);
1508 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1509 char *packet
, int packet_size
)
1511 struct target
*target
= get_target_from_connection(connection
);
1513 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1514 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1522 type
= strtoul(packet
+ 1, &separator
, 16);
1524 if (type
== 0) /* memory breakpoint */
1525 bp_type
= BKPT_SOFT
;
1526 else if (type
== 1) /* hardware breakpoint */
1527 bp_type
= BKPT_HARD
;
1528 else if (type
== 2) /* write watchpoint */
1529 wp_type
= WPT_WRITE
;
1530 else if (type
== 3) /* read watchpoint */
1532 else if (type
== 4) /* access watchpoint */
1533 wp_type
= WPT_ACCESS
;
1535 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1536 return ERROR_SERVER_REMOTE_CLOSED
;
1539 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1540 bp_type
= gdb_breakpoint_override_type
;
1542 if (*separator
!= ',') {
1543 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1544 return ERROR_SERVER_REMOTE_CLOSED
;
1547 address
= strtoul(separator
+ 1, &separator
, 16);
1549 if (*separator
!= ',') {
1550 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1551 return ERROR_SERVER_REMOTE_CLOSED
;
1554 size
= strtoul(separator
+ 1, &separator
, 16);
1559 if (packet
[0] == 'Z') {
1560 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1561 if (retval
!= ERROR_OK
) {
1562 retval
= gdb_error(connection
, retval
);
1563 if (retval
!= ERROR_OK
)
1566 gdb_put_packet(connection
, "OK", 2);
1568 breakpoint_remove(target
, address
);
1569 gdb_put_packet(connection
, "OK", 2);
1576 if (packet
[0] == 'Z') {
1577 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1578 if (retval
!= ERROR_OK
) {
1579 retval
= gdb_error(connection
, retval
);
1580 if (retval
!= ERROR_OK
)
1583 gdb_put_packet(connection
, "OK", 2);
1585 watchpoint_remove(target
, address
);
1586 gdb_put_packet(connection
, "OK", 2);
1597 /* print out a string and allocate more space as needed,
1598 * mainly used for XML at this point
1600 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1601 const char *fmt
, ...)
1603 if (*retval
!= ERROR_OK
)
1608 if ((*xml
== NULL
) || (!first
)) {
1609 /* start by 0 to exercise all the code paths.
1610 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1612 *size
= *size
* 2 + 2;
1614 *xml
= realloc(*xml
, *size
);
1618 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1626 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1628 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1632 /* there was just enough or not enough space, allocate more. */
1637 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1641 /* Extract and NUL-terminate the annex. */
1643 while (*buf
&& *buf
!= ':')
1649 /* After the read marker and annex, qXfer looks like a
1650 * traditional 'm' packet. */
1652 *ofs
= strtoul(buf
, &separator
, 16);
1654 if (*separator
!= ',')
1657 *len
= strtoul(separator
+ 1, NULL
, 16);
1662 static int compare_bank(const void *a
, const void *b
)
1664 struct flash_bank
*b1
, *b2
;
1665 b1
= *((struct flash_bank
**)a
);
1666 b2
= *((struct flash_bank
**)b
);
1668 if (b1
->base
== b2
->base
)
1670 else if (b1
->base
> b2
->base
)
1676 static int gdb_memory_map(struct connection
*connection
,
1677 char *packet
, int packet_size
)
1679 /* We get away with only specifying flash here. Regions that are not
1680 * specified are treated as if we provided no memory map(if not we
1681 * could detect the holes and mark them as RAM).
1682 * Normally we only execute this code once, but no big deal if we
1683 * have to regenerate it a couple of times.
1686 struct target
*target
= get_target_from_connection(connection
);
1687 struct flash_bank
*p
;
1691 int retval
= ERROR_OK
;
1692 struct flash_bank
**banks
;
1696 uint32_t ram_start
= 0;
1698 int target_flash_banks
= 0;
1700 /* skip command character */
1703 offset
= strtoul(packet
, &separator
, 16);
1704 length
= strtoul(separator
+ 1, &separator
, 16);
1706 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1708 /* Sort banks in ascending order. We need to report non-flash
1709 * memory as ram (or rather read/write) by default for GDB, since
1710 * it has no concept of non-cacheable read/write memory (i/o etc).
1712 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1713 * Current versions of GDB assume unlisted addresses are RAM...
1715 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1717 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1718 retval
= get_flash_bank_by_num(i
, &p
);
1719 if (retval
!= ERROR_OK
) {
1721 gdb_error(connection
, retval
);
1724 if (p
->target
== target
)
1725 banks
[target_flash_banks
++] = p
;
1728 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1731 for (i
= 0; i
< target_flash_banks
; i
++) {
1733 unsigned sector_size
= 0;
1739 if (ram_start
< p
->base
)
1740 xml_printf(&retval
, &xml
, &pos
, &size
,
1741 "<memory type=\"ram\" start=\"0x%x\" "
1742 "length=\"0x%x\"/>\n",
1743 ram_start
, p
->base
- ram_start
);
1745 /* Report adjacent groups of same-size sectors. So for
1746 * example top boot CFI flash will list an initial region
1747 * with several large sectors (maybe 128KB) and several
1748 * smaller ones at the end (maybe 32KB). STR7 will have
1749 * regions with 8KB, 32KB, and 64KB sectors; etc.
1751 for (j
= 0; j
< p
->num_sectors
; j
++) {
1754 /* Maybe start a new group of sectors. */
1755 if (sector_size
== 0) {
1756 start
= p
->base
+ p
->sectors
[j
].offset
;
1757 xml_printf(&retval
, &xml
, &pos
, &size
,
1758 "<memory type=\"flash\" "
1761 sector_size
= p
->sectors
[j
].size
;
1764 /* Does this finish a group of sectors?
1765 * If not, continue an already-started group.
1767 if (j
== p
->num_sectors
- 1)
1768 group_len
= (p
->base
+ p
->size
) - start
;
1769 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1770 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1775 xml_printf(&retval
, &xml
, &pos
, &size
,
1776 "length=\"0x%x\">\n"
1777 "<property name=\"blocksize\">"
1785 ram_start
= p
->base
+ p
->size
;
1789 xml_printf(&retval
, &xml
, &pos
, &size
,
1790 "<memory type=\"ram\" start=\"0x%x\" "
1791 "length=\"0x%x\"/>\n",
1792 ram_start
, 0-ram_start
);
1793 /* ELSE a flash chip could be at the very end of the 32 bit address
1794 * space, in which case ram_start will be precisely 0
1800 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1802 if (retval
!= ERROR_OK
) {
1803 gdb_error(connection
, retval
);
1807 if (offset
+ length
> pos
)
1808 length
= pos
- offset
;
1810 char *t
= malloc(length
+ 1);
1812 memcpy(t
+ 1, xml
+ offset
, length
);
1813 gdb_put_packet(connection
, t
, length
+ 1);
1820 static const char *gdb_get_reg_type_name(enum reg_type type
)
1825 case REG_TYPE_INT16
:
1827 case REG_TYPE_INT32
:
1829 case REG_TYPE_INT64
:
1831 case REG_TYPE_INT128
:
1833 case REG_TYPE_UINT8
:
1835 case REG_TYPE_UINT16
:
1837 case REG_TYPE_UINT32
:
1839 case REG_TYPE_UINT64
:
1841 case REG_TYPE_UINT128
:
1843 case REG_TYPE_CODE_PTR
:
1845 case REG_TYPE_DATA_PTR
:
1847 case REG_TYPE_IEEE_SINGLE
:
1848 return "ieee_single";
1849 case REG_TYPE_IEEE_DOUBLE
:
1850 return "ieee_double";
1851 case REG_TYPE_ARCH_DEFINED
:
1852 return "int"; /* return arbitrary string to avoid compile warning. */
1855 return "int"; /* "int" as default value */
1858 static int gdb_generate_reg_type_description(struct target
*target
,
1859 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
)
1861 int retval
= ERROR_OK
;
1863 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
1864 /* <vector id="id" type="type" count="count"/> */
1865 xml_printf(&retval
, tdesc
, pos
, size
,
1866 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
1867 type
->id
, type
->reg_type_vector
->type
->id
,
1868 type
->reg_type_vector
->count
);
1870 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
1872 * <field name="name" type="type"/> ...
1874 xml_printf(&retval
, tdesc
, pos
, size
,
1875 "<union id=\"%s\">\n",
1878 struct reg_data_type_union_field
*field
;
1879 field
= type
->reg_type_union
->fields
;
1880 while (field
!= NULL
) {
1881 xml_printf(&retval
, tdesc
, pos
, size
,
1882 "<field name=\"%s\" type=\"%s\"/>\n",
1883 field
->name
, field
->type
->id
);
1885 field
= field
->next
;
1888 xml_printf(&retval
, tdesc
, pos
, size
,
1891 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
1892 struct reg_data_type_struct_field
*field
;
1893 field
= type
->reg_type_struct
->fields
;
1895 if (field
->use_bitfields
) {
1896 /* <struct id="id" size="size">
1897 * <field name="name" start="start" end="end"/> ...
1899 xml_printf(&retval
, tdesc
, pos
, size
,
1900 "<struct id=\"%s\" size=\"%d\">\n",
1901 type
->id
, type
->reg_type_struct
->size
);
1902 while (field
!= NULL
) {
1903 xml_printf(&retval
, tdesc
, pos
, size
,
1904 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1905 field
->name
, field
->bitfield
->start
,
1906 field
->bitfield
->end
);
1908 field
= field
->next
;
1912 * <field name="name" type="type"/> ...
1914 xml_printf(&retval
, tdesc
, pos
, size
,
1915 "<struct id=\"%s\">\n",
1917 while (field
!= NULL
) {
1918 xml_printf(&retval
, tdesc
, pos
, size
,
1919 "<field name=\"%s\" type=\"%s\"/>\n",
1920 field
->name
, field
->type
->id
);
1922 field
= field
->next
;
1926 xml_printf(&retval
, tdesc
, pos
, size
,
1929 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
1930 /* <flags id="id" size="size">
1931 * <field name="name" start="start" end="end"/> ...
1933 xml_printf(&retval
, tdesc
, pos
, size
,
1934 "<flags id=\"%s\" size=\"%d\">\n",
1935 type
->id
, type
->reg_type_flags
->size
);
1937 struct reg_data_type_flags_field
*field
;
1938 field
= type
->reg_type_flags
->fields
;
1939 while (field
!= NULL
) {
1940 xml_printf(&retval
, tdesc
, pos
, size
,
1941 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1942 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
);
1944 field
= field
->next
;
1947 xml_printf(&retval
, tdesc
, pos
, size
,
1955 /* Get a list of available target registers features. feature_list must
1956 * be freed by caller.
1958 int get_reg_features_list(struct target
*target
, char **feature_list
[], int *feature_list_size
,
1959 struct reg
**reg_list
, int reg_list_size
)
1963 /* Start with only one element */
1964 *feature_list
= calloc(1, sizeof(char *));
1966 for (int i
= 0; i
< reg_list_size
; i
++) {
1967 if (reg_list
[i
]->exist
== false)
1970 if ((reg_list
[i
]->feature
->name
!= NULL
)
1971 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
1972 /* We found a feature, check if the feature is already in the
1973 * table. If not, allocate a new entry for the table and
1974 * put the new feature in it.
1976 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
1977 if (!((*feature_list
)[j
])) {
1978 (*feature_list
)[tbl_sz
++] = strdup(reg_list
[i
]->feature
->name
);
1979 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
1980 (*feature_list
)[tbl_sz
] = NULL
;
1983 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
1990 if (feature_list_size
)
1991 *feature_list_size
= tbl_sz
;
1996 static int gdb_generate_target_description(struct target
*target
, char **tdesc
)
1998 int retval
= ERROR_OK
;
1999 struct reg
**reg_list
;
2004 xml_printf(&retval
, tdesc
, &pos
, &size
,
2005 "<?xml version=\"1.0\"?>\n"
2006 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2007 "<target version=\"1.0\">\n");
2009 retval
= target_get_gdb_reg_list(target
, ®_list
,
2010 ®_list_size
, REG_CLASS_ALL
);
2012 if (retval
!= ERROR_OK
) {
2013 LOG_ERROR("get register list failed");
2017 if (reg_list_size
<= 0)
2020 char **features
= NULL
;
2021 /* Get a list of available target registers features */
2022 retval
= get_reg_features_list(target
, &features
, NULL
, reg_list
, reg_list_size
);
2023 if (retval
!= ERROR_OK
) {
2024 LOG_ERROR("Can't get the registers feature list");
2028 /* If we found some features associated with registers, create sections */
2029 int current_feature
= 0;
2031 /* generate target description according to register list */
2032 if (features
!= NULL
) {
2033 while (features
[current_feature
]) {
2035 xml_printf(&retval
, tdesc
, &pos
, &size
,
2036 "<feature name=\"%s\">\n",
2037 features
[current_feature
]);
2040 for (i
= 0; i
< reg_list_size
; i
++) {
2042 if (reg_list
[i
]->exist
== false)
2045 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2048 const char *type_str
;
2049 if (reg_list
[i
]->reg_data_type
!= NULL
) {
2050 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2051 /* generate <type... first, if there are architecture-defined types. */
2052 gdb_generate_reg_type_description(target
, tdesc
, &pos
, &size
,
2053 reg_list
[i
]->reg_data_type
);
2055 type_str
= reg_list
[i
]->reg_data_type
->id
;
2057 /* predefined type */
2058 type_str
= gdb_get_reg_type_name(
2059 reg_list
[i
]->reg_data_type
->type
);
2062 /* Default type is "int" */
2066 xml_printf(&retval
, tdesc
, &pos
, &size
,
2067 "<reg name=\"%s\"", reg_list
[i
]->name
);
2068 xml_printf(&retval
, tdesc
, &pos
, &size
,
2069 " bitsize=\"%d\"", reg_list
[i
]->size
);
2070 xml_printf(&retval
, tdesc
, &pos
, &size
,
2071 " regnum=\"%d\"", reg_list
[i
]->number
);
2072 if (reg_list
[i
]->caller_save
)
2073 xml_printf(&retval
, tdesc
, &pos
, &size
,
2074 " save-restore=\"yes\"");
2076 xml_printf(&retval
, tdesc
, &pos
, &size
,
2077 " save-restore=\"no\"");
2079 xml_printf(&retval
, tdesc
, &pos
, &size
,
2080 " type=\"%s\"", type_str
);
2082 if (reg_list
[i
]->group
!= NULL
)
2083 xml_printf(&retval
, tdesc
, &pos
, &size
,
2084 " group=\"%s\"", reg_list
[i
]->group
);
2086 xml_printf(&retval
, tdesc
, &pos
, &size
,
2090 xml_printf(&retval
, tdesc
, &pos
, &size
,
2097 xml_printf(&retval
, tdesc
, &pos
, &size
,
2100 if (reg_list
!= NULL
)
2103 if (features
!= NULL
)
2109 static int gdb_get_target_description_chunk(struct target
*target
, char **chunk
,
2110 int32_t offset
, uint32_t length
)
2113 static uint32_t tdesc_length
;
2115 if (tdesc
== NULL
) {
2116 gdb_generate_target_description(target
, &tdesc
);
2117 tdesc_length
= strlen(tdesc
);
2122 if (length
< (tdesc_length
- offset
))
2123 transfer_type
= 'm';
2125 transfer_type
= 'l';
2127 *chunk
= malloc(length
+ 2);
2128 (*chunk
)[0] = transfer_type
;
2129 if (transfer_type
== 'm') {
2130 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2131 (*chunk
)[1 + length
] = '\0';
2133 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2134 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2136 /* After gdb-server sends out last chunk, invalidate tdesc. */
2145 static int gdb_query_packet(struct connection
*connection
,
2146 char *packet
, int packet_size
)
2148 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2149 struct gdb_connection
*gdb_connection
= connection
->priv
;
2150 struct target
*target
= get_target_from_connection(connection
);
2152 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2153 if (packet_size
> 6) {
2155 cmd
= malloc((packet_size
- 6) / 2 + 1);
2156 int len
= unhexify(cmd
, packet
+ 6, (packet_size
- 6) / 2);
2159 /* We want to print all debug output to GDB connection */
2160 log_add_callback(gdb_log_callback
, connection
);
2161 target_call_timer_callbacks_now();
2162 /* some commands need to know the GDB connection, make note of current
2163 * GDB connection. */
2164 current_gdb_connection
= gdb_connection
;
2165 command_run_line(cmd_ctx
, cmd
);
2166 current_gdb_connection
= NULL
;
2167 target_call_timer_callbacks_now();
2168 log_remove_callback(gdb_log_callback
, connection
);
2171 gdb_put_packet(connection
, "OK", 2);
2173 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2174 if (packet_size
> 5) {
2182 /* skip command character */
2185 addr
= strtoul(packet
, &separator
, 16);
2187 if (*separator
!= ',') {
2188 LOG_ERROR("incomplete read memory packet received, dropping connection");
2189 return ERROR_SERVER_REMOTE_CLOSED
;
2192 len
= strtoul(separator
+ 1, NULL
, 16);
2194 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2196 if (retval
== ERROR_OK
) {
2197 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2198 gdb_put_packet(connection
, gdb_reply
, 9);
2200 retval
= gdb_error(connection
, retval
);
2201 if (retval
!= ERROR_OK
)
2207 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2208 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2209 * disable qXfer:features:read for the moment */
2210 int retval
= ERROR_OK
;
2211 char *buffer
= NULL
;
2219 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;QStartNoAckMode+",
2220 (GDB_BUFFER_SIZE
- 1),
2221 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2222 (gdb_use_target_description
== 1) ? '+' : '-');
2224 if (retval
!= ERROR_OK
) {
2225 gdb_send_error(connection
, 01);
2229 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2233 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2234 && (flash_get_bank_count() > 0))
2235 return gdb_memory_map(connection
, packet
, packet_size
);
2236 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2238 int retval
= ERROR_OK
;
2241 unsigned int length
;
2244 /* skip command character */
2247 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0) {
2248 gdb_send_error(connection
, 01);
2252 /* Target should prepare correct target description for annex.
2253 * The first character of returned xml is 'm' or 'l'. 'm' for
2254 * there are *more* chunks to transfer. 'l' for it is the *last*
2255 * chunk of target description.
2257 retval
= gdb_get_target_description_chunk(target
, &xml
, offset
, length
);
2258 if (retval
!= ERROR_OK
) {
2259 gdb_error(connection
, retval
);
2263 gdb_put_packet(connection
, xml
, strlen(xml
));
2267 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2268 gdb_connection
->noack_mode
= 1;
2269 gdb_put_packet(connection
, "OK", 2);
2273 gdb_put_packet(connection
, "", 0);
2277 static int gdb_v_packet(struct connection
*connection
,
2278 char *packet
, int packet_size
)
2280 struct gdb_connection
*gdb_connection
= connection
->priv
;
2281 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2284 /* if flash programming disabled - send a empty reply */
2286 if (gdb_flash_program
== 0) {
2287 gdb_put_packet(connection
, "", 0);
2291 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
2293 unsigned long length
;
2295 char *parse
= packet
+ 12;
2296 if (*parse
== '\0') {
2297 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2298 return ERROR_SERVER_REMOTE_CLOSED
;
2301 addr
= strtoul(parse
, &parse
, 16);
2303 if (*(parse
++) != ',' || *parse
== '\0') {
2304 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2305 return ERROR_SERVER_REMOTE_CLOSED
;
2308 length
= strtoul(parse
, &parse
, 16);
2310 if (*parse
!= '\0') {
2311 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2312 return ERROR_SERVER_REMOTE_CLOSED
;
2315 /* assume all sectors need erasing - stops any problems
2316 * when flash_write is called multiple times */
2319 /* perform any target specific operations before the erase */
2320 target_call_event_callbacks(gdb_service
->target
,
2321 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2323 /* vFlashErase:addr,length messages require region start and
2324 * end to be "block" aligned ... if padding is ever needed,
2325 * GDB will have become dangerously confused.
2327 result
= flash_erase_address_range(gdb_service
->target
,
2328 false, addr
, length
);
2330 /* perform any target specific operations after the erase */
2331 target_call_event_callbacks(gdb_service
->target
,
2332 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2335 if (result
!= ERROR_OK
) {
2336 /* GDB doesn't evaluate the actual error number returned,
2337 * treat a failed erase as an I/O error
2339 gdb_send_error(connection
, EIO
);
2340 LOG_ERROR("flash_erase returned %i", result
);
2342 gdb_put_packet(connection
, "OK", 2);
2347 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
2350 unsigned long length
;
2351 char *parse
= packet
+ 12;
2353 if (*parse
== '\0') {
2354 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2355 return ERROR_SERVER_REMOTE_CLOSED
;
2357 addr
= strtoul(parse
, &parse
, 16);
2358 if (*(parse
++) != ':') {
2359 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2360 return ERROR_SERVER_REMOTE_CLOSED
;
2362 length
= packet_size
- (parse
- packet
);
2364 /* create a new image if there isn't already one */
2365 if (gdb_connection
->vflash_image
== NULL
) {
2366 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2367 image_open(gdb_connection
->vflash_image
, "", "build");
2370 /* create new section with content from packet buffer */
2371 retval
= image_add_section(gdb_connection
->vflash_image
,
2372 addr
, length
, 0x0, (uint8_t *)parse
);
2373 if (retval
!= ERROR_OK
)
2376 gdb_put_packet(connection
, "OK", 2);
2381 if (strncmp(packet
, "vFlashDone", 10) == 0) {
2384 /* process the flashing buffer. No need to erase as GDB
2385 * always issues a vFlashErase first. */
2386 target_call_event_callbacks(gdb_service
->target
,
2387 TARGET_EVENT_GDB_FLASH_WRITE_START
);
2388 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2389 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2390 if (result
!= ERROR_OK
) {
2391 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2392 gdb_put_packet(connection
, "E.memtype", 9);
2394 gdb_send_error(connection
, EIO
);
2396 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2397 gdb_put_packet(connection
, "OK", 2);
2400 image_close(gdb_connection
->vflash_image
);
2401 free(gdb_connection
->vflash_image
);
2402 gdb_connection
->vflash_image
= NULL
;
2407 gdb_put_packet(connection
, "", 0);
2411 static int gdb_detach(struct connection
*connection
)
2413 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2415 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
2417 return gdb_put_packet(connection
, "OK", 2);
2420 /* The format of 'F' response packet is
2421 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2423 static int gdb_fileio_response_packet(struct connection
*connection
,
2424 char *packet
, int packet_size
)
2426 struct target
*target
= get_target_from_connection(connection
);
2428 char *parsing_point
;
2429 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
2430 int fileio_errno
= 0;
2431 bool fileio_ctrl_c
= false;
2436 if (*separator
== ',') {
2437 parsing_point
= separator
+ 1;
2438 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
2439 if (*separator
== ',') {
2440 if (*(separator
+ 1) == 'C') {
2441 /* TODO: process ctrl-c */
2442 fileio_ctrl_c
= true;
2447 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
2448 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
2450 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
2451 if (retval
!= ERROR_OK
)
2454 /* After File-I/O ends, keep continue or step */
2455 if (gdb_running_type
== 'c')
2456 retval
= target_resume(target
, 1, 0x0, 0, 0);
2457 else if (gdb_running_type
== 's')
2458 retval
= target_step(target
, 1, 0x0, 0);
2460 retval
= ERROR_FAIL
;
2462 if (retval
!= ERROR_OK
)
2468 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2469 const char *function
, const char *string
)
2471 struct connection
*connection
= priv
;
2472 struct gdb_connection
*gdb_con
= connection
->priv
;
2474 if (gdb_con
->busy
) {
2475 /* do not reply this using the O packet */
2479 gdb_output_con(connection
, string
);
2482 static void gdb_sig_halted(struct connection
*connection
)
2485 snprintf(sig_reply
, 4, "T%2.2x", 2);
2486 gdb_put_packet(connection
, sig_reply
, 3);
2489 static int gdb_input_inner(struct connection
*connection
)
2491 /* Do not allocate this on the stack */
2492 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2494 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2495 struct target
*target
= gdb_service
->target
;
2496 char *packet
= gdb_packet_buffer
;
2499 struct gdb_connection
*gdb_con
= connection
->priv
;
2500 static int extended_protocol
;
2502 /* drain input buffer. If one of the packets fail, then an error
2503 * packet is replied, if applicable.
2505 * This loop will terminate and the error code is returned.
2507 * The calling fn will check if this error is something that
2508 * can be recovered from, or if the connection must be closed.
2510 * If the error is recoverable, this fn is called again to
2511 * drain the rest of the buffer.
2514 packet_size
= GDB_BUFFER_SIZE
-1;
2515 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2516 if (retval
!= ERROR_OK
)
2519 /* terminate with zero */
2520 packet
[packet_size
] = 0;
2522 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2523 if (packet
[0] == 'X') {
2524 /* binary packets spew junk into the debug log stream */
2527 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
2530 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2532 LOG_DEBUG("received packet: '%s'", packet
);
2535 if (packet_size
> 0) {
2537 switch (packet
[0]) {
2538 case 'T': /* Is thread alive? */
2539 gdb_thread_packet(connection
, packet
, packet_size
);
2541 case 'H': /* Set current thread ( 'c' for step and continue,
2542 * 'g' for all other operations ) */
2543 gdb_thread_packet(connection
, packet
, packet_size
);
2547 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2548 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2549 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2552 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2555 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2558 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2561 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2564 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2567 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2571 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2574 gdb_last_signal_packet(connection
, packet
, packet_size
);
2579 gdb_thread_packet(connection
, packet
, packet_size
);
2580 log_add_callback(gdb_log_callback
, connection
);
2582 if (gdb_con
->mem_write_error
) {
2583 LOG_ERROR("Memory write failure!");
2585 /* now that we have reported the memory write error,
2586 * we can clear the condition */
2587 gdb_con
->mem_write_error
= false;
2590 bool nostep
= false;
2591 bool already_running
= false;
2592 if (target
->state
== TARGET_RUNNING
) {
2593 LOG_WARNING("WARNING! The target is already running. "
2594 "All changes GDB did to registers will be discarded! "
2595 "Waiting for target to halt.");
2596 already_running
= true;
2597 } else if (target
->state
!= TARGET_HALTED
) {
2598 LOG_WARNING("The target is not in the halted nor running stated, " \
2599 "stepi/continue ignored.");
2601 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
2602 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2603 * sent by GDB first to OpenOCD, thus defeating the check to
2604 * make only the single stepping have the sync feature...
2607 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2608 "from the target.");
2610 gdb_con
->sync
= false;
2612 if (!already_running
&& nostep
) {
2613 /* Either the target isn't in the halted state, then we can't
2614 * step/continue. This might be early setup, etc.
2616 * Or we want to allow GDB to pick up a fresh set of
2617 * register values without modifying the target state.
2620 gdb_sig_halted(connection
);
2622 /* stop forwarding log packets! */
2623 log_remove_callback(gdb_log_callback
, connection
);
2625 /* We're running/stepping, in which case we can
2626 * forward log output until the target is halted
2628 gdb_con
->frontend_state
= TARGET_RUNNING
;
2629 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2631 if (!already_running
) {
2632 /* Here we don't want packet processing to stop even if this fails,
2633 * so we use a local variable instead of retval. */
2634 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2635 if (retval
!= ERROR_OK
) {
2636 /* we'll never receive a halted
2637 * condition... issue a false one..
2639 gdb_frontend_halted(target
, connection
);
2646 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2649 retval
= gdb_detach(connection
);
2650 extended_protocol
= 0;
2653 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2654 if (retval
!= ERROR_OK
)
2658 if (extended_protocol
!= 0) {
2659 gdb_con
->attached
= false;
2662 gdb_put_packet(connection
, "OK", 2);
2663 return ERROR_SERVER_REMOTE_CLOSED
;
2665 /* handle extended remote protocol */
2666 extended_protocol
= 1;
2667 gdb_put_packet(connection
, "OK", 2);
2670 /* handle extended restart packet */
2671 breakpoint_clear_target(gdb_service
->target
);
2672 watchpoint_clear_target(gdb_service
->target
);
2673 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
2674 target_name(target
));
2675 /* set connection as attached after reset */
2676 gdb_con
->attached
= true;
2677 /* info rtos parts */
2678 gdb_thread_packet(connection
, packet
, packet_size
);
2682 /* packet supported only by smp target i.e cortex_a.c*/
2683 /* handle smp packet replying coreid played to gbd */
2684 gdb_read_smp_packet(connection
, packet
, packet_size
);
2688 /* packet supported only by smp target i.e cortex_a.c */
2689 /* handle smp packet setting coreid to be played at next
2691 gdb_write_smp_packet(connection
, packet
, packet_size
);
2695 /* File-I/O extension */
2696 /* After gdb uses host-side syscall to complete target file
2697 * I/O, gdb sends host-side syscall return value to target
2699 * The format of 'F' response packet is
2700 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2702 gdb_con
->frontend_state
= TARGET_RUNNING
;
2703 log_add_callback(gdb_log_callback
, connection
);
2704 gdb_fileio_response_packet(connection
, packet
, packet_size
);
2708 /* ignore unknown packets */
2709 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2710 gdb_put_packet(connection
, NULL
, 0);
2714 /* if a packet handler returned an error, exit input loop */
2715 if (retval
!= ERROR_OK
)
2719 if (gdb_con
->ctrl_c
) {
2720 if (target
->state
== TARGET_RUNNING
) {
2721 retval
= target_halt(target
);
2722 if (retval
!= ERROR_OK
)
2723 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2724 gdb_con
->ctrl_c
= 0;
2726 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2727 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2731 } while (gdb_con
->buf_cnt
> 0);
2736 static int gdb_input(struct connection
*connection
)
2738 int retval
= gdb_input_inner(connection
);
2739 struct gdb_connection
*gdb_con
= connection
->priv
;
2740 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2743 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2744 if (gdb_con
->closed
)
2745 return ERROR_SERVER_REMOTE_CLOSED
;
2747 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2751 static int gdb_target_start(struct target
*target
, const char *port
)
2753 struct gdb_service
*gdb_service
;
2755 gdb_service
= malloc(sizeof(struct gdb_service
));
2757 if (NULL
== gdb_service
)
2760 gdb_service
->target
= target
;
2761 gdb_service
->core
[0] = -1;
2762 gdb_service
->core
[1] = -1;
2763 target
->gdb_service
= gdb_service
;
2765 ret
= add_service("gdb",
2766 port
, 1, &gdb_new_connection
, &gdb_input
,
2767 &gdb_connection_closed
, gdb_service
);
2768 /* initialialize all targets gdb service with the same pointer */
2770 struct target_list
*head
;
2771 struct target
*curr
;
2772 head
= target
->head
;
2773 while (head
!= (struct target_list
*)NULL
) {
2774 curr
= head
->target
;
2776 curr
->gdb_service
= gdb_service
;
2783 static int gdb_target_add_one(struct target
*target
)
2785 /* one gdb instance per smp list */
2786 if ((target
->smp
) && (target
->gdb_service
))
2788 int retval
= gdb_target_start(target
, gdb_port_next
);
2789 if (retval
== ERROR_OK
) {
2791 /* If we can parse the port number
2792 * then we increment the port number for the next target.
2795 portnumber
= strtol(gdb_port_next
, &end
, 0);
2797 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
2798 free((void *)gdb_port_next
);
2799 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2806 int gdb_target_add_all(struct target
*target
)
2808 if (NULL
== target
) {
2809 LOG_WARNING("gdb services need one or more targets defined");
2813 while (NULL
!= target
) {
2814 int retval
= gdb_target_add_one(target
);
2815 if (ERROR_OK
!= retval
)
2818 target
= target
->next
;
2824 COMMAND_HANDLER(handle_gdb_sync_command
)
2827 return ERROR_COMMAND_SYNTAX_ERROR
;
2829 if (current_gdb_connection
== NULL
) {
2830 command_print(CMD_CTX
,
2831 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2835 current_gdb_connection
->sync
= true;
2840 /* daemon configuration command gdb_port */
2841 COMMAND_HANDLER(handle_gdb_port_command
)
2843 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2844 if (ERROR_OK
== retval
) {
2845 free((void *)gdb_port_next
);
2846 gdb_port_next
= strdup(gdb_port
);
2851 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2854 return ERROR_COMMAND_SYNTAX_ERROR
;
2856 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2860 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2863 return ERROR_COMMAND_SYNTAX_ERROR
;
2865 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2869 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2872 return ERROR_COMMAND_SYNTAX_ERROR
;
2874 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2878 /* gdb_breakpoint_override */
2879 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2881 if (CMD_ARGC
== 0) {
2883 } else if (CMD_ARGC
== 1) {
2884 gdb_breakpoint_override
= 1;
2885 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2886 gdb_breakpoint_override_type
= BKPT_HARD
;
2887 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2888 gdb_breakpoint_override_type
= BKPT_SOFT
;
2889 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2890 gdb_breakpoint_override
= 0;
2892 return ERROR_COMMAND_SYNTAX_ERROR
;
2893 if (gdb_breakpoint_override
)
2894 LOG_USER("force %s breakpoints",
2895 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
2897 LOG_USER("breakpoint type is not overridden");
2902 COMMAND_HANDLER(handle_gdb_target_description_command
)
2905 return ERROR_COMMAND_SYNTAX_ERROR
;
2907 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
2911 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
2914 static uint32_t tdesc_length
;
2915 struct target
*target
= get_current_target(CMD_CTX
);
2916 char *tdesc_filename
;
2918 if (tdesc
== NULL
) {
2919 gdb_generate_target_description(target
, &tdesc
);
2920 tdesc_length
= strlen(tdesc
);
2923 struct fileio fileio
;
2924 size_t size_written
;
2926 tdesc_filename
= malloc(strlen(target_type_name(target
)) + 5);
2927 sprintf(tdesc_filename
, "%s.xml", target_type_name(target
));
2929 int retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
2931 free(tdesc_filename
);
2933 if (retval
!= ERROR_OK
) {
2934 LOG_WARNING("Can't open %s for writing", tdesc_filename
);
2938 retval
= fileio_write(&fileio
, tdesc_length
, tdesc
, &size_written
);
2940 fileio_close(&fileio
);
2942 if (retval
!= ERROR_OK
) {
2943 LOG_WARNING("Error while writing the tdesc file");
2950 static const struct command_registration gdb_command_handlers
[] = {
2953 .handler
= handle_gdb_sync_command
,
2954 .mode
= COMMAND_ANY
,
2955 .help
= "next stepi will return immediately allowing "
2956 "GDB to fetch register state without affecting "
2962 .handler
= handle_gdb_port_command
,
2963 .mode
= COMMAND_ANY
,
2964 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2965 "server listens for the next port number after the "
2966 "base port number specified. "
2967 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2968 "output to stdout, an integer is base port number, \"disable\" disables "
2969 "port. Any other string is are interpreted as named pipe to listen to. "
2970 "Output pipe is the same name as input pipe, but with 'o' appended.",
2971 .usage
= "[port_num]",
2974 .name
= "gdb_memory_map",
2975 .handler
= handle_gdb_memory_map_command
,
2976 .mode
= COMMAND_CONFIG
,
2977 .help
= "enable or disable memory map",
2978 .usage
= "('enable'|'disable')"
2981 .name
= "gdb_flash_program",
2982 .handler
= handle_gdb_flash_program_command
,
2983 .mode
= COMMAND_CONFIG
,
2984 .help
= "enable or disable flash program",
2985 .usage
= "('enable'|'disable')"
2988 .name
= "gdb_report_data_abort",
2989 .handler
= handle_gdb_report_data_abort_command
,
2990 .mode
= COMMAND_CONFIG
,
2991 .help
= "enable or disable reporting data aborts",
2992 .usage
= "('enable'|'disable')"
2995 .name
= "gdb_breakpoint_override",
2996 .handler
= handle_gdb_breakpoint_override_command
,
2997 .mode
= COMMAND_ANY
,
2998 .help
= "Display or specify type of breakpoint "
2999 "to be used by gdb 'break' commands.",
3000 .usage
= "('hard'|'soft'|'disable')"
3003 .name
= "gdb_target_description",
3004 .handler
= handle_gdb_target_description_command
,
3005 .mode
= COMMAND_CONFIG
,
3006 .help
= "enable or disable target description",
3007 .usage
= "('enable'|'disable')"
3010 .name
= "gdb_save_tdesc",
3011 .handler
= handle_gdb_save_tdesc_command
,
3012 .mode
= COMMAND_EXEC
,
3013 .help
= "Save the target description file",
3015 COMMAND_REGISTRATION_DONE
3018 int gdb_register_commands(struct command_context
*cmd_ctx
)
3020 gdb_port
= strdup("3333");
3021 gdb_port_next
= strdup("3333");
3022 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);