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 /* remove the initial ACK from the incoming buffer */
840 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
843 /* FIX!!!??? would we actually ever receive a + here???
846 if (initial_ack
!= '+')
847 gdb_putback_char(connection
, initial_ack
);
848 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
850 if (gdb_use_memory_map
)
852 /* Connect must fail if the memory map can't be set up correctly.
854 * This will cause an auto_probe to be invoked, which is either
855 * a no-op or it will fail when the target isn't ready(e.g. not halted).
858 for (i
= 0; i
< flash_get_bank_count(); i
++)
860 struct flash_bank
*p
;
861 retval
= get_flash_bank_by_num(i
, &p
);
862 if (retval
!= ERROR_OK
)
864 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'.");
870 gdb_actual_connections
++;
871 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
872 gdb_actual_connections
,
873 target_name(gdb_service
->target
),
874 target_state_name(gdb_service
->target
));
876 /* DANGER! If we fail subsequently, we must remove this handler,
877 * otherwise we occasionally see crashes as the timer can invoke the
880 * register callback to be informed about target events */
881 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
886 static int gdb_connection_closed(struct connection
*connection
)
888 struct gdb_service
*gdb_service
= connection
->service
->priv
;
889 struct gdb_connection
*gdb_connection
= connection
->priv
;
891 /* we're done forwarding messages. Tear down callback before
892 * cleaning up connection.
894 log_remove_callback(gdb_log_callback
, connection
);
896 gdb_actual_connections
--;
897 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
898 target_name(gdb_service
->target
),
899 target_state_name(gdb_service
->target
),
900 gdb_actual_connections
);
902 /* see if an image built with vFlash commands is left */
903 if (gdb_connection
->vflash_image
)
905 image_close(gdb_connection
->vflash_image
);
906 free(gdb_connection
->vflash_image
);
907 gdb_connection
->vflash_image
= NULL
;
910 /* if this connection registered a debug-message receiver delete it */
911 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
913 if (connection
->priv
)
915 free(connection
->priv
);
916 connection
->priv
= NULL
;
920 LOG_ERROR("BUG: connection->priv == NULL");
924 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
926 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
928 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
933 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
936 snprintf(err
, 4, "E%2.2X", the_error
);
937 gdb_put_packet(connection
, err
, 3);
940 static int gdb_last_signal_packet(struct connection
*connection
,
941 char* packet
, int packet_size
)
943 struct target
*target
= get_target_from_connection(connection
);
947 signal_var
= gdb_last_signal(target
);
949 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
950 gdb_put_packet(connection
, sig_reply
, 3);
955 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
957 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
960 return len
- 1 - pos
;
963 /* Convert register to string of bytes. NB! The # of bits in the
964 * register might be non-divisible by 8(a byte), in which
965 * case an entire byte is shown.
967 * NB! the format on the wire is the target endianness
969 * The format of reg->value is little endian
972 static void gdb_str_to_target(struct target
*target
,
973 char *tstr
, struct reg
*reg
)
980 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
982 for (i
= 0; i
< buf_len
; i
++)
984 int j
= gdb_reg_pos(target
, i
, buf_len
);
985 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
986 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
990 static int hextoint(int c
)
1001 LOG_ERROR("BUG: invalid register value %08x", c
);
1005 /* copy over in register buffer */
1006 static void gdb_target_to_reg(struct target
*target
,
1007 char *tstr
, int str_len
, uint8_t *bin
)
1011 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1016 for (i
= 0; i
< str_len
; i
+= 2)
1018 uint8_t t
= hextoint(tstr
[i
]) << 4;
1019 t
|= hextoint(tstr
[i
+ 1]);
1021 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1026 static int gdb_get_registers_packet(struct connection
*connection
,
1027 char* packet
, int packet_size
)
1029 struct target
*target
= get_target_from_connection(connection
);
1030 struct reg
**reg_list
;
1033 int reg_packet_size
= 0;
1038 #ifdef _DEBUG_GDB_IO_
1042 if ((target
->rtos
!= NULL
) &&
1043 (ERROR_OK
== rtos_get_gdb_reg_list(connection
, ®_list
, ®_list_size
)))
1048 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1050 return gdb_error(connection
, retval
);
1053 for (i
= 0; i
< reg_list_size
; i
++)
1055 reg_packet_size
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1058 assert(reg_packet_size
> 0);
1060 reg_packet
= malloc(reg_packet_size
);
1061 reg_packet_p
= reg_packet
;
1063 for (i
= 0; i
< reg_list_size
; i
++)
1065 if (!reg_list
[i
]->valid
)
1066 reg_list
[i
]->type
->get(reg_list
[i
]);
1067 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1068 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1071 #ifdef _DEBUG_GDB_IO_
1073 char *reg_packet_p_debug
;
1074 reg_packet_p_debug
= strndup(reg_packet
, reg_packet_size
);
1075 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug
);
1076 free(reg_packet_p_debug
);
1080 gdb_put_packet(connection
, reg_packet
, reg_packet_size
);
1088 static int gdb_set_registers_packet(struct connection
*connection
,
1089 char *packet
, int packet_size
)
1091 struct target
*target
= get_target_from_connection(connection
);
1093 struct reg
**reg_list
;
1098 #ifdef _DEBUG_GDB_IO_
1102 /* skip command character */
1106 if (packet_size
% 2)
1108 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1109 return ERROR_SERVER_REMOTE_CLOSED
;
1112 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1114 return gdb_error(connection
, retval
);
1118 for (i
= 0; i
< reg_list_size
; i
++)
1121 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1123 if (packet_p
+ chars
> packet
+ packet_size
)
1125 LOG_ERROR("BUG: register packet is too small for registers");
1128 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1129 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1131 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1133 /* advance packet pointer */
1140 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1143 gdb_put_packet(connection
, "OK", 2);
1148 static int gdb_get_register_packet(struct connection
*connection
,
1149 char *packet
, int packet_size
)
1151 struct target
*target
= get_target_from_connection(connection
);
1153 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1154 struct reg
**reg_list
;
1158 #ifdef _DEBUG_GDB_IO_
1162 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1164 return gdb_error(connection
, retval
);
1167 if (reg_list_size
<= reg_num
)
1169 LOG_ERROR("gdb requested a non-existing register");
1173 if (!reg_list
[reg_num
]->valid
)
1174 reg_list
[reg_num
]->type
->get(reg_list
[reg_num
]);
1176 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1178 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1180 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1188 static int gdb_set_register_packet(struct connection
*connection
,
1189 char *packet
, int packet_size
)
1191 struct target
*target
= get_target_from_connection(connection
);
1194 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1195 struct reg
**reg_list
;
1201 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1203 return gdb_error(connection
, retval
);
1206 if (reg_list_size
< reg_num
)
1208 LOG_ERROR("gdb requested a non-existing register");
1209 return ERROR_SERVER_REMOTE_CLOSED
;
1212 if (*separator
!= '=')
1214 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1215 return ERROR_SERVER_REMOTE_CLOSED
;
1218 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1219 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1220 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1222 /* fix!!! add some sanity checks on packet size here */
1224 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1226 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1228 gdb_put_packet(connection
, "OK", 2);
1236 /* No attempt is made to translate the "retval" to
1237 * GDB speak. This has to be done at the calling
1238 * site as no mapping really exists.
1240 static int gdb_error(struct connection
*connection
, int retval
)
1242 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1243 gdb_send_error(connection
, EFAULT
);
1247 /* We don't have to worry about the default 2 second timeout for GDB packets,
1248 * because GDB breaks up large memory reads into smaller reads.
1250 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1252 static int gdb_read_memory_packet(struct connection
*connection
,
1253 char *packet
, int packet_size
)
1255 struct target
*target
= get_target_from_connection(connection
);
1263 int retval
= ERROR_OK
;
1265 /* skip command character */
1268 addr
= strtoul(packet
, &separator
, 16);
1270 if (*separator
!= ',')
1272 LOG_ERROR("incomplete read memory packet received, dropping connection");
1273 return ERROR_SERVER_REMOTE_CLOSED
;
1276 len
= strtoul(separator
+ 1, NULL
, 16);
1278 buffer
= malloc(len
);
1280 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1282 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1284 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1286 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1287 * At some point this might be fixed in GDB, in which case this code can be removed.
1289 * OpenOCD developers are acutely aware of this problem, but there is nothing
1290 * gained by involving the user in this problem that hopefully will get resolved
1293 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1295 * For now, the default is to fix up things to make current GDB versions work.
1296 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1298 memset(buffer
, 0, len
);
1302 if (retval
== ERROR_OK
)
1304 hex_buffer
= malloc(len
* 2 + 1);
1307 for (i
= 0; i
< len
; i
++)
1309 uint8_t t
= buffer
[i
];
1310 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1311 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1314 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1320 retval
= gdb_error(connection
, retval
);
1328 static int gdb_write_memory_packet(struct connection
*connection
,
1329 char *packet
, int packet_size
)
1331 struct target
*target
= get_target_from_connection(connection
);
1341 /* skip command character */
1344 addr
= strtoul(packet
, &separator
, 16);
1346 if (*separator
!= ',')
1348 LOG_ERROR("incomplete write memory packet received, dropping connection");
1349 return ERROR_SERVER_REMOTE_CLOSED
;
1352 len
= strtoul(separator
+ 1, &separator
, 16);
1354 if (*(separator
++) != ':')
1356 LOG_ERROR("incomplete write memory packet received, dropping connection");
1357 return ERROR_SERVER_REMOTE_CLOSED
;
1360 buffer
= malloc(len
);
1362 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1364 for (i
= 0; i
< len
; i
++)
1367 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1371 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1373 if (retval
== ERROR_OK
)
1375 gdb_put_packet(connection
, "OK", 2);
1379 retval
= gdb_error(connection
, retval
);
1387 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1388 char *packet
, int packet_size
)
1390 struct target
*target
= get_target_from_connection(connection
);
1395 int retval
= ERROR_OK
;
1397 /* skip command character */
1400 addr
= strtoul(packet
, &separator
, 16);
1402 if (*separator
!= ',')
1404 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1405 return ERROR_SERVER_REMOTE_CLOSED
;
1408 len
= strtoul(separator
+ 1, &separator
, 16);
1410 if (*(separator
++) != ':')
1412 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1413 return ERROR_SERVER_REMOTE_CLOSED
;
1416 struct gdb_connection
*gdb_connection
= connection
->priv
;
1418 if (gdb_connection
->mem_write_error
)
1420 retval
= ERROR_FAIL
;
1421 /* now that we have reported the memory write error, we can clear the condition */
1422 gdb_connection
->mem_write_error
= false;
1425 /* By replying the packet *immediately* GDB will send us a new packet
1426 * while we write the last one to the target.
1428 if (retval
== ERROR_OK
)
1430 gdb_put_packet(connection
, "OK", 2);
1434 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1440 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1442 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1443 if (retval
!= ERROR_OK
)
1445 gdb_connection
->mem_write_error
= true;
1452 static int gdb_step_continue_packet(struct connection
*connection
,
1453 char *packet
, int packet_size
)
1455 struct target
*target
= get_target_from_connection(connection
);
1457 uint32_t address
= 0x0;
1458 int retval
= ERROR_OK
;
1462 if (packet_size
> 1)
1464 packet
[packet_size
] = 0;
1465 address
= strtoul(packet
+ 1, NULL
, 16);
1472 if (packet
[0] == 'c')
1474 LOG_DEBUG("continue");
1475 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1476 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1478 else if (packet
[0] == 's')
1481 /* step at current or address, don't handle breakpoints */
1482 retval
= target_step(target
, current
, address
, 0);
1487 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1488 char *packet
, int packet_size
)
1490 struct target
*target
= get_target_from_connection(connection
);
1492 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1493 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1501 type
= strtoul(packet
+ 1, &separator
, 16);
1503 if (type
== 0) /* memory breakpoint */
1504 bp_type
= BKPT_SOFT
;
1505 else if (type
== 1) /* hardware breakpoint */
1506 bp_type
= BKPT_HARD
;
1507 else if (type
== 2) /* write watchpoint */
1508 wp_type
= WPT_WRITE
;
1509 else if (type
== 3) /* read watchpoint */
1511 else if (type
== 4) /* access watchpoint */
1512 wp_type
= WPT_ACCESS
;
1515 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1516 return ERROR_SERVER_REMOTE_CLOSED
;
1520 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1522 bp_type
= gdb_breakpoint_override_type
;
1525 if (*separator
!= ',')
1527 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1528 return ERROR_SERVER_REMOTE_CLOSED
;
1531 address
= strtoul(separator
+ 1, &separator
, 16);
1533 if (*separator
!= ',')
1535 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1536 return ERROR_SERVER_REMOTE_CLOSED
;
1539 size
= strtoul(separator
+ 1, &separator
, 16);
1545 if (packet
[0] == 'Z')
1547 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1549 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1554 gdb_put_packet(connection
, "OK", 2);
1559 breakpoint_remove(target
, address
);
1560 gdb_put_packet(connection
, "OK", 2);
1567 if (packet
[0] == 'Z')
1569 if ((retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
)) != ERROR_OK
)
1571 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1576 gdb_put_packet(connection
, "OK", 2);
1581 watchpoint_remove(target
, address
);
1582 gdb_put_packet(connection
, "OK", 2);
1593 /* print out a string and allocate more space as needed,
1594 * mainly used for XML at this point
1596 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1597 const char *fmt
, ...)
1599 if (*retval
!= ERROR_OK
)
1607 if ((*xml
== NULL
) || (!first
))
1609 /* start by 0 to exercise all the code paths.
1610 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1612 *size
= *size
* 2 + 2;
1614 *xml
= realloc(*xml
, *size
);
1619 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1627 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1629 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1634 /* there was just enough or not enough space, allocate more. */
1639 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1643 /* Extract and NUL-terminate the annex. */
1645 while (*buf
&& *buf
!= ':')
1651 /* After the read marker and annex, qXfer looks like a
1652 * traditional 'm' packet. */
1654 *ofs
= strtoul(buf
, &separator
, 16);
1656 if (*separator
!= ',')
1659 *len
= strtoul(separator
+ 1, NULL
, 16);
1664 static int compare_bank (const void * a
, const void * b
)
1666 struct flash_bank
*b1
, *b2
;
1667 b1
=*((struct flash_bank
**)a
);
1668 b2
=*((struct flash_bank
**)b
);
1670 if (b1
->base
== b2
->base
)
1673 } else if (b1
->base
> b2
->base
)
1682 static int gdb_memory_map(struct connection
*connection
,
1683 char *packet
, int packet_size
)
1685 /* We get away with only specifying flash here. Regions that are not
1686 * specified are treated as if we provided no memory map(if not we
1687 * could detect the holes and mark them as RAM).
1688 * Normally we only execute this code once, but no big deal if we
1689 * have to regenerate it a couple of times.
1692 struct target
*target
= get_target_from_connection(connection
);
1693 struct flash_bank
*p
;
1697 int retval
= ERROR_OK
;
1698 struct flash_bank
**banks
;
1702 uint32_t ram_start
= 0;
1704 int target_flash_banks
= 0;
1706 /* skip command character */
1709 offset
= strtoul(packet
, &separator
, 16);
1710 length
= strtoul(separator
+ 1, &separator
, 16);
1712 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1714 /* Sort banks in ascending order. We need to report non-flash
1715 * memory as ram (or rather read/write) by default for GDB, since
1716 * it has no concept of non-cacheable read/write memory (i/o etc).
1718 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1719 * Current versions of GDB assume unlisted addresses are RAM...
1721 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1723 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1724 retval
= get_flash_bank_by_num(i
, &p
);
1725 if (retval
!= ERROR_OK
)
1728 gdb_error(connection
, retval
);
1731 if(p
->target
== target
)
1732 banks
[target_flash_banks
++] = p
;
1735 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1738 for (i
= 0; i
< target_flash_banks
; i
++) {
1740 unsigned sector_size
= 0;
1746 if (ram_start
< p
->base
)
1747 xml_printf(&retval
, &xml
, &pos
, &size
,
1748 "<memory type=\"ram\" start=\"0x%x\" "
1749 "length=\"0x%x\"/>\n",
1750 ram_start
, p
->base
- ram_start
);
1752 /* Report adjacent groups of same-size sectors. So for
1753 * example top boot CFI flash will list an initial region
1754 * with several large sectors (maybe 128KB) and several
1755 * smaller ones at the end (maybe 32KB). STR7 will have
1756 * regions with 8KB, 32KB, and 64KB sectors; etc.
1758 for (j
= 0; j
< p
->num_sectors
; j
++) {
1761 /* Maybe start a new group of sectors. */
1762 if (sector_size
== 0) {
1763 start
= p
->base
+ p
->sectors
[j
].offset
;
1764 xml_printf(&retval
, &xml
, &pos
, &size
,
1765 "<memory type=\"flash\" "
1768 sector_size
= p
->sectors
[j
].size
;
1771 /* Does this finish a group of sectors?
1772 * If not, continue an already-started group.
1774 if (j
== p
->num_sectors
-1)
1775 group_len
= (p
->base
+ p
->size
) - start
;
1776 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1777 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1782 xml_printf(&retval
, &xml
, &pos
, &size
,
1783 "length=\"0x%x\">\n"
1784 "<property name=\"blocksize\">"
1792 ram_start
= p
->base
+ p
->size
;
1796 xml_printf(&retval
, &xml
, &pos
, &size
,
1797 "<memory type=\"ram\" start=\"0x%x\" "
1798 "length=\"0x%x\"/>\n",
1799 ram_start
, 0-ram_start
);
1800 /* ELSE a flash chip could be at the very end of the 32 bit address
1801 * space, in which case ram_start will be precisely 0
1807 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1809 if (retval
!= ERROR_OK
) {
1810 gdb_error(connection
, retval
);
1814 if (offset
+ length
> pos
)
1815 length
= pos
- offset
;
1817 char *t
= malloc(length
+ 1);
1819 memcpy(t
+ 1, xml
+ offset
, length
);
1820 gdb_put_packet(connection
, t
, length
+ 1);
1827 static int gdb_query_packet(struct connection
*connection
,
1828 char *packet
, int packet_size
)
1830 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1831 struct gdb_connection
*gdb_connection
= connection
->priv
;
1832 struct target
*target
= get_target_from_connection(connection
);
1834 if (strstr(packet
, "qRcmd,"))
1836 if (packet_size
> 6)
1840 cmd
= malloc((packet_size
- 6)/2 + 1);
1841 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1844 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1847 cmd
[(packet_size
- 6)/2] = 0x0;
1849 /* We want to print all debug output to GDB connection */
1850 log_add_callback(gdb_log_callback
, connection
);
1851 target_call_timer_callbacks_now();
1852 /* some commands need to know the GDB connection, make note of current
1853 * GDB connection. */
1854 current_gdb_connection
= gdb_connection
;
1855 command_run_line(cmd_ctx
, cmd
);
1856 current_gdb_connection
= NULL
;
1857 target_call_timer_callbacks_now();
1858 log_remove_callback(gdb_log_callback
, connection
);
1861 gdb_put_packet(connection
, "OK", 2);
1864 else if (strstr(packet
, "qCRC:"))
1866 if (packet_size
> 5)
1875 /* skip command character */
1878 addr
= strtoul(packet
, &separator
, 16);
1880 if (*separator
!= ',')
1882 LOG_ERROR("incomplete read memory packet received, dropping connection");
1883 return ERROR_SERVER_REMOTE_CLOSED
;
1886 len
= strtoul(separator
+ 1, NULL
, 16);
1888 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1890 if (retval
== ERROR_OK
)
1892 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1893 gdb_put_packet(connection
, gdb_reply
, 9);
1897 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1904 else if (strstr(packet
, "qSupported"))
1906 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1907 * disable qXfer:features:read for the moment */
1908 int retval
= ERROR_OK
;
1909 char *buffer
= NULL
;
1913 xml_printf(&retval
, &buffer
, &pos
, &size
,
1914 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1915 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1917 if (retval
!= ERROR_OK
)
1919 gdb_send_error(connection
, 01);
1923 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1928 else if (strstr(packet
, "qXfer:memory-map:read::")
1929 && (flash_get_bank_count() > 0))
1930 return gdb_memory_map(connection
, packet
, packet_size
);
1931 else if (strstr(packet
, "qXfer:features:read:"))
1936 int retval
= ERROR_OK
;
1939 unsigned int length
;
1942 /* skip command character */
1945 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1947 gdb_send_error(connection
, 01);
1951 if (strcmp(annex
, "target.xml") != 0)
1953 gdb_send_error(connection
, 01);
1957 xml_printf(&retval
, &xml
, &pos
, &size
, \
1958 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1960 if (retval
!= ERROR_OK
)
1962 gdb_error(connection
, retval
);
1966 gdb_put_packet(connection
, xml
, strlen(xml
));
1971 else if (strstr(packet
, "QStartNoAckMode"))
1973 gdb_connection
->noack_mode
= 1;
1974 gdb_put_packet(connection
, "OK", 2);
1978 gdb_put_packet(connection
, "", 0);
1982 static int gdb_v_packet(struct connection
*connection
,
1983 char *packet
, int packet_size
)
1985 struct gdb_connection
*gdb_connection
= connection
->priv
;
1986 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1989 /* if flash programming disabled - send a empty reply */
1991 if (gdb_flash_program
== 0)
1993 gdb_put_packet(connection
, "", 0);
1997 if (strstr(packet
, "vFlashErase:"))
2000 unsigned long length
;
2002 char *parse
= packet
+ 12;
2005 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2006 return ERROR_SERVER_REMOTE_CLOSED
;
2009 addr
= strtoul(parse
, &parse
, 16);
2011 if (*(parse
++) != ',' || *parse
== '\0')
2013 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2014 return ERROR_SERVER_REMOTE_CLOSED
;
2017 length
= strtoul(parse
, &parse
, 16);
2021 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2022 return ERROR_SERVER_REMOTE_CLOSED
;
2025 /* assume all sectors need erasing - stops any problems
2026 * when flash_write is called multiple times */
2029 /* perform any target specific operations before the erase */
2030 target_call_event_callbacks(gdb_service
->target
,
2031 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2033 /* vFlashErase:addr,length messages require region start and
2034 * end to be "block" aligned ... if padding is ever needed,
2035 * GDB will have become dangerously confused.
2037 result
= flash_erase_address_range(gdb_service
->target
,
2038 false, addr
, length
);
2040 /* perform any target specific operations after the erase */
2041 target_call_event_callbacks(gdb_service
->target
,
2042 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2045 if (result
!= ERROR_OK
)
2047 /* GDB doesn't evaluate the actual error number returned,
2048 * treat a failed erase as an I/O error
2050 gdb_send_error(connection
, EIO
);
2051 LOG_ERROR("flash_erase returned %i", result
);
2054 gdb_put_packet(connection
, "OK", 2);
2059 if (strstr(packet
, "vFlashWrite:"))
2063 unsigned long length
;
2064 char *parse
= packet
+ 12;
2068 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2069 return ERROR_SERVER_REMOTE_CLOSED
;
2071 addr
= strtoul(parse
, &parse
, 16);
2072 if (*(parse
++) != ':')
2074 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2075 return ERROR_SERVER_REMOTE_CLOSED
;
2077 length
= packet_size
- (parse
- packet
);
2079 /* create a new image if there isn't already one */
2080 if (gdb_connection
->vflash_image
== NULL
)
2082 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2083 image_open(gdb_connection
->vflash_image
, "", "build");
2086 /* create new section with content from packet buffer */
2087 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
2092 gdb_put_packet(connection
, "OK", 2);
2097 if (!strcmp(packet
, "vFlashDone"))
2101 /* process the flashing buffer. No need to erase as GDB
2102 * always issues a vFlashErase first. */
2103 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
2104 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2105 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2106 if (result
!= ERROR_OK
)
2108 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2109 gdb_put_packet(connection
, "E.memtype", 9);
2111 gdb_send_error(connection
, EIO
);
2115 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2116 gdb_put_packet(connection
, "OK", 2);
2119 image_close(gdb_connection
->vflash_image
);
2120 free(gdb_connection
->vflash_image
);
2121 gdb_connection
->vflash_image
= NULL
;
2126 gdb_put_packet(connection
, "", 0);
2130 static int gdb_detach(struct connection
*connection
)
2132 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2134 target_call_event_callbacks(gdb_service
->target
,
2135 TARGET_EVENT_GDB_DETACH
);
2137 return gdb_put_packet(connection
, "OK", 2);
2140 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2141 const char *function
, const char *string
)
2143 struct connection
*connection
= priv
;
2144 struct gdb_connection
*gdb_con
= connection
->priv
;
2148 /* do not reply this using the O packet */
2152 gdb_output_con(connection
, string
);
2155 static void gdb_sig_halted(struct connection
*connection
)
2158 snprintf(sig_reply
, 4, "T%2.2x", 2);
2159 gdb_put_packet(connection
, sig_reply
, 3);
2163 static int gdb_input_inner(struct connection
*connection
)
2165 /* Do not allocate this on the stack */
2166 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2168 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2169 struct target
*target
= gdb_service
->target
;
2170 char *packet
= gdb_packet_buffer
;
2173 struct gdb_connection
*gdb_con
= connection
->priv
;
2174 static int extended_protocol
= 0;
2176 /* drain input buffer. If one of the packets fail, then an error
2177 * packet is replied, if applicable.
2179 * This loop will terminate and the error code is returned.
2181 * The calling fn will check if this error is something that
2182 * can be recovered from, or if the connection must be closed.
2184 * If the error is recoverable, this fn is called again to
2185 * drain the rest of the buffer.
2189 packet_size
= GDB_BUFFER_SIZE
-1;
2190 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2191 if (retval
!= ERROR_OK
)
2194 /* terminate with zero */
2195 packet
[packet_size
] = 0;
2197 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2198 if (packet
[0] == 'X') {
2199 // binary packets spew junk into the debug log stream
2202 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2206 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2208 LOG_DEBUG("received packet: '%s'", packet
);
2212 if (packet_size
> 0)
2217 case 'T': // Is thread alive?
2218 gdb_thread_packet(connection
, packet
, packet_size
);
2220 case 'H': // Set current thread ( 'c' for step and continue, 'g' for all other operations )
2221 gdb_thread_packet(connection
, packet
, packet_size
);
2225 retval
= gdb_thread_packet(connection
, packet
, packet_size
);
2226 if ( retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2228 retval
= gdb_query_packet(connection
, packet
, packet_size
);
2232 retval
= gdb_get_registers_packet(connection
, packet
, packet_size
);
2235 retval
= gdb_set_registers_packet(connection
, packet
, packet_size
);
2238 retval
= gdb_get_register_packet(connection
, packet
, packet_size
);
2241 retval
= gdb_set_register_packet(connection
, packet
, packet_size
);
2244 retval
= gdb_read_memory_packet(connection
, packet
, packet_size
);
2247 retval
= gdb_write_memory_packet(connection
, packet
, packet_size
);
2251 retval
= gdb_breakpoint_watchpoint_packet(connection
, packet
, packet_size
);
2254 gdb_last_signal_packet(connection
, packet
, packet_size
);
2259 log_add_callback(gdb_log_callback
, connection
);
2261 if (gdb_con
->mem_write_error
)
2263 LOG_ERROR("Memory write failure!");
2265 /* now that we have reported the memory write error, we can clear the condition */
2266 gdb_con
->mem_write_error
= false;
2269 bool nostep
= false;
2270 bool already_running
= false;
2271 if (target
->state
== TARGET_RUNNING
)
2273 LOG_WARNING("WARNING! The target is already running. "
2274 "All changes GDB did to registers will be discarded! "
2275 "Waiting for target to halt.");
2276 already_running
= true;
2277 } else if (target
->state
!= TARGET_HALTED
)
2279 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2281 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2283 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2284 * sent by GDB first to OpenOCD, thus defeating the check to
2285 * make only the single stepping have the sync feature...
2288 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2290 gdb_con
->sync
= false;
2292 if (!already_running
&& nostep
)
2294 /* Either the target isn't in the halted state, then we can't
2295 * step/continue. This might be early setup, etc.
2297 * Or we want to allow GDB to pick up a fresh set of
2298 * register values without modifying the target state.
2301 gdb_sig_halted(connection
);
2303 /* stop forwarding log packets! */
2304 log_remove_callback(gdb_log_callback
, connection
);
2307 /* We're running/stepping, in which case we can
2308 * forward log output until the target is halted
2310 gdb_con
->frontend_state
= TARGET_RUNNING
;
2311 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2313 if (!already_running
)
2315 /* Here we don't want packet processing to stop even if this fails,
2316 * so we use a local variable instead of retval. */
2317 retval
= gdb_step_continue_packet(connection
, packet
, packet_size
);
2318 if (retval
!= ERROR_OK
)
2320 /* we'll never receive a halted condition... issue a false one.. */
2321 gdb_frontend_halted(target
, connection
);
2328 retval
= gdb_v_packet(connection
, packet
, packet_size
);
2331 retval
= gdb_detach(connection
);
2332 extended_protocol
= 0;
2335 retval
= gdb_write_memory_binary_packet(connection
, packet
, packet_size
);
2336 if (retval
!= ERROR_OK
)
2340 if (extended_protocol
!= 0)
2342 gdb_put_packet(connection
, "OK", 2);
2343 return ERROR_SERVER_REMOTE_CLOSED
;
2345 /* handle extended remote protocol */
2346 extended_protocol
= 1;
2347 gdb_put_packet(connection
, "OK", 2);
2350 /* handle extended restart packet */
2351 breakpoint_clear_target(gdb_service
->target
);
2352 watchpoint_clear_target(gdb_service
->target
);
2353 command_run_linef(connection
->cmd_ctx
,
2354 "ocd_gdb_restart %s",
2355 target_name(target
));
2356 gdb_put_packet(connection
, "OK", 2);
2360 /* packet supported only by smp target i.e cortex_a.c*/
2361 /* handle smp packet replying coreid played to gbd */
2362 gdb_read_smp_packet(connection
, packet
, packet_size
);
2366 /* packet supported only by smp target i.e cortex_a.c */
2367 /* handle smp packet setting coreid to be played at next
2369 gdb_write_smp_packet(connection
, packet
, packet_size
);
2373 /* ignore unknown packets */
2374 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2375 gdb_put_packet(connection
, NULL
, 0);
2379 /* if a packet handler returned an error, exit input loop */
2380 if (retval
!= ERROR_OK
)
2384 if (gdb_con
->ctrl_c
)
2386 if (target
->state
== TARGET_RUNNING
)
2388 retval
= target_halt(target
);
2389 if (retval
!= ERROR_OK
)
2391 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2393 gdb_con
->ctrl_c
= 0;
2396 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2397 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2401 } while (gdb_con
->buf_cnt
> 0);
2406 static int gdb_input(struct connection
*connection
)
2408 int retval
= gdb_input_inner(connection
);
2409 struct gdb_connection
*gdb_con
= connection
->priv
;
2410 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2413 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2414 if (gdb_con
->closed
)
2415 return ERROR_SERVER_REMOTE_CLOSED
;
2417 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2421 static int gdb_target_start(struct target
*target
, const char *port
)
2424 struct gdb_service
*gdb_service
;
2426 gdb_service
= malloc(sizeof(struct gdb_service
));
2428 if (NULL
== gdb_service
)
2431 gdb_service
->target
= target
;
2432 gdb_service
->core
[0] = -1;
2433 gdb_service
->core
[1] = -1;
2434 target
->gdb_service
= gdb_service
;
2436 ret
= add_service("gdb",
2437 port
, 1, &gdb_new_connection
, &gdb_input
,
2438 &gdb_connection_closed
, gdb_service
);
2439 /* initialialize all targets gdb service with the same pointer */
2441 struct target_list
*head
;
2442 struct target
*curr
;
2443 head
= target
->head
;
2444 while(head
!= (struct target_list
*)NULL
)
2446 curr
= head
->target
;
2447 if (curr
!= target
) curr
->gdb_service
= gdb_service
;
2454 static int gdb_target_add_one(struct target
*target
)
2456 /* one gdb instance per smp list */
2457 if ((target
->smp
) && (target
->gdb_service
)) return ERROR_OK
;
2458 int retval
= gdb_target_start(target
, gdb_port_next
);
2459 if (retval
== ERROR_OK
)
2462 /* If we can parse the port number
2463 * then we increment the port number for the next target.
2466 portnumber
= strtol(gdb_port_next
, &end
, 0);
2469 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
)
2471 free((void *)gdb_port_next
);
2472 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2479 int gdb_target_add_all(struct target
*target
)
2483 LOG_WARNING("gdb services need one or more targets defined");
2487 while (NULL
!= target
)
2489 int retval
= gdb_target_add_one(target
);
2490 if (ERROR_OK
!= retval
)
2493 target
= target
->next
;
2499 COMMAND_HANDLER(handle_gdb_sync_command
)
2503 return ERROR_COMMAND_SYNTAX_ERROR
;
2506 if (current_gdb_connection
== NULL
)
2508 command_print(CMD_CTX
,
2509 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2513 current_gdb_connection
->sync
= true;
2518 /* daemon configuration command gdb_port */
2519 COMMAND_HANDLER(handle_gdb_port_command
)
2521 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2522 if (ERROR_OK
== retval
) {
2523 free((void*)gdb_port_next
);
2524 gdb_port_next
= strdup(gdb_port
);
2529 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2532 return ERROR_COMMAND_SYNTAX_ERROR
;
2534 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2538 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2541 return ERROR_COMMAND_SYNTAX_ERROR
;
2543 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2547 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2550 return ERROR_COMMAND_SYNTAX_ERROR
;
2552 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2556 /* gdb_breakpoint_override */
2557 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2562 } else if (CMD_ARGC
== 1)
2564 gdb_breakpoint_override
= 1;
2565 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2567 gdb_breakpoint_override_type
= BKPT_HARD
;
2568 } else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2570 gdb_breakpoint_override_type
= BKPT_SOFT
;
2571 } else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2573 gdb_breakpoint_override
= 0;
2577 return ERROR_COMMAND_SYNTAX_ERROR
;
2579 if (gdb_breakpoint_override
)
2581 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2584 LOG_USER("breakpoint type is not overridden");
2590 static const struct command_registration gdb_command_handlers
[] = {
2593 .handler
= handle_gdb_sync_command
,
2594 .mode
= COMMAND_ANY
,
2595 .help
= "next stepi will return immediately allowing "
2596 "GDB to fetch register state without affecting "
2601 .handler
= handle_gdb_port_command
,
2602 .mode
= COMMAND_ANY
,
2603 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2604 "server listens for the next port number after the "
2605 "base port number specified. "
2606 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2607 "output to stdout, an integer is base port number, \"disable\" disables "
2608 "port. Any other string is are interpreted as named pipe to listen to. "
2609 "Output pipe is the same name as input pipe, but with 'o' appended.",
2610 .usage
= "[port_num]",
2613 .name
= "gdb_memory_map",
2614 .handler
= handle_gdb_memory_map_command
,
2615 .mode
= COMMAND_CONFIG
,
2616 .help
= "enable or disable memory map",
2617 .usage
= "('enable'|'disable')"
2620 .name
= "gdb_flash_program",
2621 .handler
= handle_gdb_flash_program_command
,
2622 .mode
= COMMAND_CONFIG
,
2623 .help
= "enable or disable flash program",
2624 .usage
= "('enable'|'disable')"
2627 .name
= "gdb_report_data_abort",
2628 .handler
= handle_gdb_report_data_abort_command
,
2629 .mode
= COMMAND_CONFIG
,
2630 .help
= "enable or disable reporting data aborts",
2631 .usage
= "('enable'|'disable')"
2634 .name
= "gdb_breakpoint_override",
2635 .handler
= handle_gdb_breakpoint_override_command
,
2636 .mode
= COMMAND_ANY
,
2637 .help
= "Display or specify type of breakpoint "
2638 "to be used by gdb 'break' commands.",
2639 .usage
= "('hard'|'soft'|'disable')"
2641 COMMAND_REGISTRATION_DONE
2644 int gdb_register_commands(struct command_context
*cmd_ctx
)
2646 gdb_port
= strdup("3333");
2647 gdb_port_next
= strdup("3333");
2648 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);