1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * Copyright (C) 2011 by Broadcom Corporation *
12 * Evan Hunter - ehunter@broadcom.com *
14 * Copyright (C) ST-Ericsson SA 2011 *
15 * michel.jaouen@stericsson.com : smp minimum support *
17 * Copyright (C) 2013 Andes Technology *
18 * Hsiangkai Wang <hkwang@andestech.com> *
20 * Copyright (C) 2013 Franck Jullien *
21 * elec4fun@gmail.com *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
35 ***************************************************************************/
41 #include <target/breakpoints.h>
42 #include <target/target_request.h>
43 #include <target/register.h>
45 #include <flash/nor/core.h>
46 #include "gdb_server.h"
47 #include <target/image.h>
48 #include <jtag/jtag.h>
49 #include "rtos/rtos.h"
50 #include "target/smp.h"
54 * GDB server implementation.
56 * This implements the GDB Remote Serial Protocol, over TCP connections,
57 * giving GDB access to the JTAG or other hardware debugging facilities
58 * found in most modern embedded processors.
61 struct target_desc_format
{
63 uint32_t tdesc_length
;
66 /* private connection data for GDB */
67 struct gdb_connection
{
68 char buffer
[GDB_BUFFER_SIZE
];
72 enum target_state frontend_state
;
73 struct image
*vflash_image
;
77 /* set flag to true if you want the next stepi to return immediately.
78 * allowing GDB to pick up a fresh set of register values from the target
79 * without modifying the target state. */
81 /* We delay reporting memory write errors until next step/continue or memory
82 * write. This improves performance of gdb load significantly as the GDB packet
83 * can be replied immediately and a new GDB packet will be ready without delay
84 * (ca. 10% or so...). */
86 /* with extended-remote it seems we need to better emulate attach/detach.
87 * what this means is we reply with a W stop reply after a kill packet,
88 * normally we reply with a S reply via gdb_last_signal_packet.
89 * as a side note this behaviour only effects gdb > 6.8 */
91 /* temporarily used for target description support */
92 struct target_desc_format target_desc
;
93 /* temporarily used for thread list support */
98 #define _DEBUG_GDB_IO_
101 static struct gdb_connection
*current_gdb_connection
;
103 static int gdb_breakpoint_override
;
104 static enum breakpoint_type gdb_breakpoint_override_type
;
106 static int gdb_error(struct connection
*connection
, int retval
);
107 static char *gdb_port
;
108 static char *gdb_port_next
;
110 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
111 const char *function
, const char *string
);
113 /* number of gdb connections, mainly to suppress gdb related debugging spam
114 * in helper/log.c when no gdb connections are actually active */
115 int gdb_actual_connections
;
117 /* set if we are sending a memory map to gdb
118 * via qXfer:memory-map:read packet */
119 /* enabled by default*/
120 static int gdb_use_memory_map
= 1;
121 /* enabled by default*/
122 static int gdb_flash_program
= 1;
124 /* if set, data aborts cause an error to be reported in memory read packets
125 * see the code in gdb_read_memory_packet() for further explanations.
126 * Disabled by default.
128 static int gdb_report_data_abort
;
130 /* set if we are sending target descriptions to gdb
131 * via qXfer:features:read packet */
132 /* enabled by default */
133 static int gdb_use_target_description
= 1;
135 /* current processing free-run type, used by file-I/O */
136 static char gdb_running_type
;
138 static int gdb_last_signal(struct target
*target
)
140 switch (target
->debug_reason
) {
141 case DBG_REASON_DBGRQ
:
142 return 0x2; /* SIGINT */
143 case DBG_REASON_BREAKPOINT
:
144 case DBG_REASON_WATCHPOINT
:
145 case DBG_REASON_WPTANDBKPT
:
146 return 0x05; /* SIGTRAP */
147 case DBG_REASON_SINGLESTEP
:
148 return 0x05; /* SIGTRAP */
149 case DBG_REASON_NOTHALTED
:
150 return 0x0; /* no signal... shouldn't happen */
152 LOG_USER("undefined debug reason %d - target needs reset",
153 target
->debug_reason
);
158 static int check_pending(struct connection
*connection
,
159 int timeout_s
, int *got_data
)
161 /* a non-blocking socket will block if there is 0 bytes available on the socket,
162 * but return with as many bytes as are available immediately
166 struct gdb_connection
*gdb_con
= connection
->priv
;
168 if (got_data
== NULL
)
172 if (gdb_con
->buf_cnt
> 0) {
178 FD_SET(connection
->fd
, &read_fds
);
180 tv
.tv_sec
= timeout_s
;
182 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
183 /* This can typically be because a "monitor" command took too long
184 * before printing any progress messages
187 return ERROR_GDB_TIMEOUT
;
191 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
195 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
197 struct gdb_connection
*gdb_con
= connection
->priv
;
198 int retval
= ERROR_OK
;
200 #ifdef _DEBUG_GDB_IO_
204 if (connection
->service
->type
!= CONNECTION_TCP
)
205 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
207 retval
= check_pending(connection
, 1, NULL
);
208 if (retval
!= ERROR_OK
)
210 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
215 if (gdb_con
->buf_cnt
> 0)
217 if (gdb_con
->buf_cnt
== 0) {
219 return ERROR_SERVER_REMOTE_CLOSED
;
223 errno
= WSAGetLastError();
229 case WSAECONNABORTED
:
231 return ERROR_SERVER_REMOTE_CLOSED
;
234 return ERROR_SERVER_REMOTE_CLOSED
;
236 LOG_ERROR("read: %d", errno
);
246 return ERROR_SERVER_REMOTE_CLOSED
;
249 return ERROR_SERVER_REMOTE_CLOSED
;
251 LOG_ERROR("read: %s", strerror(errno
));
253 return ERROR_SERVER_REMOTE_CLOSED
;
258 #ifdef _DEBUG_GDB_IO_
259 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
260 LOG_DEBUG("received '%s'", debug_buffer
);
264 gdb_con
->buf_p
= gdb_con
->buffer
;
266 *next_char
= *(gdb_con
->buf_p
++);
267 if (gdb_con
->buf_cnt
> 0)
268 connection
->input_pending
= 1;
270 connection
->input_pending
= 0;
271 #ifdef _DEBUG_GDB_IO_
272 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
279 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
280 * held in registers in the inner loop.
282 * For small caches and embedded systems this is important!
284 static inline int gdb_get_char_fast(struct connection
*connection
,
285 int *next_char
, char **buf_p
, int *buf_cnt
)
287 int retval
= ERROR_OK
;
289 if ((*buf_cnt
)-- > 0) {
290 *next_char
= **buf_p
;
293 connection
->input_pending
= 1;
295 connection
->input_pending
= 0;
297 #ifdef _DEBUG_GDB_IO_
298 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
304 struct gdb_connection
*gdb_con
= connection
->priv
;
305 gdb_con
->buf_p
= *buf_p
;
306 gdb_con
->buf_cnt
= *buf_cnt
;
307 retval
= gdb_get_char_inner(connection
, next_char
);
308 *buf_p
= gdb_con
->buf_p
;
309 *buf_cnt
= gdb_con
->buf_cnt
;
314 static int gdb_get_char(struct connection
*connection
, int *next_char
)
316 struct gdb_connection
*gdb_con
= connection
->priv
;
317 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
320 static int gdb_putback_char(struct connection
*connection
, int last_char
)
322 struct gdb_connection
*gdb_con
= connection
->priv
;
324 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
325 *(--gdb_con
->buf_p
) = last_char
;
328 LOG_ERROR("BUG: couldn't put character back");
333 /* The only way we can detect that the socket is closed is the first time
334 * we write to it, we will fail. Subsequent write operations will
335 * succeed. Shudder! */
336 static int gdb_write(struct connection
*connection
, void *data
, int len
)
338 struct gdb_connection
*gdb_con
= connection
->priv
;
340 return ERROR_SERVER_REMOTE_CLOSED
;
342 if (connection_write(connection
, data
, len
) == len
)
345 return ERROR_SERVER_REMOTE_CLOSED
;
348 static int gdb_put_packet_inner(struct connection
*connection
,
349 char *buffer
, int len
)
352 unsigned char my_checksum
= 0;
353 #ifdef _DEBUG_GDB_IO_
358 struct gdb_connection
*gdb_con
= connection
->priv
;
360 for (i
= 0; i
< len
; i
++)
361 my_checksum
+= buffer
[i
];
363 #ifdef _DEBUG_GDB_IO_
365 * At this point we should have nothing in the input queue from GDB,
366 * however sometimes '-' is sent even though we've already received
367 * an ACK (+) for everything we've sent off.
371 retval
= check_pending(connection
, 0, &gotdata
);
372 if (retval
!= ERROR_OK
)
376 retval
= gdb_get_char(connection
, &reply
);
377 if (retval
!= ERROR_OK
)
380 /* fix a problem with some IAR tools */
381 gdb_putback_char(connection
, reply
);
382 LOG_DEBUG("Unexpected start of new packet");
386 LOG_WARNING("Discard unexpected char %c", reply
);
391 #ifdef _DEBUG_GDB_IO_
392 debug_buffer
= strndup(buffer
, len
);
393 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
397 char local_buffer
[1024];
398 local_buffer
[0] = '$';
399 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
400 /* performance gain on smaller packets by only a single call to gdb_write() */
401 memcpy(local_buffer
+ 1, buffer
, len
++);
402 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
403 retval
= gdb_write(connection
, local_buffer
, len
);
404 if (retval
!= ERROR_OK
)
407 /* larger packets are transmitted directly from caller supplied buffer
408 * by several calls to gdb_write() to avoid dynamic allocation */
409 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
410 retval
= gdb_write(connection
, local_buffer
, 1);
411 if (retval
!= ERROR_OK
)
413 retval
= gdb_write(connection
, buffer
, len
);
414 if (retval
!= ERROR_OK
)
416 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
417 if (retval
!= ERROR_OK
)
421 if (gdb_con
->noack_mode
)
424 retval
= gdb_get_char(connection
, &reply
);
425 if (retval
!= ERROR_OK
)
430 else if (reply
== '-') {
431 /* Stop sending output packets for now */
432 log_remove_callback(gdb_log_callback
, connection
);
433 LOG_WARNING("negative reply, retrying");
434 } else if (reply
== 0x3) {
436 retval
= gdb_get_char(connection
, &reply
);
437 if (retval
!= ERROR_OK
)
441 else if (reply
== '-') {
442 /* Stop sending output packets for now */
443 log_remove_callback(gdb_log_callback
, connection
);
444 LOG_WARNING("negative reply, retrying");
445 } else if (reply
== '$') {
446 LOG_ERROR("GDB missing ack(1) - assumed good");
447 gdb_putback_char(connection
, reply
);
450 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
452 return ERROR_SERVER_REMOTE_CLOSED
;
454 } else if (reply
== '$') {
455 LOG_ERROR("GDB missing ack(2) - assumed good");
456 gdb_putback_char(connection
, reply
);
459 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
462 return ERROR_SERVER_REMOTE_CLOSED
;
466 return ERROR_SERVER_REMOTE_CLOSED
;
471 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
473 struct gdb_connection
*gdb_con
= connection
->priv
;
475 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
478 /* we sent some data, reset timer for keep alive messages */
484 static inline int fetch_packet(struct connection
*connection
,
485 int *checksum_ok
, int noack
, int *len
, char *buffer
)
487 unsigned char my_checksum
= 0;
490 int retval
= ERROR_OK
;
492 struct gdb_connection
*gdb_con
= connection
->priv
;
497 /* move this over into local variables to use registers and give the
498 * more freedom to optimize */
499 char *buf_p
= gdb_con
->buf_p
;
500 int buf_cnt
= gdb_con
->buf_cnt
;
503 /* The common case is that we have an entire packet with no escape chars.
504 * We need to leave at least 2 bytes in the buffer to have
505 * gdb_get_char() update various bits and bobs correctly.
507 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
508 /* The compiler will struggle a bit with constant propagation and
509 * aliasing, so we help it by showing that these values do not
510 * change inside the loop
514 int run
= buf_cnt
- 2;
520 if (character
== '#') {
521 /* Danger! character can be '#' when esc is
522 * used so we need an explicit boolean for done here. */
527 if (character
== '}') {
528 /* data transmitted in binary mode (X packet)
529 * uses 0x7d as escape character */
530 my_checksum
+= character
& 0xff;
533 my_checksum
+= character
& 0xff;
534 buffer
[count
++] = (character
^ 0x20) & 0xff;
536 my_checksum
+= character
& 0xff;
537 buffer
[count
++] = character
& 0xff;
546 LOG_ERROR("packet buffer too small");
547 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
551 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
552 if (retval
!= ERROR_OK
)
555 if (character
== '#')
558 if (character
== '}') {
559 /* data transmitted in binary mode (X packet)
560 * uses 0x7d as escape character */
561 my_checksum
+= character
& 0xff;
563 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
564 if (retval
!= ERROR_OK
)
567 my_checksum
+= character
& 0xff;
568 buffer
[count
++] = (character
^ 0x20) & 0xff;
570 my_checksum
+= character
& 0xff;
571 buffer
[count
++] = character
& 0xff;
575 gdb_con
->buf_p
= buf_p
;
576 gdb_con
->buf_cnt
= buf_cnt
;
578 if (retval
!= ERROR_OK
)
583 retval
= gdb_get_char(connection
, &character
);
584 if (retval
!= ERROR_OK
)
586 checksum
[0] = character
;
587 retval
= gdb_get_char(connection
, &character
);
588 if (retval
!= ERROR_OK
)
590 checksum
[1] = character
;
594 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
599 static int gdb_get_packet_inner(struct connection
*connection
,
600 char *buffer
, int *len
)
604 struct gdb_connection
*gdb_con
= connection
->priv
;
608 retval
= gdb_get_char(connection
, &character
);
609 if (retval
!= ERROR_OK
)
612 #ifdef _DEBUG_GDB_IO_
613 LOG_DEBUG("character: '%c'", character
);
620 /* According to the GDB documentation
621 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
622 * "gdb sends a final `+` acknowledgment of the stub's `OK`
623 * response, which can be safely ignored by the stub."
624 * However OpenOCD server already is in noack mode at this
625 * point and instead of ignoring this it was emitting a
626 * warning. This code makes server ignore the first ACK
627 * that will be received after going into noack mode,
628 * warning only about subsequent ACK's. */
629 if (gdb_con
->noack_mode
> 1) {
630 LOG_WARNING("acknowledgment received, but no packet pending");
631 } else if (gdb_con
->noack_mode
) {
632 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
633 gdb_con
->noack_mode
= 2;
637 LOG_WARNING("negative acknowledgment, but no packet pending");
644 LOG_WARNING("ignoring character 0x%x", character
);
647 } while (character
!= '$');
650 /* explicit code expansion here to get faster inlined code in -O3 by not
651 * calculating checksum */
652 if (gdb_con
->noack_mode
) {
653 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
654 if (retval
!= ERROR_OK
)
657 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
658 if (retval
!= ERROR_OK
)
662 if (gdb_con
->noack_mode
) {
663 /* checksum is not checked in noack mode */
667 retval
= gdb_write(connection
, "+", 1);
668 if (retval
!= ERROR_OK
)
674 return ERROR_SERVER_REMOTE_CLOSED
;
679 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
681 struct gdb_connection
*gdb_con
= connection
->priv
;
683 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
688 static int gdb_output_con(struct connection
*connection
, const char *line
)
693 bin_size
= strlen(line
);
695 hex_buffer
= malloc(bin_size
* 2 + 2);
696 if (hex_buffer
== NULL
)
697 return ERROR_GDB_BUFFER_TOO_SMALL
;
700 int pkt_len
= hexify(hex_buffer
+ 1, line
, bin_size
, bin_size
* 2 + 1);
701 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
707 static int gdb_output(struct command_context
*context
, const char *line
)
709 /* this will be dumped to the log and also sent as an O packet if possible */
710 LOG_USER_N("%s", line
);
714 static void gdb_signal_reply(struct target
*target
, struct connection
*connection
)
716 struct gdb_connection
*gdb_connection
= connection
->priv
;
718 char stop_reason
[20];
719 char current_thread
[25];
723 rtos_update_threads(target
);
725 if (target
->debug_reason
== DBG_REASON_EXIT
) {
726 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "W00");
728 if (gdb_connection
->ctrl_c
) {
730 gdb_connection
->ctrl_c
= 0;
732 signal_var
= gdb_last_signal(target
);
734 stop_reason
[0] = '\0';
735 if (target
->debug_reason
== DBG_REASON_WATCHPOINT
) {
736 enum watchpoint_rw hit_wp_type
;
737 uint32_t hit_wp_address
;
739 if (watchpoint_hit(target
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
741 switch (hit_wp_type
) {
743 snprintf(stop_reason
, sizeof(stop_reason
),
744 "watch:%08" PRIx32
";", hit_wp_address
);
747 snprintf(stop_reason
, sizeof(stop_reason
),
748 "rwatch:%08" PRIx32
";", hit_wp_address
);
751 snprintf(stop_reason
, sizeof(stop_reason
),
752 "awatch:%08" PRIx32
";", hit_wp_address
);
760 current_thread
[0] = '\0';
761 if (target
->rtos
!= NULL
) {
762 snprintf(current_thread
, sizeof(current_thread
), "thread:%016" PRIx64
";", target
->rtos
->current_thread
);
763 target
->rtos
->current_threadid
= target
->rtos
->current_thread
;
766 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s%s",
767 signal_var
, stop_reason
, current_thread
);
770 gdb_put_packet(connection
, sig_reply
, sig_reply_len
);
771 gdb_connection
->frontend_state
= TARGET_HALTED
;
774 static void gdb_fileio_reply(struct target
*target
, struct connection
*connection
)
776 struct gdb_connection
*gdb_connection
= connection
->priv
;
777 char fileio_command
[256];
779 bool program_exited
= false;
781 if (strcmp(target
->fileio_info
->identifier
, "open") == 0)
782 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
",%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
783 target
->fileio_info
->param_1
,
784 target
->fileio_info
->param_2
,
785 target
->fileio_info
->param_3
,
786 target
->fileio_info
->param_4
);
787 else if (strcmp(target
->fileio_info
->identifier
, "close") == 0)
788 sprintf(fileio_command
, "F%s,%" PRIx32
, target
->fileio_info
->identifier
,
789 target
->fileio_info
->param_1
);
790 else if (strcmp(target
->fileio_info
->identifier
, "read") == 0)
791 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
792 target
->fileio_info
->param_1
,
793 target
->fileio_info
->param_2
,
794 target
->fileio_info
->param_3
);
795 else if (strcmp(target
->fileio_info
->identifier
, "write") == 0)
796 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
797 target
->fileio_info
->param_1
,
798 target
->fileio_info
->param_2
,
799 target
->fileio_info
->param_3
);
800 else if (strcmp(target
->fileio_info
->identifier
, "lseek") == 0)
801 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
802 target
->fileio_info
->param_1
,
803 target
->fileio_info
->param_2
,
804 target
->fileio_info
->param_3
);
805 else if (strcmp(target
->fileio_info
->identifier
, "rename") == 0)
806 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
",%" PRIx32
"/%" PRIx32
, target
->fileio_info
->identifier
,
807 target
->fileio_info
->param_1
,
808 target
->fileio_info
->param_2
,
809 target
->fileio_info
->param_3
,
810 target
->fileio_info
->param_4
);
811 else if (strcmp(target
->fileio_info
->identifier
, "unlink") == 0)
812 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
, target
->fileio_info
->identifier
,
813 target
->fileio_info
->param_1
,
814 target
->fileio_info
->param_2
);
815 else if (strcmp(target
->fileio_info
->identifier
, "stat") == 0)
816 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
817 target
->fileio_info
->param_1
,
818 target
->fileio_info
->param_2
,
819 target
->fileio_info
->param_3
);
820 else if (strcmp(target
->fileio_info
->identifier
, "fstat") == 0)
821 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
822 target
->fileio_info
->param_1
,
823 target
->fileio_info
->param_2
);
824 else if (strcmp(target
->fileio_info
->identifier
, "gettimeofday") == 0)
825 sprintf(fileio_command
, "F%s,%" PRIx32
",%" PRIx32
, target
->fileio_info
->identifier
,
826 target
->fileio_info
->param_1
,
827 target
->fileio_info
->param_2
);
828 else if (strcmp(target
->fileio_info
->identifier
, "isatty") == 0)
829 sprintf(fileio_command
, "F%s,%" PRIx32
, target
->fileio_info
->identifier
,
830 target
->fileio_info
->param_1
);
831 else if (strcmp(target
->fileio_info
->identifier
, "system") == 0)
832 sprintf(fileio_command
, "F%s,%" PRIx32
"/%" PRIx32
, target
->fileio_info
->identifier
,
833 target
->fileio_info
->param_1
,
834 target
->fileio_info
->param_2
);
835 else if (strcmp(target
->fileio_info
->identifier
, "exit") == 0) {
836 /* If target hits exit syscall, report to GDB the program is terminated.
837 * In addition, let target run its own exit syscall handler. */
838 program_exited
= true;
839 sprintf(fileio_command
, "W%02" PRIx32
, target
->fileio_info
->param_1
);
841 LOG_DEBUG("Unknown syscall: %s", target
->fileio_info
->identifier
);
843 /* encounter unknown syscall, continue */
844 gdb_connection
->frontend_state
= TARGET_RUNNING
;
845 target_resume(target
, 1, 0x0, 0, 0);
849 command_len
= strlen(fileio_command
);
850 gdb_put_packet(connection
, fileio_command
, command_len
);
852 if (program_exited
) {
853 /* Use target_resume() to let target run its own exit syscall handler. */
854 gdb_connection
->frontend_state
= TARGET_RUNNING
;
855 target_resume(target
, 1, 0x0, 0, 0);
857 gdb_connection
->frontend_state
= TARGET_HALTED
;
858 rtos_update_threads(target
);
862 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
864 struct gdb_connection
*gdb_connection
= connection
->priv
;
866 /* In the GDB protocol when we are stepping or continuing execution,
867 * we have a lingering reply. Upon receiving a halted event
868 * when we have that lingering packet, we reply to the original
869 * step or continue packet.
871 * Executing monitor commands can bring the target in and
872 * out of the running state so we'll see lots of TARGET_EVENT_XXX
873 * that are to be ignored.
875 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
876 /* stop forwarding log packets! */
877 log_remove_callback(gdb_log_callback
, connection
);
879 /* check fileio first */
880 if (target_get_gdb_fileio_info(target
, target
->fileio_info
) == ERROR_OK
)
881 gdb_fileio_reply(target
, connection
);
883 gdb_signal_reply(target
, connection
);
887 static int gdb_target_callback_event_handler(struct target
*target
,
888 enum target_event event
, void *priv
)
891 struct connection
*connection
= priv
;
892 struct gdb_service
*gdb_service
= connection
->service
->priv
;
894 if (gdb_service
->target
!= target
)
898 case TARGET_EVENT_GDB_HALT
:
899 gdb_frontend_halted(target
, connection
);
901 case TARGET_EVENT_HALTED
:
902 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
904 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
905 retval
= jtag_execute_queue();
906 if (retval
!= ERROR_OK
)
916 static int gdb_new_connection(struct connection
*connection
)
918 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
919 struct gdb_service
*gdb_service
= connection
->service
->priv
;
923 connection
->priv
= gdb_connection
;
925 /* initialize gdb connection information */
926 gdb_connection
->buf_p
= gdb_connection
->buffer
;
927 gdb_connection
->buf_cnt
= 0;
928 gdb_connection
->ctrl_c
= 0;
929 gdb_connection
->frontend_state
= TARGET_HALTED
;
930 gdb_connection
->vflash_image
= NULL
;
931 gdb_connection
->closed
= 0;
932 gdb_connection
->busy
= 0;
933 gdb_connection
->noack_mode
= 0;
934 gdb_connection
->sync
= false;
935 gdb_connection
->mem_write_error
= false;
936 gdb_connection
->attached
= true;
937 gdb_connection
->target_desc
.tdesc
= NULL
;
938 gdb_connection
->target_desc
.tdesc_length
= 0;
939 gdb_connection
->thread_list
= NULL
;
941 /* send ACK to GDB for debug request */
942 gdb_write(connection
, "+", 1);
944 /* output goes through gdb connection */
945 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
947 /* we must remove all breakpoints registered to the target as a previous
948 * GDB session could leave dangling breakpoints if e.g. communication
951 breakpoint_clear_target(gdb_service
->target
);
952 watchpoint_clear_target(gdb_service
->target
);
954 /* clean previous rtos session if supported*/
955 if ((gdb_service
->target
->rtos
) && (gdb_service
->target
->rtos
->type
->clean
))
956 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
958 /* remove the initial ACK from the incoming buffer */
959 retval
= gdb_get_char(connection
, &initial_ack
);
960 if (retval
!= ERROR_OK
)
963 /* FIX!!!??? would we actually ever receive a + here???
966 if (initial_ack
!= '+')
967 gdb_putback_char(connection
, initial_ack
);
968 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
970 if (gdb_use_memory_map
) {
971 /* Connect must fail if the memory map can't be set up correctly.
973 * This will cause an auto_probe to be invoked, which is either
974 * a no-op or it will fail when the target isn't ready(e.g. not halted).
977 for (i
= 0; i
< flash_get_bank_count(); i
++) {
978 struct flash_bank
*p
;
979 p
= get_flash_bank_by_num_noprobe(i
);
980 if (p
->target
!= gdb_service
->target
)
982 retval
= get_flash_bank_by_num(i
, &p
);
983 if (retval
!= ERROR_OK
) {
984 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
985 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
991 gdb_actual_connections
++;
992 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
993 gdb_actual_connections
,
994 target_name(gdb_service
->target
),
995 target_state_name(gdb_service
->target
));
997 /* DANGER! If we fail subsequently, we must remove this handler,
998 * otherwise we occasionally see crashes as the timer can invoke the
1001 * register callback to be informed about target events */
1002 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
1007 static int gdb_connection_closed(struct connection
*connection
)
1009 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1010 struct gdb_connection
*gdb_connection
= connection
->priv
;
1012 /* we're done forwarding messages. Tear down callback before
1013 * cleaning up connection.
1015 log_remove_callback(gdb_log_callback
, connection
);
1017 gdb_actual_connections
--;
1018 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1019 target_name(gdb_service
->target
),
1020 target_state_name(gdb_service
->target
),
1021 gdb_actual_connections
);
1023 /* see if an image built with vFlash commands is left */
1024 if (gdb_connection
->vflash_image
) {
1025 image_close(gdb_connection
->vflash_image
);
1026 free(gdb_connection
->vflash_image
);
1027 gdb_connection
->vflash_image
= NULL
;
1030 /* if this connection registered a debug-message receiver delete it */
1031 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
1033 if (connection
->priv
) {
1034 free(connection
->priv
);
1035 connection
->priv
= NULL
;
1037 LOG_ERROR("BUG: connection->priv == NULL");
1039 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
1041 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
1043 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
1048 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
1051 snprintf(err
, 4, "E%2.2X", the_error
);
1052 gdb_put_packet(connection
, err
, 3);
1055 static int gdb_last_signal_packet(struct connection
*connection
,
1056 char const *packet
, int packet_size
)
1058 struct target
*target
= get_target_from_connection(connection
);
1059 struct gdb_connection
*gdb_con
= connection
->priv
;
1063 if (!gdb_con
->attached
) {
1064 /* if we are here we have received a kill packet
1065 * reply W stop reply otherwise gdb gets very unhappy */
1066 gdb_put_packet(connection
, "W00", 3);
1070 signal_var
= gdb_last_signal(target
);
1072 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
1073 gdb_put_packet(connection
, sig_reply
, 3);
1078 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
1080 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
1083 return len
- 1 - pos
;
1086 /* Convert register to string of bytes. NB! The # of bits in the
1087 * register might be non-divisible by 8(a byte), in which
1088 * case an entire byte is shown.
1090 * NB! the format on the wire is the target endianness
1092 * The format of reg->value is little endian
1095 static void gdb_str_to_target(struct target
*target
,
1096 char *tstr
, struct reg
*reg
)
1103 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
1105 for (i
= 0; i
< buf_len
; i
++) {
1106 int j
= gdb_reg_pos(target
, i
, buf_len
);
1107 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
1111 /* copy over in register buffer */
1112 static void gdb_target_to_reg(struct target
*target
,
1113 char const *tstr
, int str_len
, uint8_t *bin
)
1116 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1121 for (i
= 0; i
< str_len
; i
+= 2) {
1123 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
1124 LOG_ERROR("BUG: unable to convert register value");
1128 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1133 static int gdb_get_registers_packet(struct connection
*connection
,
1134 char const *packet
, int packet_size
)
1136 struct target
*target
= get_target_from_connection(connection
);
1137 struct reg
**reg_list
;
1140 int reg_packet_size
= 0;
1145 #ifdef _DEBUG_GDB_IO_
1149 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1152 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1154 if (retval
!= ERROR_OK
)
1155 return gdb_error(connection
, retval
);
1157 for (i
= 0; i
< reg_list_size
; i
++)
1158 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1160 assert(reg_packet_size
> 0);
1162 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1163 if (reg_packet
== NULL
)
1166 reg_packet_p
= reg_packet
;
1168 for (i
= 0; i
< reg_list_size
; i
++) {
1169 if (!reg_list
[i
]->valid
)
1170 reg_list
[i
]->type
->get(reg_list
[i
]);
1171 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1172 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1175 #ifdef _DEBUG_GDB_IO_
1177 char *reg_packet_p_debug
;
1178 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1179 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1180 free(reg_packet_p_debug
);
1184 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1192 static int gdb_set_registers_packet(struct connection
*connection
,
1193 char const *packet
, int packet_size
)
1195 struct target
*target
= get_target_from_connection(connection
);
1197 struct reg
**reg_list
;
1200 char const *packet_p
;
1202 #ifdef _DEBUG_GDB_IO_
1206 /* skip command character */
1210 if (packet_size
% 2) {
1211 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1212 return ERROR_SERVER_REMOTE_CLOSED
;
1215 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1217 if (retval
!= ERROR_OK
)
1218 return gdb_error(connection
, retval
);
1221 for (i
= 0; i
< reg_list_size
; i
++) {
1223 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1225 if (packet_p
+ chars
> packet
+ packet_size
)
1226 LOG_ERROR("BUG: register packet is too small for registers");
1228 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1229 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1231 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1233 /* advance packet pointer */
1239 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1242 gdb_put_packet(connection
, "OK", 2);
1247 static int gdb_get_register_packet(struct connection
*connection
,
1248 char const *packet
, int packet_size
)
1250 struct target
*target
= get_target_from_connection(connection
);
1252 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1253 struct reg
**reg_list
;
1257 #ifdef _DEBUG_GDB_IO_
1261 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1263 if (retval
!= ERROR_OK
)
1264 return gdb_error(connection
, retval
);
1266 if (reg_list_size
<= reg_num
) {
1267 LOG_ERROR("gdb requested a non-existing register");
1268 return ERROR_SERVER_REMOTE_CLOSED
;
1271 if (!reg_list
[reg_num
]->valid
)
1272 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1274 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1); /* plus one for string termination null */
1276 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1278 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1286 static int gdb_set_register_packet(struct connection
*connection
,
1287 char const *packet
, int packet_size
)
1289 struct target
*target
= get_target_from_connection(connection
);
1292 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1293 struct reg
**reg_list
;
1299 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1301 if (retval
!= ERROR_OK
)
1302 return gdb_error(connection
, retval
);
1304 if (reg_list_size
<= reg_num
) {
1305 LOG_ERROR("gdb requested a non-existing register");
1306 return ERROR_SERVER_REMOTE_CLOSED
;
1309 if (*separator
!= '=') {
1310 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1311 return ERROR_SERVER_REMOTE_CLOSED
;
1314 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1315 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1316 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1318 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1319 LOG_ERROR("gdb sent a packet with wrong register size");
1321 return ERROR_SERVER_REMOTE_CLOSED
;
1324 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1326 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1328 gdb_put_packet(connection
, "OK", 2);
1336 /* No attempt is made to translate the "retval" to
1337 * GDB speak. This has to be done at the calling
1338 * site as no mapping really exists.
1340 static int gdb_error(struct connection
*connection
, int retval
)
1342 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1343 gdb_send_error(connection
, EFAULT
);
1347 /* We don't have to worry about the default 2 second timeout for GDB packets,
1348 * because GDB breaks up large memory reads into smaller reads.
1350 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1352 static int gdb_read_memory_packet(struct connection
*connection
,
1353 char const *packet
, int packet_size
)
1355 struct target
*target
= get_target_from_connection(connection
);
1363 int retval
= ERROR_OK
;
1365 /* skip command character */
1368 addr
= strtoul(packet
, &separator
, 16);
1370 if (*separator
!= ',') {
1371 LOG_ERROR("incomplete read memory packet received, dropping connection");
1372 return ERROR_SERVER_REMOTE_CLOSED
;
1375 len
= strtoul(separator
+ 1, NULL
, 16);
1378 LOG_WARNING("invalid read memory packet received (len == 0)");
1379 gdb_put_packet(connection
, NULL
, 0);
1383 buffer
= malloc(len
);
1385 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1387 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1389 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1390 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1391 * At some point this might be fixed in GDB, in which case this code can be removed.
1393 * OpenOCD developers are acutely aware of this problem, but there is nothing
1394 * gained by involving the user in this problem that hopefully will get resolved
1397 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1398 * cmd = view%20audit-trail&database = gdb&pr = 2395
1400 * For now, the default is to fix up things to make current GDB versions work.
1401 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1403 memset(buffer
, 0, len
);
1407 if (retval
== ERROR_OK
) {
1408 hex_buffer
= malloc(len
* 2 + 1);
1410 int pkt_len
= hexify(hex_buffer
, (char *)buffer
, len
, len
* 2 + 1);
1412 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1416 retval
= gdb_error(connection
, retval
);
1423 static int gdb_write_memory_packet(struct connection
*connection
,
1424 char const *packet
, int packet_size
)
1426 struct target
*target
= get_target_from_connection(connection
);
1434 /* skip command character */
1437 addr
= strtoul(packet
, &separator
, 16);
1439 if (*separator
!= ',') {
1440 LOG_ERROR("incomplete write memory packet received, dropping connection");
1441 return ERROR_SERVER_REMOTE_CLOSED
;
1444 len
= strtoul(separator
+ 1, &separator
, 16);
1446 if (*(separator
++) != ':') {
1447 LOG_ERROR("incomplete write memory packet received, dropping connection");
1448 return ERROR_SERVER_REMOTE_CLOSED
;
1451 buffer
= malloc(len
);
1453 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1455 if (unhexify(buffer
, separator
, len
) != len
)
1456 LOG_ERROR("unable to decode memory packet");
1458 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1460 if (retval
== ERROR_OK
)
1461 gdb_put_packet(connection
, "OK", 2);
1463 retval
= gdb_error(connection
, retval
);
1470 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1471 char const *packet
, int packet_size
)
1473 struct target
*target
= get_target_from_connection(connection
);
1478 int retval
= ERROR_OK
;
1479 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1480 * the assumption that we're in a download and it's important to go as fast
1482 uint32_t fast_limit
= 8;
1484 /* skip command character */
1487 addr
= strtoul(packet
, &separator
, 16);
1489 if (*separator
!= ',') {
1490 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1491 return ERROR_SERVER_REMOTE_CLOSED
;
1494 len
= strtoul(separator
+ 1, &separator
, 16);
1496 if (*(separator
++) != ':') {
1497 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1498 return ERROR_SERVER_REMOTE_CLOSED
;
1501 struct gdb_connection
*gdb_connection
= connection
->priv
;
1503 if (gdb_connection
->mem_write_error
)
1504 retval
= ERROR_FAIL
;
1506 if (retval
== ERROR_OK
) {
1507 if (len
>= fast_limit
) {
1508 /* By replying the packet *immediately* GDB will send us a new packet
1509 * while we write the last one to the target.
1510 * We only do this for larger writes, so that users who do something like:
1511 * p *((int*)0xdeadbeef)=8675309
1512 * will get immediate feedback that that write failed.
1514 gdb_put_packet(connection
, "OK", 2);
1517 retval
= gdb_error(connection
, retval
);
1518 /* now that we have reported the memory write error, we can clear the condition */
1519 gdb_connection
->mem_write_error
= false;
1520 if (retval
!= ERROR_OK
)
1525 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1527 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1528 if (retval
!= ERROR_OK
)
1529 gdb_connection
->mem_write_error
= true;
1532 if (len
< fast_limit
) {
1533 if (retval
!= ERROR_OK
) {
1534 gdb_error(connection
, retval
);
1535 gdb_connection
->mem_write_error
= false;
1537 gdb_put_packet(connection
, "OK", 2);
1544 static int gdb_step_continue_packet(struct connection
*connection
,
1545 char const *packet
, int packet_size
)
1547 struct target
*target
= get_target_from_connection(connection
);
1549 uint32_t address
= 0x0;
1550 int retval
= ERROR_OK
;
1554 if (packet_size
> 1)
1555 address
= strtoul(packet
+ 1, NULL
, 16);
1559 gdb_running_type
= packet
[0];
1560 if (packet
[0] == 'c') {
1561 LOG_DEBUG("continue");
1562 /* resume at current address, don't handle breakpoints, not debugging */
1563 retval
= target_resume(target
, current
, address
, 0, 0);
1564 } else if (packet
[0] == 's') {
1566 /* step at current or address, don't handle breakpoints */
1567 retval
= target_step(target
, current
, address
, 0);
1572 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1573 char const *packet
, int packet_size
)
1575 struct target
*target
= get_target_from_connection(connection
);
1577 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1578 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1586 type
= strtoul(packet
+ 1, &separator
, 16);
1588 if (type
== 0) /* memory breakpoint */
1589 bp_type
= BKPT_SOFT
;
1590 else if (type
== 1) /* hardware breakpoint */
1591 bp_type
= BKPT_HARD
;
1592 else if (type
== 2) /* write watchpoint */
1593 wp_type
= WPT_WRITE
;
1594 else if (type
== 3) /* read watchpoint */
1596 else if (type
== 4) /* access watchpoint */
1597 wp_type
= WPT_ACCESS
;
1599 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1600 return ERROR_SERVER_REMOTE_CLOSED
;
1603 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1604 bp_type
= gdb_breakpoint_override_type
;
1606 if (*separator
!= ',') {
1607 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1608 return ERROR_SERVER_REMOTE_CLOSED
;
1611 address
= strtoul(separator
+ 1, &separator
, 16);
1613 if (*separator
!= ',') {
1614 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1615 return ERROR_SERVER_REMOTE_CLOSED
;
1618 size
= strtoul(separator
+ 1, &separator
, 16);
1623 if (packet
[0] == 'Z') {
1624 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1625 if (retval
!= ERROR_OK
) {
1626 retval
= gdb_error(connection
, retval
);
1627 if (retval
!= ERROR_OK
)
1630 gdb_put_packet(connection
, "OK", 2);
1632 breakpoint_remove(target
, address
);
1633 gdb_put_packet(connection
, "OK", 2);
1640 if (packet
[0] == 'Z') {
1641 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1642 if (retval
!= ERROR_OK
) {
1643 retval
= gdb_error(connection
, retval
);
1644 if (retval
!= ERROR_OK
)
1647 gdb_put_packet(connection
, "OK", 2);
1649 watchpoint_remove(target
, address
);
1650 gdb_put_packet(connection
, "OK", 2);
1661 /* print out a string and allocate more space as needed,
1662 * mainly used for XML at this point
1664 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1665 const char *fmt
, ...)
1667 if (*retval
!= ERROR_OK
)
1672 if ((*xml
== NULL
) || (!first
)) {
1673 /* start by 0 to exercise all the code paths.
1674 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1676 *size
= *size
* 2 + 2;
1678 *xml
= realloc(*xml
, *size
);
1682 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1690 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1692 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1696 /* there was just enough or not enough space, allocate more. */
1701 static int decode_xfer_read(char const *buf
, char **annex
, int *ofs
, unsigned int *len
)
1703 /* Locate the annex. */
1704 const char *annex_end
= strchr(buf
, ':');
1705 if (annex_end
== NULL
)
1708 /* After the read marker and annex, qXfer looks like a
1709 * traditional 'm' packet. */
1711 *ofs
= strtoul(annex_end
+ 1, &separator
, 16);
1713 if (*separator
!= ',')
1716 *len
= strtoul(separator
+ 1, NULL
, 16);
1718 /* Extract the annex if needed */
1719 if (annex
!= NULL
) {
1720 *annex
= strndup(buf
, annex_end
- buf
);
1728 static int compare_bank(const void *a
, const void *b
)
1730 struct flash_bank
*b1
, *b2
;
1731 b1
= *((struct flash_bank
**)a
);
1732 b2
= *((struct flash_bank
**)b
);
1734 if (b1
->base
== b2
->base
)
1736 else if (b1
->base
> b2
->base
)
1742 static int gdb_memory_map(struct connection
*connection
,
1743 char const *packet
, int packet_size
)
1745 /* We get away with only specifying flash here. Regions that are not
1746 * specified are treated as if we provided no memory map(if not we
1747 * could detect the holes and mark them as RAM).
1748 * Normally we only execute this code once, but no big deal if we
1749 * have to regenerate it a couple of times.
1752 struct target
*target
= get_target_from_connection(connection
);
1753 struct flash_bank
*p
;
1757 int retval
= ERROR_OK
;
1758 struct flash_bank
**banks
;
1762 uint32_t ram_start
= 0;
1764 int target_flash_banks
= 0;
1766 /* skip command character */
1769 offset
= strtoul(packet
, &separator
, 16);
1770 length
= strtoul(separator
+ 1, &separator
, 16);
1772 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1774 /* Sort banks in ascending order. We need to report non-flash
1775 * memory as ram (or rather read/write) by default for GDB, since
1776 * it has no concept of non-cacheable read/write memory (i/o etc).
1778 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1779 * Current versions of GDB assume unlisted addresses are RAM...
1781 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1783 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1784 p
= get_flash_bank_by_num_noprobe(i
);
1785 if (p
->target
!= target
)
1787 retval
= get_flash_bank_by_num(i
, &p
);
1788 if (retval
!= ERROR_OK
) {
1790 gdb_error(connection
, retval
);
1793 banks
[target_flash_banks
++] = p
;
1796 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1799 for (i
= 0; i
< target_flash_banks
; i
++) {
1801 unsigned sector_size
= 0;
1807 if (ram_start
< p
->base
)
1808 xml_printf(&retval
, &xml
, &pos
, &size
,
1809 "<memory type=\"ram\" start=\"0x%x\" "
1810 "length=\"0x%x\"/>\n",
1811 ram_start
, p
->base
- ram_start
);
1813 /* Report adjacent groups of same-size sectors. So for
1814 * example top boot CFI flash will list an initial region
1815 * with several large sectors (maybe 128KB) and several
1816 * smaller ones at the end (maybe 32KB). STR7 will have
1817 * regions with 8KB, 32KB, and 64KB sectors; etc.
1819 for (j
= 0; j
< p
->num_sectors
; j
++) {
1822 /* Maybe start a new group of sectors. */
1823 if (sector_size
== 0) {
1824 start
= p
->base
+ p
->sectors
[j
].offset
;
1825 xml_printf(&retval
, &xml
, &pos
, &size
,
1826 "<memory type=\"flash\" "
1829 sector_size
= p
->sectors
[j
].size
;
1832 /* Does this finish a group of sectors?
1833 * If not, continue an already-started group.
1835 if (j
== p
->num_sectors
- 1)
1836 group_len
= (p
->base
+ p
->size
) - start
;
1837 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1838 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1843 xml_printf(&retval
, &xml
, &pos
, &size
,
1844 "length=\"0x%x\">\n"
1845 "<property name=\"blocksize\">"
1853 ram_start
= p
->base
+ p
->size
;
1857 xml_printf(&retval
, &xml
, &pos
, &size
,
1858 "<memory type=\"ram\" start=\"0x%x\" "
1859 "length=\"0x%x\"/>\n",
1860 ram_start
, 0-ram_start
);
1861 /* ELSE a flash chip could be at the very end of the 32 bit address
1862 * space, in which case ram_start will be precisely 0
1868 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1870 if (retval
!= ERROR_OK
) {
1871 gdb_error(connection
, retval
);
1875 if (offset
+ length
> pos
)
1876 length
= pos
- offset
;
1878 char *t
= malloc(length
+ 1);
1880 memcpy(t
+ 1, xml
+ offset
, length
);
1881 gdb_put_packet(connection
, t
, length
+ 1);
1888 static const char *gdb_get_reg_type_name(enum reg_type type
)
1895 case REG_TYPE_INT16
:
1897 case REG_TYPE_INT32
:
1899 case REG_TYPE_INT64
:
1901 case REG_TYPE_INT128
:
1903 case REG_TYPE_UINT8
:
1905 case REG_TYPE_UINT16
:
1907 case REG_TYPE_UINT32
:
1909 case REG_TYPE_UINT64
:
1911 case REG_TYPE_UINT128
:
1913 case REG_TYPE_CODE_PTR
:
1915 case REG_TYPE_DATA_PTR
:
1917 case REG_TYPE_FLOAT
:
1919 case REG_TYPE_IEEE_SINGLE
:
1920 return "ieee_single";
1921 case REG_TYPE_IEEE_DOUBLE
:
1922 return "ieee_double";
1923 case REG_TYPE_ARCH_DEFINED
:
1924 return "int"; /* return arbitrary string to avoid compile warning. */
1927 return "int"; /* "int" as default value */
1930 static int gdb_generate_reg_type_description(struct target
*target
,
1931 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
)
1933 int retval
= ERROR_OK
;
1935 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
1936 /* <vector id="id" type="type" count="count"/> */
1937 xml_printf(&retval
, tdesc
, pos
, size
,
1938 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
1939 type
->id
, type
->reg_type_vector
->type
->id
,
1940 type
->reg_type_vector
->count
);
1942 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
1944 * <field name="name" type="type"/> ...
1946 xml_printf(&retval
, tdesc
, pos
, size
,
1947 "<union id=\"%s\">\n",
1950 struct reg_data_type_union_field
*field
;
1951 field
= type
->reg_type_union
->fields
;
1952 while (field
!= NULL
) {
1953 xml_printf(&retval
, tdesc
, pos
, size
,
1954 "<field name=\"%s\" type=\"%s\"/>\n",
1955 field
->name
, field
->type
->id
);
1957 field
= field
->next
;
1960 xml_printf(&retval
, tdesc
, pos
, size
,
1963 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
1964 struct reg_data_type_struct_field
*field
;
1965 field
= type
->reg_type_struct
->fields
;
1967 if (field
->use_bitfields
) {
1968 /* <struct id="id" size="size">
1969 * <field name="name" start="start" end="end"/> ...
1971 xml_printf(&retval
, tdesc
, pos
, size
,
1972 "<struct id=\"%s\" size=\"%d\">\n",
1973 type
->id
, type
->reg_type_struct
->size
);
1974 while (field
!= NULL
) {
1975 xml_printf(&retval
, tdesc
, pos
, size
,
1976 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1977 field
->name
, field
->bitfield
->start
,
1978 field
->bitfield
->end
);
1980 field
= field
->next
;
1984 * <field name="name" type="type"/> ...
1986 xml_printf(&retval
, tdesc
, pos
, size
,
1987 "<struct id=\"%s\">\n",
1989 while (field
!= NULL
) {
1990 xml_printf(&retval
, tdesc
, pos
, size
,
1991 "<field name=\"%s\" type=\"%s\"/>\n",
1992 field
->name
, field
->type
->id
);
1994 field
= field
->next
;
1998 xml_printf(&retval
, tdesc
, pos
, size
,
2001 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
2002 /* <flags id="id" size="size">
2003 * <field name="name" start="start" end="end"/> ...
2005 xml_printf(&retval
, tdesc
, pos
, size
,
2006 "<flags id=\"%s\" size=\"%d\">\n",
2007 type
->id
, type
->reg_type_flags
->size
);
2009 struct reg_data_type_flags_field
*field
;
2010 field
= type
->reg_type_flags
->fields
;
2011 while (field
!= NULL
) {
2012 xml_printf(&retval
, tdesc
, pos
, size
,
2013 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
2014 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
);
2016 field
= field
->next
;
2019 xml_printf(&retval
, tdesc
, pos
, size
,
2027 /* Get a list of available target registers features. feature_list must
2028 * be freed by caller.
2030 static int get_reg_features_list(struct target
*target
, char const **feature_list
[], int *feature_list_size
,
2031 struct reg
**reg_list
, int reg_list_size
)
2035 /* Start with only one element */
2036 *feature_list
= calloc(1, sizeof(char *));
2038 for (int i
= 0; i
< reg_list_size
; i
++) {
2039 if (reg_list
[i
]->exist
== false)
2042 if (reg_list
[i
]->feature
!= NULL
2043 && reg_list
[i
]->feature
->name
!= NULL
2044 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
2045 /* We found a feature, check if the feature is already in the
2046 * table. If not, allocate a new entry for the table and
2047 * put the new feature in it.
2049 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
2050 if (!((*feature_list
)[j
])) {
2051 (*feature_list
)[tbl_sz
++] = reg_list
[i
]->feature
->name
;
2052 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
2053 (*feature_list
)[tbl_sz
] = NULL
;
2056 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
2063 if (feature_list_size
)
2064 *feature_list_size
= tbl_sz
;
2069 static int gdb_generate_target_description(struct target
*target
, char **tdesc_out
)
2071 int retval
= ERROR_OK
;
2072 struct reg
**reg_list
= NULL
;
2074 char const **features
= NULL
;
2075 int feature_list_size
= 0;
2080 retval
= target_get_gdb_reg_list(target
, ®_list
,
2081 ®_list_size
, REG_CLASS_ALL
);
2083 if (retval
!= ERROR_OK
) {
2084 LOG_ERROR("get register list failed");
2085 retval
= ERROR_FAIL
;
2089 if (reg_list_size
<= 0) {
2090 LOG_ERROR("get register list failed");
2091 retval
= ERROR_FAIL
;
2095 /* Get a list of available target registers features */
2096 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2097 if (retval
!= ERROR_OK
) {
2098 LOG_ERROR("Can't get the registers feature list");
2099 retval
= ERROR_FAIL
;
2103 /* If we found some features associated with registers, create sections */
2104 int current_feature
= 0;
2106 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2107 "<?xml version=\"1.0\"?>\n"
2108 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2109 "<target version=\"1.0\">\n");
2111 /* generate target description according to register list */
2112 if (features
!= NULL
) {
2113 while (features
[current_feature
]) {
2115 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2116 "<feature name=\"%s\">\n",
2117 features
[current_feature
]);
2120 for (i
= 0; i
< reg_list_size
; i
++) {
2122 if (reg_list
[i
]->exist
== false)
2125 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
2128 const char *type_str
;
2129 if (reg_list
[i
]->reg_data_type
!= NULL
) {
2130 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
2131 /* generate <type... first, if there are architecture-defined types. */
2132 gdb_generate_reg_type_description(target
, &tdesc
, &pos
, &size
,
2133 reg_list
[i
]->reg_data_type
);
2135 type_str
= reg_list
[i
]->reg_data_type
->id
;
2137 /* predefined type */
2138 type_str
= gdb_get_reg_type_name(
2139 reg_list
[i
]->reg_data_type
->type
);
2142 /* Default type is "int" */
2146 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2147 "<reg name=\"%s\"", reg_list
[i
]->name
);
2148 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2149 " bitsize=\"%d\"", reg_list
[i
]->size
);
2150 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2151 " regnum=\"%d\"", reg_list
[i
]->number
);
2152 if (reg_list
[i
]->caller_save
)
2153 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2154 " save-restore=\"yes\"");
2156 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2157 " save-restore=\"no\"");
2159 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2160 " type=\"%s\"", type_str
);
2162 if (reg_list
[i
]->group
!= NULL
)
2163 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2164 " group=\"%s\"", reg_list
[i
]->group
);
2166 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2170 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2177 xml_printf(&retval
, &tdesc
, &pos
, &size
,
2184 if (retval
== ERROR_OK
)
2192 static int gdb_get_target_description_chunk(struct target
*target
, struct target_desc_format
*target_desc
,
2193 char **chunk
, int32_t offset
, uint32_t length
)
2195 if (target_desc
== NULL
) {
2196 LOG_ERROR("Unable to Generate Target Description");
2200 char *tdesc
= target_desc
->tdesc
;
2201 uint32_t tdesc_length
= target_desc
->tdesc_length
;
2203 if (tdesc
== NULL
) {
2204 int retval
= gdb_generate_target_description(target
, &tdesc
);
2205 if (retval
!= ERROR_OK
) {
2206 LOG_ERROR("Unable to Generate Target Description");
2210 tdesc_length
= strlen(tdesc
);
2215 if (length
< (tdesc_length
- offset
))
2216 transfer_type
= 'm';
2218 transfer_type
= 'l';
2220 *chunk
= malloc(length
+ 2);
2221 if (*chunk
== NULL
) {
2222 LOG_ERROR("Unable to allocate memory");
2226 (*chunk
)[0] = transfer_type
;
2227 if (transfer_type
== 'm') {
2228 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2229 (*chunk
)[1 + length
] = '\0';
2231 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2232 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2234 /* After gdb-server sends out last chunk, invalidate tdesc. */
2240 target_desc
->tdesc
= tdesc
;
2241 target_desc
->tdesc_length
= tdesc_length
;
2246 static int gdb_target_description_supported(struct target
*target
, int *supported
)
2248 int retval
= ERROR_OK
;
2249 struct reg
**reg_list
= NULL
;
2250 int reg_list_size
= 0;
2251 char const **features
= NULL
;
2252 int feature_list_size
= 0;
2254 retval
= target_get_gdb_reg_list(target
, ®_list
,
2255 ®_list_size
, REG_CLASS_ALL
);
2256 if (retval
!= ERROR_OK
) {
2257 LOG_ERROR("get register list failed");
2261 if (reg_list_size
<= 0) {
2262 LOG_ERROR("get register list failed");
2263 retval
= ERROR_FAIL
;
2267 /* Get a list of available target registers features */
2268 retval
= get_reg_features_list(target
, &features
, &feature_list_size
, reg_list
, reg_list_size
);
2269 if (retval
!= ERROR_OK
) {
2270 LOG_ERROR("Can't get the registers feature list");
2275 if (feature_list_size
)
2289 static int gdb_generate_thread_list(struct target
*target
, char **thread_list_out
)
2291 struct rtos
*rtos
= target
->rtos
;
2292 int retval
= ERROR_OK
;
2293 char *thread_list
= NULL
;
2297 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2298 "<?xml version=\"1.0\"?>\n"
2302 for (int i
= 0; i
< rtos
->thread_count
; i
++) {
2303 struct thread_detail
*thread_detail
= &rtos
->thread_details
[i
];
2305 if (!thread_detail
->exists
)
2308 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2309 "<thread id=\"%" PRIx64
"\">", thread_detail
->threadid
);
2311 if (thread_detail
->thread_name_str
!= NULL
)
2312 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2313 "Name: %s", thread_detail
->thread_name_str
);
2315 if (thread_detail
->extra_info_str
!= NULL
) {
2316 if (thread_detail
->thread_name_str
!= NULL
)
2317 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2319 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2320 thread_detail
->extra_info_str
);
2323 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2328 xml_printf(&retval
, &thread_list
, &pos
, &size
,
2331 if (retval
== ERROR_OK
)
2332 *thread_list_out
= thread_list
;
2339 static int gdb_get_thread_list_chunk(struct target
*target
, char **thread_list
,
2340 char **chunk
, int32_t offset
, uint32_t length
)
2342 if (*thread_list
== NULL
) {
2343 int retval
= gdb_generate_thread_list(target
, thread_list
);
2344 if (retval
!= ERROR_OK
) {
2345 LOG_ERROR("Unable to Generate Thread List");
2350 size_t thread_list_length
= strlen(*thread_list
);
2353 length
= MIN(length
, thread_list_length
- offset
);
2354 if (length
< (thread_list_length
- offset
))
2355 transfer_type
= 'm';
2357 transfer_type
= 'l';
2359 *chunk
= malloc(length
+ 2);
2360 if (*chunk
== NULL
) {
2361 LOG_ERROR("Unable to allocate memory");
2365 (*chunk
)[0] = transfer_type
;
2366 strncpy((*chunk
) + 1, (*thread_list
) + offset
, length
);
2367 (*chunk
)[1 + length
] = '\0';
2369 /* After gdb-server sends out last chunk, invalidate thread list. */
2370 if (transfer_type
== 'l') {
2372 *thread_list
= NULL
;
2378 static int gdb_query_packet(struct connection
*connection
,
2379 char const *packet
, int packet_size
)
2381 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2382 struct gdb_connection
*gdb_connection
= connection
->priv
;
2383 struct target
*target
= get_target_from_connection(connection
);
2385 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2386 if (packet_size
> 6) {
2388 cmd
= malloc((packet_size
- 6) / 2 + 1);
2389 size_t len
= unhexify((uint8_t *)cmd
, packet
+ 6, (packet_size
- 6) / 2);
2392 /* We want to print all debug output to GDB connection */
2393 log_add_callback(gdb_log_callback
, connection
);
2394 target_call_timer_callbacks_now();
2395 /* some commands need to know the GDB connection, make note of current
2396 * GDB connection. */
2397 current_gdb_connection
= gdb_connection
;
2398 command_run_line(cmd_ctx
, cmd
);
2399 current_gdb_connection
= NULL
;
2400 target_call_timer_callbacks_now();
2401 log_remove_callback(gdb_log_callback
, connection
);
2404 gdb_put_packet(connection
, "OK", 2);
2406 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2407 if (packet_size
> 5) {
2415 /* skip command character */
2418 addr
= strtoul(packet
, &separator
, 16);
2420 if (*separator
!= ',') {
2421 LOG_ERROR("incomplete read memory packet received, dropping connection");
2422 return ERROR_SERVER_REMOTE_CLOSED
;
2425 len
= strtoul(separator
+ 1, NULL
, 16);
2427 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2429 if (retval
== ERROR_OK
) {
2430 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2431 gdb_put_packet(connection
, gdb_reply
, 9);
2433 retval
= gdb_error(connection
, retval
);
2434 if (retval
!= ERROR_OK
)
2440 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2441 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2442 * qXfer:features:read is supported for some targets */
2443 int retval
= ERROR_OK
;
2444 char *buffer
= NULL
;
2447 int gdb_target_desc_supported
= 0;
2449 /* we need to test that the target supports target descriptions */
2450 retval
= gdb_target_description_supported(target
, &gdb_target_desc_supported
);
2451 if (retval
!= ERROR_OK
) {
2452 LOG_INFO("Failed detecting Target Description Support, disabling");
2453 gdb_target_desc_supported
= 0;
2456 /* support may be disabled globally */
2457 if (gdb_use_target_description
== 0) {
2458 if (gdb_target_desc_supported
)
2459 LOG_WARNING("Target Descriptions Supported, but disabled");
2460 gdb_target_desc_supported
= 0;
2467 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+",
2468 (GDB_BUFFER_SIZE
- 1),
2469 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2470 (gdb_target_desc_supported
== 1) ? '+' : '-');
2472 if (retval
!= ERROR_OK
) {
2473 gdb_send_error(connection
, 01);
2477 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2481 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2482 && (flash_get_bank_count() > 0))
2483 return gdb_memory_map(connection
, packet
, packet_size
);
2484 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2486 int retval
= ERROR_OK
;
2489 unsigned int length
;
2491 /* skip command character */
2494 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2495 gdb_send_error(connection
, 01);
2499 /* Target should prepare correct target description for annex.
2500 * The first character of returned xml is 'm' or 'l'. 'm' for
2501 * there are *more* chunks to transfer. 'l' for it is the *last*
2502 * chunk of target description.
2504 retval
= gdb_get_target_description_chunk(target
, &gdb_connection
->target_desc
,
2505 &xml
, offset
, length
);
2506 if (retval
!= ERROR_OK
) {
2507 gdb_error(connection
, retval
);
2511 gdb_put_packet(connection
, xml
, strlen(xml
));
2515 } else if (strncmp(packet
, "qXfer:threads:read:", 19) == 0) {
2517 int retval
= ERROR_OK
;
2520 unsigned int length
;
2522 /* skip command character */
2525 if (decode_xfer_read(packet
, NULL
, &offset
, &length
) < 0) {
2526 gdb_send_error(connection
, 01);
2530 /* Target should prepare correct thread list for annex.
2531 * The first character of returned xml is 'm' or 'l'. 'm' for
2532 * there are *more* chunks to transfer. 'l' for it is the *last*
2533 * chunk of target description.
2535 retval
= gdb_get_thread_list_chunk(target
, &gdb_connection
->thread_list
,
2536 &xml
, offset
, length
);
2537 if (retval
!= ERROR_OK
) {
2538 gdb_error(connection
, retval
);
2542 gdb_put_packet(connection
, xml
, strlen(xml
));
2546 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2547 gdb_connection
->noack_mode
= 1;
2548 gdb_put_packet(connection
, "OK", 2);
2552 gdb_put_packet(connection
, "", 0);
2556 static int gdb_v_packet(struct connection
*connection
,
2557 char const *packet
, int packet_size
)
2559 struct gdb_connection
*gdb_connection
= connection
->priv
;
2560 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2563 /* if flash programming disabled - send a empty reply */
2565 if (gdb_flash_program
== 0) {
2566 gdb_put_packet(connection
, "", 0);
2570 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
2572 unsigned long length
;
2574 char const *parse
= packet
+ 12;
2575 if (*parse
== '\0') {
2576 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2577 return ERROR_SERVER_REMOTE_CLOSED
;
2580 addr
= strtoul(parse
, (char **)&parse
, 16);
2582 if (*(parse
++) != ',' || *parse
== '\0') {
2583 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2584 return ERROR_SERVER_REMOTE_CLOSED
;
2587 length
= strtoul(parse
, (char **)&parse
, 16);
2589 if (*parse
!= '\0') {
2590 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2591 return ERROR_SERVER_REMOTE_CLOSED
;
2594 /* assume all sectors need erasing - stops any problems
2595 * when flash_write is called multiple times */
2598 /* perform any target specific operations before the erase */
2599 target_call_event_callbacks(gdb_service
->target
,
2600 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2602 /* vFlashErase:addr,length messages require region start and
2603 * end to be "block" aligned ... if padding is ever needed,
2604 * GDB will have become dangerously confused.
2606 result
= flash_erase_address_range(gdb_service
->target
,
2607 false, addr
, length
);
2609 /* perform any target specific operations after the erase */
2610 target_call_event_callbacks(gdb_service
->target
,
2611 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2614 if (result
!= ERROR_OK
) {
2615 /* GDB doesn't evaluate the actual error number returned,
2616 * treat a failed erase as an I/O error
2618 gdb_send_error(connection
, EIO
);
2619 LOG_ERROR("flash_erase returned %i", result
);
2621 gdb_put_packet(connection
, "OK", 2);
2626 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
2629 unsigned long length
;
2630 char const *parse
= packet
+ 12;
2632 if (*parse
== '\0') {
2633 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2634 return ERROR_SERVER_REMOTE_CLOSED
;
2636 addr
= strtoul(parse
, (char **)&parse
, 16);
2637 if (*(parse
++) != ':') {
2638 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2639 return ERROR_SERVER_REMOTE_CLOSED
;
2641 length
= packet_size
- (parse
- packet
);
2643 /* create a new image if there isn't already one */
2644 if (gdb_connection
->vflash_image
== NULL
) {
2645 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2646 image_open(gdb_connection
->vflash_image
, "", "build");
2649 /* create new section with content from packet buffer */
2650 retval
= image_add_section(gdb_connection
->vflash_image
,
2651 addr
, length
, 0x0, (uint8_t const *)parse
);
2652 if (retval
!= ERROR_OK
)
2655 gdb_put_packet(connection
, "OK", 2);
2660 if (strncmp(packet
, "vFlashDone", 10) == 0) {
2663 /* process the flashing buffer. No need to erase as GDB
2664 * always issues a vFlashErase first. */
2665 target_call_event_callbacks(gdb_service
->target
,
2666 TARGET_EVENT_GDB_FLASH_WRITE_START
);
2667 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2668 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2669 if (result
!= ERROR_OK
) {
2670 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2671 gdb_put_packet(connection
, "E.memtype", 9);
2673 gdb_send_error(connection
, EIO
);
2675 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2676 gdb_put_packet(connection
, "OK", 2);
2679 image_close(gdb_connection
->vflash_image
);
2680 free(gdb_connection
->vflash_image
);
2681 gdb_connection
->vflash_image
= NULL
;
2686 gdb_put_packet(connection
, "", 0);
2690 static int gdb_detach(struct connection
*connection
)
2692 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2694 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
2696 return gdb_put_packet(connection
, "OK", 2);
2699 /* The format of 'F' response packet is
2700 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2702 static int gdb_fileio_response_packet(struct connection
*connection
,
2703 char const *packet
, int packet_size
)
2705 struct target
*target
= get_target_from_connection(connection
);
2707 char *parsing_point
;
2708 int fileio_retcode
= strtoul(packet
+ 1, &separator
, 16);
2709 int fileio_errno
= 0;
2710 bool fileio_ctrl_c
= false;
2715 if (*separator
== ',') {
2716 parsing_point
= separator
+ 1;
2717 fileio_errno
= strtoul(parsing_point
, &separator
, 16);
2718 if (*separator
== ',') {
2719 if (*(separator
+ 1) == 'C') {
2720 /* TODO: process ctrl-c */
2721 fileio_ctrl_c
= true;
2726 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
2727 fileio_retcode
, fileio_errno
, fileio_ctrl_c
? "true" : "false");
2729 retval
= target_gdb_fileio_end(target
, fileio_retcode
, fileio_errno
, fileio_ctrl_c
);
2730 if (retval
!= ERROR_OK
)
2733 /* After File-I/O ends, keep continue or step */
2734 if (gdb_running_type
== 'c')
2735 retval
= target_resume(target
, 1, 0x0, 0, 0);
2736 else if (gdb_running_type
== 's')
2737 retval
= target_step(target
, 1, 0x0, 0);
2739 retval
= ERROR_FAIL
;
2741 if (retval
!= ERROR_OK
)
2747 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2748 const char *function
, const char *string
)
2750 struct connection
*connection
= priv
;
2751 struct gdb_connection
*gdb_con
= connection
->priv
;
2753 if (gdb_con
->busy
) {
2754 /* do not reply this using the O packet */
2758 gdb_output_con(connection
, string
);
2761 static void gdb_sig_halted(struct connection
*connection
)
2764 snprintf(sig_reply
, 4, "T%2.2x", 2);
2765 gdb_put_packet(connection
, sig_reply
, 3);
2768 static int gdb_input_inner(struct connection
*connection
)
2770 /* Do not allocate this on the stack */
2771 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2773 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2774 struct target
*target
= gdb_service
->target
;
2775 char const *packet
= gdb_packet_buffer
;
2778 struct gdb_connection
*gdb_con
= connection
->priv
;
2779 static int extended_protocol
;
2781 /* drain input buffer. If one of the packets fail, then an error
2782 * packet is replied, if applicable.
2784 * This loop will terminate and the error code is returned.
2786 * The calling fn will check if this error is something that
2787 * can be recovered from, or if the connection must be closed.
2789 * If the error is recoverable, this fn is called again to
2790 * drain the rest of the buffer.
2793 packet_size
= GDB_BUFFER_SIZE
-1;
2794 retval
= gdb_get_packet(connection
, gdb_packet_buffer
, &packet_size
);
2795 if (retval
!= ERROR_OK
)
2798 /* terminate with zero */
2799 gdb_packet_buffer
[packet_size
] = '\0';
2801 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2802 if (packet
[0] == 'X') {
2803 /* binary packets spew junk into the debug log stream */
2806 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
2809 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2811 LOG_DEBUG("received packet: '%s'", packet
);
2814 if (packet_size
> 0) {
2816 switch (packet
[0]) {
2817 case 'T': /* Is thread alive? */
2818 gdb_thread_packet(connection
, packet
, packet_size
);
2820 case 'H': /* Set current thread ( 'c' for step and continue,
2821 * 'g' for all other operations ) */
2822 gdb_thread_packet(connection
, packet
, packet_size
);
2826 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2827 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2828 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2831 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2834 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2837 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2840 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2843 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2846 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2850 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2853 gdb_last_signal_packet(connection
, packet
, packet_size
);
2858 gdb_thread_packet(connection
, packet
, packet_size
);
2859 log_add_callback(gdb_log_callback
, connection
);
2861 if (gdb_con
->mem_write_error
) {
2862 LOG_ERROR("Memory write failure!");
2864 /* now that we have reported the memory write error,
2865 * we can clear the condition */
2866 gdb_con
->mem_write_error
= false;
2869 bool nostep
= false;
2870 bool already_running
= false;
2871 if (target
->state
== TARGET_RUNNING
) {
2872 LOG_WARNING("WARNING! The target is already running. "
2873 "All changes GDB did to registers will be discarded! "
2874 "Waiting for target to halt.");
2875 already_running
= true;
2876 } else if (target
->state
!= TARGET_HALTED
) {
2877 LOG_WARNING("The target is not in the halted nor running stated, " \
2878 "stepi/continue ignored.");
2880 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
2881 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2882 * sent by GDB first to OpenOCD, thus defeating the check to
2883 * make only the single stepping have the sync feature...
2886 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2887 "from the target.");
2889 gdb_con
->sync
= false;
2891 if (!already_running
&& nostep
) {
2892 /* Either the target isn't in the halted state, then we can't
2893 * step/continue. This might be early setup, etc.
2895 * Or we want to allow GDB to pick up a fresh set of
2896 * register values without modifying the target state.
2899 gdb_sig_halted(connection
);
2901 /* stop forwarding log packets! */
2902 log_remove_callback(gdb_log_callback
, connection
);
2904 /* We're running/stepping, in which case we can
2905 * forward log output until the target is halted
2907 gdb_con
->frontend_state
= TARGET_RUNNING
;
2908 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2910 if (!already_running
) {
2911 /* Here we don't want packet processing to stop even if this fails,
2912 * so we use a local variable instead of retval. */
2913 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2914 if (retval
!= ERROR_OK
) {
2915 /* we'll never receive a halted
2916 * condition... issue a false one..
2918 gdb_frontend_halted(target
, connection
);
2925 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2928 retval
= gdb_detach(connection
);
2929 extended_protocol
= 0;
2932 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2933 if (retval
!= ERROR_OK
)
2937 if (extended_protocol
!= 0) {
2938 gdb_con
->attached
= false;
2941 gdb_put_packet(connection
, "OK", 2);
2942 return ERROR_SERVER_REMOTE_CLOSED
;
2944 /* handle extended remote protocol */
2945 extended_protocol
= 1;
2946 gdb_put_packet(connection
, "OK", 2);
2949 /* handle extended restart packet */
2950 breakpoint_clear_target(gdb_service
->target
);
2951 watchpoint_clear_target(gdb_service
->target
);
2952 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
2953 target_name(target
));
2954 /* set connection as attached after reset */
2955 gdb_con
->attached
= true;
2956 /* info rtos parts */
2957 gdb_thread_packet(connection
, packet
, packet_size
);
2961 /* packet supported only by smp target i.e cortex_a.c*/
2962 /* handle smp packet replying coreid played to gbd */
2963 gdb_read_smp_packet(connection
, packet
, packet_size
);
2967 /* packet supported only by smp target i.e cortex_a.c */
2968 /* handle smp packet setting coreid to be played at next
2970 gdb_write_smp_packet(connection
, packet
, packet_size
);
2974 /* File-I/O extension */
2975 /* After gdb uses host-side syscall to complete target file
2976 * I/O, gdb sends host-side syscall return value to target
2978 * The format of 'F' response packet is
2979 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2981 gdb_con
->frontend_state
= TARGET_RUNNING
;
2982 log_add_callback(gdb_log_callback
, connection
);
2983 gdb_fileio_response_packet(connection
, packet
, packet_size
);
2987 /* ignore unknown packets */
2988 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2989 gdb_put_packet(connection
, NULL
, 0);
2993 /* if a packet handler returned an error, exit input loop */
2994 if (retval
!= ERROR_OK
)
2998 if (gdb_con
->ctrl_c
) {
2999 if (target
->state
== TARGET_RUNNING
) {
3000 retval
= target_halt(target
);
3001 if (retval
!= ERROR_OK
)
3002 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3003 gdb_con
->ctrl_c
= 0;
3005 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3006 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
3010 } while (gdb_con
->buf_cnt
> 0);
3015 static int gdb_input(struct connection
*connection
)
3017 int retval
= gdb_input_inner(connection
);
3018 struct gdb_connection
*gdb_con
= connection
->priv
;
3019 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
3022 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3023 if (gdb_con
->closed
)
3024 return ERROR_SERVER_REMOTE_CLOSED
;
3026 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3030 static int gdb_target_start(struct target
*target
, const char *port
)
3032 struct gdb_service
*gdb_service
;
3034 gdb_service
= malloc(sizeof(struct gdb_service
));
3036 if (NULL
== gdb_service
)
3039 gdb_service
->target
= target
;
3040 gdb_service
->core
[0] = -1;
3041 gdb_service
->core
[1] = -1;
3042 target
->gdb_service
= gdb_service
;
3044 ret
= add_service("gdb",
3045 port
, 1, &gdb_new_connection
, &gdb_input
,
3046 &gdb_connection_closed
, gdb_service
);
3047 /* initialialize all targets gdb service with the same pointer */
3049 struct target_list
*head
;
3050 struct target
*curr
;
3051 head
= target
->head
;
3052 while (head
!= (struct target_list
*)NULL
) {
3053 curr
= head
->target
;
3055 curr
->gdb_service
= gdb_service
;
3062 static int gdb_target_add_one(struct target
*target
)
3064 if (strcmp(gdb_port
, "disabled") == 0) {
3065 LOG_INFO("gdb port disabled");
3069 /* one gdb instance per smp list */
3070 if ((target
->smp
) && (target
->gdb_service
))
3072 int retval
= gdb_target_start(target
, gdb_port_next
);
3073 if (retval
== ERROR_OK
) {
3075 /* If we can parse the port number
3076 * then we increment the port number for the next target.
3079 portnumber
= strtol(gdb_port_next
, &end
, 0);
3081 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
3082 free(gdb_port_next
);
3083 gdb_port_next
= alloc_printf("%d", portnumber
+1);
3090 int gdb_target_add_all(struct target
*target
)
3092 if (strcmp(gdb_port
, "disabled") == 0) {
3093 LOG_INFO("gdb server disabled");
3097 if (NULL
== target
) {
3098 LOG_WARNING("gdb services need one or more targets defined");
3102 while (NULL
!= target
) {
3103 int retval
= gdb_target_add_one(target
);
3104 if (ERROR_OK
!= retval
)
3107 target
= target
->next
;
3113 COMMAND_HANDLER(handle_gdb_sync_command
)
3116 return ERROR_COMMAND_SYNTAX_ERROR
;
3118 if (current_gdb_connection
== NULL
) {
3119 command_print(CMD_CTX
,
3120 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3124 current_gdb_connection
->sync
= true;
3129 /* daemon configuration command gdb_port */
3130 COMMAND_HANDLER(handle_gdb_port_command
)
3132 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
3133 if (ERROR_OK
== retval
) {
3134 free(gdb_port_next
);
3135 gdb_port_next
= strdup(gdb_port
);
3140 COMMAND_HANDLER(handle_gdb_memory_map_command
)
3143 return ERROR_COMMAND_SYNTAX_ERROR
;
3145 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
3149 COMMAND_HANDLER(handle_gdb_flash_program_command
)
3152 return ERROR_COMMAND_SYNTAX_ERROR
;
3154 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
3158 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
3161 return ERROR_COMMAND_SYNTAX_ERROR
;
3163 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
3167 /* gdb_breakpoint_override */
3168 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
3170 if (CMD_ARGC
== 0) {
3172 } else if (CMD_ARGC
== 1) {
3173 gdb_breakpoint_override
= 1;
3174 if (strcmp(CMD_ARGV
[0], "hard") == 0)
3175 gdb_breakpoint_override_type
= BKPT_HARD
;
3176 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
3177 gdb_breakpoint_override_type
= BKPT_SOFT
;
3178 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
3179 gdb_breakpoint_override
= 0;
3181 return ERROR_COMMAND_SYNTAX_ERROR
;
3182 if (gdb_breakpoint_override
)
3183 LOG_USER("force %s breakpoints",
3184 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
3186 LOG_USER("breakpoint type is not overridden");
3191 COMMAND_HANDLER(handle_gdb_target_description_command
)
3194 return ERROR_COMMAND_SYNTAX_ERROR
;
3196 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
3200 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
3203 uint32_t tdesc_length
;
3204 struct target
*target
= get_current_target(CMD_CTX
);
3206 int retval
= gdb_generate_target_description(target
, &tdesc
);
3207 if (retval
!= ERROR_OK
) {
3208 LOG_ERROR("Unable to Generate Target Description");
3212 tdesc_length
= strlen(tdesc
);
3214 struct fileio
*fileio
;
3215 size_t size_written
;
3217 char *tdesc_filename
= alloc_printf("%s.xml", target_type_name(target
));
3218 if (tdesc_filename
== NULL
) {
3219 retval
= ERROR_FAIL
;
3223 retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
3225 if (retval
!= ERROR_OK
) {
3226 LOG_ERROR("Can't open %s for writing", tdesc_filename
);
3230 retval
= fileio_write(fileio
, tdesc_length
, tdesc
, &size_written
);
3232 fileio_close(fileio
);
3234 if (retval
!= ERROR_OK
)
3235 LOG_ERROR("Error while writing the tdesc file");
3238 free(tdesc_filename
);
3244 static const struct command_registration gdb_command_handlers
[] = {
3247 .handler
= handle_gdb_sync_command
,
3248 .mode
= COMMAND_ANY
,
3249 .help
= "next stepi will return immediately allowing "
3250 "GDB to fetch register state without affecting "
3256 .handler
= handle_gdb_port_command
,
3257 .mode
= COMMAND_ANY
,
3258 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
3259 "server listens for the next port number after the "
3260 "base port number specified. "
3261 "No arguments reports GDB port. \"pipe\" means listen to stdin "
3262 "output to stdout, an integer is base port number, \"disabled\" disables "
3263 "port. Any other string is are interpreted as named pipe to listen to. "
3264 "Output pipe is the same name as input pipe, but with 'o' appended.",
3265 .usage
= "[port_num]",
3268 .name
= "gdb_memory_map",
3269 .handler
= handle_gdb_memory_map_command
,
3270 .mode
= COMMAND_CONFIG
,
3271 .help
= "enable or disable memory map",
3272 .usage
= "('enable'|'disable')"
3275 .name
= "gdb_flash_program",
3276 .handler
= handle_gdb_flash_program_command
,
3277 .mode
= COMMAND_CONFIG
,
3278 .help
= "enable or disable flash program",
3279 .usage
= "('enable'|'disable')"
3282 .name
= "gdb_report_data_abort",
3283 .handler
= handle_gdb_report_data_abort_command
,
3284 .mode
= COMMAND_CONFIG
,
3285 .help
= "enable or disable reporting data aborts",
3286 .usage
= "('enable'|'disable')"
3289 .name
= "gdb_breakpoint_override",
3290 .handler
= handle_gdb_breakpoint_override_command
,
3291 .mode
= COMMAND_ANY
,
3292 .help
= "Display or specify type of breakpoint "
3293 "to be used by gdb 'break' commands.",
3294 .usage
= "('hard'|'soft'|'disable')"
3297 .name
= "gdb_target_description",
3298 .handler
= handle_gdb_target_description_command
,
3299 .mode
= COMMAND_CONFIG
,
3300 .help
= "enable or disable target description",
3301 .usage
= "('enable'|'disable')"
3304 .name
= "gdb_save_tdesc",
3305 .handler
= handle_gdb_save_tdesc_command
,
3306 .mode
= COMMAND_EXEC
,
3307 .help
= "Save the target description file",
3309 COMMAND_REGISTRATION_DONE
3312 int gdb_register_commands(struct command_context
*cmd_ctx
)
3314 gdb_port
= strdup("3333");
3315 gdb_port_next
= strdup("3333");
3316 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);