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...). */
80 #define _DEBUG_GDB_IO_
83 static struct gdb_connection
*current_gdb_connection
;
85 static int gdb_breakpoint_override
;
86 static enum breakpoint_type gdb_breakpoint_override_type
;
88 static int gdb_error(struct connection
*connection
, int retval
);
89 static const char *gdb_port
;
90 static const char *gdb_port_next
;
91 static const char DIGITS
[16] = "0123456789abcdef";
93 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
94 const char *function
, const char *string
);
96 /* number of gdb connections, mainly to suppress gdb related debugging spam
97 * in helper/log.c when no gdb connections are actually active */
98 int gdb_actual_connections
;
100 /* set if we are sending a memory map to gdb
101 * via qXfer:memory-map:read packet */
102 /* enabled by default*/
103 static int gdb_use_memory_map
= 1;
104 /* enabled by default*/
105 static int gdb_flash_program
= 1;
107 /* if set, data aborts cause an error to be reported in memory read packets
108 * see the code in gdb_read_memory_packet() for further explanations.
109 * Disabled by default.
111 static int gdb_report_data_abort
;
113 static int gdb_last_signal(struct target
*target
)
115 switch (target
->debug_reason
) {
116 case DBG_REASON_DBGRQ
:
117 return 0x2; /* SIGINT */
118 case DBG_REASON_BREAKPOINT
:
119 case DBG_REASON_WATCHPOINT
:
120 case DBG_REASON_WPTANDBKPT
:
121 return 0x05; /* SIGTRAP */
122 case DBG_REASON_SINGLESTEP
:
123 return 0x05; /* SIGTRAP */
124 case DBG_REASON_NOTHALTED
:
125 return 0x0; /* no signal... shouldn't happen */
127 LOG_USER("undefined debug reason %d - target needs reset",
128 target
->debug_reason
);
133 static int check_pending(struct connection
*connection
,
134 int timeout_s
, int *got_data
)
136 /* a non-blocking socket will block if there is 0 bytes available on the socket,
137 * but return with as many bytes as are available immediately
141 struct gdb_connection
*gdb_con
= connection
->priv
;
143 if (got_data
== NULL
)
147 if (gdb_con
->buf_cnt
> 0) {
153 FD_SET(connection
->fd
, &read_fds
);
155 tv
.tv_sec
= timeout_s
;
157 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0) {
158 /* This can typically be because a "monitor" command took too long
159 * before printing any progress messages
162 return ERROR_GDB_TIMEOUT
;
166 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
170 static int gdb_get_char_inner(struct connection
*connection
, int *next_char
)
172 struct gdb_connection
*gdb_con
= connection
->priv
;
173 int retval
= ERROR_OK
;
175 #ifdef _DEBUG_GDB_IO_
179 if (connection
->service
->type
!= CONNECTION_TCP
)
180 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
182 retval
= check_pending(connection
, 1, NULL
);
183 if (retval
!= ERROR_OK
)
185 gdb_con
->buf_cnt
= read_socket(connection
->fd
,
190 if (gdb_con
->buf_cnt
> 0)
192 if (gdb_con
->buf_cnt
== 0) {
194 return ERROR_SERVER_REMOTE_CLOSED
;
198 errno
= WSAGetLastError();
204 case WSAECONNABORTED
:
206 return ERROR_SERVER_REMOTE_CLOSED
;
209 return ERROR_SERVER_REMOTE_CLOSED
;
211 LOG_ERROR("read: %d", errno
);
221 return ERROR_SERVER_REMOTE_CLOSED
;
224 return ERROR_SERVER_REMOTE_CLOSED
;
226 LOG_ERROR("read: %s", strerror(errno
));
228 return ERROR_SERVER_REMOTE_CLOSED
;
233 #ifdef _DEBUG_GDB_IO_
234 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
235 LOG_DEBUG("received '%s'", debug_buffer
);
239 gdb_con
->buf_p
= gdb_con
->buffer
;
241 *next_char
= *(gdb_con
->buf_p
++);
242 if (gdb_con
->buf_cnt
> 0)
243 connection
->input_pending
= 1;
245 connection
->input_pending
= 0;
246 #ifdef _DEBUG_GDB_IO_
247 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
254 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
255 * held in registers in the inner loop.
257 * For small caches and embedded systems this is important!
259 static inline int gdb_get_char_fast(struct connection
*connection
,
260 int *next_char
, char **buf_p
, int *buf_cnt
)
262 int retval
= ERROR_OK
;
264 if ((*buf_cnt
)-- > 0) {
265 *next_char
= **buf_p
;
268 connection
->input_pending
= 1;
270 connection
->input_pending
= 0;
272 #ifdef _DEBUG_GDB_IO_
273 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
279 struct gdb_connection
*gdb_con
= connection
->priv
;
280 gdb_con
->buf_p
= *buf_p
;
281 gdb_con
->buf_cnt
= *buf_cnt
;
282 retval
= gdb_get_char_inner(connection
, next_char
);
283 *buf_p
= gdb_con
->buf_p
;
284 *buf_cnt
= gdb_con
->buf_cnt
;
289 static int gdb_get_char(struct connection
*connection
, int *next_char
)
291 struct gdb_connection
*gdb_con
= connection
->priv
;
292 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
295 static int gdb_putback_char(struct connection
*connection
, int last_char
)
297 struct gdb_connection
*gdb_con
= connection
->priv
;
299 if (gdb_con
->buf_p
> gdb_con
->buffer
) {
300 *(--gdb_con
->buf_p
) = last_char
;
303 LOG_ERROR("BUG: couldn't put character back");
308 /* The only way we can detect that the socket is closed is the first time
309 * we write to it, we will fail. Subsequent write operations will
310 * succeed. Shudder! */
311 static int gdb_write(struct connection
*connection
, void *data
, int len
)
313 struct gdb_connection
*gdb_con
= connection
->priv
;
315 return ERROR_SERVER_REMOTE_CLOSED
;
317 if (connection_write(connection
, data
, len
) == len
)
320 return ERROR_SERVER_REMOTE_CLOSED
;
323 static int gdb_put_packet_inner(struct connection
*connection
,
324 char *buffer
, int len
)
327 unsigned char my_checksum
= 0;
328 #ifdef _DEBUG_GDB_IO_
333 struct gdb_connection
*gdb_con
= connection
->priv
;
335 for (i
= 0; i
< len
; i
++)
336 my_checksum
+= buffer
[i
];
338 #ifdef _DEBUG_GDB_IO_
340 * At this point we should have nothing in the input queue from GDB,
341 * however sometimes '-' is sent even though we've already received
342 * an ACK (+) for everything we've sent off.
346 retval
= check_pending(connection
, 0, &gotdata
);
347 if (retval
!= ERROR_OK
)
351 retval
= gdb_get_char(connection
, &reply
);
352 if (retval
!= ERROR_OK
)
355 /* fix a problem with some IAR tools */
356 gdb_putback_char(connection
, reply
);
357 LOG_DEBUG("Unexpected start of new packet");
361 LOG_WARNING("Discard unexpected char %c", reply
);
366 #ifdef _DEBUG_GDB_IO_
367 debug_buffer
= strndup(buffer
, len
);
368 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
372 char local_buffer
[1024];
373 local_buffer
[0] = '$';
374 if ((size_t)len
+ 4 <= sizeof(local_buffer
)) {
375 /* performance gain on smaller packets by only a single call to gdb_write() */
376 memcpy(local_buffer
+ 1, buffer
, len
++);
377 local_buffer
[len
++] = '#';
378 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
379 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
380 retval
= gdb_write(connection
, local_buffer
, len
);
381 if (retval
!= ERROR_OK
)
384 /* larger packets are transmitted directly from caller supplied buffer
385 * by several calls to gdb_write() to avoid dynamic allocation */
386 local_buffer
[1] = '#';
387 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
388 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
389 retval
= gdb_write(connection
, local_buffer
, 1);
390 if (retval
!= ERROR_OK
)
392 retval
= gdb_write(connection
, buffer
, len
);
393 if (retval
!= ERROR_OK
)
395 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
396 if (retval
!= ERROR_OK
)
400 if (gdb_con
->noack_mode
)
403 retval
= gdb_get_char(connection
, &reply
);
404 if (retval
!= ERROR_OK
)
409 else if (reply
== '-') {
410 /* Stop sending output packets for now */
411 log_remove_callback(gdb_log_callback
, connection
);
412 LOG_WARNING("negative reply, retrying");
413 } else if (reply
== 0x3) {
415 retval
= gdb_get_char(connection
, &reply
);
416 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
== '$') {
425 LOG_ERROR("GDB missing ack(1) - assumed good");
426 gdb_putback_char(connection
, reply
);
429 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
431 return ERROR_SERVER_REMOTE_CLOSED
;
433 } else if (reply
== '$') {
434 LOG_ERROR("GDB missing ack(2) - assumed good");
435 gdb_putback_char(connection
, reply
);
438 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
441 return ERROR_SERVER_REMOTE_CLOSED
;
445 return ERROR_SERVER_REMOTE_CLOSED
;
450 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
452 struct gdb_connection
*gdb_con
= connection
->priv
;
454 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
457 /* we sent some data, reset timer for keep alive messages */
463 static inline int fetch_packet(struct connection
*connection
,
464 int *checksum_ok
, int noack
, int *len
, char *buffer
)
466 unsigned char my_checksum
= 0;
469 int retval
= ERROR_OK
;
471 struct gdb_connection
*gdb_con
= connection
->priv
;
476 /* move this over into local variables to use registers and give the
477 * more freedom to optimize */
478 char *buf_p
= gdb_con
->buf_p
;
479 int buf_cnt
= gdb_con
->buf_cnt
;
482 /* The common case is that we have an entire packet with no escape chars.
483 * We need to leave at least 2 bytes in the buffer to have
484 * gdb_get_char() update various bits and bobs correctly.
486 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
487 /* The compiler will struggle a bit with constant propagation and
488 * aliasing, so we help it by showing that these values do not
489 * change inside the loop
493 int run
= buf_cnt
- 2;
499 if (character
== '#') {
500 /* Danger! character can be '#' when esc is
501 * used so we need an explicit boolean for done here. */
506 if (character
== '}') {
507 /* data transmitted in binary mode (X packet)
508 * uses 0x7d as escape character */
509 my_checksum
+= character
& 0xff;
512 my_checksum
+= character
& 0xff;
513 buffer
[count
++] = (character
^ 0x20) & 0xff;
515 my_checksum
+= character
& 0xff;
516 buffer
[count
++] = character
& 0xff;
525 LOG_ERROR("packet buffer too small");
526 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
530 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
531 if (retval
!= ERROR_OK
)
534 if (character
== '#')
537 if (character
== '}') {
538 /* data transmitted in binary mode (X packet)
539 * uses 0x7d as escape character */
540 my_checksum
+= character
& 0xff;
542 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
543 if (retval
!= ERROR_OK
)
546 my_checksum
+= character
& 0xff;
547 buffer
[count
++] = (character
^ 0x20) & 0xff;
549 my_checksum
+= character
& 0xff;
550 buffer
[count
++] = character
& 0xff;
554 gdb_con
->buf_p
= buf_p
;
555 gdb_con
->buf_cnt
= buf_cnt
;
557 if (retval
!= ERROR_OK
)
562 retval
= gdb_get_char(connection
, &character
);
563 if (retval
!= ERROR_OK
)
565 checksum
[0] = character
;
566 retval
= gdb_get_char(connection
, &character
);
567 if (retval
!= ERROR_OK
)
569 checksum
[1] = character
;
573 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
578 static int gdb_get_packet_inner(struct connection
*connection
,
579 char *buffer
, int *len
)
583 struct gdb_connection
*gdb_con
= connection
->priv
;
587 retval
= gdb_get_char(connection
, &character
);
588 if (retval
!= ERROR_OK
)
591 #ifdef _DEBUG_GDB_IO_
592 LOG_DEBUG("character: '%c'", character
);
599 /* gdb sends a dummy ack '+' at every remote connect - see
600 * remote_start_remote (remote.c)
601 * in case anyone tries to debug why they receive this
602 * warning every time */
603 LOG_WARNING("acknowledgment received, but no packet pending");
606 LOG_WARNING("negative acknowledgment, but no packet pending");
613 LOG_WARNING("ignoring character 0x%x", character
);
616 } while (character
!= '$');
619 /* explicit code expansion here to get faster inlined code in -O3 by not
620 * calculating checksum */
621 if (gdb_con
->noack_mode
) {
622 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
623 if (retval
!= ERROR_OK
)
626 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
627 if (retval
!= ERROR_OK
)
631 if (gdb_con
->noack_mode
) {
632 /* checksum is not checked in noack mode */
636 retval
= gdb_write(connection
, "+", 1);
637 if (retval
!= ERROR_OK
)
643 return ERROR_SERVER_REMOTE_CLOSED
;
648 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
650 struct gdb_connection
*gdb_con
= connection
->priv
;
652 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
657 static int gdb_output_con(struct connection
*connection
, const char *line
)
662 bin_size
= strlen(line
);
664 hex_buffer
= malloc(bin_size
*2 + 2);
665 if (hex_buffer
== NULL
)
666 return ERROR_GDB_BUFFER_TOO_SMALL
;
669 for (i
= 0; i
< bin_size
; i
++)
670 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
671 hex_buffer
[bin_size
*2 + 1] = 0;
673 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
679 static int gdb_output(struct command_context
*context
, const char *line
)
681 /* this will be dumped to the log and also sent as an O packet if possible */
682 LOG_USER_N("%s", line
);
686 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
688 struct gdb_connection
*gdb_connection
= connection
->priv
;
690 /* In the GDB protocol when we are stepping or continuing execution,
691 * we have a lingering reply. Upon receiving a halted event
692 * when we have that lingering packet, we reply to the original
693 * step or continue packet.
695 * Executing monitor commands can bring the target in and
696 * out of the running state so we'll see lots of TARGET_EVENT_XXX
697 * that are to be ignored.
699 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
703 /* stop forwarding log packets! */
704 log_remove_callback(gdb_log_callback
, connection
);
706 if (gdb_connection
->ctrl_c
) {
708 gdb_connection
->ctrl_c
= 0;
710 signal_var
= gdb_last_signal(target
);
712 snprintf(sig_reply
, 4, "T%2.2x", signal_var
);
713 gdb_put_packet(connection
, sig_reply
, 3);
714 gdb_connection
->frontend_state
= TARGET_HALTED
;
715 rtos_update_threads(target
);
719 static int gdb_target_callback_event_handler(struct target
*target
,
720 enum target_event event
, void *priv
)
723 struct connection
*connection
= priv
;
725 target_handle_event(target
, event
);
727 case TARGET_EVENT_GDB_HALT
:
728 gdb_frontend_halted(target
, connection
);
730 case TARGET_EVENT_HALTED
:
731 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
733 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
734 retval
= jtag_execute_queue();
735 if (retval
!= ERROR_OK
)
745 static int gdb_new_connection(struct connection
*connection
)
747 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
748 struct gdb_service
*gdb_service
= connection
->service
->priv
;
752 connection
->priv
= gdb_connection
;
754 /* initialize gdb connection information */
755 gdb_connection
->buf_p
= gdb_connection
->buffer
;
756 gdb_connection
->buf_cnt
= 0;
757 gdb_connection
->ctrl_c
= 0;
758 gdb_connection
->frontend_state
= TARGET_HALTED
;
759 gdb_connection
->vflash_image
= NULL
;
760 gdb_connection
->closed
= 0;
761 gdb_connection
->busy
= 0;
762 gdb_connection
->noack_mode
= 0;
763 gdb_connection
->sync
= true;
764 gdb_connection
->mem_write_error
= false;
766 /* send ACK to GDB for debug request */
767 gdb_write(connection
, "+", 1);
769 /* output goes through gdb connection */
770 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
772 /* we must remove all breakpoints registered to the target as a previous
773 * GDB session could leave dangling breakpoints if e.g. communication
776 breakpoint_clear_target(gdb_service
->target
);
777 watchpoint_clear_target(gdb_service
->target
);
779 /* clean previous rtos session if supported*/
780 if ((gdb_service
->target
->rtos
) && (gdb_service
->target
->rtos
->type
->clean
))
781 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
783 /* remove the initial ACK from the incoming buffer */
784 retval
= gdb_get_char(connection
, &initial_ack
);
785 if (retval
!= ERROR_OK
)
788 /* FIX!!!??? would we actually ever receive a + here???
791 if (initial_ack
!= '+')
792 gdb_putback_char(connection
, initial_ack
);
793 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
795 if (gdb_use_memory_map
) {
796 /* Connect must fail if the memory map can't be set up correctly.
798 * This will cause an auto_probe to be invoked, which is either
799 * a no-op or it will fail when the target isn't ready(e.g. not halted).
802 for (i
= 0; i
< flash_get_bank_count(); i
++) {
803 struct flash_bank
*p
;
804 retval
= get_flash_bank_by_num(i
, &p
);
805 if (retval
!= ERROR_OK
) {
806 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
807 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
813 gdb_actual_connections
++;
814 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
815 gdb_actual_connections
,
816 target_name(gdb_service
->target
),
817 target_state_name(gdb_service
->target
));
819 /* DANGER! If we fail subsequently, we must remove this handler,
820 * otherwise we occasionally see crashes as the timer can invoke the
823 * register callback to be informed about target events */
824 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
829 static int gdb_connection_closed(struct connection
*connection
)
831 struct gdb_service
*gdb_service
= connection
->service
->priv
;
832 struct gdb_connection
*gdb_connection
= connection
->priv
;
834 /* we're done forwarding messages. Tear down callback before
835 * cleaning up connection.
837 log_remove_callback(gdb_log_callback
, connection
);
839 gdb_actual_connections
--;
840 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
841 target_name(gdb_service
->target
),
842 target_state_name(gdb_service
->target
),
843 gdb_actual_connections
);
845 /* see if an image built with vFlash commands is left */
846 if (gdb_connection
->vflash_image
) {
847 image_close(gdb_connection
->vflash_image
);
848 free(gdb_connection
->vflash_image
);
849 gdb_connection
->vflash_image
= NULL
;
852 /* if this connection registered a debug-message receiver delete it */
853 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
855 if (connection
->priv
) {
856 free(connection
->priv
);
857 connection
->priv
= NULL
;
859 LOG_ERROR("BUG: connection->priv == NULL");
861 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
863 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
865 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
870 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
873 snprintf(err
, 4, "E%2.2X", the_error
);
874 gdb_put_packet(connection
, err
, 3);
877 static int gdb_last_signal_packet(struct connection
*connection
,
878 char *packet
, int packet_size
)
880 struct target
*target
= get_target_from_connection(connection
);
884 signal_var
= gdb_last_signal(target
);
886 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
887 gdb_put_packet(connection
, sig_reply
, 3);
892 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
894 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
897 return len
- 1 - pos
;
900 /* Convert register to string of bytes. NB! The # of bits in the
901 * register might be non-divisible by 8(a byte), in which
902 * case an entire byte is shown.
904 * NB! the format on the wire is the target endianness
906 * The format of reg->value is little endian
909 static void gdb_str_to_target(struct target
*target
,
910 char *tstr
, struct reg
*reg
)
917 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
919 for (i
= 0; i
< buf_len
; i
++) {
920 int j
= gdb_reg_pos(target
, i
, buf_len
);
921 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
922 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
926 static int hextoint(int c
)
928 if (c
>= '0' && c
<= '9')
931 if (c
>= 'A' && c
<= 'F')
933 LOG_ERROR("BUG: invalid register value %08x", c
);
937 /* copy over in register buffer */
938 static void gdb_target_to_reg(struct target
*target
,
939 char *tstr
, int str_len
, uint8_t *bin
)
942 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
947 for (i
= 0; i
< str_len
; i
+= 2) {
948 uint8_t t
= hextoint(tstr
[i
]) << 4;
949 t
|= hextoint(tstr
[i
+ 1]);
951 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
956 static int gdb_get_registers_packet(struct connection
*connection
,
957 char *packet
, int packet_size
)
959 struct target
*target
= get_target_from_connection(connection
);
960 struct reg
**reg_list
;
963 int reg_packet_size
= 0;
968 #ifdef _DEBUG_GDB_IO_
972 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
975 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
976 if (retval
!= ERROR_OK
)
977 return gdb_error(connection
, retval
);
979 for (i
= 0; i
< reg_list_size
; i
++)
980 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
982 assert(reg_packet_size
> 0);
984 reg_packet
= malloc(reg_packet_size
);
985 reg_packet_p
= reg_packet
;
987 for (i
= 0; i
< reg_list_size
; i
++) {
988 if (!reg_list
[i
]->valid
)
989 reg_list
[i
]->type
->get(reg_list
[i
]);
990 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
991 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
994 #ifdef _DEBUG_GDB_IO_
996 char *reg_packet_p_debug
;
997 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
998 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
999 free(reg_packet_p_debug
);
1003 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1011 static int gdb_set_registers_packet(struct connection
*connection
,
1012 char *packet
, int packet_size
)
1014 struct target
*target
= get_target_from_connection(connection
);
1016 struct reg
**reg_list
;
1021 #ifdef _DEBUG_GDB_IO_
1025 /* skip command character */
1029 if (packet_size
% 2) {
1030 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1031 return ERROR_SERVER_REMOTE_CLOSED
;
1034 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1035 if (retval
!= ERROR_OK
)
1036 return gdb_error(connection
, retval
);
1039 for (i
= 0; i
< reg_list_size
; i
++) {
1041 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1043 if (packet_p
+ chars
> packet
+ packet_size
)
1044 LOG_ERROR("BUG: register packet is too small for registers");
1046 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1047 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1049 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1051 /* advance packet pointer */
1057 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1060 gdb_put_packet(connection
, "OK", 2);
1065 static int gdb_get_register_packet(struct connection
*connection
,
1066 char *packet
, int packet_size
)
1068 struct target
*target
= get_target_from_connection(connection
);
1070 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1071 struct reg
**reg_list
;
1075 #ifdef _DEBUG_GDB_IO_
1079 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1080 if (retval
!= ERROR_OK
)
1081 return gdb_error(connection
, retval
);
1083 if (reg_list_size
<= reg_num
) {
1084 LOG_ERROR("gdb requested a non-existing register");
1085 return ERROR_SERVER_REMOTE_CLOSED
;
1088 if (!reg_list
[reg_num
]->valid
)
1089 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1091 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1093 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1095 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1103 static int gdb_set_register_packet(struct connection
*connection
,
1104 char *packet
, int packet_size
)
1106 struct target
*target
= get_target_from_connection(connection
);
1109 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1110 struct reg
**reg_list
;
1116 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1117 if (retval
!= ERROR_OK
)
1118 return gdb_error(connection
, retval
);
1120 if (reg_list_size
<= reg_num
) {
1121 LOG_ERROR("gdb requested a non-existing register");
1122 return ERROR_SERVER_REMOTE_CLOSED
;
1125 if (*separator
!= '=') {
1126 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1127 return ERROR_SERVER_REMOTE_CLOSED
;
1130 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1131 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1132 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1134 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1135 LOG_ERROR("gdb sent a packet with wrong register size");
1137 return ERROR_SERVER_REMOTE_CLOSED
;
1140 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1142 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1144 gdb_put_packet(connection
, "OK", 2);
1152 /* No attempt is made to translate the "retval" to
1153 * GDB speak. This has to be done at the calling
1154 * site as no mapping really exists.
1156 static int gdb_error(struct connection
*connection
, int retval
)
1158 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1159 gdb_send_error(connection
, EFAULT
);
1163 /* We don't have to worry about the default 2 second timeout for GDB packets,
1164 * because GDB breaks up large memory reads into smaller reads.
1166 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1168 static int gdb_read_memory_packet(struct connection
*connection
,
1169 char *packet
, int packet_size
)
1171 struct target
*target
= get_target_from_connection(connection
);
1179 int retval
= ERROR_OK
;
1181 /* skip command character */
1184 addr
= strtoul(packet
, &separator
, 16);
1186 if (*separator
!= ',') {
1187 LOG_ERROR("incomplete read memory packet received, dropping connection");
1188 return ERROR_SERVER_REMOTE_CLOSED
;
1191 len
= strtoul(separator
+ 1, NULL
, 16);
1193 buffer
= malloc(len
);
1195 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1197 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1199 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1200 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1201 * At some point this might be fixed in GDB, in which case this code can be removed.
1203 * OpenOCD developers are acutely aware of this problem, but there is nothing
1204 * gained by involving the user in this problem that hopefully will get resolved
1207 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1208 * cmd = view%20audit-trail&database = gdb&pr = 2395
1210 * For now, the default is to fix up things to make current GDB versions work.
1211 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1213 memset(buffer
, 0, len
);
1217 if (retval
== ERROR_OK
) {
1218 hex_buffer
= malloc(len
* 2 + 1);
1221 for (i
= 0; i
< len
; i
++) {
1222 uint8_t t
= buffer
[i
];
1223 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1224 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1227 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1231 retval
= gdb_error(connection
, retval
);
1238 static int gdb_write_memory_packet(struct connection
*connection
,
1239 char *packet
, int packet_size
)
1241 struct target
*target
= get_target_from_connection(connection
);
1251 /* skip command character */
1254 addr
= strtoul(packet
, &separator
, 16);
1256 if (*separator
!= ',') {
1257 LOG_ERROR("incomplete write memory packet received, dropping connection");
1258 return ERROR_SERVER_REMOTE_CLOSED
;
1261 len
= strtoul(separator
+ 1, &separator
, 16);
1263 if (*(separator
++) != ':') {
1264 LOG_ERROR("incomplete write memory packet received, dropping connection");
1265 return ERROR_SERVER_REMOTE_CLOSED
;
1268 buffer
= malloc(len
);
1270 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1272 for (i
= 0; i
< len
; i
++) {
1274 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1278 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1280 if (retval
== ERROR_OK
)
1281 gdb_put_packet(connection
, "OK", 2);
1283 retval
= gdb_error(connection
, retval
);
1290 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1291 char *packet
, int packet_size
)
1293 struct target
*target
= get_target_from_connection(connection
);
1298 int retval
= ERROR_OK
;
1300 /* skip command character */
1303 addr
= strtoul(packet
, &separator
, 16);
1305 if (*separator
!= ',') {
1306 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1307 return ERROR_SERVER_REMOTE_CLOSED
;
1310 len
= strtoul(separator
+ 1, &separator
, 16);
1312 if (*(separator
++) != ':') {
1313 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1314 return ERROR_SERVER_REMOTE_CLOSED
;
1317 struct gdb_connection
*gdb_connection
= connection
->priv
;
1319 if (gdb_connection
->mem_write_error
) {
1320 retval
= ERROR_FAIL
;
1321 /* now that we have reported the memory write error, we can clear the condition */
1322 gdb_connection
->mem_write_error
= false;
1325 /* By replying the packet *immediately* GDB will send us a new packet
1326 * while we write the last one to the target.
1328 if (retval
== ERROR_OK
)
1329 gdb_put_packet(connection
, "OK", 2);
1331 retval
= gdb_error(connection
, retval
);
1332 if (retval
!= ERROR_OK
)
1337 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1339 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1340 if (retval
!= ERROR_OK
)
1341 gdb_connection
->mem_write_error
= true;
1347 static int gdb_step_continue_packet(struct connection
*connection
,
1348 char *packet
, int packet_size
)
1350 struct target
*target
= get_target_from_connection(connection
);
1352 uint32_t address
= 0x0;
1353 int retval
= ERROR_OK
;
1357 if (packet_size
> 1) {
1358 packet
[packet_size
] = 0;
1359 address
= strtoul(packet
+ 1, NULL
, 16);
1363 if (packet
[0] == 'c') {
1364 LOG_DEBUG("continue");
1365 /* resume at current address, don't handle breakpoints, not debugging */
1366 retval
= target_resume(target
, current
, address
, 0, 0);
1367 } else if (packet
[0] == 's') {
1369 /* step at current or address, don't handle breakpoints */
1370 retval
= target_step(target
, current
, address
, 0);
1375 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1376 char *packet
, int packet_size
)
1378 struct target
*target
= get_target_from_connection(connection
);
1380 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1381 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1389 type
= strtoul(packet
+ 1, &separator
, 16);
1391 if (type
== 0) /* memory breakpoint */
1392 bp_type
= BKPT_SOFT
;
1393 else if (type
== 1) /* hardware breakpoint */
1394 bp_type
= BKPT_HARD
;
1395 else if (type
== 2) /* write watchpoint */
1396 wp_type
= WPT_WRITE
;
1397 else if (type
== 3) /* read watchpoint */
1399 else if (type
== 4) /* access watchpoint */
1400 wp_type
= WPT_ACCESS
;
1402 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1403 return ERROR_SERVER_REMOTE_CLOSED
;
1406 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1407 bp_type
= gdb_breakpoint_override_type
;
1409 if (*separator
!= ',') {
1410 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1411 return ERROR_SERVER_REMOTE_CLOSED
;
1414 address
= strtoul(separator
+ 1, &separator
, 16);
1416 if (*separator
!= ',') {
1417 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1418 return ERROR_SERVER_REMOTE_CLOSED
;
1421 size
= strtoul(separator
+ 1, &separator
, 16);
1426 if (packet
[0] == 'Z') {
1427 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1428 if (retval
!= ERROR_OK
) {
1429 retval
= gdb_error(connection
, retval
);
1430 if (retval
!= ERROR_OK
)
1433 gdb_put_packet(connection
, "OK", 2);
1435 breakpoint_remove(target
, address
);
1436 gdb_put_packet(connection
, "OK", 2);
1443 if (packet
[0] == 'Z') {
1444 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1445 if (retval
!= ERROR_OK
) {
1446 retval
= gdb_error(connection
, retval
);
1447 if (retval
!= ERROR_OK
)
1450 gdb_put_packet(connection
, "OK", 2);
1452 watchpoint_remove(target
, address
);
1453 gdb_put_packet(connection
, "OK", 2);
1464 /* print out a string and allocate more space as needed,
1465 * mainly used for XML at this point
1467 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1468 const char *fmt
, ...)
1470 if (*retval
!= ERROR_OK
)
1475 if ((*xml
== NULL
) || (!first
)) {
1476 /* start by 0 to exercise all the code paths.
1477 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1479 *size
= *size
* 2 + 2;
1481 *xml
= realloc(*xml
, *size
);
1485 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1493 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1495 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1499 /* there was just enough or not enough space, allocate more. */
1504 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1508 /* Extract and NUL-terminate the annex. */
1510 while (*buf
&& *buf
!= ':')
1516 /* After the read marker and annex, qXfer looks like a
1517 * traditional 'm' packet. */
1519 *ofs
= strtoul(buf
, &separator
, 16);
1521 if (*separator
!= ',')
1524 *len
= strtoul(separator
+ 1, NULL
, 16);
1529 static int compare_bank(const void *a
, const void *b
)
1531 struct flash_bank
*b1
, *b2
;
1532 b1
= *((struct flash_bank
**)a
);
1533 b2
= *((struct flash_bank
**)b
);
1535 if (b1
->base
== b2
->base
)
1537 else if (b1
->base
> b2
->base
)
1543 static int gdb_memory_map(struct connection
*connection
,
1544 char *packet
, int packet_size
)
1546 /* We get away with only specifying flash here. Regions that are not
1547 * specified are treated as if we provided no memory map(if not we
1548 * could detect the holes and mark them as RAM).
1549 * Normally we only execute this code once, but no big deal if we
1550 * have to regenerate it a couple of times.
1553 struct target
*target
= get_target_from_connection(connection
);
1554 struct flash_bank
*p
;
1558 int retval
= ERROR_OK
;
1559 struct flash_bank
**banks
;
1563 uint32_t ram_start
= 0;
1565 int target_flash_banks
= 0;
1567 /* skip command character */
1570 offset
= strtoul(packet
, &separator
, 16);
1571 length
= strtoul(separator
+ 1, &separator
, 16);
1573 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1575 /* Sort banks in ascending order. We need to report non-flash
1576 * memory as ram (or rather read/write) by default for GDB, since
1577 * it has no concept of non-cacheable read/write memory (i/o etc).
1579 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1580 * Current versions of GDB assume unlisted addresses are RAM...
1582 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1584 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1585 retval
= get_flash_bank_by_num(i
, &p
);
1586 if (retval
!= ERROR_OK
) {
1588 gdb_error(connection
, retval
);
1591 if (p
->target
== target
)
1592 banks
[target_flash_banks
++] = p
;
1595 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1598 for (i
= 0; i
< target_flash_banks
; i
++) {
1600 unsigned sector_size
= 0;
1606 if (ram_start
< p
->base
)
1607 xml_printf(&retval
, &xml
, &pos
, &size
,
1608 "<memory type=\"ram\" start=\"0x%x\" "
1609 "length=\"0x%x\"/>\n",
1610 ram_start
, p
->base
- ram_start
);
1612 /* Report adjacent groups of same-size sectors. So for
1613 * example top boot CFI flash will list an initial region
1614 * with several large sectors (maybe 128KB) and several
1615 * smaller ones at the end (maybe 32KB). STR7 will have
1616 * regions with 8KB, 32KB, and 64KB sectors; etc.
1618 for (j
= 0; j
< p
->num_sectors
; j
++) {
1621 /* Maybe start a new group of sectors. */
1622 if (sector_size
== 0) {
1623 start
= p
->base
+ p
->sectors
[j
].offset
;
1624 xml_printf(&retval
, &xml
, &pos
, &size
,
1625 "<memory type=\"flash\" "
1628 sector_size
= p
->sectors
[j
].size
;
1631 /* Does this finish a group of sectors?
1632 * If not, continue an already-started group.
1634 if (j
== p
->num_sectors
- 1)
1635 group_len
= (p
->base
+ p
->size
) - start
;
1636 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1637 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1642 xml_printf(&retval
, &xml
, &pos
, &size
,
1643 "length=\"0x%x\">\n"
1644 "<property name=\"blocksize\">"
1652 ram_start
= p
->base
+ p
->size
;
1656 xml_printf(&retval
, &xml
, &pos
, &size
,
1657 "<memory type=\"ram\" start=\"0x%x\" "
1658 "length=\"0x%x\"/>\n",
1659 ram_start
, 0-ram_start
);
1660 /* ELSE a flash chip could be at the very end of the 32 bit address
1661 * space, in which case ram_start will be precisely 0
1667 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1669 if (retval
!= ERROR_OK
) {
1670 gdb_error(connection
, retval
);
1674 if (offset
+ length
> pos
)
1675 length
= pos
- offset
;
1677 char *t
= malloc(length
+ 1);
1679 memcpy(t
+ 1, xml
+ offset
, length
);
1680 gdb_put_packet(connection
, t
, length
+ 1);
1687 static int gdb_query_packet(struct connection
*connection
,
1688 char *packet
, int packet_size
)
1690 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1691 struct gdb_connection
*gdb_connection
= connection
->priv
;
1692 struct target
*target
= get_target_from_connection(connection
);
1694 if (strstr(packet
, "qRcmd,")) {
1695 if (packet_size
> 6) {
1698 cmd
= malloc((packet_size
- 6)/2 + 1);
1699 for (i
= 0; i
< (packet_size
- 6)/2; i
++) {
1701 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1704 cmd
[(packet_size
- 6)/2] = 0x0;
1706 /* We want to print all debug output to GDB connection */
1707 log_add_callback(gdb_log_callback
, connection
);
1708 target_call_timer_callbacks_now();
1709 /* some commands need to know the GDB connection, make note of current
1710 * GDB connection. */
1711 current_gdb_connection
= gdb_connection
;
1712 command_run_line(cmd_ctx
, cmd
);
1713 current_gdb_connection
= NULL
;
1714 target_call_timer_callbacks_now();
1715 log_remove_callback(gdb_log_callback
, connection
);
1718 gdb_put_packet(connection
, "OK", 2);
1720 } else if (strstr(packet
, "qCRC:")) {
1721 if (packet_size
> 5) {
1729 /* skip command character */
1732 addr
= strtoul(packet
, &separator
, 16);
1734 if (*separator
!= ',') {
1735 LOG_ERROR("incomplete read memory packet received, dropping connection");
1736 return ERROR_SERVER_REMOTE_CLOSED
;
1739 len
= strtoul(separator
+ 1, NULL
, 16);
1741 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1743 if (retval
== ERROR_OK
) {
1744 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1745 gdb_put_packet(connection
, gdb_reply
, 9);
1747 retval
= gdb_error(connection
, retval
);
1748 if (retval
!= ERROR_OK
)
1754 } else if (strstr(packet
, "qSupported")) {
1755 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1756 * disable qXfer:features:read for the moment */
1757 int retval
= ERROR_OK
;
1758 char *buffer
= NULL
;
1766 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1767 (GDB_BUFFER_SIZE
- 1),
1768 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1770 if (retval
!= ERROR_OK
) {
1771 gdb_send_error(connection
, 01);
1775 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1779 } else if (strstr(packet
, "qXfer:memory-map:read::")
1780 && (flash_get_bank_count() > 0))
1781 return gdb_memory_map(connection
, packet
, packet_size
);
1782 else if (strstr(packet
, "qXfer:features:read:")) {
1786 int retval
= ERROR_OK
;
1789 unsigned int length
;
1792 /* skip command character */
1795 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0) {
1796 gdb_send_error(connection
, 01);
1800 if (strcmp(annex
, "target.xml") != 0) {
1801 gdb_send_error(connection
, 01);
1809 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1811 if (retval
!= ERROR_OK
) {
1812 gdb_error(connection
, retval
);
1816 gdb_put_packet(connection
, xml
, strlen(xml
));
1820 } else if (strstr(packet
, "QStartNoAckMode")) {
1821 gdb_connection
->noack_mode
= 1;
1822 gdb_put_packet(connection
, "OK", 2);
1826 gdb_put_packet(connection
, "", 0);
1830 static int gdb_v_packet(struct connection
*connection
,
1831 char *packet
, int packet_size
)
1833 struct gdb_connection
*gdb_connection
= connection
->priv
;
1834 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1837 /* if flash programming disabled - send a empty reply */
1839 if (gdb_flash_program
== 0) {
1840 gdb_put_packet(connection
, "", 0);
1844 if (strstr(packet
, "vFlashErase:")) {
1846 unsigned long length
;
1848 char *parse
= packet
+ 12;
1849 if (*parse
== '\0') {
1850 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1851 return ERROR_SERVER_REMOTE_CLOSED
;
1854 addr
= strtoul(parse
, &parse
, 16);
1856 if (*(parse
++) != ',' || *parse
== '\0') {
1857 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1858 return ERROR_SERVER_REMOTE_CLOSED
;
1861 length
= strtoul(parse
, &parse
, 16);
1863 if (*parse
!= '\0') {
1864 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1865 return ERROR_SERVER_REMOTE_CLOSED
;
1868 /* assume all sectors need erasing - stops any problems
1869 * when flash_write is called multiple times */
1872 /* perform any target specific operations before the erase */
1873 target_call_event_callbacks(gdb_service
->target
,
1874 TARGET_EVENT_GDB_FLASH_ERASE_START
);
1876 /* vFlashErase:addr,length messages require region start and
1877 * end to be "block" aligned ... if padding is ever needed,
1878 * GDB will have become dangerously confused.
1880 result
= flash_erase_address_range(gdb_service
->target
,
1881 false, addr
, length
);
1883 /* perform any target specific operations after the erase */
1884 target_call_event_callbacks(gdb_service
->target
,
1885 TARGET_EVENT_GDB_FLASH_ERASE_END
);
1888 if (result
!= ERROR_OK
) {
1889 /* GDB doesn't evaluate the actual error number returned,
1890 * treat a failed erase as an I/O error
1892 gdb_send_error(connection
, EIO
);
1893 LOG_ERROR("flash_erase returned %i", result
);
1895 gdb_put_packet(connection
, "OK", 2);
1900 if (strstr(packet
, "vFlashWrite:")) {
1903 unsigned long length
;
1904 char *parse
= packet
+ 12;
1906 if (*parse
== '\0') {
1907 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1908 return ERROR_SERVER_REMOTE_CLOSED
;
1910 addr
= strtoul(parse
, &parse
, 16);
1911 if (*(parse
++) != ':') {
1912 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1913 return ERROR_SERVER_REMOTE_CLOSED
;
1915 length
= packet_size
- (parse
- packet
);
1917 /* create a new image if there isn't already one */
1918 if (gdb_connection
->vflash_image
== NULL
) {
1919 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
1920 image_open(gdb_connection
->vflash_image
, "", "build");
1923 /* create new section with content from packet buffer */
1924 retval
= image_add_section(gdb_connection
->vflash_image
,
1925 addr
, length
, 0x0, (uint8_t *)parse
);
1926 if (retval
!= ERROR_OK
)
1929 gdb_put_packet(connection
, "OK", 2);
1934 if (!strcmp(packet
, "vFlashDone")) {
1937 /* process the flashing buffer. No need to erase as GDB
1938 * always issues a vFlashErase first. */
1939 target_call_event_callbacks(gdb_service
->target
,
1940 TARGET_EVENT_GDB_FLASH_WRITE_START
);
1941 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1942 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1943 if (result
!= ERROR_OK
) {
1944 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1945 gdb_put_packet(connection
, "E.memtype", 9);
1947 gdb_send_error(connection
, EIO
);
1949 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
1950 gdb_put_packet(connection
, "OK", 2);
1953 image_close(gdb_connection
->vflash_image
);
1954 free(gdb_connection
->vflash_image
);
1955 gdb_connection
->vflash_image
= NULL
;
1960 gdb_put_packet(connection
, "", 0);
1964 static int gdb_detach(struct connection
*connection
)
1966 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1968 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
1970 return gdb_put_packet(connection
, "OK", 2);
1973 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
1974 const char *function
, const char *string
)
1976 struct connection
*connection
= priv
;
1977 struct gdb_connection
*gdb_con
= connection
->priv
;
1979 if (gdb_con
->busy
) {
1980 /* do not reply this using the O packet */
1984 gdb_output_con(connection
, string
);
1987 static void gdb_sig_halted(struct connection
*connection
)
1990 snprintf(sig_reply
, 4, "T%2.2x", 2);
1991 gdb_put_packet(connection
, sig_reply
, 3);
1994 static int gdb_input_inner(struct connection
*connection
)
1996 /* Do not allocate this on the stack */
1997 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
1999 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2000 struct target
*target
= gdb_service
->target
;
2001 char *packet
= gdb_packet_buffer
;
2004 struct gdb_connection
*gdb_con
= connection
->priv
;
2005 static int extended_protocol
;
2007 /* drain input buffer. If one of the packets fail, then an error
2008 * packet is replied, if applicable.
2010 * This loop will terminate and the error code is returned.
2012 * The calling fn will check if this error is something that
2013 * can be recovered from, or if the connection must be closed.
2015 * If the error is recoverable, this fn is called again to
2016 * drain the rest of the buffer.
2019 packet_size
= GDB_BUFFER_SIZE
-1;
2020 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2021 if (retval
!= ERROR_OK
)
2024 /* terminate with zero */
2025 packet
[packet_size
] = 0;
2027 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2028 if (packet
[0] == 'X') {
2029 /* binary packets spew junk into the debug log stream */
2032 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
2035 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2037 LOG_DEBUG("received packet: '%s'", packet
);
2040 if (packet_size
> 0) {
2042 switch (packet
[0]) {
2043 case 'T': /* Is thread alive? */
2044 gdb_thread_packet(connection
, packet
, packet_size
);
2046 case 'H': /* Set current thread ( 'c' for step and continue,
2047 * 'g' for all other operations ) */
2048 gdb_thread_packet(connection
, packet
, packet_size
);
2052 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2053 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2054 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2057 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2060 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2063 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2066 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2069 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2072 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2076 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2079 gdb_last_signal_packet(connection
, packet
, packet_size
);
2084 gdb_thread_packet(connection
, packet
, packet_size
);
2085 log_add_callback(gdb_log_callback
, connection
);
2087 if (gdb_con
->mem_write_error
) {
2088 LOG_ERROR("Memory write failure!");
2090 /* now that we have reported the memory write error,
2091 * we can clear the condition */
2092 gdb_con
->mem_write_error
= false;
2095 bool nostep
= false;
2096 bool already_running
= false;
2097 if (target
->state
== TARGET_RUNNING
) {
2098 LOG_WARNING("WARNING! The target is already running. "
2099 "All changes GDB did to registers will be discarded! "
2100 "Waiting for target to halt.");
2101 already_running
= true;
2102 } else if (target
->state
!= TARGET_HALTED
) {
2103 LOG_WARNING("The target is not in the halted nor running stated, " \
2104 "stepi/continue ignored.");
2106 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
2107 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2108 * sent by GDB first to OpenOCD, thus defeating the check to
2109 * make only the single stepping have the sync feature...
2112 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2113 "from the target.");
2115 gdb_con
->sync
= false;
2117 if (!already_running
&& nostep
) {
2118 /* Either the target isn't in the halted state, then we can't
2119 * step/continue. This might be early setup, etc.
2121 * Or we want to allow GDB to pick up a fresh set of
2122 * register values without modifying the target state.
2125 gdb_sig_halted(connection
);
2127 /* stop forwarding log packets! */
2128 log_remove_callback(gdb_log_callback
, connection
);
2130 /* We're running/stepping, in which case we can
2131 * forward log output until the target is halted
2133 gdb_con
->frontend_state
= TARGET_RUNNING
;
2134 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2136 if (!already_running
) {
2137 /* Here we don't want packet processing to stop even if this fails,
2138 * so we use a local variable instead of retval. */
2139 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2140 if (retval
!= ERROR_OK
) {
2141 /* we'll never receive a halted
2142 * condition... issue a false one..
2144 gdb_frontend_halted(target
, connection
);
2151 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2154 retval
= gdb_detach(connection
);
2155 extended_protocol
= 0;
2158 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2159 if (retval
!= ERROR_OK
)
2163 if (extended_protocol
!= 0)
2165 gdb_put_packet(connection
, "OK", 2);
2166 return ERROR_SERVER_REMOTE_CLOSED
;
2168 /* handle extended remote protocol */
2169 extended_protocol
= 1;
2170 gdb_put_packet(connection
, "OK", 2);
2173 /* handle extended restart packet */
2174 breakpoint_clear_target(gdb_service
->target
);
2175 watchpoint_clear_target(gdb_service
->target
);
2176 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %s",
2177 target_name(target
));
2178 /* info rtos parts */
2179 gdb_thread_packet(connection
, packet
, packet_size
);
2180 gdb_put_packet(connection
, "OK", 2);
2184 /* packet supported only by smp target i.e cortex_a.c*/
2185 /* handle smp packet replying coreid played to gbd */
2186 gdb_read_smp_packet(connection
, packet
, packet_size
);
2190 /* packet supported only by smp target i.e cortex_a.c */
2191 /* handle smp packet setting coreid to be played at next
2193 gdb_write_smp_packet(connection
, packet
, packet_size
);
2197 /* ignore unknown packets */
2198 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2199 gdb_put_packet(connection
, NULL
, 0);
2203 /* if a packet handler returned an error, exit input loop */
2204 if (retval
!= ERROR_OK
)
2208 if (gdb_con
->ctrl_c
) {
2209 if (target
->state
== TARGET_RUNNING
) {
2210 retval
= target_halt(target
);
2211 if (retval
!= ERROR_OK
)
2212 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2213 gdb_con
->ctrl_c
= 0;
2215 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2216 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2220 } while (gdb_con
->buf_cnt
> 0);
2225 static int gdb_input(struct connection
*connection
)
2227 int retval
= gdb_input_inner(connection
);
2228 struct gdb_connection
*gdb_con
= connection
->priv
;
2229 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2232 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2233 if (gdb_con
->closed
)
2234 return ERROR_SERVER_REMOTE_CLOSED
;
2236 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2240 static int gdb_target_start(struct target
*target
, const char *port
)
2242 struct gdb_service
*gdb_service
;
2244 gdb_service
= malloc(sizeof(struct gdb_service
));
2246 if (NULL
== gdb_service
)
2249 gdb_service
->target
= target
;
2250 gdb_service
->core
[0] = -1;
2251 gdb_service
->core
[1] = -1;
2252 target
->gdb_service
= gdb_service
;
2254 ret
= add_service("gdb",
2255 port
, 1, &gdb_new_connection
, &gdb_input
,
2256 &gdb_connection_closed
, gdb_service
);
2257 /* initialialize all targets gdb service with the same pointer */
2259 struct target_list
*head
;
2260 struct target
*curr
;
2261 head
= target
->head
;
2262 while (head
!= (struct target_list
*)NULL
) {
2263 curr
= head
->target
;
2265 curr
->gdb_service
= gdb_service
;
2272 static int gdb_target_add_one(struct target
*target
)
2274 /* one gdb instance per smp list */
2275 if ((target
->smp
) && (target
->gdb_service
))
2277 int retval
= gdb_target_start(target
, gdb_port_next
);
2278 if (retval
== ERROR_OK
) {
2280 /* If we can parse the port number
2281 * then we increment the port number for the next target.
2284 portnumber
= strtol(gdb_port_next
, &end
, 0);
2286 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
2287 free((void *)gdb_port_next
);
2288 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2295 int gdb_target_add_all(struct target
*target
)
2297 if (NULL
== target
) {
2298 LOG_WARNING("gdb services need one or more targets defined");
2302 while (NULL
!= target
) {
2303 int retval
= gdb_target_add_one(target
);
2304 if (ERROR_OK
!= retval
)
2307 target
= target
->next
;
2313 COMMAND_HANDLER(handle_gdb_sync_command
)
2316 return ERROR_COMMAND_SYNTAX_ERROR
;
2318 if (current_gdb_connection
== NULL
) {
2319 command_print(CMD_CTX
,
2320 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2324 current_gdb_connection
->sync
= true;
2329 /* daemon configuration command gdb_port */
2330 COMMAND_HANDLER(handle_gdb_port_command
)
2332 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2333 if (ERROR_OK
== retval
) {
2334 free((void *)gdb_port_next
);
2335 gdb_port_next
= strdup(gdb_port
);
2340 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2343 return ERROR_COMMAND_SYNTAX_ERROR
;
2345 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2349 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2352 return ERROR_COMMAND_SYNTAX_ERROR
;
2354 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2358 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2361 return ERROR_COMMAND_SYNTAX_ERROR
;
2363 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2367 /* gdb_breakpoint_override */
2368 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2370 if (CMD_ARGC
== 0) {
2372 } else if (CMD_ARGC
== 1) {
2373 gdb_breakpoint_override
= 1;
2374 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2375 gdb_breakpoint_override_type
= BKPT_HARD
;
2376 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2377 gdb_breakpoint_override_type
= BKPT_SOFT
;
2378 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2379 gdb_breakpoint_override
= 0;
2381 return ERROR_COMMAND_SYNTAX_ERROR
;
2382 if (gdb_breakpoint_override
)
2383 LOG_USER("force %s breakpoints",
2384 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
2386 LOG_USER("breakpoint type is not overridden");
2391 static const struct command_registration gdb_command_handlers
[] = {
2394 .handler
= handle_gdb_sync_command
,
2395 .mode
= COMMAND_ANY
,
2396 .help
= "next stepi will return immediately allowing "
2397 "GDB to fetch register state without affecting "
2403 .handler
= handle_gdb_port_command
,
2404 .mode
= COMMAND_ANY
,
2405 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2406 "server listens for the next port number after the "
2407 "base port number specified. "
2408 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2409 "output to stdout, an integer is base port number, \"disable\" disables "
2410 "port. Any other string is are interpreted as named pipe to listen to. "
2411 "Output pipe is the same name as input pipe, but with 'o' appended.",
2412 .usage
= "[port_num]",
2415 .name
= "gdb_memory_map",
2416 .handler
= handle_gdb_memory_map_command
,
2417 .mode
= COMMAND_CONFIG
,
2418 .help
= "enable or disable memory map",
2419 .usage
= "('enable'|'disable')"
2422 .name
= "gdb_flash_program",
2423 .handler
= handle_gdb_flash_program_command
,
2424 .mode
= COMMAND_CONFIG
,
2425 .help
= "enable or disable flash program",
2426 .usage
= "('enable'|'disable')"
2429 .name
= "gdb_report_data_abort",
2430 .handler
= handle_gdb_report_data_abort_command
,
2431 .mode
= COMMAND_CONFIG
,
2432 .help
= "enable or disable reporting data aborts",
2433 .usage
= "('enable'|'disable')"
2436 .name
= "gdb_breakpoint_override",
2437 .handler
= handle_gdb_breakpoint_override_command
,
2438 .mode
= COMMAND_ANY
,
2439 .help
= "Display or specify type of breakpoint "
2440 "to be used by gdb 'break' commands.",
2441 .usage
= "('hard'|'soft'|'disable')"
2443 COMMAND_REGISTRATION_DONE
2446 int gdb_register_commands(struct command_context
*cmd_ctx
)
2448 gdb_port
= strdup("3333");
2449 gdb_port_next
= strdup("3333");
2450 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);