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 /* gdb sends a dummy ack '+' at every remote connect - see
621 * remote_start_remote (remote.c)
622 * in case anyone tries to debug why they receive this
623 * warning every time */
624 LOG_WARNING("acknowledgment received, but no packet pending");
627 LOG_WARNING("negative acknowledgment, but no packet pending");
634 LOG_WARNING("ignoring character 0x%x", character
);
637 } while (character
!= '$');
640 /* explicit code expansion here to get faster inlined code in -O3 by not
641 * calculating checksum */
642 if (gdb_con
->noack_mode
) {
643 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
644 if (retval
!= ERROR_OK
)
647 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
648 if (retval
!= ERROR_OK
)
652 if (gdb_con
->noack_mode
) {
653 /* checksum is not checked in noack mode */
657 retval
= gdb_write(connection
, "+", 1);
658 if (retval
!= ERROR_OK
)
664 return ERROR_SERVER_REMOTE_CLOSED
;
669 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
671 struct gdb_connection
*gdb_con
= connection
->priv
;
673 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
678 static int gdb_output_con(struct connection
*connection
, const char *line
)
683 bin_size
= strlen(line
);
685 hex_buffer
= malloc(bin_size
* 2 + 2);
686 if (hex_buffer
== NULL
)
687 return ERROR_GDB_BUFFER_TOO_SMALL
;
690 int pkt_len
= hexify(hex_buffer
+ 1, line
, bin_size
, bin_size
* 2 + 1);
691 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
697 static int gdb_output(struct command_context
*context
, const char *line
)
699 /* this will be dumped to the log and also sent as an O packet if possible */
700 LOG_USER_N("%s", line
);
704 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
706 struct gdb_connection
*gdb_connection
= connection
->priv
;
708 char stop_reason
[20];
712 if (target
->debug_reason
== DBG_REASON_EXIT
) {
713 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
715 if (gdb_connection
->ctrl_c
) {
717 gdb_connection
->ctrl_c
= 0;
719 signal_var
= gdb_last_signal(target
);
721 stop_reason
[0] = '\0';
722 if (target
->debug_reason
== DBG_REASON_WATCHPOINT
) {
723 enum watchpoint_rw hit_wp_type
;
724 uint32_t hit_wp_address
;
726 if (watchpoint_hit(target
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
728 switch (hit_wp_type
) {
730 snprintf(stop_reason
, sizeof(stop_reason
),
731 "watch:%08x;", hit_wp_address
);
734 snprintf(stop_reason
, sizeof(stop_reason
),
735 "rwatch:%08x;", hit_wp_address
);
738 snprintf(stop_reason
, sizeof(stop_reason
),
739 "awatch:%08x;", hit_wp_address
);
747 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s",
748 signal_var
, stop_reason
);
751 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
752 gdb_connection
->frontend_state
= TARGET_HALTED
;
753 rtos_update_threads(target
);
756 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
758 struct gdb_connection
*gdb_connection
= connection
->priv
;
759 char fileio_command
[256];
761 bool program_exited
= false;
763 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
764 sprintf(fileio_command
, "F%s,%x/%x,%x,%x", target
->fileio_info
->identifier
,
765 target
->fileio_info
->param_1
,
766 target
->fileio_info
->param_2
,
767 target
->fileio_info
->param_3
,
768 target
->fileio_info
->param_4
);
769 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
770 sprintf(fileio_command
, "F%s,%x", target
->fileio_info
->identifier
,
771 target
->fileio_info
->param_1
);
772 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
773 sprintf(fileio_command
, "F%s,%x,%x,%x", target
->fileio_info
->identifier
,
774 target
->fileio_info
->param_1
,
775 target
->fileio_info
->param_2
,
776 target
->fileio_info
->param_3
);
777 else if (strcmp(target
->fileio_info
->identifier
, "write") == 0)
778 sprintf(fileio_command
, "F%s,%x,%x,%x", target
->fileio_info
->identifier
,
779 target
->fileio_info
->param_1
,
780 target
->fileio_info
->param_2
,
781 target
->fileio_info
->param_3
);
782 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
783 sprintf(fileio_command
, "F%s,%x,%x,%x", 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
, "rename") == 0)
788 sprintf(fileio_command
, "F%s,%x/%x,%x/%x", target
->fileio_info
->identifier
,
789 target
->fileio_info
->param_1
,
790 target
->fileio_info
->param_2
,
791 target
->fileio_info
->param_3
,
792 target
->fileio_info
->param_4
);
793 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
794 sprintf(fileio_command
, "F%s,%x/%x", target
->fileio_info
->identifier
,
795 target
->fileio_info
->param_1
,
796 target
->fileio_info
->param_2
);
797 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
798 sprintf(fileio_command
, "F%s,%x/%x,%x", target
->fileio_info
->identifier
,
799 target
->fileio_info
->param_1
,
800 target
->fileio_info
->param_2
,
801 target
->fileio_info
->param_3
);
802 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 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
, "gettimeofday") == 0)
807 sprintf(fileio_command
, "F%s,%x,%x", target
->fileio_info
->identifier
,
808 target
->fileio_info
->param_1
,
809 target
->fileio_info
->param_2
);
810 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
811 sprintf(fileio_command
, "F%s,%x", target
->fileio_info
->identifier
,
812 target
->fileio_info
->param_1
);
813 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
814 sprintf(fileio_command
, "F%s,%x/%x", target
->fileio_info
->identifier
,
815 target
->fileio_info
->param_1
,
816 target
->fileio_info
->param_2
);
817 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
818 /* If target hits exit syscall, report to GDB the program is terminated.
819 * In addition, let target run its own exit syscall handler. */
820 program_exited
= true;
821 sprintf(fileio_command
, "W%02x", target
->fileio_info
->param_1
);
823 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
825 /* encounter unknown syscall, continue */
826 gdb_connection
->frontend_state
= TARGET_RUNNING
;
827 target_resume(target
, 1, 0x0, 0, 0);
831 command_len
= strlen(fileio_command
);
832 gdb_put_packet(connection
, fileio_command
, command_len
);
834 if (program_exited
) {
835 /* Use target_resume() to let target run its own exit syscall handler. */
836 gdb_connection
->frontend_state
= TARGET_RUNNING
;
837 target_resume(target
, 1, 0x0, 0, 0);
839 gdb_connection
->frontend_state
= TARGET_HALTED
;
840 rtos_update_threads(target
);
844 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
846 struct gdb_connection
*gdb_connection
= connection
->priv
;
848 /* In the GDB protocol when we are stepping or continuing execution,
849 * we have a lingering reply. Upon receiving a halted event
850 * when we have that lingering packet, we reply to the original
851 * step or continue packet.
853 * Executing monitor commands can bring the target in and
854 * out of the running state so we'll see lots of TARGET_EVENT_XXX
855 * that are to be ignored.
857 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
858 /* stop forwarding log packets! */
859 log_remove_callback(gdb_log_callback
, connection
);
861 /* check fileio first */
862 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
863 gdb_fileio_reply(target
, connection
);
865 gdb_signal_reply(target
, connection
);
869 static int gdb_target_callback_event_handler(struct target
*target
,
870 enum target_event event
, void *priv
)
873 struct connection
*connection
= priv
;
874 struct gdb_service
*gdb_service
= connection
->service
->priv
;
876 if (gdb_service
->target
!= target
)
880 case TARGET_EVENT_GDB_HALT
:
881 gdb_frontend_halted(target
, connection
);
883 case TARGET_EVENT_HALTED
:
884 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
886 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
887 retval
= jtag_execute_queue();
888 if (retval
!= ERROR_OK
)
898 static int gdb_new_connection(struct connection
*connection
)
900 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
901 struct gdb_service
*gdb_service
= connection
->service
->priv
;
905 connection
->priv
= gdb_connection
;
907 /* initialize gdb connection information */
908 gdb_connection
->buf_p
= gdb_connection
->buffer
;
909 gdb_connection
->buf_cnt
= 0;
910 gdb_connection
->ctrl_c
= 0;
911 gdb_connection
->frontend_state
= TARGET_HALTED
;
912 gdb_connection
->vflash_image
= NULL
;
913 gdb_connection
->closed
= 0;
914 gdb_connection
->busy
= 0;
915 gdb_connection
->noack_mode
= 0;
916 gdb_connection
->sync
= false;
917 gdb_connection
->mem_write_error
= false;
918 gdb_connection
->attached
= true;
919 gdb_connection
->target_desc
.tdesc
= NULL
;
920 gdb_connection
->target_desc
.tdesc_length
= 0;
922 /* send ACK to GDB for debug request */
923 gdb_write(connection
, "+", 1);
925 /* output goes through gdb connection */
926 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
928 /* we must remove all breakpoints registered to the target as a previous
929 * GDB session could leave dangling breakpoints if e.g. communication
932 breakpoint_clear_target(gdb_service
->target
);
933 watchpoint_clear_target(gdb_service
->target
);
935 /* clean previous rtos session if supported*/
936 if ((gdb_service
->target
->rtos
) && (gdb_service
->target
->rtos
->type
->clean
))
937 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
939 /* remove the initial ACK from the incoming buffer */
940 retval
= gdb_get_char(connection
, &initial_ack
);
941 if (retval
!= ERROR_OK
)
944 /* FIX!!!??? would we actually ever receive a + here???
947 if (initial_ack
!= '+')
948 gdb_putback_char(connection
, initial_ack
);
949 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
951 if (gdb_use_memory_map
) {
952 /* Connect must fail if the memory map can't be set up correctly.
954 * This will cause an auto_probe to be invoked, which is either
955 * a no-op or it will fail when the target isn't ready(e.g. not halted).
958 for (i
= 0; i
< flash_get_bank_count(); i
++) {
959 struct flash_bank
*p
;
960 retval
= get_flash_bank_by_num(i
, &p
);
961 if (retval
!= ERROR_OK
) {
962 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
963 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
969 gdb_actual_connections
++;
970 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
971 gdb_actual_connections
,
972 target_name(gdb_service
->target
),
973 target_state_name(gdb_service
->target
));
975 /* DANGER! If we fail subsequently, we must remove this handler,
976 * otherwise we occasionally see crashes as the timer can invoke the
979 * register callback to be informed about target events */
980 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
985 static int gdb_connection_closed(struct connection
*connection
)
987 struct gdb_service
*gdb_service
= connection
->service
->priv
;
988 struct gdb_connection
*gdb_connection
= connection
->priv
;
990 /* we're done forwarding messages. Tear down callback before
991 * cleaning up connection.
993 log_remove_callback(gdb_log_callback
, connection
);
995 gdb_actual_connections
--;
996 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
997 target_name(gdb_service
->target
),
998 target_state_name(gdb_service
->target
),
999 gdb_actual_connections
);
1001 /* see if an image built with vFlash commands is left */
1002 if (gdb_connection
->vflash_image
) {
1003 image_close(gdb_connection
->vflash_image
);
1004 free(gdb_connection
->vflash_image
);
1005 gdb_connection
->vflash_image
= NULL
;
1008 /* if this connection registered a debug-message receiver delete it */
1009 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
1011 if (connection
->priv
) {
1012 free(connection
->priv
);
1013 connection
->priv
= NULL
;
1015 LOG_ERROR("BUG: connection->priv == NULL");
1017 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1019 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
1021 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
1026 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1029 snprintf(err
, 4, "E%2.2X", the_error
);
1030 gdb_put_packet(connection
, err
, 3);
1033 static int gdb_last_signal_packet(struct connection
*connection
,
1034 char *packet
, int packet_size
)
1036 struct target
*target
= get_target_from_connection(connection
);
1037 struct gdb_connection
*gdb_con
= connection
->priv
;
1041 if (!gdb_con
->attached
) {
1042 /* if we are here we have received a kill packet
1043 * reply W stop reply otherwise gdb gets very unhappy */
1044 gdb_put_packet(connection
, "W00", 3);
1048 signal_var
= gdb_last_signal(target
);
1050 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1051 gdb_put_packet(connection
, sig_reply
, 3);
1056 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1058 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1061 return len
- 1 - pos
;
1064 /* Convert register to string of bytes. NB! The # of bits in the
1065 * register might be non-divisible by 8(a byte), in which
1066 * case an entire byte is shown.
1068 * NB! the format on the wire is the target endianness
1070 * The format of reg->value is little endian
1073 static void gdb_str_to_target(struct target
*target
,
1074 char *tstr
, struct reg
*reg
)
1081 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1083 for (i
= 0; i
< buf_len
; i
++) {
1084 int j
= gdb_reg_pos(target
, i
, buf_len
);
1085 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1089 /* copy over in register buffer */
1090 static void gdb_target_to_reg(struct target
*target
,
1091 char *tstr
, int str_len
, uint8_t *bin
)
1094 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1099 for (i
= 0; i
< str_len
; i
+= 2) {
1101 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1102 LOG_ERROR("BUG: unable to convert register value");
1106 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1111 static int gdb_get_registers_packet(struct connection
*connection
,
1112 char *packet
, int packet_size
)
1114 struct target
*target
= get_target_from_connection(connection
);
1115 struct reg
**reg_list
;
1118 int reg_packet_size
= 0;
1123 #ifdef _DEBUG_GDB_IO_
1127 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1130 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1132 if (retval
!= ERROR_OK
)
1133 return gdb_error(connection
, retval
);
1135 for (i
= 0; i
< reg_list_size
; i
++)
1136 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1138 assert(reg_packet_size
> 0);
1140 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1141 reg_packet_p
= reg_packet
;
1143 for (i
= 0; i
< reg_list_size
; i
++) {
1144 if (!reg_list
[i
]->valid
)
1145 reg_list
[i
]->type
->get(reg_list
[i
]);
1146 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1147 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1150 #ifdef _DEBUG_GDB_IO_
1152 char *reg_packet_p_debug
;
1153 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1154 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1155 free(reg_packet_p_debug
);
1159 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1167 static int gdb_set_registers_packet(struct connection
*connection
,
1168 char *packet
, int packet_size
)
1170 struct target
*target
= get_target_from_connection(connection
);
1172 struct reg
**reg_list
;
1177 #ifdef _DEBUG_GDB_IO_
1181 /* skip command character */
1185 if (packet_size
% 2) {
1186 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1187 return ERROR_SERVER_REMOTE_CLOSED
;
1190 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1192 if (retval
!= ERROR_OK
)
1193 return gdb_error(connection
, retval
);
1196 for (i
= 0; i
< reg_list_size
; i
++) {
1198 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1200 if (packet_p
+ chars
> packet
+ packet_size
)
1201 LOG_ERROR("BUG: register packet is too small for registers");
1203 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1204 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1206 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1208 /* advance packet pointer */
1214 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1217 gdb_put_packet(connection
, "OK", 2);
1222 static int gdb_get_register_packet(struct connection
*connection
,
1223 char *packet
, int packet_size
)
1225 struct target
*target
= get_target_from_connection(connection
);
1227 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1228 struct reg
**reg_list
;
1232 #ifdef _DEBUG_GDB_IO_
1236 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1238 if (retval
!= ERROR_OK
)
1239 return gdb_error(connection
, retval
);
1241 if (reg_list_size
<= reg_num
) {
1242 LOG_ERROR("gdb requested a non-existing register");
1243 return ERROR_SERVER_REMOTE_CLOSED
;
1246 if (!reg_list
[reg_num
]->valid
)
1247 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1249 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1); /* plus one for string termination null */
1251 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1253 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1261 static int gdb_set_register_packet(struct connection
*connection
,
1262 char *packet
, int packet_size
)
1264 struct target
*target
= get_target_from_connection(connection
);
1267 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1268 struct reg
**reg_list
;
1274 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1276 if (retval
!= ERROR_OK
)
1277 return gdb_error(connection
, retval
);
1279 if (reg_list_size
<= reg_num
) {
1280 LOG_ERROR("gdb requested a non-existing register");
1281 return ERROR_SERVER_REMOTE_CLOSED
;
1284 if (*separator
!= '=') {
1285 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1286 return ERROR_SERVER_REMOTE_CLOSED
;
1289 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1290 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1291 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1293 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1294 LOG_ERROR("gdb sent a packet with wrong register size");
1296 return ERROR_SERVER_REMOTE_CLOSED
;
1299 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1301 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1303 gdb_put_packet(connection
, "OK", 2);
1311 /* No attempt is made to translate the "retval" to
1312 * GDB speak. This has to be done at the calling
1313 * site as no mapping really exists.
1315 static int gdb_error(struct connection
*connection
, int retval
)
1317 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1318 gdb_send_error(connection
, EFAULT
);
1322 /* We don't have to worry about the default 2 second timeout for GDB packets,
1323 * because GDB breaks up large memory reads into smaller reads.
1325 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1327 static int gdb_read_memory_packet(struct connection
*connection
,
1328 char *packet
, int packet_size
)
1330 struct target
*target
= get_target_from_connection(connection
);
1338 int retval
= ERROR_OK
;
1340 /* skip command character */
1343 addr
= strtoul(packet
, &separator
, 16);
1345 if (*separator
!= ',') {
1346 LOG_ERROR("incomplete read memory packet received, dropping connection");
1347 return ERROR_SERVER_REMOTE_CLOSED
;
1350 len
= strtoul(separator
+ 1, NULL
, 16);
1352 buffer
= malloc(len
);
1354 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1356 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1358 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1359 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1360 * At some point this might be fixed in GDB, in which case this code can be removed.
1362 * OpenOCD developers are acutely aware of this problem, but there is nothing
1363 * gained by involving the user in this problem that hopefully will get resolved
1366 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1367 * cmd = view%20audit-trail&database = gdb&pr = 2395
1369 * For now, the default is to fix up things to make current GDB versions work.
1370 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1372 memset(buffer
, 0, len
);
1376 if (retval
== ERROR_OK
) {
1377 hex_buffer
= malloc(len
* 2 + 1);
1379 int pkt_len
= hexify(hex_buffer
, (char *)buffer
, len
, len
* 2 + 1);
1381 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1385 retval
= gdb_error(connection
, retval
);
1392 static int gdb_write_memory_packet(struct connection
*connection
,
1393 char *packet
, int packet_size
)
1395 struct target
*target
= get_target_from_connection(connection
);
1403 /* skip command character */
1406 addr
= strtoul(packet
, &separator
, 16);
1408 if (*separator
!= ',') {
1409 LOG_ERROR("incomplete write memory packet received, dropping connection");
1410 return ERROR_SERVER_REMOTE_CLOSED
;
1413 len
= strtoul(separator
+ 1, &separator
, 16);
1415 if (*(separator
++) != ':') {
1416 LOG_ERROR("incomplete write memory packet received, dropping connection");
1417 return ERROR_SERVER_REMOTE_CLOSED
;
1420 buffer
= malloc(len
);
1422 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1424 if (unhexify((char *)buffer
, separator
, len
) != (int)len
)
1425 LOG_ERROR("unable to decode memory packet");
1427 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1429 if (retval
== ERROR_OK
)
1430 gdb_put_packet(connection
, "OK", 2);
1432 retval
= gdb_error(connection
, retval
);
1439 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1440 char *packet
, int packet_size
)
1442 struct target
*target
= get_target_from_connection(connection
);
1447 int retval
= ERROR_OK
;
1449 /* skip command character */
1452 addr
= strtoul(packet
, &separator
, 16);
1454 if (*separator
!= ',') {
1455 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1456 return ERROR_SERVER_REMOTE_CLOSED
;
1459 len
= strtoul(separator
+ 1, &separator
, 16);
1461 if (*(separator
++) != ':') {
1462 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1463 return ERROR_SERVER_REMOTE_CLOSED
;
1466 struct gdb_connection
*gdb_connection
= connection
->priv
;
1468 if (gdb_connection
->mem_write_error
) {
1469 retval
= ERROR_FAIL
;
1470 /* now that we have reported the memory write error, we can clear the condition */
1471 gdb_connection
->mem_write_error
= false;
1474 /* By replying the packet *immediately* GDB will send us a new packet
1475 * while we write the last one to the target.
1477 if (retval
== ERROR_OK
)
1478 gdb_put_packet(connection
, "OK", 2);
1480 retval
= gdb_error(connection
, retval
);
1481 if (retval
!= ERROR_OK
)
1486 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1488 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1489 if (retval
!= ERROR_OK
)
1490 gdb_connection
->mem_write_error
= true;
1496 static int gdb_step_continue_packet(struct connection
*connection
,
1497 char *packet
, int packet_size
)
1499 struct target
*target
= get_target_from_connection(connection
);
1501 uint32_t address
= 0x0;
1502 int retval
= ERROR_OK
;
1506 if (packet_size
> 1) {
1507 packet
[packet_size
] = 0;
1508 address
= strtoul(packet
+ 1, NULL
, 16);
1512 gdb_running_type
= packet
[0];
1513 if (packet
[0] == 'c') {
1514 LOG_DEBUG("continue");
1515 /* resume at current address, don't handle breakpoints, not debugging */
1516 retval
= target_resume(target
, current
, address
, 0, 0);
1517 } else if (packet
[0] == 's') {
1519 /* step at current or address, don't handle breakpoints */
1520 retval
= target_step(target
, current
, address
, 0);
1525 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1526 char *packet
, int packet_size
)
1528 struct target
*target
= get_target_from_connection(connection
);
1530 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1531 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1539 type
= strtoul(packet
+ 1, &separator
, 16);
1541 if (type
== 0) /* memory breakpoint */
1542 bp_type
= BKPT_SOFT
;
1543 else if (type
== 1) /* hardware breakpoint */
1544 bp_type
= BKPT_HARD
;
1545 else if (type
== 2) /* write watchpoint */
1546 wp_type
= WPT_WRITE
;
1547 else if (type
== 3) /* read watchpoint */
1549 else if (type
== 4) /* access watchpoint */
1550 wp_type
= WPT_ACCESS
;
1552 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1553 return ERROR_SERVER_REMOTE_CLOSED
;
1556 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1557 bp_type
= gdb_breakpoint_override_type
;
1559 if (*separator
!= ',') {
1560 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1561 return ERROR_SERVER_REMOTE_CLOSED
;
1564 address
= strtoul(separator
+ 1, &separator
, 16);
1566 if (*separator
!= ',') {
1567 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1568 return ERROR_SERVER_REMOTE_CLOSED
;
1571 size
= strtoul(separator
+ 1, &separator
, 16);
1576 if (packet
[0] == 'Z') {
1577 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1578 if (retval
!= ERROR_OK
) {
1579 retval
= gdb_error(connection
, retval
);
1580 if (retval
!= ERROR_OK
)
1583 gdb_put_packet(connection
, "OK", 2);
1585 breakpoint_remove(target
, address
);
1586 gdb_put_packet(connection
, "OK", 2);
1593 if (packet
[0] == 'Z') {
1594 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1595 if (retval
!= ERROR_OK
) {
1596 retval
= gdb_error(connection
, retval
);
1597 if (retval
!= ERROR_OK
)
1600 gdb_put_packet(connection
, "OK", 2);
1602 watchpoint_remove(target
, address
);
1603 gdb_put_packet(connection
, "OK", 2);
1614 /* print out a string and allocate more space as needed,
1615 * mainly used for XML at this point
1617 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1618 const char *fmt
, ...)
1620 if (*retval
!= ERROR_OK
)
1625 if ((*xml
== NULL
) || (!first
)) {
1626 /* start by 0 to exercise all the code paths.
1627 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1629 *size
= *size
* 2 + 2;
1631 *xml
= realloc(*xml
, *size
);
1635 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1643 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1645 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1649 /* there was just enough or not enough space, allocate more. */
1654 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1658 /* Extract and NUL-terminate the annex. */
1660 while (*buf
&& *buf
!= ':')
1666 /* After the read marker and annex, qXfer looks like a
1667 * traditional 'm' packet. */
1669 *ofs
= strtoul(buf
, &separator
, 16);
1671 if (*separator
!= ',')
1674 *len
= strtoul(separator
+ 1, NULL
, 16);
1679 static int compare_bank(const void *a
, const void *b
)
1681 struct flash_bank
*b1
, *b2
;
1682 b1
= *((struct flash_bank
**)a
);
1683 b2
= *((struct flash_bank
**)b
);
1685 if (b1
->base
== b2
->base
)
1687 else if (b1
->base
> b2
->base
)
1693 static int gdb_memory_map(struct connection
*connection
,
1694 char *packet
, int packet_size
)
1696 /* We get away with only specifying flash here. Regions that are not
1697 * specified are treated as if we provided no memory map(if not we
1698 * could detect the holes and mark them as RAM).
1699 * Normally we only execute this code once, but no big deal if we
1700 * have to regenerate it a couple of times.
1703 struct target
*target
= get_target_from_connection(connection
);
1704 struct flash_bank
*p
;
1708 int retval
= ERROR_OK
;
1709 struct flash_bank
**banks
;
1713 uint32_t ram_start
= 0;
1715 int target_flash_banks
= 0;
1717 /* skip command character */
1720 offset
= strtoul(packet
, &separator
, 16);
1721 length
= strtoul(separator
+ 1, &separator
, 16);
1723 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1725 /* Sort banks in ascending order. We need to report non-flash
1726 * memory as ram (or rather read/write) by default for GDB, since
1727 * it has no concept of non-cacheable read/write memory (i/o etc).
1729 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1730 * Current versions of GDB assume unlisted addresses are RAM...
1732 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1734 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1735 retval
= get_flash_bank_by_num(i
, &p
);
1736 if (retval
!= ERROR_OK
) {
1738 gdb_error(connection
, retval
);
1741 if (p
->target
== target
)
1742 banks
[target_flash_banks
++] = p
;
1745 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1748 for (i
= 0; i
< target_flash_banks
; i
++) {
1750 unsigned sector_size
= 0;
1756 if (ram_start
< p
->base
)
1757 xml_printf(&retval
, &xml
, &pos
, &size
,
1758 "<memory type=\"ram\" start=\"0x%x\" "
1759 "length=\"0x%x\"/>\n",
1760 ram_start
, p
->base
- ram_start
);
1762 /* Report adjacent groups of same-size sectors. So for
1763 * example top boot CFI flash will list an initial region
1764 * with several large sectors (maybe 128KB) and several
1765 * smaller ones at the end (maybe 32KB). STR7 will have
1766 * regions with 8KB, 32KB, and 64KB sectors; etc.
1768 for (j
= 0; j
< p
->num_sectors
; j
++) {
1771 /* Maybe start a new group of sectors. */
1772 if (sector_size
== 0) {
1773 start
= p
->base
+ p
->sectors
[j
].offset
;
1774 xml_printf(&retval
, &xml
, &pos
, &size
,
1775 "<memory type=\"flash\" "
1778 sector_size
= p
->sectors
[j
].size
;
1781 /* Does this finish a group of sectors?
1782 * If not, continue an already-started group.
1784 if (j
== p
->num_sectors
- 1)
1785 group_len
= (p
->base
+ p
->size
) - start
;
1786 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1787 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1792 xml_printf(&retval
, &xml
, &pos
, &size
,
1793 "length=\"0x%x\">\n"
1794 "<property name=\"blocksize\">"
1802 ram_start
= p
->base
+ p
->size
;
1806 xml_printf(&retval
, &xml
, &pos
, &size
,
1807 "<memory type=\"ram\" start=\"0x%x\" "
1808 "length=\"0x%x\"/>\n",
1809 ram_start
, 0-ram_start
);
1810 /* ELSE a flash chip could be at the very end of the 32 bit address
1811 * space, in which case ram_start will be precisely 0
1817 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1819 if (retval
!= ERROR_OK
) {
1820 gdb_error(connection
, retval
);
1824 if (offset
+ length
> pos
)
1825 length
= pos
- offset
;
1827 char *t
= malloc(length
+ 1);
1829 memcpy(t
+ 1, xml
+ offset
, length
);
1830 gdb_put_packet(connection
, t
, length
+ 1);
1837 static const char *gdb_get_reg_type_name(enum reg_type type
)
1844 case REG_TYPE_INT16
:
1846 case REG_TYPE_INT32
:
1848 case REG_TYPE_INT64
:
1850 case REG_TYPE_INT128
:
1852 case REG_TYPE_UINT8
:
1854 case REG_TYPE_UINT16
:
1856 case REG_TYPE_UINT32
:
1858 case REG_TYPE_UINT64
:
1860 case REG_TYPE_UINT128
:
1862 case REG_TYPE_CODE_PTR
:
1864 case REG_TYPE_DATA_PTR
:
1866 case REG_TYPE_FLOAT
:
1868 case REG_TYPE_IEEE_SINGLE
:
1869 return "ieee_single";
1870 case REG_TYPE_IEEE_DOUBLE
:
1871 return "ieee_double";
1872 case REG_TYPE_ARCH_DEFINED
:
1873 return "int"; /* return arbitrary string to avoid compile warning. */
1876 return "int"; /* "int" as default value */
1879 static int gdb_generate_reg_type_description(struct target
*target
,
1880 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
)
1882 int retval
= ERROR_OK
;
1884 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
1885 /* <vector id="id" type="type" count="count"/> */
1886 xml_printf(&retval
, tdesc
, pos
, size
,
1887 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
1888 type
->id
, type
->reg_type_vector
->type
->id
,
1889 type
->reg_type_vector
->count
);
1891 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
1893 * <field name="name" type="type"/> ...
1895 xml_printf(&retval
, tdesc
, pos
, size
,
1896 "<union id=\"%s\">\n",
1899 struct reg_data_type_union_field
*field
;
1900 field
= type
->reg_type_union
->fields
;
1901 while (field
!= NULL
) {
1902 xml_printf(&retval
, tdesc
, pos
, size
,
1903 "<field name=\"%s\" type=\"%s\"/>\n",
1904 field
->name
, field
->type
->id
);
1906 field
= field
->next
;
1909 xml_printf(&retval
, tdesc
, pos
, size
,
1912 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
1913 struct reg_data_type_struct_field
*field
;
1914 field
= type
->reg_type_struct
->fields
;
1916 if (field
->use_bitfields
) {
1917 /* <struct id="id" size="size">
1918 * <field name="name" start="start" end="end"/> ...
1920 xml_printf(&retval
, tdesc
, pos
, size
,
1921 "<struct id=\"%s\" size=\"%d\">\n",
1922 type
->id
, type
->reg_type_struct
->size
);
1923 while (field
!= NULL
) {
1924 xml_printf(&retval
, tdesc
, pos
, size
,
1925 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1926 field
->name
, field
->bitfield
->start
,
1927 field
->bitfield
->end
);
1929 field
= field
->next
;
1933 * <field name="name" type="type"/> ...
1935 xml_printf(&retval
, tdesc
, pos
, size
,
1936 "<struct id=\"%s\">\n",
1938 while (field
!= NULL
) {
1939 xml_printf(&retval
, tdesc
, pos
, size
,
1940 "<field name=\"%s\" type=\"%s\"/>\n",
1941 field
->name
, field
->type
->id
);
1943 field
= field
->next
;
1947 xml_printf(&retval
, tdesc
, pos
, size
,
1950 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
1951 /* <flags id="id" size="size">
1952 * <field name="name" start="start" end="end"/> ...
1954 xml_printf(&retval
, tdesc
, pos
, size
,
1955 "<flags id=\"%s\" size=\"%d\">\n",
1956 type
->id
, type
->reg_type_flags
->size
);
1958 struct reg_data_type_flags_field
*field
;
1959 field
= type
->reg_type_flags
->fields
;
1960 while (field
!= NULL
) {
1961 xml_printf(&retval
, tdesc
, pos
, size
,
1962 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1963 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
);
1965 field
= field
->next
;
1968 xml_printf(&retval
, tdesc
, pos
, size
,
1976 /* Get a list of available target registers features. feature_list must
1977 * be freed by caller.
1979 static int get_reg_features_list(struct target
*target
, char **feature_list
[], int *feature_list_size
,
1980 struct reg
**reg_list
, int reg_list_size
)
1984 /* Start with only one element */
1985 *feature_list
= calloc(1, sizeof(char *));
1987 for (int i
= 0; i
< reg_list_size
; i
++) {
1988 if (reg_list
[i
]->exist
== false)
1991 if (reg_list
[i
]->feature
!= NULL
1992 && reg_list
[i
]->feature
->name
!= NULL
1993 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
1994 /* We found a feature, check if the feature is already in the
1995 * table. If not, allocate a new entry for the table and
1996 * put the new feature in it.
1998 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
1999 if (!((*feature_list
)[j
])) {
2000 (*feature_list
)[tbl_sz
++] = strdup(reg_list
[i
]->feature
->name
);
2001 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2002 (*feature_list
)[tbl_sz
] = NULL
;
2005 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2012 if (feature_list_size
)
2013 *feature_list_size
= tbl_sz
;
2018 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2020 int retval
= ERROR_OK
;
2021 struct reg
**reg_list
;
2027 retval
= target_get_gdb_reg_list(target
, ®_list
,
2028 ®_list_size
, REG_CLASS_ALL
);
2030 if (retval
!= ERROR_OK
) {
2031 LOG_ERROR("get register list failed");
2035 if (reg_list_size
<= 0) {
2040 char **features
= NULL
;
2041 /* Get a list of available target registers features */
2042 retval
= get_reg_features_list(target
, &features
, NULL
, reg_list
, reg_list_size
);
2043 if (retval
!= ERROR_OK
) {
2044 LOG_ERROR("Can't get the registers feature list");
2049 /* If we found some features associated with registers, create sections */
2050 int current_feature
= 0;
2052 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2053 "<?xml version=\"1.0\"?>\n"
2054 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2055 "<target version=\"1.0\">\n");
2057 /* generate target description according to register list */
2058 if (features
!= NULL
) {
2059 while (features
[current_feature
]) {
2061 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2062 "<feature name=\"%s\">\n",
2063 features
[current_feature
]);
2066 for (i
= 0; i
< reg_list_size
; i
++) {
2068 if (reg_list
[i
]->exist
== false)
2071 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2074 const char *type_str
;
2075 if (reg_list
[i
]->reg_data_type
!= NULL
) {
2076 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2077 /* generate <type... first, if there are architecture-defined types. */
2078 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2079 reg_list
[i
]->reg_data_type
);
2081 type_str
= reg_list
[i
]->reg_data_type
->id
;
2083 /* predefined type */
2084 type_str
= gdb_get_reg_type_name(
2085 reg_list
[i
]->reg_data_type
->type
);
2088 /* Default type is "int" */
2092 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2093 "<reg name=\"%s\"", reg_list
[i
]->name
);
2094 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2095 " bitsize=\"%d\"", reg_list
[i
]->size
);
2096 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2097 " regnum=\"%d\"", reg_list
[i
]->number
);
2098 if (reg_list
[i
]->caller_save
)
2099 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2100 " save-restore=\"yes\"");
2102 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2103 " save-restore=\"no\"");
2105 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2106 " type=\"%s\"", type_str
);
2108 if (reg_list
[i
]->group
!= NULL
)
2109 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2110 " group=\"%s\"", reg_list
[i
]->group
);
2112 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2116 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2123 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2129 if (retval
== ERROR_OK
)
2137 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2138 char **chunk
, int32_t offset
, uint32_t length
)
2140 if (target_desc
== NULL
) {
2141 LOG_ERROR("Unable to Generate Target Description");
2145 char *tdesc
= target_desc
->tdesc
;
2146 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2148 if (tdesc
== NULL
) {
2149 int retval
= gdb_generate_target_description(target
, &tdesc
);
2150 if (retval
!= ERROR_OK
) {
2151 LOG_ERROR("Unable to Generate Target Description");
2155 tdesc_length
= strlen(tdesc
);
2160 if (length
< (tdesc_length
- offset
))
2161 transfer_type
= 'm';
2163 transfer_type
= 'l';
2165 *chunk
= malloc(length
+ 2);
2166 if (*chunk
== NULL
) {
2167 LOG_ERROR("Unable to allocate memory");
2171 (*chunk
)[0] = transfer_type
;
2172 if (transfer_type
== 'm') {
2173 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2174 (*chunk
)[1 + length
] = '\0';
2176 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2177 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2179 /* After gdb-server sends out last chunk, invalidate tdesc. */
2185 target_desc
->tdesc
= tdesc
;
2186 target_desc
->tdesc_length
= tdesc_length
;
2191 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2193 int retval
= ERROR_OK
;
2194 struct reg
**reg_list
= NULL
;
2195 int reg_list_size
= 0;
2196 int feature_list_size
= 0;
2198 retval
= target_get_gdb_reg_list(target
, ®_list
,
2199 ®_list_size
, REG_CLASS_ALL
);
2200 if (retval
!= ERROR_OK
) {
2201 LOG_ERROR("get register list failed");
2205 if (reg_list_size
<= 0) {
2206 retval
= ERROR_FAIL
;
2210 char **features
= NULL
;
2211 /* Get a list of available target registers features */
2212 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2213 if (retval
!= ERROR_OK
) {
2214 LOG_ERROR("Can't get the registers feature list");
2219 if (feature_list_size
)
2226 if (reg_list
!= NULL
)
2229 if (features
!= NULL
)
2235 static int gdb_query_packet(struct connection
*connection
,
2236 char *packet
, int packet_size
)
2238 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2239 struct gdb_connection
*gdb_connection
= connection
->priv
;
2240 struct target
*target
= get_target_from_connection(connection
);
2242 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2243 if (packet_size
> 6) {
2245 cmd
= malloc((packet_size
- 6) / 2 + 1);
2246 int len
= unhexify(cmd
, packet
+ 6, (packet_size
- 6) / 2);
2249 /* We want to print all debug output to GDB connection */
2250 log_add_callback(gdb_log_callback
, connection
);
2251 target_call_timer_callbacks_now();
2252 /* some commands need to know the GDB connection, make note of current
2253 * GDB connection. */
2254 current_gdb_connection
= gdb_connection
;
2255 command_run_line(cmd_ctx
, cmd
);
2256 current_gdb_connection
= NULL
;
2257 target_call_timer_callbacks_now();
2258 log_remove_callback(gdb_log_callback
, connection
);
2261 gdb_put_packet(connection
, "OK", 2);
2263 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2264 if (packet_size
> 5) {
2272 /* skip command character */
2275 addr
= strtoul(packet
, &separator
, 16);
2277 if (*separator
!= ',') {
2278 LOG_ERROR("incomplete read memory packet received, dropping connection");
2279 return ERROR_SERVER_REMOTE_CLOSED
;
2282 len
= strtoul(separator
+ 1, NULL
, 16);
2284 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2286 if (retval
== ERROR_OK
) {
2287 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2288 gdb_put_packet(connection
, gdb_reply
, 9);
2290 retval
= gdb_error(connection
, retval
);
2291 if (retval
!= ERROR_OK
)
2297 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2298 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2299 * qXfer:features:read is supported for some targets */
2300 int retval
= ERROR_OK
;
2301 char *buffer
= NULL
;
2304 int gdb_target_desc_supported
= 0;
2306 /* we need to test that the target supports target descriptions */
2307 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2308 if (retval
!= ERROR_OK
) {
2309 LOG_INFO("Failed detecting Target Description Support, disabling");
2310 gdb_target_desc_supported
= 0;
2313 /* support may be disabled globally */
2314 if (gdb_use_target_description
== 0) {
2315 if (gdb_target_desc_supported
)
2316 LOG_WARNING("Target Descriptions Supported, but disabled");
2317 gdb_target_desc_supported
= 0;
2324 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;QStartNoAckMode+",
2325 (GDB_BUFFER_SIZE
- 1),
2326 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2327 (gdb_target_desc_supported
== 1) ? '+' : '-');
2329 if (retval
!= ERROR_OK
) {
2330 gdb_send_error(connection
, 01);
2334 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2338 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2339 && (flash_get_bank_count() > 0))
2340 return gdb_memory_map(connection
, packet
, packet_size
);
2341 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2343 int retval
= ERROR_OK
;
2346 unsigned int length
;
2349 /* skip command character */
2352 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0) {
2353 gdb_send_error(connection
, 01);
2357 /* Target should prepare correct target description for annex.
2358 * The first character of returned xml is 'm' or 'l'. 'm' for
2359 * there are *more* chunks to transfer. 'l' for it is the *last*
2360 * chunk of target description.
2362 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2363 &xml
, offset
, length
);
2364 if (retval
!= ERROR_OK
) {
2365 gdb_error(connection
, retval
);
2369 gdb_put_packet(connection
, xml
, strlen(xml
));
2373 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2374 gdb_connection
->noack_mode
= 1;
2375 gdb_put_packet(connection
, "OK", 2);
2379 gdb_put_packet(connection
, "", 0);
2383 static int gdb_v_packet(struct connection
*connection
,
2384 char *packet
, int packet_size
)
2386 struct gdb_connection
*gdb_connection
= connection
->priv
;
2387 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2390 /* if flash programming disabled - send a empty reply */
2392 if (gdb_flash_program
== 0) {
2393 gdb_put_packet(connection
, "", 0);
2397 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
2399 unsigned long length
;
2401 char *parse
= packet
+ 12;
2402 if (*parse
== '\0') {
2403 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2404 return ERROR_SERVER_REMOTE_CLOSED
;
2407 addr
= strtoul(parse
, &parse
, 16);
2409 if (*(parse
++) != ',' || *parse
== '\0') {
2410 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2411 return ERROR_SERVER_REMOTE_CLOSED
;
2414 length
= strtoul(parse
, &parse
, 16);
2416 if (*parse
!= '\0') {
2417 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2418 return ERROR_SERVER_REMOTE_CLOSED
;
2421 /* assume all sectors need erasing - stops any problems
2422 * when flash_write is called multiple times */
2425 /* perform any target specific operations before the erase */
2426 target_call_event_callbacks(gdb_service
->target
,
2427 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2429 /* vFlashErase:addr,length messages require region start and
2430 * end to be "block" aligned ... if padding is ever needed,
2431 * GDB will have become dangerously confused.
2433 result
= flash_erase_address_range(gdb_service
->target
,
2434 false, addr
, length
);
2436 /* perform any target specific operations after the erase */
2437 target_call_event_callbacks(gdb_service
->target
,
2438 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2441 if (result
!= ERROR_OK
) {
2442 /* GDB doesn't evaluate the actual error number returned,
2443 * treat a failed erase as an I/O error
2445 gdb_send_error(connection
, EIO
);
2446 LOG_ERROR("flash_erase returned %i", result
);
2448 gdb_put_packet(connection
, "OK", 2);
2453 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
2456 unsigned long length
;
2457 char *parse
= packet
+ 12;
2459 if (*parse
== '\0') {
2460 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2461 return ERROR_SERVER_REMOTE_CLOSED
;
2463 addr
= strtoul(parse
, &parse
, 16);
2464 if (*(parse
++) != ':') {
2465 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2466 return ERROR_SERVER_REMOTE_CLOSED
;
2468 length
= packet_size
- (parse
- packet
);
2470 /* create a new image if there isn't already one */
2471 if (gdb_connection
->vflash_image
== NULL
) {
2472 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2473 image_open(gdb_connection
->vflash_image
, "", "build");
2476 /* create new section with content from packet buffer */
2477 retval
= image_add_section(gdb_connection
->vflash_image
,
2478 addr
, length
, 0x0, (uint8_t *)parse
);
2479 if (retval
!= ERROR_OK
)
2482 gdb_put_packet(connection
, "OK", 2);
2487 if (strncmp(packet
, "vFlashDone", 10) == 0) {
2490 /* process the flashing buffer. No need to erase as GDB
2491 * always issues a vFlashErase first. */
2492 target_call_event_callbacks(gdb_service
->target
,
2493 TARGET_EVENT_GDB_FLASH_WRITE_START
);
2494 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2495 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2496 if (result
!= ERROR_OK
) {
2497 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2498 gdb_put_packet(connection
, "E.memtype", 9);
2500 gdb_send_error(connection
, EIO
);
2502 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2503 gdb_put_packet(connection
, "OK", 2);
2506 image_close(gdb_connection
->vflash_image
);
2507 free(gdb_connection
->vflash_image
);
2508 gdb_connection
->vflash_image
= NULL
;
2513 gdb_put_packet(connection
, "", 0);
2517 static int gdb_detach(struct connection
*connection
)
2519 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2521 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
2523 return gdb_put_packet(connection
, "OK", 2);
2526 /* The format of 'F' response packet is
2527 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2529 static int gdb_fileio_response_packet(struct connection
*connection
,
2530 char *packet
, int packet_size
)
2532 struct target
*target
= get_target_from_connection(connection
);
2534 char *parsing_point
;
2535 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
2536 int fileio_errno
= 0;
2537 bool fileio_ctrl_c
= false;
2542 if (*separator
== ',') {
2543 parsing_point
= separator
+ 1;
2544 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
2545 if (*separator
== ',') {
2546 if (*(separator
+ 1) == 'C') {
2547 /* TODO: process ctrl-c */
2548 fileio_ctrl_c
= true;
2553 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
2554 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
2556 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
2557 if (retval
!= ERROR_OK
)
2560 /* After File-I/O ends, keep continue or step */
2561 if (gdb_running_type
== 'c')
2562 retval
= target_resume(target
, 1, 0x0, 0, 0);
2563 else if (gdb_running_type
== 's')
2564 retval
= target_step(target
, 1, 0x0, 0);
2566 retval
= ERROR_FAIL
;
2568 if (retval
!= ERROR_OK
)
2574 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2575 const char *function
, const char *string
)
2577 struct connection
*connection
= priv
;
2578 struct gdb_connection
*gdb_con
= connection
->priv
;
2580 if (gdb_con
->busy
) {
2581 /* do not reply this using the O packet */
2585 gdb_output_con(connection
, string
);
2588 static void gdb_sig_halted(struct connection
*connection
)
2591 snprintf(sig_reply
, 4, "T%2.2x", 2);
2592 gdb_put_packet(connection
, sig_reply
, 3);
2595 static int gdb_input_inner(struct connection
*connection
)
2597 /* Do not allocate this on the stack */
2598 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2600 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2601 struct target
*target
= gdb_service
->target
;
2602 char *packet
= gdb_packet_buffer
;
2605 struct gdb_connection
*gdb_con
= connection
->priv
;
2606 static int extended_protocol
;
2608 /* drain input buffer. If one of the packets fail, then an error
2609 * packet is replied, if applicable.
2611 * This loop will terminate and the error code is returned.
2613 * The calling fn will check if this error is something that
2614 * can be recovered from, or if the connection must be closed.
2616 * If the error is recoverable, this fn is called again to
2617 * drain the rest of the buffer.
2620 packet_size
= GDB_BUFFER_SIZE
-1;
2621 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2622 if (retval
!= ERROR_OK
)
2625 /* terminate with zero */
2626 packet
[packet_size
] = 0;
2628 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2629 if (packet
[0] == 'X') {
2630 /* binary packets spew junk into the debug log stream */
2633 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
2636 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2638 LOG_DEBUG("received packet: '%s'", packet
);
2641 if (packet_size
> 0) {
2643 switch (packet
[0]) {
2644 case 'T': /* Is thread alive? */
2645 gdb_thread_packet(connection
, packet
, packet_size
);
2647 case 'H': /* Set current thread ( 'c' for step and continue,
2648 * 'g' for all other operations ) */
2649 gdb_thread_packet(connection
, packet
, packet_size
);
2653 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2654 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2655 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2658 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2661 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2664 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2667 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2670 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2673 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2677 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2680 gdb_last_signal_packet(connection
, packet
, packet_size
);
2685 gdb_thread_packet(connection
, packet
, packet_size
);
2686 log_add_callback(gdb_log_callback
, connection
);
2688 if (gdb_con
->mem_write_error
) {
2689 LOG_ERROR("Memory write failure!");
2691 /* now that we have reported the memory write error,
2692 * we can clear the condition */
2693 gdb_con
->mem_write_error
= false;
2696 bool nostep
= false;
2697 bool already_running
= false;
2698 if (target
->state
== TARGET_RUNNING
) {
2699 LOG_WARNING("WARNING! The target is already running. "
2700 "All changes GDB did to registers will be discarded! "
2701 "Waiting for target to halt.");
2702 already_running
= true;
2703 } else if (target
->state
!= TARGET_HALTED
) {
2704 LOG_WARNING("The target is not in the halted nor running stated, " \
2705 "stepi/continue ignored.");
2707 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
2708 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2709 * sent by GDB first to OpenOCD, thus defeating the check to
2710 * make only the single stepping have the sync feature...
2713 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2714 "from the target.");
2716 gdb_con
->sync
= false;
2718 if (!already_running
&& nostep
) {
2719 /* Either the target isn't in the halted state, then we can't
2720 * step/continue. This might be early setup, etc.
2722 * Or we want to allow GDB to pick up a fresh set of
2723 * register values without modifying the target state.
2726 gdb_sig_halted(connection
);
2728 /* stop forwarding log packets! */
2729 log_remove_callback(gdb_log_callback
, connection
);
2731 /* We're running/stepping, in which case we can
2732 * forward log output until the target is halted
2734 gdb_con
->frontend_state
= TARGET_RUNNING
;
2735 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2737 if (!already_running
) {
2738 /* Here we don't want packet processing to stop even if this fails,
2739 * so we use a local variable instead of retval. */
2740 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2741 if (retval
!= ERROR_OK
) {
2742 /* we'll never receive a halted
2743 * condition... issue a false one..
2745 gdb_frontend_halted(target
, connection
);
2752 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2755 retval
= gdb_detach(connection
);
2756 extended_protocol
= 0;
2759 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2760 if (retval
!= ERROR_OK
)
2764 if (extended_protocol
!= 0) {
2765 gdb_con
->attached
= false;
2768 gdb_put_packet(connection
, "OK", 2);
2769 return ERROR_SERVER_REMOTE_CLOSED
;
2771 /* handle extended remote protocol */
2772 extended_protocol
= 1;
2773 gdb_put_packet(connection
, "OK", 2);
2776 /* handle extended restart packet */
2777 breakpoint_clear_target(gdb_service
->target
);
2778 watchpoint_clear_target(gdb_service
->target
);
2779 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
2780 target_name(target
));
2781 /* set connection as attached after reset */
2782 gdb_con
->attached
= true;
2783 /* info rtos parts */
2784 gdb_thread_packet(connection
, packet
, packet_size
);
2788 /* packet supported only by smp target i.e cortex_a.c*/
2789 /* handle smp packet replying coreid played to gbd */
2790 gdb_read_smp_packet(connection
, packet
, packet_size
);
2794 /* packet supported only by smp target i.e cortex_a.c */
2795 /* handle smp packet setting coreid to be played at next
2797 gdb_write_smp_packet(connection
, packet
, packet_size
);
2801 /* File-I/O extension */
2802 /* After gdb uses host-side syscall to complete target file
2803 * I/O, gdb sends host-side syscall return value to target
2805 * The format of 'F' response packet is
2806 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2808 gdb_con
->frontend_state
= TARGET_RUNNING
;
2809 log_add_callback(gdb_log_callback
, connection
);
2810 gdb_fileio_response_packet(connection
, packet
, packet_size
);
2814 /* ignore unknown packets */
2815 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2816 gdb_put_packet(connection
, NULL
, 0);
2820 /* if a packet handler returned an error, exit input loop */
2821 if (retval
!= ERROR_OK
)
2825 if (gdb_con
->ctrl_c
) {
2826 if (target
->state
== TARGET_RUNNING
) {
2827 retval
= target_halt(target
);
2828 if (retval
!= ERROR_OK
)
2829 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2830 gdb_con
->ctrl_c
= 0;
2832 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2833 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2837 } while (gdb_con
->buf_cnt
> 0);
2842 static int gdb_input(struct connection
*connection
)
2844 int retval
= gdb_input_inner(connection
);
2845 struct gdb_connection
*gdb_con
= connection
->priv
;
2846 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2849 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2850 if (gdb_con
->closed
)
2851 return ERROR_SERVER_REMOTE_CLOSED
;
2853 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2857 static int gdb_target_start(struct target
*target
, const char *port
)
2859 struct gdb_service
*gdb_service
;
2861 gdb_service
= malloc(sizeof(struct gdb_service
));
2863 if (NULL
== gdb_service
)
2866 gdb_service
->target
= target
;
2867 gdb_service
->core
[0] = -1;
2868 gdb_service
->core
[1] = -1;
2869 target
->gdb_service
= gdb_service
;
2871 ret
= add_service("gdb",
2872 port
, 1, &gdb_new_connection
, &gdb_input
,
2873 &gdb_connection_closed
, gdb_service
);
2874 /* initialialize all targets gdb service with the same pointer */
2876 struct target_list
*head
;
2877 struct target
*curr
;
2878 head
= target
->head
;
2879 while (head
!= (struct target_list
*)NULL
) {
2880 curr
= head
->target
;
2882 curr
->gdb_service
= gdb_service
;
2889 static int gdb_target_add_one(struct target
*target
)
2891 /* one gdb instance per smp list */
2892 if ((target
->smp
) && (target
->gdb_service
))
2894 int retval
= gdb_target_start(target
, gdb_port_next
);
2895 if (retval
== ERROR_OK
) {
2897 /* If we can parse the port number
2898 * then we increment the port number for the next target.
2901 portnumber
= strtol(gdb_port_next
, &end
, 0);
2903 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
2904 free(gdb_port_next
);
2905 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2912 int gdb_target_add_all(struct target
*target
)
2914 if (NULL
== target
) {
2915 LOG_WARNING("gdb services need one or more targets defined");
2919 while (NULL
!= target
) {
2920 int retval
= gdb_target_add_one(target
);
2921 if (ERROR_OK
!= retval
)
2924 target
= target
->next
;
2930 COMMAND_HANDLER(handle_gdb_sync_command
)
2933 return ERROR_COMMAND_SYNTAX_ERROR
;
2935 if (current_gdb_connection
== NULL
) {
2936 command_print(CMD_CTX
,
2937 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2941 current_gdb_connection
->sync
= true;
2946 /* daemon configuration command gdb_port */
2947 COMMAND_HANDLER(handle_gdb_port_command
)
2949 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2950 if (ERROR_OK
== retval
) {
2951 free(gdb_port_next
);
2952 gdb_port_next
= strdup(gdb_port
);
2957 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2960 return ERROR_COMMAND_SYNTAX_ERROR
;
2962 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2966 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2969 return ERROR_COMMAND_SYNTAX_ERROR
;
2971 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2975 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2978 return ERROR_COMMAND_SYNTAX_ERROR
;
2980 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2984 /* gdb_breakpoint_override */
2985 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2987 if (CMD_ARGC
== 0) {
2989 } else if (CMD_ARGC
== 1) {
2990 gdb_breakpoint_override
= 1;
2991 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2992 gdb_breakpoint_override_type
= BKPT_HARD
;
2993 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2994 gdb_breakpoint_override_type
= BKPT_SOFT
;
2995 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2996 gdb_breakpoint_override
= 0;
2998 return ERROR_COMMAND_SYNTAX_ERROR
;
2999 if (gdb_breakpoint_override
)
3000 LOG_USER("force %s breakpoints",
3001 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3003 LOG_USER("breakpoint type is not overridden");
3008 COMMAND_HANDLER(handle_gdb_target_description_command
)
3011 return ERROR_COMMAND_SYNTAX_ERROR
;
3013 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3017 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3020 uint32_t tdesc_length
;
3021 struct target
*target
= get_current_target(CMD_CTX
);
3023 int retval
= gdb_generate_target_description(target
, &tdesc
);
3024 if (retval
!= ERROR_OK
) {
3025 LOG_ERROR("Unable to Generate Target Description");
3029 tdesc_length
= strlen(tdesc
);
3031 struct fileio fileio
;
3032 size_t size_written
;
3034 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3035 if (tdesc_filename
== NULL
) {
3036 retval
= ERROR_FAIL
;
3040 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
3042 if (retval
!= ERROR_OK
) {
3043 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
3047 retval
= fileio_write(&fileio
, tdesc_length
, tdesc
, &size_written
);
3049 fileio_close(&fileio
);
3051 if (retval
!= ERROR_OK
)
3052 LOG_ERROR("Error while writing the tdesc file");
3055 free(tdesc_filename
);
3061 static const struct command_registration gdb_command_handlers
[] = {
3064 .handler
= handle_gdb_sync_command
,
3065 .mode
= COMMAND_ANY
,
3066 .help
= "next stepi will return immediately allowing "
3067 "GDB to fetch register state without affecting "
3073 .handler
= handle_gdb_port_command
,
3074 .mode
= COMMAND_ANY
,
3075 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
3076 "server listens for the next port number after the "
3077 "base port number specified. "
3078 "No arguments reports GDB port. \"pipe\" means listen to stdin "
3079 "output to stdout, an integer is base port number, \"disable\" disables "
3080 "port. Any other string is are interpreted as named pipe to listen to. "
3081 "Output pipe is the same name as input pipe, but with 'o' appended.",
3082 .usage
= "[port_num]",
3085 .name
= "gdb_memory_map",
3086 .handler
= handle_gdb_memory_map_command
,
3087 .mode
= COMMAND_CONFIG
,
3088 .help
= "enable or disable memory map",
3089 .usage
= "('enable'|'disable')"
3092 .name
= "gdb_flash_program",
3093 .handler
= handle_gdb_flash_program_command
,
3094 .mode
= COMMAND_CONFIG
,
3095 .help
= "enable or disable flash program",
3096 .usage
= "('enable'|'disable')"
3099 .name
= "gdb_report_data_abort",
3100 .handler
= handle_gdb_report_data_abort_command
,
3101 .mode
= COMMAND_CONFIG
,
3102 .help
= "enable or disable reporting data aborts",
3103 .usage
= "('enable'|'disable')"
3106 .name
= "gdb_breakpoint_override",
3107 .handler
= handle_gdb_breakpoint_override_command
,
3108 .mode
= COMMAND_ANY
,
3109 .help
= "Display or specify type of breakpoint "
3110 "to be used by gdb 'break' commands.",
3111 .usage
= "('hard'|'soft'|'disable')"
3114 .name
= "gdb_target_description",
3115 .handler
= handle_gdb_target_description_command
,
3116 .mode
= COMMAND_CONFIG
,
3117 .help
= "enable or disable target description",
3118 .usage
= "('enable'|'disable')"
3121 .name
= "gdb_save_tdesc",
3122 .handler
= handle_gdb_save_tdesc_command
,
3123 .mode
= COMMAND_EXEC
,
3124 .help
= "Save the target description file",
3126 COMMAND_REGISTRATION_DONE
3129 int gdb_register_commands(struct command_context
*cmd_ctx
)
3131 gdb_port
= strdup("3333");
3132 gdb_port_next
= strdup("3333");
3133 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);