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 ***************************************************************************/
36 #include <target/breakpoints.h>
37 #include <target/target_request.h>
38 #include <target/register.h>
40 #include <flash/nor/core.h>
41 #include "gdb_server.h"
42 #include <target/image.h>
43 #include <jtag/jtag.h>
44 #include "rtos/rtos.h"
45 #include "target/smp.h"
49 * GDB server implementation.
51 * This implements the GDB Remote Serial Protocol, over TCP connections,
52 * giving GDB access to the JTAG or other hardware debugging facilities
53 * found in most modern embedded processors.
56 /* private connection data for GDB */
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
)
117 case DBG_REASON_DBGRQ
:
118 return 0x2; /* SIGINT */
119 case DBG_REASON_BREAKPOINT
:
120 case DBG_REASON_WATCHPOINT
:
121 case DBG_REASON_WPTANDBKPT
:
122 return 0x05; /* SIGTRAP */
123 case DBG_REASON_SINGLESTEP
:
124 return 0x05; /* SIGTRAP */
125 case DBG_REASON_NOTHALTED
:
126 return 0x0; /* no signal... shouldn't happen */
128 LOG_USER("undefined debug reason %d - target needs reset", 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)
154 FD_SET(connection
->fd
, &read_fds
);
156 tv
.tv_sec
= timeout_s
;
158 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
160 /* This can typically be because a "monitor" command took too long
161 * before printing any progress messages
165 return ERROR_GDB_TIMEOUT
;
171 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
175 static int gdb_get_char_inner(struct connection
*connection
, int* next_char
)
177 struct gdb_connection
*gdb_con
= connection
->priv
;
178 int retval
= ERROR_OK
;
180 #ifdef _DEBUG_GDB_IO_
185 if (connection
->service
->type
!= CONNECTION_TCP
)
187 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
191 retval
= check_pending(connection
, 1, NULL
);
192 if (retval
!= ERROR_OK
)
194 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
197 if (gdb_con
->buf_cnt
> 0)
201 if (gdb_con
->buf_cnt
== 0)
204 return ERROR_SERVER_REMOTE_CLOSED
;
208 errno
= WSAGetLastError();
215 case WSAECONNABORTED
:
217 return ERROR_SERVER_REMOTE_CLOSED
;
220 return ERROR_SERVER_REMOTE_CLOSED
;
222 LOG_ERROR("read: %d", errno
);
233 return ERROR_SERVER_REMOTE_CLOSED
;
236 return ERROR_SERVER_REMOTE_CLOSED
;
238 LOG_ERROR("read: %s", strerror(errno
));
240 return ERROR_SERVER_REMOTE_CLOSED
;
245 #ifdef _DEBUG_GDB_IO_
246 debug_buffer
= strndup(gdb_con
->buffer
, gdb_con
->buf_cnt
);
247 LOG_DEBUG("received '%s'", debug_buffer
);
251 gdb_con
->buf_p
= gdb_con
->buffer
;
253 *next_char
= *(gdb_con
->buf_p
++);
254 if (gdb_con
->buf_cnt
> 0)
255 connection
->input_pending
= 1;
257 connection
->input_pending
= 0;
258 #ifdef _DEBUG_GDB_IO_
259 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
266 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
267 * held in registers in the inner loop.
269 * For small caches and embedded systems this is important!
271 static inline int gdb_get_char_fast(struct connection
*connection
, int* next_char
, char **buf_p
, int *buf_cnt
)
273 int retval
= ERROR_OK
;
275 if ((*buf_cnt
)-- > 0)
277 *next_char
= **buf_p
;
280 connection
->input_pending
= 1;
282 connection
->input_pending
= 0;
284 #ifdef _DEBUG_GDB_IO_
285 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
291 struct gdb_connection
*gdb_con
= connection
->priv
;
292 gdb_con
->buf_p
= *buf_p
;
293 gdb_con
->buf_cnt
= *buf_cnt
;
294 retval
= gdb_get_char_inner(connection
, next_char
);
295 *buf_p
= gdb_con
->buf_p
;
296 *buf_cnt
= gdb_con
->buf_cnt
;
302 static int gdb_get_char(struct connection
*connection
, int* next_char
)
304 struct gdb_connection
*gdb_con
= connection
->priv
;
305 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
309 static int gdb_putback_char(struct connection
*connection
, int last_char
)
311 struct gdb_connection
*gdb_con
= connection
->priv
;
313 if (gdb_con
->buf_p
> gdb_con
->buffer
)
315 *(--gdb_con
->buf_p
) = last_char
;
320 LOG_ERROR("BUG: couldn't put character back");
326 /* The only way we can detect that the socket is closed is the first time
327 * we write to it, we will fail. Subsequent write operations will
328 * succeed. Shudder! */
329 static int gdb_write(struct connection
*connection
, void *data
, int len
)
331 struct gdb_connection
*gdb_con
= connection
->priv
;
333 return ERROR_SERVER_REMOTE_CLOSED
;
335 if (connection_write(connection
, data
, len
) == len
)
340 return ERROR_SERVER_REMOTE_CLOSED
;
343 static int gdb_put_packet_inner(struct connection
*connection
,
344 char *buffer
, int len
)
347 unsigned char my_checksum
= 0;
348 #ifdef _DEBUG_GDB_IO_
353 struct gdb_connection
*gdb_con
= connection
->priv
;
355 for (i
= 0; i
< len
; i
++)
356 my_checksum
+= buffer
[i
];
358 #ifdef _DEBUG_GDB_IO_
360 * At this point we should have nothing in the input queue from GDB,
361 * however sometimes '-' is sent even though we've already received
362 * an ACK (+) for everything we've sent off.
367 retval
= check_pending(connection
, 0, &gotdata
);
368 if (retval
!= ERROR_OK
)
372 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
375 /* fix a problem with some IAR tools */
376 gdb_putback_char(connection
, reply
);
377 LOG_DEBUG("Unexpected start of new packet");
381 LOG_WARNING("Discard unexpected char %c", reply
);
387 #ifdef _DEBUG_GDB_IO_
388 debug_buffer
= strndup(buffer
, len
);
389 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
393 char local_buffer
[1024];
394 local_buffer
[0] = '$';
395 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
397 /* performance gain on smaller packets by only a single call to gdb_write() */
398 memcpy(local_buffer
+ 1, buffer
, len
++);
399 local_buffer
[len
++] = '#';
400 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
401 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
402 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
409 /* larger packets are transmitted directly from caller supplied buffer
410 by several calls to gdb_write() to avoid dynamic allocation */
411 local_buffer
[1] = '#';
412 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
413 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
414 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
418 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
422 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
428 if (gdb_con
->noack_mode
)
431 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
436 else if (reply
== '-')
438 /* Stop sending output packets for now */
439 log_remove_callback(gdb_log_callback
, connection
);
440 LOG_WARNING("negative reply, retrying");
442 else if (reply
== 0x3)
445 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
449 else if (reply
== '-')
451 /* Stop sending output packets for now */
452 log_remove_callback(gdb_log_callback
, connection
);
453 LOG_WARNING("negative reply, retrying");
455 else if (reply
== '$') {
456 LOG_ERROR("GDB missing ack(1) - assumed good");
457 gdb_putback_char(connection
, reply
);
461 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
463 return ERROR_SERVER_REMOTE_CLOSED
;
466 else if (reply
== '$') {
467 LOG_ERROR("GDB missing ack(2) - assumed good");
468 gdb_putback_char(connection
, reply
);
473 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
475 return ERROR_SERVER_REMOTE_CLOSED
;
479 return ERROR_SERVER_REMOTE_CLOSED
;
484 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
486 struct gdb_connection
*gdb_con
= connection
->priv
;
488 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
491 /* we sent some data, reset timer for keep alive messages */
497 static __inline__
int fetch_packet(struct connection
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
499 unsigned char my_checksum
= 0;
502 int retval
= ERROR_OK
;
504 struct gdb_connection
*gdb_con
= connection
->priv
;
509 /* move this over into local variables to use registers and give the
510 * more freedom to optimize */
511 char *buf_p
= gdb_con
->buf_p
;
512 int buf_cnt
= gdb_con
->buf_cnt
;
516 /* The common case is that we have an entire packet with no escape chars.
517 * We need to leave at least 2 bytes in the buffer to have
518 * gdb_get_char() update various bits and bobs correctly.
520 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
))
522 /* The compiler will struggle a bit with constant propagation and
523 * aliasing, so we help it by showing that these values do not
524 * change inside the loop
528 int run
= buf_cnt
- 2;
535 if (character
== '#')
537 /* Danger! character can be '#' when esc is
538 * used so we need an explicit boolean for done here.
544 if (character
== '}')
546 /* data transmitted in binary mode (X packet)
547 * uses 0x7d as escape character */
548 my_checksum
+= character
& 0xff;
551 my_checksum
+= character
& 0xff;
552 buffer
[count
++] = (character
^ 0x20) & 0xff;
556 my_checksum
+= character
& 0xff;
557 buffer
[count
++] = character
& 0xff;
567 LOG_ERROR("packet buffer too small");
568 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
572 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
573 if (retval
!= ERROR_OK
)
576 if (character
== '#')
579 if (character
== '}')
581 /* data transmitted in binary mode (X packet)
582 * uses 0x7d as escape character */
583 my_checksum
+= character
& 0xff;
585 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
586 if (retval
!= ERROR_OK
)
589 my_checksum
+= character
& 0xff;
590 buffer
[count
++] = (character
^ 0x20) & 0xff;
594 my_checksum
+= character
& 0xff;
595 buffer
[count
++] = character
& 0xff;
599 gdb_con
->buf_p
= buf_p
;
600 gdb_con
->buf_cnt
= buf_cnt
;
602 if (retval
!= ERROR_OK
)
607 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
609 checksum
[0] = character
;
610 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
612 checksum
[1] = character
;
617 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
623 static int gdb_get_packet_inner(struct connection
*connection
,
624 char *buffer
, int *len
)
628 struct gdb_connection
*gdb_con
= connection
->priv
;
634 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
637 #ifdef _DEBUG_GDB_IO_
638 LOG_DEBUG("character: '%c'", character
);
646 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
647 * in case anyone tries to debug why they receive this warning every time */
648 LOG_WARNING("acknowledgment received, but no packet pending");
651 LOG_WARNING("negative acknowledgment, but no packet pending");
658 LOG_WARNING("ignoring character 0x%x", character
);
661 } while (character
!= '$');
666 /* explicit code expansion here to get faster inlined code in -O3 by not
667 * calculating checksum
669 if (gdb_con
->noack_mode
)
671 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
675 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
679 if (gdb_con
->noack_mode
)
681 /* checksum is not checked in noack mode */
686 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
694 return ERROR_SERVER_REMOTE_CLOSED
;
699 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
701 struct gdb_connection
*gdb_con
= connection
->priv
;
703 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
708 static int gdb_output_con(struct connection
*connection
, const char* line
)
713 bin_size
= strlen(line
);
715 hex_buffer
= malloc(bin_size
*2 + 2);
716 if (hex_buffer
== NULL
)
717 return ERROR_GDB_BUFFER_TOO_SMALL
;
720 for (i
= 0; i
< bin_size
; i
++)
721 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
722 hex_buffer
[bin_size
*2 + 1] = 0;
724 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
730 static int gdb_output(struct command_context
*context
, const char* line
)
732 /* this will be dumped to the log and also sent as an O packet if possible */
733 LOG_USER_N("%s", line
);
738 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
740 struct gdb_connection
*gdb_connection
= connection
->priv
;
742 /* In the GDB protocol when we are stepping or continuing execution,
743 * we have a lingering reply. Upon receiving a halted event
744 * when we have that lingering packet, we reply to the original
745 * step or continue packet.
747 * Executing monitor commands can bring the target in and
748 * out of the running state so we'll see lots of TARGET_EVENT_XXX
749 * that are to be ignored.
751 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
756 /* stop forwarding log packets! */
757 log_remove_callback(gdb_log_callback
, connection
);
759 if (gdb_connection
->ctrl_c
)
762 gdb_connection
->ctrl_c
= 0;
766 signal_var
= gdb_last_signal(target
);
769 snprintf(sig_reply
, 4, "T%2.2x", signal_var
);
770 gdb_put_packet(connection
, sig_reply
, 3);
771 gdb_connection
->frontend_state
= TARGET_HALTED
;
772 rtos_update_threads( target
);
776 static int gdb_target_callback_event_handler(struct target
*target
,
777 enum target_event event
, void *priv
)
780 struct connection
*connection
= priv
;
782 target_handle_event(target
, event
);
785 case TARGET_EVENT_GDB_HALT
:
786 gdb_frontend_halted(target
, connection
);
788 case TARGET_EVENT_HALTED
:
789 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
791 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
792 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
793 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
805 static int gdb_new_connection(struct connection
*connection
)
807 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
808 struct gdb_service
*gdb_service
= connection
->service
->priv
;
812 connection
->priv
= gdb_connection
;
814 /* initialize gdb connection information */
815 gdb_connection
->buf_p
= gdb_connection
->buffer
;
816 gdb_connection
->buf_cnt
= 0;
817 gdb_connection
->ctrl_c
= 0;
818 gdb_connection
->frontend_state
= TARGET_HALTED
;
819 gdb_connection
->vflash_image
= NULL
;
820 gdb_connection
->closed
= 0;
821 gdb_connection
->busy
= 0;
822 gdb_connection
->noack_mode
= 0;
823 gdb_connection
->sync
= true;
824 gdb_connection
->mem_write_error
= false;
826 /* send ACK to GDB for debug request */
827 gdb_write(connection
, "+", 1);
829 /* output goes through gdb connection */
830 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
832 /* we must remove all breakpoints registered to the target as a previous
833 * GDB session could leave dangling breakpoints if e.g. communication
836 breakpoint_clear_target(gdb_service
->target
);
837 watchpoint_clear_target(gdb_service
->target
);
839 /* clean previous rtos session if supported*/
840 if ((gdb_service
->target
->rtos
) &&
841 (gdb_service
->target
->rtos
->type
->clean
))
842 gdb_service
->target
->rtos
->type
->clean(gdb_service
->target
);
844 /* remove the initial ACK from the incoming buffer */
845 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
848 /* FIX!!!??? would we actually ever receive a + here???
851 if (initial_ack
!= '+')
852 gdb_putback_char(connection
, initial_ack
);
853 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
855 if (gdb_use_memory_map
)
857 /* Connect must fail if the memory map can't be set up correctly.
859 * This will cause an auto_probe to be invoked, which is either
860 * a no-op or it will fail when the target isn't ready(e.g. not halted).
863 for (i
= 0; i
< flash_get_bank_count(); i
++)
865 struct flash_bank
*p
;
866 retval
= get_flash_bank_by_num(i
, &p
);
867 if (retval
!= ERROR_OK
)
869 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
875 gdb_actual_connections
++;
876 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
877 gdb_actual_connections
,
878 target_name(gdb_service
->target
),
879 target_state_name(gdb_service
->target
));
881 /* DANGER! If we fail subsequently, we must remove this handler,
882 * otherwise we occasionally see crashes as the timer can invoke the
885 * register callback to be informed about target events */
886 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
891 static int gdb_connection_closed(struct connection
*connection
)
893 struct gdb_service
*gdb_service
= connection
->service
->priv
;
894 struct gdb_connection
*gdb_connection
= connection
->priv
;
896 /* we're done forwarding messages. Tear down callback before
897 * cleaning up connection.
899 log_remove_callback(gdb_log_callback
, connection
);
901 gdb_actual_connections
--;
902 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
903 target_name(gdb_service
->target
),
904 target_state_name(gdb_service
->target
),
905 gdb_actual_connections
);
907 /* see if an image built with vFlash commands is left */
908 if (gdb_connection
->vflash_image
)
910 image_close(gdb_connection
->vflash_image
);
911 free(gdb_connection
->vflash_image
);
912 gdb_connection
->vflash_image
= NULL
;
915 /* if this connection registered a debug-message receiver delete it */
916 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
918 if (connection
->priv
)
920 free(connection
->priv
);
921 connection
->priv
= NULL
;
925 LOG_ERROR("BUG: connection->priv == NULL");
929 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
931 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
933 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
938 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
941 snprintf(err
, 4, "E%2.2X", the_error
);
942 gdb_put_packet(connection
, err
, 3);
945 static int gdb_last_signal_packet(struct connection
*connection
,
946 char* packet
, int packet_size
)
948 struct target
*target
= get_target_from_connection(connection
);
952 signal_var
= gdb_last_signal(target
);
954 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
955 gdb_put_packet(connection
, sig_reply
, 3);
960 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
962 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
965 return len
- 1 - pos
;
968 /* Convert register to string of bytes. NB! The # of bits in the
969 * register might be non-divisible by 8(a byte), in which
970 * case an entire byte is shown.
972 * NB! the format on the wire is the target endianness
974 * The format of reg->value is little endian
977 static void gdb_str_to_target(struct target
*target
,
978 char *tstr
, struct reg
*reg
)
985 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
987 for (i
= 0; i
< buf_len
; i
++)
989 int j
= gdb_reg_pos(target
, i
, buf_len
);
990 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
991 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
995 static int hextoint(int c
)
1006 LOG_ERROR("BUG: invalid register value %08x", c
);
1010 /* copy over in register buffer */
1011 static void gdb_target_to_reg(struct target
*target
,
1012 char *tstr
, int str_len
, uint8_t *bin
)
1016 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1021 for (i
= 0; i
< str_len
; i
+= 2)
1023 uint8_t t
= hextoint(tstr
[i
]) << 4;
1024 t
|= hextoint(tstr
[i
+ 1]);
1026 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1031 static int gdb_get_registers_packet(struct connection
*connection
,
1032 char* packet
, int packet_size
)
1034 struct target
*target
= get_target_from_connection(connection
);
1035 struct reg
**reg_list
;
1038 int reg_packet_size
= 0;
1043 #ifdef _DEBUG_GDB_IO_
1047 if ((target
->rtos
!= NULL
) &&
1048 (ERROR_OK
== rtos_get_gdb_reg_list(connection
)))
1053 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1055 return gdb_error(connection
, retval
);
1058 for (i
= 0; i
< reg_list_size
; i
++)
1060 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1063 assert(reg_packet_size
> 0);
1065 reg_packet
= malloc(reg_packet_size
);
1066 reg_packet_p
= reg_packet
;
1068 for (i
= 0; i
< reg_list_size
; i
++)
1070 if (!reg_list
[i
]->valid
)
1071 reg_list
[i
]->type
->get(reg_list
[i
]);
1072 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1073 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1076 #ifdef _DEBUG_GDB_IO_
1078 char *reg_packet_p_debug
;
1079 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1080 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1081 free(reg_packet_p_debug
);
1085 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1093 static int gdb_set_registers_packet(struct connection
*connection
,
1094 char *packet
, int packet_size
)
1096 struct target
*target
= get_target_from_connection(connection
);
1098 struct reg
**reg_list
;
1103 #ifdef _DEBUG_GDB_IO_
1107 /* skip command character */
1111 if (packet_size
% 2)
1113 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1114 return ERROR_SERVER_REMOTE_CLOSED
;
1117 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1119 return gdb_error(connection
, retval
);
1123 for (i
= 0; i
< reg_list_size
; i
++)
1126 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1128 if (packet_p
+ chars
> packet
+ packet_size
)
1130 LOG_ERROR("BUG: register packet is too small for registers");
1133 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1134 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1136 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1138 /* advance packet pointer */
1145 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1148 gdb_put_packet(connection
, "OK", 2);
1153 static int gdb_get_register_packet(struct connection
*connection
,
1154 char *packet
, int packet_size
)
1156 struct target
*target
= get_target_from_connection(connection
);
1158 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1159 struct reg
**reg_list
;
1163 #ifdef _DEBUG_GDB_IO_
1167 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1169 return gdb_error(connection
, retval
);
1172 if (reg_list_size
<= reg_num
)
1174 LOG_ERROR("gdb requested a non-existing register");
1178 if (!reg_list
[reg_num
]->valid
)
1179 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1181 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1183 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1185 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1193 static int gdb_set_register_packet(struct connection
*connection
,
1194 char *packet
, int packet_size
)
1196 struct target
*target
= get_target_from_connection(connection
);
1199 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1200 struct reg
**reg_list
;
1206 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1208 return gdb_error(connection
, retval
);
1211 if (reg_list_size
< reg_num
)
1213 LOG_ERROR("gdb requested a non-existing register");
1214 return ERROR_SERVER_REMOTE_CLOSED
;
1217 if (*separator
!= '=')
1219 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1220 return ERROR_SERVER_REMOTE_CLOSED
;
1223 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1224 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1225 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1227 /* fix!!! add some sanity checks on packet size here */
1229 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1231 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1233 gdb_put_packet(connection
, "OK", 2);
1241 /* No attempt is made to translate the "retval" to
1242 * GDB speak. This has to be done at the calling
1243 * site as no mapping really exists.
1245 static int gdb_error(struct connection
*connection
, int retval
)
1247 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1248 gdb_send_error(connection
, EFAULT
);
1252 /* We don't have to worry about the default 2 second timeout for GDB packets,
1253 * because GDB breaks up large memory reads into smaller reads.
1255 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1257 static int gdb_read_memory_packet(struct connection
*connection
,
1258 char *packet
, int packet_size
)
1260 struct target
*target
= get_target_from_connection(connection
);
1268 int retval
= ERROR_OK
;
1270 /* skip command character */
1273 addr
= strtoul(packet
, &separator
, 16);
1275 if (*separator
!= ',')
1277 LOG_ERROR("incomplete read memory packet received, dropping connection");
1278 return ERROR_SERVER_REMOTE_CLOSED
;
1281 len
= strtoul(separator
+ 1, NULL
, 16);
1283 buffer
= malloc(len
);
1285 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1287 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1289 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1291 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1292 * At some point this might be fixed in GDB, in which case this code can be removed.
1294 * OpenOCD developers are acutely aware of this problem, but there is nothing
1295 * gained by involving the user in this problem that hopefully will get resolved
1298 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1300 * For now, the default is to fix up things to make current GDB versions work.
1301 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1303 memset(buffer
, 0, len
);
1307 if (retval
== ERROR_OK
)
1309 hex_buffer
= malloc(len
* 2 + 1);
1312 for (i
= 0; i
< len
; i
++)
1314 uint8_t t
= buffer
[i
];
1315 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1316 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1319 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1325 retval
= gdb_error(connection
, retval
);
1333 static int gdb_write_memory_packet(struct connection
*connection
,
1334 char *packet
, int packet_size
)
1336 struct target
*target
= get_target_from_connection(connection
);
1346 /* skip command character */
1349 addr
= strtoul(packet
, &separator
, 16);
1351 if (*separator
!= ',')
1353 LOG_ERROR("incomplete write memory packet received, dropping connection");
1354 return ERROR_SERVER_REMOTE_CLOSED
;
1357 len
= strtoul(separator
+ 1, &separator
, 16);
1359 if (*(separator
++) != ':')
1361 LOG_ERROR("incomplete write memory packet received, dropping connection");
1362 return ERROR_SERVER_REMOTE_CLOSED
;
1365 buffer
= malloc(len
);
1367 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1369 for (i
= 0; i
< len
; i
++)
1372 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1376 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1378 if (retval
== ERROR_OK
)
1380 gdb_put_packet(connection
, "OK", 2);
1384 retval
= gdb_error(connection
, retval
);
1392 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1393 char *packet
, int packet_size
)
1395 struct target
*target
= get_target_from_connection(connection
);
1400 int retval
= ERROR_OK
;
1402 /* skip command character */
1405 addr
= strtoul(packet
, &separator
, 16);
1407 if (*separator
!= ',')
1409 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1410 return ERROR_SERVER_REMOTE_CLOSED
;
1413 len
= strtoul(separator
+ 1, &separator
, 16);
1415 if (*(separator
++) != ':')
1417 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1418 return ERROR_SERVER_REMOTE_CLOSED
;
1421 struct gdb_connection
*gdb_connection
= connection
->priv
;
1423 if (gdb_connection
->mem_write_error
)
1425 retval
= ERROR_FAIL
;
1426 /* now that we have reported the memory write error, we can clear the condition */
1427 gdb_connection
->mem_write_error
= false;
1430 /* By replying the packet *immediately* GDB will send us a new packet
1431 * while we write the last one to the target.
1433 if (retval
== ERROR_OK
)
1435 gdb_put_packet(connection
, "OK", 2);
1439 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1445 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1447 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1448 if (retval
!= ERROR_OK
)
1450 gdb_connection
->mem_write_error
= true;
1457 static int gdb_step_continue_packet(struct connection
*connection
,
1458 char *packet
, int packet_size
)
1460 struct target
*target
= get_target_from_connection(connection
);
1462 uint32_t address
= 0x0;
1463 int retval
= ERROR_OK
;
1467 if (packet_size
> 1)
1469 packet
[packet_size
] = 0;
1470 address
= strtoul(packet
+ 1, NULL
, 16);
1477 if (packet
[0] == 'c')
1479 LOG_DEBUG("continue");
1480 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1481 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1483 else if (packet
[0] == 's')
1486 /* step at current or address, don't handle breakpoints */
1487 retval
= target_step(target
, current
, address
, 0);
1492 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1493 char *packet
, int packet_size
)
1495 struct target
*target
= get_target_from_connection(connection
);
1497 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1498 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1506 type
= strtoul(packet
+ 1, &separator
, 16);
1508 if (type
== 0) /* memory breakpoint */
1509 bp_type
= BKPT_SOFT
;
1510 else if (type
== 1) /* hardware breakpoint */
1511 bp_type
= BKPT_HARD
;
1512 else if (type
== 2) /* write watchpoint */
1513 wp_type
= WPT_WRITE
;
1514 else if (type
== 3) /* read watchpoint */
1516 else if (type
== 4) /* access watchpoint */
1517 wp_type
= WPT_ACCESS
;
1520 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1521 return ERROR_SERVER_REMOTE_CLOSED
;
1525 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1527 bp_type
= gdb_breakpoint_override_type
;
1530 if (*separator
!= ',')
1532 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1533 return ERROR_SERVER_REMOTE_CLOSED
;
1536 address
= strtoul(separator
+ 1, &separator
, 16);
1538 if (*separator
!= ',')
1540 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1541 return ERROR_SERVER_REMOTE_CLOSED
;
1544 size
= strtoul(separator
+ 1, &separator
, 16);
1550 if (packet
[0] == 'Z')
1552 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1554 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1559 gdb_put_packet(connection
, "OK", 2);
1564 breakpoint_remove(target
, address
);
1565 gdb_put_packet(connection
, "OK", 2);
1572 if (packet
[0] == 'Z')
1574 if ((retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
)) != ERROR_OK
)
1576 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1581 gdb_put_packet(connection
, "OK", 2);
1586 watchpoint_remove(target
, address
);
1587 gdb_put_packet(connection
, "OK", 2);
1598 /* print out a string and allocate more space as needed,
1599 * mainly used for XML at this point
1601 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1602 const char *fmt
, ...)
1604 if (*retval
!= ERROR_OK
)
1612 if ((*xml
== NULL
) || (!first
))
1614 /* start by 0 to exercise all the code paths.
1615 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1617 *size
= *size
* 2 + 2;
1619 *xml
= realloc(*xml
, *size
);
1624 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1632 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1634 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1639 /* there was just enough or not enough space, allocate more. */
1644 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1648 /* Extract and NUL-terminate the annex. */
1650 while (*buf
&& *buf
!= ':')
1656 /* After the read marker and annex, qXfer looks like a
1657 * traditional 'm' packet. */
1659 *ofs
= strtoul(buf
, &separator
, 16);
1661 if (*separator
!= ',')
1664 *len
= strtoul(separator
+ 1, NULL
, 16);
1669 static int compare_bank (const void * a
, const void * b
)
1671 struct flash_bank
*b1
, *b2
;
1672 b1
=*((struct flash_bank
**)a
);
1673 b2
=*((struct flash_bank
**)b
);
1675 if (b1
->base
== b2
->base
)
1678 } else if (b1
->base
> b2
->base
)
1687 static int gdb_memory_map(struct connection
*connection
,
1688 char *packet
, int packet_size
)
1690 /* We get away with only specifying flash here. Regions that are not
1691 * specified are treated as if we provided no memory map(if not we
1692 * could detect the holes and mark them as RAM).
1693 * Normally we only execute this code once, but no big deal if we
1694 * have to regenerate it a couple of times.
1697 struct target
*target
= get_target_from_connection(connection
);
1698 struct flash_bank
*p
;
1702 int retval
= ERROR_OK
;
1703 struct flash_bank
**banks
;
1707 uint32_t ram_start
= 0;
1709 int target_flash_banks
= 0;
1711 /* skip command character */
1714 offset
= strtoul(packet
, &separator
, 16);
1715 length
= strtoul(separator
+ 1, &separator
, 16);
1717 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1719 /* Sort banks in ascending order. We need to report non-flash
1720 * memory as ram (or rather read/write) by default for GDB, since
1721 * it has no concept of non-cacheable read/write memory (i/o etc).
1723 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1724 * Current versions of GDB assume unlisted addresses are RAM...
1726 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1728 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1729 retval
= get_flash_bank_by_num(i
, &p
);
1730 if (retval
!= ERROR_OK
)
1733 gdb_error(connection
, retval
);
1736 if(p
->target
== target
)
1737 banks
[target_flash_banks
++] = p
;
1740 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1743 for (i
= 0; i
< target_flash_banks
; i
++) {
1745 unsigned sector_size
= 0;
1751 if (ram_start
< p
->base
)
1752 xml_printf(&retval
, &xml
, &pos
, &size
,
1753 "<memory type=\"ram\" start=\"0x%x\" "
1754 "length=\"0x%x\"/>\n",
1755 ram_start
, p
->base
- ram_start
);
1757 /* Report adjacent groups of same-size sectors. So for
1758 * example top boot CFI flash will list an initial region
1759 * with several large sectors (maybe 128KB) and several
1760 * smaller ones at the end (maybe 32KB). STR7 will have
1761 * regions with 8KB, 32KB, and 64KB sectors; etc.
1763 for (j
= 0; j
< p
->num_sectors
; j
++) {
1766 /* Maybe start a new group of sectors. */
1767 if (sector_size
== 0) {
1768 start
= p
->base
+ p
->sectors
[j
].offset
;
1769 xml_printf(&retval
, &xml
, &pos
, &size
,
1770 "<memory type=\"flash\" "
1773 sector_size
= p
->sectors
[j
].size
;
1776 /* Does this finish a group of sectors?
1777 * If not, continue an already-started group.
1779 if (j
== p
->num_sectors
-1)
1780 group_len
= (p
->base
+ p
->size
) - start
;
1781 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1782 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1787 xml_printf(&retval
, &xml
, &pos
, &size
,
1788 "length=\"0x%x\">\n"
1789 "<property name=\"blocksize\">"
1797 ram_start
= p
->base
+ p
->size
;
1801 xml_printf(&retval
, &xml
, &pos
, &size
,
1802 "<memory type=\"ram\" start=\"0x%x\" "
1803 "length=\"0x%x\"/>\n",
1804 ram_start
, 0-ram_start
);
1805 /* ELSE a flash chip could be at the very end of the 32 bit address
1806 * space, in which case ram_start will be precisely 0
1812 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1814 if (retval
!= ERROR_OK
) {
1815 gdb_error(connection
, retval
);
1819 if (offset
+ length
> pos
)
1820 length
= pos
- offset
;
1822 char *t
= malloc(length
+ 1);
1824 memcpy(t
+ 1, xml
+ offset
, length
);
1825 gdb_put_packet(connection
, t
, length
+ 1);
1832 static int gdb_query_packet(struct connection
*connection
,
1833 char *packet
, int packet_size
)
1835 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1836 struct gdb_connection
*gdb_connection
= connection
->priv
;
1837 struct target
*target
= get_target_from_connection(connection
);
1839 if (strstr(packet
, "qRcmd,"))
1841 if (packet_size
> 6)
1845 cmd
= malloc((packet_size
- 6)/2 + 1);
1846 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1849 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1852 cmd
[(packet_size
- 6)/2] = 0x0;
1854 /* We want to print all debug output to GDB connection */
1855 log_add_callback(gdb_log_callback
, connection
);
1856 target_call_timer_callbacks_now();
1857 /* some commands need to know the GDB connection, make note of current
1858 * GDB connection. */
1859 current_gdb_connection
= gdb_connection
;
1860 command_run_line(cmd_ctx
, cmd
);
1861 current_gdb_connection
= NULL
;
1862 target_call_timer_callbacks_now();
1863 log_remove_callback(gdb_log_callback
, connection
);
1866 gdb_put_packet(connection
, "OK", 2);
1869 else if (strstr(packet
, "qCRC:"))
1871 if (packet_size
> 5)
1880 /* skip command character */
1883 addr
= strtoul(packet
, &separator
, 16);
1885 if (*separator
!= ',')
1887 LOG_ERROR("incomplete read memory packet received, dropping connection");
1888 return ERROR_SERVER_REMOTE_CLOSED
;
1891 len
= strtoul(separator
+ 1, NULL
, 16);
1893 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1895 if (retval
== ERROR_OK
)
1897 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1898 gdb_put_packet(connection
, gdb_reply
, 9);
1902 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1909 else if (strstr(packet
, "qSupported"))
1911 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1912 * disable qXfer:features:read for the moment */
1913 int retval
= ERROR_OK
;
1914 char *buffer
= NULL
;
1918 xml_printf(&retval
, &buffer
, &pos
, &size
,
1919 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1920 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1922 if (retval
!= ERROR_OK
)
1924 gdb_send_error(connection
, 01);
1928 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1933 else if (strstr(packet
, "qXfer:memory-map:read::")
1934 && (flash_get_bank_count() > 0))
1935 return gdb_memory_map(connection
, packet
, packet_size
);
1936 else if (strstr(packet
, "qXfer:features:read:"))
1941 int retval
= ERROR_OK
;
1944 unsigned int length
;
1947 /* skip command character */
1950 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1952 gdb_send_error(connection
, 01);
1956 if (strcmp(annex
, "target.xml") != 0)
1958 gdb_send_error(connection
, 01);
1962 xml_printf(&retval
, &xml
, &pos
, &size
, \
1963 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1965 if (retval
!= ERROR_OK
)
1967 gdb_error(connection
, retval
);
1971 gdb_put_packet(connection
, xml
, strlen(xml
));
1976 else if (strstr(packet
, "QStartNoAckMode"))
1978 gdb_connection
->noack_mode
= 1;
1979 gdb_put_packet(connection
, "OK", 2);
1983 gdb_put_packet(connection
, "", 0);
1987 static int gdb_v_packet(struct connection
*connection
,
1988 char *packet
, int packet_size
)
1990 struct gdb_connection
*gdb_connection
= connection
->priv
;
1991 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1994 /* if flash programming disabled - send a empty reply */
1996 if (gdb_flash_program
== 0)
1998 gdb_put_packet(connection
, "", 0);
2002 if (strstr(packet
, "vFlashErase:"))
2005 unsigned long length
;
2007 char *parse
= packet
+ 12;
2010 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2011 return ERROR_SERVER_REMOTE_CLOSED
;
2014 addr
= strtoul(parse
, &parse
, 16);
2016 if (*(parse
++) != ',' || *parse
== '\0')
2018 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2019 return ERROR_SERVER_REMOTE_CLOSED
;
2022 length
= strtoul(parse
, &parse
, 16);
2026 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2027 return ERROR_SERVER_REMOTE_CLOSED
;
2030 /* assume all sectors need erasing - stops any problems
2031 * when flash_write is called multiple times */
2034 /* perform any target specific operations before the erase */
2035 target_call_event_callbacks(gdb_service
->target
,
2036 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2038 /* vFlashErase:addr,length messages require region start and
2039 * end to be "block" aligned ... if padding is ever needed,
2040 * GDB will have become dangerously confused.
2042 result
= flash_erase_address_range(gdb_service
->target
,
2043 false, addr
, length
);
2045 /* perform any target specific operations after the erase */
2046 target_call_event_callbacks(gdb_service
->target
,
2047 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2050 if (result
!= ERROR_OK
)
2052 /* GDB doesn't evaluate the actual error number returned,
2053 * treat a failed erase as an I/O error
2055 gdb_send_error(connection
, EIO
);
2056 LOG_ERROR("flash_erase returned %i", result
);
2059 gdb_put_packet(connection
, "OK", 2);
2064 if (strstr(packet
, "vFlashWrite:"))
2068 unsigned long length
;
2069 char *parse
= packet
+ 12;
2073 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2074 return ERROR_SERVER_REMOTE_CLOSED
;
2076 addr
= strtoul(parse
, &parse
, 16);
2077 if (*(parse
++) != ':')
2079 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2080 return ERROR_SERVER_REMOTE_CLOSED
;
2082 length
= packet_size
- (parse
- packet
);
2084 /* create a new image if there isn't already one */
2085 if (gdb_connection
->vflash_image
== NULL
)
2087 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2088 image_open(gdb_connection
->vflash_image
, "", "build");
2091 /* create new section with content from packet buffer */
2092 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
2097 gdb_put_packet(connection
, "OK", 2);
2102 if (!strcmp(packet
, "vFlashDone"))
2106 /* process the flashing buffer. No need to erase as GDB
2107 * always issues a vFlashErase first. */
2108 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
2109 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2110 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2111 if (result
!= ERROR_OK
)
2113 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2114 gdb_put_packet(connection
, "E.memtype", 9);
2116 gdb_send_error(connection
, EIO
);
2120 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2121 gdb_put_packet(connection
, "OK", 2);
2124 image_close(gdb_connection
->vflash_image
);
2125 free(gdb_connection
->vflash_image
);
2126 gdb_connection
->vflash_image
= NULL
;
2131 gdb_put_packet(connection
, "", 0);
2135 static int gdb_detach(struct connection
*connection
)
2137 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2139 target_call_event_callbacks(gdb_service
->target
,
2140 TARGET_EVENT_GDB_DETACH
);
2142 return gdb_put_packet(connection
, "OK", 2);
2145 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2146 const char *function
, const char *string
)
2148 struct connection
*connection
= priv
;
2149 struct gdb_connection
*gdb_con
= connection
->priv
;
2153 /* do not reply this using the O packet */
2157 gdb_output_con(connection
, string
);
2160 static void gdb_sig_halted(struct connection
*connection
)
2163 snprintf(sig_reply
, 4, "T%2.2x", 2);
2164 gdb_put_packet(connection
, sig_reply
, 3);
2168 static int gdb_input_inner(struct connection
*connection
)
2170 /* Do not allocate this on the stack */
2171 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2173 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2174 struct target
*target
= gdb_service
->target
;
2175 char *packet
= gdb_packet_buffer
;
2178 struct gdb_connection
*gdb_con
= connection
->priv
;
2179 static int extended_protocol
= 0;
2181 /* drain input buffer. If one of the packets fail, then an error
2182 * packet is replied, if applicable.
2184 * This loop will terminate and the error code is returned.
2186 * The calling fn will check if this error is something that
2187 * can be recovered from, or if the connection must be closed.
2189 * If the error is recoverable, this fn is called again to
2190 * drain the rest of the buffer.
2194 packet_size
= GDB_BUFFER_SIZE
-1;
2195 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2196 if (retval
!= ERROR_OK
)
2199 /* terminate with zero */
2200 packet
[packet_size
] = 0;
2202 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2203 if (packet
[0] == 'X') {
2204 // binary packets spew junk into the debug log stream
2207 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2211 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2213 LOG_DEBUG("received packet: '%s'", packet
);
2217 if (packet_size
> 0)
2222 case 'T': // Is thread alive?
2223 gdb_thread_packet(connection
, packet
, packet_size
);
2225 case 'H': // Set current thread ( 'c' for step and continue, 'g' for all other operations )
2226 gdb_thread_packet(connection
, packet
, packet_size
);
2230 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2231 if ( retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2233 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2237 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2240 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2243 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2246 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2249 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2252 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2256 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2259 gdb_last_signal_packet(connection
, packet
, packet_size
);
2264 gdb_thread_packet(connection
, packet
, packet_size
);
2265 log_add_callback(gdb_log_callback
, connection
);
2267 if (gdb_con
->mem_write_error
)
2269 LOG_ERROR("Memory write failure!");
2271 /* now that we have reported the memory write error, we can clear the condition */
2272 gdb_con
->mem_write_error
= false;
2275 bool nostep
= false;
2276 bool already_running
= false;
2277 if (target
->state
== TARGET_RUNNING
)
2279 LOG_WARNING("WARNING! The target is already running. "
2280 "All changes GDB did to registers will be discarded! "
2281 "Waiting for target to halt.");
2282 already_running
= true;
2283 } else if (target
->state
!= TARGET_HALTED
)
2285 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2287 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2289 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2290 * sent by GDB first to OpenOCD, thus defeating the check to
2291 * make only the single stepping have the sync feature...
2294 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2296 gdb_con
->sync
= false;
2298 if (!already_running
&& nostep
)
2300 /* Either the target isn't in the halted state, then we can't
2301 * step/continue. This might be early setup, etc.
2303 * Or we want to allow GDB to pick up a fresh set of
2304 * register values without modifying the target state.
2307 gdb_sig_halted(connection
);
2309 /* stop forwarding log packets! */
2310 log_remove_callback(gdb_log_callback
, connection
);
2313 /* We're running/stepping, in which case we can
2314 * forward log output until the target is halted
2316 gdb_con
->frontend_state
= TARGET_RUNNING
;
2317 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2319 if (!already_running
)
2321 /* Here we don't want packet processing to stop even if this fails,
2322 * so we use a local variable instead of retval. */
2323 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2324 if (retval
!= ERROR_OK
)
2326 /* we'll never receive a halted condition... issue a false one.. */
2327 gdb_frontend_halted(target
, connection
);
2334 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2337 retval
= gdb_detach(connection
);
2338 extended_protocol
= 0;
2341 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2342 if (retval
!= ERROR_OK
)
2346 if (extended_protocol
!= 0)
2348 gdb_put_packet(connection
, "OK", 2);
2349 return ERROR_SERVER_REMOTE_CLOSED
;
2351 /* handle extended remote protocol */
2352 extended_protocol
= 1;
2353 gdb_put_packet(connection
, "OK", 2);
2356 /* handle extended restart packet */
2357 breakpoint_clear_target(gdb_service
->target
);
2358 watchpoint_clear_target(gdb_service
->target
);
2359 command_run_linef(connection
->cmd_ctx
,
2360 "ocd_gdb_restart %s",
2361 target_name(target
));
2362 /* info rtos parts */
2363 gdb_thread_packet(connection
, packet
, packet_size
);
2364 gdb_put_packet(connection
, "OK", 2);
2368 /* packet supported only by smp target i.e cortex_a.c*/
2369 /* handle smp packet replying coreid played to gbd */
2370 gdb_read_smp_packet(connection
, packet
, packet_size
);
2374 /* packet supported only by smp target i.e cortex_a.c */
2375 /* handle smp packet setting coreid to be played at next
2377 gdb_write_smp_packet(connection
, packet
, packet_size
);
2381 /* ignore unknown packets */
2382 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2383 gdb_put_packet(connection
, NULL
, 0);
2387 /* if a packet handler returned an error, exit input loop */
2388 if (retval
!= ERROR_OK
)
2392 if (gdb_con
->ctrl_c
)
2394 if (target
->state
== TARGET_RUNNING
)
2396 retval
= target_halt(target
);
2397 if (retval
!= ERROR_OK
)
2399 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2401 gdb_con
->ctrl_c
= 0;
2404 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2405 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2409 } while (gdb_con
->buf_cnt
> 0);
2414 static int gdb_input(struct connection
*connection
)
2416 int retval
= gdb_input_inner(connection
);
2417 struct gdb_connection
*gdb_con
= connection
->priv
;
2418 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2421 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2422 if (gdb_con
->closed
)
2423 return ERROR_SERVER_REMOTE_CLOSED
;
2425 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2429 static int gdb_target_start(struct target
*target
, const char *port
)
2432 struct gdb_service
*gdb_service
;
2434 gdb_service
= malloc(sizeof(struct gdb_service
));
2436 if (NULL
== gdb_service
)
2439 gdb_service
->target
= target
;
2440 gdb_service
->core
[0] = -1;
2441 gdb_service
->core
[1] = -1;
2442 target
->gdb_service
= gdb_service
;
2444 ret
= add_service("gdb",
2445 port
, 1, &gdb_new_connection
, &gdb_input
,
2446 &gdb_connection_closed
, gdb_service
);
2447 /* initialialize all targets gdb service with the same pointer */
2449 struct target_list
*head
;
2450 struct target
*curr
;
2451 head
= target
->head
;
2452 while(head
!= (struct target_list
*)NULL
)
2454 curr
= head
->target
;
2455 if (curr
!= target
) curr
->gdb_service
= gdb_service
;
2462 static int gdb_target_add_one(struct target
*target
)
2464 /* one gdb instance per smp list */
2465 if ((target
->smp
) && (target
->gdb_service
)) return ERROR_OK
;
2466 int retval
= gdb_target_start(target
, gdb_port_next
);
2467 if (retval
== ERROR_OK
)
2470 /* If we can parse the port number
2471 * then we increment the port number for the next target.
2474 portnumber
= strtol(gdb_port_next
, &end
, 0);
2477 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
)
2479 free((void *)gdb_port_next
);
2480 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2487 int gdb_target_add_all(struct target
*target
)
2491 LOG_WARNING("gdb services need one or more targets defined");
2495 while (NULL
!= target
)
2497 int retval
= gdb_target_add_one(target
);
2498 if (ERROR_OK
!= retval
)
2501 target
= target
->next
;
2507 COMMAND_HANDLER(handle_gdb_sync_command
)
2511 return ERROR_COMMAND_SYNTAX_ERROR
;
2514 if (current_gdb_connection
== NULL
)
2516 command_print(CMD_CTX
,
2517 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2521 current_gdb_connection
->sync
= true;
2526 /* daemon configuration command gdb_port */
2527 COMMAND_HANDLER(handle_gdb_port_command
)
2529 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2530 if (ERROR_OK
== retval
) {
2531 free((void*)gdb_port_next
);
2532 gdb_port_next
= strdup(gdb_port
);
2537 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2540 return ERROR_COMMAND_SYNTAX_ERROR
;
2542 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2546 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2549 return ERROR_COMMAND_SYNTAX_ERROR
;
2551 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2555 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2558 return ERROR_COMMAND_SYNTAX_ERROR
;
2560 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2564 /* gdb_breakpoint_override */
2565 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2570 } else if (CMD_ARGC
== 1)
2572 gdb_breakpoint_override
= 1;
2573 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2575 gdb_breakpoint_override_type
= BKPT_HARD
;
2576 } else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2578 gdb_breakpoint_override_type
= BKPT_SOFT
;
2579 } else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2581 gdb_breakpoint_override
= 0;
2585 return ERROR_COMMAND_SYNTAX_ERROR
;
2587 if (gdb_breakpoint_override
)
2589 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2592 LOG_USER("breakpoint type is not overridden");
2598 static const struct command_registration gdb_command_handlers
[] = {
2601 .handler
= handle_gdb_sync_command
,
2602 .mode
= COMMAND_ANY
,
2603 .help
= "next stepi will return immediately allowing "
2604 "GDB to fetch register state without affecting "
2610 .handler
= handle_gdb_port_command
,
2611 .mode
= COMMAND_ANY
,
2612 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2613 "server listens for the next port number after the "
2614 "base port number specified. "
2615 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2616 "output to stdout, an integer is base port number, \"disable\" disables "
2617 "port. Any other string is are interpreted as named pipe to listen to. "
2618 "Output pipe is the same name as input pipe, but with 'o' appended.",
2619 .usage
= "[port_num]",
2622 .name
= "gdb_memory_map",
2623 .handler
= handle_gdb_memory_map_command
,
2624 .mode
= COMMAND_CONFIG
,
2625 .help
= "enable or disable memory map",
2626 .usage
= "('enable'|'disable')"
2629 .name
= "gdb_flash_program",
2630 .handler
= handle_gdb_flash_program_command
,
2631 .mode
= COMMAND_CONFIG
,
2632 .help
= "enable or disable flash program",
2633 .usage
= "('enable'|'disable')"
2636 .name
= "gdb_report_data_abort",
2637 .handler
= handle_gdb_report_data_abort_command
,
2638 .mode
= COMMAND_CONFIG
,
2639 .help
= "enable or disable reporting data aborts",
2640 .usage
= "('enable'|'disable')"
2643 .name
= "gdb_breakpoint_override",
2644 .handler
= handle_gdb_breakpoint_override_command
,
2645 .mode
= COMMAND_ANY
,
2646 .help
= "Display or specify type of breakpoint "
2647 "to be used by gdb 'break' commands.",
2648 .usage
= "('hard'|'soft'|'disable')"
2650 COMMAND_REGISTRATION_DONE
2653 int gdb_register_commands(struct command_context
*cmd_ctx
)
2655 gdb_port
= strdup("3333");
2656 gdb_port_next
= strdup("3333");
2657 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);