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 bool sync
; /* 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. */
71 /* We delay reporting memory write errors until next step/continue or memory
72 * write. This improves performance of gdb load significantly as the GDB packet
73 * can be replied immediately and a new GDB packet will be ready without delay
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()
377 memcpy(local_buffer
+ 1, buffer
, len
++);
378 local_buffer
[len
++] = '#';
379 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
380 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
381 retval
= gdb_write(connection
, local_buffer
, len
);
382 if (retval
!= ERROR_OK
)
385 /* larger packets are transmitted directly from caller supplied buffer
386 by several calls to gdb_write() to avoid dynamic allocation */
387 local_buffer
[1] = '#';
388 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
389 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
390 retval
= gdb_write(connection
, local_buffer
, 1);
391 if (retval
!= ERROR_OK
)
393 retval
= gdb_write(connection
, buffer
, len
);
394 if (retval
!= ERROR_OK
)
396 retval
= gdb_write(connection
, local_buffer
+ 1, 3);
397 if (retval
!= ERROR_OK
)
401 if (gdb_con
->noack_mode
)
404 retval
= gdb_get_char(connection
, &reply
);
405 if (retval
!= ERROR_OK
)
410 else if (reply
== '-') {
411 /* Stop sending output packets for now */
412 log_remove_callback(gdb_log_callback
, connection
);
413 LOG_WARNING("negative reply, retrying");
414 } else if (reply
== 0x3) {
416 retval
= gdb_get_char(connection
, &reply
);
417 if (retval
!= ERROR_OK
)
421 else if (reply
== '-') {
422 /* Stop sending output packets for now */
423 log_remove_callback(gdb_log_callback
, connection
);
424 LOG_WARNING("negative reply, retrying");
425 } else if (reply
== '$') {
426 LOG_ERROR("GDB missing ack(1) - assumed good");
427 gdb_putback_char(connection
, reply
);
431 "unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
433 return ERROR_SERVER_REMOTE_CLOSED
;
435 } else if (reply
== '$') {
436 LOG_ERROR("GDB missing ack(2) - assumed good");
437 gdb_putback_char(connection
, reply
);
440 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
443 return ERROR_SERVER_REMOTE_CLOSED
;
447 return ERROR_SERVER_REMOTE_CLOSED
;
452 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
454 struct gdb_connection
*gdb_con
= connection
->priv
;
456 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
459 /* we sent some data, reset timer for keep alive messages */
465 static inline int fetch_packet(struct connection
*connection
,
466 int *checksum_ok
, int noack
, int *len
, char *buffer
)
468 unsigned char my_checksum
= 0;
471 int retval
= ERROR_OK
;
473 struct gdb_connection
*gdb_con
= connection
->priv
;
478 /* move this over into local variables to use registers and give the
479 * more freedom to optimize */
480 char *buf_p
= gdb_con
->buf_p
;
481 int buf_cnt
= gdb_con
->buf_cnt
;
484 /* The common case is that we have an entire packet with no escape chars.
485 * We need to leave at least 2 bytes in the buffer to have
486 * gdb_get_char() update various bits and bobs correctly.
488 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
)) {
489 /* The compiler will struggle a bit with constant propagation and
490 * aliasing, so we help it by showing that these values do not
491 * change inside the loop
495 int run
= buf_cnt
- 2;
501 if (character
== '#') {
502 /* Danger! character can be '#' when esc is
503 * used so we need an explicit boolean for done here.
509 if (character
== '}') {
510 /* data transmitted in binary mode (X packet)
511 * uses 0x7d as escape character */
512 my_checksum
+= character
& 0xff;
515 my_checksum
+= character
& 0xff;
516 buffer
[count
++] = (character
^ 0x20) & 0xff;
518 my_checksum
+= character
& 0xff;
519 buffer
[count
++] = character
& 0xff;
528 LOG_ERROR("packet buffer too small");
529 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
533 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
534 if (retval
!= ERROR_OK
)
537 if (character
== '#')
540 if (character
== '}') {
541 /* data transmitted in binary mode (X packet)
542 * uses 0x7d as escape character */
543 my_checksum
+= character
& 0xff;
545 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
546 if (retval
!= ERROR_OK
)
549 my_checksum
+= character
& 0xff;
550 buffer
[count
++] = (character
^ 0x20) & 0xff;
552 my_checksum
+= character
& 0xff;
553 buffer
[count
++] = character
& 0xff;
557 gdb_con
->buf_p
= buf_p
;
558 gdb_con
->buf_cnt
= buf_cnt
;
560 if (retval
!= ERROR_OK
)
565 retval
= gdb_get_char(connection
, &character
);
566 if (retval
!= ERROR_OK
)
568 checksum
[0] = character
;
569 retval
= gdb_get_char(connection
, &character
);
570 if (retval
!= ERROR_OK
)
572 checksum
[1] = character
;
576 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
581 static int gdb_get_packet_inner(struct connection
*connection
,
582 char *buffer
, int *len
)
586 struct gdb_connection
*gdb_con
= connection
->priv
;
590 retval
= gdb_get_char(connection
, &character
);
591 if (retval
!= ERROR_OK
)
594 #ifdef _DEBUG_GDB_IO_
595 LOG_DEBUG("character: '%c'", character
);
602 /* gdb sends a dummy ack '+' at every remote connect - see
603 * remote_start_remote (remote.c)
604 * in case anyone tries to debug why they receive this
605 * warning every time */
606 LOG_WARNING("acknowledgment received, but no packet pending");
609 LOG_WARNING("negative acknowledgment, but no packet pending");
616 LOG_WARNING("ignoring character 0x%x", character
);
619 } while (character
!= '$');
622 /* explicit code expansion here to get faster inlined code in -O3 by not
623 * calculating checksum
625 if (gdb_con
->noack_mode
) {
626 retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
);
627 if (retval
!= ERROR_OK
)
630 retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
);
631 if (retval
!= ERROR_OK
)
635 if (gdb_con
->noack_mode
) {
636 /* checksum is not checked in noack mode */
640 retval
= gdb_write(connection
, "+", 1);
641 if (retval
!= ERROR_OK
)
647 return ERROR_SERVER_REMOTE_CLOSED
;
652 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
654 struct gdb_connection
*gdb_con
= connection
->priv
;
656 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
661 static int gdb_output_con(struct connection
*connection
, const char *line
)
666 bin_size
= strlen(line
);
668 hex_buffer
= malloc(bin_size
*2 + 2);
669 if (hex_buffer
== NULL
)
670 return ERROR_GDB_BUFFER_TOO_SMALL
;
673 for (i
= 0; i
< bin_size
; i
++)
674 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
675 hex_buffer
[bin_size
*2 + 1] = 0;
677 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
683 static int gdb_output(struct command_context
*context
, const char *line
)
685 /* this will be dumped to the log and also sent as an O packet if possible */
686 LOG_USER_N("%s", line
);
690 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
692 struct gdb_connection
*gdb_connection
= connection
->priv
;
694 /* In the GDB protocol when we are stepping or continuing execution,
695 * we have a lingering reply. Upon receiving a halted event
696 * when we have that lingering packet, we reply to the original
697 * step or continue packet.
699 * Executing monitor commands can bring the target in and
700 * out of the running state so we'll see lots of TARGET_EVENT_XXX
701 * that are to be ignored.
703 if (gdb_connection
->frontend_state
== TARGET_RUNNING
) {
707 /* stop forwarding log packets! */
708 log_remove_callback(gdb_log_callback
, connection
);
710 if (gdb_connection
->ctrl_c
) {
712 gdb_connection
->ctrl_c
= 0;
714 signal_var
= gdb_last_signal(target
);
716 snprintf(sig_reply
, 4, "T%2.2x", signal_var
);
717 gdb_put_packet(connection
, sig_reply
, 3);
718 gdb_connection
->frontend_state
= TARGET_HALTED
;
719 rtos_update_threads(target
);
723 static int gdb_target_callback_event_handler(struct target
*target
,
724 enum target_event event
, void *priv
)
727 struct connection
*connection
= priv
;
729 target_handle_event(target
, event
);
731 case TARGET_EVENT_GDB_HALT
:
732 gdb_frontend_halted(target
, connection
);
734 case TARGET_EVENT_HALTED
:
735 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
737 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
738 retval
= jtag_execute_queue();
739 if (retval
!= ERROR_OK
)
749 static int gdb_new_connection(struct connection
*connection
)
751 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
752 struct gdb_service
*gdb_service
= connection
->service
->priv
;
756 connection
->priv
= gdb_connection
;
758 /* initialize gdb connection information */
759 gdb_connection
->buf_p
= gdb_connection
->buffer
;
760 gdb_connection
->buf_cnt
= 0;
761 gdb_connection
->ctrl_c
= 0;
762 gdb_connection
->frontend_state
= TARGET_HALTED
;
763 gdb_connection
->vflash_image
= NULL
;
764 gdb_connection
->closed
= 0;
765 gdb_connection
->busy
= 0;
766 gdb_connection
->noack_mode
= 0;
767 gdb_connection
->sync
= true;
768 gdb_connection
->mem_write_error
= false;
770 /* send ACK to GDB for debug request */
771 gdb_write(connection
, "+", 1);
773 /* output goes through gdb connection */
774 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
776 /* we must remove all breakpoints registered to the target as a previous
777 * GDB session could leave dangling breakpoints if e.g. communication
780 breakpoint_clear_target(gdb_service
->target
);
781 watchpoint_clear_target(gdb_service
->target
);
783 /* clean previous rtos session if supported*/
784 if ((gdb_service
->target
->rtos
) && (gdb_service
->target
->rtos
->type
->clean
))
785 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
787 /* remove the initial ACK from the incoming buffer */
788 retval
= gdb_get_char(connection
, &initial_ack
);
789 if (retval
!= ERROR_OK
)
792 /* FIX!!!??? would we actually ever receive a + here???
795 if (initial_ack
!= '+')
796 gdb_putback_char(connection
, initial_ack
);
797 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
799 if (gdb_use_memory_map
) {
800 /* Connect must fail if the memory map can't be set up correctly.
802 * This will cause an auto_probe to be invoked, which is either
803 * a no-op or it will fail when the target isn't ready(e.g. not halted).
806 for (i
= 0; i
< flash_get_bank_count(); i
++) {
807 struct flash_bank
*p
;
808 retval
= get_flash_bank_by_num(i
, &p
);
809 if (retval
!= ERROR_OK
) {
811 "Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
817 gdb_actual_connections
++;
818 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
819 gdb_actual_connections
,
820 target_name(gdb_service
->target
),
821 target_state_name(gdb_service
->target
));
823 /* DANGER! If we fail subsequently, we must remove this handler,
824 * otherwise we occasionally see crashes as the timer can invoke the
827 * register callback to be informed about target events */
828 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
833 static int gdb_connection_closed(struct connection
*connection
)
835 struct gdb_service
*gdb_service
= connection
->service
->priv
;
836 struct gdb_connection
*gdb_connection
= connection
->priv
;
838 /* we're done forwarding messages. Tear down callback before
839 * cleaning up connection.
841 log_remove_callback(gdb_log_callback
, connection
);
843 gdb_actual_connections
--;
844 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
845 target_name(gdb_service
->target
),
846 target_state_name(gdb_service
->target
),
847 gdb_actual_connections
);
849 /* see if an image built with vFlash commands is left */
850 if (gdb_connection
->vflash_image
) {
851 image_close(gdb_connection
->vflash_image
);
852 free(gdb_connection
->vflash_image
);
853 gdb_connection
->vflash_image
= NULL
;
856 /* if this connection registered a debug-message receiver delete it */
857 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
859 if (connection
->priv
) {
860 free(connection
->priv
);
861 connection
->priv
= NULL
;
863 LOG_ERROR("BUG: connection->priv == NULL");
865 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
867 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
869 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
874 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
877 snprintf(err
, 4, "E%2.2X", the_error
);
878 gdb_put_packet(connection
, err
, 3);
881 static int gdb_last_signal_packet(struct connection
*connection
,
882 char *packet
, int packet_size
)
884 struct target
*target
= get_target_from_connection(connection
);
888 signal_var
= gdb_last_signal(target
);
890 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
891 gdb_put_packet(connection
, sig_reply
, 3);
896 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
898 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
901 return len
- 1 - pos
;
904 /* Convert register to string of bytes. NB! The # of bits in the
905 * register might be non-divisible by 8(a byte), in which
906 * case an entire byte is shown.
908 * NB! the format on the wire is the target endianness
910 * The format of reg->value is little endian
913 static void gdb_str_to_target(struct target
*target
,
914 char *tstr
, struct reg
*reg
)
921 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
923 for (i
= 0; i
< buf_len
; i
++) {
924 int j
= gdb_reg_pos(target
, i
, buf_len
);
925 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
926 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
930 static int hextoint(int c
)
932 if (c
>= '0' && c
<= '9')
935 if (c
>= 'A' && c
<= 'F')
937 LOG_ERROR("BUG: invalid register value %08x", c
);
941 /* copy over in register buffer */
942 static void gdb_target_to_reg(struct target
*target
,
943 char *tstr
, int str_len
, uint8_t *bin
)
946 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
951 for (i
= 0; i
< str_len
; i
+= 2) {
952 uint8_t t
= hextoint(tstr
[i
]) << 4;
953 t
|= hextoint(tstr
[i
+ 1]);
955 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
960 static int gdb_get_registers_packet(struct connection
*connection
,
961 char *packet
, int packet_size
)
963 struct target
*target
= get_target_from_connection(connection
);
964 struct reg
**reg_list
;
967 int reg_packet_size
= 0;
972 #ifdef _DEBUG_GDB_IO_
976 if ((target
->rtos
!= NULL
) && (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
979 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
980 if (retval
!= ERROR_OK
)
981 return gdb_error(connection
, retval
);
983 for (i
= 0; i
< reg_list_size
; i
++)
984 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
986 assert(reg_packet_size
> 0);
988 reg_packet
= malloc(reg_packet_size
);
989 reg_packet_p
= reg_packet
;
991 for (i
= 0; i
< reg_list_size
; i
++) {
992 if (!reg_list
[i
]->valid
)
993 reg_list
[i
]->type
->get(reg_list
[i
]);
994 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
995 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
998 #ifdef _DEBUG_GDB_IO_
1000 char *reg_packet_p_debug
;
1001 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1002 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1003 free(reg_packet_p_debug
);
1007 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1015 static int gdb_set_registers_packet(struct connection
*connection
,
1016 char *packet
, int packet_size
)
1018 struct target
*target
= get_target_from_connection(connection
);
1020 struct reg
**reg_list
;
1025 #ifdef _DEBUG_GDB_IO_
1029 /* skip command character */
1033 if (packet_size
% 2) {
1035 "GDB set_registers packet with uneven characters received, dropping connection");
1036 return ERROR_SERVER_REMOTE_CLOSED
;
1039 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1040 if (retval
!= ERROR_OK
)
1041 return gdb_error(connection
, retval
);
1044 for (i
= 0; i
< reg_list_size
; i
++) {
1046 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1048 if (packet_p
+ chars
> packet
+ packet_size
)
1049 LOG_ERROR("BUG: register packet is too small for registers");
1051 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1052 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1054 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1056 /* advance packet pointer */
1062 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1065 gdb_put_packet(connection
, "OK", 2);
1070 static int gdb_get_register_packet(struct connection
*connection
,
1071 char *packet
, int packet_size
)
1073 struct target
*target
= get_target_from_connection(connection
);
1075 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1076 struct reg
**reg_list
;
1080 #ifdef _DEBUG_GDB_IO_
1084 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1085 if (retval
!= ERROR_OK
)
1086 return gdb_error(connection
, retval
);
1088 if (reg_list_size
<= reg_num
) {
1089 LOG_ERROR("gdb requested a non-existing register");
1090 return ERROR_SERVER_REMOTE_CLOSED
;
1093 if (!reg_list
[reg_num
]->valid
)
1094 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1096 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1098 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1100 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1108 static int gdb_set_register_packet(struct connection
*connection
,
1109 char *packet
, int packet_size
)
1111 struct target
*target
= get_target_from_connection(connection
);
1114 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1115 struct reg
**reg_list
;
1121 retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
);
1122 if (retval
!= ERROR_OK
)
1123 return gdb_error(connection
, retval
);
1125 if (reg_list_size
<= reg_num
) {
1126 LOG_ERROR("gdb requested a non-existing register");
1127 return ERROR_SERVER_REMOTE_CLOSED
;
1130 if (*separator
!= '=') {
1131 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1132 return ERROR_SERVER_REMOTE_CLOSED
;
1135 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1136 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1137 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1139 if ((unsigned int)chars
!= strlen(separator
+ 1)) {
1140 LOG_ERROR("gdb sent a packet with wrong register size");
1142 return ERROR_SERVER_REMOTE_CLOSED
;
1145 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1147 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1149 gdb_put_packet(connection
, "OK", 2);
1157 /* No attempt is made to translate the "retval" to
1158 * GDB speak. This has to be done at the calling
1159 * site as no mapping really exists.
1161 static int gdb_error(struct connection
*connection
, int retval
)
1163 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1164 gdb_send_error(connection
, EFAULT
);
1168 /* We don't have to worry about the default 2 second timeout for GDB packets,
1169 * because GDB breaks up large memory reads into smaller reads.
1171 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1173 static int gdb_read_memory_packet(struct connection
*connection
,
1174 char *packet
, int packet_size
)
1176 struct target
*target
= get_target_from_connection(connection
);
1184 int retval
= ERROR_OK
;
1186 /* skip command character */
1189 addr
= strtoul(packet
, &separator
, 16);
1191 if (*separator
!= ',') {
1192 LOG_ERROR("incomplete read memory packet received, dropping connection");
1193 return ERROR_SERVER_REMOTE_CLOSED
;
1196 len
= strtoul(separator
+ 1, NULL
, 16);
1198 buffer
= malloc(len
);
1200 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1202 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1204 if ((retval
!= ERROR_OK
) && !gdb_report_data_abort
) {
1205 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1206 * At some point this might be fixed in GDB, in which case this code can be removed.
1208 * OpenOCD developers are acutely aware of this problem, but there is nothing
1209 * gained by involving the user in this problem that hopefully will get resolved
1212 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1213 * cmd = view%20audit-trail&database = gdb&pr = 2395
1215 * For now, the default is to fix up things to make current GDB versions work.
1216 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1218 memset(buffer
, 0, len
);
1222 if (retval
== ERROR_OK
) {
1223 hex_buffer
= malloc(len
* 2 + 1);
1226 for (i
= 0; i
< len
; i
++) {
1227 uint8_t t
= buffer
[i
];
1228 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1229 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1232 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1236 retval
= gdb_error(connection
, retval
);
1243 static int gdb_write_memory_packet(struct connection
*connection
,
1244 char *packet
, int packet_size
)
1246 struct target
*target
= get_target_from_connection(connection
);
1256 /* skip command character */
1259 addr
= strtoul(packet
, &separator
, 16);
1261 if (*separator
!= ',') {
1262 LOG_ERROR("incomplete write memory packet received, dropping connection");
1263 return ERROR_SERVER_REMOTE_CLOSED
;
1266 len
= strtoul(separator
+ 1, &separator
, 16);
1268 if (*(separator
++) != ':') {
1269 LOG_ERROR("incomplete write memory packet received, dropping connection");
1270 return ERROR_SERVER_REMOTE_CLOSED
;
1273 buffer
= malloc(len
);
1275 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1277 for (i
= 0; i
< len
; i
++) {
1279 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1283 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1285 if (retval
== ERROR_OK
)
1286 gdb_put_packet(connection
, "OK", 2);
1288 retval
= gdb_error(connection
, retval
);
1295 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1296 char *packet
, int packet_size
)
1298 struct target
*target
= get_target_from_connection(connection
);
1303 int retval
= ERROR_OK
;
1305 /* skip command character */
1308 addr
= strtoul(packet
, &separator
, 16);
1310 if (*separator
!= ',') {
1311 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1312 return ERROR_SERVER_REMOTE_CLOSED
;
1315 len
= strtoul(separator
+ 1, &separator
, 16);
1317 if (*(separator
++) != ':') {
1318 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1319 return ERROR_SERVER_REMOTE_CLOSED
;
1322 struct gdb_connection
*gdb_connection
= connection
->priv
;
1324 if (gdb_connection
->mem_write_error
) {
1325 retval
= ERROR_FAIL
;
1326 /* now that we have reported the memory write error, we can clear the condition */
1327 gdb_connection
->mem_write_error
= false;
1330 /* By replying the packet *immediately* GDB will send us a new packet
1331 * while we write the last one to the target.
1333 if (retval
== ERROR_OK
)
1334 gdb_put_packet(connection
, "OK", 2);
1336 retval
= gdb_error(connection
, retval
);
1337 if (retval
!= ERROR_OK
)
1342 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1344 retval
= target_write_buffer(target
, addr
, len
, (uint8_t *)separator
);
1345 if (retval
!= ERROR_OK
)
1346 gdb_connection
->mem_write_error
= true;
1352 static int gdb_step_continue_packet(struct connection
*connection
,
1353 char *packet
, int packet_size
)
1355 struct target
*target
= get_target_from_connection(connection
);
1357 uint32_t address
= 0x0;
1358 int retval
= ERROR_OK
;
1362 if (packet_size
> 1) {
1363 packet
[packet_size
] = 0;
1364 address
= strtoul(packet
+ 1, NULL
, 16);
1368 if (packet
[0] == 'c') {
1369 LOG_DEBUG("continue");
1370 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current
1371 *address, don't handle
1372 *breakpoints, not debugging
1374 } else if (packet
[0] == 's') {
1376 /* step at current or address, don't handle breakpoints */
1377 retval
= target_step(target
, current
, address
, 0);
1382 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1383 char *packet
, int packet_size
)
1385 struct target
*target
= get_target_from_connection(connection
);
1387 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1388 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1396 type
= strtoul(packet
+ 1, &separator
, 16);
1398 if (type
== 0) /* memory breakpoint */
1399 bp_type
= BKPT_SOFT
;
1400 else if (type
== 1) /* hardware breakpoint */
1401 bp_type
= BKPT_HARD
;
1402 else if (type
== 2) /* write watchpoint */
1403 wp_type
= WPT_WRITE
;
1404 else if (type
== 3) /* read watchpoint */
1406 else if (type
== 4) /* access watchpoint */
1407 wp_type
= WPT_ACCESS
;
1409 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1410 return ERROR_SERVER_REMOTE_CLOSED
;
1413 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
) || (bp_type
== BKPT_HARD
)))
1414 bp_type
= gdb_breakpoint_override_type
;
1416 if (*separator
!= ',') {
1417 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1418 return ERROR_SERVER_REMOTE_CLOSED
;
1421 address
= strtoul(separator
+ 1, &separator
, 16);
1423 if (*separator
!= ',') {
1424 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1425 return ERROR_SERVER_REMOTE_CLOSED
;
1428 size
= strtoul(separator
+ 1, &separator
, 16);
1433 if (packet
[0] == 'Z') {
1434 retval
= breakpoint_add(target
, address
, size
, bp_type
);
1435 if (retval
!= ERROR_OK
) {
1436 retval
= gdb_error(connection
, retval
);
1437 if (retval
!= ERROR_OK
)
1440 gdb_put_packet(connection
, "OK", 2);
1442 breakpoint_remove(target
, address
);
1443 gdb_put_packet(connection
, "OK", 2);
1450 if (packet
[0] == 'Z') {
1451 retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
);
1452 if (retval
!= ERROR_OK
) {
1453 retval
= gdb_error(connection
, retval
);
1454 if (retval
!= ERROR_OK
)
1457 gdb_put_packet(connection
, "OK", 2);
1459 watchpoint_remove(target
, address
);
1460 gdb_put_packet(connection
, "OK", 2);
1471 /* print out a string and allocate more space as needed,
1472 * mainly used for XML at this point
1474 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1475 const char *fmt
, ...)
1477 if (*retval
!= ERROR_OK
)
1482 if ((*xml
== NULL
) || (!first
)) {
1483 /* start by 0 to exercise all the code paths.
1484 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1486 *size
= *size
* 2 + 2;
1488 *xml
= realloc(*xml
, *size
);
1492 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1500 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1502 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
)) {
1506 /* there was just enough or not enough space, allocate more. */
1511 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1515 /* Extract and NUL-terminate the annex. */
1517 while (*buf
&& *buf
!= ':')
1523 /* After the read marker and annex, qXfer looks like a
1524 * traditional 'm' packet. */
1526 *ofs
= strtoul(buf
, &separator
, 16);
1528 if (*separator
!= ',')
1531 *len
= strtoul(separator
+ 1, NULL
, 16);
1536 static int compare_bank(const void *a
, const void *b
)
1538 struct flash_bank
*b1
, *b2
;
1539 b1
= *((struct flash_bank
**)a
);
1540 b2
= *((struct flash_bank
**)b
);
1542 if (b1
->base
== b2
->base
)
1544 else if (b1
->base
> b2
->base
)
1550 static int gdb_memory_map(struct connection
*connection
,
1551 char *packet
, int packet_size
)
1553 /* We get away with only specifying flash here. Regions that are not
1554 * specified are treated as if we provided no memory map(if not we
1555 * could detect the holes and mark them as RAM).
1556 * Normally we only execute this code once, but no big deal if we
1557 * have to regenerate it a couple of times.
1560 struct target
*target
= get_target_from_connection(connection
);
1561 struct flash_bank
*p
;
1565 int retval
= ERROR_OK
;
1566 struct flash_bank
**banks
;
1570 uint32_t ram_start
= 0;
1572 int target_flash_banks
= 0;
1574 /* skip command character */
1577 offset
= strtoul(packet
, &separator
, 16);
1578 length
= strtoul(separator
+ 1, &separator
, 16);
1580 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1582 /* Sort banks in ascending order. We need to report non-flash
1583 * memory as ram (or rather read/write) by default for GDB, since
1584 * it has no concept of non-cacheable read/write memory (i/o etc).
1586 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1587 * Current versions of GDB assume unlisted addresses are RAM...
1589 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1591 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1592 retval
= get_flash_bank_by_num(i
, &p
);
1593 if (retval
!= ERROR_OK
) {
1595 gdb_error(connection
, retval
);
1598 if (p
->target
== target
)
1599 banks
[target_flash_banks
++] = p
;
1602 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1605 for (i
= 0; i
< target_flash_banks
; i
++) {
1607 unsigned sector_size
= 0;
1613 if (ram_start
< p
->base
)
1614 xml_printf(&retval
, &xml
, &pos
, &size
,
1615 "<memory type=\"ram\" start=\"0x%x\" "
1616 "length=\"0x%x\"/>\n",
1617 ram_start
, p
->base
- ram_start
);
1619 /* Report adjacent groups of same-size sectors. So for
1620 * example top boot CFI flash will list an initial region
1621 * with several large sectors (maybe 128KB) and several
1622 * smaller ones at the end (maybe 32KB). STR7 will have
1623 * regions with 8KB, 32KB, and 64KB sectors; etc.
1625 for (j
= 0; j
< p
->num_sectors
; j
++) {
1628 /* Maybe start a new group of sectors. */
1629 if (sector_size
== 0) {
1630 start
= p
->base
+ p
->sectors
[j
].offset
;
1631 xml_printf(&retval
, &xml
, &pos
, &size
,
1632 "<memory type=\"flash\" "
1635 sector_size
= p
->sectors
[j
].size
;
1638 /* Does this finish a group of sectors?
1639 * If not, continue an already-started group.
1641 if (j
== p
->num_sectors
- 1)
1642 group_len
= (p
->base
+ p
->size
) - start
;
1643 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1644 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1649 xml_printf(&retval
, &xml
, &pos
, &size
,
1650 "length=\"0x%x\">\n"
1651 "<property name=\"blocksize\">"
1659 ram_start
= p
->base
+ p
->size
;
1663 xml_printf(&retval
, &xml
, &pos
, &size
,
1664 "<memory type=\"ram\" start=\"0x%x\" "
1665 "length=\"0x%x\"/>\n",
1666 ram_start
, 0-ram_start
);
1667 /* ELSE a flash chip could be at the very end of the 32 bit address
1668 * space, in which case ram_start will be precisely 0
1674 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1676 if (retval
!= ERROR_OK
) {
1677 gdb_error(connection
, retval
);
1681 if (offset
+ length
> pos
)
1682 length
= pos
- offset
;
1684 char *t
= malloc(length
+ 1);
1686 memcpy(t
+ 1, xml
+ offset
, length
);
1687 gdb_put_packet(connection
, t
, length
+ 1);
1694 static int gdb_query_packet(struct connection
*connection
,
1695 char *packet
, int packet_size
)
1697 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1698 struct gdb_connection
*gdb_connection
= connection
->priv
;
1699 struct target
*target
= get_target_from_connection(connection
);
1701 if (strstr(packet
, "qRcmd,")) {
1702 if (packet_size
> 6) {
1705 cmd
= malloc((packet_size
- 6)/2 + 1);
1706 for (i
= 0; i
< (packet_size
- 6)/2; i
++) {
1708 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1711 cmd
[(packet_size
- 6)/2] = 0x0;
1713 /* We want to print all debug output to GDB connection */
1714 log_add_callback(gdb_log_callback
, connection
);
1715 target_call_timer_callbacks_now();
1716 /* some commands need to know the GDB connection, make note of current
1717 * GDB connection. */
1718 current_gdb_connection
= gdb_connection
;
1719 command_run_line(cmd_ctx
, cmd
);
1720 current_gdb_connection
= NULL
;
1721 target_call_timer_callbacks_now();
1722 log_remove_callback(gdb_log_callback
, connection
);
1725 gdb_put_packet(connection
, "OK", 2);
1727 } else if (strstr(packet
, "qCRC:")) {
1728 if (packet_size
> 5) {
1736 /* skip command character */
1739 addr
= strtoul(packet
, &separator
, 16);
1741 if (*separator
!= ',') {
1743 "incomplete read memory packet received, dropping connection");
1744 return ERROR_SERVER_REMOTE_CLOSED
;
1747 len
= strtoul(separator
+ 1, NULL
, 16);
1749 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1751 if (retval
== ERROR_OK
) {
1752 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1753 gdb_put_packet(connection
, gdb_reply
, 9);
1755 retval
= gdb_error(connection
, retval
);
1756 if (retval
!= ERROR_OK
)
1762 } else if (strstr(packet
, "qSupported")) {
1763 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1764 * disable qXfer:features:read for the moment */
1765 int retval
= ERROR_OK
;
1766 char *buffer
= NULL
;
1774 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1775 (GDB_BUFFER_SIZE
- 1),
1776 ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1778 if (retval
!= ERROR_OK
) {
1779 gdb_send_error(connection
, 01);
1783 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1787 } else if (strstr(packet
, "qXfer:memory-map:read::")
1788 && (flash_get_bank_count() > 0))
1789 return gdb_memory_map(connection
, packet
, packet_size
);
1790 else if (strstr(packet
, "qXfer:features:read:")) {
1794 int retval
= ERROR_OK
;
1797 unsigned int length
;
1800 /* skip command character */
1803 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0) {
1804 gdb_send_error(connection
, 01);
1808 if (strcmp(annex
, "target.xml") != 0) {
1809 gdb_send_error(connection
, 01);
1817 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1819 if (retval
!= ERROR_OK
) {
1820 gdb_error(connection
, retval
);
1824 gdb_put_packet(connection
, xml
, strlen(xml
));
1828 } else if (strstr(packet
, "QStartNoAckMode")) {
1829 gdb_connection
->noack_mode
= 1;
1830 gdb_put_packet(connection
, "OK", 2);
1834 gdb_put_packet(connection
, "", 0);
1838 static int gdb_v_packet(struct connection
*connection
,
1839 char *packet
, int packet_size
)
1841 struct gdb_connection
*gdb_connection
= connection
->priv
;
1842 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1845 /* if flash programming disabled - send a empty reply */
1847 if (gdb_flash_program
== 0) {
1848 gdb_put_packet(connection
, "", 0);
1852 if (strstr(packet
, "vFlashErase:")) {
1854 unsigned long length
;
1856 char *parse
= packet
+ 12;
1857 if (*parse
== '\0') {
1858 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1859 return ERROR_SERVER_REMOTE_CLOSED
;
1862 addr
= strtoul(parse
, &parse
, 16);
1864 if (*(parse
++) != ',' || *parse
== '\0') {
1865 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1866 return ERROR_SERVER_REMOTE_CLOSED
;
1869 length
= strtoul(parse
, &parse
, 16);
1871 if (*parse
!= '\0') {
1872 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1873 return ERROR_SERVER_REMOTE_CLOSED
;
1876 /* assume all sectors need erasing - stops any problems
1877 * when flash_write is called multiple times */
1880 /* perform any target specific operations before the erase */
1881 target_call_event_callbacks(gdb_service
->target
,
1882 TARGET_EVENT_GDB_FLASH_ERASE_START
);
1884 /* vFlashErase:addr,length messages require region start and
1885 * end to be "block" aligned ... if padding is ever needed,
1886 * GDB will have become dangerously confused.
1888 result
= flash_erase_address_range(gdb_service
->target
,
1889 false, addr
, length
);
1891 /* perform any target specific operations after the erase */
1892 target_call_event_callbacks(gdb_service
->target
,
1893 TARGET_EVENT_GDB_FLASH_ERASE_END
);
1896 if (result
!= ERROR_OK
) {
1897 /* GDB doesn't evaluate the actual error number returned,
1898 * treat a failed erase as an I/O error
1900 gdb_send_error(connection
, EIO
);
1901 LOG_ERROR("flash_erase returned %i", result
);
1903 gdb_put_packet(connection
, "OK", 2);
1908 if (strstr(packet
, "vFlashWrite:")) {
1911 unsigned long length
;
1912 char *parse
= packet
+ 12;
1914 if (*parse
== '\0') {
1915 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1916 return ERROR_SERVER_REMOTE_CLOSED
;
1918 addr
= strtoul(parse
, &parse
, 16);
1919 if (*(parse
++) != ':') {
1920 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1921 return ERROR_SERVER_REMOTE_CLOSED
;
1923 length
= packet_size
- (parse
- packet
);
1925 /* create a new image if there isn't already one */
1926 if (gdb_connection
->vflash_image
== NULL
) {
1927 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
1928 image_open(gdb_connection
->vflash_image
, "", "build");
1931 /* create new section with content from packet buffer */
1932 retval
= image_add_section(gdb_connection
->vflash_image
,
1933 addr
, length
, 0x0, (uint8_t *)parse
);
1934 if (retval
!= ERROR_OK
)
1937 gdb_put_packet(connection
, "OK", 2);
1942 if (!strcmp(packet
, "vFlashDone")) {
1945 /* process the flashing buffer. No need to erase as GDB
1946 * always issues a vFlashErase first. */
1947 target_call_event_callbacks(gdb_service
->target
,
1948 TARGET_EVENT_GDB_FLASH_WRITE_START
);
1950 flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1951 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1952 if (result
!= ERROR_OK
) {
1953 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1954 gdb_put_packet(connection
, "E.memtype", 9);
1956 gdb_send_error(connection
, EIO
);
1958 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
1959 gdb_put_packet(connection
, "OK", 2);
1962 image_close(gdb_connection
->vflash_image
);
1963 free(gdb_connection
->vflash_image
);
1964 gdb_connection
->vflash_image
= NULL
;
1969 gdb_put_packet(connection
, "", 0);
1973 static int gdb_detach(struct connection
*connection
)
1975 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1977 target_call_event_callbacks(gdb_service
->target
,
1978 TARGET_EVENT_GDB_DETACH
);
1980 return gdb_put_packet(connection
, "OK", 2);
1983 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
1984 const char *function
, const char *string
)
1986 struct connection
*connection
= priv
;
1987 struct gdb_connection
*gdb_con
= connection
->priv
;
1989 if (gdb_con
->busy
) {
1990 /* do not reply this using the O packet */
1994 gdb_output_con(connection
, string
);
1997 static void gdb_sig_halted(struct connection
*connection
)
2000 snprintf(sig_reply
, 4, "T%2.2x", 2);
2001 gdb_put_packet(connection
, sig_reply
, 3);
2005 static int gdb_input_inner(struct connection
*connection
)
2007 /* Do not allocate this on the stack */
2008 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2010 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2011 struct target
*target
= gdb_service
->target
;
2012 char *packet
= gdb_packet_buffer
;
2015 struct gdb_connection
*gdb_con
= connection
->priv
;
2016 static int extended_protocol
;
2018 /* drain input buffer. If one of the packets fail, then an error
2019 * packet is replied, if applicable.
2021 * This loop will terminate and the error code is returned.
2023 * The calling fn will check if this error is something that
2024 * can be recovered from, or if the connection must be closed.
2026 * If the error is recoverable, this fn is called again to
2027 * drain the rest of the buffer.
2030 packet_size
= GDB_BUFFER_SIZE
-1;
2031 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2032 if (retval
!= ERROR_OK
)
2035 /* terminate with zero */
2036 packet
[packet_size
] = 0;
2038 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2039 if (packet
[0] == 'X') {
2040 /* binary packets spew junk into the debug log stream */
2043 for (x
= 0; (x
< 49) && (packet
[x
] != ':'); x
++)
2046 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2048 LOG_DEBUG("received packet: '%s'", packet
);
2051 if (packet_size
> 0) {
2053 switch (packet
[0]) {
2054 case 'T': /* Is thread alive? */
2055 gdb_thread_packet(connection
, packet
, packet_size
);
2057 case 'H': /* Set current thread ( 'c' for step and continue, 'g' for
2058 * all other operations ) */
2059 gdb_thread_packet(connection
, packet
, packet_size
);
2063 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2064 if (retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2065 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2068 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2071 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2074 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2077 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2080 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2083 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2087 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2090 gdb_last_signal_packet(connection
, packet
, packet_size
);
2095 gdb_thread_packet(connection
, packet
, packet_size
);
2096 log_add_callback(gdb_log_callback
, connection
);
2098 if (gdb_con
->mem_write_error
) {
2099 LOG_ERROR("Memory write failure!");
2101 /* now that we have reported the memory write error,
2102 *we can clear the condition */
2103 gdb_con
->mem_write_error
= false;
2106 bool nostep
= false;
2107 bool already_running
= false;
2108 if (target
->state
== TARGET_RUNNING
) {
2109 LOG_WARNING("WARNING! The target is already running. "
2110 "All changes GDB did to registers will be discarded! "
2111 "Waiting for target to halt.");
2112 already_running
= true;
2113 } else if (target
->state
!= TARGET_HALTED
) {
2114 LOG_WARNING("The target is not in the halted nor running stated, " \
2115 "stepi/continue ignored.");
2117 } else if ((packet
[0] == 's') && gdb_con
->sync
) {
2118 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2119 * sent by GDB first to OpenOCD, thus defeating the check to
2120 * make only the single stepping have the sync feature...
2123 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2124 "from the target.");
2126 gdb_con
->sync
= false;
2128 if (!already_running
&& nostep
) {
2129 /* Either the target isn't in the halted state, then we can't
2130 * step/continue. This might be early setup, etc.
2132 * Or we want to allow GDB to pick up a fresh set of
2133 * register values without modifying the target state.
2136 gdb_sig_halted(connection
);
2138 /* stop forwarding log packets! */
2139 log_remove_callback(gdb_log_callback
, connection
);
2141 /* We're running/stepping, in which case we can
2142 * forward log output until the target is halted
2144 gdb_con
->frontend_state
= TARGET_RUNNING
;
2145 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2147 if (!already_running
) {
2148 /* Here we don't want packet processing to stop even if this fails,
2149 * so we use a local variable instead of retval. */
2150 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2151 if (retval
!= ERROR_OK
) {
2152 /* we'll never receive a halted
2153 *condition... issue a false one..
2155 gdb_frontend_halted(target
, connection
);
2162 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2165 retval
= gdb_detach(connection
);
2166 extended_protocol
= 0;
2169 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2170 if (retval
!= ERROR_OK
)
2174 if (extended_protocol
!= 0)
2176 gdb_put_packet(connection
, "OK", 2);
2177 return ERROR_SERVER_REMOTE_CLOSED
;
2179 /* handle extended remote protocol */
2180 extended_protocol
= 1;
2181 gdb_put_packet(connection
, "OK", 2);
2184 /* handle extended restart packet */
2185 breakpoint_clear_target(gdb_service
->target
);
2186 watchpoint_clear_target(gdb_service
->target
);
2187 command_run_linef(connection
->cmd_ctx
,
2188 "ocd_gdb_restart %s",
2189 target_name(target
));
2190 /* info rtos parts */
2191 gdb_thread_packet(connection
, packet
, packet_size
);
2192 gdb_put_packet(connection
, "OK", 2);
2196 /* packet supported only by smp target i.e cortex_a.c*/
2197 /* handle smp packet replying coreid played to gbd */
2198 gdb_read_smp_packet(connection
, packet
, packet_size
);
2202 /* packet supported only by smp target i.e cortex_a.c */
2203 /* handle smp packet setting coreid to be played at next
2205 gdb_write_smp_packet(connection
, packet
, packet_size
);
2209 /* ignore unknown packets */
2210 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2211 gdb_put_packet(connection
, NULL
, 0);
2215 /* if a packet handler returned an error, exit input loop */
2216 if (retval
!= ERROR_OK
)
2220 if (gdb_con
->ctrl_c
) {
2221 if (target
->state
== TARGET_RUNNING
) {
2222 retval
= target_halt(target
);
2223 if (retval
!= ERROR_OK
)
2224 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2225 gdb_con
->ctrl_c
= 0;
2228 "The target is not running when halt was requested, stopping GDB.");
2229 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2233 } while (gdb_con
->buf_cnt
> 0);
2238 static int gdb_input(struct connection
*connection
)
2240 int retval
= gdb_input_inner(connection
);
2241 struct gdb_connection
*gdb_con
= connection
->priv
;
2242 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2245 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2246 if (gdb_con
->closed
)
2247 return ERROR_SERVER_REMOTE_CLOSED
;
2249 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2253 static int gdb_target_start(struct target
*target
, const char *port
)
2255 struct gdb_service
*gdb_service
;
2257 gdb_service
= malloc(sizeof(struct gdb_service
));
2259 if (NULL
== gdb_service
)
2262 gdb_service
->target
= target
;
2263 gdb_service
->core
[0] = -1;
2264 gdb_service
->core
[1] = -1;
2265 target
->gdb_service
= gdb_service
;
2267 ret
= add_service("gdb",
2268 port
, 1, &gdb_new_connection
, &gdb_input
,
2269 &gdb_connection_closed
, gdb_service
);
2270 /* initialialize all targets gdb service with the same pointer */
2272 struct target_list
*head
;
2273 struct target
*curr
;
2274 head
= target
->head
;
2275 while (head
!= (struct target_list
*)NULL
) {
2276 curr
= head
->target
;
2278 curr
->gdb_service
= gdb_service
;
2285 static int gdb_target_add_one(struct target
*target
)
2287 /* one gdb instance per smp list */
2288 if ((target
->smp
) && (target
->gdb_service
))
2290 int retval
= gdb_target_start(target
, gdb_port_next
);
2291 if (retval
== ERROR_OK
) {
2293 /* If we can parse the port number
2294 * then we increment the port number for the next target.
2297 portnumber
= strtol(gdb_port_next
, &end
, 0);
2299 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
) {
2300 free((void *)gdb_port_next
);
2301 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2308 int gdb_target_add_all(struct target
*target
)
2310 if (NULL
== target
) {
2311 LOG_WARNING("gdb services need one or more targets defined");
2315 while (NULL
!= target
) {
2316 int retval
= gdb_target_add_one(target
);
2317 if (ERROR_OK
!= retval
)
2320 target
= target
->next
;
2326 COMMAND_HANDLER(handle_gdb_sync_command
)
2329 return ERROR_COMMAND_SYNTAX_ERROR
;
2331 if (current_gdb_connection
== NULL
) {
2332 command_print(CMD_CTX
,
2333 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2337 current_gdb_connection
->sync
= true;
2342 /* daemon configuration command gdb_port */
2343 COMMAND_HANDLER(handle_gdb_port_command
)
2345 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2346 if (ERROR_OK
== retval
) {
2347 free((void *)gdb_port_next
);
2348 gdb_port_next
= strdup(gdb_port
);
2353 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2356 return ERROR_COMMAND_SYNTAX_ERROR
;
2358 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2362 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2365 return ERROR_COMMAND_SYNTAX_ERROR
;
2367 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2371 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2374 return ERROR_COMMAND_SYNTAX_ERROR
;
2376 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2380 /* gdb_breakpoint_override */
2381 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2383 if (CMD_ARGC
== 0) {
2385 } else if (CMD_ARGC
== 1) {
2386 gdb_breakpoint_override
= 1;
2387 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2388 gdb_breakpoint_override_type
= BKPT_HARD
;
2389 else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2390 gdb_breakpoint_override_type
= BKPT_SOFT
;
2391 else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2392 gdb_breakpoint_override
= 0;
2394 return ERROR_COMMAND_SYNTAX_ERROR
;
2395 if (gdb_breakpoint_override
)
2396 LOG_USER("force %s breakpoints",
2397 (gdb_breakpoint_override_type
== BKPT_HARD
) ? "hard" : "soft");
2399 LOG_USER("breakpoint type is not overridden");
2404 static const struct command_registration gdb_command_handlers
[] = {
2407 .handler
= handle_gdb_sync_command
,
2408 .mode
= COMMAND_ANY
,
2409 .help
= "next stepi will return immediately allowing "
2410 "GDB to fetch register state without affecting "
2416 .handler
= handle_gdb_port_command
,
2417 .mode
= COMMAND_ANY
,
2418 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2419 "server listens for the next port number after the "
2420 "base port number specified. "
2421 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2422 "output to stdout, an integer is base port number, \"disable\" disables "
2423 "port. Any other string is are interpreted as named pipe to listen to. "
2424 "Output pipe is the same name as input pipe, but with 'o' appended.",
2425 .usage
= "[port_num]",
2428 .name
= "gdb_memory_map",
2429 .handler
= handle_gdb_memory_map_command
,
2430 .mode
= COMMAND_CONFIG
,
2431 .help
= "enable or disable memory map",
2432 .usage
= "('enable'|'disable')"
2435 .name
= "gdb_flash_program",
2436 .handler
= handle_gdb_flash_program_command
,
2437 .mode
= COMMAND_CONFIG
,
2438 .help
= "enable or disable flash program",
2439 .usage
= "('enable'|'disable')"
2442 .name
= "gdb_report_data_abort",
2443 .handler
= handle_gdb_report_data_abort_command
,
2444 .mode
= COMMAND_CONFIG
,
2445 .help
= "enable or disable reporting data aborts",
2446 .usage
= "('enable'|'disable')"
2449 .name
= "gdb_breakpoint_override",
2450 .handler
= handle_gdb_breakpoint_override_command
,
2451 .mode
= COMMAND_ANY
,
2452 .help
= "Display or specify type of breakpoint "
2453 "to be used by gdb 'break' commands.",
2454 .usage
= "('hard'|'soft'|'disable')"
2456 COMMAND_REGISTRATION_DONE
2459 int gdb_register_commands(struct command_context
*cmd_ctx
)
2461 gdb_port
= strdup("3333");
2462 gdb_port_next
= strdup("3333");
2463 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);