1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * Copyright (C) 2011 by Broadcom Corporation *
12 * Evan Hunter - ehunter@broadcom.com *
14 * Copyright (C) ST-Ericsson SA 2011 *
15 * michel.jaouen@stericsson.com : smp minimum support *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
37 #include <target/breakpoints.h>
38 #include <target/target_request.h>
39 #include <target/register.h>
41 #include <flash/nor/core.h>
42 #include "gdb_server.h"
43 #include <target/image.h>
44 #include <jtag/jtag.h>
45 #include "rtos/rtos.h"
46 #include "target/smp.h"
50 * GDB server implementation.
52 * This implements the GDB Remote Serial Protocol, over TCP connections,
53 * giving GDB access to the JTAG or other hardware debugging facilities
54 * found in most modern embedded processors.
57 /* private connection data for GDB */
58 struct gdb_connection {
59 char buffer[GDB_BUFFER_SIZE];
63 enum target_state frontend_state;
64 struct image *vflash_image;
68 /* set flag to true if you want the next stepi to return immediately.
69 * allowing GDB to pick up a fresh set of register values from the target
70 * without modifying the target state. */
72 /* We delay reporting memory write errors until next step/continue or memory
73 * write. This improves performance of gdb load significantly as the GDB packet
74 * can be replied immediately and a new GDB packet will be ready without delay
75 * (ca. 10% or so...). */
77 /* with extended-remote it seems we need to better emulate attach/detach.
78 * what this means is we reply with a W stop reply after a kill packet,
79 * normally we reply with a S reply via gdb_last_signal_packet.
80 * as a side note this behaviour only effects gdb > 6.8 */
85 #define _DEBUG_GDB_IO_
88 static struct gdb_connection *current_gdb_connection;
90 static int gdb_breakpoint_override;
91 static enum breakpoint_type gdb_breakpoint_override_type;
93 static int gdb_error(struct connection *connection, int retval);
94 static const char *gdb_port;
95 static const char *gdb_port_next;
97 static void gdb_log_callback(void *priv, const char *file, unsigned line,
98 const char *function, const char *string);
100 /* number of gdb connections, mainly to suppress gdb related debugging spam
101 * in helper/log.c when no gdb connections are actually active */
102 int gdb_actual_connections;
104 /* set if we are sending a memory map to gdb
105 * via qXfer:memory-map:read packet */
106 /* enabled by default*/
107 static int gdb_use_memory_map = 1;
108 /* enabled by default*/
109 static int gdb_flash_program = 1;
111 /* if set, data aborts cause an error to be reported in memory read packets
112 * see the code in gdb_read_memory_packet() for further explanations.
113 * Disabled by default.
115 static int gdb_report_data_abort;
117 static int gdb_last_signal(struct target *target)
119 switch (target->debug_reason) {
120 case DBG_REASON_DBGRQ:
121 return 0x2; /* SIGINT */
122 case DBG_REASON_BREAKPOINT:
123 case DBG_REASON_WATCHPOINT:
124 case DBG_REASON_WPTANDBKPT:
125 return 0x05; /* SIGTRAP */
126 case DBG_REASON_SINGLESTEP:
127 return 0x05; /* SIGTRAP */
128 case DBG_REASON_NOTHALTED:
129 return 0x0; /* no signal... shouldn't happen */
131 LOG_USER("undefined debug reason %d - target needs reset",
132 target->debug_reason);
137 static int check_pending(struct connection *connection,
138 int timeout_s, int *got_data)
140 /* a non-blocking socket will block if there is 0 bytes available on the socket,
141 * but return with as many bytes as are available immediately
145 struct gdb_connection *gdb_con = connection->priv;
147 if (got_data == NULL)
151 if (gdb_con->buf_cnt > 0) {
157 FD_SET(connection->fd, &read_fds);
159 tv.tv_sec = timeout_s;
161 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0) {
162 /* This can typically be because a "monitor" command took too long
163 * before printing any progress messages
166 return ERROR_GDB_TIMEOUT;
170 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
174 static int gdb_get_char_inner(struct connection *connection, int *next_char)
176 struct gdb_connection *gdb_con = connection->priv;
177 int retval = ERROR_OK;
179 #ifdef _DEBUG_GDB_IO_
183 if (connection->service->type != CONNECTION_TCP)
184 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
186 retval = check_pending(connection, 1, NULL);
187 if (retval != ERROR_OK)
189 gdb_con->buf_cnt = read_socket(connection->fd,
194 if (gdb_con->buf_cnt > 0)
196 if (gdb_con->buf_cnt == 0) {
198 return ERROR_SERVER_REMOTE_CLOSED;
202 errno = WSAGetLastError();
208 case WSAECONNABORTED:
210 return ERROR_SERVER_REMOTE_CLOSED;
213 return ERROR_SERVER_REMOTE_CLOSED;
215 LOG_ERROR("read: %d", errno);
225 return ERROR_SERVER_REMOTE_CLOSED;
228 return ERROR_SERVER_REMOTE_CLOSED;
230 LOG_ERROR("read: %s", strerror(errno));
232 return ERROR_SERVER_REMOTE_CLOSED;
237 #ifdef _DEBUG_GDB_IO_
238 debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
239 LOG_DEBUG("received '%s'", debug_buffer);
243 gdb_con->buf_p = gdb_con->buffer;
245 *next_char = *(gdb_con->buf_p++);
246 if (gdb_con->buf_cnt > 0)
247 connection->input_pending = 1;
249 connection->input_pending = 0;
250 #ifdef _DEBUG_GDB_IO_
251 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
258 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
259 * held in registers in the inner loop.
261 * For small caches and embedded systems this is important!
263 static inline int gdb_get_char_fast(struct connection *connection,
264 int *next_char, char **buf_p, int *buf_cnt)
266 int retval = ERROR_OK;
268 if ((*buf_cnt)-- > 0) {
269 *next_char = **buf_p;
272 connection->input_pending = 1;
274 connection->input_pending = 0;
276 #ifdef _DEBUG_GDB_IO_
277 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
283 struct gdb_connection *gdb_con = connection->priv;
284 gdb_con->buf_p = *buf_p;
285 gdb_con->buf_cnt = *buf_cnt;
286 retval = gdb_get_char_inner(connection, next_char);
287 *buf_p = gdb_con->buf_p;
288 *buf_cnt = gdb_con->buf_cnt;
293 static int gdb_get_char(struct connection *connection, int *next_char)
295 struct gdb_connection *gdb_con = connection->priv;
296 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
299 static int gdb_putback_char(struct connection *connection, int last_char)
301 struct gdb_connection *gdb_con = connection->priv;
303 if (gdb_con->buf_p > gdb_con->buffer) {
304 *(--gdb_con->buf_p) = last_char;
307 LOG_ERROR("BUG: couldn't put character back");
312 /* The only way we can detect that the socket is closed is the first time
313 * we write to it, we will fail. Subsequent write operations will
314 * succeed. Shudder! */
315 static int gdb_write(struct connection *connection, void *data, int len)
317 struct gdb_connection *gdb_con = connection->priv;
319 return ERROR_SERVER_REMOTE_CLOSED;
321 if (connection_write(connection, data, len) == len)
324 return ERROR_SERVER_REMOTE_CLOSED;
327 static int gdb_put_packet_inner(struct connection *connection,
328 char *buffer, int len)
331 unsigned char my_checksum = 0;
332 #ifdef _DEBUG_GDB_IO_
337 struct gdb_connection *gdb_con = connection->priv;
339 for (i = 0; i < len; i++)
340 my_checksum += buffer[i];
342 #ifdef _DEBUG_GDB_IO_
344 * At this point we should have nothing in the input queue from GDB,
345 * however sometimes '-' is sent even though we've already received
346 * an ACK (+) for everything we've sent off.
350 retval = check_pending(connection, 0, &gotdata);
351 if (retval != ERROR_OK)
355 retval = gdb_get_char(connection, &reply);
356 if (retval != ERROR_OK)
359 /* fix a problem with some IAR tools */
360 gdb_putback_char(connection, reply);
361 LOG_DEBUG("Unexpected start of new packet");
365 LOG_WARNING("Discard unexpected char %c", reply);
370 #ifdef _DEBUG_GDB_IO_
371 debug_buffer = strndup(buffer, len);
372 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
376 char local_buffer[1024];
377 local_buffer[0] = '$';
378 if ((size_t)len + 4 <= sizeof(local_buffer)) {
379 /* performance gain on smaller packets by only a single call to gdb_write() */
380 memcpy(local_buffer + 1, buffer, len++);
381 len += snprintf(local_buffer + len, sizeof(local_buffer) - len, "#%02x", my_checksum);
382 retval = gdb_write(connection, local_buffer, len);
383 if (retval != ERROR_OK)
386 /* larger packets are transmitted directly from caller supplied buffer
387 * by several calls to gdb_write() to avoid dynamic allocation */
388 snprintf(local_buffer + 1, sizeof(local_buffer) - 1, "#%02x", my_checksum);
389 retval = gdb_write(connection, local_buffer, 1);
390 if (retval != ERROR_OK)
392 retval = gdb_write(connection, buffer, len);
393 if (retval != ERROR_OK)
395 retval = gdb_write(connection, local_buffer + 1, 3);
396 if (retval != ERROR_OK)
400 if (gdb_con->noack_mode)
403 retval = gdb_get_char(connection, &reply);
404 if (retval != ERROR_OK)
409 else if (reply == '-') {
410 /* Stop sending output packets for now */
411 log_remove_callback(gdb_log_callback, connection);
412 LOG_WARNING("negative reply, retrying");
413 } else if (reply == 0x3) {
415 retval = gdb_get_char(connection, &reply);
416 if (retval != ERROR_OK)
420 else if (reply == '-') {
421 /* Stop sending output packets for now */
422 log_remove_callback(gdb_log_callback, connection);
423 LOG_WARNING("negative reply, retrying");
424 } else if (reply == '$') {
425 LOG_ERROR("GDB missing ack(1) - assumed good");
426 gdb_putback_char(connection, reply);
429 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
431 return ERROR_SERVER_REMOTE_CLOSED;
433 } else if (reply == '$') {
434 LOG_ERROR("GDB missing ack(2) - assumed good");
435 gdb_putback_char(connection, reply);
438 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
441 return ERROR_SERVER_REMOTE_CLOSED;
445 return ERROR_SERVER_REMOTE_CLOSED;
450 int gdb_put_packet(struct connection *connection, char *buffer, int len)
452 struct gdb_connection *gdb_con = connection->priv;
454 int retval = gdb_put_packet_inner(connection, buffer, len);
457 /* we sent some data, reset timer for keep alive messages */
463 static inline int fetch_packet(struct connection *connection,
464 int *checksum_ok, int noack, int *len, char *buffer)
466 unsigned char my_checksum = 0;
469 int retval = ERROR_OK;
471 struct gdb_connection *gdb_con = connection->priv;
476 /* move this over into local variables to use registers and give the
477 * more freedom to optimize */
478 char *buf_p = gdb_con->buf_p;
479 int buf_cnt = gdb_con->buf_cnt;
482 /* The common case is that we have an entire packet with no escape chars.
483 * We need to leave at least 2 bytes in the buffer to have
484 * gdb_get_char() update various bits and bobs correctly.
486 if ((buf_cnt > 2) && ((buf_cnt + count) < *len)) {
487 /* The compiler will struggle a bit with constant propagation and
488 * aliasing, so we help it by showing that these values do not
489 * change inside the loop
493 int run = buf_cnt - 2;
499 if (character == '#') {
500 /* Danger! character can be '#' when esc is
501 * used so we need an explicit boolean for done here. */
506 if (character == '}') {
507 /* data transmitted in binary mode (X packet)
508 * uses 0x7d as escape character */
509 my_checksum += character & 0xff;
512 my_checksum += character & 0xff;
513 buffer[count++] = (character ^ 0x20) & 0xff;
515 my_checksum += character & 0xff;
516 buffer[count++] = character & 0xff;
525 LOG_ERROR("packet buffer too small");
526 retval = ERROR_GDB_BUFFER_TOO_SMALL;
530 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
531 if (retval != ERROR_OK)
534 if (character == '#')
537 if (character == '}') {
538 /* data transmitted in binary mode (X packet)
539 * uses 0x7d as escape character */
540 my_checksum += character & 0xff;
542 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
543 if (retval != ERROR_OK)
546 my_checksum += character & 0xff;
547 buffer[count++] = (character ^ 0x20) & 0xff;
549 my_checksum += character & 0xff;
550 buffer[count++] = character & 0xff;
554 gdb_con->buf_p = buf_p;
555 gdb_con->buf_cnt = buf_cnt;
557 if (retval != ERROR_OK)
562 retval = gdb_get_char(connection, &character);
563 if (retval != ERROR_OK)
565 checksum[0] = character;
566 retval = gdb_get_char(connection, &character);
567 if (retval != ERROR_OK)
569 checksum[1] = character;
573 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
578 static int gdb_get_packet_inner(struct connection *connection,
579 char *buffer, int *len)
583 struct gdb_connection *gdb_con = connection->priv;
587 retval = gdb_get_char(connection, &character);
588 if (retval != ERROR_OK)
591 #ifdef _DEBUG_GDB_IO_
592 LOG_DEBUG("character: '%c'", character);
599 /* gdb sends a dummy ack '+' at every remote connect - see
600 * remote_start_remote (remote.c)
601 * in case anyone tries to debug why they receive this
602 * warning every time */
603 LOG_WARNING("acknowledgment received, but no packet pending");
606 LOG_WARNING("negative acknowledgment, but no packet pending");
613 LOG_WARNING("ignoring character 0x%x", character);
616 } while (character != '$');
619 /* explicit code expansion here to get faster inlined code in -O3 by not
620 * calculating checksum */
621 if (gdb_con->noack_mode) {
622 retval = fetch_packet(connection, &checksum_ok, 1, len, buffer);
623 if (retval != ERROR_OK)
626 retval = fetch_packet(connection, &checksum_ok, 0, len, buffer);
627 if (retval != ERROR_OK)
631 if (gdb_con->noack_mode) {
632 /* checksum is not checked in noack mode */
636 retval = gdb_write(connection, "+", 1);
637 if (retval != ERROR_OK)
643 return ERROR_SERVER_REMOTE_CLOSED;
648 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
650 struct gdb_connection *gdb_con = connection->priv;
652 int retval = gdb_get_packet_inner(connection, buffer, len);
657 static int gdb_output_con(struct connection *connection, const char *line)
662 bin_size = strlen(line);
664 hex_buffer = malloc(bin_size * 2 + 2);
665 if (hex_buffer == NULL)
666 return ERROR_GDB_BUFFER_TOO_SMALL;
669 int pkt_len = hexify(hex_buffer + 1, line, bin_size, bin_size * 2 + 1);
670 int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1);
676 static int gdb_output(struct command_context *context, const char *line)
678 /* this will be dumped to the log and also sent as an O packet if possible */
679 LOG_USER_N("%s", line);
683 static void gdb_frontend_halted(struct target *target, struct connection *connection)
685 struct gdb_connection *gdb_connection = connection->priv;
687 /* In the GDB protocol when we are stepping or continuing execution,
688 * we have a lingering reply. Upon receiving a halted event
689 * when we have that lingering packet, we reply to the original
690 * step or continue packet.
692 * Executing monitor commands can bring the target in and
693 * out of the running state so we'll see lots of TARGET_EVENT_XXX
694 * that are to be ignored.
696 if (gdb_connection->frontend_state == TARGET_RUNNING) {
700 /* stop forwarding log packets! */
701 log_remove_callback(gdb_log_callback, connection);
703 if (gdb_connection->ctrl_c) {
705 gdb_connection->ctrl_c = 0;
707 signal_var = gdb_last_signal(target);
709 snprintf(sig_reply, 4, "T%2.2x", signal_var);
710 gdb_put_packet(connection, sig_reply, 3);
711 gdb_connection->frontend_state = TARGET_HALTED;
712 rtos_update_threads(target);
716 static int gdb_target_callback_event_handler(struct target *target,
717 enum target_event event, void *priv)
720 struct connection *connection = priv;
722 target_handle_event(target, event);
724 case TARGET_EVENT_GDB_HALT:
725 gdb_frontend_halted(target, connection);
727 case TARGET_EVENT_HALTED:
728 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
730 case TARGET_EVENT_GDB_FLASH_ERASE_START:
731 retval = jtag_execute_queue();
732 if (retval != ERROR_OK)
742 static int gdb_new_connection(struct connection *connection)
744 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
745 struct gdb_service *gdb_service = connection->service->priv;
749 connection->priv = gdb_connection;
751 /* initialize gdb connection information */
752 gdb_connection->buf_p = gdb_connection->buffer;
753 gdb_connection->buf_cnt = 0;
754 gdb_connection->ctrl_c = 0;
755 gdb_connection->frontend_state = TARGET_HALTED;
756 gdb_connection->vflash_image = NULL;
757 gdb_connection->closed = 0;
758 gdb_connection->busy = 0;
759 gdb_connection->noack_mode = 0;
760 gdb_connection->sync = true;
761 gdb_connection->mem_write_error = false;
762 gdb_connection->attached = true;
764 /* send ACK to GDB for debug request */
765 gdb_write(connection, "+", 1);
767 /* output goes through gdb connection */
768 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
770 /* we must remove all breakpoints registered to the target as a previous
771 * GDB session could leave dangling breakpoints if e.g. communication
774 breakpoint_clear_target(gdb_service->target);
775 watchpoint_clear_target(gdb_service->target);
777 /* clean previous rtos session if supported*/
778 if ((gdb_service->target->rtos) && (gdb_service->target->rtos->type->clean))
779 gdb_service->target->rtos->type->clean(gdb_service->target);
781 /* remove the initial ACK from the incoming buffer */
782 retval = gdb_get_char(connection, &initial_ack);
783 if (retval != ERROR_OK)
786 /* FIX!!!??? would we actually ever receive a + here???
789 if (initial_ack != '+')
790 gdb_putback_char(connection, initial_ack);
791 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
793 if (gdb_use_memory_map) {
794 /* Connect must fail if the memory map can't be set up correctly.
796 * This will cause an auto_probe to be invoked, which is either
797 * a no-op or it will fail when the target isn't ready(e.g. not halted).
800 for (i = 0; i < flash_get_bank_count(); i++) {
801 struct flash_bank *p;
802 retval = get_flash_bank_by_num(i, &p);
803 if (retval != ERROR_OK) {
804 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
805 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
811 gdb_actual_connections++;
812 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
813 gdb_actual_connections,
814 target_name(gdb_service->target),
815 target_state_name(gdb_service->target));
817 /* DANGER! If we fail subsequently, we must remove this handler,
818 * otherwise we occasionally see crashes as the timer can invoke the
821 * register callback to be informed about target events */
822 target_register_event_callback(gdb_target_callback_event_handler, connection);
827 static int gdb_connection_closed(struct connection *connection)
829 struct gdb_service *gdb_service = connection->service->priv;
830 struct gdb_connection *gdb_connection = connection->priv;
832 /* we're done forwarding messages. Tear down callback before
833 * cleaning up connection.
835 log_remove_callback(gdb_log_callback, connection);
837 gdb_actual_connections--;
838 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
839 target_name(gdb_service->target),
840 target_state_name(gdb_service->target),
841 gdb_actual_connections);
843 /* see if an image built with vFlash commands is left */
844 if (gdb_connection->vflash_image) {
845 image_close(gdb_connection->vflash_image);
846 free(gdb_connection->vflash_image);
847 gdb_connection->vflash_image = NULL;
850 /* if this connection registered a debug-message receiver delete it */
851 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
853 if (connection->priv) {
854 free(connection->priv);
855 connection->priv = NULL;
857 LOG_ERROR("BUG: connection->priv == NULL");
859 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
861 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
863 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
868 static void gdb_send_error(struct connection *connection, uint8_t the_error)
871 snprintf(err, 4, "E%2.2X", the_error);
872 gdb_put_packet(connection, err, 3);
875 static int gdb_last_signal_packet(struct connection *connection,
876 char *packet, int packet_size)
878 struct target *target = get_target_from_connection(connection);
879 struct gdb_connection *gdb_con = connection->priv;
883 if (!gdb_con->attached) {
884 /* if we are here we have received a kill packet
885 * reply W stop reply otherwise gdb gets very unhappy */
886 gdb_put_packet(connection, "W00", 3);
890 signal_var = gdb_last_signal(target);
892 snprintf(sig_reply, 4, "S%2.2x", signal_var);
893 gdb_put_packet(connection, sig_reply, 3);
898 static inline int gdb_reg_pos(struct target *target, int pos, int len)
900 if (target->endianness == TARGET_LITTLE_ENDIAN)
903 return len - 1 - pos;
906 /* Convert register to string of bytes. NB! The # of bits in the
907 * register might be non-divisible by 8(a byte), in which
908 * case an entire byte is shown.
910 * NB! the format on the wire is the target endianness
912 * The format of reg->value is little endian
915 static void gdb_str_to_target(struct target *target,
916 char *tstr, struct reg *reg)
923 buf_len = DIV_ROUND_UP(reg->size, 8);
925 for (i = 0; i < buf_len; i++) {
926 int j = gdb_reg_pos(target, i, buf_len);
927 tstr += sprintf(tstr, "%02x", buf[j]);
931 /* copy over in register buffer */
932 static void gdb_target_to_reg(struct target *target,
933 char *tstr, int str_len, uint8_t *bin)
936 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
941 for (i = 0; i < str_len; i += 2) {
943 if (sscanf(tstr + i, "%02x", &t) != 1) {
944 LOG_ERROR("BUG: unable to convert register value");
948 int j = gdb_reg_pos(target, i/2, str_len/2);
953 static int gdb_get_registers_packet(struct connection *connection,
954 char *packet, int packet_size)
956 struct target *target = get_target_from_connection(connection);
957 struct reg **reg_list;
960 int reg_packet_size = 0;
965 #ifdef _DEBUG_GDB_IO_
969 if ((target->rtos != NULL) && (ERROR_OK == rtos_get_gdb_reg_list(connection)))
972 retval = target_get_gdb_reg_list(target, ®_list, ®_list_size);
973 if (retval != ERROR_OK)
974 return gdb_error(connection, retval);
976 for (i = 0; i < reg_list_size; i++)
977 reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
979 assert(reg_packet_size > 0);
981 reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
982 reg_packet_p = reg_packet;
984 for (i = 0; i < reg_list_size; i++) {
985 if (!reg_list[i]->valid)
986 reg_list[i]->type->get(reg_list[i]);
987 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
988 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
991 #ifdef _DEBUG_GDB_IO_
993 char *reg_packet_p_debug;
994 reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
995 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
996 free(reg_packet_p_debug);
1000 gdb_put_packet(connection, reg_packet, reg_packet_size);
1008 static int gdb_set_registers_packet(struct connection *connection,
1009 char *packet, int packet_size)
1011 struct target *target = get_target_from_connection(connection);
1013 struct reg **reg_list;
1018 #ifdef _DEBUG_GDB_IO_
1022 /* skip command character */
1026 if (packet_size % 2) {
1027 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1028 return ERROR_SERVER_REMOTE_CLOSED;
1031 retval = target_get_gdb_reg_list(target, ®_list, ®_list_size);
1032 if (retval != ERROR_OK)
1033 return gdb_error(connection, retval);
1036 for (i = 0; i < reg_list_size; i++) {
1038 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1040 if (packet_p + chars > packet + packet_size)
1041 LOG_ERROR("BUG: register packet is too small for registers");
1043 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1044 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1046 reg_list[i]->type->set(reg_list[i], bin_buf);
1048 /* advance packet pointer */
1054 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1057 gdb_put_packet(connection, "OK", 2);
1062 static int gdb_get_register_packet(struct connection *connection,
1063 char *packet, int packet_size)
1065 struct target *target = get_target_from_connection(connection);
1067 int reg_num = strtoul(packet + 1, NULL, 16);
1068 struct reg **reg_list;
1072 #ifdef _DEBUG_GDB_IO_
1076 retval = target_get_gdb_reg_list(target, ®_list, ®_list_size);
1077 if (retval != ERROR_OK)
1078 return gdb_error(connection, retval);
1080 if (reg_list_size <= reg_num) {
1081 LOG_ERROR("gdb requested a non-existing register");
1082 return ERROR_SERVER_REMOTE_CLOSED;
1085 if (!reg_list[reg_num]->valid)
1086 reg_list[reg_num]->type->get(reg_list[reg_num]);
1088 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1); /* plus one for string termination null */
1090 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1092 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1100 static int gdb_set_register_packet(struct connection *connection,
1101 char *packet, int packet_size)
1103 struct target *target = get_target_from_connection(connection);
1106 int reg_num = strtoul(packet + 1, &separator, 16);
1107 struct reg **reg_list;
1113 retval = target_get_gdb_reg_list(target, ®_list, ®_list_size);
1114 if (retval != ERROR_OK)
1115 return gdb_error(connection, retval);
1117 if (reg_list_size <= reg_num) {
1118 LOG_ERROR("gdb requested a non-existing register");
1119 return ERROR_SERVER_REMOTE_CLOSED;
1122 if (*separator != '=') {
1123 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1124 return ERROR_SERVER_REMOTE_CLOSED;
1127 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1128 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1129 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1131 if ((unsigned int)chars != strlen(separator + 1)) {
1132 LOG_ERROR("gdb sent a packet with wrong register size");
1134 return ERROR_SERVER_REMOTE_CLOSED;
1137 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1139 reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1141 gdb_put_packet(connection, "OK", 2);
1149 /* No attempt is made to translate the "retval" to
1150 * GDB speak. This has to be done at the calling
1151 * site as no mapping really exists.
1153 static int gdb_error(struct connection *connection, int retval)
1155 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1156 gdb_send_error(connection, EFAULT);
1160 /* We don't have to worry about the default 2 second timeout for GDB packets,
1161 * because GDB breaks up large memory reads into smaller reads.
1163 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1165 static int gdb_read_memory_packet(struct connection *connection,
1166 char *packet, int packet_size)
1168 struct target *target = get_target_from_connection(connection);
1176 int retval = ERROR_OK;
1178 /* skip command character */
1181 addr = strtoul(packet, &separator, 16);
1183 if (*separator != ',') {
1184 LOG_ERROR("incomplete read memory packet received, dropping connection");
1185 return ERROR_SERVER_REMOTE_CLOSED;
1188 len = strtoul(separator + 1, NULL, 16);
1190 buffer = malloc(len);
1192 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1194 retval = target_read_buffer(target, addr, len, buffer);
1196 if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1197 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1198 * At some point this might be fixed in GDB, in which case this code can be removed.
1200 * OpenOCD developers are acutely aware of this problem, but there is nothing
1201 * gained by involving the user in this problem that hopefully will get resolved
1204 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1205 * cmd = view%20audit-trail&database = gdb&pr = 2395
1207 * For now, the default is to fix up things to make current GDB versions work.
1208 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1210 memset(buffer, 0, len);
1214 if (retval == ERROR_OK) {
1215 hex_buffer = malloc(len * 2 + 1);
1217 int pkt_len = hexify(hex_buffer, (char *)buffer, len, len * 2 + 1);
1219 gdb_put_packet(connection, hex_buffer, pkt_len);
1223 retval = gdb_error(connection, retval);
1230 static int gdb_write_memory_packet(struct connection *connection,
1231 char *packet, int packet_size)
1233 struct target *target = get_target_from_connection(connection);
1241 /* skip command character */
1244 addr = strtoul(packet, &separator, 16);
1246 if (*separator != ',') {
1247 LOG_ERROR("incomplete write memory packet received, dropping connection");
1248 return ERROR_SERVER_REMOTE_CLOSED;
1251 len = strtoul(separator + 1, &separator, 16);
1253 if (*(separator++) != ':') {
1254 LOG_ERROR("incomplete write memory packet received, dropping connection");
1255 return ERROR_SERVER_REMOTE_CLOSED;
1258 buffer = malloc(len);
1260 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1262 if (unhexify((char *)buffer, separator, len) != (int)len)
1263 LOG_ERROR("unable to decode memory packet");
1265 retval = target_write_buffer(target, addr, len, buffer);
1267 if (retval == ERROR_OK)
1268 gdb_put_packet(connection, "OK", 2);
1270 retval = gdb_error(connection, retval);
1277 static int gdb_write_memory_binary_packet(struct connection *connection,
1278 char *packet, int packet_size)
1280 struct target *target = get_target_from_connection(connection);
1285 int retval = ERROR_OK;
1287 /* skip command character */
1290 addr = strtoul(packet, &separator, 16);
1292 if (*separator != ',') {
1293 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1294 return ERROR_SERVER_REMOTE_CLOSED;
1297 len = strtoul(separator + 1, &separator, 16);
1299 if (*(separator++) != ':') {
1300 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1301 return ERROR_SERVER_REMOTE_CLOSED;
1304 struct gdb_connection *gdb_connection = connection->priv;
1306 if (gdb_connection->mem_write_error) {
1307 retval = ERROR_FAIL;
1308 /* now that we have reported the memory write error, we can clear the condition */
1309 gdb_connection->mem_write_error = false;
1312 /* By replying the packet *immediately* GDB will send us a new packet
1313 * while we write the last one to the target.
1315 if (retval == ERROR_OK)
1316 gdb_put_packet(connection, "OK", 2);
1318 retval = gdb_error(connection, retval);
1319 if (retval != ERROR_OK)
1324 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1326 retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1327 if (retval != ERROR_OK)
1328 gdb_connection->mem_write_error = true;
1334 static int gdb_step_continue_packet(struct connection *connection,
1335 char *packet, int packet_size)
1337 struct target *target = get_target_from_connection(connection);
1339 uint32_t address = 0x0;
1340 int retval = ERROR_OK;
1344 if (packet_size > 1) {
1345 packet[packet_size] = 0;
1346 address = strtoul(packet + 1, NULL, 16);
1350 if (packet[0] == 'c') {
1351 LOG_DEBUG("continue");
1352 /* resume at current address, don't handle breakpoints, not debugging */
1353 retval = target_resume(target, current, address, 0, 0);
1354 } else if (packet[0] == 's') {
1356 /* step at current or address, don't handle breakpoints */
1357 retval = target_step(target, current, address, 0);
1362 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1363 char *packet, int packet_size)
1365 struct target *target = get_target_from_connection(connection);
1367 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1368 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1376 type = strtoul(packet + 1, &separator, 16);
1378 if (type == 0) /* memory breakpoint */
1379 bp_type = BKPT_SOFT;
1380 else if (type == 1) /* hardware breakpoint */
1381 bp_type = BKPT_HARD;
1382 else if (type == 2) /* write watchpoint */
1383 wp_type = WPT_WRITE;
1384 else if (type == 3) /* read watchpoint */
1386 else if (type == 4) /* access watchpoint */
1387 wp_type = WPT_ACCESS;
1389 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1390 return ERROR_SERVER_REMOTE_CLOSED;
1393 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1394 bp_type = gdb_breakpoint_override_type;
1396 if (*separator != ',') {
1397 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1398 return ERROR_SERVER_REMOTE_CLOSED;
1401 address = strtoul(separator + 1, &separator, 16);
1403 if (*separator != ',') {
1404 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1405 return ERROR_SERVER_REMOTE_CLOSED;
1408 size = strtoul(separator + 1, &separator, 16);
1413 if (packet[0] == 'Z') {
1414 retval = breakpoint_add(target, address, size, bp_type);
1415 if (retval != ERROR_OK) {
1416 retval = gdb_error(connection, retval);
1417 if (retval != ERROR_OK)
1420 gdb_put_packet(connection, "OK", 2);
1422 breakpoint_remove(target, address);
1423 gdb_put_packet(connection, "OK", 2);
1430 if (packet[0] == 'Z') {
1431 retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu);
1432 if (retval != ERROR_OK) {
1433 retval = gdb_error(connection, retval);
1434 if (retval != ERROR_OK)
1437 gdb_put_packet(connection, "OK", 2);
1439 watchpoint_remove(target, address);
1440 gdb_put_packet(connection, "OK", 2);
1451 /* print out a string and allocate more space as needed,
1452 * mainly used for XML at this point
1454 static void xml_printf(int *retval, char **xml, int *pos, int *size,
1455 const char *fmt, ...)
1457 if (*retval != ERROR_OK)
1462 if ((*xml == NULL) || (!first)) {
1463 /* start by 0 to exercise all the code paths.
1464 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1466 *size = *size * 2 + 2;
1468 *xml = realloc(*xml, *size);
1472 *retval = ERROR_SERVER_REMOTE_CLOSED;
1480 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1482 if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1486 /* there was just enough or not enough space, allocate more. */
1491 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1495 /* Extract and NUL-terminate the annex. */
1497 while (*buf && *buf != ':')
1503 /* After the read marker and annex, qXfer looks like a
1504 * traditional 'm' packet. */
1506 *ofs = strtoul(buf, &separator, 16);
1508 if (*separator != ',')
1511 *len = strtoul(separator + 1, NULL, 16);
1516 static int compare_bank(const void *a, const void *b)
1518 struct flash_bank *b1, *b2;
1519 b1 = *((struct flash_bank **)a);
1520 b2 = *((struct flash_bank **)b);
1522 if (b1->base == b2->base)
1524 else if (b1->base > b2->base)
1530 static int gdb_memory_map(struct connection *connection,
1531 char *packet, int packet_size)
1533 /* We get away with only specifying flash here. Regions that are not
1534 * specified are treated as if we provided no memory map(if not we
1535 * could detect the holes and mark them as RAM).
1536 * Normally we only execute this code once, but no big deal if we
1537 * have to regenerate it a couple of times.
1540 struct target *target = get_target_from_connection(connection);
1541 struct flash_bank *p;
1545 int retval = ERROR_OK;
1546 struct flash_bank **banks;
1550 uint32_t ram_start = 0;
1552 int target_flash_banks = 0;
1554 /* skip command character */
1557 offset = strtoul(packet, &separator, 16);
1558 length = strtoul(separator + 1, &separator, 16);
1560 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1562 /* Sort banks in ascending order. We need to report non-flash
1563 * memory as ram (or rather read/write) by default for GDB, since
1564 * it has no concept of non-cacheable read/write memory (i/o etc).
1566 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1567 * Current versions of GDB assume unlisted addresses are RAM...
1569 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1571 for (i = 0; i < flash_get_bank_count(); i++) {
1572 retval = get_flash_bank_by_num(i, &p);
1573 if (retval != ERROR_OK) {
1575 gdb_error(connection, retval);
1578 if (p->target == target)
1579 banks[target_flash_banks++] = p;
1582 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1585 for (i = 0; i < target_flash_banks; i++) {
1587 unsigned sector_size = 0;
1593 if (ram_start < p->base)
1594 xml_printf(&retval, &xml, &pos, &size,
1595 "<memory type=\"ram\" start=\"0x%x\" "
1596 "length=\"0x%x\"/>\n",
1597 ram_start, p->base - ram_start);
1599 /* Report adjacent groups of same-size sectors. So for
1600 * example top boot CFI flash will list an initial region
1601 * with several large sectors (maybe 128KB) and several
1602 * smaller ones at the end (maybe 32KB). STR7 will have
1603 * regions with 8KB, 32KB, and 64KB sectors; etc.
1605 for (j = 0; j < p->num_sectors; j++) {
1608 /* Maybe start a new group of sectors. */
1609 if (sector_size == 0) {
1610 start = p->base + p->sectors[j].offset;
1611 xml_printf(&retval, &xml, &pos, &size,
1612 "<memory type=\"flash\" "
1615 sector_size = p->sectors[j].size;
1618 /* Does this finish a group of sectors?
1619 * If not, continue an already-started group.
1621 if (j == p->num_sectors - 1)
1622 group_len = (p->base + p->size) - start;
1623 else if (p->sectors[j + 1].size != sector_size)
1624 group_len = p->base + p->sectors[j + 1].offset
1629 xml_printf(&retval, &xml, &pos, &size,
1630 "length=\"0x%x\">\n"
1631 "<property name=\"blocksize\">"
1639 ram_start = p->base + p->size;
1643 xml_printf(&retval, &xml, &pos, &size,
1644 "<memory type=\"ram\" start=\"0x%x\" "
1645 "length=\"0x%x\"/>\n",
1646 ram_start, 0-ram_start);
1647 /* ELSE a flash chip could be at the very end of the 32 bit address
1648 * space, in which case ram_start will be precisely 0
1654 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1656 if (retval != ERROR_OK) {
1657 gdb_error(connection, retval);
1661 if (offset + length > pos)
1662 length = pos - offset;
1664 char *t = malloc(length + 1);
1666 memcpy(t + 1, xml + offset, length);
1667 gdb_put_packet(connection, t, length + 1);
1674 static int gdb_query_packet(struct connection *connection,
1675 char *packet, int packet_size)
1677 struct command_context *cmd_ctx = connection->cmd_ctx;
1678 struct gdb_connection *gdb_connection = connection->priv;
1679 struct target *target = get_target_from_connection(connection);
1681 if (strncmp(packet, "qRcmd,", 6) == 0) {
1682 if (packet_size > 6) {
1684 cmd = malloc((packet_size - 6) / 2 + 1);
1685 int len = unhexify(cmd, packet + 6, (packet_size - 6) / 2);
1688 /* We want to print all debug output to GDB connection */
1689 log_add_callback(gdb_log_callback, connection);
1690 target_call_timer_callbacks_now();
1691 /* some commands need to know the GDB connection, make note of current
1692 * GDB connection. */
1693 current_gdb_connection = gdb_connection;
1694 command_run_line(cmd_ctx, cmd);
1695 current_gdb_connection = NULL;
1696 target_call_timer_callbacks_now();
1697 log_remove_callback(gdb_log_callback, connection);
1700 gdb_put_packet(connection, "OK", 2);
1702 } else if (strncmp(packet, "qCRC:", 5) == 0) {
1703 if (packet_size > 5) {
1711 /* skip command character */
1714 addr = strtoul(packet, &separator, 16);
1716 if (*separator != ',') {
1717 LOG_ERROR("incomplete read memory packet received, dropping connection");
1718 return ERROR_SERVER_REMOTE_CLOSED;
1721 len = strtoul(separator + 1, NULL, 16);
1723 retval = target_checksum_memory(target, addr, len, &checksum);
1725 if (retval == ERROR_OK) {
1726 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
1727 gdb_put_packet(connection, gdb_reply, 9);
1729 retval = gdb_error(connection, retval);
1730 if (retval != ERROR_OK)
1736 } else if (strncmp(packet, "qSupported", 10) == 0) {
1737 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1738 * disable qXfer:features:read for the moment */
1739 int retval = ERROR_OK;
1740 char *buffer = NULL;
1748 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1749 (GDB_BUFFER_SIZE - 1),
1750 ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1752 if (retval != ERROR_OK) {
1753 gdb_send_error(connection, 01);
1757 gdb_put_packet(connection, buffer, strlen(buffer));
1761 } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
1762 && (flash_get_bank_count() > 0))
1763 return gdb_memory_map(connection, packet, packet_size);
1764 else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
1768 int retval = ERROR_OK;
1771 unsigned int length;
1774 /* skip command character */
1777 if (decode_xfer_read(packet, &annex, &offset, &length) < 0) {
1778 gdb_send_error(connection, 01);
1782 if (strcmp(annex, "target.xml") != 0) {
1783 gdb_send_error(connection, 01);
1791 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1793 if (retval != ERROR_OK) {
1794 gdb_error(connection, retval);
1798 gdb_put_packet(connection, xml, strlen(xml));
1802 } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
1803 gdb_connection->noack_mode = 1;
1804 gdb_put_packet(connection, "OK", 2);
1808 gdb_put_packet(connection, "", 0);
1812 static int gdb_v_packet(struct connection *connection,
1813 char *packet, int packet_size)
1815 struct gdb_connection *gdb_connection = connection->priv;
1816 struct gdb_service *gdb_service = connection->service->priv;
1819 /* if flash programming disabled - send a empty reply */
1821 if (gdb_flash_program == 0) {
1822 gdb_put_packet(connection, "", 0);
1826 if (strncmp(packet, "vFlashErase:", 12) == 0) {
1828 unsigned long length;
1830 char *parse = packet + 12;
1831 if (*parse == '\0') {
1832 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1833 return ERROR_SERVER_REMOTE_CLOSED;
1836 addr = strtoul(parse, &parse, 16);
1838 if (*(parse++) != ',' || *parse == '\0') {
1839 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1840 return ERROR_SERVER_REMOTE_CLOSED;
1843 length = strtoul(parse, &parse, 16);
1845 if (*parse != '\0') {
1846 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1847 return ERROR_SERVER_REMOTE_CLOSED;
1850 /* assume all sectors need erasing - stops any problems
1851 * when flash_write is called multiple times */
1854 /* perform any target specific operations before the erase */
1855 target_call_event_callbacks(gdb_service->target,
1856 TARGET_EVENT_GDB_FLASH_ERASE_START);
1858 /* vFlashErase:addr,length messages require region start and
1859 * end to be "block" aligned ... if padding is ever needed,
1860 * GDB will have become dangerously confused.
1862 result = flash_erase_address_range(gdb_service->target,
1863 false, addr, length);
1865 /* perform any target specific operations after the erase */
1866 target_call_event_callbacks(gdb_service->target,
1867 TARGET_EVENT_GDB_FLASH_ERASE_END);
1870 if (result != ERROR_OK) {
1871 /* GDB doesn't evaluate the actual error number returned,
1872 * treat a failed erase as an I/O error
1874 gdb_send_error(connection, EIO);
1875 LOG_ERROR("flash_erase returned %i", result);
1877 gdb_put_packet(connection, "OK", 2);
1882 if (strncmp(packet, "vFlashWrite:", 12) == 0) {
1885 unsigned long length;
1886 char *parse = packet + 12;
1888 if (*parse == '\0') {
1889 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1890 return ERROR_SERVER_REMOTE_CLOSED;
1892 addr = strtoul(parse, &parse, 16);
1893 if (*(parse++) != ':') {
1894 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1895 return ERROR_SERVER_REMOTE_CLOSED;
1897 length = packet_size - (parse - packet);
1899 /* create a new image if there isn't already one */
1900 if (gdb_connection->vflash_image == NULL) {
1901 gdb_connection->vflash_image = malloc(sizeof(struct image));
1902 image_open(gdb_connection->vflash_image, "", "build");
1905 /* create new section with content from packet buffer */
1906 retval = image_add_section(gdb_connection->vflash_image,
1907 addr, length, 0x0, (uint8_t *)parse);
1908 if (retval != ERROR_OK)
1911 gdb_put_packet(connection, "OK", 2);
1916 if (strncmp(packet, "vFlashDone", 10) == 0) {
1919 /* process the flashing buffer. No need to erase as GDB
1920 * always issues a vFlashErase first. */
1921 target_call_event_callbacks(gdb_service->target,
1922 TARGET_EVENT_GDB_FLASH_WRITE_START);
1923 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1924 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1925 if (result != ERROR_OK) {
1926 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1927 gdb_put_packet(connection, "E.memtype", 9);
1929 gdb_send_error(connection, EIO);
1931 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
1932 gdb_put_packet(connection, "OK", 2);
1935 image_close(gdb_connection->vflash_image);
1936 free(gdb_connection->vflash_image);
1937 gdb_connection->vflash_image = NULL;
1942 gdb_put_packet(connection, "", 0);
1946 static int gdb_detach(struct connection *connection)
1948 struct gdb_service *gdb_service = connection->service->priv;
1950 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
1952 return gdb_put_packet(connection, "OK", 2);
1955 static void gdb_log_callback(void *priv, const char *file, unsigned line,
1956 const char *function, const char *string)
1958 struct connection *connection = priv;
1959 struct gdb_connection *gdb_con = connection->priv;
1961 if (gdb_con->busy) {
1962 /* do not reply this using the O packet */
1966 gdb_output_con(connection, string);
1969 static void gdb_sig_halted(struct connection *connection)
1972 snprintf(sig_reply, 4, "T%2.2x", 2);
1973 gdb_put_packet(connection, sig_reply, 3);
1976 static int gdb_input_inner(struct connection *connection)
1978 /* Do not allocate this on the stack */
1979 static char gdb_packet_buffer[GDB_BUFFER_SIZE];
1981 struct gdb_service *gdb_service = connection->service->priv;
1982 struct target *target = gdb_service->target;
1983 char *packet = gdb_packet_buffer;
1986 struct gdb_connection *gdb_con = connection->priv;
1987 static int extended_protocol;
1989 /* drain input buffer. If one of the packets fail, then an error
1990 * packet is replied, if applicable.
1992 * This loop will terminate and the error code is returned.
1994 * The calling fn will check if this error is something that
1995 * can be recovered from, or if the connection must be closed.
1997 * If the error is recoverable, this fn is called again to
1998 * drain the rest of the buffer.
2001 packet_size = GDB_BUFFER_SIZE-1;
2002 retval = gdb_get_packet(connection, packet, &packet_size);
2003 if (retval != ERROR_OK)
2006 /* terminate with zero */
2007 packet[packet_size] = 0;
2009 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
2010 if (packet[0] == 'X') {
2011 /* binary packets spew junk into the debug log stream */
2014 for (x = 0; (x < 49) && (packet[x] != ':'); x++)
2017 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
2019 LOG_DEBUG("received packet: '%s'", packet);
2022 if (packet_size > 0) {
2024 switch (packet[0]) {
2025 case 'T': /* Is thread alive? */
2026 gdb_thread_packet(connection, packet, packet_size);
2028 case 'H': /* Set current thread ( 'c' for step and continue,
2029 * 'g' for all other operations ) */
2030 gdb_thread_packet(connection, packet, packet_size);
2034 retval = gdb_thread_packet(connection, packet, packet_size);
2035 if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
2036 retval = gdb_query_packet(connection, packet, packet_size);
2039 retval = gdb_get_registers_packet(connection, packet, packet_size);
2042 retval = gdb_set_registers_packet(connection, packet, packet_size);
2045 retval = gdb_get_register_packet(connection, packet, packet_size);
2048 retval = gdb_set_register_packet(connection, packet, packet_size);
2051 retval = gdb_read_memory_packet(connection, packet, packet_size);
2054 retval = gdb_write_memory_packet(connection, packet, packet_size);
2058 retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
2061 gdb_last_signal_packet(connection, packet, packet_size);
2066 gdb_thread_packet(connection, packet, packet_size);
2067 log_add_callback(gdb_log_callback, connection);
2069 if (gdb_con->mem_write_error) {
2070 LOG_ERROR("Memory write failure!");
2072 /* now that we have reported the memory write error,
2073 * we can clear the condition */
2074 gdb_con->mem_write_error = false;
2077 bool nostep = false;
2078 bool already_running = false;
2079 if (target->state == TARGET_RUNNING) {
2080 LOG_WARNING("WARNING! The target is already running. "
2081 "All changes GDB did to registers will be discarded! "
2082 "Waiting for target to halt.");
2083 already_running = true;
2084 } else if (target->state != TARGET_HALTED) {
2085 LOG_WARNING("The target is not in the halted nor running stated, " \
2086 "stepi/continue ignored.");
2088 } else if ((packet[0] == 's') && gdb_con->sync) {
2089 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2090 * sent by GDB first to OpenOCD, thus defeating the check to
2091 * make only the single stepping have the sync feature...
2094 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2095 "from the target.");
2097 gdb_con->sync = false;
2099 if (!already_running && nostep) {
2100 /* Either the target isn't in the halted state, then we can't
2101 * step/continue. This might be early setup, etc.
2103 * Or we want to allow GDB to pick up a fresh set of
2104 * register values without modifying the target state.
2107 gdb_sig_halted(connection);
2109 /* stop forwarding log packets! */
2110 log_remove_callback(gdb_log_callback, connection);
2112 /* We're running/stepping, in which case we can
2113 * forward log output until the target is halted
2115 gdb_con->frontend_state = TARGET_RUNNING;
2116 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2118 if (!already_running) {
2119 /* Here we don't want packet processing to stop even if this fails,
2120 * so we use a local variable instead of retval. */
2121 retval = gdb_step_continue_packet(connection, packet, packet_size);
2122 if (retval != ERROR_OK) {
2123 /* we'll never receive a halted
2124 * condition... issue a false one..
2126 gdb_frontend_halted(target, connection);
2133 retval = gdb_v_packet(connection, packet, packet_size);
2136 retval = gdb_detach(connection);
2137 extended_protocol = 0;
2140 retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
2141 if (retval != ERROR_OK)
2145 if (extended_protocol != 0) {
2146 gdb_con->attached = false;
2149 gdb_put_packet(connection, "OK", 2);
2150 return ERROR_SERVER_REMOTE_CLOSED;
2152 /* handle extended remote protocol */
2153 extended_protocol = 1;
2154 gdb_put_packet(connection, "OK", 2);
2157 /* handle extended restart packet */
2158 breakpoint_clear_target(gdb_service->target);
2159 watchpoint_clear_target(gdb_service->target);
2160 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
2161 target_name(target));
2162 /* set connection as attached after reset */
2163 gdb_con->attached = true;
2164 /* info rtos parts */
2165 gdb_thread_packet(connection, packet, packet_size);
2169 /* packet supported only by smp target i.e cortex_a.c*/
2170 /* handle smp packet replying coreid played to gbd */
2171 gdb_read_smp_packet(connection, packet, packet_size);
2175 /* packet supported only by smp target i.e cortex_a.c */
2176 /* handle smp packet setting coreid to be played at next
2178 gdb_write_smp_packet(connection, packet, packet_size);
2182 /* ignore unknown packets */
2183 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2184 gdb_put_packet(connection, NULL, 0);
2188 /* if a packet handler returned an error, exit input loop */
2189 if (retval != ERROR_OK)
2193 if (gdb_con->ctrl_c) {
2194 if (target->state == TARGET_RUNNING) {
2195 retval = target_halt(target);
2196 if (retval != ERROR_OK)
2197 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2198 gdb_con->ctrl_c = 0;
2200 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2201 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2205 } while (gdb_con->buf_cnt > 0);
2210 static int gdb_input(struct connection *connection)
2212 int retval = gdb_input_inner(connection);
2213 struct gdb_connection *gdb_con = connection->priv;
2214 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2217 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2218 if (gdb_con->closed)
2219 return ERROR_SERVER_REMOTE_CLOSED;
2221 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2225 static int gdb_target_start(struct target *target, const char *port)
2227 struct gdb_service *gdb_service;
2229 gdb_service = malloc(sizeof(struct gdb_service));
2231 if (NULL == gdb_service)
2234 gdb_service->target = target;
2235 gdb_service->core[0] = -1;
2236 gdb_service->core[1] = -1;
2237 target->gdb_service = gdb_service;
2239 ret = add_service("gdb",
2240 port, 1, &gdb_new_connection, &gdb_input,
2241 &gdb_connection_closed, gdb_service);
2242 /* initialialize all targets gdb service with the same pointer */
2244 struct target_list *head;
2245 struct target *curr;
2246 head = target->head;
2247 while (head != (struct target_list *)NULL) {
2248 curr = head->target;
2250 curr->gdb_service = gdb_service;
2257 static int gdb_target_add_one(struct target *target)
2259 /* one gdb instance per smp list */
2260 if ((target->smp) && (target->gdb_service))
2262 int retval = gdb_target_start(target, gdb_port_next);
2263 if (retval == ERROR_OK) {
2265 /* If we can parse the port number
2266 * then we increment the port number for the next target.
2269 portnumber = strtol(gdb_port_next, &end, 0);
2271 if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
2272 free((void *)gdb_port_next);
2273 gdb_port_next = alloc_printf("%d", portnumber+1);
2280 int gdb_target_add_all(struct target *target)
2282 if (NULL == target) {
2283 LOG_WARNING("gdb services need one or more targets defined");
2287 while (NULL != target) {
2288 int retval = gdb_target_add_one(target);
2289 if (ERROR_OK != retval)
2292 target = target->next;
2298 COMMAND_HANDLER(handle_gdb_sync_command)
2301 return ERROR_COMMAND_SYNTAX_ERROR;
2303 if (current_gdb_connection == NULL) {
2304 command_print(CMD_CTX,
2305 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2309 current_gdb_connection->sync = true;
2314 /* daemon configuration command gdb_port */
2315 COMMAND_HANDLER(handle_gdb_port_command)
2317 int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
2318 if (ERROR_OK == retval) {
2319 free((void *)gdb_port_next);
2320 gdb_port_next = strdup(gdb_port);
2325 COMMAND_HANDLER(handle_gdb_memory_map_command)
2328 return ERROR_COMMAND_SYNTAX_ERROR;
2330 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
2334 COMMAND_HANDLER(handle_gdb_flash_program_command)
2337 return ERROR_COMMAND_SYNTAX_ERROR;
2339 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
2343 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
2346 return ERROR_COMMAND_SYNTAX_ERROR;
2348 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
2352 /* gdb_breakpoint_override */
2353 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
2355 if (CMD_ARGC == 0) {
2357 } else if (CMD_ARGC == 1) {
2358 gdb_breakpoint_override = 1;
2359 if (strcmp(CMD_ARGV[0], "hard") == 0)
2360 gdb_breakpoint_override_type = BKPT_HARD;
2361 else if (strcmp(CMD_ARGV[0], "soft") == 0)
2362 gdb_breakpoint_override_type = BKPT_SOFT;
2363 else if (strcmp(CMD_ARGV[0], "disable") == 0)
2364 gdb_breakpoint_override = 0;
2366 return ERROR_COMMAND_SYNTAX_ERROR;
2367 if (gdb_breakpoint_override)
2368 LOG_USER("force %s breakpoints",
2369 (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
2371 LOG_USER("breakpoint type is not overridden");
2376 static const struct command_registration gdb_command_handlers[] = {
2379 .handler = handle_gdb_sync_command,
2380 .mode = COMMAND_ANY,
2381 .help = "next stepi will return immediately allowing "
2382 "GDB to fetch register state without affecting "
2388 .handler = handle_gdb_port_command,
2389 .mode = COMMAND_ANY,
2390 .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2391 "server listens for the next port number after the "
2392 "base port number specified. "
2393 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2394 "output to stdout, an integer is base port number, \"disable\" disables "
2395 "port. Any other string is are interpreted as named pipe to listen to. "
2396 "Output pipe is the same name as input pipe, but with 'o' appended.",
2397 .usage = "[port_num]",
2400 .name = "gdb_memory_map",
2401 .handler = handle_gdb_memory_map_command,
2402 .mode = COMMAND_CONFIG,
2403 .help = "enable or disable memory map",
2404 .usage = "('enable'|'disable')"
2407 .name = "gdb_flash_program",
2408 .handler = handle_gdb_flash_program_command,
2409 .mode = COMMAND_CONFIG,
2410 .help = "enable or disable flash program",
2411 .usage = "('enable'|'disable')"
2414 .name = "gdb_report_data_abort",
2415 .handler = handle_gdb_report_data_abort_command,
2416 .mode = COMMAND_CONFIG,
2417 .help = "enable or disable reporting data aborts",
2418 .usage = "('enable'|'disable')"
2421 .name = "gdb_breakpoint_override",
2422 .handler = handle_gdb_breakpoint_override_command,
2423 .mode = COMMAND_ANY,
2424 .help = "Display or specify type of breakpoint "
2425 "to be used by gdb 'break' commands.",
2426 .usage = "('hard'|'soft'|'disable')"
2428 COMMAND_REGISTRATION_DONE
2431 int gdb_register_commands(struct command_context *cmd_ctx)
2433 gdb_port = strdup("3333");
2434 gdb_port_next = strdup("3333");
2435 return register_commands(cmd_ctx, NULL, gdb_command_handlers);