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 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
30 #include <target/breakpoints.h>
31 #include <target/target_request.h>
32 #include <target/register.h>
34 #include <flash/nor/core.h>
35 #include "gdb_server.h"
36 #include <target/image.h>
37 #include <jtag/jtag.h>
42 * GDB server implementation.
44 * This implements the GDB Remote Serial Protocol, over TCP connections,
45 * giving GDB access to the JTAG or other hardware debugging facilities
46 * found in most modern embedded processors.
49 /* private connection data for GDB */
52 char buffer
[GDB_BUFFER_SIZE
];
56 enum target_state frontend_state
;
57 struct image
*vflash_image
;
61 bool sync
; /* set flag to true if you want the next stepi to return immediately.
62 allowing GDB to pick up a fresh set of register values from the target
63 without modifying the target state. */
64 /* We delay reporting memory write errors until next step/continue or memory
65 * write. This improves performance of gdb load significantly as the GDB packet
66 * can be replied immediately and a new GDB packet will be ready without delay
74 #define _DEBUG_GDB_IO_
77 static struct gdb_connection
*current_gdb_connection
;
79 static int gdb_breakpoint_override
;
80 static enum breakpoint_type gdb_breakpoint_override_type
;
82 static int gdb_error(struct connection
*connection
, int retval
);
83 static unsigned short gdb_port
= 3333;
84 static unsigned short gdb_port_next
= 0;
85 static const char DIGITS
[16] = "0123456789abcdef";
87 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
88 const char *function
, const char *string
);
90 /* number of gdb connections, mainly to suppress gdb related debugging spam
91 * in helper/log.c when no gdb connections are actually active */
92 int gdb_actual_connections
;
94 /* set if we are sending a memory map to gdb
95 * via qXfer:memory-map:read packet */
96 /* enabled by default*/
97 static int gdb_use_memory_map
= 1;
98 /* enabled by default*/
99 static int gdb_flash_program
= 1;
101 /* if set, data aborts cause an error to be reported in memory read packets
102 * see the code in gdb_read_memory_packet() for further explanations.
103 * Disabled by default.
105 static int gdb_report_data_abort
;
107 static int gdb_last_signal(struct target
*target
)
109 switch (target
->debug_reason
)
111 case DBG_REASON_DBGRQ
:
112 return 0x2; /* SIGINT */
113 case DBG_REASON_BREAKPOINT
:
114 case DBG_REASON_WATCHPOINT
:
115 case DBG_REASON_WPTANDBKPT
:
116 return 0x05; /* SIGTRAP */
117 case DBG_REASON_SINGLESTEP
:
118 return 0x05; /* SIGTRAP */
119 case DBG_REASON_NOTHALTED
:
120 return 0x0; /* no signal... shouldn't happen */
122 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
127 static int check_pending(struct connection
*connection
,
128 int timeout_s
, int *got_data
)
130 /* a non-blocking socket will block if there is 0 bytes available on the socket,
131 * but return with as many bytes as are available immediately
135 struct gdb_connection
*gdb_con
= connection
->priv
;
137 if (got_data
== NULL
)
141 if (gdb_con
->buf_cnt
> 0)
148 FD_SET(connection
->fd
, &read_fds
);
150 tv
.tv_sec
= timeout_s
;
152 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
154 /* This can typically be because a "monitor" command took too long
155 * before printing any progress messages
159 return ERROR_GDB_TIMEOUT
;
165 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
169 static int gdb_get_char_inner(struct connection
*connection
, int* next_char
)
171 struct gdb_connection
*gdb_con
= connection
->priv
;
172 int retval
= ERROR_OK
;
174 #ifdef _DEBUG_GDB_IO_
179 if (connection
->service
->type
== CONNECTION_PIPE
)
181 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
185 retval
= check_pending(connection
, 1, NULL
);
186 if (retval
!= ERROR_OK
)
188 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
191 if (gdb_con
->buf_cnt
> 0)
195 if (gdb_con
->buf_cnt
== 0)
198 return ERROR_SERVER_REMOTE_CLOSED
;
202 errno
= WSAGetLastError();
209 case WSAECONNABORTED
:
211 return ERROR_SERVER_REMOTE_CLOSED
;
214 return ERROR_SERVER_REMOTE_CLOSED
;
216 LOG_ERROR("read: %d", errno
);
227 return ERROR_SERVER_REMOTE_CLOSED
;
230 return ERROR_SERVER_REMOTE_CLOSED
;
232 LOG_ERROR("read: %s", strerror(errno
));
234 return ERROR_SERVER_REMOTE_CLOSED
;
239 #ifdef _DEBUG_GDB_IO_
240 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
241 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
242 debug_buffer
[gdb_con
->buf_cnt
] = 0;
243 LOG_DEBUG("received '%s'", debug_buffer
);
247 gdb_con
->buf_p
= gdb_con
->buffer
;
249 *next_char
= *(gdb_con
->buf_p
++);
250 if (gdb_con
->buf_cnt
> 0)
251 connection
->input_pending
= 1;
253 connection
->input_pending
= 0;
254 #ifdef _DEBUG_GDB_IO_
255 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
262 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
263 * held in registers in the inner loop.
265 * For small caches and embedded systems this is important!
267 static inline int gdb_get_char_fast(struct connection
*connection
, int* next_char
, char **buf_p
, int *buf_cnt
)
269 int retval
= ERROR_OK
;
271 if ((*buf_cnt
)-- > 0)
273 *next_char
= **buf_p
;
276 connection
->input_pending
= 1;
278 connection
->input_pending
= 0;
280 #ifdef _DEBUG_GDB_IO_
281 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
287 struct gdb_connection
*gdb_con
= connection
->priv
;
288 gdb_con
->buf_p
= *buf_p
;
289 gdb_con
->buf_cnt
= *buf_cnt
;
290 retval
= gdb_get_char_inner(connection
, next_char
);
291 *buf_p
= gdb_con
->buf_p
;
292 *buf_cnt
= gdb_con
->buf_cnt
;
298 static int gdb_get_char(struct connection
*connection
, int* next_char
)
300 struct gdb_connection
*gdb_con
= connection
->priv
;
301 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
305 static int gdb_putback_char(struct connection
*connection
, int last_char
)
307 struct gdb_connection
*gdb_con
= connection
->priv
;
309 if (gdb_con
->buf_p
> gdb_con
->buffer
)
311 *(--gdb_con
->buf_p
) = last_char
;
316 LOG_ERROR("BUG: couldn't put character back");
322 /* The only way we can detect that the socket is closed is the first time
323 * we write to it, we will fail. Subsequent write operations will
324 * succeed. Shudder! */
325 static int gdb_write(struct connection
*connection
, void *data
, int len
)
327 struct gdb_connection
*gdb_con
= connection
->priv
;
329 return ERROR_SERVER_REMOTE_CLOSED
;
331 if (connection
->service
->type
== CONNECTION_PIPE
)
333 /* write to stdout */
334 if (write(STDOUT_FILENO
, data
, len
) == len
)
341 if (write_socket(connection
->fd
, data
, len
) == len
)
347 return ERROR_SERVER_REMOTE_CLOSED
;
350 static int gdb_put_packet_inner(struct connection
*connection
,
351 char *buffer
, int len
)
354 unsigned char my_checksum
= 0;
355 #ifdef _DEBUG_GDB_IO_
360 struct gdb_connection
*gdb_con
= connection
->priv
;
362 for (i
= 0; i
< len
; i
++)
363 my_checksum
+= buffer
[i
];
365 #ifdef _DEBUG_GDB_IO_
367 * At this point we should have nothing in the input queue from GDB,
368 * however sometimes '-' is sent even though we've already received
369 * an ACK (+) for everything we've sent off.
374 retval
= check_pending(connection
, 0, &gotdata
);
375 if (retval
!= ERROR_OK
)
379 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
382 /* fix a problem with some IAR tools */
383 gdb_putback_char(connection
, reply
);
384 LOG_DEBUG("Unexpected start of new packet");
388 LOG_WARNING("Discard unexpected char %c", reply
);
394 #ifdef _DEBUG_GDB_IO_
395 debug_buffer
= malloc(len
+ 1);
396 memcpy(debug_buffer
, buffer
, len
);
397 debug_buffer
[len
] = 0;
398 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
402 char local_buffer
[1024];
403 local_buffer
[0] = '$';
404 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
406 /* performance gain on smaller packets by only a single call to gdb_write() */
407 memcpy(local_buffer
+ 1, buffer
, len
++);
408 local_buffer
[len
++] = '#';
409 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
410 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
411 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
418 /* larger packets are transmitted directly from caller supplied buffer
419 by several calls to gdb_write() to avoid dynamic allocation */
420 local_buffer
[1] = '#';
421 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
422 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
423 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
427 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
431 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
437 if (gdb_con
->noack_mode
)
440 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
445 else if (reply
== '-')
447 /* Stop sending output packets for now */
448 log_remove_callback(gdb_log_callback
, connection
);
449 LOG_WARNING("negative reply, retrying");
451 else if (reply
== 0x3)
454 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
458 else if (reply
== '-')
460 /* Stop sending output packets for now */
461 log_remove_callback(gdb_log_callback
, connection
);
462 LOG_WARNING("negative reply, retrying");
464 else if (reply
== '$') {
465 LOG_ERROR("GDB missing ack(1) - assumed good");
466 gdb_putback_char(connection
, reply
);
470 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
472 return ERROR_SERVER_REMOTE_CLOSED
;
475 else if (reply
== '$') {
476 LOG_ERROR("GDB missing ack(2) - assumed good");
477 gdb_putback_char(connection
, reply
);
482 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
484 return ERROR_SERVER_REMOTE_CLOSED
;
488 return ERROR_SERVER_REMOTE_CLOSED
;
493 static int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
495 struct gdb_connection
*gdb_con
= connection
->priv
;
497 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
500 /* we sent some data, reset timer for keep alive messages */
506 static __inline__
int fetch_packet(struct connection
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
508 unsigned char my_checksum
= 0;
511 int retval
= ERROR_OK
;
513 struct gdb_connection
*gdb_con
= connection
->priv
;
518 /* move this over into local variables to use registers and give the
519 * more freedom to optimize */
520 char *buf_p
= gdb_con
->buf_p
;
521 int buf_cnt
= gdb_con
->buf_cnt
;
525 /* The common case is that we have an entire packet with no escape chars.
526 * We need to leave at least 2 bytes in the buffer to have
527 * gdb_get_char() update various bits and bobs correctly.
529 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
))
531 /* The compiler will struggle a bit with constant propagation and
532 * aliasing, so we help it by showing that these values do not
533 * change inside the loop
537 int run
= buf_cnt
- 2;
544 if (character
== '#')
546 /* Danger! character can be '#' when esc is
547 * used so we need an explicit boolean for done here.
553 if (character
== '}')
555 /* data transmitted in binary mode (X packet)
556 * uses 0x7d as escape character */
557 my_checksum
+= character
& 0xff;
560 my_checksum
+= character
& 0xff;
561 buffer
[count
++] = (character
^ 0x20) & 0xff;
565 my_checksum
+= character
& 0xff;
566 buffer
[count
++] = character
& 0xff;
576 LOG_ERROR("packet buffer too small");
577 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
581 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
582 if (retval
!= ERROR_OK
)
585 if (character
== '#')
588 if (character
== '}')
590 /* data transmitted in binary mode (X packet)
591 * uses 0x7d as escape character */
592 my_checksum
+= character
& 0xff;
594 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
595 if (retval
!= ERROR_OK
)
598 my_checksum
+= character
& 0xff;
599 buffer
[count
++] = (character
^ 0x20) & 0xff;
603 my_checksum
+= character
& 0xff;
604 buffer
[count
++] = character
& 0xff;
608 gdb_con
->buf_p
= buf_p
;
609 gdb_con
->buf_cnt
= buf_cnt
;
611 if (retval
!= ERROR_OK
)
616 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
618 checksum
[0] = character
;
619 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
621 checksum
[1] = character
;
626 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
632 static int gdb_get_packet_inner(struct connection
*connection
,
633 char *buffer
, int *len
)
637 struct gdb_connection
*gdb_con
= connection
->priv
;
643 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
646 #ifdef _DEBUG_GDB_IO_
647 LOG_DEBUG("character: '%c'", character
);
655 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
656 * in case anyone tries to debug why they receive this warning every time */
657 LOG_WARNING("acknowledgment received, but no packet pending");
660 LOG_WARNING("negative acknowledgment, but no packet pending");
667 LOG_WARNING("ignoring character 0x%x", character
);
670 } while (character
!= '$');
675 /* explicit code expansion here to get faster inlined code in -O3 by not
676 * calculating checksum
678 if (gdb_con
->noack_mode
)
680 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
684 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
688 if (gdb_con
->noack_mode
)
690 /* checksum is not checked in noack mode */
695 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
703 return ERROR_SERVER_REMOTE_CLOSED
;
708 static int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
710 struct gdb_connection
*gdb_con
= connection
->priv
;
712 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
717 static int gdb_output_con(struct connection
*connection
, const char* line
)
722 bin_size
= strlen(line
);
724 hex_buffer
= malloc(bin_size
*2 + 2);
725 if (hex_buffer
== NULL
)
726 return ERROR_GDB_BUFFER_TOO_SMALL
;
729 for (i
= 0; i
< bin_size
; i
++)
730 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
731 hex_buffer
[bin_size
*2 + 1] = 0;
733 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
739 static int gdb_output(struct command_context
*context
, const char* line
)
741 /* this will be dumped to the log and also sent as an O packet if possible */
742 LOG_USER_N("%s", line
);
747 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
749 struct gdb_connection
*gdb_connection
= connection
->priv
;
751 /* In the GDB protocol when we are stepping or continuing execution,
752 * we have a lingering reply. Upon receiving a halted event
753 * when we have that lingering packet, we reply to the original
754 * step or continue packet.
756 * Executing monitor commands can bring the target in and
757 * out of the running state so we'll see lots of TARGET_EVENT_XXX
758 * that are to be ignored.
760 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
765 /* stop forwarding log packets! */
766 log_remove_callback(gdb_log_callback
, connection
);
768 if (gdb_connection
->ctrl_c
)
771 gdb_connection
->ctrl_c
= 0;
775 signal
= gdb_last_signal(target
);
778 snprintf(sig_reply
, 4, "T%2.2x", signal
);
779 gdb_put_packet(connection
, sig_reply
, 3);
780 gdb_connection
->frontend_state
= TARGET_HALTED
;
784 static int gdb_target_callback_event_handler(struct target
*target
,
785 enum target_event event
, void *priv
)
788 struct connection
*connection
= priv
;
790 target_handle_event(target
, event
);
793 case TARGET_EVENT_GDB_HALT
:
794 gdb_frontend_halted(target
, connection
);
796 case TARGET_EVENT_HALTED
:
797 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
799 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
800 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
801 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
813 static int gdb_new_connection(struct connection
*connection
)
815 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
816 struct gdb_service
*gdb_service
= connection
->service
->priv
;
820 connection
->priv
= gdb_connection
;
822 /* initialize gdb connection information */
823 gdb_connection
->buf_p
= gdb_connection
->buffer
;
824 gdb_connection
->buf_cnt
= 0;
825 gdb_connection
->ctrl_c
= 0;
826 gdb_connection
->frontend_state
= TARGET_HALTED
;
827 gdb_connection
->vflash_image
= NULL
;
828 gdb_connection
->closed
= 0;
829 gdb_connection
->busy
= 0;
830 gdb_connection
->noack_mode
= 0;
831 gdb_connection
->sync
= true;
832 gdb_connection
->mem_write_error
= false;
834 /* send ACK to GDB for debug request */
835 gdb_write(connection
, "+", 1);
837 /* output goes through gdb connection */
838 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
840 /* we must remove all breakpoints registered to the target as a previous
841 * GDB session could leave dangling breakpoints if e.g. communication
844 breakpoint_clear_target(gdb_service
->target
);
845 watchpoint_clear_target(gdb_service
->target
);
847 /* register callback to be informed about target events */
848 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
850 /* remove the initial ACK from the incoming buffer */
851 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
854 /* FIX!!!??? would we actually ever receive a + here???
857 if (initial_ack
!= '+')
858 gdb_putback_char(connection
, initial_ack
);
859 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
861 if (gdb_use_memory_map
)
863 /* Connect must fail if the memory map can't be set up correctly.
865 * This will cause an auto_probe to be invoked, which is either
866 * a no-op or it will fail when the target isn't ready(e.g. not halted).
869 for (i
= 0; i
< flash_get_bank_count(); i
++)
871 struct flash_bank
*p
;
872 retval
= get_flash_bank_by_num(i
, &p
);
873 if (retval
!= ERROR_OK
)
875 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect.");
881 gdb_actual_connections
++;
882 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
883 gdb_actual_connections
,
884 target_name(gdb_service
->target
),
885 target_state_name(gdb_service
->target
));
890 static int gdb_connection_closed(struct connection
*connection
)
892 struct gdb_service
*gdb_service
= connection
->service
->priv
;
893 struct gdb_connection
*gdb_connection
= connection
->priv
;
895 /* we're done forwarding messages. Tear down callback before
896 * cleaning up connection.
898 log_remove_callback(gdb_log_callback
, connection
);
900 gdb_actual_connections
--;
901 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
902 target_name(gdb_service
->target
),
903 target_state_name(gdb_service
->target
),
904 gdb_actual_connections
);
906 /* see if an image built with vFlash commands is left */
907 if (gdb_connection
->vflash_image
)
909 image_close(gdb_connection
->vflash_image
);
910 free(gdb_connection
->vflash_image
);
911 gdb_connection
->vflash_image
= NULL
;
914 /* if this connection registered a debug-message receiver delete it */
915 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
917 if (connection
->priv
)
919 free(connection
->priv
);
920 connection
->priv
= NULL
;
924 LOG_ERROR("BUG: connection->priv == NULL");
928 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
930 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
932 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
937 static void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
940 snprintf(err
, 4, "E%2.2X", the_error
);
941 gdb_put_packet(connection
, err
, 3);
944 static int gdb_last_signal_packet(struct connection
*connection
,
945 struct target
*target
, char* packet
, int packet_size
)
950 signal
= gdb_last_signal(target
);
952 snprintf(sig_reply
, 4, "S%2.2x", signal
);
953 gdb_put_packet(connection
, sig_reply
, 3);
958 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
960 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
963 return len
- 1 - pos
;
966 /* Convert register to string of bytes. NB! The # of bits in the
967 * register might be non-divisible by 8(a byte), in which
968 * case an entire byte is shown.
970 * NB! the format on the wire is the target endianness
972 * The format of reg->value is little endian
975 static void gdb_str_to_target(struct target
*target
,
976 char *tstr
, struct reg
*reg
)
983 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
985 for (i
= 0; i
< buf_len
; i
++)
987 int j
= gdb_reg_pos(target
, i
, buf_len
);
988 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
989 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
993 static int hextoint(int c
)
1004 LOG_ERROR("BUG: invalid register value %08x", c
);
1008 /* copy over in register buffer */
1009 static void gdb_target_to_reg(struct target
*target
,
1010 char *tstr
, int str_len
, uint8_t *bin
)
1014 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1019 for (i
= 0; i
< str_len
; i
+= 2)
1021 uint8_t t
= hextoint(tstr
[i
]) << 4;
1022 t
|= hextoint(tstr
[i
+ 1]);
1024 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
1029 static int gdb_get_registers_packet(struct connection
*connection
,
1030 struct target
*target
, char* packet
, int packet_size
)
1032 struct reg
**reg_list
;
1035 int reg_packet_size
= 0;
1040 #ifdef _DEBUG_GDB_IO_
1044 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1046 return gdb_error(connection
, retval
);
1049 for (i
= 0; i
< reg_list_size
; i
++)
1051 reg_packet_size
+= reg_list
[i
]->size
;
1054 reg_packet
= malloc(DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1055 reg_packet_p
= reg_packet
;
1057 for (i
= 0; i
< reg_list_size
; i
++)
1059 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1060 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1063 #ifdef _DEBUG_GDB_IO_
1066 reg_packet_p
= strndup(reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1067 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
1072 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1080 static int gdb_set_registers_packet(struct connection
*connection
,
1081 struct target
*target
, char *packet
, int packet_size
)
1084 struct reg
**reg_list
;
1089 #ifdef _DEBUG_GDB_IO_
1093 /* skip command character */
1097 if (packet_size
% 2)
1099 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1100 return ERROR_SERVER_REMOTE_CLOSED
;
1103 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1105 return gdb_error(connection
, retval
);
1109 for (i
= 0; i
< reg_list_size
; i
++)
1112 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1114 if (packet_p
+ chars
> packet
+ packet_size
)
1116 LOG_ERROR("BUG: register packet is too small for registers");
1119 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1120 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1122 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1124 /* advance packet pointer */
1131 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1134 gdb_put_packet(connection
, "OK", 2);
1139 static int gdb_get_register_packet(struct connection
*connection
,
1140 struct target
*target
, char *packet
, int packet_size
)
1143 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1144 struct reg
**reg_list
;
1148 #ifdef _DEBUG_GDB_IO_
1152 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1154 return gdb_error(connection
, retval
);
1157 if (reg_list_size
<= reg_num
)
1159 LOG_ERROR("gdb requested a non-existing register");
1163 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1165 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1167 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1175 static int gdb_set_register_packet(struct connection
*connection
,
1176 struct target
*target
, char *packet
, int packet_size
)
1180 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1181 struct reg
**reg_list
;
1187 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1189 return gdb_error(connection
, retval
);
1192 if (reg_list_size
< reg_num
)
1194 LOG_ERROR("gdb requested a non-existing register");
1195 return ERROR_SERVER_REMOTE_CLOSED
;
1198 if (*separator
!= '=')
1200 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1201 return ERROR_SERVER_REMOTE_CLOSED
;
1204 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1205 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1206 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1208 /* fix!!! add some sanity checks on packet size here */
1210 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1212 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1214 gdb_put_packet(connection
, "OK", 2);
1222 static int gdb_error(struct connection
*connection
, int retval
)
1226 case ERROR_TARGET_DATA_ABORT
:
1227 gdb_send_error(connection
, EIO
);
1229 case ERROR_TARGET_TRANSLATION_FAULT
:
1230 gdb_send_error(connection
, EFAULT
);
1232 case ERROR_TARGET_UNALIGNED_ACCESS
:
1233 gdb_send_error(connection
, EFAULT
);
1235 case ERROR_TARGET_NOT_HALTED
:
1236 gdb_send_error(connection
, EFAULT
);
1239 /* This could be that the target reset itself. */
1240 LOG_ERROR("unexpected error %i", retval
);
1241 gdb_send_error(connection
, EFAULT
);
1248 /* We don't have to worry about the default 2 second timeout for GDB packets,
1249 * because GDB breaks up large memory reads into smaller reads.
1251 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1253 static int gdb_read_memory_packet(struct connection
*connection
,
1254 struct target
*target
, char *packet
, int packet_size
)
1263 int retval
= ERROR_OK
;
1265 /* skip command character */
1268 addr
= strtoul(packet
, &separator
, 16);
1270 if (*separator
!= ',')
1272 LOG_ERROR("incomplete read memory packet received, dropping connection");
1273 return ERROR_SERVER_REMOTE_CLOSED
;
1276 len
= strtoul(separator
+ 1, NULL
, 16);
1278 buffer
= malloc(len
);
1280 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1282 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1284 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1286 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1287 * At some point this might be fixed in GDB, in which case this code can be removed.
1289 * OpenOCD developers are acutely aware of this problem, but there is nothing
1290 * gained by involving the user in this problem that hopefully will get resolved
1293 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1295 * For now, the default is to fix up things to make current GDB versions work.
1296 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1298 memset(buffer
, 0, len
);
1302 if (retval
== ERROR_OK
)
1304 hex_buffer
= malloc(len
* 2 + 1);
1307 for (i
= 0; i
< len
; i
++)
1309 uint8_t t
= buffer
[i
];
1310 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1311 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1314 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1320 retval
= gdb_error(connection
, retval
);
1328 static int gdb_write_memory_packet(struct connection
*connection
,
1329 struct target
*target
, char *packet
, int packet_size
)
1340 /* skip command character */
1343 addr
= strtoul(packet
, &separator
, 16);
1345 if (*separator
!= ',')
1347 LOG_ERROR("incomplete write memory packet received, dropping connection");
1348 return ERROR_SERVER_REMOTE_CLOSED
;
1351 len
= strtoul(separator
+ 1, &separator
, 16);
1353 if (*(separator
++) != ':')
1355 LOG_ERROR("incomplete write memory packet received, dropping connection");
1356 return ERROR_SERVER_REMOTE_CLOSED
;
1359 buffer
= malloc(len
);
1361 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1363 for (i
= 0; i
< len
; i
++)
1366 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1370 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1372 if (retval
== ERROR_OK
)
1374 gdb_put_packet(connection
, "OK", 2);
1378 retval
= gdb_error(connection
, retval
);
1386 static int gdb_write_memory_binary_packet(struct connection
*connection
,
1387 struct target
*target
, char *packet
, int packet_size
)
1393 int retval
= ERROR_OK
;
1395 /* skip command character */
1398 addr
= strtoul(packet
, &separator
, 16);
1400 if (*separator
!= ',')
1402 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1403 return ERROR_SERVER_REMOTE_CLOSED
;
1406 len
= strtoul(separator
+ 1, &separator
, 16);
1408 if (*(separator
++) != ':')
1410 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1411 return ERROR_SERVER_REMOTE_CLOSED
;
1414 struct gdb_connection
*gdb_connection
= connection
->priv
;
1416 if (gdb_connection
->mem_write_error
)
1418 retval
= ERROR_FAIL
;
1419 /* now that we have reported the memory write error, we can clear the condition */
1420 gdb_connection
->mem_write_error
= false;
1423 /* By replying the packet *immediately* GDB will send us a new packet
1424 * while we write the last one to the target.
1426 if (retval
== ERROR_OK
)
1428 gdb_put_packet(connection
, "OK", 2);
1432 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1438 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1440 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1441 if (retval
!= ERROR_OK
)
1443 gdb_connection
->mem_write_error
= true;
1450 static int gdb_step_continue_packet(struct connection
*connection
,
1451 struct target
*target
, char *packet
, int packet_size
)
1454 uint32_t address
= 0x0;
1455 int retval
= ERROR_OK
;
1459 if (packet_size
> 1)
1461 packet
[packet_size
] = 0;
1462 address
= strtoul(packet
+ 1, NULL
, 16);
1469 if (packet
[0] == 'c')
1471 LOG_DEBUG("continue");
1472 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1473 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1475 else if (packet
[0] == 's')
1478 /* step at current or address, don't handle breakpoints */
1479 retval
= target_step(target
, current
, address
, 0);
1484 static int gdb_breakpoint_watchpoint_packet(struct connection
*connection
,
1485 struct target
*target
, char *packet
, int packet_size
)
1488 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1489 enum watchpoint_rw wp_type
= WPT_READ
/* dummy init to avoid warning */;
1497 type
= strtoul(packet
+ 1, &separator
, 16);
1499 if (type
== 0) /* memory breakpoint */
1500 bp_type
= BKPT_SOFT
;
1501 else if (type
== 1) /* hardware breakpoint */
1502 bp_type
= BKPT_HARD
;
1503 else if (type
== 2) /* write watchpoint */
1504 wp_type
= WPT_WRITE
;
1505 else if (type
== 3) /* read watchpoint */
1507 else if (type
== 4) /* access watchpoint */
1508 wp_type
= WPT_ACCESS
;
1511 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type
);
1512 return ERROR_SERVER_REMOTE_CLOSED
;
1516 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1518 bp_type
= gdb_breakpoint_override_type
;
1521 if (*separator
!= ',')
1523 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1524 return ERROR_SERVER_REMOTE_CLOSED
;
1527 address
= strtoul(separator
+ 1, &separator
, 16);
1529 if (*separator
!= ',')
1531 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1532 return ERROR_SERVER_REMOTE_CLOSED
;
1535 size
= strtoul(separator
+ 1, &separator
, 16);
1541 if (packet
[0] == 'Z')
1543 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1545 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1550 gdb_put_packet(connection
, "OK", 2);
1555 breakpoint_remove(target
, address
);
1556 gdb_put_packet(connection
, "OK", 2);
1563 if (packet
[0] == 'Z')
1565 if ((retval
= watchpoint_add(target
, address
, size
, wp_type
, 0, 0xffffffffu
)) != ERROR_OK
)
1567 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1572 gdb_put_packet(connection
, "OK", 2);
1577 watchpoint_remove(target
, address
);
1578 gdb_put_packet(connection
, "OK", 2);
1589 /* print out a string and allocate more space as needed,
1590 * mainly used for XML at this point
1592 static void xml_printf(int *retval
, char **xml
, int *pos
, int *size
,
1593 const char *fmt
, ...)
1595 if (*retval
!= ERROR_OK
)
1603 if ((*xml
== NULL
) || (!first
))
1605 /* start by 0 to exercise all the code paths.
1606 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1608 *size
= *size
* 2 + 2;
1610 *xml
= realloc(*xml
, *size
);
1615 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1623 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1625 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1630 /* there was just enough or not enough space, allocate more. */
1635 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1639 /* Extract and NUL-terminate the annex. */
1641 while (*buf
&& *buf
!= ':')
1647 /* After the read marker and annex, qXfer looks like a
1648 * traditional 'm' packet. */
1650 *ofs
= strtoul(buf
, &separator
, 16);
1652 if (*separator
!= ',')
1655 *len
= strtoul(separator
+ 1, NULL
, 16);
1660 static int compare_bank (const void * a
, const void * b
)
1662 struct flash_bank
*b1
, *b2
;
1663 b1
=*((struct flash_bank
**)a
);
1664 b2
=*((struct flash_bank
**)b
);
1666 if (b1
->base
== b2
->base
)
1669 } else if (b1
->base
> b2
->base
)
1678 static int gdb_memory_map(struct connection
*connection
,
1679 struct target
*target
, char *packet
, int packet_size
)
1681 /* We get away with only specifying flash here. Regions that are not
1682 * specified are treated as if we provided no memory map(if not we
1683 * could detect the holes and mark them as RAM).
1684 * Normally we only execute this code once, but no big deal if we
1685 * have to regenerate it a couple of times.
1688 struct flash_bank
*p
;
1692 int retval
= ERROR_OK
;
1693 struct flash_bank
**banks
;
1697 uint32_t ram_start
= 0;
1700 /* skip command character */
1703 offset
= strtoul(packet
, &separator
, 16);
1704 length
= strtoul(separator
+ 1, &separator
, 16);
1706 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1708 /* Sort banks in ascending order. We need to report non-flash
1709 * memory as ram (or rather read/write) by default for GDB, since
1710 * it has no concept of non-cacheable read/write memory (i/o etc).
1712 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1713 * Current versions of GDB assume unlisted addresses are RAM...
1715 banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1717 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1718 retval
= get_flash_bank_by_num(i
, &p
);
1719 if (retval
!= ERROR_OK
)
1722 gdb_send_error(connection
, retval
);
1728 qsort(banks
, flash_get_bank_count(), sizeof(struct flash_bank
*),
1731 for (i
= 0; i
< flash_get_bank_count(); i
++) {
1733 unsigned sector_size
= 0;
1734 uint32_t start
, end
;
1738 end
= p
->base
+ p
->size
;
1740 if (ram_start
< p
->base
)
1741 xml_printf(&retval
, &xml
, &pos
, &size
,
1742 "<memory type=\"ram\" start=\"0x%x\" "
1743 "length=\"0x%x\"/>\n",
1744 ram_start
, p
->base
- ram_start
);
1746 /* Report adjacent groups of same-size sectors. So for
1747 * example top boot CFI flash will list an initial region
1748 * with several large sectors (maybe 128KB) and several
1749 * smaller ones at the end (maybe 32KB). STR7 will have
1750 * regions with 8KB, 32KB, and 64KB sectors; etc.
1752 for (j
= 0; j
< p
->num_sectors
; j
++) {
1755 /* Maybe start a new group of sectors. */
1756 if (sector_size
== 0) {
1757 start
= p
->base
+ p
->sectors
[j
].offset
;
1758 xml_printf(&retval
, &xml
, &pos
, &size
,
1759 "<memory type=\"flash\" "
1762 sector_size
= p
->sectors
[j
].size
;
1765 /* Does this finish a group of sectors?
1766 * If not, continue an already-started group.
1768 if (j
== p
->num_sectors
-1)
1769 group_len
= (p
->base
+ p
->size
) - start
;
1770 else if (p
->sectors
[j
+ 1].size
!= sector_size
)
1771 group_len
= p
->base
+ p
->sectors
[j
+ 1].offset
1776 xml_printf(&retval
, &xml
, &pos
, &size
,
1777 "length=\"0x%x\">\n"
1778 "<property name=\"blocksize\">"
1786 ram_start
= p
->base
+ p
->size
;
1790 xml_printf(&retval
, &xml
, &pos
, &size
,
1791 "<memory type=\"ram\" start=\"0x%x\" "
1792 "length=\"0x%x\"/>\n",
1793 ram_start
, 0-ram_start
);
1794 /* ELSE a flash chip could be at the very end of the 32 bit address
1795 * space, in which case ram_start will be precisely 0
1801 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1803 if (retval
!= ERROR_OK
) {
1804 gdb_send_error(connection
, retval
);
1808 if (offset
+ length
> pos
)
1809 length
= pos
- offset
;
1811 char *t
= malloc(length
+ 1);
1813 memcpy(t
+ 1, xml
+ offset
, length
);
1814 gdb_put_packet(connection
, t
, length
+ 1);
1821 static int gdb_query_packet(struct connection
*connection
,
1822 struct target
*target
, char *packet
, int packet_size
)
1824 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1825 struct gdb_connection
*gdb_connection
= connection
->priv
;
1827 if (strstr(packet
, "qRcmd,"))
1829 if (packet_size
> 6)
1833 cmd
= malloc((packet_size
- 6)/2 + 1);
1834 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1837 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1840 cmd
[(packet_size
- 6)/2] = 0x0;
1842 /* We want to print all debug output to GDB connection */
1843 log_add_callback(gdb_log_callback
, connection
);
1844 target_call_timer_callbacks_now();
1845 /* some commands need to know the GDB connection, make note of current
1846 * GDB connection. */
1847 current_gdb_connection
= gdb_connection
;
1848 command_run_line(cmd_ctx
, cmd
);
1849 current_gdb_connection
= NULL
;
1850 target_call_timer_callbacks_now();
1851 log_remove_callback(gdb_log_callback
, connection
);
1854 gdb_put_packet(connection
, "OK", 2);
1857 else if (strstr(packet
, "qCRC:"))
1859 if (packet_size
> 5)
1868 /* skip command character */
1871 addr
= strtoul(packet
, &separator
, 16);
1873 if (*separator
!= ',')
1875 LOG_ERROR("incomplete read memory packet received, dropping connection");
1876 return ERROR_SERVER_REMOTE_CLOSED
;
1879 len
= strtoul(separator
+ 1, NULL
, 16);
1881 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1883 if (retval
== ERROR_OK
)
1885 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1886 gdb_put_packet(connection
, gdb_reply
, 9);
1890 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1897 else if (strstr(packet
, "qSupported"))
1899 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1900 * disable qXfer:features:read for the moment */
1901 int retval
= ERROR_OK
;
1902 char *buffer
= NULL
;
1906 xml_printf(&retval
, &buffer
, &pos
, &size
,
1907 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1908 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1910 if (retval
!= ERROR_OK
)
1912 gdb_send_error(connection
, 01);
1916 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1921 else if (strstr(packet
, "qXfer:memory-map:read::")
1922 && (flash_get_bank_count() > 0))
1923 return gdb_memory_map(connection
, target
, packet
, packet_size
);
1924 else if (strstr(packet
, "qXfer:features:read:"))
1929 int retval
= ERROR_OK
;
1932 unsigned int length
;
1935 /* skip command character */
1938 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1940 gdb_send_error(connection
, 01);
1944 if (strcmp(annex
, "target.xml") != 0)
1946 gdb_send_error(connection
, 01);
1950 xml_printf(&retval
, &xml
, &pos
, &size
, \
1951 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1953 if (retval
!= ERROR_OK
)
1955 gdb_send_error(connection
, retval
);
1959 gdb_put_packet(connection
, xml
, strlen(xml
));
1964 else if (strstr(packet
, "QStartNoAckMode"))
1966 gdb_connection
->noack_mode
= 1;
1967 gdb_put_packet(connection
, "OK", 2);
1971 gdb_put_packet(connection
, "", 0);
1975 static int gdb_v_packet(struct connection
*connection
,
1976 struct target
*target
, char *packet
, int packet_size
)
1978 struct gdb_connection
*gdb_connection
= connection
->priv
;
1979 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1982 /* if flash programming disabled - send a empty reply */
1984 if (gdb_flash_program
== 0)
1986 gdb_put_packet(connection
, "", 0);
1990 if (strstr(packet
, "vFlashErase:"))
1993 unsigned long length
;
1995 char *parse
= packet
+ 12;
1998 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1999 return ERROR_SERVER_REMOTE_CLOSED
;
2002 addr
= strtoul(parse
, &parse
, 16);
2004 if (*(parse
++) != ',' || *parse
== '\0')
2006 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2007 return ERROR_SERVER_REMOTE_CLOSED
;
2010 length
= strtoul(parse
, &parse
, 16);
2014 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2015 return ERROR_SERVER_REMOTE_CLOSED
;
2018 /* assume all sectors need erasing - stops any problems
2019 * when flash_write is called multiple times */
2022 /* perform any target specific operations before the erase */
2023 target_call_event_callbacks(gdb_service
->target
,
2024 TARGET_EVENT_GDB_FLASH_ERASE_START
);
2026 /* vFlashErase:addr,length messages require region start and
2027 * end to be "block" aligned ... if padding is ever needed,
2028 * GDB will have become dangerously confused.
2030 result
= flash_erase_address_range(gdb_service
->target
,
2031 false, addr
, length
);
2033 /* perform any target specific operations after the erase */
2034 target_call_event_callbacks(gdb_service
->target
,
2035 TARGET_EVENT_GDB_FLASH_ERASE_END
);
2038 if (result
!= ERROR_OK
)
2040 /* GDB doesn't evaluate the actual error number returned,
2041 * treat a failed erase as an I/O error
2043 gdb_send_error(connection
, EIO
);
2044 LOG_ERROR("flash_erase returned %i", result
);
2047 gdb_put_packet(connection
, "OK", 2);
2052 if (strstr(packet
, "vFlashWrite:"))
2056 unsigned long length
;
2057 char *parse
= packet
+ 12;
2061 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2062 return ERROR_SERVER_REMOTE_CLOSED
;
2064 addr
= strtoul(parse
, &parse
, 16);
2065 if (*(parse
++) != ':')
2067 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2068 return ERROR_SERVER_REMOTE_CLOSED
;
2070 length
= packet_size
- (parse
- packet
);
2072 /* create a new image if there isn't already one */
2073 if (gdb_connection
->vflash_image
== NULL
)
2075 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
2076 image_open(gdb_connection
->vflash_image
, "", "build");
2079 /* create new section with content from packet buffer */
2080 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
2085 gdb_put_packet(connection
, "OK", 2);
2090 if (!strcmp(packet
, "vFlashDone"))
2094 /* process the flashing buffer. No need to erase as GDB
2095 * always issues a vFlashErase first. */
2096 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
2097 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
2098 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
2099 if (result
!= ERROR_OK
)
2101 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
2102 gdb_put_packet(connection
, "E.memtype", 9);
2104 gdb_send_error(connection
, EIO
);
2108 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
2109 gdb_put_packet(connection
, "OK", 2);
2112 image_close(gdb_connection
->vflash_image
);
2113 free(gdb_connection
->vflash_image
);
2114 gdb_connection
->vflash_image
= NULL
;
2119 gdb_put_packet(connection
, "", 0);
2123 static int gdb_detach(struct connection
*connection
, struct target
*target
)
2125 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2127 target_call_event_callbacks(gdb_service
->target
,
2128 TARGET_EVENT_GDB_DETACH
);
2130 return gdb_put_packet(connection
, "OK", 2);
2133 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2134 const char *function
, const char *string
)
2136 struct connection
*connection
= priv
;
2137 struct gdb_connection
*gdb_con
= connection
->priv
;
2141 /* do not reply this using the O packet */
2145 gdb_output_con(connection
, string
);
2148 static void gdb_sig_halted(struct connection
*connection
)
2151 snprintf(sig_reply
, 4, "T%2.2x", 2);
2152 gdb_put_packet(connection
, sig_reply
, 3);
2156 static int gdb_input_inner(struct connection
*connection
)
2158 /* Do not allocate this on the stack */
2159 static char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2161 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2162 struct target
*target
= gdb_service
->target
;
2163 char *packet
= gdb_packet_buffer
;
2166 struct gdb_connection
*gdb_con
= connection
->priv
;
2167 static int extended_protocol
= 0;
2169 /* drain input buffer */
2172 packet_size
= GDB_BUFFER_SIZE
-1;
2173 retval
= gdb_get_packet(connection
, packet
, &packet_size
);
2174 if (retval
!= ERROR_OK
)
2177 /* terminate with zero */
2178 packet
[packet_size
] = 0;
2180 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2181 if (packet
[0] == 'X') {
2182 // binary packets spew junk into the debug log stream
2185 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2189 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2191 LOG_DEBUG("received packet: '%s'", packet
);
2195 if (packet_size
> 0)
2201 /* Hct... -- set thread
2202 * we don't have threads, send empty reply */
2203 gdb_put_packet(connection
, NULL
, 0);
2207 retval
= gdb_query_packet(connection
,
2212 retval
= gdb_get_registers_packet(
2214 packet
, packet_size
);
2217 retval
= gdb_set_registers_packet(
2219 packet
, packet_size
);
2222 retval
= gdb_get_register_packet(
2224 packet
, packet_size
);
2227 retval
= gdb_set_register_packet(
2229 packet
, packet_size
);
2232 retval
= gdb_read_memory_packet(
2234 packet
, packet_size
);
2237 retval
= gdb_write_memory_packet(
2239 packet
, packet_size
);
2243 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2246 gdb_last_signal_packet(
2248 packet
, packet_size
);
2253 int retval
= ERROR_OK
;
2255 struct gdb_connection
*gdb_con
= connection
->priv
;
2256 log_add_callback(gdb_log_callback
, connection
);
2258 if (gdb_con
->mem_write_error
)
2260 LOG_ERROR("Memory write failure!");
2262 /* now that we have reported the memory write error, we can clear the condition */
2263 gdb_con
->mem_write_error
= false;
2266 bool nostep
= false;
2267 bool already_running
= false;
2268 if (target
->state
== TARGET_RUNNING
)
2270 LOG_WARNING("WARNING! The target is already running. "
2271 "All changes GDB did to registers will be discarded! "
2272 "Waiting for target to halt.");
2273 already_running
= true;
2274 } else if (target
->state
!= TARGET_HALTED
)
2276 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2278 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2280 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2281 * sent by GDB first to OpenOCD, thus defeating the check to
2282 * make only the single stepping have the sync feature...
2285 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2287 gdb_con
->sync
= false;
2289 if ((retval
!=ERROR_OK
) || (!already_running
&& nostep
))
2291 /* Either the target isn't in the halted state, then we can't
2292 * step/continue. This might be early setup, etc.
2294 * Or we want to allow GDB to pick up a fresh set of
2295 * register values without modifying the target state.
2298 gdb_sig_halted(connection
);
2300 /* stop forwarding log packets! */
2301 log_remove_callback(gdb_log_callback
, connection
);
2304 /* We're running/stepping, in which case we can
2305 * forward log output until the target is halted
2307 gdb_con
->frontend_state
= TARGET_RUNNING
;
2308 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2310 if (!already_running
)
2312 int retval
= gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2313 if (retval
!= ERROR_OK
)
2315 /* we'll never receive a halted condition... issue a false one.. */
2316 gdb_frontend_halted(target
, connection
);
2323 retval
= gdb_v_packet(
2325 packet
, packet_size
);
2328 retval
= gdb_detach(connection
, target
);
2329 extended_protocol
= 0;
2332 retval
= gdb_write_memory_binary_packet(
2334 packet
, packet_size
);
2335 if (retval
!= ERROR_OK
)
2339 if (extended_protocol
!= 0)
2341 gdb_put_packet(connection
, "OK", 2);
2342 return ERROR_SERVER_REMOTE_CLOSED
;
2344 /* handle extended remote protocol */
2345 extended_protocol
= 1;
2346 gdb_put_packet(connection
, "OK", 2);
2349 /* handle extended restart packet */
2350 breakpoint_clear_target(gdb_service
->target
);
2351 watchpoint_clear_target(gdb_service
->target
);
2352 command_run_linef(connection
->cmd_ctx
,
2353 "ocd_gdb_restart %s",
2354 target_name(target
));
2357 /* ignore unknown packets */
2358 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2359 gdb_put_packet(connection
, NULL
, 0);
2363 /* if a packet handler returned an error, exit input loop */
2364 if (retval
!= ERROR_OK
)
2368 if (gdb_con
->ctrl_c
)
2370 if (target
->state
== TARGET_RUNNING
)
2372 retval
= target_halt(target
);
2373 if (retval
!= ERROR_OK
)
2375 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2377 gdb_con
->ctrl_c
= 0;
2380 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2381 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2385 } while (gdb_con
->buf_cnt
> 0);
2390 static int gdb_input(struct connection
*connection
)
2392 int retval
= gdb_input_inner(connection
);
2393 struct gdb_connection
*gdb_con
= connection
->priv
;
2394 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2397 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2398 if (gdb_con
->closed
)
2399 return ERROR_SERVER_REMOTE_CLOSED
;
2401 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2405 static int gdb_target_start(struct target
*target
, uint16_t port
)
2407 bool use_pipes
= 0 == port
;
2408 struct gdb_service
*gdb_service
= malloc(sizeof(struct gdb_service
));
2409 if (NULL
== gdb_service
)
2412 gdb_service
->target
= target
;
2414 add_service("gdb", use_pipes
? CONNECTION_PIPE
: CONNECTION_TCP
,
2415 port
, 1, &gdb_new_connection
, &gdb_input
,
2416 &gdb_connection_closed
, gdb_service
);
2418 const char *name
= target_name(target
);
2420 LOG_DEBUG("gdb service for target '%s' using pipes", name
);
2422 LOG_DEBUG("gdb service for target '%s' on TCP port %u", name
, port
);
2426 static int gdb_target_add_one(struct target
*target
)
2428 if (gdb_port
== 0 && server_use_pipes
== 0)
2430 LOG_INFO("gdb port disabled");
2433 if (0 == gdb_port_next
)
2434 gdb_port_next
= gdb_port
;
2436 bool use_pipes
= server_use_pipes
;
2437 static bool server_started_with_pipes
= false;
2438 if (server_started_with_pipes
)
2440 LOG_WARNING("gdb service permits one target when using pipes");
2447 int e
= gdb_target_start(target
, use_pipes
? 0 : gdb_port_next
);
2450 server_started_with_pipes
|= use_pipes
;
2456 int gdb_target_add_all(struct target
*target
)
2460 LOG_WARNING("gdb services need one or more targets defined");
2464 while (NULL
!= target
)
2466 int retval
= gdb_target_add_one(target
);
2467 if (ERROR_OK
!= retval
)
2470 target
= target
->next
;
2476 COMMAND_HANDLER(handle_gdb_sync_command
)
2480 return ERROR_COMMAND_SYNTAX_ERROR
;
2483 if (current_gdb_connection
== NULL
)
2485 command_print(CMD_CTX
,
2486 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2490 current_gdb_connection
->sync
= true;
2495 /* daemon configuration command gdb_port */
2496 COMMAND_HANDLER(handle_gdb_port_command
)
2498 int retval
= CALL_COMMAND_HANDLER(server_port_command
, &gdb_port
);
2499 if (ERROR_OK
== retval
)
2500 gdb_port_next
= gdb_port
;
2504 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2507 return ERROR_COMMAND_SYNTAX_ERROR
;
2509 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2513 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2516 return ERROR_COMMAND_SYNTAX_ERROR
;
2518 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2522 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2525 return ERROR_COMMAND_SYNTAX_ERROR
;
2527 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2531 /* gdb_breakpoint_override */
2532 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2537 } else if (CMD_ARGC
== 1)
2539 gdb_breakpoint_override
= 1;
2540 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2542 gdb_breakpoint_override_type
= BKPT_HARD
;
2543 } else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2545 gdb_breakpoint_override_type
= BKPT_SOFT
;
2546 } else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2548 gdb_breakpoint_override
= 0;
2552 return ERROR_COMMAND_SYNTAX_ERROR
;
2554 if (gdb_breakpoint_override
)
2556 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2559 LOG_USER("breakpoint type is not overridden");
2565 static const struct command_registration gdb_command_handlers
[] = {
2568 .handler
= handle_gdb_sync_command
,
2569 .mode
= COMMAND_ANY
,
2570 .help
= "next stepi will return immediately allowing "
2571 "GDB to fetch register state without affecting "
2576 .handler
= handle_gdb_port_command
,
2577 .mode
= COMMAND_ANY
,
2578 .help
= "Display or specify base port on which to listen "
2579 "for incoming GDB connections. "
2580 "No arguments reports GDB port; zero disables.",
2581 .usage
= "[port_num]",
2584 .name
= "gdb_memory_map",
2585 .handler
= handle_gdb_memory_map_command
,
2586 .mode
= COMMAND_CONFIG
,
2587 .help
= "enable or disable memory map",
2588 .usage
= "('enable'|'disable')"
2591 .name
= "gdb_flash_program",
2592 .handler
= handle_gdb_flash_program_command
,
2593 .mode
= COMMAND_CONFIG
,
2594 .help
= "enable or disable flash program",
2595 .usage
= "('enable'|'disable')"
2598 .name
= "gdb_report_data_abort",
2599 .handler
= handle_gdb_report_data_abort_command
,
2600 .mode
= COMMAND_CONFIG
,
2601 .help
= "enable or disable reporting data aborts",
2602 .usage
= "('enable'|'disable')"
2605 .name
= "gdb_breakpoint_override",
2606 .handler
= handle_gdb_breakpoint_override_command
,
2607 .mode
= COMMAND_ANY
,
2608 .help
= "Display or specify type of breakpoint "
2609 "to be used by gdb 'break' commands.",
2610 .usage
= "('hard'|'soft'|'disable')"
2612 COMMAND_REGISTRATION_DONE
2615 int gdb_register_commands(struct command_context
*cmd_ctx
)
2617 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);