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 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
33 #include <target/breakpoints.h>
34 #include <target/target_request.h>
35 #include <target/register.h>
37 #include <flash/nor/core.h>
38 #include "gdb_server.h"
39 #include <target/image.h>
40 #include <jtag/jtag.h>
41 #include "rtos/rtos.h"
46 * GDB server implementation.
48 * This implements the GDB Remote Serial Protocol, over TCP connections,
49 * giving GDB access to the JTAG or other hardware debugging facilities
50 * found in most modern embedded processors.
53 /* private connection data for GDB */
56 char buffer
[GDB_BUFFER_SIZE
];
60 enum target_state frontend_state
;
61 struct image
*vflash_image
;
65 bool sync
; /* set flag to true if you want the next stepi to return immediately.
66 allowing GDB to pick up a fresh set of register values from the target
67 without modifying the target state. */
68 /* We delay reporting memory write errors until next step/continue or memory
69 * write. This improves performance of gdb load significantly as the GDB packet
70 * can be replied immediately and a new GDB packet will be ready without delay
78 #define _DEBUG_GDB_IO_
81 static struct gdb_connection
*current_gdb_connection
;
83 static int gdb_breakpoint_override
;
84 static enum breakpoint_type gdb_breakpoint_override_type
;
86 static int gdb_error(struct connection
*connection
, int retval
);
87 static const char *gdb_port
;
88 static const char *gdb_port_next
;
89 static const char DIGITS
[16] = "0123456789abcdef";
91 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
92 const char *function
, const char *string
);
94 /* number of gdb connections, mainly to suppress gdb related debugging spam
95 * in helper/log.c when no gdb connections are actually active */
96 int gdb_actual_connections
;
98 /* set if we are sending a memory map to gdb
99 * via qXfer:memory-map:read packet */
100 /* enabled by default*/
101 static int gdb_use_memory_map
= 1;
102 /* enabled by default*/
103 static int gdb_flash_program
= 1;
105 /* if set, data aborts cause an error to be reported in memory read packets
106 * see the code in gdb_read_memory_packet() for further explanations.
107 * Disabled by default.
109 static int gdb_report_data_abort
;
111 static int gdb_last_signal(struct target
*target
)
113 switch (target
->debug_reason
)
115 case DBG_REASON_DBGRQ
:
116 return 0x2; /* SIGINT */
117 case DBG_REASON_BREAKPOINT
:
118 case DBG_REASON_WATCHPOINT
:
119 case DBG_REASON_WPTANDBKPT
:
120 return 0x05; /* SIGTRAP */
121 case DBG_REASON_SINGLESTEP
:
122 return 0x05; /* SIGTRAP */
123 case DBG_REASON_NOTHALTED
:
124 return 0x0; /* no signal... shouldn't happen */
126 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
131 static int check_pending(struct connection
*connection
,
132 int timeout_s
, int *got_data
)
134 /* a non-blocking socket will block if there is 0 bytes available on the socket,
135 * but return with as many bytes as are available immediately
139 struct gdb_connection
*gdb_con
= connection
->priv
;
141 if (got_data
== NULL
)
145 if (gdb_con
->buf_cnt
> 0)
152 FD_SET(connection
->fd
, &read_fds
);
154 tv
.tv_sec
= timeout_s
;
156 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
158 /* This can typically be because a "monitor" command took too long
159 * before printing any progress messages
163 return ERROR_GDB_TIMEOUT
;
169 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
173 static int gdb_get_char_inner(struct connection
*connection
, int* next_char
)
175 struct gdb_connection
*gdb_con
= connection
->priv
;
176 int retval
= ERROR_OK
;
178 #ifdef _DEBUG_GDB_IO_
183 if (connection
->service
->type
!= CONNECTION_TCP
)
185 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
189 retval
= check_pending(connection
, 1, NULL
);
190 if (retval
!= ERROR_OK
)
192 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
195 if (gdb_con
->buf_cnt
> 0)
199 if (gdb_con
->buf_cnt
== 0)
202 return ERROR_SERVER_REMOTE_CLOSED
;
206 errno
= WSAGetLastError();
213 case WSAECONNABORTED
:
215 return ERROR_SERVER_REMOTE_CLOSED
;
218 return ERROR_SERVER_REMOTE_CLOSED
;
220 LOG_ERROR("read: %d", errno
);
231 return ERROR_SERVER_REMOTE_CLOSED
;
234 return ERROR_SERVER_REMOTE_CLOSED
;
236 LOG_ERROR("read: %s", strerror(errno
));
238 return ERROR_SERVER_REMOTE_CLOSED
;
243 #ifdef _DEBUG_GDB_IO_
244 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
245 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
246 debug_buffer
[gdb_con
->buf_cnt
] = 0;
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
= malloc(len
+ 1);
389 memcpy(debug_buffer
, buffer
, len
);
390 debug_buffer
[len
] = 0;
391 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
395 char local_buffer
[1024];
396 local_buffer
[0] = '$';
397 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
399 /* performance gain on smaller packets by only a single call to gdb_write() */
400 memcpy(local_buffer
+ 1, buffer
, len
++);
401 local_buffer
[len
++] = '#';
402 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
403 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
404 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
411 /* larger packets are transmitted directly from caller supplied buffer
412 by several calls to gdb_write() to avoid dynamic allocation */
413 local_buffer
[1] = '#';
414 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
415 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
416 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
420 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
424 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
430 if (gdb_con
->noack_mode
)
433 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
438 else if (reply
== '-')
440 /* Stop sending output packets for now */
441 log_remove_callback(gdb_log_callback
, connection
);
442 LOG_WARNING("negative reply, retrying");
444 else if (reply
== 0x3)
447 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
451 else if (reply
== '-')
453 /* Stop sending output packets for now */
454 log_remove_callback(gdb_log_callback
, connection
);
455 LOG_WARNING("negative reply, retrying");
457 else if (reply
== '$') {
458 LOG_ERROR("GDB missing ack(1) - assumed good");
459 gdb_putback_char(connection
, reply
);
463 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
465 return ERROR_SERVER_REMOTE_CLOSED
;
468 else if (reply
== '$') {
469 LOG_ERROR("GDB missing ack(2) - assumed good");
470 gdb_putback_char(connection
, reply
);
475 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
477 return ERROR_SERVER_REMOTE_CLOSED
;
481 return ERROR_SERVER_REMOTE_CLOSED
;
486 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
488 struct gdb_connection
*gdb_con
= connection
->priv
;
490 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
493 /* we sent some data, reset timer for keep alive messages */
499 static __inline__
int fetch_packet(struct connection
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
501 unsigned char my_checksum
= 0;
504 int retval
= ERROR_OK
;
506 struct gdb_connection
*gdb_con
= connection
->priv
;
511 /* move this over into local variables to use registers and give the
512 * more freedom to optimize */
513 char *buf_p
= gdb_con
->buf_p
;
514 int buf_cnt
= gdb_con
->buf_cnt
;
518 /* The common case is that we have an entire packet with no escape chars.
519 * We need to leave at least 2 bytes in the buffer to have
520 * gdb_get_char() update various bits and bobs correctly.
522 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
))
524 /* The compiler will struggle a bit with constant propagation and
525 * aliasing, so we help it by showing that these values do not
526 * change inside the loop
530 int run
= buf_cnt
- 2;
537 if (character
== '#')
539 /* Danger! character can be '#' when esc is
540 * used so we need an explicit boolean for done here.
546 if (character
== '}')
548 /* data transmitted in binary mode (X packet)
549 * uses 0x7d as escape character */
550 my_checksum
+= character
& 0xff;
553 my_checksum
+= character
& 0xff;
554 buffer
[count
++] = (character
^ 0x20) & 0xff;
558 my_checksum
+= character
& 0xff;
559 buffer
[count
++] = character
& 0xff;
569 LOG_ERROR("packet buffer too small");
570 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
574 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
575 if (retval
!= ERROR_OK
)
578 if (character
== '#')
581 if (character
== '}')
583 /* data transmitted in binary mode (X packet)
584 * uses 0x7d as escape character */
585 my_checksum
+= character
& 0xff;
587 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
588 if (retval
!= ERROR_OK
)
591 my_checksum
+= character
& 0xff;
592 buffer
[count
++] = (character
^ 0x20) & 0xff;
596 my_checksum
+= character
& 0xff;
597 buffer
[count
++] = character
& 0xff;
601 gdb_con
->buf_p
= buf_p
;
602 gdb_con
->buf_cnt
= buf_cnt
;
604 if (retval
!= ERROR_OK
)
609 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
611 checksum
[0] = character
;
612 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
614 checksum
[1] = character
;
619 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
625 static int gdb_get_packet_inner(struct connection
*connection
,
626 char *buffer
, int *len
)
630 struct gdb_connection
*gdb_con
= connection
->priv
;
636 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
639 #ifdef _DEBUG_GDB_IO_
640 LOG_DEBUG("character: '%c'", character
);
648 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
649 * in case anyone tries to debug why they receive this warning every time */
650 LOG_WARNING("acknowledgment received, but no packet pending");
653 LOG_WARNING("negative acknowledgment, but no packet pending");
660 LOG_WARNING("ignoring character 0x%x", character
);
663 } while (character
!= '$');
668 /* explicit code expansion here to get faster inlined code in -O3 by not
669 * calculating checksum
671 if (gdb_con
->noack_mode
)
673 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
677 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
681 if (gdb_con
->noack_mode
)
683 /* checksum is not checked in noack mode */
688 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
696 return ERROR_SERVER_REMOTE_CLOSED
;
701 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
703 struct gdb_connection
*gdb_con
= connection
->priv
;
705 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
710 static int gdb_output_con(struct connection
*connection
, const char* line
)
715 bin_size
= strlen(line
);
717 hex_buffer
= malloc(bin_size
*2 + 2);
718 if (hex_buffer
== NULL
)
719 return ERROR_GDB_BUFFER_TOO_SMALL
;
722 for (i
= 0; i
< bin_size
; i
++)
723 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
724 hex_buffer
[bin_size
*2 + 1] = 0;
726 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
732 static int gdb_output(struct command_context
*context
, const char* line
)
734 /* this will be dumped to the log and also sent as an O packet if possible */
735 LOG_USER_N("%s", line
);
740 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
742 struct gdb_connection
*gdb_connection
= connection
->priv
;
744 /* In the GDB protocol when we are stepping or continuing execution,
745 * we have a lingering reply. Upon receiving a halted event
746 * when we have that lingering packet, we reply to the original
747 * step or continue packet.
749 * Executing monitor commands can bring the target in and
750 * out of the running state so we'll see lots of TARGET_EVENT_XXX
751 * that are to be ignored.
753 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
758 /* stop forwarding log packets! */
759 log_remove_callback(gdb_log_callback
, connection
);
761 if (gdb_connection
->ctrl_c
)
764 gdb_connection
->ctrl_c
= 0;
768 signal_var
= gdb_last_signal(target
);
771 snprintf(sig_reply
, 4, "T%2.2x", signal_var
);
772 gdb_put_packet(connection
, sig_reply
, 3);
773 gdb_connection
->frontend_state
= TARGET_HALTED
;
774 rtos_update_threads( target
);
778 static int gdb_target_callback_event_handler(struct target
*target
,
779 enum target_event event
, void *priv
)
782 struct connection
*connection
= priv
;
784 target_handle_event(target
, event
);
787 case TARGET_EVENT_GDB_HALT
:
788 gdb_frontend_halted(target
, connection
);
790 case TARGET_EVENT_HALTED
:
791 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
793 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
794 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
795 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
807 static int gdb_new_connection(struct connection
*connection
)
809 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
810 struct gdb_service
*gdb_service
= connection
->service
->priv
;
814 connection
->priv
= gdb_connection
;
816 /* initialize gdb connection information */
817 gdb_connection
->buf_p
= gdb_connection
->buffer
;
818 gdb_connection
->buf_cnt
= 0;
819 gdb_connection
->ctrl_c
= 0;
820 gdb_connection
->frontend_state
= TARGET_HALTED
;
821 gdb_connection
->vflash_image
= NULL
;
822 gdb_connection
->closed
= 0;
823 gdb_connection
->busy
= 0;
824 gdb_connection
->noack_mode
= 0;
825 gdb_connection
->sync
= true;
826 gdb_connection
->mem_write_error
= false;
828 /* send ACK to GDB for debug request */
829 gdb_write(connection
, "+", 1);
831 /* output goes through gdb connection */
832 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
834 /* we must remove all breakpoints registered to the target as a previous
835 * GDB session could leave dangling breakpoints if e.g. communication
838 breakpoint_clear_target(gdb_service
->target
);
839 watchpoint_clear_target(gdb_service
->target
);
841 /* remove the initial ACK from the incoming buffer */
842 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
845 /* FIX!!!??? would we actually ever receive a + here???
848 if (initial_ack
!= '+')
849 gdb_putback_char(connection
, initial_ack
);
850 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
852 if (gdb_use_memory_map
)
854 /* Connect must fail if the memory map can't be set up correctly.
856 * This will cause an auto_probe to be invoked, which is either
857 * a no-op or it will fail when the target isn't ready(e.g. not halted).
860 for (i
= 0; i
< flash_get_bank_count(); i
++)
862 struct flash_bank
*p
;
863 retval
= get_flash_bank_by_num(i
, &p
);
864 if (retval
!= ERROR_OK
)
866 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'.");
872 gdb_actual_connections
++;
873 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
874 gdb_actual_connections
,
875 target_name(gdb_service
->target
),
876 target_state_name(gdb_service
->target
));
878 /* DANGER! If we fail subsequently, we must remove this handler,
879 * otherwise we occasionally see crashes as the timer can invoke the
882 * register callback to be informed about target events */
883 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
888 static int gdb_connection_closed(struct connection
*connection
)
890 struct gdb_service
*gdb_service
= connection
->service
->priv
;
891 struct gdb_connection
*gdb_connection
= connection
->priv
;
893 /* we're done forwarding messages. Tear down callback before
894 * cleaning up connection.
896 log_remove_callback(gdb_log_callback
, connection
);
898 gdb_actual_connections
--;
899 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
900 target_name(gdb_service
->target
),
901 target_state_name(gdb_service
->target
),
902 gdb_actual_connections
);
904 /* see if an image built with vFlash commands is left */
905 if (gdb_connection
->vflash_image
)
907 image_close(gdb_connection
->vflash_image
);
908 free(gdb_connection
->vflash_image
);
909 gdb_connection
->vflash_image
= NULL
;
912 /* if this connection registered a debug-message receiver delete it */
913 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
915 if (connection
->priv
)
917 free(connection
->priv
);
918 connection
->priv
= NULL
;
922 LOG_ERROR("BUG: connection->priv == NULL");
926 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
928 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
930 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
935 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
938 snprintf(err
, 4, "E%2.2X", the_error
);
939 gdb_put_packet(connection
, err
, 3);
942 static int gdb_last_signal_packet(struct connection
*connection
,
943 struct target
*target
, char* packet
, int packet_size
)
948 signal_var
= gdb_last_signal(target
);
950 snprintf(sig_reply
, 4, "S%2.2x", signal_var
);
951 gdb_put_packet(connection
, sig_reply
, 3);
956 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
958 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
961 return len
- 1 - pos
;
964 /* Convert register to string of bytes. NB! The # of bits in the
965 * register might be non-divisible by 8(a byte), in which
966 * case an entire byte is shown.
968 * NB! the format on the wire is the target endianness
970 * The format of reg->value is little endian
973 static void gdb_str_to_target(struct target
*target
,
974 char *tstr
, struct reg
*reg
)
981 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
983 for (i
= 0; i
< buf_len
; i
++)
985 int j
= gdb_reg_pos(target
, i
, buf_len
);
986 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
987 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
991 static int hextoint(int c
)
1002 LOG_ERROR("BUG: invalid register value %08x", c
);
1006 /* copy over in register buffer */
1007 static void gdb_target_to_reg(struct target
*target
,
1008 char *tstr
, int str_len
, uint8_t *bin
)
1012 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1017 for (i
= 0; i
< str_len
; i
+= 2)
1019 uint8_t t
= hextoint(tstr
[i
]) << 4;
1020 t
|= hextoint(tstr
[i
+ 1]);
1022 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1027 static int gdb_get_registers_packet(struct connection
*connection
,
1028 struct target
*target
, char* packet
, int packet_size
)
1030 struct reg
**reg_list
;
1033 int reg_packet_size
= 0;
1038 #ifdef _DEBUG_GDB_IO_
1042 if ( ( target
->rtos
!= NULL
) &&
1043 ( ERROR_FAIL
!= rtos_get_gdb_reg_list( connection
, target
, ®_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
+= reg_list
[i
]->size
;
1058 reg_packet
= malloc(DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1059 reg_packet_p
= reg_packet
;
1061 for (i
= 0; i
< reg_list_size
; i
++)
1063 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1064 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1067 #ifdef _DEBUG_GDB_IO_
1070 reg_packet_p
= strndup(reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1071 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
1076 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1084 static int gdb_set_registers_packet(struct connection
*connection
,
1085 struct target
*target
, char *packet
, int packet_size
)
1088 struct reg
**reg_list
;
1093 #ifdef _DEBUG_GDB_IO_
1097 /* skip command character */
1101 if (packet_size
% 2)
1103 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1104 return ERROR_SERVER_REMOTE_CLOSED
;
1107 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1109 return gdb_error(connection
, retval
);
1113 for (i
= 0; i
< reg_list_size
; i
++)
1116 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1118 if (packet_p
+ chars
> packet
+ packet_size
)
1120 LOG_ERROR("BUG: register packet is too small for registers");
1123 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1124 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1126 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1128 /* advance packet pointer */
1135 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1138 gdb_put_packet(connection
, "OK", 2);
1143 static int gdb_get_register_packet(struct connection
*connection
,
1144 struct target
*target
, char *packet
, int packet_size
)
1147 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1148 struct reg
**reg_list
;
1152 #ifdef _DEBUG_GDB_IO_
1156 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1158 return gdb_error(connection
, retval
);
1161 if (reg_list_size
<= reg_num
)
1163 LOG_ERROR("gdb requested a non-existing register");
1167 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1169 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1171 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1179 static int gdb_set_register_packet(struct connection
*connection
,
1180 struct target
*target
, char *packet
, int packet_size
)
1184 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1185 struct reg
**reg_list
;
1191 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1193 return gdb_error(connection
, retval
);
1196 if (reg_list_size
< reg_num
)
1198 LOG_ERROR("gdb requested a non-existing register");
1199 return ERROR_SERVER_REMOTE_CLOSED
;
1202 if (*separator
!= '=')
1204 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1205 return ERROR_SERVER_REMOTE_CLOSED
;
1208 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1209 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1210 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1212 /* fix!!! add some sanity checks on packet size here */
1214 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1216 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1218 gdb_put_packet(connection
, "OK", 2);
1226 /* No attempt is made to translate the "retval" to
1227 * GDB speak. This has to be done at the calling
1228 * site as no mapping really exists.
1230 static int gdb_error(struct connection
*connection
, int retval
)
1232 LOG_DEBUG("Reporting %i to GDB as generic error", retval
);
1233 gdb_send_error(connection
, EFAULT
);
1237 /* We don't have to worry about the default 2 second timeout for GDB packets,
1238 * because GDB breaks up large memory reads into smaller reads.
1240 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1242 static int gdb_read_memory_packet(struct connection
*connection
,
1243 struct target
*target
, char *packet
, int packet_size
)
1252 int retval
= ERROR_OK
;
1254 /* skip command character */
1257 addr
= strtoul(packet
, &separator
, 16);
1259 if (*separator
!= ',')
1261 LOG_ERROR("incomplete read memory packet received, dropping connection");
1262 return ERROR_SERVER_REMOTE_CLOSED
;
1265 len
= strtoul(separator
+ 1, NULL
, 16);
1267 buffer
= malloc(len
);
1269 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1271 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1273 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1275 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1276 * At some point this might be fixed in GDB, in which case this code can be removed.
1278 * OpenOCD developers are acutely aware of this problem, but there is nothing
1279 * gained by involving the user in this problem that hopefully will get resolved
1282 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1284 * For now, the default is to fix up things to make current GDB versions work.
1285 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1287 memset(buffer
, 0, len
);
1291 if (retval
== ERROR_OK
)
1293 hex_buffer
= malloc(len
* 2 + 1);
1296 for (i
= 0; i
< len
; i
++)
1298 uint8_t t
= buffer
[i
];
1299 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1300 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1303 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1309 retval
= gdb_error(connection
, retval
);
1317 static int gdb_write_memory_packet(struct connection
*connection
,
1318 struct target
*target
, char *packet
, int packet_size
)
1329 /* skip command character */
1332 addr
= strtoul(packet
, &separator
, 16);
1334 if (*separator
!= ',')
1336 LOG_ERROR("incomplete write memory packet received, dropping connection");
1337 return ERROR_SERVER_REMOTE_CLOSED
;
1340 len
= strtoul(separator
+ 1, &separator
, 16);
1342 if (*(separator
++) != ':')
1344 LOG_ERROR("incomplete write memory packet received, dropping connection");
1345 return ERROR_SERVER_REMOTE_CLOSED
;
1348 buffer
= malloc(len
);
1350 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1352 for (i
= 0; i
< len
; i
++)
1355 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1359 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1361 if (retval
== ERROR_OK
)
1363 gdb_put_packet(connection
, "OK", 2);
1367 retval
= gdb_error(connection
, retval
);
1375 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1376 struct target
*target
, char *packet
, int packet_size
)
1382 int retval
= ERROR_OK
;
1384 /* skip command character */
1387 addr
= strtoul(packet
, &separator
, 16);
1389 if (*separator
!= ',')
1391 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1392 return ERROR_SERVER_REMOTE_CLOSED
;
1395 len
= strtoul(separator
+ 1, &separator
, 16);
1397 if (*(separator
++) != ':')
1399 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1400 return ERROR_SERVER_REMOTE_CLOSED
;
1403 struct gdb_connection
*gdb_connection
= connection
->priv
;
1405 if (gdb_connection
->mem_write_error
)
1407 retval
= ERROR_FAIL
;
1408 /* now that we have reported the memory write error, we can clear the condition */
1409 gdb_connection
->mem_write_error
= false;
1412 /* By replying the packet *immediately* GDB will send us a new packet
1413 * while we write the last one to the target.
1415 if (retval
== ERROR_OK
)
1417 gdb_put_packet(connection
, "OK", 2);
1421 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1427 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1429 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1430 if (retval
!= ERROR_OK
)
1432 gdb_connection
->mem_write_error
= true;
1439 static int gdb_step_continue_packet(struct connection
*connection
,
1440 struct target
*target
, char *packet
, int packet_size
)
1443 uint32_t address
= 0x0;
1444 int retval
= ERROR_OK
;
1448 if (packet_size
> 1)
1450 packet
[packet_size
] = 0;
1451 address
= strtoul(packet
+ 1, NULL
, 16);
1458 if (packet
[0] == 'c')
1460 LOG_DEBUG("continue");
1461 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1462 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1464 else if (packet
[0] == 's')
1467 /* step at current or address, don't handle breakpoints */
1468 retval
= target_step(target
, current
, address
, 0);
1473 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1474 struct target
*target
, char *packet
, int packet_size
)
1477 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1478 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1486 type
= strtoul(packet
+ 1, &separator
, 16);
1488 if (type
== 0) /* memory breakpoint */
1489 bp_type
= BKPT_SOFT
;
1490 else if (type
== 1) /* hardware breakpoint */
1491 bp_type
= BKPT_HARD
;
1492 else if (type
== 2) /* write watchpoint */
1493 wp_type
= WPT_WRITE
;
1494 else if (type
== 3) /* read watchpoint */
1496 else if (type
== 4) /* access watchpoint */
1497 wp_type
= WPT_ACCESS
;
1500 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1501 return ERROR_SERVER_REMOTE_CLOSED
;
1505 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1507 bp_type
= gdb_breakpoint_override_type
;
1510 if (*separator
!= ',')
1512 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1513 return ERROR_SERVER_REMOTE_CLOSED
;
1516 address
= strtoul(separator
+ 1, &separator
, 16);
1518 if (*separator
!= ',')
1520 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1521 return ERROR_SERVER_REMOTE_CLOSED
;
1524 size
= strtoul(separator
+ 1, &separator
, 16);
1530 if (packet
[0] == 'Z')
1532 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1534 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1539 gdb_put_packet(connection
, "OK", 2);
1544 breakpoint_remove(target
, address
);
1545 gdb_put_packet(connection
, "OK", 2);
1552 if (packet
[0] == 'Z')
1554 if ((retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
)) != ERROR_OK
)
1556 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1561 gdb_put_packet(connection
, "OK", 2);
1566 watchpoint_remove(target
, address
);
1567 gdb_put_packet(connection
, "OK", 2);
1578 /* print out a string and allocate more space as needed,
1579 * mainly used for XML at this point
1581 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1582 const char *fmt
, ...)
1584 if (*retval
!= ERROR_OK
)
1592 if ((*xml
== NULL
) || (!first
))
1594 /* start by 0 to exercise all the code paths.
1595 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1597 *size
= *size
* 2 + 2;
1599 *xml
= realloc(*xml
, *size
);
1604 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1612 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1614 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1619 /* there was just enough or not enough space, allocate more. */
1624 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1628 /* Extract and NUL-terminate the annex. */
1630 while (*buf
&& *buf
!= ':')
1636 /* After the read marker and annex, qXfer looks like a
1637 * traditional 'm' packet. */
1639 *ofs
= strtoul(buf
, &separator
, 16);
1641 if (*separator
!= ',')
1644 *len
= strtoul(separator
+ 1, NULL
, 16);
1649 static int compare_bank (const void * a
, const void * b
)
1651 struct flash_bank
*b1
, *b2
;
1652 b1
=*((struct flash_bank
**)a
);
1653 b2
=*((struct flash_bank
**)b
);
1655 if (b1
->base
== b2
->base
)
1658 } else if (b1
->base
> b2
->base
)
1667 static int gdb_memory_map(struct connection
*connection
,
1668 struct target
*target
, char *packet
, int packet_size
)
1670 /* We get away with only specifying flash here. Regions that are not
1671 * specified are treated as if we provided no memory map(if not we
1672 * could detect the holes and mark them as RAM).
1673 * Normally we only execute this code once, but no big deal if we
1674 * have to regenerate it a couple of times.
1677 struct flash_bank
*p
;
1681 int retval
= ERROR_OK
;
1682 struct flash_bank
**banks
;
1686 uint32_t ram_start
= 0;
1688 int target_flash_banks
= 0;
1690 /* skip command character */
1693 offset
= strtoul(packet
, &separator
, 16);
1694 length
= strtoul(separator
+ 1, &separator
, 16);
1696 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1698 /* Sort banks in ascending order. We need to report non-flash
1699 * memory as ram (or rather read/write) by default for GDB, since
1700 * it has no concept of non-cacheable read/write memory (i/o etc).
1702 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1703 * Current versions of GDB assume unlisted addresses are RAM...
1705 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1707 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1708 retval
= get_flash_bank_by_num(i
, &p
);
1709 if (retval
!= ERROR_OK
)
1712 gdb_error(connection
, retval
);
1715 if(p
->target
== target
)
1716 banks
[target_flash_banks
++] = p
;
1719 qsort(banks
, target_flash_banks
, sizeof(struct flash_bank
*),
1722 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1724 unsigned sector_size
= 0;
1725 uint32_t start
, end
;
1729 end
= p
->base
+ p
->size
;
1731 if (ram_start
< p
->base
)
1732 xml_printf(&retval
, &xml
, &pos
, &size
,
1733 "<memory type=\"ram\" start=\"0x%x\" "
1734 "length=\"0x%x\"/>\n",
1735 ram_start
, p
->base
- ram_start
);
1737 /* Report adjacent groups of same-size sectors. So for
1738 * example top boot CFI flash will list an initial region
1739 * with several large sectors (maybe 128KB) and several
1740 * smaller ones at the end (maybe 32KB). STR7 will have
1741 * regions with 8KB, 32KB, and 64KB sectors; etc.
1743 for (j
= 0; j
< p
->num_sectors
; j
++) {
1746 /* Maybe start a new group of sectors. */
1747 if (sector_size
== 0) {
1748 start
= p
->base
+ p
->sectors
[j
].offset
;
1749 xml_printf(&retval
, &xml
, &pos
, &size
,
1750 "<memory type=\"flash\" "
1753 sector_size
= p
->sectors
[j
].size
;
1756 /* Does this finish a group of sectors?
1757 * If not, continue an already-started group.
1759 if (j
== p
->num_sectors
-1)
1760 group_len
= (p
->base
+ p
->size
) - start
;
1761 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1762 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1767 xml_printf(&retval
, &xml
, &pos
, &size
,
1768 "length=\"0x%x\">\n"
1769 "<property name=\"blocksize\">"
1777 ram_start
= p
->base
+ p
->size
;
1781 xml_printf(&retval
, &xml
, &pos
, &size
,
1782 "<memory type=\"ram\" start=\"0x%x\" "
1783 "length=\"0x%x\"/>\n",
1784 ram_start
, 0-ram_start
);
1785 /* ELSE a flash chip could be at the very end of the 32 bit address
1786 * space, in which case ram_start will be precisely 0
1792 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1794 if (retval
!= ERROR_OK
) {
1795 gdb_error(connection
, retval
);
1799 if (offset
+ length
> pos
)
1800 length
= pos
- offset
;
1802 char *t
= malloc(length
+ 1);
1804 memcpy(t
+ 1, xml
+ offset
, length
);
1805 gdb_put_packet(connection
, t
, length
+ 1);
1812 static int gdb_query_packet(struct connection
*connection
,
1813 struct target
*target
, char *packet
, int packet_size
)
1815 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1816 struct gdb_connection
*gdb_connection
= connection
->priv
;
1818 if (strstr(packet
, "qRcmd,"))
1820 if (packet_size
> 6)
1824 cmd
= malloc((packet_size
- 6)/2 + 1);
1825 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1828 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1831 cmd
[(packet_size
- 6)/2] = 0x0;
1833 /* We want to print all debug output to GDB connection */
1834 log_add_callback(gdb_log_callback
, connection
);
1835 target_call_timer_callbacks_now();
1836 /* some commands need to know the GDB connection, make note of current
1837 * GDB connection. */
1838 current_gdb_connection
= gdb_connection
;
1839 command_run_line(cmd_ctx
, cmd
);
1840 current_gdb_connection
= NULL
;
1841 target_call_timer_callbacks_now();
1842 log_remove_callback(gdb_log_callback
, connection
);
1845 gdb_put_packet(connection
, "OK", 2);
1848 else if (strstr(packet
, "qCRC:"))
1850 if (packet_size
> 5)
1859 /* skip command character */
1862 addr
= strtoul(packet
, &separator
, 16);
1864 if (*separator
!= ',')
1866 LOG_ERROR("incomplete read memory packet received, dropping connection");
1867 return ERROR_SERVER_REMOTE_CLOSED
;
1870 len
= strtoul(separator
+ 1, NULL
, 16);
1872 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1874 if (retval
== ERROR_OK
)
1876 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1877 gdb_put_packet(connection
, gdb_reply
, 9);
1881 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1888 else if (strstr(packet
, "qSupported"))
1890 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1891 * disable qXfer:features:read for the moment */
1892 int retval
= ERROR_OK
;
1893 char *buffer
= NULL
;
1897 xml_printf(&retval
, &buffer
, &pos
, &size
,
1898 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1899 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1901 if (retval
!= ERROR_OK
)
1903 gdb_send_error(connection
, 01);
1907 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1912 else if (strstr(packet
, "qXfer:memory-map:read::")
1913 && (flash_get_bank_count() > 0))
1914 return gdb_memory_map(connection
, target
, packet
, packet_size
);
1915 else if (strstr(packet
, "qXfer:features:read:"))
1920 int retval
= ERROR_OK
;
1923 unsigned int length
;
1926 /* skip command character */
1929 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1931 gdb_send_error(connection
, 01);
1935 if (strcmp(annex
, "target.xml") != 0)
1937 gdb_send_error(connection
, 01);
1941 xml_printf(&retval
, &xml
, &pos
, &size
, \
1942 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1944 if (retval
!= ERROR_OK
)
1946 gdb_error(connection
, retval
);
1950 gdb_put_packet(connection
, xml
, strlen(xml
));
1955 else if (strstr(packet
, "QStartNoAckMode"))
1957 gdb_connection
->noack_mode
= 1;
1958 gdb_put_packet(connection
, "OK", 2);
1962 gdb_put_packet(connection
, "", 0);
1966 static int gdb_v_packet(struct connection
*connection
,
1967 struct target
*target
, char *packet
, int packet_size
)
1969 struct gdb_connection
*gdb_connection
= connection
->priv
;
1970 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1973 /* if flash programming disabled - send a empty reply */
1975 if (gdb_flash_program
== 0)
1977 gdb_put_packet(connection
, "", 0);
1981 if (strstr(packet
, "vFlashErase:"))
1984 unsigned long length
;
1986 char *parse
= packet
+ 12;
1989 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1990 return ERROR_SERVER_REMOTE_CLOSED
;
1993 addr
= strtoul(parse
, &parse
, 16);
1995 if (*(parse
++) != ',' || *parse
== '\0')
1997 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1998 return ERROR_SERVER_REMOTE_CLOSED
;
2001 length
= strtoul(parse
, &parse
, 16);
2005 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2006 return ERROR_SERVER_REMOTE_CLOSED
;
2009 /* assume all sectors need erasing - stops any problems
2010 * when flash_write is called multiple times */
2013 /* perform any target specific operations before the erase */
2014 target_call_event_callbacks(gdb_service
->target
,
2015 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2017 /* vFlashErase:addr,length messages require region start and
2018 * end to be "block" aligned ... if padding is ever needed,
2019 * GDB will have become dangerously confused.
2021 result
= flash_erase_address_range(gdb_service
->target
,
2022 false, addr
, length
);
2024 /* perform any target specific operations after the erase */
2025 target_call_event_callbacks(gdb_service
->target
,
2026 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2029 if (result
!= ERROR_OK
)
2031 /* GDB doesn't evaluate the actual error number returned,
2032 * treat a failed erase as an I/O error
2034 gdb_send_error(connection
, EIO
);
2035 LOG_ERROR("flash_erase returned %i", result
);
2038 gdb_put_packet(connection
, "OK", 2);
2043 if (strstr(packet
, "vFlashWrite:"))
2047 unsigned long length
;
2048 char *parse
= packet
+ 12;
2052 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2053 return ERROR_SERVER_REMOTE_CLOSED
;
2055 addr
= strtoul(parse
, &parse
, 16);
2056 if (*(parse
++) != ':')
2058 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2059 return ERROR_SERVER_REMOTE_CLOSED
;
2061 length
= packet_size
- (parse
- packet
);
2063 /* create a new image if there isn't already one */
2064 if (gdb_connection
->vflash_image
== NULL
)
2066 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2067 image_open(gdb_connection
->vflash_image
, "", "build");
2070 /* create new section with content from packet buffer */
2071 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
2076 gdb_put_packet(connection
, "OK", 2);
2081 if (!strcmp(packet
, "vFlashDone"))
2085 /* process the flashing buffer. No need to erase as GDB
2086 * always issues a vFlashErase first. */
2087 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
2088 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2089 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2090 if (result
!= ERROR_OK
)
2092 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2093 gdb_put_packet(connection
, "E.memtype", 9);
2095 gdb_send_error(connection
, EIO
);
2099 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2100 gdb_put_packet(connection
, "OK", 2);
2103 image_close(gdb_connection
->vflash_image
);
2104 free(gdb_connection
->vflash_image
);
2105 gdb_connection
->vflash_image
= NULL
;
2110 gdb_put_packet(connection
, "", 0);
2114 static int gdb_detach(struct connection
*connection
, struct target
*target
)
2116 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2118 target_call_event_callbacks(gdb_service
->target
,
2119 TARGET_EVENT_GDB_DETACH
);
2121 return gdb_put_packet(connection
, "OK", 2);
2124 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2125 const char *function
, const char *string
)
2127 struct connection
*connection
= priv
;
2128 struct gdb_connection
*gdb_con
= connection
->priv
;
2132 /* do not reply this using the O packet */
2136 gdb_output_con(connection
, string
);
2139 static void gdb_sig_halted(struct connection
*connection
)
2142 snprintf(sig_reply
, 4, "T%2.2x", 2);
2143 gdb_put_packet(connection
, sig_reply
, 3);
2147 static int gdb_input_inner(struct connection
*connection
)
2149 /* Do not allocate this on the stack */
2150 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2152 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2153 struct target
*target
= gdb_service
->target
;
2154 char *packet
= gdb_packet_buffer
;
2157 struct gdb_connection
*gdb_con
= connection
->priv
;
2158 static int extended_protocol
= 0;
2160 /* drain input buffer. If one of the packets fail, then an error
2161 * packet is replied, if applicable.
2163 * This loop will terminate and the error code is returned.
2165 * The calling fn will check if this error is something that
2166 * can be recovered from, or if the connection must be closed.
2168 * If the error is recoverable, this fn is called again to
2169 * drain the rest of the buffer.
2173 packet_size
= GDB_BUFFER_SIZE
-1;
2174 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2175 if (retval
!= ERROR_OK
)
2178 /* terminate with zero */
2179 packet
[packet_size
] = 0;
2181 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2182 if (packet
[0] == 'X') {
2183 // binary packets spew junk into the debug log stream
2186 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2190 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2192 LOG_DEBUG("received packet: '%s'", packet
);
2196 if (packet_size
> 0)
2201 case 'T': // Is thread alive?
2202 gdb_thread_packet(connection
, target
, packet
, packet_size
);
2204 case 'H': // Set current thread ( 'c' for step and continue, 'g' for all other operations )
2205 gdb_thread_packet(connection
, target
, packet
, packet_size
);
2209 retval
= gdb_thread_packet(connection
,
2212 if ( retval
== GDB_THREAD_PACKET_NOT_CONSUMED
)
2214 retval
= gdb_query_packet(connection
,
2220 retval
= gdb_get_registers_packet(
2222 packet
, packet_size
);
2225 retval
= gdb_set_registers_packet(
2227 packet
, packet_size
);
2230 retval
= gdb_get_register_packet(
2232 packet
, packet_size
);
2235 retval
= gdb_set_register_packet(
2237 packet
, packet_size
);
2240 retval
= gdb_read_memory_packet(
2242 packet
, packet_size
);
2245 retval
= gdb_write_memory_packet(
2247 packet
, packet_size
);
2251 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2254 gdb_last_signal_packet(
2256 packet
, packet_size
);
2261 log_add_callback(gdb_log_callback
, connection
);
2263 if (gdb_con
->mem_write_error
)
2265 LOG_ERROR("Memory write failure!");
2267 /* now that we have reported the memory write error, we can clear the condition */
2268 gdb_con
->mem_write_error
= false;
2271 bool nostep
= false;
2272 bool already_running
= false;
2273 if (target
->state
== TARGET_RUNNING
)
2275 LOG_WARNING("WARNING! The target is already running. "
2276 "All changes GDB did to registers will be discarded! "
2277 "Waiting for target to halt.");
2278 already_running
= true;
2279 } else if (target
->state
!= TARGET_HALTED
)
2281 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2283 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2285 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2286 * sent by GDB first to OpenOCD, thus defeating the check to
2287 * make only the single stepping have the sync feature...
2290 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2292 gdb_con
->sync
= false;
2294 if (!already_running
&& nostep
)
2296 /* Either the target isn't in the halted state, then we can't
2297 * step/continue. This might be early setup, etc.
2299 * Or we want to allow GDB to pick up a fresh set of
2300 * register values without modifying the target state.
2303 gdb_sig_halted(connection
);
2305 /* stop forwarding log packets! */
2306 log_remove_callback(gdb_log_callback
, connection
);
2309 /* We're running/stepping, in which case we can
2310 * forward log output until the target is halted
2312 gdb_con
->frontend_state
= TARGET_RUNNING
;
2313 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2315 if (!already_running
)
2317 /* Here we don't want packet processing to stop even if this fails,
2318 * so we use a local variable instead of retval. */
2319 retval
= gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2320 if (retval
!= ERROR_OK
)
2322 /* we'll never receive a halted condition... issue a false one.. */
2323 gdb_frontend_halted(target
, connection
);
2330 retval
= gdb_v_packet(
2332 packet
, packet_size
);
2335 retval
= gdb_detach(connection
, target
);
2336 extended_protocol
= 0;
2339 retval
= gdb_write_memory_binary_packet(
2341 packet
, packet_size
);
2342 if (retval
!= ERROR_OK
)
2346 if (extended_protocol
!= 0)
2348 gdb_put_packet(connection
, "OK", 2);
2349 return ERROR_SERVER_REMOTE_CLOSED
;
2351 /* handle extended remote protocol */
2352 extended_protocol
= 1;
2353 gdb_put_packet(connection
, "OK", 2);
2356 /* handle extended restart packet */
2357 breakpoint_clear_target(gdb_service
->target
);
2358 watchpoint_clear_target(gdb_service
->target
);
2359 command_run_linef(connection
->cmd_ctx
,
2360 "ocd_gdb_restart %s",
2361 target_name(target
));
2364 /* ignore unknown packets */
2365 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2366 gdb_put_packet(connection
, NULL
, 0);
2370 /* if a packet handler returned an error, exit input loop */
2371 if (retval
!= ERROR_OK
)
2375 if (gdb_con
->ctrl_c
)
2377 if (target
->state
== TARGET_RUNNING
)
2379 retval
= target_halt(target
);
2380 if (retval
!= ERROR_OK
)
2382 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2384 gdb_con
->ctrl_c
= 0;
2387 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2388 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2392 } while (gdb_con
->buf_cnt
> 0);
2397 static int gdb_input(struct connection
*connection
)
2399 int retval
= gdb_input_inner(connection
);
2400 struct gdb_connection
*gdb_con
= connection
->priv
;
2401 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2404 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2405 if (gdb_con
->closed
)
2406 return ERROR_SERVER_REMOTE_CLOSED
;
2408 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2412 static int gdb_target_start(struct target
*target
, const char *port
)
2414 struct gdb_service
*gdb_service
= malloc(sizeof(struct gdb_service
));
2415 if (NULL
== gdb_service
)
2418 gdb_service
->target
= target
;
2420 return add_service("gdb",
2421 port
, 1, &gdb_new_connection
, &gdb_input
,
2422 &gdb_connection_closed
, gdb_service
);
2425 static int gdb_target_add_one(struct target
*target
)
2427 int retval
= gdb_target_start(target
, gdb_port_next
);
2428 if (retval
== ERROR_OK
)
2431 /* If we can parse the port number
2432 * then we increment the port number for the next target.
2435 portnumber
= strtol(gdb_port_next
, &end
, 0);
2438 if (parse_long(gdb_port_next
, &portnumber
) == ERROR_OK
)
2440 free((void *)gdb_port_next
);
2441 gdb_port_next
= alloc_printf("%d", portnumber
+1);
2448 int gdb_target_add_all(struct target
*target
)
2452 LOG_WARNING("gdb services need one or more targets defined");
2456 while (NULL
!= target
)
2458 int retval
= gdb_target_add_one(target
);
2459 if (ERROR_OK
!= retval
)
2462 target
= target
->next
;
2468 COMMAND_HANDLER(handle_gdb_sync_command
)
2472 return ERROR_COMMAND_SYNTAX_ERROR
;
2475 if (current_gdb_connection
== NULL
)
2477 command_print(CMD_CTX
,
2478 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2482 current_gdb_connection
->sync
= true;
2487 /* daemon configuration command gdb_port */
2488 COMMAND_HANDLER(handle_gdb_port_command
)
2490 int retval
= CALL_COMMAND_HANDLER(server_pipe_command
, &gdb_port
);
2491 if (ERROR_OK
== retval
) {
2492 free((void*)gdb_port_next
);
2493 gdb_port_next
= strdup(gdb_port
);
2498 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2501 return ERROR_COMMAND_SYNTAX_ERROR
;
2503 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2507 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2510 return ERROR_COMMAND_SYNTAX_ERROR
;
2512 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2516 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2519 return ERROR_COMMAND_SYNTAX_ERROR
;
2521 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2525 /* gdb_breakpoint_override */
2526 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2531 } else if (CMD_ARGC
== 1)
2533 gdb_breakpoint_override
= 1;
2534 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2536 gdb_breakpoint_override_type
= BKPT_HARD
;
2537 } else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2539 gdb_breakpoint_override_type
= BKPT_SOFT
;
2540 } else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2542 gdb_breakpoint_override
= 0;
2546 return ERROR_COMMAND_SYNTAX_ERROR
;
2548 if (gdb_breakpoint_override
)
2550 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2553 LOG_USER("breakpoint type is not overridden");
2559 static const struct command_registration gdb_command_handlers
[] = {
2562 .handler
= handle_gdb_sync_command
,
2563 .mode
= COMMAND_ANY
,
2564 .help
= "next stepi will return immediately allowing "
2565 "GDB to fetch register state without affecting "
2570 .handler
= handle_gdb_port_command
,
2571 .mode
= COMMAND_ANY
,
2572 .help
= "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2573 "server listens for the next port number after the "
2574 "base port number specified. "
2575 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2576 "output to stdout, an integer is base port number, \"disable\" disables "
2577 "port. Any other string is are interpreted as named pipe to listen to. "
2578 "Output pipe is the same name as input pipe, but with 'o' appended.",
2579 .usage
= "[port_num]",
2582 .name
= "gdb_memory_map",
2583 .handler
= handle_gdb_memory_map_command
,
2584 .mode
= COMMAND_CONFIG
,
2585 .help
= "enable or disable memory map",
2586 .usage
= "('enable'|'disable')"
2589 .name
= "gdb_flash_program",
2590 .handler
= handle_gdb_flash_program_command
,
2591 .mode
= COMMAND_CONFIG
,
2592 .help
= "enable or disable flash program",
2593 .usage
= "('enable'|'disable')"
2596 .name
= "gdb_report_data_abort",
2597 .handler
= handle_gdb_report_data_abort_command
,
2598 .mode
= COMMAND_CONFIG
,
2599 .help
= "enable or disable reporting data aborts",
2600 .usage
= "('enable'|'disable')"
2603 .name
= "gdb_breakpoint_override",
2604 .handler
= handle_gdb_breakpoint_override_command
,
2605 .mode
= COMMAND_ANY
,
2606 .help
= "Display or specify type of breakpoint "
2607 "to be used by gdb 'break' commands.",
2608 .usage
= "('hard'|'soft'|'disable')"
2610 COMMAND_REGISTRATION_DONE
2613 int gdb_register_commands(struct command_context
*cmd_ctx
)
2615 gdb_port
= strdup("3333");
2616 gdb_port_next
= strdup("3333");
2617 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);