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 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
37 #include <target/breakpoints.h>
38 #include <target/target_request.h>
39 #include <target/register.h>
41 #include <flash/nor/core.h>
42 #include "gdb_server.h"
43 #include <target/image.h>
44 #include <jtag/jtag.h>
45 #include "rtos/rtos.h"
46 #include "target/smp.h"
50 * GDB server implementation.
52 * This implements the GDB Remote Serial Protocol, over TCP connections,
53 * giving GDB access to the JTAG or other hardware debugging facilities
54 * found in most modern embedded processors.
57 /* private connection data for GDB */
58 struct gdb_connection
{
59 char buffer
[GDB_BUFFER_SIZE
];
63 enum target_state frontend_state
;
64 struct image
*vflash_image
;
68 /* set flag to true if you want the next stepi to return immediately.
69 * allowing GDB to pick up a fresh set of register values from the target
70 * without modifying the target state. */
72 /* We delay reporting memory write errors until next step/continue or memory
73 * write. This improves performance of gdb load significantly as the GDB packet
74 * can be replied immediately and a new GDB packet will be ready without delay
75 * (ca. 10% or so...). */
77 /* with extended-remote it seems we need to better emulate attach/detach.
78 * what this means is we reply with a W stop reply after a kill packet,
79 * normally we reply with a S reply via gdb_last_signal_packet.
80 * as a side note this behaviour only effects gdb > 6.8 */
85 #define _DEBUG_GDB_IO_
88 static struct gdb_connection
*current_gdb_connection
;
90 static int gdb_breakpoint_override
;
91 static enum breakpoint_type gdb_breakpoint_override_type
;
93 static int gdb_error(struct connection
*connection
, int retval
);
94 static const char *gdb_port
;
95 static const char *gdb_port_next
;
96 static const char DIGITS
[16] = "0123456789abcdef";
98 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
99 const char *function
, const char *string
);
101 /* number of gdb connections, mainly to suppress gdb related debugging spam
102 * in helper/log.c when no gdb connections are actually active */
103 int gdb_actual_connections
;
105 /* set if we are sending a memory map to gdb
106 * via qXfer:memory-map:read packet */
107 /* enabled by default*/
108 static int gdb_use_memory_map
= 1;
109 /* enabled by default*/
110 static int gdb_flash_program
= 1;
112 /* if set, data aborts cause an error to be reported in memory read packets
113 * see the code in gdb_read_memory_packet() for further explanations.
114 * Disabled by default.
116 static int gdb_report_data_abort
;
118 static int gdb_last_signal(struct target
*target
)
120 switch (target
->debug_reason
) {
121 case DBG_REASON_DBGRQ
:
122 return 0x2; /* SIGINT */
123 case DBG_REASON_BREAKPOINT
:
124 case DBG_REASON_WATCHPOINT
:
125 case DBG_REASON_WPTANDBKPT
:
126 return 0x05; /* SIGTRAP */
127 case DBG_REASON_SINGLESTEP
:
128 return 0x05; /* SIGTRAP */
129 case DBG_REASON_NOTHALTED
:
130 return 0x0; /* no signal... shouldn't happen */
132 LOG_USER("undefined debug reason %d - target needs reset",
133 target
->debug_reason
);
138 static int check_pending(struct connection
*connection
,
139 int timeout_s
, int *got_data
)
141 /* a non-blocking socket will block if there is 0 bytes available on the socket,
142 * but return with as many bytes as are available immediately
146 struct gdb_connection
*gdb_con
= connection
->priv
;
148 if (got_data
== NULL
)
152 if (gdb_con
->buf_cnt
> 0) {
158 FD_SET(connection
->fd
, &read_fds
);
160 tv
.tv_sec
= timeout_s
;
162 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
163 /* This can typically be because a "monitor" command took too long
164 * before printing any progress messages
167 return ERROR_GDB_TIMEOUT
;
171 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
175 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
177 struct gdb_connection
*gdb_con
= connection
->priv
;
178 int retval
= ERROR_OK
;
180 #ifdef _DEBUG_GDB_IO_
184 if (connection
->service
->type
!= CONNECTION_TCP
)
185 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
187 retval
= check_pending(connection
, 1, NULL
);
188 if (retval
!= ERROR_OK
)
190 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
195 if (gdb_con
->buf_cnt
> 0)
197 if (gdb_con
->buf_cnt
== 0) {
199 return ERROR_SERVER_REMOTE_CLOSED
;
203 errno
= WSAGetLastError();
209 case WSAECONNABORTED
:
211 return ERROR_SERVER_REMOTE_CLOSED
;
214 return ERROR_SERVER_REMOTE_CLOSED
;
216 LOG_ERROR("read: %d", errno
);
226 return ERROR_SERVER_REMOTE_CLOSED
;
229 return ERROR_SERVER_REMOTE_CLOSED
;
231 LOG_ERROR("read: %s", strerror(errno
));
233 return ERROR_SERVER_REMOTE_CLOSED
;
238 #ifdef _DEBUG_GDB_IO_
239 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
240 LOG_DEBUG("received '%s'", debug_buffer
);
244 gdb_con
->buf_p
= gdb_con
->buffer
;
246 *next_char
= *(gdb_con
->buf_p
++);
247 if (gdb_con
->buf_cnt
> 0)
248 connection
->input_pending
= 1;
250 connection
->input_pending
= 0;
251 #ifdef _DEBUG_GDB_IO_
252 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
259 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
260 * held in registers in the inner loop.
262 * For small caches and embedded systems this is important!
264 static inline int gdb_get_char_fast(struct connection
*connection
,
265 int *next_char
, char **buf_p
, int *buf_cnt
)
267 int retval
= ERROR_OK
;
269 if ((*buf_cnt
)-- > 0) {
270 *next_char
= **buf_p
;
273 connection
->input_pending
= 1;
275 connection
->input_pending
= 0;
277 #ifdef _DEBUG_GDB_IO_
278 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
284 struct gdb_connection
*gdb_con
= connection
->priv
;
285 gdb_con
->buf_p
= *buf_p
;
286 gdb_con
->buf_cnt
= *buf_cnt
;
287 retval
= gdb_get_char_inner(connection
, next_char
);
288 *buf_p
= gdb_con
->buf_p
;
289 *buf_cnt
= gdb_con
->buf_cnt
;
294 static int gdb_get_char(struct connection
*connection
, int *next_char
)
296 struct gdb_connection
*gdb_con
= connection
->priv
;
297 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
300 static int gdb_putback_char(struct connection
*connection
, int last_char
)
302 struct gdb_connection
*gdb_con
= connection
->priv
;
304 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
305 *(--gdb_con
->buf_p
) = last_char
;
308 LOG_ERROR("BUG: couldn't put character back");
313 /* The only way we can detect that the socket is closed is the first time
314 * we write to it, we will fail. Subsequent write operations will
315 * succeed. Shudder! */
316 static int gdb_write(struct connection
*connection
, void *data
, int len
)
318 struct gdb_connection
*gdb_con
= connection
->priv
;
320 return ERROR_SERVER_REMOTE_CLOSED
;
322 if (connection_write(connection
, data
, len
) == len
)
325 return ERROR_SERVER_REMOTE_CLOSED
;
328 static int gdb_put_packet_inner(struct connection
*connection
,
329 char *buffer
, int len
)
332 unsigned char my_checksum
= 0;
333 #ifdef _DEBUG_GDB_IO_
338 struct gdb_connection
*gdb_con
= connection
->priv
;
340 for (i
= 0; i
< len
; i
++)
341 my_checksum
+= buffer
[i
];
343 #ifdef _DEBUG_GDB_IO_
345 * At this point we should have nothing in the input queue from GDB,
346 * however sometimes '-' is sent even though we've already received
347 * an ACK (+) for everything we've sent off.
351 retval
= check_pending(connection
, 0, &gotdata
);
352 if (retval
!= ERROR_OK
)
356 retval
= gdb_get_char(connection
, &reply
);
357 if (retval
!= ERROR_OK
)
360 /* fix a problem with some IAR tools */
361 gdb_putback_char(connection
, reply
);
362 LOG_DEBUG("Unexpected start of new packet");
366 LOG_WARNING("Discard unexpected char %c", reply
);
371 #ifdef _DEBUG_GDB_IO_
372 debug_buffer
= strndup(buffer
, len
);
373 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
377 char local_buffer
[1024];
378 local_buffer
[0] = '$';
379 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
380 /* performance gain on smaller packets by only a single call to gdb_write() */
381 memcpy(local_buffer
+ 1, buffer
, len
++);
382 local_buffer
[len
++] = '#';
383 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
384 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
385 retval
= gdb_write(connection
, local_buffer
, len
);
386 if (retval
!= ERROR_OK
)
389 /* larger packets are transmitted directly from caller supplied buffer
390 * by several calls to gdb_write() to avoid dynamic allocation */
391 local_buffer
[1] = '#';
392 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
393 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
394 retval
= gdb_write(connection
, local_buffer
, 1);
395 if (retval
!= ERROR_OK
)
397 retval
= gdb_write(connection
, buffer
, len
);
398 if (retval
!= ERROR_OK
)
400 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
401 if (retval
!= ERROR_OK
)
405 if (gdb_con
->noack_mode
)
408 retval
= gdb_get_char(connection
, &reply
);
409 if (retval
!= ERROR_OK
)
414 else if (reply
== '-') {
415 /* Stop sending output packets for now */
416 log_remove_callback(gdb_log_callback
, connection
);
417 LOG_WARNING("negative reply, retrying");
418 } else if (reply
== 0x3) {
420 retval
= gdb_get_char(connection
, &reply
);
421 if (retval
!= ERROR_OK
)
425 else if (reply
== '-') {
426 /* Stop sending output packets for now */
427 log_remove_callback(gdb_log_callback
, connection
);
428 LOG_WARNING("negative reply, retrying");
429 } else if (reply
== '$') {
430 LOG_ERROR("GDB missing ack(1) - assumed good");
431 gdb_putback_char(connection
, reply
);
434 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
436 return ERROR_SERVER_REMOTE_CLOSED
;
438 } else if (reply
== '$') {
439 LOG_ERROR("GDB missing ack(2) - assumed good");
440 gdb_putback_char(connection
, reply
);
443 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
446 return ERROR_SERVER_REMOTE_CLOSED
;
450 return ERROR_SERVER_REMOTE_CLOSED
;
455 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
457 struct gdb_connection
*gdb_con
= connection
->priv
;
459 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
462 /* we sent some data, reset timer for keep alive messages */
468 static inline int fetch_packet(struct connection
*connection
,
469 int *checksum_ok
, int noack
, int *len
, char *buffer
)
471 unsigned char my_checksum
= 0;
474 int retval
= ERROR_OK
;
476 struct gdb_connection
*gdb_con
= connection
->priv
;
481 /* move this over into local variables to use registers and give the
482 * more freedom to optimize */
483 char *buf_p
= gdb_con
->buf_p
;
484 int buf_cnt
= gdb_con
->buf_cnt
;
487 /* The common case is that we have an entire packet with no escape chars.
488 * We need to leave at least 2 bytes in the buffer to have
489 * gdb_get_char() update various bits and bobs correctly.
491 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
492 /* The compiler will struggle a bit with constant propagation and
493 * aliasing, so we help it by showing that these values do not
494 * change inside the loop
498 int run
= buf_cnt
- 2;
504 if (character
== '#') {
505 /* Danger! character can be '#' when esc is
506 * used so we need an explicit boolean for done here. */
511 if (character
== '}') {
512 /* data transmitted in binary mode (X packet)
513 * uses 0x7d as escape character */
514 my_checksum
+= character
& 0xff;
517 my_checksum
+= character
& 0xff;
518 buffer
[count
++] = (character
^ 0x20) & 0xff;
520 my_checksum
+= character
& 0xff;
521 buffer
[count
++] = character
& 0xff;
530 LOG_ERROR("packet buffer too small");
531 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
535 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
536 if (retval
!= ERROR_OK
)
539 if (character
== '#')
542 if (character
== '}') {
543 /* data transmitted in binary mode (X packet)
544 * uses 0x7d as escape character */
545 my_checksum
+= character
& 0xff;
547 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
548 if (retval
!= ERROR_OK
)
551 my_checksum
+= character
& 0xff;
552 buffer
[count
++] = (character
^ 0x20) & 0xff;
554 my_checksum
+= character
& 0xff;
555 buffer
[count
++] = character
& 0xff;
559 gdb_con
->buf_p
= buf_p
;
560 gdb_con
->buf_cnt
= buf_cnt
;
562 if (retval
!= ERROR_OK
)
567 retval
= gdb_get_char(connection
, &character
);
568 if (retval
!= ERROR_OK
)
570 checksum
[0] = character
;
571 retval
= gdb_get_char(connection
, &character
);
572 if (retval
!= ERROR_OK
)
574 checksum
[1] = character
;
578 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
583 static int gdb_get_packet_inner(struct connection
*connection
,
584 char *buffer
, int *len
)
588 struct gdb_connection
*gdb_con
= connection
->priv
;
592 retval
= gdb_get_char(connection
, &character
);
593 if (retval
!= ERROR_OK
)
596 #ifdef _DEBUG_GDB_IO_
597 LOG_DEBUG("character: '%c'", character
);
604 /* gdb sends a dummy ack '+' at every remote connect - see
605 * remote_start_remote (remote.c)
606 * in case anyone tries to debug why they receive this
607 * warning every time */
608 LOG_WARNING("acknowledgment received, but no packet pending");
611 LOG_WARNING("negative acknowledgment, but no packet pending");
618 LOG_WARNING("ignoring character 0x%x", character
);
621 } while (character
!= '$');
624 /* explicit code expansion here to get faster inlined code in -O3 by not
625 * calculating checksum */
626 if (gdb_con
->noack_mode
) {
627 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
628 if (retval
!= ERROR_OK
)
631 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
632 if (retval
!= ERROR_OK
)
636 if (gdb_con
->noack_mode
) {
637 /* checksum is not checked in noack mode */
641 retval
= gdb_write(connection
, "+", 1);
642 if (retval
!= ERROR_OK
)
648 return ERROR_SERVER_REMOTE_CLOSED
;
653 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
655 struct gdb_connection
*gdb_con
= connection
->priv
;
657 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
662 static int gdb_output_con(struct connection
*connection
, const char *line
)
667 bin_size
= strlen(line
);
669 hex_buffer
= malloc(bin_size
* 2 + 2);
670 if (hex_buffer
== NULL
)
671 return ERROR_GDB_BUFFER_TOO_SMALL
;
674 int pkt_len
= hexify(hex_buffer
+ 1, line
, bin_size
, bin_size
* 2 + 1);
675 int retval
= gdb_put_packet(connection
, hex_buffer
, pkt_len
+ 1);
681 static int gdb_output(struct command_context
*context
, const char *line
)
683 /* this will be dumped to the log and also sent as an O packet if possible */
684 LOG_USER_N("%s", line
);
688 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
690 struct gdb_connection
*gdb_connection
= connection
->priv
;
692 /* In the GDB protocol when we are stepping or continuing execution,
693 * we have a lingering reply. Upon receiving a halted event
694 * when we have that lingering packet, we reply to the original
695 * step or continue packet.
697 * Executing monitor commands can bring the target in and
698 * out of the running state so we'll see lots of TARGET_EVENT_XXX
699 * that are to be ignored.
701 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
705 /* stop forwarding log packets! */
706 log_remove_callback(gdb_log_callback
, connection
);
708 if (gdb_connection
->ctrl_c
) {
710 gdb_connection
->ctrl_c
= 0;
712 signal_var
= gdb_last_signal(target
);
714 snprintf(sig_reply
, 4, "T%2.2x", signal_var
);
715 gdb_put_packet(connection
, sig_reply
, 3);
716 gdb_connection
->frontend_state
= TARGET_HALTED
;
717 rtos_update_threads(target
);
721 static int gdb_target_callback_event_handler(struct target
*target
,
722 enum target_event event
, void *priv
)
725 struct connection
*connection
= priv
;
727 target_handle_event(target
, event
);
729 case TARGET_EVENT_GDB_HALT
:
730 gdb_frontend_halted(target
, connection
);
732 case TARGET_EVENT_HALTED
:
733 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
735 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
736 retval
= jtag_execute_queue();
737 if (retval
!= ERROR_OK
)
747 static int gdb_new_connection(struct connection
*connection
)
749 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
750 struct gdb_service
*gdb_service
= connection
->service
->priv
;
754 connection
->priv
= gdb_connection
;
756 /* initialize gdb connection information */
757 gdb_connection
->buf_p
= gdb_connection
->buffer
;
758 gdb_connection
->buf_cnt
= 0;
759 gdb_connection
->ctrl_c
= 0;
760 gdb_connection
->frontend_state
= TARGET_HALTED
;
761 gdb_connection
->vflash_image
= NULL
;
762 gdb_connection
->closed
= 0;
763 gdb_connection
->busy
= 0;
764 gdb_connection
->noack_mode
= 0;
765 gdb_connection
->sync
= true;
766 gdb_connection
->mem_write_error
= false;
767 gdb_connection
->attached
= true;
769 /* send ACK to GDB for debug request */
770 gdb_write(connection
, "+", 1);
772 /* output goes through gdb connection */
773 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
775 /* we must remove all breakpoints registered to the target as a previous
776 * GDB session could leave dangling breakpoints if e.g. communication
779 breakpoint_clear_target(gdb_service
->target
);
780 watchpoint_clear_target(gdb_service
->target
);
782 /* clean previous rtos session if supported*/
783 if ((gdb_service
->target
->rtos
) && (gdb_service
->target
->rtos
->type
->clean
))
784 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
786 /* remove the initial ACK from the incoming buffer */
787 retval
= gdb_get_char(connection
, &initial_ack
);
788 if (retval
!= ERROR_OK
)
791 /* FIX!!!??? would we actually ever receive a + here???
794 if (initial_ack
!= '+')
795 gdb_putback_char(connection
, initial_ack
);
796 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
798 if (gdb_use_memory_map
) {
799 /* Connect must fail if the memory map can't be set up correctly.
801 * This will cause an auto_probe to be invoked, which is either
802 * a no-op or it will fail when the target isn't ready(e.g. not halted).
805 for (i
= 0; i
< flash_get_bank_count(); i
++) {
806 struct flash_bank
*p
;
807 retval
= get_flash_bank_by_num(i
, &p
);
808 if (retval
!= ERROR_OK
) {
809 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
810 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
816 gdb_actual_connections
++;
817 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
818 gdb_actual_connections
,
819 target_name(gdb_service
->target
),
820 target_state_name(gdb_service
->target
));
822 /* DANGER! If we fail subsequently, we must remove this handler,
823 * otherwise we occasionally see crashes as the timer can invoke the
826 * register callback to be informed about target events */
827 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
832 static int gdb_connection_closed(struct connection
*connection
)
834 struct gdb_service
*gdb_service
= connection
->service
->priv
;
835 struct gdb_connection
*gdb_connection
= connection
->priv
;
837 /* we're done forwarding messages. Tear down callback before
838 * cleaning up connection.
840 log_remove_callback(gdb_log_callback
, connection
);
842 gdb_actual_connections
--;
843 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
844 target_name(gdb_service
->target
),
845 target_state_name(gdb_service
->target
),
846 gdb_actual_connections
);
848 /* see if an image built with vFlash commands is left */
849 if (gdb_connection
->vflash_image
) {
850 image_close(gdb_connection
->vflash_image
);
851 free(gdb_connection
->vflash_image
);
852 gdb_connection
->vflash_image
= NULL
;
855 /* if this connection registered a debug-message receiver delete it */
856 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
858 if (connection
->priv
) {
859 free(connection
->priv
);
860 connection
->priv
= NULL
;
862 LOG_ERROR("BUG: connection->priv == NULL");
864 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
866 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
868 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
873 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
876 snprintf(err
, 4, "E%2.2X", the_error
);
877 gdb_put_packet(connection
, err
, 3);
880 static int gdb_last_signal_packet(struct connection
*connection
,
881 char *packet
, int packet_size
)
883 struct target
*target
= get_target_from_connection(connection
);
884 struct gdb_connection
*gdb_con
= connection
->priv
;
888 if (!gdb_con
->attached
) {
889 /* if we are here we have received a kill packet
890 * reply W stop reply otherwise gdb gets very unhappy */
891 gdb_put_packet(connection
, "W00", 3);
895 signal_var
= gdb_last_signal(target
);
897 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
898 gdb_put_packet(connection
, sig_reply
, 3);
903 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
905 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
908 return len
- 1 - pos
;
911 /* Convert register to string of bytes. NB! The # of bits in the
912 * register might be non-divisible by 8(a byte), in which
913 * case an entire byte is shown.
915 * NB! the format on the wire is the target endianness
917 * The format of reg->value is little endian
920 static void gdb_str_to_target(struct target
*target
,
921 char *tstr
, struct reg
*reg
)
928 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
930 for (i
= 0; i
< buf_len
; i
++) {
931 int j
= gdb_reg_pos(target
, i
, buf_len
);
932 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
933 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
937 static int hextoint(int c
)
939 if (c
>= '0' && c
<= '9')
942 if (c
>= 'A' && c
<= 'F')
944 LOG_ERROR("BUG: invalid register value %08x", c
);
948 /* copy over in register buffer */
949 static void gdb_target_to_reg(struct target
*target
,
950 char *tstr
, int str_len
, uint8_t *bin
)
953 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
958 for (i
= 0; i
< str_len
; i
+= 2) {
959 uint8_t t
= hextoint(tstr
[i
]) << 4;
960 t
|= hextoint(tstr
[i
+ 1]);
962 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
967 static int gdb_get_registers_packet(struct connection
*connection
,
968 char *packet
, int packet_size
)
970 struct target
*target
= get_target_from_connection(connection
);
971 struct reg
**reg_list
;
974 int reg_packet_size
= 0;
979 #ifdef _DEBUG_GDB_IO_
983 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
986 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
987 if (retval
!= ERROR_OK
)
988 return gdb_error(connection
, retval
);
990 for (i
= 0; i
< reg_list_size
; i
++)
991 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
993 assert(reg_packet_size
> 0);
995 reg_packet
= malloc(reg_packet_size
);
996 reg_packet_p
= reg_packet
;
998 for (i
= 0; i
< reg_list_size
; i
++) {
999 if (!reg_list
[i
]->valid
)
1000 reg_list
[i
]->type
->get(reg_list
[i
]);
1001 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1002 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1005 #ifdef _DEBUG_GDB_IO_
1007 char *reg_packet_p_debug
;
1008 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1009 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1010 free(reg_packet_p_debug
);
1014 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1022 static int gdb_set_registers_packet(struct connection
*connection
,
1023 char *packet
, int packet_size
)
1025 struct target
*target
= get_target_from_connection(connection
);
1027 struct reg
**reg_list
;
1032 #ifdef _DEBUG_GDB_IO_
1036 /* skip command character */
1040 if (packet_size
% 2) {
1041 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1042 return ERROR_SERVER_REMOTE_CLOSED
;
1045 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1046 if (retval
!= ERROR_OK
)
1047 return gdb_error(connection
, retval
);
1050 for (i
= 0; i
< reg_list_size
; i
++) {
1052 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1054 if (packet_p
+ chars
> packet
+ packet_size
)
1055 LOG_ERROR("BUG: register packet is too small for registers");
1057 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1058 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1060 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1062 /* advance packet pointer */
1068 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1071 gdb_put_packet(connection
, "OK", 2);
1076 static int gdb_get_register_packet(struct connection
*connection
,
1077 char *packet
, int packet_size
)
1079 struct target
*target
= get_target_from_connection(connection
);
1081 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1082 struct reg
**reg_list
;
1086 #ifdef _DEBUG_GDB_IO_
1090 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1091 if (retval
!= ERROR_OK
)
1092 return gdb_error(connection
, retval
);
1094 if (reg_list_size
<= reg_num
) {
1095 LOG_ERROR("gdb requested a non-existing register");
1096 return ERROR_SERVER_REMOTE_CLOSED
;
1099 if (!reg_list
[reg_num
]->valid
)
1100 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1102 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1104 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1106 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1114 static int gdb_set_register_packet(struct connection
*connection
,
1115 char *packet
, int packet_size
)
1117 struct target
*target
= get_target_from_connection(connection
);
1120 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1121 struct reg
**reg_list
;
1127 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1128 if (retval
!= ERROR_OK
)
1129 return gdb_error(connection
, retval
);
1131 if (reg_list_size
<= reg_num
) {
1132 LOG_ERROR("gdb requested a non-existing register");
1133 return ERROR_SERVER_REMOTE_CLOSED
;
1136 if (*separator
!= '=') {
1137 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1138 return ERROR_SERVER_REMOTE_CLOSED
;
1141 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1142 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1143 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1145 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1146 LOG_ERROR("gdb sent a packet with wrong register size");
1148 return ERROR_SERVER_REMOTE_CLOSED
;
1151 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1153 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1155 gdb_put_packet(connection
, "OK", 2);
1163 /* No attempt is made to translate the "retval" to
1164 * GDB speak. This has to be done at the calling
1165 * site as no mapping really exists.
1167 static int gdb_error(struct connection
*connection
, int retval
)
1169 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1170 gdb_send_error(connection
, EFAULT
);
1174 /* We don't have to worry about the default 2 second timeout for GDB packets,
1175 * because GDB breaks up large memory reads into smaller reads.
1177 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1179 static int gdb_read_memory_packet(struct connection
*connection
,
1180 char *packet
, int packet_size
)
1182 struct target
*target
= get_target_from_connection(connection
);
1190 int retval
= ERROR_OK
;
1192 /* skip command character */
1195 addr
= strtoul(packet
, &separator
, 16);
1197 if (*separator
!= ',') {
1198 LOG_ERROR("incomplete read memory packet received, dropping connection");
1199 return ERROR_SERVER_REMOTE_CLOSED
;
1202 len
= strtoul(separator
+ 1, NULL
, 16);
1204 buffer
= malloc(len
);
1206 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1208 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1210 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1211 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1212 * At some point this might be fixed in GDB, in which case this code can be removed.
1214 * OpenOCD developers are acutely aware of this problem, but there is nothing
1215 * gained by involving the user in this problem that hopefully will get resolved
1218 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1219 * cmd = view%20audit-trail&database = gdb&pr = 2395
1221 * For now, the default is to fix up things to make current GDB versions work.
1222 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1224 memset(buffer
, 0, len
);
1228 if (retval
== ERROR_OK
) {
1229 hex_buffer
= malloc(len
* 2 + 1);
1231 int pkt_len
= hexify(hex_buffer
, (char *)buffer
, len
, len
* 2 + 1);
1233 gdb_put_packet(connection
, hex_buffer
, pkt_len
);
1237 retval
= gdb_error(connection
, retval
);
1244 static int gdb_write_memory_packet(struct connection
*connection
,
1245 char *packet
, int packet_size
)
1247 struct target
*target
= get_target_from_connection(connection
);
1255 /* skip command character */
1258 addr
= strtoul(packet
, &separator
, 16);
1260 if (*separator
!= ',') {
1261 LOG_ERROR("incomplete write memory packet received, dropping connection");
1262 return ERROR_SERVER_REMOTE_CLOSED
;
1265 len
= strtoul(separator
+ 1, &separator
, 16);
1267 if (*(separator
++) != ':') {
1268 LOG_ERROR("incomplete write memory packet received, dropping connection");
1269 return ERROR_SERVER_REMOTE_CLOSED
;
1272 buffer
= malloc(len
);
1274 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1276 if (unhexify((char *)buffer
, separator
+ 2, len
) != (int)len
)
1277 LOG_ERROR("unable to decode memory packet");
1279 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1281 if (retval
== ERROR_OK
)
1282 gdb_put_packet(connection
, "OK", 2);
1284 retval
= gdb_error(connection
, retval
);
1291 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1292 char *packet
, int packet_size
)
1294 struct target
*target
= get_target_from_connection(connection
);
1299 int retval
= ERROR_OK
;
1301 /* skip command character */
1304 addr
= strtoul(packet
, &separator
, 16);
1306 if (*separator
!= ',') {
1307 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1308 return ERROR_SERVER_REMOTE_CLOSED
;
1311 len
= strtoul(separator
+ 1, &separator
, 16);
1313 if (*(separator
++) != ':') {
1314 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1315 return ERROR_SERVER_REMOTE_CLOSED
;
1318 struct gdb_connection
*gdb_connection
= connection
->priv
;
1320 if (gdb_connection
->mem_write_error
) {
1321 retval
= ERROR_FAIL
;
1322 /* now that we have reported the memory write error, we can clear the condition */
1323 gdb_connection
->mem_write_error
= false;
1326 /* By replying the packet *immediately* GDB will send us a new packet
1327 * while we write the last one to the target.
1329 if (retval
== ERROR_OK
)
1330 gdb_put_packet(connection
, "OK", 2);
1332 retval
= gdb_error(connection
, retval
);
1333 if (retval
!= ERROR_OK
)
1338 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1340 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1341 if (retval
!= ERROR_OK
)
1342 gdb_connection
->mem_write_error
= true;
1348 static int gdb_step_continue_packet(struct connection
*connection
,
1349 char *packet
, int packet_size
)
1351 struct target
*target
= get_target_from_connection(connection
);
1353 uint32_t address
= 0x0;
1354 int retval
= ERROR_OK
;
1358 if (packet_size
> 1) {
1359 packet
[packet_size
] = 0;
1360 address
= strtoul(packet
+ 1, NULL
, 16);
1364 if (packet
[0] == 'c') {
1365 LOG_DEBUG("continue");
1366 /* resume at current address, don't handle breakpoints, not debugging */
1367 retval
= target_resume(target
, current
, address
, 0, 0);
1368 } else if (packet
[0] == 's') {
1370 /* step at current or address, don't handle breakpoints */
1371 retval
= target_step(target
, current
, address
, 0);
1376 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1377 char *packet
, int packet_size
)
1379 struct target
*target
= get_target_from_connection(connection
);
1381 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1382 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1390 type
= strtoul(packet
+ 1, &separator
, 16);
1392 if (type
== 0) /* memory breakpoint */
1393 bp_type
= BKPT_SOFT
;
1394 else if (type
== 1) /* hardware breakpoint */
1395 bp_type
= BKPT_HARD
;
1396 else if (type
== 2) /* write watchpoint */
1397 wp_type
= WPT_WRITE
;
1398 else if (type
== 3) /* read watchpoint */
1400 else if (type
== 4) /* access watchpoint */
1401 wp_type
= WPT_ACCESS
;
1403 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1404 return ERROR_SERVER_REMOTE_CLOSED
;
1407 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1408 bp_type
= gdb_breakpoint_override_type
;
1410 if (*separator
!= ',') {
1411 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1412 return ERROR_SERVER_REMOTE_CLOSED
;
1415 address
= strtoul(separator
+ 1, &separator
, 16);
1417 if (*separator
!= ',') {
1418 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1419 return ERROR_SERVER_REMOTE_CLOSED
;
1422 size
= strtoul(separator
+ 1, &separator
, 16);
1427 if (packet
[0] == 'Z') {
1428 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1429 if (retval
!= ERROR_OK
) {
1430 retval
= gdb_error(connection
, retval
);
1431 if (retval
!= ERROR_OK
)
1434 gdb_put_packet(connection
, "OK", 2);
1436 breakpoint_remove(target
, address
);
1437 gdb_put_packet(connection
, "OK", 2);
1444 if (packet
[0] == 'Z') {
1445 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1446 if (retval
!= ERROR_OK
) {
1447 retval
= gdb_error(connection
, retval
);
1448 if (retval
!= ERROR_OK
)
1451 gdb_put_packet(connection
, "OK", 2);
1453 watchpoint_remove(target
, address
);
1454 gdb_put_packet(connection
, "OK", 2);
1465 /* print out a string and allocate more space as needed,
1466 * mainly used for XML at this point
1468 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1469 const char *fmt
, ...)
1471 if (*retval
!= ERROR_OK
)
1476 if ((*xml
== NULL
) || (!first
)) {
1477 /* start by 0 to exercise all the code paths.
1478 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1480 *size
= *size
* 2 + 2;
1482 *xml
= realloc(*xml
, *size
);
1486 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1494 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1496 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1500 /* there was just enough or not enough space, allocate more. */
1505 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1509 /* Extract and NUL-terminate the annex. */
1511 while (*buf
&& *buf
!= ':')
1517 /* After the read marker and annex, qXfer looks like a
1518 * traditional 'm' packet. */
1520 *ofs
= strtoul(buf
, &separator
, 16);
1522 if (*separator
!= ',')
1525 *len
= strtoul(separator
+ 1, NULL
, 16);
1530 static int compare_bank(const void *a
, const void *b
)
1532 struct flash_bank
*b1
, *b2
;
1533 b1
= *((struct flash_bank
**)a
);
1534 b2
= *((struct flash_bank
**)b
);
1536 if (b1
->base
== b2
->base
)
1538 else if (b1
->base
> b2
->base
)
1544 static int gdb_memory_map(struct connection
*connection
,
1545 char *packet
, int packet_size
)
1547 /* We get away with only specifying flash here. Regions that are not
1548 * specified are treated as if we provided no memory map(if not we
1549 * could detect the holes and mark them as RAM).
1550 * Normally we only execute this code once, but no big deal if we
1551 * have to regenerate it a couple of times.
1554 struct target
*target
= get_target_from_connection(connection
);
1555 struct flash_bank
*p
;
1559 int retval
= ERROR_OK
;
1560 struct flash_bank
**banks
;
1564 uint32_t ram_start
= 0;
1566 int target_flash_banks
= 0;
1568 /* skip command character */
1571 offset
= strtoul(packet
, &separator
, 16);
1572 length
= strtoul(separator
+ 1, &separator
, 16);
1574 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1576 /* Sort banks in ascending order. We need to report non-flash
1577 * memory as ram (or rather read/write) by default for GDB, since
1578 * it has no concept of non-cacheable read/write memory (i/o etc).
1580 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1581 * Current versions of GDB assume unlisted addresses are RAM...
1583 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1585 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1586 retval
= get_flash_bank_by_num(i
, &p
);
1587 if (retval
!= ERROR_OK
) {
1589 gdb_error(connection
, retval
);
1592 if (p
->target
== target
)
1593 banks
[target_flash_banks
++] = p
;
1596 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1599 for (i
= 0; i
< target_flash_banks
; i
++) {
1601 unsigned sector_size
= 0;
1607 if (ram_start
< p
->base
)
1608 xml_printf(&retval
, &xml
, &pos
, &size
,
1609 "<memory type=\"ram\" start=\"0x%x\" "
1610 "length=\"0x%x\"/>\n",
1611 ram_start
, p
->base
- ram_start
);
1613 /* Report adjacent groups of same-size sectors. So for
1614 * example top boot CFI flash will list an initial region
1615 * with several large sectors (maybe 128KB) and several
1616 * smaller ones at the end (maybe 32KB). STR7 will have
1617 * regions with 8KB, 32KB, and 64KB sectors; etc.
1619 for (j
= 0; j
< p
->num_sectors
; j
++) {
1622 /* Maybe start a new group of sectors. */
1623 if (sector_size
== 0) {
1624 start
= p
->base
+ p
->sectors
[j
].offset
;
1625 xml_printf(&retval
, &xml
, &pos
, &size
,
1626 "<memory type=\"flash\" "
1629 sector_size
= p
->sectors
[j
].size
;
1632 /* Does this finish a group of sectors?
1633 * If not, continue an already-started group.
1635 if (j
== p
->num_sectors
- 1)
1636 group_len
= (p
->base
+ p
->size
) - start
;
1637 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1638 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1643 xml_printf(&retval
, &xml
, &pos
, &size
,
1644 "length=\"0x%x\">\n"
1645 "<property name=\"blocksize\">"
1653 ram_start
= p
->base
+ p
->size
;
1657 xml_printf(&retval
, &xml
, &pos
, &size
,
1658 "<memory type=\"ram\" start=\"0x%x\" "
1659 "length=\"0x%x\"/>\n",
1660 ram_start
, 0-ram_start
);
1661 /* ELSE a flash chip could be at the very end of the 32 bit address
1662 * space, in which case ram_start will be precisely 0
1668 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1670 if (retval
!= ERROR_OK
) {
1671 gdb_error(connection
, retval
);
1675 if (offset
+ length
> pos
)
1676 length
= pos
- offset
;
1678 char *t
= malloc(length
+ 1);
1680 memcpy(t
+ 1, xml
+ offset
, length
);
1681 gdb_put_packet(connection
, t
, length
+ 1);
1688 static int gdb_query_packet(struct connection
*connection
,
1689 char *packet
, int packet_size
)
1691 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1692 struct gdb_connection
*gdb_connection
= connection
->priv
;
1693 struct target
*target
= get_target_from_connection(connection
);
1695 if (strncmp(packet
, "qRcmd,", 6) == 0) {
1696 if (packet_size
> 6) {
1698 cmd
= malloc((packet_size
- 6) / 2 + 1);
1699 int len
= unhexify(cmd
, packet
+ 6, (packet_size
- 6) / 2);
1702 /* We want to print all debug output to GDB connection */
1703 log_add_callback(gdb_log_callback
, connection
);
1704 target_call_timer_callbacks_now();
1705 /* some commands need to know the GDB connection, make note of current
1706 * GDB connection. */
1707 current_gdb_connection
= gdb_connection
;
1708 command_run_line(cmd_ctx
, cmd
);
1709 current_gdb_connection
= NULL
;
1710 target_call_timer_callbacks_now();
1711 log_remove_callback(gdb_log_callback
, connection
);
1714 gdb_put_packet(connection
, "OK", 2);
1716 } else if (strncmp(packet
, "qCRC:", 5) == 0) {
1717 if (packet_size
> 5) {
1725 /* skip command character */
1728 addr
= strtoul(packet
, &separator
, 16);
1730 if (*separator
!= ',') {
1731 LOG_ERROR("incomplete read memory packet received, dropping connection");
1732 return ERROR_SERVER_REMOTE_CLOSED
;
1735 len
= strtoul(separator
+ 1, NULL
, 16);
1737 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1739 if (retval
== ERROR_OK
) {
1740 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1741 gdb_put_packet(connection
, gdb_reply
, 9);
1743 retval
= gdb_error(connection
, retval
);
1744 if (retval
!= ERROR_OK
)
1750 } else if (strncmp(packet
, "qSupported", 10) == 0) {
1751 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1752 * disable qXfer:features:read for the moment */
1753 int retval
= ERROR_OK
;
1754 char *buffer
= NULL
;
1762 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1763 (GDB_BUFFER_SIZE
- 1),
1764 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1766 if (retval
!= ERROR_OK
) {
1767 gdb_send_error(connection
, 01);
1771 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1775 } else if ((strncmp(packet
, "qXfer:memory-map:read::", 23) == 0)
1776 && (flash_get_bank_count() > 0))
1777 return gdb_memory_map(connection
, packet
, packet_size
);
1778 else if (strncmp(packet
, "qXfer:features:read:", 20) == 0) {
1782 int retval
= ERROR_OK
;
1785 unsigned int length
;
1788 /* skip command character */
1791 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0) {
1792 gdb_send_error(connection
, 01);
1796 if (strcmp(annex
, "target.xml") != 0) {
1797 gdb_send_error(connection
, 01);
1805 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1807 if (retval
!= ERROR_OK
) {
1808 gdb_error(connection
, retval
);
1812 gdb_put_packet(connection
, xml
, strlen(xml
));
1816 } else if (strncmp(packet
, "QStartNoAckMode", 15) == 0) {
1817 gdb_connection
->noack_mode
= 1;
1818 gdb_put_packet(connection
, "OK", 2);
1822 gdb_put_packet(connection
, "", 0);
1826 static int gdb_v_packet(struct connection
*connection
,
1827 char *packet
, int packet_size
)
1829 struct gdb_connection
*gdb_connection
= connection
->priv
;
1830 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1833 /* if flash programming disabled - send a empty reply */
1835 if (gdb_flash_program
== 0) {
1836 gdb_put_packet(connection
, "", 0);
1840 if (strncmp(packet
, "vFlashErase:", 12) == 0) {
1842 unsigned long length
;
1844 char *parse
= packet
+ 12;
1845 if (*parse
== '\0') {
1846 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1847 return ERROR_SERVER_REMOTE_CLOSED
;
1850 addr
= strtoul(parse
, &parse
, 16);
1852 if (*(parse
++) != ',' || *parse
== '\0') {
1853 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1854 return ERROR_SERVER_REMOTE_CLOSED
;
1857 length
= strtoul(parse
, &parse
, 16);
1859 if (*parse
!= '\0') {
1860 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1861 return ERROR_SERVER_REMOTE_CLOSED
;
1864 /* assume all sectors need erasing - stops any problems
1865 * when flash_write is called multiple times */
1868 /* perform any target specific operations before the erase */
1869 target_call_event_callbacks(gdb_service
->target
,
1870 TARGET_EVENT_GDB_FLASH_ERASE_START
);
1872 /* vFlashErase:addr,length messages require region start and
1873 * end to be "block" aligned ... if padding is ever needed,
1874 * GDB will have become dangerously confused.
1876 result
= flash_erase_address_range(gdb_service
->target
,
1877 false, addr
, length
);
1879 /* perform any target specific operations after the erase */
1880 target_call_event_callbacks(gdb_service
->target
,
1881 TARGET_EVENT_GDB_FLASH_ERASE_END
);
1884 if (result
!= ERROR_OK
) {
1885 /* GDB doesn't evaluate the actual error number returned,
1886 * treat a failed erase as an I/O error
1888 gdb_send_error(connection
, EIO
);
1889 LOG_ERROR("flash_erase returned %i", result
);
1891 gdb_put_packet(connection
, "OK", 2);
1896 if (strncmp(packet
, "vFlashWrite:", 12) == 0) {
1899 unsigned long length
;
1900 char *parse
= packet
+ 12;
1902 if (*parse
== '\0') {
1903 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1904 return ERROR_SERVER_REMOTE_CLOSED
;
1906 addr
= strtoul(parse
, &parse
, 16);
1907 if (*(parse
++) != ':') {
1908 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1909 return ERROR_SERVER_REMOTE_CLOSED
;
1911 length
= packet_size
- (parse
- packet
);
1913 /* create a new image if there isn't already one */
1914 if (gdb_connection
->vflash_image
== NULL
) {
1915 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
1916 image_open(gdb_connection
->vflash_image
, "", "build");
1919 /* create new section with content from packet buffer */
1920 retval
= image_add_section(gdb_connection
->vflash_image
,
1921 addr
, length
, 0x0, (uint8_t *)parse
);
1922 if (retval
!= ERROR_OK
)
1925 gdb_put_packet(connection
, "OK", 2);
1930 if (strncmp(packet
, "vFlashDone", 10) == 0) {
1933 /* process the flashing buffer. No need to erase as GDB
1934 * always issues a vFlashErase first. */
1935 target_call_event_callbacks(gdb_service
->target
,
1936 TARGET_EVENT_GDB_FLASH_WRITE_START
);
1937 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1938 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1939 if (result
!= ERROR_OK
) {
1940 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1941 gdb_put_packet(connection
, "E.memtype", 9);
1943 gdb_send_error(connection
, EIO
);
1945 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
1946 gdb_put_packet(connection
, "OK", 2);
1949 image_close(gdb_connection
->vflash_image
);
1950 free(gdb_connection
->vflash_image
);
1951 gdb_connection
->vflash_image
= NULL
;
1956 gdb_put_packet(connection
, "", 0);
1960 static int gdb_detach(struct connection
*connection
)
1962 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1964 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
1966 return gdb_put_packet(connection
, "OK", 2);
1969 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
1970 const char *function
, const char *string
)
1972 struct connection
*connection
= priv
;
1973 struct gdb_connection
*gdb_con
= connection
->priv
;
1975 if (gdb_con
->busy
) {
1976 /* do not reply this using the O packet */
1980 gdb_output_con(connection
, string
);
1983 static void gdb_sig_halted(struct connection
*connection
)
1986 snprintf(sig_reply
, 4, "T%2.2x", 2);
1987 gdb_put_packet(connection
, sig_reply
, 3);
1990 static int gdb_input_inner(struct connection
*connection
)
1992 /* Do not allocate this on the stack */
1993 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
1995 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1996 struct target
*target
= gdb_service
->target
;
1997 char *packet
= gdb_packet_buffer
;
2000 struct gdb_connection
*gdb_con
= connection
->priv
;
2001 static int extended_protocol
;
2003 /* drain input buffer. If one of the packets fail, then an error
2004 * packet is replied, if applicable.
2006 * This loop will terminate and the error code is returned.
2008 * The calling fn will check if this error is something that
2009 * can be recovered from, or if the connection must be closed.
2011 * If the error is recoverable, this fn is called again to
2012 * drain the rest of the buffer.
2015 packet_size
= GDB_BUFFER_SIZE
-1;
2016 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2017 if (retval
!= ERROR_OK
)
2020 /* terminate with zero */
2021 packet
[packet_size
] = 0;
2023 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2024 if (packet
[0] == 'X') {
2025 /* binary packets spew junk into the debug log stream */
2028 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
2031 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2033 LOG_DEBUG("received packet: '%s'", packet
);
2036 if (packet_size
> 0) {
2038 switch (packet
[0]) {
2039 case 'T': /* Is thread alive? */
2040 gdb_thread_packet(connection
, packet
, packet_size
);
2042 case 'H': /* Set current thread ( 'c' for step and continue,
2043 * 'g' for all other operations ) */
2044 gdb_thread_packet(connection
, packet
, packet_size
);
2048 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2049 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2050 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2053 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2056 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2059 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2062 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2065 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2068 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2072 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2075 gdb_last_signal_packet(connection
, packet
, packet_size
);
2080 gdb_thread_packet(connection
, packet
, packet_size
);
2081 log_add_callback(gdb_log_callback
, connection
);
2083 if (gdb_con
->mem_write_error
) {
2084 LOG_ERROR("Memory write failure!");
2086 /* now that we have reported the memory write error,
2087 * we can clear the condition */
2088 gdb_con
->mem_write_error
= false;
2091 bool nostep
= false;
2092 bool already_running
= false;
2093 if (target
->state
== TARGET_RUNNING
) {
2094 LOG_WARNING("WARNING! The target is already running. "
2095 "All changes GDB did to registers will be discarded! "
2096 "Waiting for target to halt.");
2097 already_running
= true;
2098 } else if (target
->state
!= TARGET_HALTED
) {
2099 LOG_WARNING("The target is not in the halted nor running stated, " \
2100 "stepi/continue ignored.");
2102 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
2103 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2104 * sent by GDB first to OpenOCD, thus defeating the check to
2105 * make only the single stepping have the sync feature...
2108 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2109 "from the target.");
2111 gdb_con
->sync
= false;
2113 if (!already_running
&& nostep
) {
2114 /* Either the target isn't in the halted state, then we can't
2115 * step/continue. This might be early setup, etc.
2117 * Or we want to allow GDB to pick up a fresh set of
2118 * register values without modifying the target state.
2121 gdb_sig_halted(connection
);
2123 /* stop forwarding log packets! */
2124 log_remove_callback(gdb_log_callback
, connection
);
2126 /* We're running/stepping, in which case we can
2127 * forward log output until the target is halted
2129 gdb_con
->frontend_state
= TARGET_RUNNING
;
2130 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2132 if (!already_running
) {
2133 /* Here we don't want packet processing to stop even if this fails,
2134 * so we use a local variable instead of retval. */
2135 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2136 if (retval
!= ERROR_OK
) {
2137 /* we'll never receive a halted
2138 * condition... issue a false one..
2140 gdb_frontend_halted(target
, connection
);
2147 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2150 retval
= gdb_detach(connection
);
2151 extended_protocol
= 0;
2154 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2155 if (retval
!= ERROR_OK
)
2159 if (extended_protocol
!= 0) {
2160 gdb_con
->attached
= false;
2163 gdb_put_packet(connection
, "OK", 2);
2164 return ERROR_SERVER_REMOTE_CLOSED
;
2166 /* handle extended remote protocol */
2167 extended_protocol
= 1;
2168 gdb_put_packet(connection
, "OK", 2);
2171 /* handle extended restart packet */
2172 breakpoint_clear_target(gdb_service
->target
);
2173 watchpoint_clear_target(gdb_service
->target
);
2174 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
2175 target_name(target
));
2176 /* set connection as attached after reset */
2177 gdb_con
->attached
= true;
2178 /* info rtos parts */
2179 gdb_thread_packet(connection
, packet
, packet_size
);
2183 /* packet supported only by smp target i.e cortex_a.c*/
2184 /* handle smp packet replying coreid played to gbd */
2185 gdb_read_smp_packet(connection
, packet
, packet_size
);
2189 /* packet supported only by smp target i.e cortex_a.c */
2190 /* handle smp packet setting coreid to be played at next
2192 gdb_write_smp_packet(connection
, packet
, packet_size
);
2196 /* ignore unknown packets */
2197 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2198 gdb_put_packet(connection
, NULL
, 0);
2202 /* if a packet handler returned an error, exit input loop */
2203 if (retval
!= ERROR_OK
)
2207 if (gdb_con
->ctrl_c
) {
2208 if (target
->state
== TARGET_RUNNING
) {
2209 retval
= target_halt(target
);
2210 if (retval
!= ERROR_OK
)
2211 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2212 gdb_con
->ctrl_c
= 0;
2214 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2215 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2219 } while (gdb_con
->buf_cnt
> 0);
2224 static int gdb_input(struct connection
*connection
)
2226 int retval
= gdb_input_inner(connection
);
2227 struct gdb_connection
*gdb_con
= connection
->priv
;
2228 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2231 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2232 if (gdb_con
->closed
)
2233 return ERROR_SERVER_REMOTE_CLOSED
;
2235 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2239 static int gdb_target_start(struct target
*target
, const char *port
)
2241 struct gdb_service
*gdb_service
;
2243 gdb_service
= malloc(sizeof(struct gdb_service
));
2245 if (NULL
== gdb_service
)
2248 gdb_service
->target
= target
;
2249 gdb_service
->core
[0] = -1;
2250 gdb_service
->core
[1] = -1;
2251 target
->gdb_service
= gdb_service
;
2253 ret
= add_service("gdb",
2254 port
, 1, &gdb_new_connection
, &gdb_input
,
2255 &gdb_connection_closed
, gdb_service
);
2256 /* initialialize all targets gdb service with the same pointer */
2258 struct target_list
*head
;
2259 struct target
*curr
;
2260 head
= target
->head
;
2261 while (head
!= (struct target_list
*)NULL
) {
2262 curr
= head
->target
;
2264 curr
->gdb_service
= gdb_service
;
2271 static int gdb_target_add_one(struct target
*target
)
2273 /* one gdb instance per smp list */
2274 if ((target
->smp
) && (target
->gdb_service
))
2276 int retval
= gdb_target_start(target
, gdb_port_next
);
2277 if (retval
== ERROR_OK
) {
2279 /* If we can parse the port number
2280 * then we increment the port number for the next target.
2283 portnumber
= strtol(gdb_port_next
, &end
, 0);
2285 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
2286 free((void *)gdb_port_next
);
2287 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2294 int gdb_target_add_all(struct target
*target
)
2296 if (NULL
== target
) {
2297 LOG_WARNING("gdb services need one or more targets defined");
2301 while (NULL
!= target
) {
2302 int retval
= gdb_target_add_one(target
);
2303 if (ERROR_OK
!= retval
)
2306 target
= target
->next
;
2312 COMMAND_HANDLER(handle_gdb_sync_command
)
2315 return ERROR_COMMAND_SYNTAX_ERROR
;
2317 if (current_gdb_connection
== NULL
) {
2318 command_print(CMD_CTX
,
2319 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2323 current_gdb_connection
->sync
= true;
2328 /* daemon configuration command gdb_port */
2329 COMMAND_HANDLER(handle_gdb_port_command
)
2331 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2332 if (ERROR_OK
== retval
) {
2333 free((void *)gdb_port_next
);
2334 gdb_port_next
= strdup(gdb_port
);
2339 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2342 return ERROR_COMMAND_SYNTAX_ERROR
;
2344 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2348 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2351 return ERROR_COMMAND_SYNTAX_ERROR
;
2353 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2357 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2360 return ERROR_COMMAND_SYNTAX_ERROR
;
2362 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2366 /* gdb_breakpoint_override */
2367 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2369 if (CMD_ARGC
== 0) {
2371 } else if (CMD_ARGC
== 1) {
2372 gdb_breakpoint_override
= 1;
2373 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2374 gdb_breakpoint_override_type
= BKPT_HARD
;
2375 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2376 gdb_breakpoint_override_type
= BKPT_SOFT
;
2377 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2378 gdb_breakpoint_override
= 0;
2380 return ERROR_COMMAND_SYNTAX_ERROR
;
2381 if (gdb_breakpoint_override
)
2382 LOG_USER("force %s breakpoints",
2383 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
2385 LOG_USER("breakpoint type is not overridden");
2390 static const struct command_registration gdb_command_handlers
[] = {
2393 .handler
= handle_gdb_sync_command
,
2394 .mode
= COMMAND_ANY
,
2395 .help
= "next stepi will return immediately allowing "
2396 "GDB to fetch register state without affecting "
2402 .handler
= handle_gdb_port_command
,
2403 .mode
= COMMAND_ANY
,
2404 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2405 "server listens for the next port number after the "
2406 "base port number specified. "
2407 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2408 "output to stdout, an integer is base port number, \"disable\" disables "
2409 "port. Any other string is are interpreted as named pipe to listen to. "
2410 "Output pipe is the same name as input pipe, but with 'o' appended.",
2411 .usage
= "[port_num]",
2414 .name
= "gdb_memory_map",
2415 .handler
= handle_gdb_memory_map_command
,
2416 .mode
= COMMAND_CONFIG
,
2417 .help
= "enable or disable memory map",
2418 .usage
= "('enable'|'disable')"
2421 .name
= "gdb_flash_program",
2422 .handler
= handle_gdb_flash_program_command
,
2423 .mode
= COMMAND_CONFIG
,
2424 .help
= "enable or disable flash program",
2425 .usage
= "('enable'|'disable')"
2428 .name
= "gdb_report_data_abort",
2429 .handler
= handle_gdb_report_data_abort_command
,
2430 .mode
= COMMAND_CONFIG
,
2431 .help
= "enable or disable reporting data aborts",
2432 .usage
= "('enable'|'disable')"
2435 .name
= "gdb_breakpoint_override",
2436 .handler
= handle_gdb_breakpoint_override_command
,
2437 .mode
= COMMAND_ANY
,
2438 .help
= "Display or specify type of breakpoint "
2439 "to be used by gdb 'break' commands.",
2440 .usage
= "('hard'|'soft'|'disable')"
2442 COMMAND_REGISTRATION_DONE
2445 int gdb_register_commands(struct command_context
*cmd_ctx
)
2447 gdb_port
= strdup("3333");
2448 gdb_port_next
= strdup("3333");
2449 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);