2 /***************************************************************************
3 * Copyright (C) 2005 by Dominic Rath *
4 * Dominic.Rath@gmx.de *
6 * Copyright (C) 2007-2010 Øyvind Harboe *
7 * oyvind.harboe@zylin.com *
9 * Copyright (C) 2008 by Spencer Oliver *
10 * spen@spen-soft.co.uk *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the *
24 * Free Software Foundation, Inc., *
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
26 ***************************************************************************/
31 #include <target/breakpoints.h>
32 #include <target/target_request.h>
33 #include <target/register.h>
35 #include <flash/nor/core.h>
36 #include "gdb_server.h"
37 #include <target/image.h>
38 #include <jtag/jtag.h>
43 * GDB server implementation.
45 * This implements the GDB Remote Serial Protocol, over TCP connections,
46 * giving GDB access to the JTAG or other hardware debugging facilities
47 * found in most modern embedded processors.
50 /* private connection data for GDB */
53 char buffer
[GDB_BUFFER_SIZE
];
57 enum target_state frontend_state
;
58 struct image
*vflash_image
;
62 bool sync
; /* set flag to true if you want the next stepi to return immediately.
63 allowing GDB to pick up a fresh set of register values from the target
64 without modifying the target state. */
65 /* We delay reporting memory write errors until next step/continue or memory
66 * write. This improves performance of gdb load significantly as the GDB packet
67 * can be replied immediately and a new GDB packet will be ready without delay
75 #define _DEBUG_GDB_IO_
78 static struct gdb_connection
*current_gdb_connection
;
80 static int gdb_breakpoint_override
;
81 static enum breakpoint_type gdb_breakpoint_override_type
;
83 static int gdb_error(struct connection
*connection
, int retval
);
84 static unsigned short gdb_port
= 3333;
85 static unsigned short gdb_port_next
= 0;
86 static const char DIGITS
[16] = "0123456789abcdef";
88 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
89 const char *function
, const char *string
);
91 /* number of gdb connections, mainly to suppress gdb related debugging spam
92 * in helper/log.c when no gdb connections are actually active */
93 int gdb_actual_connections
;
95 /* set if we are sending a memory map to gdb
96 * via qXfer:memory-map:read packet */
97 /* enabled by default*/
98 static int gdb_use_memory_map
= 1;
99 /* enabled by default*/
100 static int gdb_flash_program
= 1;
102 /* if set, data aborts cause an error to be reported in memory read packets
103 * see the code in gdb_read_memory_packet() for further explanations.
104 * Disabled by default.
106 static int gdb_report_data_abort
;
108 static int gdb_last_signal(struct target
*target
)
110 switch (target
->debug_reason
)
112 case DBG_REASON_DBGRQ
:
113 return 0x2; /* SIGINT */
114 case DBG_REASON_BREAKPOINT
:
115 case DBG_REASON_WATCHPOINT
:
116 case DBG_REASON_WPTANDBKPT
:
117 return 0x05; /* SIGTRAP */
118 case DBG_REASON_SINGLESTEP
:
119 return 0x05; /* SIGTRAP */
120 case DBG_REASON_NOTHALTED
:
121 return 0x0; /* no signal... shouldn't happen */
123 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
128 static int check_pending(struct connection
*connection
,
129 int timeout_s
, int *got_data
)
131 /* a non-blocking socket will block if there is 0 bytes available on the socket,
132 * but return with as many bytes as are available immediately
136 struct gdb_connection
*gdb_con
= connection
->priv
;
138 if (got_data
== NULL
)
142 if (gdb_con
->buf_cnt
> 0)
149 FD_SET(connection
->fd
, &read_fds
);
151 tv
.tv_sec
= timeout_s
;
153 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
155 /* This can typically be because a "monitor" command took too long
156 * before printing any progress messages
160 return ERROR_GDB_TIMEOUT
;
166 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
170 static int gdb_get_char_inner(struct connection
*connection
, int* next_char
)
172 struct gdb_connection
*gdb_con
= connection
->priv
;
173 int retval
= ERROR_OK
;
175 #ifdef _DEBUG_GDB_IO_
180 if (connection
->service
->type
== CONNECTION_PIPE
)
182 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
186 retval
= check_pending(connection
, 1, NULL
);
187 if (retval
!= ERROR_OK
)
189 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
192 if (gdb_con
->buf_cnt
> 0)
196 if (gdb_con
->buf_cnt
== 0)
199 return ERROR_SERVER_REMOTE_CLOSED
;
203 errno
= WSAGetLastError();
210 case WSAECONNABORTED
:
212 return ERROR_SERVER_REMOTE_CLOSED
;
215 return ERROR_SERVER_REMOTE_CLOSED
;
217 LOG_ERROR("read: %d", errno
);
228 return ERROR_SERVER_REMOTE_CLOSED
;
231 return ERROR_SERVER_REMOTE_CLOSED
;
233 LOG_ERROR("read: %s", strerror(errno
));
235 return ERROR_SERVER_REMOTE_CLOSED
;
240 #ifdef _DEBUG_GDB_IO_
241 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
242 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
243 debug_buffer
[gdb_con
->buf_cnt
] = 0;
244 LOG_DEBUG("received '%s'", debug_buffer
);
248 gdb_con
->buf_p
= gdb_con
->buffer
;
250 *next_char
= *(gdb_con
->buf_p
++);
251 if (gdb_con
->buf_cnt
> 0)
252 connection
->input_pending
= 1;
254 connection
->input_pending
= 0;
255 #ifdef _DEBUG_GDB_IO_
256 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
263 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
264 * held in registers in the inner loop.
266 * For small caches and embedded systems this is important!
268 static inline int gdb_get_char_fast(struct connection
*connection
, int* next_char
, char **buf_p
, int *buf_cnt
)
270 int retval
= ERROR_OK
;
272 if ((*buf_cnt
)-- > 0)
274 *next_char
= **buf_p
;
277 connection
->input_pending
= 1;
279 connection
->input_pending
= 0;
281 #ifdef _DEBUG_GDB_IO_
282 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
288 struct gdb_connection
*gdb_con
= connection
->priv
;
289 gdb_con
->buf_p
= *buf_p
;
290 gdb_con
->buf_cnt
= *buf_cnt
;
291 retval
= gdb_get_char_inner(connection
, next_char
);
292 *buf_p
= gdb_con
->buf_p
;
293 *buf_cnt
= gdb_con
->buf_cnt
;
299 static int gdb_get_char(struct connection
*connection
, int* next_char
)
301 struct gdb_connection
*gdb_con
= connection
->priv
;
302 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
306 static int gdb_putback_char(struct connection
*connection
, int last_char
)
308 struct gdb_connection
*gdb_con
= connection
->priv
;
310 if (gdb_con
->buf_p
> gdb_con
->buffer
)
312 *(--gdb_con
->buf_p
) = last_char
;
317 LOG_ERROR("BUG: couldn't put character back");
323 /* The only way we can detect that the socket is closed is the first time
324 * we write to it, we will fail. Subsequent write operations will
325 * succeed. Shudder! */
326 static int gdb_write(struct connection
*connection
, void *data
, int len
)
328 struct gdb_connection
*gdb_con
= connection
->priv
;
330 return ERROR_SERVER_REMOTE_CLOSED
;
332 if (connection
->service
->type
== CONNECTION_PIPE
)
334 /* write to stdout */
335 if (write(STDOUT_FILENO
, data
, len
) == len
)
342 if (write_socket(connection
->fd
, data
, len
) == len
)
348 return ERROR_SERVER_REMOTE_CLOSED
;
351 static int gdb_put_packet_inner(struct connection
*connection
,
352 char *buffer
, int len
)
355 unsigned char my_checksum
= 0;
356 #ifdef _DEBUG_GDB_IO_
361 struct gdb_connection
*gdb_con
= connection
->priv
;
363 for (i
= 0; i
< len
; i
++)
364 my_checksum
+= buffer
[i
];
366 #ifdef _DEBUG_GDB_IO_
368 * At this point we should have nothing in the input queue from GDB,
369 * however sometimes '-' is sent even though we've already received
370 * an ACK (+) for everything we've sent off.
375 retval
= check_pending(connection
, 0, &gotdata
);
376 if (retval
!= ERROR_OK
)
380 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
383 /* fix a problem with some IAR tools */
384 gdb_putback_char(connection
, reply
);
385 LOG_DEBUG("Unexpected start of new packet");
389 LOG_WARNING("Discard unexpected char %c", reply
);
395 #ifdef _DEBUG_GDB_IO_
396 debug_buffer
= malloc(len
+ 1);
397 memcpy(debug_buffer
, buffer
, len
);
398 debug_buffer
[len
] = 0;
399 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
403 char local_buffer
[1024];
404 local_buffer
[0] = '$';
405 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
407 /* performance gain on smaller packets by only a single call to gdb_write() */
408 memcpy(local_buffer
+ 1, buffer
, len
++);
409 local_buffer
[len
++] = '#';
410 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
411 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
412 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
419 /* larger packets are transmitted directly from caller supplied buffer
420 by several calls to gdb_write() to avoid dynamic allocation */
421 local_buffer
[1] = '#';
422 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
423 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
424 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
428 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
432 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
438 if (gdb_con
->noack_mode
)
441 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
446 else if (reply
== '-')
448 /* Stop sending output packets for now */
449 log_remove_callback(gdb_log_callback
, connection
);
450 LOG_WARNING("negative reply, retrying");
452 else if (reply
== 0x3)
455 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
459 else if (reply
== '-')
461 /* Stop sending output packets for now */
462 log_remove_callback(gdb_log_callback
, connection
);
463 LOG_WARNING("negative reply, retrying");
465 else if (reply
== '$') {
466 LOG_ERROR("GDB missing ack(1) - assumed good");
467 gdb_putback_char(connection
, reply
);
471 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
473 return ERROR_SERVER_REMOTE_CLOSED
;
476 else if (reply
== '$') {
477 LOG_ERROR("GDB missing ack(2) - assumed good");
478 gdb_putback_char(connection
, reply
);
483 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
485 return ERROR_SERVER_REMOTE_CLOSED
;
489 return ERROR_SERVER_REMOTE_CLOSED
;
494 static int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
496 struct gdb_connection
*gdb_con
= connection
->priv
;
498 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
501 /* we sent some data, reset timer for keep alive messages */
507 static __inline__
int fetch_packet(struct connection
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
509 unsigned char my_checksum
= 0;
512 int retval
= ERROR_OK
;
514 struct gdb_connection
*gdb_con
= connection
->priv
;
519 /* move this over into local variables to use registers and give the
520 * more freedom to optimize */
521 char *buf_p
= gdb_con
->buf_p
;
522 int buf_cnt
= gdb_con
->buf_cnt
;
526 /* The common case is that we have an entire packet with no escape chars.
527 * We need to leave at least 2 bytes in the buffer to have
528 * gdb_get_char() update various bits and bobs correctly.
530 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
))
532 /* The compiler will struggle a bit with constant propagation and
533 * aliasing, so we help it by showing that these values do not
534 * change inside the loop
538 int run
= buf_cnt
- 2;
545 if (character
== '#')
547 /* Danger! character can be '#' when esc is
548 * used so we need an explicit boolean for done here.
554 if (character
== '}')
556 /* data transmitted in binary mode (X packet)
557 * uses 0x7d as escape character */
558 my_checksum
+= character
& 0xff;
561 my_checksum
+= character
& 0xff;
562 buffer
[count
++] = (character
^ 0x20) & 0xff;
566 my_checksum
+= character
& 0xff;
567 buffer
[count
++] = character
& 0xff;
577 LOG_ERROR("packet buffer too small");
578 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
582 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
583 if (retval
!= ERROR_OK
)
586 if (character
== '#')
589 if (character
== '}')
591 /* data transmitted in binary mode (X packet)
592 * uses 0x7d as escape character */
593 my_checksum
+= character
& 0xff;
595 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
596 if (retval
!= ERROR_OK
)
599 my_checksum
+= character
& 0xff;
600 buffer
[count
++] = (character
^ 0x20) & 0xff;
604 my_checksum
+= character
& 0xff;
605 buffer
[count
++] = character
& 0xff;
609 gdb_con
->buf_p
= buf_p
;
610 gdb_con
->buf_cnt
= buf_cnt
;
612 if (retval
!= ERROR_OK
)
617 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
619 checksum
[0] = character
;
620 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
622 checksum
[1] = character
;
627 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
633 static int gdb_get_packet_inner(struct connection
*connection
,
634 char *buffer
, int *len
)
638 struct gdb_connection
*gdb_con
= connection
->priv
;
644 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
647 #ifdef _DEBUG_GDB_IO_
648 LOG_DEBUG("character: '%c'", character
);
656 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
657 * in case anyone tries to debug why they receive this warning every time */
658 LOG_WARNING("acknowledgment received, but no packet pending");
661 LOG_WARNING("negative acknowledgment, but no packet pending");
668 LOG_WARNING("ignoring character 0x%x", character
);
671 } while (character
!= '$');
676 /* explicit code expansion here to get faster inlined code in -O3 by not
677 * calculating checksum
679 if (gdb_con
->noack_mode
)
681 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
685 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
689 if (gdb_con
->noack_mode
)
691 /* checksum is not checked in noack mode */
696 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
704 return ERROR_SERVER_REMOTE_CLOSED
;
709 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
711 struct gdb_connection
*gdb_con
= connection
->priv
;
713 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
718 static int gdb_output_con(struct connection
*connection
, const char* line
)
723 bin_size
= strlen(line
);
725 hex_buffer
= malloc(bin_size
*2 + 2);
726 if (hex_buffer
== NULL
)
727 return ERROR_GDB_BUFFER_TOO_SMALL
;
730 for (i
= 0; i
< bin_size
; i
++)
731 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
732 hex_buffer
[bin_size
*2 + 1] = 0;
734 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
740 static int gdb_output(struct command_context
*context
, const char* line
)
742 /* this will be dumped to the log and also sent as an O packet if possible */
743 LOG_USER_N("%s", line
);
748 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
750 struct gdb_connection
*gdb_connection
= connection
->priv
;
752 /* In the GDB protocol when we are stepping or continuing execution,
753 * we have a lingering reply. Upon receiving a halted event
754 * when we have that lingering packet, we reply to the original
755 * step or continue packet.
757 * Executing monitor commands can bring the target in and
758 * out of the running state so we'll see lots of TARGET_EVENT_XXX
759 * that are to be ignored.
761 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
766 /* stop forwarding log packets! */
767 log_remove_callback(gdb_log_callback
, connection
);
769 if (gdb_connection
->ctrl_c
)
772 gdb_connection
->ctrl_c
= 0;
776 signal_var
= gdb_last_signal(target
);
779 snprintf(sig_reply
, 4, "T%2.2x", signal_var
);
780 gdb_put_packet(connection
, sig_reply
, 3);
781 gdb_connection
->frontend_state
= TARGET_HALTED
;
785 static int gdb_target_callback_event_handler(struct target
*target
,
786 enum target_event event
, void *priv
)
789 struct connection
*connection
= priv
;
791 target_handle_event(target
, event
);
794 case TARGET_EVENT_GDB_HALT
:
795 gdb_frontend_halted(target
, connection
);
797 case TARGET_EVENT_HALTED
:
798 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
800 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
801 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
802 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
814 static int gdb_new_connection(struct connection
*connection
)
816 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
817 struct gdb_service
*gdb_service
= connection
->service
->priv
;
821 connection
->priv
= gdb_connection
;
823 /* initialize gdb connection information */
824 gdb_connection
->buf_p
= gdb_connection
->buffer
;
825 gdb_connection
->buf_cnt
= 0;
826 gdb_connection
->ctrl_c
= 0;
827 gdb_connection
->frontend_state
= TARGET_HALTED
;
828 gdb_connection
->vflash_image
= NULL
;
829 gdb_connection
->closed
= 0;
830 gdb_connection
->busy
= 0;
831 gdb_connection
->noack_mode
= 0;
832 gdb_connection
->sync
= true;
833 gdb_connection
->mem_write_error
= false;
835 /* send ACK to GDB for debug request */
836 gdb_write(connection
, "+", 1);
838 /* output goes through gdb connection */
839 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
841 /* we must remove all breakpoints registered to the target as a previous
842 * GDB session could leave dangling breakpoints if e.g. communication
845 breakpoint_clear_target(gdb_service
->target
);
846 watchpoint_clear_target(gdb_service
->target
);
848 /* register callback to be informed about target events */
849 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
851 /* remove the initial ACK from the incoming buffer */
852 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
855 /* FIX!!!??? would we actually ever receive a + here???
858 if (initial_ack
!= '+')
859 gdb_putback_char(connection
, initial_ack
);
860 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
862 if (gdb_use_memory_map
)
864 /* Connect must fail if the memory map can't be set up correctly.
866 * This will cause an auto_probe to be invoked, which is either
867 * a no-op or it will fail when the target isn't ready(e.g. not halted).
870 for (i
= 0; i
< flash_get_bank_count(); i
++)
872 struct flash_bank
*p
;
873 retval
= get_flash_bank_by_num(i
, &p
);
874 if (retval
!= ERROR_OK
)
876 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect.");
882 gdb_actual_connections
++;
883 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
884 gdb_actual_connections
,
885 target_name(gdb_service
->target
),
886 target_state_name(gdb_service
->target
));
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 struct target
*target
, char* packet
, int packet_size
)
951 signal_var
= gdb_last_signal(target
);
953 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
954 gdb_put_packet(connection
, sig_reply
, 3);
959 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
961 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
964 return len
- 1 - pos
;
967 /* Convert register to string of bytes. NB! The # of bits in the
968 * register might be non-divisible by 8(a byte), in which
969 * case an entire byte is shown.
971 * NB! the format on the wire is the target endianness
973 * The format of reg->value is little endian
976 static void gdb_str_to_target(struct target
*target
,
977 char *tstr
, struct reg
*reg
)
984 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
986 for (i
= 0; i
< buf_len
; i
++)
988 int j
= gdb_reg_pos(target
, i
, buf_len
);
989 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
990 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
994 static int hextoint(int c
)
1005 LOG_ERROR("BUG: invalid register value %08x", c
);
1009 /* copy over in register buffer */
1010 static void gdb_target_to_reg(struct target
*target
,
1011 char *tstr
, int str_len
, uint8_t *bin
)
1015 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1020 for (i
= 0; i
< str_len
; i
+= 2)
1022 uint8_t t
= hextoint(tstr
[i
]) << 4;
1023 t
|= hextoint(tstr
[i
+ 1]);
1025 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1030 static int gdb_get_registers_packet(struct connection
*connection
,
1031 struct target
*target
, char* packet
, int packet_size
)
1033 struct reg
**reg_list
;
1036 int reg_packet_size
= 0;
1041 #ifdef _DEBUG_GDB_IO_
1045 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1047 return gdb_error(connection
, retval
);
1050 for (i
= 0; i
< reg_list_size
; i
++)
1052 reg_packet_size
+= reg_list
[i
]->size
;
1055 reg_packet
= malloc(DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1056 reg_packet_p
= reg_packet
;
1058 for (i
= 0; i
< reg_list_size
; i
++)
1060 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1061 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1064 #ifdef _DEBUG_GDB_IO_
1067 reg_packet_p
= strndup(reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1068 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
1073 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1081 static int gdb_set_registers_packet(struct connection
*connection
,
1082 struct target
*target
, char *packet
, int packet_size
)
1085 struct reg
**reg_list
;
1090 #ifdef _DEBUG_GDB_IO_
1094 /* skip command character */
1098 if (packet_size
% 2)
1100 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1101 return ERROR_SERVER_REMOTE_CLOSED
;
1104 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1106 return gdb_error(connection
, retval
);
1110 for (i
= 0; i
< reg_list_size
; i
++)
1113 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1115 if (packet_p
+ chars
> packet
+ packet_size
)
1117 LOG_ERROR("BUG: register packet is too small for registers");
1120 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1121 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1123 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1125 /* advance packet pointer */
1132 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1135 gdb_put_packet(connection
, "OK", 2);
1140 static int gdb_get_register_packet(struct connection
*connection
,
1141 struct target
*target
, char *packet
, int packet_size
)
1144 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1145 struct reg
**reg_list
;
1149 #ifdef _DEBUG_GDB_IO_
1153 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1155 return gdb_error(connection
, retval
);
1158 if (reg_list_size
<= reg_num
)
1160 LOG_ERROR("gdb requested a non-existing register");
1164 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1166 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1168 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1176 static int gdb_set_register_packet(struct connection
*connection
,
1177 struct target
*target
, char *packet
, int packet_size
)
1181 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1182 struct reg
**reg_list
;
1188 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1190 return gdb_error(connection
, retval
);
1193 if (reg_list_size
< reg_num
)
1195 LOG_ERROR("gdb requested a non-existing register");
1196 return ERROR_SERVER_REMOTE_CLOSED
;
1199 if (*separator
!= '=')
1201 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1202 return ERROR_SERVER_REMOTE_CLOSED
;
1205 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1206 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1207 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1209 /* fix!!! add some sanity checks on packet size here */
1211 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1213 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1215 gdb_put_packet(connection
, "OK", 2);
1223 /* No attempt is made to translate the "retval" to
1224 * GDB speak. This has to be done at the calling
1225 * site as no mapping really exists.
1227 static int gdb_error(struct connection
*connection
, int retval
)
1229 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1230 gdb_send_error(connection
, EFAULT
);
1234 /* We don't have to worry about the default 2 second timeout for GDB packets,
1235 * because GDB breaks up large memory reads into smaller reads.
1237 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1239 static int gdb_read_memory_packet(struct connection
*connection
,
1240 struct target
*target
, char *packet
, int packet_size
)
1249 int retval
= ERROR_OK
;
1251 /* skip command character */
1254 addr
= strtoul(packet
, &separator
, 16);
1256 if (*separator
!= ',')
1258 LOG_ERROR("incomplete read memory packet received, dropping connection");
1259 return ERROR_SERVER_REMOTE_CLOSED
;
1262 len
= strtoul(separator
+ 1, NULL
, 16);
1264 buffer
= malloc(len
);
1266 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1268 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1270 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1272 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1273 * At some point this might be fixed in GDB, in which case this code can be removed.
1275 * OpenOCD developers are acutely aware of this problem, but there is nothing
1276 * gained by involving the user in this problem that hopefully will get resolved
1279 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1281 * For now, the default is to fix up things to make current GDB versions work.
1282 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1284 memset(buffer
, 0, len
);
1288 if (retval
== ERROR_OK
)
1290 hex_buffer
= malloc(len
* 2 + 1);
1293 for (i
= 0; i
< len
; i
++)
1295 uint8_t t
= buffer
[i
];
1296 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1297 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1300 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1306 retval
= gdb_error(connection
, retval
);
1314 static int gdb_write_memory_packet(struct connection
*connection
,
1315 struct target
*target
, char *packet
, int packet_size
)
1326 /* skip command character */
1329 addr
= strtoul(packet
, &separator
, 16);
1331 if (*separator
!= ',')
1333 LOG_ERROR("incomplete write memory packet received, dropping connection");
1334 return ERROR_SERVER_REMOTE_CLOSED
;
1337 len
= strtoul(separator
+ 1, &separator
, 16);
1339 if (*(separator
++) != ':')
1341 LOG_ERROR("incomplete write memory packet received, dropping connection");
1342 return ERROR_SERVER_REMOTE_CLOSED
;
1345 buffer
= malloc(len
);
1347 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1349 for (i
= 0; i
< len
; i
++)
1352 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1356 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1358 if (retval
== ERROR_OK
)
1360 gdb_put_packet(connection
, "OK", 2);
1364 retval
= gdb_error(connection
, retval
);
1372 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1373 struct target
*target
, char *packet
, int packet_size
)
1379 int retval
= ERROR_OK
;
1381 /* skip command character */
1384 addr
= strtoul(packet
, &separator
, 16);
1386 if (*separator
!= ',')
1388 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1389 return ERROR_SERVER_REMOTE_CLOSED
;
1392 len
= strtoul(separator
+ 1, &separator
, 16);
1394 if (*(separator
++) != ':')
1396 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1397 return ERROR_SERVER_REMOTE_CLOSED
;
1400 struct gdb_connection
*gdb_connection
= connection
->priv
;
1402 if (gdb_connection
->mem_write_error
)
1404 retval
= ERROR_FAIL
;
1405 /* now that we have reported the memory write error, we can clear the condition */
1406 gdb_connection
->mem_write_error
= false;
1409 /* By replying the packet *immediately* GDB will send us a new packet
1410 * while we write the last one to the target.
1412 if (retval
== ERROR_OK
)
1414 gdb_put_packet(connection
, "OK", 2);
1418 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1424 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1426 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1427 if (retval
!= ERROR_OK
)
1429 gdb_connection
->mem_write_error
= true;
1436 static int gdb_step_continue_packet(struct connection
*connection
,
1437 struct target
*target
, char *packet
, int packet_size
)
1440 uint32_t address
= 0x0;
1441 int retval
= ERROR_OK
;
1445 if (packet_size
> 1)
1447 packet
[packet_size
] = 0;
1448 address
= strtoul(packet
+ 1, NULL
, 16);
1455 if (packet
[0] == 'c')
1457 LOG_DEBUG("continue");
1458 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1459 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1461 else if (packet
[0] == 's')
1464 /* step at current or address, don't handle breakpoints */
1465 retval
= target_step(target
, current
, address
, 0);
1470 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1471 struct target
*target
, char *packet
, int packet_size
)
1474 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1475 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1483 type
= strtoul(packet
+ 1, &separator
, 16);
1485 if (type
== 0) /* memory breakpoint */
1486 bp_type
= BKPT_SOFT
;
1487 else if (type
== 1) /* hardware breakpoint */
1488 bp_type
= BKPT_HARD
;
1489 else if (type
== 2) /* write watchpoint */
1490 wp_type
= WPT_WRITE
;
1491 else if (type
== 3) /* read watchpoint */
1493 else if (type
== 4) /* access watchpoint */
1494 wp_type
= WPT_ACCESS
;
1497 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1498 return ERROR_SERVER_REMOTE_CLOSED
;
1502 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1504 bp_type
= gdb_breakpoint_override_type
;
1507 if (*separator
!= ',')
1509 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1510 return ERROR_SERVER_REMOTE_CLOSED
;
1513 address
= strtoul(separator
+ 1, &separator
, 16);
1515 if (*separator
!= ',')
1517 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1518 return ERROR_SERVER_REMOTE_CLOSED
;
1521 size
= strtoul(separator
+ 1, &separator
, 16);
1527 if (packet
[0] == 'Z')
1529 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1531 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1536 gdb_put_packet(connection
, "OK", 2);
1541 breakpoint_remove(target
, address
);
1542 gdb_put_packet(connection
, "OK", 2);
1549 if (packet
[0] == 'Z')
1551 if ((retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
)) != ERROR_OK
)
1553 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1558 gdb_put_packet(connection
, "OK", 2);
1563 watchpoint_remove(target
, address
);
1564 gdb_put_packet(connection
, "OK", 2);
1575 /* print out a string and allocate more space as needed,
1576 * mainly used for XML at this point
1578 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1579 const char *fmt
, ...)
1581 if (*retval
!= ERROR_OK
)
1589 if ((*xml
== NULL
) || (!first
))
1591 /* start by 0 to exercise all the code paths.
1592 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1594 *size
= *size
* 2 + 2;
1596 *xml
= realloc(*xml
, *size
);
1601 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1609 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1611 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1616 /* there was just enough or not enough space, allocate more. */
1621 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1625 /* Extract and NUL-terminate the annex. */
1627 while (*buf
&& *buf
!= ':')
1633 /* After the read marker and annex, qXfer looks like a
1634 * traditional 'm' packet. */
1636 *ofs
= strtoul(buf
, &separator
, 16);
1638 if (*separator
!= ',')
1641 *len
= strtoul(separator
+ 1, NULL
, 16);
1646 static int compare_bank (const void * a
, const void * b
)
1648 struct flash_bank
*b1
, *b2
;
1649 b1
=*((struct flash_bank
**)a
);
1650 b2
=*((struct flash_bank
**)b
);
1652 if (b1
->base
== b2
->base
)
1655 } else if (b1
->base
> b2
->base
)
1664 static int gdb_memory_map(struct connection
*connection
,
1665 struct target
*target
, char *packet
, int packet_size
)
1667 /* We get away with only specifying flash here. Regions that are not
1668 * specified are treated as if we provided no memory map(if not we
1669 * could detect the holes and mark them as RAM).
1670 * Normally we only execute this code once, but no big deal if we
1671 * have to regenerate it a couple of times.
1674 struct flash_bank
*p
;
1678 int retval
= ERROR_OK
;
1679 struct flash_bank
**banks
;
1683 uint32_t ram_start
= 0;
1685 int target_flash_banks
= 0;
1687 /* skip command character */
1690 offset
= strtoul(packet
, &separator
, 16);
1691 length
= strtoul(separator
+ 1, &separator
, 16);
1693 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1695 /* Sort banks in ascending order. We need to report non-flash
1696 * memory as ram (or rather read/write) by default for GDB, since
1697 * it has no concept of non-cacheable read/write memory (i/o etc).
1699 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1700 * Current versions of GDB assume unlisted addresses are RAM...
1702 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1704 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1705 retval
= get_flash_bank_by_num(i
, &p
);
1706 if (retval
!= ERROR_OK
)
1709 gdb_error(connection
, retval
);
1712 if(p
->target
== target
)
1713 banks
[target_flash_banks
++] = p
;
1716 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1719 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1721 unsigned sector_size
= 0;
1722 uint32_t start
, end
;
1726 end
= p
->base
+ p
->size
;
1728 if (ram_start
< p
->base
)
1729 xml_printf(&retval
, &xml
, &pos
, &size
,
1730 "<memory type=\"ram\" start=\"0x%x\" "
1731 "length=\"0x%x\"/>\n",
1732 ram_start
, p
->base
- ram_start
);
1734 /* Report adjacent groups of same-size sectors. So for
1735 * example top boot CFI flash will list an initial region
1736 * with several large sectors (maybe 128KB) and several
1737 * smaller ones at the end (maybe 32KB). STR7 will have
1738 * regions with 8KB, 32KB, and 64KB sectors; etc.
1740 for (j
= 0; j
< p
->num_sectors
; j
++) {
1743 /* Maybe start a new group of sectors. */
1744 if (sector_size
== 0) {
1745 start
= p
->base
+ p
->sectors
[j
].offset
;
1746 xml_printf(&retval
, &xml
, &pos
, &size
,
1747 "<memory type=\"flash\" "
1750 sector_size
= p
->sectors
[j
].size
;
1753 /* Does this finish a group of sectors?
1754 * If not, continue an already-started group.
1756 if (j
== p
->num_sectors
-1)
1757 group_len
= (p
->base
+ p
->size
) - start
;
1758 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1759 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1764 xml_printf(&retval
, &xml
, &pos
, &size
,
1765 "length=\"0x%x\">\n"
1766 "<property name=\"blocksize\">"
1774 ram_start
= p
->base
+ p
->size
;
1778 xml_printf(&retval
, &xml
, &pos
, &size
,
1779 "<memory type=\"ram\" start=\"0x%x\" "
1780 "length=\"0x%x\"/>\n",
1781 ram_start
, 0-ram_start
);
1782 /* ELSE a flash chip could be at the very end of the 32 bit address
1783 * space, in which case ram_start will be precisely 0
1789 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1791 if (retval
!= ERROR_OK
) {
1792 gdb_error(connection
, retval
);
1796 if (offset
+ length
> pos
)
1797 length
= pos
- offset
;
1799 char *t
= malloc(length
+ 1);
1801 memcpy(t
+ 1, xml
+ offset
, length
);
1802 gdb_put_packet(connection
, t
, length
+ 1);
1809 static int gdb_query_packet(struct connection
*connection
,
1810 struct target
*target
, char *packet
, int packet_size
)
1812 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1813 struct gdb_connection
*gdb_connection
= connection
->priv
;
1815 if (strstr(packet
, "qRcmd,"))
1817 if (packet_size
> 6)
1821 cmd
= malloc((packet_size
- 6)/2 + 1);
1822 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1825 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1828 cmd
[(packet_size
- 6)/2] = 0x0;
1830 /* We want to print all debug output to GDB connection */
1831 log_add_callback(gdb_log_callback
, connection
);
1832 target_call_timer_callbacks_now();
1833 /* some commands need to know the GDB connection, make note of current
1834 * GDB connection. */
1835 current_gdb_connection
= gdb_connection
;
1836 command_run_line(cmd_ctx
, cmd
);
1837 current_gdb_connection
= NULL
;
1838 target_call_timer_callbacks_now();
1839 log_remove_callback(gdb_log_callback
, connection
);
1842 gdb_put_packet(connection
, "OK", 2);
1845 else if (strstr(packet
, "qCRC:"))
1847 if (packet_size
> 5)
1856 /* skip command character */
1859 addr
= strtoul(packet
, &separator
, 16);
1861 if (*separator
!= ',')
1863 LOG_ERROR("incomplete read memory packet received, dropping connection");
1864 return ERROR_SERVER_REMOTE_CLOSED
;
1867 len
= strtoul(separator
+ 1, NULL
, 16);
1869 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1871 if (retval
== ERROR_OK
)
1873 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1874 gdb_put_packet(connection
, gdb_reply
, 9);
1878 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1885 else if (strstr(packet
, "qSupported"))
1887 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1888 * disable qXfer:features:read for the moment */
1889 int retval
= ERROR_OK
;
1890 char *buffer
= NULL
;
1894 xml_printf(&retval
, &buffer
, &pos
, &size
,
1895 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1896 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1898 if (retval
!= ERROR_OK
)
1900 gdb_send_error(connection
, 01);
1904 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1909 else if (strstr(packet
, "qXfer:memory-map:read::")
1910 && (flash_get_bank_count() > 0))
1911 return gdb_memory_map(connection
, target
, packet
, packet_size
);
1912 else if (strstr(packet
, "qXfer:features:read:"))
1917 int retval
= ERROR_OK
;
1920 unsigned int length
;
1923 /* skip command character */
1926 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1928 gdb_send_error(connection
, 01);
1932 if (strcmp(annex
, "target.xml") != 0)
1934 gdb_send_error(connection
, 01);
1938 xml_printf(&retval
, &xml
, &pos
, &size
, \
1939 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1941 if (retval
!= ERROR_OK
)
1943 gdb_error(connection
, retval
);
1947 gdb_put_packet(connection
, xml
, strlen(xml
));
1952 else if (strstr(packet
, "QStartNoAckMode"))
1954 gdb_connection
->noack_mode
= 1;
1955 gdb_put_packet(connection
, "OK", 2);
1959 gdb_put_packet(connection
, "", 0);
1963 static int gdb_v_packet(struct connection
*connection
,
1964 struct target
*target
, char *packet
, int packet_size
)
1966 struct gdb_connection
*gdb_connection
= connection
->priv
;
1967 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1970 /* if flash programming disabled - send a empty reply */
1972 if (gdb_flash_program
== 0)
1974 gdb_put_packet(connection
, "", 0);
1978 if (strstr(packet
, "vFlashErase:"))
1981 unsigned long length
;
1983 char *parse
= packet
+ 12;
1986 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1987 return ERROR_SERVER_REMOTE_CLOSED
;
1990 addr
= strtoul(parse
, &parse
, 16);
1992 if (*(parse
++) != ',' || *parse
== '\0')
1994 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1995 return ERROR_SERVER_REMOTE_CLOSED
;
1998 length
= strtoul(parse
, &parse
, 16);
2002 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2003 return ERROR_SERVER_REMOTE_CLOSED
;
2006 /* assume all sectors need erasing - stops any problems
2007 * when flash_write is called multiple times */
2010 /* perform any target specific operations before the erase */
2011 target_call_event_callbacks(gdb_service
->target
,
2012 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2014 /* vFlashErase:addr,length messages require region start and
2015 * end to be "block" aligned ... if padding is ever needed,
2016 * GDB will have become dangerously confused.
2018 result
= flash_erase_address_range(gdb_service
->target
,
2019 false, addr
, length
);
2021 /* perform any target specific operations after the erase */
2022 target_call_event_callbacks(gdb_service
->target
,
2023 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2026 if (result
!= ERROR_OK
)
2028 /* GDB doesn't evaluate the actual error number returned,
2029 * treat a failed erase as an I/O error
2031 gdb_send_error(connection
, EIO
);
2032 LOG_ERROR("flash_erase returned %i", result
);
2035 gdb_put_packet(connection
, "OK", 2);
2040 if (strstr(packet
, "vFlashWrite:"))
2044 unsigned long length
;
2045 char *parse
= packet
+ 12;
2049 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2050 return ERROR_SERVER_REMOTE_CLOSED
;
2052 addr
= strtoul(parse
, &parse
, 16);
2053 if (*(parse
++) != ':')
2055 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2056 return ERROR_SERVER_REMOTE_CLOSED
;
2058 length
= packet_size
- (parse
- packet
);
2060 /* create a new image if there isn't already one */
2061 if (gdb_connection
->vflash_image
== NULL
)
2063 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2064 image_open(gdb_connection
->vflash_image
, "", "build");
2067 /* create new section with content from packet buffer */
2068 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
2073 gdb_put_packet(connection
, "OK", 2);
2078 if (!strcmp(packet
, "vFlashDone"))
2082 /* process the flashing buffer. No need to erase as GDB
2083 * always issues a vFlashErase first. */
2084 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
2085 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2086 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2087 if (result
!= ERROR_OK
)
2089 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2090 gdb_put_packet(connection
, "E.memtype", 9);
2092 gdb_send_error(connection
, EIO
);
2096 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2097 gdb_put_packet(connection
, "OK", 2);
2100 image_close(gdb_connection
->vflash_image
);
2101 free(gdb_connection
->vflash_image
);
2102 gdb_connection
->vflash_image
= NULL
;
2107 gdb_put_packet(connection
, "", 0);
2111 static int gdb_detach(struct connection
*connection
, struct target
*target
)
2113 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2115 target_call_event_callbacks(gdb_service
->target
,
2116 TARGET_EVENT_GDB_DETACH
);
2118 return gdb_put_packet(connection
, "OK", 2);
2121 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2122 const char *function
, const char *string
)
2124 struct connection
*connection
= priv
;
2125 struct gdb_connection
*gdb_con
= connection
->priv
;
2129 /* do not reply this using the O packet */
2133 gdb_output_con(connection
, string
);
2136 static void gdb_sig_halted(struct connection
*connection
)
2139 snprintf(sig_reply
, 4, "T%2.2x", 2);
2140 gdb_put_packet(connection
, sig_reply
, 3);
2144 static int gdb_input_inner(struct connection
*connection
)
2146 /* Do not allocate this on the stack */
2147 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2149 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2150 struct target
*target
= gdb_service
->target
;
2151 char *packet
= gdb_packet_buffer
;
2154 struct gdb_connection
*gdb_con
= connection
->priv
;
2155 static int extended_protocol
= 0;
2157 /* drain input buffer. If one of the packets fail, then an error
2158 * packet is replied, if applicable.
2160 * This loop will terminate and the error code is returned.
2162 * The calling fn will check if this error is something that
2163 * can be recovered from, or if the connection must be closed.
2165 * If the error is recoverable, this fn is called again to
2166 * drain the rest of the buffer.
2170 packet_size
= GDB_BUFFER_SIZE
-1;
2171 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2172 if (retval
!= ERROR_OK
)
2175 /* terminate with zero */
2176 packet
[packet_size
] = 0;
2178 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2179 if (packet
[0] == 'X') {
2180 // binary packets spew junk into the debug log stream
2183 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2187 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2189 LOG_DEBUG("received packet: '%s'", packet
);
2193 if (packet_size
> 0)
2199 /* Hct... -- set thread
2200 * we don't have threads, send empty reply */
2201 gdb_put_packet(connection
, NULL
, 0);
2205 retval
= gdb_query_packet(connection
,
2210 retval
= gdb_get_registers_packet(
2212 packet
, packet_size
);
2215 retval
= gdb_set_registers_packet(
2217 packet
, packet_size
);
2220 retval
= gdb_get_register_packet(
2222 packet
, packet_size
);
2225 retval
= gdb_set_register_packet(
2227 packet
, packet_size
);
2230 retval
= gdb_read_memory_packet(
2232 packet
, packet_size
);
2235 retval
= gdb_write_memory_packet(
2237 packet
, packet_size
);
2241 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2244 gdb_last_signal_packet(
2246 packet
, packet_size
);
2251 log_add_callback(gdb_log_callback
, connection
);
2253 if (gdb_con
->mem_write_error
)
2255 LOG_ERROR("Memory write failure!");
2257 /* now that we have reported the memory write error, we can clear the condition */
2258 gdb_con
->mem_write_error
= false;
2261 bool nostep
= false;
2262 bool already_running
= false;
2263 if (target
->state
== TARGET_RUNNING
)
2265 LOG_WARNING("WARNING! The target is already running. "
2266 "All changes GDB did to registers will be discarded! "
2267 "Waiting for target to halt.");
2268 already_running
= true;
2269 } else if (target
->state
!= TARGET_HALTED
)
2271 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2273 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2275 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2276 * sent by GDB first to OpenOCD, thus defeating the check to
2277 * make only the single stepping have the sync feature...
2280 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2282 gdb_con
->sync
= false;
2284 if (!already_running
&& nostep
)
2286 /* Either the target isn't in the halted state, then we can't
2287 * step/continue. This might be early setup, etc.
2289 * Or we want to allow GDB to pick up a fresh set of
2290 * register values without modifying the target state.
2293 gdb_sig_halted(connection
);
2295 /* stop forwarding log packets! */
2296 log_remove_callback(gdb_log_callback
, connection
);
2299 /* We're running/stepping, in which case we can
2300 * forward log output until the target is halted
2302 gdb_con
->frontend_state
= TARGET_RUNNING
;
2303 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2305 if (!already_running
)
2307 /* Here we don't want packet processing to stop even if this fails,
2308 * so we use a local variable instead of retval. */
2309 retval
= gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2310 if (retval
!= ERROR_OK
)
2312 /* we'll never receive a halted condition... issue a false one.. */
2313 gdb_frontend_halted(target
, connection
);
2320 retval
= gdb_v_packet(
2322 packet
, packet_size
);
2325 retval
= gdb_detach(connection
, target
);
2326 extended_protocol
= 0;
2329 retval
= gdb_write_memory_binary_packet(
2331 packet
, packet_size
);
2332 if (retval
!= ERROR_OK
)
2336 if (extended_protocol
!= 0)
2338 gdb_put_packet(connection
, "OK", 2);
2339 return ERROR_SERVER_REMOTE_CLOSED
;
2341 /* handle extended remote protocol */
2342 extended_protocol
= 1;
2343 gdb_put_packet(connection
, "OK", 2);
2346 /* handle extended restart packet */
2347 breakpoint_clear_target(gdb_service
->target
);
2348 watchpoint_clear_target(gdb_service
->target
);
2349 command_run_linef(connection
->cmd_ctx
,
2350 "ocd_gdb_restart %s",
2351 target_name(target
));
2354 /* ignore unknown packets */
2355 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2356 gdb_put_packet(connection
, NULL
, 0);
2360 /* if a packet handler returned an error, exit input loop */
2361 if (retval
!= ERROR_OK
)
2365 if (gdb_con
->ctrl_c
)
2367 if (target
->state
== TARGET_RUNNING
)
2369 retval
= target_halt(target
);
2370 if (retval
!= ERROR_OK
)
2372 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2374 gdb_con
->ctrl_c
= 0;
2377 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2378 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2382 } while (gdb_con
->buf_cnt
> 0);
2387 static int gdb_input(struct connection
*connection
)
2389 int retval
= gdb_input_inner(connection
);
2390 struct gdb_connection
*gdb_con
= connection
->priv
;
2391 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2394 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2395 if (gdb_con
->closed
)
2396 return ERROR_SERVER_REMOTE_CLOSED
;
2398 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2402 static int gdb_target_start(struct target
*target
, uint16_t port
)
2404 bool use_pipes
= 0 == port
;
2405 struct gdb_service
*gdb_service
= malloc(sizeof(struct gdb_service
));
2406 if (NULL
== gdb_service
)
2409 gdb_service
->target
= target
;
2411 add_service("gdb", use_pipes
? CONNECTION_PIPE
: CONNECTION_TCP
,
2412 port
, 1, &gdb_new_connection
, &gdb_input
,
2413 &gdb_connection_closed
, gdb_service
);
2415 const char *name
= target_name(target
);
2417 LOG_DEBUG("gdb service for target '%s' using pipes", name
);
2419 LOG_DEBUG("gdb service for target '%s' on TCP port %u", name
, port
);
2423 static int gdb_target_add_one(struct target
*target
)
2425 if (gdb_port
== 0 && server_use_pipes
== 0)
2427 LOG_INFO("gdb port disabled");
2430 if (0 == gdb_port_next
)
2431 gdb_port_next
= gdb_port
;
2433 bool use_pipes
= server_use_pipes
;
2434 static bool server_started_with_pipes
= false;
2435 if (server_started_with_pipes
)
2437 LOG_WARNING("gdb service permits one target when using pipes");
2444 int e
= gdb_target_start(target
, use_pipes
? 0 : gdb_port_next
);
2447 server_started_with_pipes
|= use_pipes
;
2453 int gdb_target_add_all(struct target
*target
)
2457 LOG_WARNING("gdb services need one or more targets defined");
2461 while (NULL
!= target
)
2463 int retval
= gdb_target_add_one(target
);
2464 if (ERROR_OK
!= retval
)
2467 target
= target
->next
;
2473 COMMAND_HANDLER(handle_gdb_sync_command
)
2477 return ERROR_COMMAND_SYNTAX_ERROR
;
2480 if (current_gdb_connection
== NULL
)
2482 command_print(CMD_CTX
,
2483 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2487 current_gdb_connection
->sync
= true;
2492 /* daemon configuration command gdb_port */
2493 COMMAND_HANDLER(handle_gdb_port_command
)
2495 int retval
= CALL_COMMAND_HANDLER(server_port_command
, &gdb_port
);
2496 if (ERROR_OK
== retval
)
2497 gdb_port_next
= gdb_port
;
2501 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2504 return ERROR_COMMAND_SYNTAX_ERROR
;
2506 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2510 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2513 return ERROR_COMMAND_SYNTAX_ERROR
;
2515 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2519 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2522 return ERROR_COMMAND_SYNTAX_ERROR
;
2524 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2528 /* gdb_breakpoint_override */
2529 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2534 } else if (CMD_ARGC
== 1)
2536 gdb_breakpoint_override
= 1;
2537 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2539 gdb_breakpoint_override_type
= BKPT_HARD
;
2540 } else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2542 gdb_breakpoint_override_type
= BKPT_SOFT
;
2543 } else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2545 gdb_breakpoint_override
= 0;
2549 return ERROR_COMMAND_SYNTAX_ERROR
;
2551 if (gdb_breakpoint_override
)
2553 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2556 LOG_USER("breakpoint type is not overridden");
2562 static const struct command_registration gdb_command_handlers
[] = {
2565 .handler
= handle_gdb_sync_command
,
2566 .mode
= COMMAND_ANY
,
2567 .help
= "next stepi will return immediately allowing "
2568 "GDB to fetch register state without affecting "
2573 .handler
= handle_gdb_port_command
,
2574 .mode
= COMMAND_ANY
,
2575 .help
= "Display or specify base port on which to listen "
2576 "for incoming GDB connections. "
2577 "No arguments reports GDB port; zero disables.",
2578 .usage
= "[port_num]",
2581 .name
= "gdb_memory_map",
2582 .handler
= handle_gdb_memory_map_command
,
2583 .mode
= COMMAND_CONFIG
,
2584 .help
= "enable or disable memory map",
2585 .usage
= "('enable'|'disable')"
2588 .name
= "gdb_flash_program",
2589 .handler
= handle_gdb_flash_program_command
,
2590 .mode
= COMMAND_CONFIG
,
2591 .help
= "enable or disable flash program",
2592 .usage
= "('enable'|'disable')"
2595 .name
= "gdb_report_data_abort",
2596 .handler
= handle_gdb_report_data_abort_command
,
2597 .mode
= COMMAND_CONFIG
,
2598 .help
= "enable or disable reporting data aborts",
2599 .usage
= "('enable'|'disable')"
2602 .name
= "gdb_breakpoint_override",
2603 .handler
= handle_gdb_breakpoint_override_command
,
2604 .mode
= COMMAND_ANY
,
2605 .help
= "Display or specify type of breakpoint "
2606 "to be used by gdb 'break' commands.",
2607 .usage
= "('hard'|'soft'|'disable')"
2609 COMMAND_REGISTRATION_DONE
2612 int gdb_register_commands(struct command_context
*cmd_ctx
)
2614 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);