1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * Copyright (C) 2011 by Broadcom Corporation *
12 * Evan Hunter - ehunter@broadcom.com *
14 * Copyright (C) ST-Ericsson SA 2011 *
15 * michel.jaouen@stericsson.com : smp minimum support *
17 * Copyright (C) 2013 Andes Technology *
18 * Hsiangkai Wang <hkwang@andestech.com> *
20 * Copyright (C) 2013 Franck Jullien *
21 * elec4fun@gmail.com *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the *
35 * Free Software Foundation, Inc., *
36 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
37 ***************************************************************************/
43 #include <target/breakpoints.h>
44 #include <target/target_request.h>
45 #include <target/register.h>
47 #include <flash/nor/core.h>
48 #include "gdb_server.h"
49 #include <target/image.h>
50 #include <jtag/jtag.h>
51 #include "rtos/rtos.h"
52 #include "target/smp.h"
56 * GDB server implementation.
58 * This implements the GDB Remote Serial Protocol, over TCP connections,
59 * giving GDB access to the JTAG or other hardware debugging facilities
60 * found in most modern embedded processors.
63 /* private connection data for GDB */
64 struct gdb_connection
{
65 char buffer
[GDB_BUFFER_SIZE
];
69 enum target_state frontend_state
;
70 struct image
*vflash_image
;
74 /* set flag to true if you want the next stepi to return immediately.
75 * allowing GDB to pick up a fresh set of register values from the target
76 * without modifying the target state. */
78 /* We delay reporting memory write errors until next step/continue or memory
79 * write. This improves performance of gdb load significantly as the GDB packet
80 * can be replied immediately and a new GDB packet will be ready without delay
81 * (ca. 10% or so...). */
83 /* with extended-remote it seems we need to better emulate attach/detach.
84 * what this means is we reply with a W stop reply after a kill packet,
85 * normally we reply with a S reply via gdb_last_signal_packet.
86 * as a side note this behaviour only effects gdb > 6.8 */
91 #define _DEBUG_GDB_IO_
94 static struct gdb_connection
*current_gdb_connection
;
96 static int gdb_breakpoint_override
;
97 static enum breakpoint_type gdb_breakpoint_override_type
;
99 static int gdb_error(struct connection
*connection
, int retval
);
100 static const char *gdb_port
;
101 static const char *gdb_port_next
;
103 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
104 const char *function
, const char *string
);
106 /* number of gdb connections, mainly to suppress gdb related debugging spam
107 * in helper/log.c when no gdb connections are actually active */
108 int gdb_actual_connections
;
110 /* set if we are sending a memory map to gdb
111 * via qXfer:memory-map:read packet */
112 /* enabled by default*/
113 static int gdb_use_memory_map
= 1;
114 /* enabled by default*/
115 static int gdb_flash_program
= 1;
117 /* if set, data aborts cause an error to be reported in memory read packets
118 * see the code in gdb_read_memory_packet() for further explanations.
119 * Disabled by default.
121 static int gdb_report_data_abort
;
123 /* set if we are sending target descriptions to gdb
124 * via qXfer:features:read packet */
125 /* disabled by default */
126 static int gdb_use_target_description
;
128 static int gdb_last_signal(struct target
*target
)
130 switch (target
->debug_reason
) {
131 case DBG_REASON_DBGRQ
:
132 return 0x2; /* SIGINT */
133 case DBG_REASON_BREAKPOINT
:
134 case DBG_REASON_WATCHPOINT
:
135 case DBG_REASON_WPTANDBKPT
:
136 return 0x05; /* SIGTRAP */
137 case DBG_REASON_SINGLESTEP
:
138 return 0x05; /* SIGTRAP */
139 case DBG_REASON_NOTHALTED
:
140 return 0x0; /* no signal... shouldn't happen */
142 LOG_USER("undefined debug reason %d - target needs reset",
143 target
->debug_reason
);
148 static int check_pending(struct connection
*connection
,
149 int timeout_s
, int *got_data
)
151 /* a non-blocking socket will block if there is 0 bytes available on the socket,
152 * but return with as many bytes as are available immediately
156 struct gdb_connection
*gdb_con
= connection
->priv
;
158 if (got_data
== NULL
)
162 if (gdb_con
->buf_cnt
> 0) {
168 FD_SET(connection
->fd
, &read_fds
);
170 tv
.tv_sec
= timeout_s
;
172 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
173 /* This can typically be because a "monitor" command took too long
174 * before printing any progress messages
177 return ERROR_GDB_TIMEOUT
;
181 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
185 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
187 struct gdb_connection
*gdb_con
= connection
->priv
;
188 int retval
= ERROR_OK
;
190 #ifdef _DEBUG_GDB_IO_
194 if (connection
->service
->type
!= CONNECTION_TCP
)
195 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
197 retval
= check_pending(connection
, 1, NULL
);
198 if (retval
!= ERROR_OK
)
200 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
205 if (gdb_con
->buf_cnt
> 0)
207 if (gdb_con
->buf_cnt
== 0) {
209 return ERROR_SERVER_REMOTE_CLOSED
;
213 errno
= WSAGetLastError();
219 case WSAECONNABORTED
:
221 return ERROR_SERVER_REMOTE_CLOSED
;
224 return ERROR_SERVER_REMOTE_CLOSED
;
226 LOG_ERROR("read: %d", errno
);
236 return ERROR_SERVER_REMOTE_CLOSED
;
239 return ERROR_SERVER_REMOTE_CLOSED
;
241 LOG_ERROR("read: %s", strerror(errno
));
243 return ERROR_SERVER_REMOTE_CLOSED
;
248 #ifdef _DEBUG_GDB_IO_
249 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
250 LOG_DEBUG("received '%s'", debug_buffer
);
254 gdb_con
->buf_p
= gdb_con
->buffer
;
256 *next_char
= *(gdb_con
->buf_p
++);
257 if (gdb_con
->buf_cnt
> 0)
258 connection
->input_pending
= 1;
260 connection
->input_pending
= 0;
261 #ifdef _DEBUG_GDB_IO_
262 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
269 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
270 * held in registers in the inner loop.
272 * For small caches and embedded systems this is important!
274 static inline int gdb_get_char_fast(struct connection
*connection
,
275 int *next_char
, char **buf_p
, int *buf_cnt
)
277 int retval
= ERROR_OK
;
279 if ((*buf_cnt
)-- > 0) {
280 *next_char
= **buf_p
;
283 connection
->input_pending
= 1;
285 connection
->input_pending
= 0;
287 #ifdef _DEBUG_GDB_IO_
288 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
294 struct gdb_connection
*gdb_con
= connection
->priv
;
295 gdb_con
->buf_p
= *buf_p
;
296 gdb_con
->buf_cnt
= *buf_cnt
;
297 retval
= gdb_get_char_inner(connection
, next_char
);
298 *buf_p
= gdb_con
->buf_p
;
299 *buf_cnt
= gdb_con
->buf_cnt
;
304 static int gdb_get_char(struct connection
*connection
, int *next_char
)
306 struct gdb_connection
*gdb_con
= connection
->priv
;
307 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
310 static int gdb_putback_char(struct connection
*connection
, int last_char
)
312 struct gdb_connection
*gdb_con
= connection
->priv
;
314 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
315 *(--gdb_con
->buf_p
) = last_char
;
318 LOG_ERROR("BUG: couldn't put character back");
323 /* The only way we can detect that the socket is closed is the first time
324 * we write to it, we will fail. Subsequent write operations will
325 * succeed. Shudder! */
326 static int gdb_write(struct connection
*connection
, void *data
, int len
)
328 struct gdb_connection
*gdb_con
= connection
->priv
;
330 return ERROR_SERVER_REMOTE_CLOSED
;
332 if (connection_write(connection
, data
, len
) == len
)
335 return ERROR_SERVER_REMOTE_CLOSED
;
338 static int gdb_put_packet_inner(struct connection
*connection
,
339 char *buffer
, int len
)
342 unsigned char my_checksum
= 0;
343 #ifdef _DEBUG_GDB_IO_
348 struct gdb_connection
*gdb_con
= connection
->priv
;
350 for (i
= 0; i
< len
; i
++)
351 my_checksum
+= buffer
[i
];
353 #ifdef _DEBUG_GDB_IO_
355 * At this point we should have nothing in the input queue from GDB,
356 * however sometimes '-' is sent even though we've already received
357 * an ACK (+) for everything we've sent off.
361 retval
= check_pending(connection
, 0, &gotdata
);
362 if (retval
!= ERROR_OK
)
366 retval
= gdb_get_char(connection
, &reply
);
367 if (retval
!= ERROR_OK
)
370 /* fix a problem with some IAR tools */
371 gdb_putback_char(connection
, reply
);
372 LOG_DEBUG("Unexpected start of new packet");
376 LOG_WARNING("Discard unexpected char %c", reply
);
381 #ifdef _DEBUG_GDB_IO_
382 debug_buffer
= strndup(buffer
, len
);
383 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
387 char local_buffer
[1024];
388 local_buffer
[0] = '$';
389 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
390 /* performance gain on smaller packets by only a single call to gdb_write() */
391 memcpy(local_buffer
+ 1, buffer
, len
++);
392 len
+= snprintf(local_buffer
+ len
, sizeof(local_buffer
) - len
, "#%02x", my_checksum
);
393 retval
= gdb_write(connection
, local_buffer
, len
);
394 if (retval
!= ERROR_OK
)
397 /* larger packets are transmitted directly from caller supplied buffer
398 * by several calls to gdb_write() to avoid dynamic allocation */
399 snprintf(local_buffer
+ 1, sizeof(local_buffer
) - 1, "#%02x", my_checksum
);
400 retval
= gdb_write(connection
, local_buffer
, 1);
401 if (retval
!= ERROR_OK
)
403 retval
= gdb_write(connection
, buffer
, len
);
404 if (retval
!= ERROR_OK
)
406 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
407 if (retval
!= ERROR_OK
)
411 if (gdb_con
->noack_mode
)
414 retval
= gdb_get_char(connection
, &reply
);
415 if (retval
!= ERROR_OK
)
420 else if (reply
== '-') {
421 /* Stop sending output packets for now */
422 log_remove_callback(gdb_log_callback
, connection
);
423 LOG_WARNING("negative reply, retrying");
424 } else if (reply
== 0x3) {
426 retval
= gdb_get_char(connection
, &reply
);
427 if (retval
!= ERROR_OK
)
431 else if (reply
== '-') {
432 /* Stop sending output packets for now */
433 log_remove_callback(gdb_log_callback
, connection
);
434 LOG_WARNING("negative reply, retrying");
435 } else if (reply
== '$') {
436 LOG_ERROR("GDB missing ack(1) - assumed good");
437 gdb_putback_char(connection
, reply
);
440 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
442 return ERROR_SERVER_REMOTE_CLOSED
;
444 } else if (reply
== '$') {
445 LOG_ERROR("GDB missing ack(2) - assumed good");
446 gdb_putback_char(connection
, reply
);
449 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
452 return ERROR_SERVER_REMOTE_CLOSED
;
456 return ERROR_SERVER_REMOTE_CLOSED
;
461 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
463 struct gdb_connection
*gdb_con
= connection
->priv
;
465 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
468 /* we sent some data, reset timer for keep alive messages */
474 static inline int fetch_packet(struct connection
*connection
,
475 int *checksum_ok
, int noack
, int *len
, char *buffer
)
477 unsigned char my_checksum
= 0;
480 int retval
= ERROR_OK
;
482 struct gdb_connection
*gdb_con
= connection
->priv
;
487 /* move this over into local variables to use registers and give the
488 * more freedom to optimize */
489 char *buf_p
= gdb_con
->buf_p
;
490 int buf_cnt
= gdb_con
->buf_cnt
;
493 /* The common case is that we have an entire packet with no escape chars.
494 * We need to leave at least 2 bytes in the buffer to have
495 * gdb_get_char() update various bits and bobs correctly.
497 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
498 /* The compiler will struggle a bit with constant propagation and
499 * aliasing, so we help it by showing that these values do not
500 * change inside the loop
504 int run
= buf_cnt
- 2;
510 if (character
== '#') {
511 /* Danger! character can be '#' when esc is
512 * used so we need an explicit boolean for done here. */
517 if (character
== '}') {
518 /* data transmitted in binary mode (X packet)
519 * uses 0x7d as escape character */
520 my_checksum
+= character
& 0xff;
523 my_checksum
+= character
& 0xff;
524 buffer
[count
++] = (character
^ 0x20) & 0xff;
526 my_checksum
+= character
& 0xff;
527 buffer
[count
++] = character
& 0xff;
536 LOG_ERROR("packet buffer too small");
537 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
541 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
542 if (retval
!= ERROR_OK
)
545 if (character
== '#')
548 if (character
== '}') {
549 /* data transmitted in binary mode (X packet)
550 * uses 0x7d as escape character */
551 my_checksum
+= character
& 0xff;
553 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
554 if (retval
!= ERROR_OK
)
557 my_checksum
+= character
& 0xff;
558 buffer
[count
++] = (character
^ 0x20) & 0xff;
560 my_checksum
+= character
& 0xff;
561 buffer
[count
++] = character
& 0xff;
565 gdb_con
->buf_p
= buf_p
;
566 gdb_con
->buf_cnt
= buf_cnt
;
568 if (retval
!= ERROR_OK
)
573 retval
= gdb_get_char(connection
, &character
);
574 if (retval
!= ERROR_OK
)
576 checksum
[0] = character
;
577 retval
= gdb_get_char(connection
, &character
);
578 if (retval
!= ERROR_OK
)
580 checksum
[1] = character
;
584 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
589 static int gdb_get_packet_inner(struct connection
*connection
,
590 char *buffer
, int *len
)
594 struct gdb_connection
*gdb_con
= connection
->priv
;
598 retval
= gdb_get_char(connection
, &character
);
599 if (retval
!= ERROR_OK
)
602 #ifdef _DEBUG_GDB_IO_
603 LOG_DEBUG("character: '%c'", character
);
610 /* gdb sends a dummy ack '+' at every remote connect - see
611 * remote_start_remote (remote.c)
612 * in case anyone tries to debug why they receive this
613 * warning every time */
614 LOG_WARNING("acknowledgment received, but no packet pending");
617 LOG_WARNING("negative acknowledgment, but no packet pending");
624 LOG_WARNING("ignoring character 0x%x", character
);
627 } while (character
!= '$');
630 /* explicit code expansion here to get faster inlined code in -O3 by not
631 * calculating checksum */
632 if (gdb_con
->noack_mode
) {
633 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
634 if (retval
!= ERROR_OK
)
637 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
638 if (retval
!= ERROR_OK
)
642 if (gdb_con
->noack_mode
) {
643 /* checksum is not checked in noack mode */
647 retval
= gdb_write(connection
, "+", 1);
648 if (retval
!= ERROR_OK
)
654 return ERROR_SERVER_REMOTE_CLOSED
;
659 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
661 struct gdb_connection
*gdb_con
= connection
->priv
;
663 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
668 static int gdb_output_con(struct connection
*connection
, const char *line
)
673 bin_size
= strlen(line
);
675 hex_buffer
= malloc(bin_size
* 2 + 2);
676 if (hex_buffer
== NULL
)
677 return ERROR_GDB_BUFFER_TOO_SMALL
;
680 int pkt_len
= hexify(hex_buffer
+ 1, line
, bin_size
, bin_size
* 2 + 1);
681 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
687 static int gdb_output(struct command_context
*context
, const char *line
)
689 /* this will be dumped to the log and also sent as an O packet if possible */
690 LOG_USER_N("%s", line
);
694 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
696 struct gdb_connection
*gdb_connection
= connection
->priv
;
698 /* In the GDB protocol when we are stepping or continuing execution,
699 * we have a lingering reply. Upon receiving a halted event
700 * when we have that lingering packet, we reply to the original
701 * step or continue packet.
703 * Executing monitor commands can bring the target in and
704 * out of the running state so we'll see lots of TARGET_EVENT_XXX
705 * that are to be ignored.
707 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
709 char stop_reason
[20];
713 /* stop forwarding log packets! */
714 log_remove_callback(gdb_log_callback
, connection
);
716 if (gdb_connection
->ctrl_c
) {
718 gdb_connection
->ctrl_c
= 0;
720 signal_var
= gdb_last_signal(target
);
722 stop_reason
[0] = '\0';
723 if (target
->debug_reason
== DBG_REASON_WATCHPOINT
) {
724 enum watchpoint_rw hit_wp_type
;
725 uint32_t hit_wp_address
;
727 if (watchpoint_hit(target
, &hit_wp_type
, &hit_wp_address
) == ERROR_OK
) {
729 switch (hit_wp_type
) {
731 snprintf(stop_reason
, sizeof(stop_reason
),
732 "watch:%08x;", hit_wp_address
);
735 snprintf(stop_reason
, sizeof(stop_reason
),
736 "rwatch:%08x;", hit_wp_address
);
739 snprintf(stop_reason
, sizeof(stop_reason
),
740 "awatch:%08x;", hit_wp_address
);
748 sig_reply_len
= snprintf(sig_reply
, sizeof(sig_reply
), "T%2.2x%s",
749 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
);
757 static int gdb_target_callback_event_handler(struct target
*target
,
758 enum target_event event
, void *priv
)
761 struct connection
*connection
= priv
;
764 case TARGET_EVENT_GDB_HALT
:
765 gdb_frontend_halted(target
, connection
);
767 case TARGET_EVENT_HALTED
:
768 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
770 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
771 retval
= jtag_execute_queue();
772 if (retval
!= ERROR_OK
)
782 static int gdb_new_connection(struct connection
*connection
)
784 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
785 struct gdb_service
*gdb_service
= connection
->service
->priv
;
789 connection
->priv
= gdb_connection
;
791 /* initialize gdb connection information */
792 gdb_connection
->buf_p
= gdb_connection
->buffer
;
793 gdb_connection
->buf_cnt
= 0;
794 gdb_connection
->ctrl_c
= 0;
795 gdb_connection
->frontend_state
= TARGET_HALTED
;
796 gdb_connection
->vflash_image
= NULL
;
797 gdb_connection
->closed
= 0;
798 gdb_connection
->busy
= 0;
799 gdb_connection
->noack_mode
= 0;
800 gdb_connection
->sync
= true;
801 gdb_connection
->mem_write_error
= false;
802 gdb_connection
->attached
= true;
804 /* send ACK to GDB for debug request */
805 gdb_write(connection
, "+", 1);
807 /* output goes through gdb connection */
808 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
810 /* we must remove all breakpoints registered to the target as a previous
811 * GDB session could leave dangling breakpoints if e.g. communication
814 breakpoint_clear_target(gdb_service
->target
);
815 watchpoint_clear_target(gdb_service
->target
);
817 /* clean previous rtos session if supported*/
818 if ((gdb_service
->target
->rtos
) && (gdb_service
->target
->rtos
->type
->clean
))
819 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
821 /* remove the initial ACK from the incoming buffer */
822 retval
= gdb_get_char(connection
, &initial_ack
);
823 if (retval
!= ERROR_OK
)
826 /* FIX!!!??? would we actually ever receive a + here???
829 if (initial_ack
!= '+')
830 gdb_putback_char(connection
, initial_ack
);
831 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
833 if (gdb_use_memory_map
) {
834 /* Connect must fail if the memory map can't be set up correctly.
836 * This will cause an auto_probe to be invoked, which is either
837 * a no-op or it will fail when the target isn't ready(e.g. not halted).
840 for (i
= 0; i
< flash_get_bank_count(); i
++) {
841 struct flash_bank
*p
;
842 retval
= get_flash_bank_by_num(i
, &p
);
843 if (retval
!= ERROR_OK
) {
844 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
845 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
851 gdb_actual_connections
++;
852 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
853 gdb_actual_connections
,
854 target_name(gdb_service
->target
),
855 target_state_name(gdb_service
->target
));
857 /* DANGER! If we fail subsequently, we must remove this handler,
858 * otherwise we occasionally see crashes as the timer can invoke the
861 * register callback to be informed about target events */
862 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
867 static int gdb_connection_closed(struct connection
*connection
)
869 struct gdb_service
*gdb_service
= connection
->service
->priv
;
870 struct gdb_connection
*gdb_connection
= connection
->priv
;
872 /* we're done forwarding messages. Tear down callback before
873 * cleaning up connection.
875 log_remove_callback(gdb_log_callback
, connection
);
877 gdb_actual_connections
--;
878 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
879 target_name(gdb_service
->target
),
880 target_state_name(gdb_service
->target
),
881 gdb_actual_connections
);
883 /* see if an image built with vFlash commands is left */
884 if (gdb_connection
->vflash_image
) {
885 image_close(gdb_connection
->vflash_image
);
886 free(gdb_connection
->vflash_image
);
887 gdb_connection
->vflash_image
= NULL
;
890 /* if this connection registered a debug-message receiver delete it */
891 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
893 if (connection
->priv
) {
894 free(connection
->priv
);
895 connection
->priv
= NULL
;
897 LOG_ERROR("BUG: connection->priv == NULL");
899 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
901 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
903 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
908 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
911 snprintf(err
, 4, "E%2.2X", the_error
);
912 gdb_put_packet(connection
, err
, 3);
915 static int gdb_last_signal_packet(struct connection
*connection
,
916 char *packet
, int packet_size
)
918 struct target
*target
= get_target_from_connection(connection
);
919 struct gdb_connection
*gdb_con
= connection
->priv
;
923 if (!gdb_con
->attached
) {
924 /* if we are here we have received a kill packet
925 * reply W stop reply otherwise gdb gets very unhappy */
926 gdb_put_packet(connection
, "W00", 3);
930 signal_var
= gdb_last_signal(target
);
932 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
933 gdb_put_packet(connection
, sig_reply
, 3);
938 static inline int gdb_reg_pos(struct target
*target
, int pos
, int len
)
940 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
943 return len
- 1 - pos
;
946 /* Convert register to string of bytes. NB! The # of bits in the
947 * register might be non-divisible by 8(a byte), in which
948 * case an entire byte is shown.
950 * NB! the format on the wire is the target endianness
952 * The format of reg->value is little endian
955 static void gdb_str_to_target(struct target
*target
,
956 char *tstr
, struct reg
*reg
)
963 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
965 for (i
= 0; i
< buf_len
; i
++) {
966 int j
= gdb_reg_pos(target
, i
, buf_len
);
967 tstr
+= sprintf(tstr
, "%02x", buf
[j
]);
971 /* copy over in register buffer */
972 static void gdb_target_to_reg(struct target
*target
,
973 char *tstr
, int str_len
, uint8_t *bin
)
976 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
981 for (i
= 0; i
< str_len
; i
+= 2) {
983 if (sscanf(tstr
+ i
, "%02x", &t
) != 1) {
984 LOG_ERROR("BUG: unable to convert register value");
988 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
993 static int gdb_get_registers_packet(struct connection
*connection
,
994 char *packet
, int packet_size
)
996 struct target
*target
= get_target_from_connection(connection
);
997 struct reg
**reg_list
;
1000 int reg_packet_size
= 0;
1005 #ifdef _DEBUG_GDB_IO_
1009 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1012 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1014 if (retval
!= ERROR_OK
)
1015 return gdb_error(connection
, retval
);
1017 for (i
= 0; i
< reg_list_size
; i
++)
1018 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1020 assert(reg_packet_size
> 0);
1022 reg_packet
= malloc(reg_packet_size
+ 1); /* plus one for string termination null */
1023 reg_packet_p
= reg_packet
;
1025 for (i
= 0; i
< reg_list_size
; i
++) {
1026 if (!reg_list
[i
]->valid
)
1027 reg_list
[i
]->type
->get(reg_list
[i
]);
1028 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1029 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1032 #ifdef _DEBUG_GDB_IO_
1034 char *reg_packet_p_debug
;
1035 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1036 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1037 free(reg_packet_p_debug
);
1041 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1049 static int gdb_set_registers_packet(struct connection
*connection
,
1050 char *packet
, int packet_size
)
1052 struct target
*target
= get_target_from_connection(connection
);
1054 struct reg
**reg_list
;
1059 #ifdef _DEBUG_GDB_IO_
1063 /* skip command character */
1067 if (packet_size
% 2) {
1068 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1069 return ERROR_SERVER_REMOTE_CLOSED
;
1072 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1074 if (retval
!= ERROR_OK
)
1075 return gdb_error(connection
, retval
);
1078 for (i
= 0; i
< reg_list_size
; i
++) {
1080 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1082 if (packet_p
+ chars
> packet
+ packet_size
)
1083 LOG_ERROR("BUG: register packet is too small for registers");
1085 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1086 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1088 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1090 /* advance packet pointer */
1096 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1099 gdb_put_packet(connection
, "OK", 2);
1104 static int gdb_get_register_packet(struct connection
*connection
,
1105 char *packet
, int packet_size
)
1107 struct target
*target
= get_target_from_connection(connection
);
1109 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1110 struct reg
**reg_list
;
1114 #ifdef _DEBUG_GDB_IO_
1118 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1120 if (retval
!= ERROR_OK
)
1121 return gdb_error(connection
, retval
);
1123 if (reg_list_size
<= reg_num
) {
1124 LOG_ERROR("gdb requested a non-existing register");
1125 return ERROR_SERVER_REMOTE_CLOSED
;
1128 if (!reg_list
[reg_num
]->valid
)
1129 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1131 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2 + 1); /* plus one for string termination null */
1133 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1135 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1143 static int gdb_set_register_packet(struct connection
*connection
,
1144 char *packet
, int packet_size
)
1146 struct target
*target
= get_target_from_connection(connection
);
1149 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1150 struct reg
**reg_list
;
1156 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
,
1158 if (retval
!= ERROR_OK
)
1159 return gdb_error(connection
, retval
);
1161 if (reg_list_size
<= reg_num
) {
1162 LOG_ERROR("gdb requested a non-existing register");
1163 return ERROR_SERVER_REMOTE_CLOSED
;
1166 if (*separator
!= '=') {
1167 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1168 return ERROR_SERVER_REMOTE_CLOSED
;
1171 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1172 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1173 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1175 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1176 LOG_ERROR("gdb sent a packet with wrong register size");
1178 return ERROR_SERVER_REMOTE_CLOSED
;
1181 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1183 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1185 gdb_put_packet(connection
, "OK", 2);
1193 /* No attempt is made to translate the "retval" to
1194 * GDB speak. This has to be done at the calling
1195 * site as no mapping really exists.
1197 static int gdb_error(struct connection
*connection
, int retval
)
1199 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1200 gdb_send_error(connection
, EFAULT
);
1204 /* We don't have to worry about the default 2 second timeout for GDB packets,
1205 * because GDB breaks up large memory reads into smaller reads.
1207 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1209 static int gdb_read_memory_packet(struct connection
*connection
,
1210 char *packet
, int packet_size
)
1212 struct target
*target
= get_target_from_connection(connection
);
1220 int retval
= ERROR_OK
;
1222 /* skip command character */
1225 addr
= strtoul(packet
, &separator
, 16);
1227 if (*separator
!= ',') {
1228 LOG_ERROR("incomplete read memory packet received, dropping connection");
1229 return ERROR_SERVER_REMOTE_CLOSED
;
1232 len
= strtoul(separator
+ 1, NULL
, 16);
1234 buffer
= malloc(len
);
1236 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1238 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1240 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1241 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1242 * At some point this might be fixed in GDB, in which case this code can be removed.
1244 * OpenOCD developers are acutely aware of this problem, but there is nothing
1245 * gained by involving the user in this problem that hopefully will get resolved
1248 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1249 * cmd = view%20audit-trail&database = gdb&pr = 2395
1251 * For now, the default is to fix up things to make current GDB versions work.
1252 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1254 memset(buffer
, 0, len
);
1258 if (retval
== ERROR_OK
) {
1259 hex_buffer
= malloc(len
* 2 + 1);
1261 int pkt_len
= hexify(hex_buffer
, (char *)buffer
, len
, len
* 2 + 1);
1263 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1267 retval
= gdb_error(connection
, retval
);
1274 static int gdb_write_memory_packet(struct connection
*connection
,
1275 char *packet
, int packet_size
)
1277 struct target
*target
= get_target_from_connection(connection
);
1285 /* skip command character */
1288 addr
= strtoul(packet
, &separator
, 16);
1290 if (*separator
!= ',') {
1291 LOG_ERROR("incomplete write memory packet received, dropping connection");
1292 return ERROR_SERVER_REMOTE_CLOSED
;
1295 len
= strtoul(separator
+ 1, &separator
, 16);
1297 if (*(separator
++) != ':') {
1298 LOG_ERROR("incomplete write memory packet received, dropping connection");
1299 return ERROR_SERVER_REMOTE_CLOSED
;
1302 buffer
= malloc(len
);
1304 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1306 if (unhexify((char *)buffer
, separator
, len
) != (int)len
)
1307 LOG_ERROR("unable to decode memory packet");
1309 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1311 if (retval
== ERROR_OK
)
1312 gdb_put_packet(connection
, "OK", 2);
1314 retval
= gdb_error(connection
, retval
);
1321 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1322 char *packet
, int packet_size
)
1324 struct target
*target
= get_target_from_connection(connection
);
1329 int retval
= ERROR_OK
;
1331 /* skip command character */
1334 addr
= strtoul(packet
, &separator
, 16);
1336 if (*separator
!= ',') {
1337 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1338 return ERROR_SERVER_REMOTE_CLOSED
;
1341 len
= strtoul(separator
+ 1, &separator
, 16);
1343 if (*(separator
++) != ':') {
1344 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1345 return ERROR_SERVER_REMOTE_CLOSED
;
1348 struct gdb_connection
*gdb_connection
= connection
->priv
;
1350 if (gdb_connection
->mem_write_error
) {
1351 retval
= ERROR_FAIL
;
1352 /* now that we have reported the memory write error, we can clear the condition */
1353 gdb_connection
->mem_write_error
= false;
1356 /* By replying the packet *immediately* GDB will send us a new packet
1357 * while we write the last one to the target.
1359 if (retval
== ERROR_OK
)
1360 gdb_put_packet(connection
, "OK", 2);
1362 retval
= gdb_error(connection
, retval
);
1363 if (retval
!= ERROR_OK
)
1368 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1370 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1371 if (retval
!= ERROR_OK
)
1372 gdb_connection
->mem_write_error
= true;
1378 static int gdb_step_continue_packet(struct connection
*connection
,
1379 char *packet
, int packet_size
)
1381 struct target
*target
= get_target_from_connection(connection
);
1383 uint32_t address
= 0x0;
1384 int retval
= ERROR_OK
;
1388 if (packet_size
> 1) {
1389 packet
[packet_size
] = 0;
1390 address
= strtoul(packet
+ 1, NULL
, 16);
1394 if (packet
[0] == 'c') {
1395 LOG_DEBUG("continue");
1396 /* resume at current address, don't handle breakpoints, not debugging */
1397 retval
= target_resume(target
, current
, address
, 0, 0);
1398 } else if (packet
[0] == 's') {
1400 /* step at current or address, don't handle breakpoints */
1401 retval
= target_step(target
, current
, address
, 0);
1406 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1407 char *packet
, int packet_size
)
1409 struct target
*target
= get_target_from_connection(connection
);
1411 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1412 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1420 type
= strtoul(packet
+ 1, &separator
, 16);
1422 if (type
== 0) /* memory breakpoint */
1423 bp_type
= BKPT_SOFT
;
1424 else if (type
== 1) /* hardware breakpoint */
1425 bp_type
= BKPT_HARD
;
1426 else if (type
== 2) /* write watchpoint */
1427 wp_type
= WPT_WRITE
;
1428 else if (type
== 3) /* read watchpoint */
1430 else if (type
== 4) /* access watchpoint */
1431 wp_type
= WPT_ACCESS
;
1433 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1434 return ERROR_SERVER_REMOTE_CLOSED
;
1437 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1438 bp_type
= gdb_breakpoint_override_type
;
1440 if (*separator
!= ',') {
1441 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1442 return ERROR_SERVER_REMOTE_CLOSED
;
1445 address
= strtoul(separator
+ 1, &separator
, 16);
1447 if (*separator
!= ',') {
1448 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1449 return ERROR_SERVER_REMOTE_CLOSED
;
1452 size
= strtoul(separator
+ 1, &separator
, 16);
1457 if (packet
[0] == 'Z') {
1458 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1459 if (retval
!= ERROR_OK
) {
1460 retval
= gdb_error(connection
, retval
);
1461 if (retval
!= ERROR_OK
)
1464 gdb_put_packet(connection
, "OK", 2);
1466 breakpoint_remove(target
, address
);
1467 gdb_put_packet(connection
, "OK", 2);
1474 if (packet
[0] == 'Z') {
1475 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1476 if (retval
!= ERROR_OK
) {
1477 retval
= gdb_error(connection
, retval
);
1478 if (retval
!= ERROR_OK
)
1481 gdb_put_packet(connection
, "OK", 2);
1483 watchpoint_remove(target
, address
);
1484 gdb_put_packet(connection
, "OK", 2);
1495 /* print out a string and allocate more space as needed,
1496 * mainly used for XML at this point
1498 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1499 const char *fmt
, ...)
1501 if (*retval
!= ERROR_OK
)
1506 if ((*xml
== NULL
) || (!first
)) {
1507 /* start by 0 to exercise all the code paths.
1508 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1510 *size
= *size
* 2 + 2;
1512 *xml
= realloc(*xml
, *size
);
1516 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1524 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1526 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1530 /* there was just enough or not enough space, allocate more. */
1535 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1539 /* Extract and NUL-terminate the annex. */
1541 while (*buf
&& *buf
!= ':')
1547 /* After the read marker and annex, qXfer looks like a
1548 * traditional 'm' packet. */
1550 *ofs
= strtoul(buf
, &separator
, 16);
1552 if (*separator
!= ',')
1555 *len
= strtoul(separator
+ 1, NULL
, 16);
1560 static int compare_bank(const void *a
, const void *b
)
1562 struct flash_bank
*b1
, *b2
;
1563 b1
= *((struct flash_bank
**)a
);
1564 b2
= *((struct flash_bank
**)b
);
1566 if (b1
->base
== b2
->base
)
1568 else if (b1
->base
> b2
->base
)
1574 static int gdb_memory_map(struct connection
*connection
,
1575 char *packet
, int packet_size
)
1577 /* We get away with only specifying flash here. Regions that are not
1578 * specified are treated as if we provided no memory map(if not we
1579 * could detect the holes and mark them as RAM).
1580 * Normally we only execute this code once, but no big deal if we
1581 * have to regenerate it a couple of times.
1584 struct target
*target
= get_target_from_connection(connection
);
1585 struct flash_bank
*p
;
1589 int retval
= ERROR_OK
;
1590 struct flash_bank
**banks
;
1594 uint32_t ram_start
= 0;
1596 int target_flash_banks
= 0;
1598 /* skip command character */
1601 offset
= strtoul(packet
, &separator
, 16);
1602 length
= strtoul(separator
+ 1, &separator
, 16);
1604 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1606 /* Sort banks in ascending order. We need to report non-flash
1607 * memory as ram (or rather read/write) by default for GDB, since
1608 * it has no concept of non-cacheable read/write memory (i/o etc).
1610 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1611 * Current versions of GDB assume unlisted addresses are RAM...
1613 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1615 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1616 retval
= get_flash_bank_by_num(i
, &p
);
1617 if (retval
!= ERROR_OK
) {
1619 gdb_error(connection
, retval
);
1622 if (p
->target
== target
)
1623 banks
[target_flash_banks
++] = p
;
1626 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1629 for (i
= 0; i
< target_flash_banks
; i
++) {
1631 unsigned sector_size
= 0;
1637 if (ram_start
< p
->base
)
1638 xml_printf(&retval
, &xml
, &pos
, &size
,
1639 "<memory type=\"ram\" start=\"0x%x\" "
1640 "length=\"0x%x\"/>\n",
1641 ram_start
, p
->base
- ram_start
);
1643 /* Report adjacent groups of same-size sectors. So for
1644 * example top boot CFI flash will list an initial region
1645 * with several large sectors (maybe 128KB) and several
1646 * smaller ones at the end (maybe 32KB). STR7 will have
1647 * regions with 8KB, 32KB, and 64KB sectors; etc.
1649 for (j
= 0; j
< p
->num_sectors
; j
++) {
1652 /* Maybe start a new group of sectors. */
1653 if (sector_size
== 0) {
1654 start
= p
->base
+ p
->sectors
[j
].offset
;
1655 xml_printf(&retval
, &xml
, &pos
, &size
,
1656 "<memory type=\"flash\" "
1659 sector_size
= p
->sectors
[j
].size
;
1662 /* Does this finish a group of sectors?
1663 * If not, continue an already-started group.
1665 if (j
== p
->num_sectors
- 1)
1666 group_len
= (p
->base
+ p
->size
) - start
;
1667 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1668 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1673 xml_printf(&retval
, &xml
, &pos
, &size
,
1674 "length=\"0x%x\">\n"
1675 "<property name=\"blocksize\">"
1683 ram_start
= p
->base
+ p
->size
;
1687 xml_printf(&retval
, &xml
, &pos
, &size
,
1688 "<memory type=\"ram\" start=\"0x%x\" "
1689 "length=\"0x%x\"/>\n",
1690 ram_start
, 0-ram_start
);
1691 /* ELSE a flash chip could be at the very end of the 32 bit address
1692 * space, in which case ram_start will be precisely 0
1698 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1700 if (retval
!= ERROR_OK
) {
1701 gdb_error(connection
, retval
);
1705 if (offset
+ length
> pos
)
1706 length
= pos
- offset
;
1708 char *t
= malloc(length
+ 1);
1710 memcpy(t
+ 1, xml
+ offset
, length
);
1711 gdb_put_packet(connection
, t
, length
+ 1);
1718 static const char *gdb_get_reg_type_name(enum reg_type type
)
1723 case REG_TYPE_INT16
:
1725 case REG_TYPE_INT32
:
1727 case REG_TYPE_INT64
:
1729 case REG_TYPE_INT128
:
1731 case REG_TYPE_UINT8
:
1733 case REG_TYPE_UINT16
:
1735 case REG_TYPE_UINT32
:
1737 case REG_TYPE_UINT64
:
1739 case REG_TYPE_UINT128
:
1741 case REG_TYPE_CODE_PTR
:
1743 case REG_TYPE_DATA_PTR
:
1745 case REG_TYPE_IEEE_SINGLE
:
1746 return "ieee_single";
1747 case REG_TYPE_IEEE_DOUBLE
:
1748 return "ieee_double";
1749 case REG_TYPE_ARCH_DEFINED
:
1750 return "int"; /* return arbitrary string to avoid compile warning. */
1753 return "int"; /* "int" as default value */
1756 static int gdb_generate_reg_type_description(struct target
*target
,
1757 char **tdesc
, int *pos
, int *size
, struct reg_data_type
*type
)
1759 int retval
= ERROR_OK
;
1761 if (type
->type_class
== REG_TYPE_CLASS_VECTOR
) {
1762 /* <vector id="id" type="type" count="count"/> */
1763 xml_printf(&retval
, tdesc
, pos
, size
,
1764 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
1765 type
->id
, type
->reg_type_vector
->type
->id
,
1766 type
->reg_type_vector
->count
);
1768 } else if (type
->type_class
== REG_TYPE_CLASS_UNION
) {
1770 * <field name="name" type="type"/> ...
1772 xml_printf(&retval
, tdesc
, pos
, size
,
1773 "<union id=\"%s\">\n",
1776 struct reg_data_type_union_field
*field
;
1777 field
= type
->reg_type_union
->fields
;
1778 while (field
!= NULL
) {
1779 xml_printf(&retval
, tdesc
, pos
, size
,
1780 "<field name=\"%s\" type=\"%s\"/>\n",
1781 field
->name
, field
->type
->id
);
1783 field
= field
->next
;
1786 xml_printf(&retval
, tdesc
, pos
, size
,
1789 } else if (type
->type_class
== REG_TYPE_CLASS_STRUCT
) {
1790 struct reg_data_type_struct_field
*field
;
1791 field
= type
->reg_type_struct
->fields
;
1793 if (field
->use_bitfields
) {
1794 /* <struct id="id" size="size">
1795 * <field name="name" start="start" end="end"/> ...
1797 xml_printf(&retval
, tdesc
, pos
, size
,
1798 "<struct id=\"%s\" size=\"%d\">\n",
1799 type
->id
, type
->reg_type_struct
->size
);
1800 while (field
!= NULL
) {
1801 xml_printf(&retval
, tdesc
, pos
, size
,
1802 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1803 field
->name
, field
->bitfield
->start
,
1804 field
->bitfield
->end
);
1806 field
= field
->next
;
1810 * <field name="name" type="type"/> ...
1812 xml_printf(&retval
, tdesc
, pos
, size
,
1813 "<struct id=\"%s\">\n",
1815 while (field
!= NULL
) {
1816 xml_printf(&retval
, tdesc
, pos
, size
,
1817 "<field name=\"%s\" type=\"%s\"/>\n",
1818 field
->name
, field
->type
->id
);
1820 field
= field
->next
;
1824 xml_printf(&retval
, tdesc
, pos
, size
,
1827 } else if (type
->type_class
== REG_TYPE_CLASS_FLAGS
) {
1828 /* <flags id="id" size="size">
1829 * <field name="name" start="start" end="end"/> ...
1831 xml_printf(&retval
, tdesc
, pos
, size
,
1832 "<flags id=\"%s\" size=\"%d\">\n",
1833 type
->id
, type
->reg_type_flags
->size
);
1835 struct reg_data_type_flags_field
*field
;
1836 field
= type
->reg_type_flags
->fields
;
1837 while (field
!= NULL
) {
1838 xml_printf(&retval
, tdesc
, pos
, size
,
1839 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
1840 field
->name
, field
->bitfield
->start
, field
->bitfield
->end
);
1842 field
= field
->next
;
1845 xml_printf(&retval
, tdesc
, pos
, size
,
1853 /* Get a list of available target registers features. feature_list must
1854 * be freed by caller.
1856 int get_reg_features_list(struct target
*target
, char **feature_list
[], int *feature_list_size
,
1857 struct reg
**reg_list
, int reg_list_size
)
1861 /* Start with only one element */
1862 *feature_list
= calloc(1, sizeof(char *));
1864 for (int i
= 0; i
< reg_list_size
; i
++) {
1865 if (reg_list
[i
]->exist
== false)
1868 if ((reg_list
[i
]->feature
->name
!= NULL
)
1869 && (strcmp(reg_list
[i
]->feature
->name
, ""))) {
1870 /* We found a feature, check if the feature is already in the
1871 * table. If not, allocate a new entry for the table and
1872 * put the new feature in it.
1874 for (int j
= 0; j
< (tbl_sz
+ 1); j
++) {
1875 if (!((*feature_list
)[j
])) {
1876 (*feature_list
)[tbl_sz
++] = strdup(reg_list
[i
]->feature
->name
);
1877 *feature_list
= realloc(*feature_list
, sizeof(char *) * (tbl_sz
+ 1));
1878 (*feature_list
)[tbl_sz
] = NULL
;
1881 if (!strcmp((*feature_list
)[j
], reg_list
[i
]->feature
->name
))
1888 if (feature_list_size
)
1889 *feature_list_size
= tbl_sz
;
1894 static int gdb_generate_target_description(struct target
*target
, char **tdesc
)
1896 int retval
= ERROR_OK
;
1897 struct reg
**reg_list
;
1902 xml_printf(&retval
, tdesc
, &pos
, &size
,
1903 "<?xml version=\"1.0\"?>\n"
1904 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
1905 "<target version=\"1.0\">\n");
1907 retval
= target_get_gdb_reg_list(target
, ®_list
,
1908 ®_list_size
, REG_CLASS_ALL
);
1910 if (retval
!= ERROR_OK
) {
1911 LOG_ERROR("get register list failed");
1915 if (reg_list_size
<= 0)
1918 char **features
= NULL
;
1919 /* Get a list of available target registers features */
1920 retval
= get_reg_features_list(target
, &features
, NULL
, reg_list
, reg_list_size
);
1921 if (retval
!= ERROR_OK
) {
1922 LOG_ERROR("Can't get the registers feature list");
1926 /* If we found some features associated with registers, create sections */
1927 int current_feature
= 0;
1929 /* generate target description according to register list */
1930 if (features
!= NULL
) {
1931 while (features
[current_feature
]) {
1933 xml_printf(&retval
, tdesc
, &pos
, &size
,
1934 "<feature name=\"%s\">\n",
1935 features
[current_feature
]);
1938 for (i
= 0; i
< reg_list_size
; i
++) {
1940 if (reg_list
[i
]->exist
== false)
1943 if (strcmp(reg_list
[i
]->feature
->name
, features
[current_feature
]))
1946 const char *type_str
;
1947 if (reg_list
[i
]->reg_data_type
!= NULL
) {
1948 if (reg_list
[i
]->reg_data_type
->type
== REG_TYPE_ARCH_DEFINED
) {
1949 /* generate <type... first, if there are architecture-defined types. */
1950 gdb_generate_reg_type_description(target
, tdesc
, &pos
, &size
,
1951 reg_list
[i
]->reg_data_type
);
1953 type_str
= reg_list
[i
]->reg_data_type
->id
;
1955 /* predefined type */
1956 type_str
= gdb_get_reg_type_name(
1957 reg_list
[i
]->reg_data_type
->type
);
1960 /* Default type is "int" */
1964 xml_printf(&retval
, tdesc
, &pos
, &size
,
1965 "<reg name=\"%s\"", reg_list
[i
]->name
);
1966 xml_printf(&retval
, tdesc
, &pos
, &size
,
1967 " bitsize=\"%d\"", reg_list
[i
]->size
);
1968 xml_printf(&retval
, tdesc
, &pos
, &size
,
1969 " regnum=\"%d\"", reg_list
[i
]->number
);
1970 if (reg_list
[i
]->caller_save
)
1971 xml_printf(&retval
, tdesc
, &pos
, &size
,
1972 " save-restore=\"yes\"");
1974 xml_printf(&retval
, tdesc
, &pos
, &size
,
1975 " save-restore=\"no\"");
1977 xml_printf(&retval
, tdesc
, &pos
, &size
,
1978 " type=\"%s\"", type_str
);
1980 if (reg_list
[i
]->group
!= NULL
)
1981 xml_printf(&retval
, tdesc
, &pos
, &size
,
1982 " group=\"%s\"", reg_list
[i
]->group
);
1984 xml_printf(&retval
, tdesc
, &pos
, &size
,
1988 xml_printf(&retval
, tdesc
, &pos
, &size
,
1995 xml_printf(&retval
, tdesc
, &pos
, &size
,
1998 if (reg_list
!= NULL
)
2001 if (features
!= NULL
)
2007 static int gdb_get_target_description_chunk(struct target
*target
, char **chunk
,
2008 int32_t offset
, uint32_t length
)
2011 static uint32_t tdesc_length
;
2013 if (tdesc
== NULL
) {
2014 gdb_generate_target_description(target
, &tdesc
);
2015 tdesc_length
= strlen(tdesc
);
2020 if (length
< (tdesc_length
- offset
))
2021 transfer_type
= 'm';
2023 transfer_type
= 'l';
2025 *chunk
= malloc(length
+ 2);
2026 (*chunk
)[0] = transfer_type
;
2027 if (transfer_type
== 'm') {
2028 strncpy((*chunk
) + 1, tdesc
+ offset
, length
);
2029 (*chunk
)[1 + length
] = '\0';
2031 strncpy((*chunk
) + 1, tdesc
+ offset
, tdesc_length
- offset
);
2032 (*chunk
)[1 + (tdesc_length
- offset
)] = '\0';
2034 /* After gdb-server sends out last chunk, invalidate tdesc. */
2043 static int gdb_query_packet(struct connection
*connection
,
2044 char *packet
, int packet_size
)
2046 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
2047 struct gdb_connection
*gdb_connection
= connection
->priv
;
2048 struct target
*target
= get_target_from_connection(connection
);
2050 if (strncmp(packet
, "qRcmd,", 6) == 0) {
2051 if (packet_size
> 6) {
2053 cmd
= malloc((packet_size
- 6) / 2 + 1);
2054 int len
= unhexify(cmd
, packet
+ 6, (packet_size
- 6) / 2);
2057 /* We want to print all debug output to GDB connection */
2058 log_add_callback(gdb_log_callback
, connection
);
2059 target_call_timer_callbacks_now();
2060 /* some commands need to know the GDB connection, make note of current
2061 * GDB connection. */
2062 current_gdb_connection
= gdb_connection
;
2063 command_run_line(cmd_ctx
, cmd
);
2064 current_gdb_connection
= NULL
;
2065 target_call_timer_callbacks_now();
2066 log_remove_callback(gdb_log_callback
, connection
);
2069 gdb_put_packet(connection
, "OK", 2);
2071 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
2072 if (packet_size
> 5) {
2080 /* skip command character */
2083 addr
= strtoul(packet
, &separator
, 16);
2085 if (*separator
!= ',') {
2086 LOG_ERROR("incomplete read memory packet received, dropping connection");
2087 return ERROR_SERVER_REMOTE_CLOSED
;
2090 len
= strtoul(separator
+ 1, NULL
, 16);
2092 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
2094 if (retval
== ERROR_OK
) {
2095 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
2096 gdb_put_packet(connection
, gdb_reply
, 9);
2098 retval
= gdb_error(connection
, retval
);
2099 if (retval
!= ERROR_OK
)
2105 } else if (strncmp(packet
, "qSupported", 10) == 0) {
2106 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2107 * disable qXfer:features:read for the moment */
2108 int retval
= ERROR_OK
;
2109 char *buffer
= NULL
;
2117 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;QStartNoAckMode+",
2118 (GDB_BUFFER_SIZE
- 1),
2119 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2120 (gdb_use_target_description
== 1) ? '+' : '-');
2122 if (retval
!= ERROR_OK
) {
2123 gdb_send_error(connection
, 01);
2127 gdb_put_packet(connection
, buffer
, strlen(buffer
));
2131 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
2132 && (flash_get_bank_count() > 0))
2133 return gdb_memory_map(connection
, packet
, packet_size
);
2134 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
2136 int retval
= ERROR_OK
;
2139 unsigned int length
;
2142 /* skip command character */
2145 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0) {
2146 gdb_send_error(connection
, 01);
2150 /* Target should prepare correct target description for annex.
2151 * The first character of returned xml is 'm' or 'l'. 'm' for
2152 * there are *more* chunks to transfer. 'l' for it is the *last*
2153 * chunk of target description.
2155 retval
= gdb_get_target_description_chunk(target
, &xml
, offset
, length
);
2156 if (retval
!= ERROR_OK
) {
2157 gdb_error(connection
, retval
);
2161 gdb_put_packet(connection
, xml
, strlen(xml
));
2165 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
2166 gdb_connection
->noack_mode
= 1;
2167 gdb_put_packet(connection
, "OK", 2);
2171 gdb_put_packet(connection
, "", 0);
2175 static int gdb_v_packet(struct connection
*connection
,
2176 char *packet
, int packet_size
)
2178 struct gdb_connection
*gdb_connection
= connection
->priv
;
2179 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2182 /* if flash programming disabled - send a empty reply */
2184 if (gdb_flash_program
== 0) {
2185 gdb_put_packet(connection
, "", 0);
2189 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
2191 unsigned long length
;
2193 char *parse
= packet
+ 12;
2194 if (*parse
== '\0') {
2195 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2196 return ERROR_SERVER_REMOTE_CLOSED
;
2199 addr
= strtoul(parse
, &parse
, 16);
2201 if (*(parse
++) != ',' || *parse
== '\0') {
2202 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2203 return ERROR_SERVER_REMOTE_CLOSED
;
2206 length
= strtoul(parse
, &parse
, 16);
2208 if (*parse
!= '\0') {
2209 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2210 return ERROR_SERVER_REMOTE_CLOSED
;
2213 /* assume all sectors need erasing - stops any problems
2214 * when flash_write is called multiple times */
2217 /* perform any target specific operations before the erase */
2218 target_call_event_callbacks(gdb_service
->target
,
2219 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2221 /* vFlashErase:addr,length messages require region start and
2222 * end to be "block" aligned ... if padding is ever needed,
2223 * GDB will have become dangerously confused.
2225 result
= flash_erase_address_range(gdb_service
->target
,
2226 false, addr
, length
);
2228 /* perform any target specific operations after the erase */
2229 target_call_event_callbacks(gdb_service
->target
,
2230 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2233 if (result
!= ERROR_OK
) {
2234 /* GDB doesn't evaluate the actual error number returned,
2235 * treat a failed erase as an I/O error
2237 gdb_send_error(connection
, EIO
);
2238 LOG_ERROR("flash_erase returned %i", result
);
2240 gdb_put_packet(connection
, "OK", 2);
2245 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
2248 unsigned long length
;
2249 char *parse
= packet
+ 12;
2251 if (*parse
== '\0') {
2252 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2253 return ERROR_SERVER_REMOTE_CLOSED
;
2255 addr
= strtoul(parse
, &parse
, 16);
2256 if (*(parse
++) != ':') {
2257 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2258 return ERROR_SERVER_REMOTE_CLOSED
;
2260 length
= packet_size
- (parse
- packet
);
2262 /* create a new image if there isn't already one */
2263 if (gdb_connection
->vflash_image
== NULL
) {
2264 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2265 image_open(gdb_connection
->vflash_image
, "", "build");
2268 /* create new section with content from packet buffer */
2269 retval
= image_add_section(gdb_connection
->vflash_image
,
2270 addr
, length
, 0x0, (uint8_t *)parse
);
2271 if (retval
!= ERROR_OK
)
2274 gdb_put_packet(connection
, "OK", 2);
2279 if (strncmp(packet
, "vFlashDone", 10) == 0) {
2282 /* process the flashing buffer. No need to erase as GDB
2283 * always issues a vFlashErase first. */
2284 target_call_event_callbacks(gdb_service
->target
,
2285 TARGET_EVENT_GDB_FLASH_WRITE_START
);
2286 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2287 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2288 if (result
!= ERROR_OK
) {
2289 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2290 gdb_put_packet(connection
, "E.memtype", 9);
2292 gdb_send_error(connection
, EIO
);
2294 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2295 gdb_put_packet(connection
, "OK", 2);
2298 image_close(gdb_connection
->vflash_image
);
2299 free(gdb_connection
->vflash_image
);
2300 gdb_connection
->vflash_image
= NULL
;
2305 gdb_put_packet(connection
, "", 0);
2309 static int gdb_detach(struct connection
*connection
)
2311 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2313 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
2315 return gdb_put_packet(connection
, "OK", 2);
2318 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2319 const char *function
, const char *string
)
2321 struct connection
*connection
= priv
;
2322 struct gdb_connection
*gdb_con
= connection
->priv
;
2324 if (gdb_con
->busy
) {
2325 /* do not reply this using the O packet */
2329 gdb_output_con(connection
, string
);
2332 static void gdb_sig_halted(struct connection
*connection
)
2335 snprintf(sig_reply
, 4, "T%2.2x", 2);
2336 gdb_put_packet(connection
, sig_reply
, 3);
2339 static int gdb_input_inner(struct connection
*connection
)
2341 /* Do not allocate this on the stack */
2342 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2344 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2345 struct target
*target
= gdb_service
->target
;
2346 char *packet
= gdb_packet_buffer
;
2349 struct gdb_connection
*gdb_con
= connection
->priv
;
2350 static int extended_protocol
;
2352 /* drain input buffer. If one of the packets fail, then an error
2353 * packet is replied, if applicable.
2355 * This loop will terminate and the error code is returned.
2357 * The calling fn will check if this error is something that
2358 * can be recovered from, or if the connection must be closed.
2360 * If the error is recoverable, this fn is called again to
2361 * drain the rest of the buffer.
2364 packet_size
= GDB_BUFFER_SIZE
-1;
2365 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2366 if (retval
!= ERROR_OK
)
2369 /* terminate with zero */
2370 packet
[packet_size
] = 0;
2372 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2373 if (packet
[0] == 'X') {
2374 /* binary packets spew junk into the debug log stream */
2377 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
2380 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2382 LOG_DEBUG("received packet: '%s'", packet
);
2385 if (packet_size
> 0) {
2387 switch (packet
[0]) {
2388 case 'T': /* Is thread alive? */
2389 gdb_thread_packet(connection
, packet
, packet_size
);
2391 case 'H': /* Set current thread ( 'c' for step and continue,
2392 * 'g' for all other operations ) */
2393 gdb_thread_packet(connection
, packet
, packet_size
);
2397 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2398 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2399 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2402 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2405 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2408 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2411 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2414 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2417 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2421 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2424 gdb_last_signal_packet(connection
, packet
, packet_size
);
2429 gdb_thread_packet(connection
, packet
, packet_size
);
2430 log_add_callback(gdb_log_callback
, connection
);
2432 if (gdb_con
->mem_write_error
) {
2433 LOG_ERROR("Memory write failure!");
2435 /* now that we have reported the memory write error,
2436 * we can clear the condition */
2437 gdb_con
->mem_write_error
= false;
2440 bool nostep
= false;
2441 bool already_running
= false;
2442 if (target
->state
== TARGET_RUNNING
) {
2443 LOG_WARNING("WARNING! The target is already running. "
2444 "All changes GDB did to registers will be discarded! "
2445 "Waiting for target to halt.");
2446 already_running
= true;
2447 } else if (target
->state
!= TARGET_HALTED
) {
2448 LOG_WARNING("The target is not in the halted nor running stated, " \
2449 "stepi/continue ignored.");
2451 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
2452 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2453 * sent by GDB first to OpenOCD, thus defeating the check to
2454 * make only the single stepping have the sync feature...
2457 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2458 "from the target.");
2460 gdb_con
->sync
= false;
2462 if (!already_running
&& nostep
) {
2463 /* Either the target isn't in the halted state, then we can't
2464 * step/continue. This might be early setup, etc.
2466 * Or we want to allow GDB to pick up a fresh set of
2467 * register values without modifying the target state.
2470 gdb_sig_halted(connection
);
2472 /* stop forwarding log packets! */
2473 log_remove_callback(gdb_log_callback
, connection
);
2475 /* We're running/stepping, in which case we can
2476 * forward log output until the target is halted
2478 gdb_con
->frontend_state
= TARGET_RUNNING
;
2479 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2481 if (!already_running
) {
2482 /* Here we don't want packet processing to stop even if this fails,
2483 * so we use a local variable instead of retval. */
2484 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2485 if (retval
!= ERROR_OK
) {
2486 /* we'll never receive a halted
2487 * condition... issue a false one..
2489 gdb_frontend_halted(target
, connection
);
2496 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2499 retval
= gdb_detach(connection
);
2500 extended_protocol
= 0;
2503 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2504 if (retval
!= ERROR_OK
)
2508 if (extended_protocol
!= 0) {
2509 gdb_con
->attached
= false;
2512 gdb_put_packet(connection
, "OK", 2);
2513 return ERROR_SERVER_REMOTE_CLOSED
;
2515 /* handle extended remote protocol */
2516 extended_protocol
= 1;
2517 gdb_put_packet(connection
, "OK", 2);
2520 /* handle extended restart packet */
2521 breakpoint_clear_target(gdb_service
->target
);
2522 watchpoint_clear_target(gdb_service
->target
);
2523 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
2524 target_name(target
));
2525 /* set connection as attached after reset */
2526 gdb_con
->attached
= true;
2527 /* info rtos parts */
2528 gdb_thread_packet(connection
, packet
, packet_size
);
2532 /* packet supported only by smp target i.e cortex_a.c*/
2533 /* handle smp packet replying coreid played to gbd */
2534 gdb_read_smp_packet(connection
, packet
, packet_size
);
2538 /* packet supported only by smp target i.e cortex_a.c */
2539 /* handle smp packet setting coreid to be played at next
2541 gdb_write_smp_packet(connection
, packet
, packet_size
);
2545 /* ignore unknown packets */
2546 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2547 gdb_put_packet(connection
, NULL
, 0);
2551 /* if a packet handler returned an error, exit input loop */
2552 if (retval
!= ERROR_OK
)
2556 if (gdb_con
->ctrl_c
) {
2557 if (target
->state
== TARGET_RUNNING
) {
2558 retval
= target_halt(target
);
2559 if (retval
!= ERROR_OK
)
2560 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2561 gdb_con
->ctrl_c
= 0;
2563 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2564 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2568 } while (gdb_con
->buf_cnt
> 0);
2573 static int gdb_input(struct connection
*connection
)
2575 int retval
= gdb_input_inner(connection
);
2576 struct gdb_connection
*gdb_con
= connection
->priv
;
2577 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2580 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2581 if (gdb_con
->closed
)
2582 return ERROR_SERVER_REMOTE_CLOSED
;
2584 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2588 static int gdb_target_start(struct target
*target
, const char *port
)
2590 struct gdb_service
*gdb_service
;
2592 gdb_service
= malloc(sizeof(struct gdb_service
));
2594 if (NULL
== gdb_service
)
2597 gdb_service
->target
= target
;
2598 gdb_service
->core
[0] = -1;
2599 gdb_service
->core
[1] = -1;
2600 target
->gdb_service
= gdb_service
;
2602 ret
= add_service("gdb",
2603 port
, 1, &gdb_new_connection
, &gdb_input
,
2604 &gdb_connection_closed
, gdb_service
);
2605 /* initialialize all targets gdb service with the same pointer */
2607 struct target_list
*head
;
2608 struct target
*curr
;
2609 head
= target
->head
;
2610 while (head
!= (struct target_list
*)NULL
) {
2611 curr
= head
->target
;
2613 curr
->gdb_service
= gdb_service
;
2620 static int gdb_target_add_one(struct target
*target
)
2622 /* one gdb instance per smp list */
2623 if ((target
->smp
) && (target
->gdb_service
))
2625 int retval
= gdb_target_start(target
, gdb_port_next
);
2626 if (retval
== ERROR_OK
) {
2628 /* If we can parse the port number
2629 * then we increment the port number for the next target.
2632 portnumber
= strtol(gdb_port_next
, &end
, 0);
2634 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
2635 free((void *)gdb_port_next
);
2636 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2643 int gdb_target_add_all(struct target
*target
)
2645 if (NULL
== target
) {
2646 LOG_WARNING("gdb services need one or more targets defined");
2650 while (NULL
!= target
) {
2651 int retval
= gdb_target_add_one(target
);
2652 if (ERROR_OK
!= retval
)
2655 target
= target
->next
;
2661 COMMAND_HANDLER(handle_gdb_sync_command
)
2664 return ERROR_COMMAND_SYNTAX_ERROR
;
2666 if (current_gdb_connection
== NULL
) {
2667 command_print(CMD_CTX
,
2668 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2672 current_gdb_connection
->sync
= true;
2677 /* daemon configuration command gdb_port */
2678 COMMAND_HANDLER(handle_gdb_port_command
)
2680 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2681 if (ERROR_OK
== retval
) {
2682 free((void *)gdb_port_next
);
2683 gdb_port_next
= strdup(gdb_port
);
2688 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2691 return ERROR_COMMAND_SYNTAX_ERROR
;
2693 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2697 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2700 return ERROR_COMMAND_SYNTAX_ERROR
;
2702 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2706 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2709 return ERROR_COMMAND_SYNTAX_ERROR
;
2711 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2715 /* gdb_breakpoint_override */
2716 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2718 if (CMD_ARGC
== 0) {
2720 } else if (CMD_ARGC
== 1) {
2721 gdb_breakpoint_override
= 1;
2722 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2723 gdb_breakpoint_override_type
= BKPT_HARD
;
2724 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2725 gdb_breakpoint_override_type
= BKPT_SOFT
;
2726 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2727 gdb_breakpoint_override
= 0;
2729 return ERROR_COMMAND_SYNTAX_ERROR
;
2730 if (gdb_breakpoint_override
)
2731 LOG_USER("force %s breakpoints",
2732 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
2734 LOG_USER("breakpoint type is not overridden");
2739 COMMAND_HANDLER(handle_gdb_target_description_command
)
2742 return ERROR_COMMAND_SYNTAX_ERROR
;
2744 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_target_description
);
2748 COMMAND_HANDLER(handle_gdb_save_tdesc_command
)
2751 static uint32_t tdesc_length
;
2752 struct target
*target
= get_current_target(CMD_CTX
);
2753 char *tdesc_filename
;
2755 if (tdesc
== NULL
) {
2756 gdb_generate_target_description(target
, &tdesc
);
2757 tdesc_length
= strlen(tdesc
);
2760 struct fileio fileio
;
2761 size_t size_written
;
2763 tdesc_filename
= malloc(strlen(target_type_name(target
)) + 5);
2764 sprintf(tdesc_filename
, "%s.xml", target_type_name(target
));
2766 int retval
= fileio_open(&fileio
, tdesc_filename
, FILEIO_WRITE
, FILEIO_TEXT
);
2768 free(tdesc_filename
);
2770 if (retval
!= ERROR_OK
) {
2771 LOG_WARNING("Can't open %s for writing", tdesc_filename
);
2775 retval
= fileio_write(&fileio
, tdesc_length
, tdesc
, &size_written
);
2777 fileio_close(&fileio
);
2779 if (retval
!= ERROR_OK
) {
2780 LOG_WARNING("Error while writing the tdesc file");
2787 static const struct command_registration gdb_command_handlers
[] = {
2790 .handler
= handle_gdb_sync_command
,
2791 .mode
= COMMAND_ANY
,
2792 .help
= "next stepi will return immediately allowing "
2793 "GDB to fetch register state without affecting "
2799 .handler
= handle_gdb_port_command
,
2800 .mode
= COMMAND_ANY
,
2801 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2802 "server listens for the next port number after the "
2803 "base port number specified. "
2804 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2805 "output to stdout, an integer is base port number, \"disable\" disables "
2806 "port. Any other string is are interpreted as named pipe to listen to. "
2807 "Output pipe is the same name as input pipe, but with 'o' appended.",
2808 .usage
= "[port_num]",
2811 .name
= "gdb_memory_map",
2812 .handler
= handle_gdb_memory_map_command
,
2813 .mode
= COMMAND_CONFIG
,
2814 .help
= "enable or disable memory map",
2815 .usage
= "('enable'|'disable')"
2818 .name
= "gdb_flash_program",
2819 .handler
= handle_gdb_flash_program_command
,
2820 .mode
= COMMAND_CONFIG
,
2821 .help
= "enable or disable flash program",
2822 .usage
= "('enable'|'disable')"
2825 .name
= "gdb_report_data_abort",
2826 .handler
= handle_gdb_report_data_abort_command
,
2827 .mode
= COMMAND_CONFIG
,
2828 .help
= "enable or disable reporting data aborts",
2829 .usage
= "('enable'|'disable')"
2832 .name
= "gdb_breakpoint_override",
2833 .handler
= handle_gdb_breakpoint_override_command
,
2834 .mode
= COMMAND_ANY
,
2835 .help
= "Display or specify type of breakpoint "
2836 "to be used by gdb 'break' commands.",
2837 .usage
= "('hard'|'soft'|'disable')"
2840 .name
= "gdb_target_description",
2841 .handler
= handle_gdb_target_description_command
,
2842 .mode
= COMMAND_CONFIG
,
2843 .help
= "enable or disable target description",
2844 .usage
= "('enable'|'disable')"
2847 .name
= "gdb_save_tdesc",
2848 .handler
= handle_gdb_save_tdesc_command
,
2849 .mode
= COMMAND_EXEC
,
2850 .help
= "Save the target description file",
2852 COMMAND_REGISTRATION_DONE
2855 int gdb_register_commands(struct command_context
*cmd_ctx
)
2857 gdb_port
= strdup("3333");
2858 gdb_port_next
= strdup("3333");
2859 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);