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 * Copyright (C) 2013 Andes Technology *
18 * Hsiangkai Wang <hkwang@andestech.com> *
20 * Copyright (C) 2013 Franck Jullien *
21 * elec4fun@gmail.com *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
35 ***************************************************************************/
41 #include <target/breakpoints.h>
42 #include <target/target_request.h>
43 #include <target/register.h>
44 #include <target/target.h>
45 #include <target/target_type.h>
47 #include <flash/nor/core.h>
48 #include "gdb_server.h"
49 #include <target/image.h>
50 #include <jtag/jtag.h>
51 #include "rtos/rtos.h"
52 #include "target/smp.h"
56 * GDB server implementation.
58 * This implements the GDB Remote Serial Protocol, over TCP connections,
59 * giving GDB access to the JTAG or other hardware debugging facilities
60 * found in most modern embedded processors.
63 struct target_desc_format {
65 uint32_t tdesc_length;
68 /* private connection data for GDB */
69 struct gdb_connection {
70 char buffer[GDB_BUFFER_SIZE];
74 enum target_state frontend_state;
75 struct image *vflash_image;
79 /* set flag to true if you want the next stepi to return immediately.
80 * allowing GDB to pick up a fresh set of register values from the target
81 * without modifying the target state. */
83 /* We delay reporting memory write errors until next step/continue or memory
84 * write. This improves performance of gdb load significantly as the GDB packet
85 * can be replied immediately and a new GDB packet will be ready without delay
86 * (ca. 10% or so...). */
88 /* with extended-remote it seems we need to better emulate attach/detach.
89 * what this means is we reply with a W stop reply after a kill packet,
90 * normally we reply with a S reply via gdb_last_signal_packet.
91 * as a side note this behaviour only effects gdb > 6.8 */
93 /* temporarily used for target description support */
94 struct target_desc_format target_desc;
95 /* temporarily used for thread list support */
100 #define _DEBUG_GDB_IO_
103 static struct gdb_connection *current_gdb_connection;
105 static int gdb_breakpoint_override;
106 static enum breakpoint_type gdb_breakpoint_override_type;
108 static int gdb_error(struct connection *connection, int retval);
109 static char *gdb_port;
110 static char *gdb_port_next;
112 static void gdb_log_callback(void *priv, const char *file, unsigned line,
113 const char *function, const char *string);
115 static void gdb_sig_halted(struct connection *connection);
117 /* number of gdb connections, mainly to suppress gdb related debugging spam
118 * in helper/log.c when no gdb connections are actually active */
119 int gdb_actual_connections;
121 /* set if we are sending a memory map to gdb
122 * via qXfer:memory-map:read packet */
123 /* enabled by default*/
124 static int gdb_use_memory_map = 1;
125 /* enabled by default*/
126 static int gdb_flash_program = 1;
128 /* if set, data aborts cause an error to be reported in memory read packets
129 * see the code in gdb_read_memory_packet() for further explanations.
130 * Disabled by default.
132 static int gdb_report_data_abort;
134 /* set if we are sending target descriptions to gdb
135 * via qXfer:features:read packet */
136 /* enabled by default */
137 static int gdb_use_target_description = 1;
139 /* current processing free-run type, used by file-I/O */
140 static char gdb_running_type;
142 static int gdb_last_signal(struct target *target)
144 switch (target->debug_reason) {
145 case DBG_REASON_DBGRQ:
146 return 0x2; /* SIGINT */
147 case DBG_REASON_BREAKPOINT:
148 case DBG_REASON_WATCHPOINT:
149 case DBG_REASON_WPTANDBKPT:
150 return 0x05; /* SIGTRAP */
151 case DBG_REASON_SINGLESTEP:
152 return 0x05; /* SIGTRAP */
153 case DBG_REASON_NOTHALTED:
154 return 0x0; /* no signal... shouldn't happen */
156 LOG_USER("undefined debug reason %d - target needs reset",
157 target->debug_reason);
162 static int check_pending(struct connection *connection,
163 int timeout_s, int *got_data)
165 /* a non-blocking socket will block if there is 0 bytes available on the socket,
166 * but return with as many bytes as are available immediately
170 struct gdb_connection *gdb_con = connection->priv;
172 if (got_data == NULL)
176 if (gdb_con->buf_cnt > 0) {
182 FD_SET(connection->fd, &read_fds);
184 tv.tv_sec = timeout_s;
186 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0) {
187 /* This can typically be because a "monitor" command took too long
188 * before printing any progress messages
191 return ERROR_GDB_TIMEOUT;
195 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
199 static int gdb_get_char_inner(struct connection *connection, int *next_char)
201 struct gdb_connection *gdb_con = connection->priv;
202 int retval = ERROR_OK;
204 #ifdef _DEBUG_GDB_IO_
208 if (connection->service->type != CONNECTION_TCP)
209 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
211 retval = check_pending(connection, 1, NULL);
212 if (retval != ERROR_OK)
214 gdb_con->buf_cnt = read_socket(connection->fd,
219 if (gdb_con->buf_cnt > 0)
221 if (gdb_con->buf_cnt == 0) {
222 gdb_con->closed = true;
223 return ERROR_SERVER_REMOTE_CLOSED;
227 errno = WSAGetLastError();
233 case WSAECONNABORTED:
234 gdb_con->closed = true;
235 return ERROR_SERVER_REMOTE_CLOSED;
237 gdb_con->closed = true;
238 return ERROR_SERVER_REMOTE_CLOSED;
240 LOG_ERROR("read: %d", errno);
249 gdb_con->closed = true;
250 return ERROR_SERVER_REMOTE_CLOSED;
252 gdb_con->closed = true;
253 return ERROR_SERVER_REMOTE_CLOSED;
255 LOG_ERROR("read: %s", strerror(errno));
256 gdb_con->closed = true;
257 return ERROR_SERVER_REMOTE_CLOSED;
262 #ifdef _DEBUG_GDB_IO_
263 debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
264 LOG_DEBUG("received '%s'", debug_buffer);
268 gdb_con->buf_p = gdb_con->buffer;
270 *next_char = *(gdb_con->buf_p++);
271 if (gdb_con->buf_cnt > 0)
272 connection->input_pending = 1;
274 connection->input_pending = 0;
275 #ifdef _DEBUG_GDB_IO_
276 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
283 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
284 * held in registers in the inner loop.
286 * For small caches and embedded systems this is important!
288 static inline int gdb_get_char_fast(struct connection *connection,
289 int *next_char, char **buf_p, int *buf_cnt)
291 int retval = ERROR_OK;
293 if ((*buf_cnt)-- > 0) {
294 *next_char = **buf_p;
297 connection->input_pending = 1;
299 connection->input_pending = 0;
301 #ifdef _DEBUG_GDB_IO_
302 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
308 struct gdb_connection *gdb_con = connection->priv;
309 gdb_con->buf_p = *buf_p;
310 gdb_con->buf_cnt = *buf_cnt;
311 retval = gdb_get_char_inner(connection, next_char);
312 *buf_p = gdb_con->buf_p;
313 *buf_cnt = gdb_con->buf_cnt;
318 static int gdb_get_char(struct connection *connection, int *next_char)
320 struct gdb_connection *gdb_con = connection->priv;
321 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
324 static int gdb_putback_char(struct connection *connection, int last_char)
326 struct gdb_connection *gdb_con = connection->priv;
328 if (gdb_con->buf_p > gdb_con->buffer) {
329 *(--gdb_con->buf_p) = last_char;
332 LOG_ERROR("BUG: couldn't put character back");
337 /* The only way we can detect that the socket is closed is the first time
338 * we write to it, we will fail. Subsequent write operations will
339 * succeed. Shudder! */
340 static int gdb_write(struct connection *connection, void *data, int len)
342 struct gdb_connection *gdb_con = connection->priv;
344 return ERROR_SERVER_REMOTE_CLOSED;
346 if (connection_write(connection, data, len) == len)
348 gdb_con->closed = true;
349 return ERROR_SERVER_REMOTE_CLOSED;
352 static int gdb_put_packet_inner(struct connection *connection,
353 char *buffer, int len)
356 unsigned char my_checksum = 0;
357 #ifdef _DEBUG_GDB_IO_
362 struct gdb_connection *gdb_con = connection->priv;
364 for (i = 0; i < len; i++)
365 my_checksum += buffer[i];
367 #ifdef _DEBUG_GDB_IO_
369 * At this point we should have nothing in the input queue from GDB,
370 * however sometimes '-' is sent even though we've already received
371 * an ACK (+) for everything we've sent off.
375 retval = check_pending(connection, 0, &gotdata);
376 if (retval != ERROR_OK)
380 retval = gdb_get_char(connection, &reply);
381 if (retval != ERROR_OK)
384 /* fix a problem with some IAR tools */
385 gdb_putback_char(connection, reply);
386 LOG_DEBUG("Unexpected start of new packet");
390 LOG_WARNING("Discard unexpected char %c", reply);
395 #ifdef _DEBUG_GDB_IO_
396 debug_buffer = strndup(buffer, len);
397 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
401 char local_buffer[1024];
402 local_buffer[0] = '$';
403 if ((size_t)len + 4 <= sizeof(local_buffer)) {
404 /* performance gain on smaller packets by only a single call to gdb_write() */
405 memcpy(local_buffer + 1, buffer, len++);
406 len += snprintf(local_buffer + len, sizeof(local_buffer) - len, "#%02x", my_checksum);
407 retval = gdb_write(connection, local_buffer, len);
408 if (retval != ERROR_OK)
411 /* larger packets are transmitted directly from caller supplied buffer
412 * by several calls to gdb_write() to avoid dynamic allocation */
413 snprintf(local_buffer + 1, sizeof(local_buffer) - 1, "#%02x", my_checksum);
414 retval = gdb_write(connection, local_buffer, 1);
415 if (retval != ERROR_OK)
417 retval = gdb_write(connection, buffer, len);
418 if (retval != ERROR_OK)
420 retval = gdb_write(connection, local_buffer + 1, 3);
421 if (retval != ERROR_OK)
425 if (gdb_con->noack_mode)
428 retval = gdb_get_char(connection, &reply);
429 if (retval != ERROR_OK)
434 else if (reply == '-') {
435 /* Stop sending output packets for now */
436 log_remove_callback(gdb_log_callback, connection);
437 LOG_WARNING("negative reply, retrying");
438 } else if (reply == 0x3) {
440 retval = gdb_get_char(connection, &reply);
441 if (retval != ERROR_OK)
445 else if (reply == '-') {
446 /* Stop sending output packets for now */
447 log_remove_callback(gdb_log_callback, connection);
448 LOG_WARNING("negative reply, retrying");
449 } else if (reply == '$') {
450 LOG_ERROR("GDB missing ack(1) - assumed good");
451 gdb_putback_char(connection, reply);
454 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
455 gdb_con->closed = true;
456 return ERROR_SERVER_REMOTE_CLOSED;
458 } else if (reply == '$') {
459 LOG_ERROR("GDB missing ack(2) - assumed good");
460 gdb_putback_char(connection, reply);
463 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
465 gdb_con->closed = true;
466 return ERROR_SERVER_REMOTE_CLOSED;
470 return ERROR_SERVER_REMOTE_CLOSED;
475 int gdb_put_packet(struct connection *connection, char *buffer, int len)
477 struct gdb_connection *gdb_con = connection->priv;
478 gdb_con->busy = true;
479 int retval = gdb_put_packet_inner(connection, buffer, len);
480 gdb_con->busy = false;
482 /* we sent some data, reset timer for keep alive messages */
488 static inline int fetch_packet(struct connection *connection,
489 int *checksum_ok, int noack, int *len, char *buffer)
491 unsigned char my_checksum = 0;
494 int retval = ERROR_OK;
496 struct gdb_connection *gdb_con = connection->priv;
501 /* move this over into local variables to use registers and give the
502 * more freedom to optimize */
503 char *buf_p = gdb_con->buf_p;
504 int buf_cnt = gdb_con->buf_cnt;
507 /* The common case is that we have an entire packet with no escape chars.
508 * We need to leave at least 2 bytes in the buffer to have
509 * gdb_get_char() update various bits and bobs correctly.
511 if ((buf_cnt > 2) && ((buf_cnt + count) < *len)) {
512 /* The compiler will struggle a bit with constant propagation and
513 * aliasing, so we help it by showing that these values do not
514 * change inside the loop
518 int run = buf_cnt - 2;
524 if (character == '#') {
525 /* Danger! character can be '#' when esc is
526 * used so we need an explicit boolean for done here. */
531 if (character == '}') {
532 /* data transmitted in binary mode (X packet)
533 * uses 0x7d as escape character */
534 my_checksum += character & 0xff;
537 my_checksum += character & 0xff;
538 buffer[count++] = (character ^ 0x20) & 0xff;
540 my_checksum += character & 0xff;
541 buffer[count++] = character & 0xff;
550 LOG_ERROR("packet buffer too small");
551 retval = ERROR_GDB_BUFFER_TOO_SMALL;
555 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
556 if (retval != ERROR_OK)
559 if (character == '#')
562 if (character == '}') {
563 /* data transmitted in binary mode (X packet)
564 * uses 0x7d as escape character */
565 my_checksum += character & 0xff;
567 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
568 if (retval != ERROR_OK)
571 my_checksum += character & 0xff;
572 buffer[count++] = (character ^ 0x20) & 0xff;
574 my_checksum += character & 0xff;
575 buffer[count++] = character & 0xff;
579 gdb_con->buf_p = buf_p;
580 gdb_con->buf_cnt = buf_cnt;
582 if (retval != ERROR_OK)
587 retval = gdb_get_char(connection, &character);
588 if (retval != ERROR_OK)
590 checksum[0] = character;
591 retval = gdb_get_char(connection, &character);
592 if (retval != ERROR_OK)
594 checksum[1] = character;
598 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
603 static int gdb_get_packet_inner(struct connection *connection,
604 char *buffer, int *len)
608 struct gdb_connection *gdb_con = connection->priv;
612 retval = gdb_get_char(connection, &character);
613 if (retval != ERROR_OK)
616 #ifdef _DEBUG_GDB_IO_
617 LOG_DEBUG("character: '%c'", character);
624 /* According to the GDB documentation
625 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
626 * "gdb sends a final `+` acknowledgment of the stub's `OK`
627 * response, which can be safely ignored by the stub."
628 * However OpenOCD server already is in noack mode at this
629 * point and instead of ignoring this it was emitting a
630 * warning. This code makes server ignore the first ACK
631 * that will be received after going into noack mode,
632 * warning only about subsequent ACK's. */
633 if (gdb_con->noack_mode > 1) {
634 LOG_WARNING("acknowledgment received, but no packet pending");
635 } else if (gdb_con->noack_mode) {
636 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
637 gdb_con->noack_mode = 2;
641 LOG_WARNING("negative acknowledgment, but no packet pending");
648 LOG_WARNING("ignoring character 0x%x", character);
651 } while (character != '$');
654 /* explicit code expansion here to get faster inlined code in -O3 by not
655 * calculating checksum */
656 if (gdb_con->noack_mode) {
657 retval = fetch_packet(connection, &checksum_ok, 1, len, buffer);
658 if (retval != ERROR_OK)
661 retval = fetch_packet(connection, &checksum_ok, 0, len, buffer);
662 if (retval != ERROR_OK)
666 if (gdb_con->noack_mode) {
667 /* checksum is not checked in noack mode */
671 retval = gdb_write(connection, "+", 1);
672 if (retval != ERROR_OK)
678 return ERROR_SERVER_REMOTE_CLOSED;
683 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
685 struct gdb_connection *gdb_con = connection->priv;
686 gdb_con->busy = true;
687 int retval = gdb_get_packet_inner(connection, buffer, len);
688 gdb_con->busy = false;
692 static int gdb_output_con(struct connection *connection, const char *line)
697 bin_size = strlen(line);
699 hex_buffer = malloc(bin_size * 2 + 2);
700 if (hex_buffer == NULL)
701 return ERROR_GDB_BUFFER_TOO_SMALL;
704 size_t pkt_len = hexify(hex_buffer + 1, (const uint8_t *)line, bin_size,
706 int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1);
712 static int gdb_output(struct command_context *context, const char *line)
714 /* this will be dumped to the log and also sent as an O packet if possible */
715 LOG_USER_N("%s", line);
719 static void gdb_signal_reply(struct target *target, struct connection *connection)
721 struct gdb_connection *gdb_connection = connection->priv;
723 char stop_reason[20];
724 char current_thread[25];
728 rtos_update_threads(target);
730 if (target->debug_reason == DBG_REASON_EXIT) {
731 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "W00");
733 if (gdb_connection->ctrl_c) {
736 signal_var = gdb_last_signal(target);
738 stop_reason[0] = '\0';
739 if (target->debug_reason == DBG_REASON_WATCHPOINT) {
740 enum watchpoint_rw hit_wp_type;
741 target_addr_t hit_wp_address;
743 if (watchpoint_hit(target, &hit_wp_type, &hit_wp_address) == ERROR_OK) {
745 switch (hit_wp_type) {
747 snprintf(stop_reason, sizeof(stop_reason),
748 "watch:%08" TARGET_PRIxADDR ";", hit_wp_address);
751 snprintf(stop_reason, sizeof(stop_reason),
752 "rwatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
755 snprintf(stop_reason, sizeof(stop_reason),
756 "awatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
764 current_thread[0] = '\0';
765 if (target->rtos != NULL) {
767 snprintf(current_thread, sizeof(current_thread), "thread:%016" PRIx64 ";",
768 target->rtos->current_thread);
769 target->rtos->current_threadid = target->rtos->current_thread;
770 target->rtos->gdb_target_for_threadid(connection, target->rtos->current_threadid, &ct);
771 if (!gdb_connection->ctrl_c)
772 signal_var = gdb_last_signal(ct);
775 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "T%2.2x%s%s",
776 signal_var, stop_reason, current_thread);
778 gdb_connection->ctrl_c = 0;
781 gdb_put_packet(connection, sig_reply, sig_reply_len);
782 gdb_connection->frontend_state = TARGET_HALTED;
785 static void gdb_fileio_reply(struct target *target, struct connection *connection)
787 struct gdb_connection *gdb_connection = connection->priv;
788 char fileio_command[256];
790 bool program_exited = false;
792 if (strcmp(target->fileio_info->identifier, "open") == 0)
793 sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32 ",%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
794 target->fileio_info->param_1,
795 target->fileio_info->param_2,
796 target->fileio_info->param_3,
797 target->fileio_info->param_4);
798 else if (strcmp(target->fileio_info->identifier, "close") == 0)
799 sprintf(fileio_command, "F%s,%" PRIx32, target->fileio_info->identifier,
800 target->fileio_info->param_1);
801 else if (strcmp(target->fileio_info->identifier, "read") == 0)
802 sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
803 target->fileio_info->param_1,
804 target->fileio_info->param_2,
805 target->fileio_info->param_3);
806 else if (strcmp(target->fileio_info->identifier, "write") == 0)
807 sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
808 target->fileio_info->param_1,
809 target->fileio_info->param_2,
810 target->fileio_info->param_3);
811 else if (strcmp(target->fileio_info->identifier, "lseek") == 0)
812 sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
813 target->fileio_info->param_1,
814 target->fileio_info->param_2,
815 target->fileio_info->param_3);
816 else if (strcmp(target->fileio_info->identifier, "rename") == 0)
817 sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32 ",%" PRIx32 "/%" PRIx32, target->fileio_info->identifier,
818 target->fileio_info->param_1,
819 target->fileio_info->param_2,
820 target->fileio_info->param_3,
821 target->fileio_info->param_4);
822 else if (strcmp(target->fileio_info->identifier, "unlink") == 0)
823 sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32, target->fileio_info->identifier,
824 target->fileio_info->param_1,
825 target->fileio_info->param_2);
826 else if (strcmp(target->fileio_info->identifier, "stat") == 0)
827 sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
828 target->fileio_info->param_1,
829 target->fileio_info->param_2,
830 target->fileio_info->param_3);
831 else if (strcmp(target->fileio_info->identifier, "fstat") == 0)
832 sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
833 target->fileio_info->param_1,
834 target->fileio_info->param_2);
835 else if (strcmp(target->fileio_info->identifier, "gettimeofday") == 0)
836 sprintf(fileio_command, "F%s,%" PRIx32 ",%" PRIx32, target->fileio_info->identifier,
837 target->fileio_info->param_1,
838 target->fileio_info->param_2);
839 else if (strcmp(target->fileio_info->identifier, "isatty") == 0)
840 sprintf(fileio_command, "F%s,%" PRIx32, target->fileio_info->identifier,
841 target->fileio_info->param_1);
842 else if (strcmp(target->fileio_info->identifier, "system") == 0)
843 sprintf(fileio_command, "F%s,%" PRIx32 "/%" PRIx32, target->fileio_info->identifier,
844 target->fileio_info->param_1,
845 target->fileio_info->param_2);
846 else if (strcmp(target->fileio_info->identifier, "exit") == 0) {
847 /* If target hits exit syscall, report to GDB the program is terminated.
848 * In addition, let target run its own exit syscall handler. */
849 program_exited = true;
850 sprintf(fileio_command, "W%02" PRIx32, target->fileio_info->param_1);
852 LOG_DEBUG("Unknown syscall: %s", target->fileio_info->identifier);
854 /* encounter unknown syscall, continue */
855 gdb_connection->frontend_state = TARGET_RUNNING;
856 target_resume(target, 1, 0x0, 0, 0);
860 command_len = strlen(fileio_command);
861 gdb_put_packet(connection, fileio_command, command_len);
863 if (program_exited) {
864 /* Use target_resume() to let target run its own exit syscall handler. */
865 gdb_connection->frontend_state = TARGET_RUNNING;
866 target_resume(target, 1, 0x0, 0, 0);
868 gdb_connection->frontend_state = TARGET_HALTED;
869 rtos_update_threads(target);
873 static void gdb_frontend_halted(struct target *target, struct connection *connection)
875 struct gdb_connection *gdb_connection = connection->priv;
877 /* In the GDB protocol when we are stepping or continuing execution,
878 * we have a lingering reply. Upon receiving a halted event
879 * when we have that lingering packet, we reply to the original
880 * step or continue packet.
882 * Executing monitor commands can bring the target in and
883 * out of the running state so we'll see lots of TARGET_EVENT_XXX
884 * that are to be ignored.
886 if (gdb_connection->frontend_state == TARGET_RUNNING) {
887 /* stop forwarding log packets! */
888 log_remove_callback(gdb_log_callback, connection);
890 /* check fileio first */
891 if (target_get_gdb_fileio_info(target, target->fileio_info) == ERROR_OK)
892 gdb_fileio_reply(target, connection);
894 gdb_signal_reply(target, connection);
898 static int gdb_target_callback_event_handler(struct target *target,
899 enum target_event event, void *priv)
902 struct connection *connection = priv;
903 struct gdb_service *gdb_service = connection->service->priv;
905 if (gdb_service->target != target)
909 case TARGET_EVENT_GDB_HALT:
910 gdb_frontend_halted(target, connection);
912 case TARGET_EVENT_HALTED:
913 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
915 case TARGET_EVENT_GDB_FLASH_ERASE_START:
916 retval = jtag_execute_queue();
917 if (retval != ERROR_OK)
927 static int gdb_new_connection(struct connection *connection)
929 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
930 struct target *target;
934 target = get_target_from_connection(connection);
935 connection->priv = gdb_connection;
937 /* initialize gdb connection information */
938 gdb_connection->buf_p = gdb_connection->buffer;
939 gdb_connection->buf_cnt = 0;
940 gdb_connection->ctrl_c = 0;
941 gdb_connection->frontend_state = TARGET_HALTED;
942 gdb_connection->vflash_image = NULL;
943 gdb_connection->closed = false;
944 gdb_connection->busy = false;
945 gdb_connection->noack_mode = 0;
946 gdb_connection->sync = false;
947 gdb_connection->mem_write_error = false;
948 gdb_connection->attached = true;
949 gdb_connection->target_desc.tdesc = NULL;
950 gdb_connection->target_desc.tdesc_length = 0;
951 gdb_connection->thread_list = NULL;
953 /* send ACK to GDB for debug request */
954 gdb_write(connection, "+", 1);
956 /* output goes through gdb connection */
957 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
959 /* we must remove all breakpoints registered to the target as a previous
960 * GDB session could leave dangling breakpoints if e.g. communication
963 breakpoint_clear_target(target);
964 watchpoint_clear_target(target);
967 /* clean previous rtos session if supported*/
968 if (target->rtos->type->clean)
969 target->rtos->type->clean(target);
972 rtos_update_threads(target);
975 /* remove the initial ACK from the incoming buffer */
976 retval = gdb_get_char(connection, &initial_ack);
977 if (retval != ERROR_OK)
980 /* FIX!!!??? would we actually ever receive a + here???
983 if (initial_ack != '+')
984 gdb_putback_char(connection, initial_ack);
985 target_call_event_callbacks(target, TARGET_EVENT_GDB_ATTACH);
987 if (gdb_use_memory_map) {
988 /* Connect must fail if the memory map can't be set up correctly.
990 * This will cause an auto_probe to be invoked, which is either
991 * a no-op or it will fail when the target isn't ready(e.g. not halted).
994 for (i = 0; i < flash_get_bank_count(); i++) {
995 struct flash_bank *p;
996 p = get_flash_bank_by_num_noprobe(i);
997 if (p->target != target)
999 retval = get_flash_bank_by_num(i, &p);
1000 if (retval != ERROR_OK) {
1001 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
1002 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1008 gdb_actual_connections++;
1009 log_printf_lf(all_targets->next != NULL ? LOG_LVL_INFO : LOG_LVL_DEBUG,
1010 __FILE__, __LINE__, __func__,
1011 "New GDB Connection: %d, Target %s, state: %s",
1012 gdb_actual_connections,
1013 target_name(target),
1014 target_state_name(target));
1016 /* DANGER! If we fail subsequently, we must remove this handler,
1017 * otherwise we occasionally see crashes as the timer can invoke the
1020 * register callback to be informed about target events */
1021 target_register_event_callback(gdb_target_callback_event_handler, connection);
1026 static int gdb_connection_closed(struct connection *connection)
1028 struct target *target;
1029 struct gdb_connection *gdb_connection = connection->priv;
1031 target = get_target_from_connection(connection);
1033 /* we're done forwarding messages. Tear down callback before
1034 * cleaning up connection.
1036 log_remove_callback(gdb_log_callback, connection);
1038 gdb_actual_connections--;
1039 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1040 target_name(target),
1041 target_state_name(target),
1042 gdb_actual_connections);
1044 /* see if an image built with vFlash commands is left */
1045 if (gdb_connection->vflash_image) {
1046 image_close(gdb_connection->vflash_image);
1047 free(gdb_connection->vflash_image);
1048 gdb_connection->vflash_image = NULL;
1051 /* if this connection registered a debug-message receiver delete it */
1052 delete_debug_msg_receiver(connection->cmd_ctx, target);
1054 if (connection->priv) {
1055 free(connection->priv);
1056 connection->priv = NULL;
1058 LOG_ERROR("BUG: connection->priv == NULL");
1060 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
1062 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
1064 target_call_event_callbacks(target, TARGET_EVENT_GDB_DETACH);
1069 static void gdb_send_error(struct connection *connection, uint8_t the_error)
1072 snprintf(err, 4, "E%2.2X", the_error);
1073 gdb_put_packet(connection, err, 3);
1076 static int gdb_last_signal_packet(struct connection *connection,
1077 char const *packet, int packet_size)
1079 struct target *target = get_target_from_connection(connection);
1080 struct gdb_connection *gdb_con = connection->priv;
1084 if (!gdb_con->attached) {
1085 /* if we are here we have received a kill packet
1086 * reply W stop reply otherwise gdb gets very unhappy */
1087 gdb_put_packet(connection, "W00", 3);
1091 signal_var = gdb_last_signal(target);
1093 snprintf(sig_reply, 4, "S%2.2x", signal_var);
1094 gdb_put_packet(connection, sig_reply, 3);
1099 static inline int gdb_reg_pos(struct target *target, int pos, int len)
1101 if (target->endianness == TARGET_LITTLE_ENDIAN)
1104 return len - 1 - pos;
1107 /* Convert register to string of bytes. NB! The # of bits in the
1108 * register might be non-divisible by 8(a byte), in which
1109 * case an entire byte is shown.
1111 * NB! the format on the wire is the target endianness
1113 * The format of reg->value is little endian
1116 static void gdb_str_to_target(struct target *target,
1117 char *tstr, struct reg *reg)
1124 buf_len = DIV_ROUND_UP(reg->size, 8);
1126 for (i = 0; i < buf_len; i++) {
1127 int j = gdb_reg_pos(target, i, buf_len);
1128 tstr += sprintf(tstr, "%02x", buf[j]);
1132 /* copy over in register buffer */
1133 static void gdb_target_to_reg(struct target *target,
1134 char const *tstr, int str_len, uint8_t *bin)
1137 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1142 for (i = 0; i < str_len; i += 2) {
1144 if (sscanf(tstr + i, "%02x", &t) != 1) {
1145 LOG_ERROR("BUG: unable to convert register value");
1149 int j = gdb_reg_pos(target, i/2, str_len/2);
1154 static int gdb_get_registers_packet(struct connection *connection,
1155 char const *packet, int packet_size)
1157 struct target *target = get_target_from_connection(connection);
1158 struct reg **reg_list;
1161 int reg_packet_size = 0;
1166 #ifdef _DEBUG_GDB_IO_
1170 if ((target->rtos != NULL) && (ERROR_OK == rtos_get_gdb_reg_list(connection)))
1173 retval = target_get_gdb_reg_list(target, ®_list, ®_list_size,
1175 if (retval != ERROR_OK)
1176 return gdb_error(connection, retval);
1178 for (i = 0; i < reg_list_size; i++)
1179 reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1181 assert(reg_packet_size > 0);
1183 reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
1184 if (reg_packet == NULL)
1187 reg_packet_p = reg_packet;
1189 for (i = 0; i < reg_list_size; i++) {
1190 if (!reg_list[i]->valid)
1191 reg_list[i]->type->get(reg_list[i]);
1192 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
1193 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1196 #ifdef _DEBUG_GDB_IO_
1198 char *reg_packet_p_debug;
1199 reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1200 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1201 free(reg_packet_p_debug);
1205 gdb_put_packet(connection, reg_packet, reg_packet_size);
1213 static int gdb_set_registers_packet(struct connection *connection,
1214 char const *packet, int packet_size)
1216 struct target *target = get_target_from_connection(connection);
1218 struct reg **reg_list;
1221 char const *packet_p;
1223 #ifdef _DEBUG_GDB_IO_
1227 /* skip command character */
1231 if (packet_size % 2) {
1232 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1233 return ERROR_SERVER_REMOTE_CLOSED;
1236 retval = target_get_gdb_reg_list(target, ®_list, ®_list_size,
1238 if (retval != ERROR_OK)
1239 return gdb_error(connection, retval);
1242 for (i = 0; i < reg_list_size; i++) {
1244 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1246 if (packet_p + chars > packet + packet_size)
1247 LOG_ERROR("BUG: register packet is too small for registers");
1249 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1250 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1252 reg_list[i]->type->set(reg_list[i], bin_buf);
1254 /* advance packet pointer */
1260 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1263 gdb_put_packet(connection, "OK", 2);
1268 static int gdb_get_register_packet(struct connection *connection,
1269 char const *packet, int packet_size)
1271 struct target *target = get_target_from_connection(connection);
1273 int reg_num = strtoul(packet + 1, NULL, 16);
1274 struct reg **reg_list;
1278 #ifdef _DEBUG_GDB_IO_
1282 retval = target_get_gdb_reg_list(target, ®_list, ®_list_size,
1284 if (retval != ERROR_OK)
1285 return gdb_error(connection, retval);
1287 if (reg_list_size <= reg_num) {
1288 LOG_ERROR("gdb requested a non-existing register");
1289 return ERROR_SERVER_REMOTE_CLOSED;
1292 if (!reg_list[reg_num]->valid)
1293 reg_list[reg_num]->type->get(reg_list[reg_num]);
1295 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1); /* plus one for string termination null */
1297 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1299 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1307 static int gdb_set_register_packet(struct connection *connection,
1308 char const *packet, int packet_size)
1310 struct target *target = get_target_from_connection(connection);
1313 int reg_num = strtoul(packet + 1, &separator, 16);
1314 struct reg **reg_list;
1320 retval = target_get_gdb_reg_list(target, ®_list, ®_list_size,
1322 if (retval != ERROR_OK)
1323 return gdb_error(connection, retval);
1325 if (reg_list_size <= reg_num) {
1326 LOG_ERROR("gdb requested a non-existing register");
1327 return ERROR_SERVER_REMOTE_CLOSED;
1330 if (*separator != '=') {
1331 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1332 return ERROR_SERVER_REMOTE_CLOSED;
1335 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1336 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1337 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1339 if ((unsigned int)chars != strlen(separator + 1)) {
1340 LOG_ERROR("gdb sent a packet with wrong register size");
1342 return ERROR_SERVER_REMOTE_CLOSED;
1345 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1347 reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1349 gdb_put_packet(connection, "OK", 2);
1357 /* No attempt is made to translate the "retval" to
1358 * GDB speak. This has to be done at the calling
1359 * site as no mapping really exists.
1361 static int gdb_error(struct connection *connection, int retval)
1363 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1364 gdb_send_error(connection, EFAULT);
1368 /* We don't have to worry about the default 2 second timeout for GDB packets,
1369 * because GDB breaks up large memory reads into smaller reads.
1371 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1373 static int gdb_read_memory_packet(struct connection *connection,
1374 char const *packet, int packet_size)
1376 struct target *target = get_target_from_connection(connection);
1384 int retval = ERROR_OK;
1386 /* skip command character */
1389 addr = strtoull(packet, &separator, 16);
1391 if (*separator != ',') {
1392 LOG_ERROR("incomplete read memory packet received, dropping connection");
1393 return ERROR_SERVER_REMOTE_CLOSED;
1396 len = strtoul(separator + 1, NULL, 16);
1399 LOG_WARNING("invalid read memory packet received (len == 0)");
1400 gdb_put_packet(connection, NULL, 0);
1404 buffer = malloc(len);
1406 LOG_DEBUG("addr: 0x%16.16" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1408 retval = target_read_buffer(target, addr, len, buffer);
1410 if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1411 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1412 * At some point this might be fixed in GDB, in which case this code can be removed.
1414 * OpenOCD developers are acutely aware of this problem, but there is nothing
1415 * gained by involving the user in this problem that hopefully will get resolved
1418 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1419 * cmd = view%20audit-trail&database = gdb&pr = 2395
1421 * For now, the default is to fix up things to make current GDB versions work.
1422 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1424 memset(buffer, 0, len);
1428 if (retval == ERROR_OK) {
1429 hex_buffer = malloc(len * 2 + 1);
1431 size_t pkt_len = hexify(hex_buffer, buffer, len, len * 2 + 1);
1433 gdb_put_packet(connection, hex_buffer, pkt_len);
1437 retval = gdb_error(connection, retval);
1444 static int gdb_write_memory_packet(struct connection *connection,
1445 char const *packet, int packet_size)
1447 struct target *target = get_target_from_connection(connection);
1455 /* skip command character */
1458 addr = strtoull(packet, &separator, 16);
1460 if (*separator != ',') {
1461 LOG_ERROR("incomplete write memory packet received, dropping connection");
1462 return ERROR_SERVER_REMOTE_CLOSED;
1465 len = strtoul(separator + 1, &separator, 16);
1467 if (*(separator++) != ':') {
1468 LOG_ERROR("incomplete write memory packet received, dropping connection");
1469 return ERROR_SERVER_REMOTE_CLOSED;
1472 buffer = malloc(len);
1474 LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1476 if (unhexify(buffer, separator, len) != len)
1477 LOG_ERROR("unable to decode memory packet");
1479 retval = target_write_buffer(target, addr, len, buffer);
1481 if (retval == ERROR_OK)
1482 gdb_put_packet(connection, "OK", 2);
1484 retval = gdb_error(connection, retval);
1491 static int gdb_write_memory_binary_packet(struct connection *connection,
1492 char const *packet, int packet_size)
1494 struct target *target = get_target_from_connection(connection);
1499 int retval = ERROR_OK;
1500 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1501 * the assumption that we're in a download and it's important to go as fast
1503 uint32_t fast_limit = 8;
1505 /* skip command character */
1508 addr = strtoull(packet, &separator, 16);
1510 if (*separator != ',') {
1511 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1512 return ERROR_SERVER_REMOTE_CLOSED;
1515 len = strtoul(separator + 1, &separator, 16);
1517 if (*(separator++) != ':') {
1518 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1519 return ERROR_SERVER_REMOTE_CLOSED;
1522 struct gdb_connection *gdb_connection = connection->priv;
1524 if (gdb_connection->mem_write_error)
1525 retval = ERROR_FAIL;
1527 if (retval == ERROR_OK) {
1528 if (len >= fast_limit) {
1529 /* By replying the packet *immediately* GDB will send us a new packet
1530 * while we write the last one to the target.
1531 * We only do this for larger writes, so that users who do something like:
1532 * p *((int*)0xdeadbeef)=8675309
1533 * will get immediate feedback that that write failed.
1535 gdb_put_packet(connection, "OK", 2);
1538 retval = gdb_error(connection, retval);
1539 /* now that we have reported the memory write error, we can clear the condition */
1540 gdb_connection->mem_write_error = false;
1541 if (retval != ERROR_OK)
1546 LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1548 retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1549 if (retval != ERROR_OK)
1550 gdb_connection->mem_write_error = true;
1553 if (len < fast_limit) {
1554 if (retval != ERROR_OK) {
1555 gdb_error(connection, retval);
1556 gdb_connection->mem_write_error = false;
1558 gdb_put_packet(connection, "OK", 2);
1565 static int gdb_step_continue_packet(struct connection *connection,
1566 char const *packet, int packet_size)
1568 struct target *target = get_target_from_connection(connection);
1570 uint64_t address = 0x0;
1571 int retval = ERROR_OK;
1575 if (packet_size > 1)
1576 address = strtoull(packet + 1, NULL, 16);
1580 gdb_running_type = packet[0];
1581 if (packet[0] == 'c') {
1582 LOG_DEBUG("continue");
1583 /* resume at current address, don't handle breakpoints, not debugging */
1584 retval = target_resume(target, current, address, 0, 0);
1585 } else if (packet[0] == 's') {
1587 /* step at current or address, don't handle breakpoints */
1588 retval = target_step(target, current, address, 0);
1593 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1594 char const *packet, int packet_size)
1596 struct target *target = get_target_from_connection(connection);
1598 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1599 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1607 type = strtoul(packet + 1, &separator, 16);
1609 if (type == 0) /* memory breakpoint */
1610 bp_type = BKPT_SOFT;
1611 else if (type == 1) /* hardware breakpoint */
1612 bp_type = BKPT_HARD;
1613 else if (type == 2) /* write watchpoint */
1614 wp_type = WPT_WRITE;
1615 else if (type == 3) /* read watchpoint */
1617 else if (type == 4) /* access watchpoint */
1618 wp_type = WPT_ACCESS;
1620 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1621 return ERROR_SERVER_REMOTE_CLOSED;
1624 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1625 bp_type = gdb_breakpoint_override_type;
1627 if (*separator != ',') {
1628 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1629 return ERROR_SERVER_REMOTE_CLOSED;
1632 address = strtoull(separator + 1, &separator, 16);
1634 if (*separator != ',') {
1635 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1636 return ERROR_SERVER_REMOTE_CLOSED;
1639 size = strtoul(separator + 1, &separator, 16);
1644 if (packet[0] == 'Z') {
1645 retval = breakpoint_add(target, address, size, bp_type);
1646 if (retval != ERROR_OK) {
1647 retval = gdb_error(connection, retval);
1648 if (retval != ERROR_OK)
1651 gdb_put_packet(connection, "OK", 2);
1653 breakpoint_remove(target, address);
1654 gdb_put_packet(connection, "OK", 2);
1661 if (packet[0] == 'Z') {
1662 retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu);
1663 if (retval != ERROR_OK) {
1664 retval = gdb_error(connection, retval);
1665 if (retval != ERROR_OK)
1668 gdb_put_packet(connection, "OK", 2);
1670 watchpoint_remove(target, address);
1671 gdb_put_packet(connection, "OK", 2);
1682 /* print out a string and allocate more space as needed,
1683 * mainly used for XML at this point
1685 static void xml_printf(int *retval, char **xml, int *pos, int *size,
1686 const char *fmt, ...)
1688 if (*retval != ERROR_OK)
1693 if ((*xml == NULL) || (!first)) {
1694 /* start by 0 to exercise all the code paths.
1695 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1697 *size = *size * 2 + 2;
1699 *xml = realloc(*xml, *size);
1703 *retval = ERROR_SERVER_REMOTE_CLOSED;
1711 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1713 if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1717 /* there was just enough or not enough space, allocate more. */
1722 static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
1724 /* Locate the annex. */
1725 const char *annex_end = strchr(buf, ':');
1726 if (annex_end == NULL)
1729 /* After the read marker and annex, qXfer looks like a
1730 * traditional 'm' packet. */
1732 *ofs = strtoul(annex_end + 1, &separator, 16);
1734 if (*separator != ',')
1737 *len = strtoul(separator + 1, NULL, 16);
1739 /* Extract the annex if needed */
1740 if (annex != NULL) {
1741 *annex = strndup(buf, annex_end - buf);
1749 static int compare_bank(const void *a, const void *b)
1751 struct flash_bank *b1, *b2;
1752 b1 = *((struct flash_bank **)a);
1753 b2 = *((struct flash_bank **)b);
1755 if (b1->base == b2->base)
1757 else if (b1->base > b2->base)
1763 static int gdb_memory_map(struct connection *connection,
1764 char const *packet, int packet_size)
1766 /* We get away with only specifying flash here. Regions that are not
1767 * specified are treated as if we provided no memory map(if not we
1768 * could detect the holes and mark them as RAM).
1769 * Normally we only execute this code once, but no big deal if we
1770 * have to regenerate it a couple of times.
1773 struct target *target = get_target_from_connection(connection);
1774 struct flash_bank *p;
1778 int retval = ERROR_OK;
1779 struct flash_bank **banks;
1783 uint32_t ram_start = 0;
1785 int target_flash_banks = 0;
1787 /* skip command character */
1790 offset = strtoul(packet, &separator, 16);
1791 length = strtoul(separator + 1, &separator, 16);
1793 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1795 /* Sort banks in ascending order. We need to report non-flash
1796 * memory as ram (or rather read/write) by default for GDB, since
1797 * it has no concept of non-cacheable read/write memory (i/o etc).
1799 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1800 * Current versions of GDB assume unlisted addresses are RAM...
1802 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1804 for (i = 0; i < flash_get_bank_count(); i++) {
1805 p = get_flash_bank_by_num_noprobe(i);
1806 if (p->target != target)
1808 retval = get_flash_bank_by_num(i, &p);
1809 if (retval != ERROR_OK) {
1811 gdb_error(connection, retval);
1814 banks[target_flash_banks++] = p;
1817 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1820 for (i = 0; i < target_flash_banks; i++) {
1822 unsigned sector_size = 0;
1828 if (ram_start < p->base)
1829 xml_printf(&retval, &xml, &pos, &size,
1830 "<memory type=\"ram\" start=\"0x%x\" "
1831 "length=\"0x%x\"/>\n",
1832 ram_start, p->base - ram_start);
1834 /* Report adjacent groups of same-size sectors. So for
1835 * example top boot CFI flash will list an initial region
1836 * with several large sectors (maybe 128KB) and several
1837 * smaller ones at the end (maybe 32KB). STR7 will have
1838 * regions with 8KB, 32KB, and 64KB sectors; etc.
1840 for (j = 0; j < p->num_sectors; j++) {
1843 /* Maybe start a new group of sectors. */
1844 if (sector_size == 0) {
1845 start = p->base + p->sectors[j].offset;
1846 xml_printf(&retval, &xml, &pos, &size,
1847 "<memory type=\"flash\" "
1850 sector_size = p->sectors[j].size;
1853 /* Does this finish a group of sectors?
1854 * If not, continue an already-started group.
1856 if (j == p->num_sectors - 1)
1857 group_len = (p->base + p->size) - start;
1858 else if (p->sectors[j + 1].size != sector_size)
1859 group_len = p->base + p->sectors[j + 1].offset
1864 xml_printf(&retval, &xml, &pos, &size,
1865 "length=\"0x%x\">\n"
1866 "<property name=\"blocksize\">"
1874 ram_start = p->base + p->size;
1878 xml_printf(&retval, &xml, &pos, &size,
1879 "<memory type=\"ram\" start=\"0x%x\" "
1880 "length=\"0x%x\"/>\n",
1881 ram_start, 0-ram_start);
1882 /* ELSE a flash chip could be at the very end of the 32 bit address
1883 * space, in which case ram_start will be precisely 0
1889 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1891 if (retval != ERROR_OK) {
1892 gdb_error(connection, retval);
1896 if (offset + length > pos)
1897 length = pos - offset;
1899 char *t = malloc(length + 1);
1901 memcpy(t + 1, xml + offset, length);
1902 gdb_put_packet(connection, t, length + 1);
1909 static const char *gdb_get_reg_type_name(enum reg_type type)
1916 case REG_TYPE_INT16:
1918 case REG_TYPE_INT32:
1920 case REG_TYPE_INT64:
1922 case REG_TYPE_INT128:
1924 case REG_TYPE_UINT8:
1926 case REG_TYPE_UINT16:
1928 case REG_TYPE_UINT32:
1930 case REG_TYPE_UINT64:
1932 case REG_TYPE_UINT128:
1934 case REG_TYPE_CODE_PTR:
1936 case REG_TYPE_DATA_PTR:
1938 case REG_TYPE_FLOAT:
1940 case REG_TYPE_IEEE_SINGLE:
1941 return "ieee_single";
1942 case REG_TYPE_IEEE_DOUBLE:
1943 return "ieee_double";
1944 case REG_TYPE_ARCH_DEFINED:
1945 return "int"; /* return arbitrary string to avoid compile warning. */
1948 return "int"; /* "int" as default value */
1951 static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id,
1952 int *num_arch_defined_types)
1954 int tbl_sz = *num_arch_defined_types;
1956 if (type_id != NULL && (strcmp(type_id, ""))) {
1957 for (int j = 0; j < (tbl_sz + 1); j++) {
1958 if (!((*arch_defined_types_list)[j])) {
1959 (*arch_defined_types_list)[tbl_sz++] = type_id;
1960 *arch_defined_types_list = realloc(*arch_defined_types_list,
1961 sizeof(char *) * (tbl_sz + 1));
1962 (*arch_defined_types_list)[tbl_sz] = NULL;
1963 *num_arch_defined_types = tbl_sz;
1966 if (!strcmp((*arch_defined_types_list)[j], type_id))
1975 static int gdb_generate_reg_type_description(struct target *target,
1976 char **tdesc, int *pos, int *size, struct reg_data_type *type,
1977 char const **arch_defined_types_list[], int * num_arch_defined_types)
1979 int retval = ERROR_OK;
1981 if (type->type_class == REG_TYPE_CLASS_VECTOR) {
1982 struct reg_data_type *data_type = type->reg_type_vector->type;
1983 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
1984 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
1985 num_arch_defined_types))
1986 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
1987 arch_defined_types_list,
1988 num_arch_defined_types);
1990 /* <vector id="id" type="type" count="count"/> */
1991 xml_printf(&retval, tdesc, pos, size,
1992 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
1993 type->id, type->reg_type_vector->type->id,
1994 type->reg_type_vector->count);
1996 } else if (type->type_class == REG_TYPE_CLASS_UNION) {
1997 struct reg_data_type_union_field *field;
1998 field = type->reg_type_union->fields;
1999 while (field != NULL) {
2000 struct reg_data_type *data_type = field->type;
2001 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2002 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2003 num_arch_defined_types))
2004 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2005 arch_defined_types_list,
2006 num_arch_defined_types);
2009 field = field->next;
2012 * <field name="name" type="type"/> ...
2014 xml_printf(&retval, tdesc, pos, size,
2015 "<union id=\"%s\">\n",
2018 field = type->reg_type_union->fields;
2019 while (field != NULL) {
2020 xml_printf(&retval, tdesc, pos, size,
2021 "<field name=\"%s\" type=\"%s\"/>\n",
2022 field->name, field->type->id);
2024 field = field->next;
2027 xml_printf(&retval, tdesc, pos, size,
2030 } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
2031 struct reg_data_type_struct_field *field;
2032 field = type->reg_type_struct->fields;
2034 if (field->use_bitfields) {
2035 /* <struct id="id" size="size">
2036 * <field name="name" start="start" end="end"/> ...
2038 xml_printf(&retval, tdesc, pos, size,
2039 "<struct id=\"%s\" size=\"%d\">\n",
2040 type->id, type->reg_type_struct->size);
2041 while (field != NULL) {
2042 xml_printf(&retval, tdesc, pos, size,
2043 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
2044 field->name, field->bitfield->start,
2045 field->bitfield->end);
2047 field = field->next;
2050 while (field != NULL) {
2051 struct reg_data_type *data_type = field->type;
2052 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2053 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2054 num_arch_defined_types))
2055 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2056 arch_defined_types_list,
2057 num_arch_defined_types);
2062 * <field name="name" type="type"/> ...
2064 xml_printf(&retval, tdesc, pos, size,
2065 "<struct id=\"%s\">\n",
2067 while (field != NULL) {
2068 xml_printf(&retval, tdesc, pos, size,
2069 "<field name=\"%s\" type=\"%s\"/>\n",
2070 field->name, field->type->id);
2072 field = field->next;
2076 xml_printf(&retval, tdesc, pos, size,
2079 } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
2080 /* <flags id="id" size="size">
2081 * <field name="name" start="start" end="end"/> ...
2083 xml_printf(&retval, tdesc, pos, size,
2084 "<flags id=\"%s\" size=\"%d\">\n",
2085 type->id, type->reg_type_flags->size);
2087 struct reg_data_type_flags_field *field;
2088 field = type->reg_type_flags->fields;
2089 while (field != NULL) {
2090 xml_printf(&retval, tdesc, pos, size,
2091 "<field name=\"%s\" start=\"%d\" end=\"%d\"/>\n",
2092 field->name, field->bitfield->start, field->bitfield->end);
2094 field = field->next;
2097 xml_printf(&retval, tdesc, pos, size,
2105 /* Get a list of available target registers features. feature_list must
2106 * be freed by caller.
2108 static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
2109 struct reg **reg_list, int reg_list_size)
2113 /* Start with only one element */
2114 *feature_list = calloc(1, sizeof(char *));
2116 for (int i = 0; i < reg_list_size; i++) {
2117 if (reg_list[i]->exist == false)
2120 if (reg_list[i]->feature != NULL
2121 && reg_list[i]->feature->name != NULL
2122 && (strcmp(reg_list[i]->feature->name, ""))) {
2123 /* We found a feature, check if the feature is already in the
2124 * table. If not, allocate a new entry for the table and
2125 * put the new feature in it.
2127 for (int j = 0; j < (tbl_sz + 1); j++) {
2128 if (!((*feature_list)[j])) {
2129 (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
2130 *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
2131 (*feature_list)[tbl_sz] = NULL;
2134 if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
2141 if (feature_list_size)
2142 *feature_list_size = tbl_sz;
2147 static int gdb_generate_target_description(struct target *target, char **tdesc_out)
2149 int retval = ERROR_OK;
2150 struct reg **reg_list = NULL;
2152 char const **features = NULL;
2153 char const **arch_defined_types = NULL;
2154 int feature_list_size = 0;
2155 int num_arch_defined_types = 0;
2160 arch_defined_types = calloc(1, sizeof(char *));
2162 retval = target_get_gdb_reg_list(target, ®_list,
2163 ®_list_size, REG_CLASS_ALL);
2165 if (retval != ERROR_OK) {
2166 LOG_ERROR("get register list failed");
2167 retval = ERROR_FAIL;
2171 if (reg_list_size <= 0) {
2172 LOG_ERROR("get register list failed");
2173 retval = ERROR_FAIL;
2177 /* Get a list of available target registers features */
2178 retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2179 if (retval != ERROR_OK) {
2180 LOG_ERROR("Can't get the registers feature list");
2181 retval = ERROR_FAIL;
2185 /* If we found some features associated with registers, create sections */
2186 int current_feature = 0;
2188 xml_printf(&retval, &tdesc, &pos, &size,
2189 "<?xml version=\"1.0\"?>\n"
2190 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2191 "<target version=\"1.0\">\n");
2193 /* generate target description according to register list */
2194 if (features != NULL) {
2195 while (features[current_feature]) {
2197 xml_printf(&retval, &tdesc, &pos, &size,
2198 "<feature name=\"%s\">\n",
2199 features[current_feature]);
2202 for (i = 0; i < reg_list_size; i++) {
2204 if (reg_list[i]->exist == false)
2207 if (strcmp(reg_list[i]->feature->name, features[current_feature]))
2210 const char *type_str;
2211 if (reg_list[i]->reg_data_type != NULL) {
2212 if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
2213 /* generate <type... first, if there are architecture-defined types. */
2214 if (lookup_add_arch_defined_types(&arch_defined_types,
2215 reg_list[i]->reg_data_type->id,
2216 &num_arch_defined_types))
2217 gdb_generate_reg_type_description(target, &tdesc, &pos, &size,
2218 reg_list[i]->reg_data_type,
2219 &arch_defined_types,
2220 &num_arch_defined_types);
2222 type_str = reg_list[i]->reg_data_type->id;
2224 /* predefined type */
2225 type_str = gdb_get_reg_type_name(
2226 reg_list[i]->reg_data_type->type);
2229 /* Default type is "int" */
2233 xml_printf(&retval, &tdesc, &pos, &size,
2234 "<reg name=\"%s\"", reg_list[i]->name);
2235 xml_printf(&retval, &tdesc, &pos, &size,
2236 " bitsize=\"%d\"", reg_list[i]->size);
2237 xml_printf(&retval, &tdesc, &pos, &size,
2238 " regnum=\"%d\"", reg_list[i]->number);
2239 if (reg_list[i]->caller_save)
2240 xml_printf(&retval, &tdesc, &pos, &size,
2241 " save-restore=\"yes\"");
2243 xml_printf(&retval, &tdesc, &pos, &size,
2244 " save-restore=\"no\"");
2246 xml_printf(&retval, &tdesc, &pos, &size,
2247 " type=\"%s\"", type_str);
2249 if (reg_list[i]->group != NULL)
2250 xml_printf(&retval, &tdesc, &pos, &size,
2251 " group=\"%s\"", reg_list[i]->group);
2253 xml_printf(&retval, &tdesc, &pos, &size,
2257 xml_printf(&retval, &tdesc, &pos, &size,
2264 xml_printf(&retval, &tdesc, &pos, &size,
2270 free(arch_defined_types);
2272 if (retval == ERROR_OK)
2280 static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
2281 char **chunk, int32_t offset, uint32_t length)
2283 if (target_desc == NULL) {
2284 LOG_ERROR("Unable to Generate Target Description");
2288 char *tdesc = target_desc->tdesc;
2289 uint32_t tdesc_length = target_desc->tdesc_length;
2291 if (tdesc == NULL) {
2292 int retval = gdb_generate_target_description(target, &tdesc);
2293 if (retval != ERROR_OK) {
2294 LOG_ERROR("Unable to Generate Target Description");
2298 tdesc_length = strlen(tdesc);
2303 if (length < (tdesc_length - offset))
2304 transfer_type = 'm';
2306 transfer_type = 'l';
2308 *chunk = malloc(length + 2);
2309 if (*chunk == NULL) {
2310 LOG_ERROR("Unable to allocate memory");
2314 (*chunk)[0] = transfer_type;
2315 if (transfer_type == 'm') {
2316 strncpy((*chunk) + 1, tdesc + offset, length);
2317 (*chunk)[1 + length] = '\0';
2319 strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
2320 (*chunk)[1 + (tdesc_length - offset)] = '\0';
2322 /* After gdb-server sends out last chunk, invalidate tdesc. */
2328 target_desc->tdesc = tdesc;
2329 target_desc->tdesc_length = tdesc_length;
2334 static int gdb_target_description_supported(struct target *target, int *supported)
2336 int retval = ERROR_OK;
2337 struct reg **reg_list = NULL;
2338 int reg_list_size = 0;
2339 char const **features = NULL;
2340 int feature_list_size = 0;
2342 retval = target_get_gdb_reg_list(target, ®_list,
2343 ®_list_size, REG_CLASS_ALL);
2344 if (retval != ERROR_OK) {
2345 LOG_ERROR("get register list failed");
2349 if (reg_list_size <= 0) {
2350 LOG_ERROR("get register list failed");
2351 retval = ERROR_FAIL;
2355 /* Get a list of available target registers features */
2356 retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2357 if (retval != ERROR_OK) {
2358 LOG_ERROR("Can't get the registers feature list");
2363 if (feature_list_size)
2377 static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
2379 struct rtos *rtos = target->rtos;
2380 int retval = ERROR_OK;
2381 char *thread_list = NULL;
2385 xml_printf(&retval, &thread_list, &pos, &size,
2386 "<?xml version=\"1.0\"?>\n"
2390 for (int i = 0; i < rtos->thread_count; i++) {
2391 struct thread_detail *thread_detail = &rtos->thread_details[i];
2393 if (!thread_detail->exists)
2396 xml_printf(&retval, &thread_list, &pos, &size,
2397 "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
2399 if (thread_detail->thread_name_str != NULL)
2400 xml_printf(&retval, &thread_list, &pos, &size,
2401 "Name: %s", thread_detail->thread_name_str);
2403 if (thread_detail->extra_info_str != NULL) {
2404 if (thread_detail->thread_name_str != NULL)
2405 xml_printf(&retval, &thread_list, &pos, &size,
2407 xml_printf(&retval, &thread_list, &pos, &size,
2408 thread_detail->extra_info_str);
2411 xml_printf(&retval, &thread_list, &pos, &size,
2416 xml_printf(&retval, &thread_list, &pos, &size,
2419 if (retval == ERROR_OK)
2420 *thread_list_out = thread_list;
2427 static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
2428 char **chunk, int32_t offset, uint32_t length)
2430 if (*thread_list == NULL) {
2431 int retval = gdb_generate_thread_list(target, thread_list);
2432 if (retval != ERROR_OK) {
2433 LOG_ERROR("Unable to Generate Thread List");
2438 size_t thread_list_length = strlen(*thread_list);
2441 length = MIN(length, thread_list_length - offset);
2442 if (length < (thread_list_length - offset))
2443 transfer_type = 'm';
2445 transfer_type = 'l';
2447 *chunk = malloc(length + 2 + 3);
2448 /* Allocating extra 3 bytes prevents false positive valgrind report
2449 * of strlen(chunk) word access:
2450 * Invalid read of size 4
2451 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2452 if (*chunk == NULL) {
2453 LOG_ERROR("Unable to allocate memory");
2457 (*chunk)[0] = transfer_type;
2458 strncpy((*chunk) + 1, (*thread_list) + offset, length);
2459 (*chunk)[1 + length] = '\0';
2461 /* After gdb-server sends out last chunk, invalidate thread list. */
2462 if (transfer_type == 'l') {
2464 *thread_list = NULL;
2470 static int gdb_query_packet(struct connection *connection,
2471 char const *packet, int packet_size)
2473 struct command_context *cmd_ctx = connection->cmd_ctx;
2474 struct gdb_connection *gdb_connection = connection->priv;
2475 struct target *target = get_target_from_connection(connection);
2477 if (strncmp(packet, "qRcmd,", 6) == 0) {
2478 if (packet_size > 6) {
2480 cmd = malloc((packet_size - 6) / 2 + 1);
2481 size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
2484 /* We want to print all debug output to GDB connection */
2485 log_add_callback(gdb_log_callback, connection);
2486 target_call_timer_callbacks_now();
2487 /* some commands need to know the GDB connection, make note of current
2488 * GDB connection. */
2489 current_gdb_connection = gdb_connection;
2490 command_run_line(cmd_ctx, cmd);
2491 current_gdb_connection = NULL;
2492 target_call_timer_callbacks_now();
2493 log_remove_callback(gdb_log_callback, connection);
2496 gdb_put_packet(connection, "OK", 2);
2498 } else if (strncmp(packet, "qCRC:", 5) == 0) {
2499 if (packet_size > 5) {
2504 target_addr_t addr = 0;
2507 /* skip command character */
2510 addr = strtoull(packet, &separator, 16);
2512 if (*separator != ',') {
2513 LOG_ERROR("incomplete read memory packet received, dropping connection");
2514 return ERROR_SERVER_REMOTE_CLOSED;
2517 len = strtoul(separator + 1, NULL, 16);
2519 retval = target_checksum_memory(target, addr, len, &checksum);
2521 if (retval == ERROR_OK) {
2522 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
2523 gdb_put_packet(connection, gdb_reply, 9);
2525 retval = gdb_error(connection, retval);
2526 if (retval != ERROR_OK)
2532 } else if (strncmp(packet, "qSupported", 10) == 0) {
2533 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2534 * qXfer:features:read is supported for some targets */
2535 int retval = ERROR_OK;
2536 char *buffer = NULL;
2539 int gdb_target_desc_supported = 0;
2541 /* we need to test that the target supports target descriptions */
2542 retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
2543 if (retval != ERROR_OK) {
2544 LOG_INFO("Failed detecting Target Description Support, disabling");
2545 gdb_target_desc_supported = 0;
2548 /* support may be disabled globally */
2549 if (gdb_use_target_description == 0) {
2550 if (gdb_target_desc_supported)
2551 LOG_WARNING("Target Descriptions Supported, but disabled");
2552 gdb_target_desc_supported = 0;
2559 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2560 (GDB_BUFFER_SIZE - 1),
2561 ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2562 (gdb_target_desc_supported == 1) ? '+' : '-');
2564 if (retval != ERROR_OK) {
2565 gdb_send_error(connection, 01);
2569 gdb_put_packet(connection, buffer, strlen(buffer));
2573 } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
2574 && (flash_get_bank_count() > 0))
2575 return gdb_memory_map(connection, packet, packet_size);
2576 else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
2578 int retval = ERROR_OK;
2581 unsigned int length;
2583 /* skip command character */
2586 if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2587 gdb_send_error(connection, 01);
2591 /* Target should prepare correct target description for annex.
2592 * The first character of returned xml is 'm' or 'l'. 'm' for
2593 * there are *more* chunks to transfer. 'l' for it is the *last*
2594 * chunk of target description.
2596 retval = gdb_get_target_description_chunk(target, &gdb_connection->target_desc,
2597 &xml, offset, length);
2598 if (retval != ERROR_OK) {
2599 gdb_error(connection, retval);
2603 gdb_put_packet(connection, xml, strlen(xml));
2607 } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
2609 int retval = ERROR_OK;
2612 unsigned int length;
2614 /* skip command character */
2617 if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2618 gdb_send_error(connection, 01);
2622 /* Target should prepare correct thread list for annex.
2623 * The first character of returned xml is 'm' or 'l'. 'm' for
2624 * there are *more* chunks to transfer. 'l' for it is the *last*
2625 * chunk of target description.
2627 retval = gdb_get_thread_list_chunk(target, &gdb_connection->thread_list,
2628 &xml, offset, length);
2629 if (retval != ERROR_OK) {
2630 gdb_error(connection, retval);
2634 gdb_put_packet(connection, xml, strlen(xml));
2638 } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
2639 gdb_connection->noack_mode = 1;
2640 gdb_put_packet(connection, "OK", 2);
2644 gdb_put_packet(connection, "", 0);
2648 static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet, int packet_size)
2650 struct gdb_connection *gdb_connection = connection->priv;
2651 struct target *target = get_target_from_connection(connection);
2652 const char *parse = packet;
2655 /* query for vCont supported */
2656 if (parse[0] == '?') {
2657 if (target->type->step != NULL) {
2658 /* gdb doesn't accept c without C and s without S */
2659 gdb_put_packet(connection, "vCont;c;C;s;S", 13);
2665 if (parse[0] == ';') {
2670 /* simple case, a continue packet */
2671 if (parse[0] == 'c') {
2672 LOG_DEBUG("target %s continue", target_name(target));
2673 log_add_callback(gdb_log_callback, connection);
2674 retval = target_resume(target, 1, 0, 0, 0);
2675 if (retval == ERROR_TARGET_NOT_HALTED)
2676 LOG_INFO("target %s was not halted when resume was requested", target_name(target));
2678 /* poll target in an attempt to make its internal state consistent */
2679 if (retval != ERROR_OK) {
2680 retval = target_poll(target);
2681 if (retval != ERROR_OK)
2682 LOG_DEBUG("error polling target %s after failed resume", target_name(target));
2686 * We don't report errors to gdb here, move frontend_state to
2687 * TARGET_RUNNING to stay in sync with gdb's expectation of the
2690 gdb_connection->frontend_state = TARGET_RUNNING;
2691 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2696 /* single-step or step-over-breakpoint */
2697 if (parse[0] == 's') {
2698 bool fake_step = false;
2700 if (strncmp(parse, "s:", 2) == 0) {
2701 struct target *ct = target;
2709 thread_id = strtoll(parse, &endp, 16);
2711 packet_size -= endp - parse;
2715 if (target->rtos != NULL) {
2716 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
2717 rtos_update_threads(target);
2719 target->rtos->gdb_target_for_threadid(connection, thread_id, &ct);
2722 * check if the thread to be stepped is the current rtos thread
2723 * if not, we must fake the step
2725 if (target->rtos->current_thread != thread_id)
2729 if (parse[0] == ';') {
2733 if (parse[0] == 'c') {
2737 /* check if thread-id follows */
2738 if (parse[0] == ':') {
2743 tid = strtoll(parse, &endp, 16);
2744 if (tid == thread_id) {
2746 * Special case: only step a single thread (core),
2747 * keep the other threads halted. Currently, only
2748 * aarch64 target understands it. Other target types don't
2749 * care (nobody checks the actual value of 'current')
2750 * and it doesn't really matter. This deserves
2751 * a symbolic constant and a formal interface documentation
2754 LOG_DEBUG("request to step current core only");
2755 /* uncomment after checking that indeed other targets are safe */
2762 LOG_DEBUG("target %s single-step thread %"PRIx64, target_name(ct), thread_id);
2763 log_add_callback(gdb_log_callback, connection);
2764 target_call_event_callbacks(ct, TARGET_EVENT_GDB_START);
2767 * work around an annoying gdb behaviour: when the current thread
2768 * is changed in gdb, it assumes that the target can follow and also
2769 * make the thread current. This is an assumption that cannot hold
2770 * for a real target running a multi-threading OS. We just fake
2771 * the step to not trigger an internal error in gdb. See
2772 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
2776 char sig_reply[128];
2778 LOG_DEBUG("fake step thread %"PRIx64, thread_id);
2780 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply),
2781 "T05thread:%016"PRIx64";", thread_id);
2783 gdb_put_packet(connection, sig_reply, sig_reply_len);
2784 log_remove_callback(gdb_log_callback, connection);
2789 /* support for gdb_sync command */
2790 if (gdb_connection->sync) {
2791 gdb_connection->sync = false;
2792 if (ct->state == TARGET_HALTED) {
2793 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2794 "from the target.");
2795 gdb_sig_halted(connection);
2796 log_remove_callback(gdb_log_callback, connection);
2798 gdb_connection->frontend_state = TARGET_RUNNING;
2802 retval = target_step(ct, current_pc, 0, 0);
2803 if (retval == ERROR_TARGET_NOT_HALTED)
2804 LOG_INFO("target %s was not halted when step was requested", target_name(ct));
2806 /* if step was successful send a reply back to gdb */
2807 if (retval == ERROR_OK) {
2808 retval = target_poll(ct);
2809 if (retval != ERROR_OK)
2810 LOG_DEBUG("error polling target %s after successful step", target_name(ct));
2811 /* send back signal information */
2812 gdb_signal_reply(ct, connection);
2813 /* stop forwarding log packets! */
2814 log_remove_callback(gdb_log_callback, connection);
2816 gdb_connection->frontend_state = TARGET_RUNNING;
2818 LOG_ERROR("Unknown vCont packet");
2827 static int gdb_v_packet(struct connection *connection,
2828 char const *packet, int packet_size)
2830 struct gdb_connection *gdb_connection = connection->priv;
2831 struct target *target;
2834 target = get_target_from_connection(connection);
2836 if (strncmp(packet, "vCont", 5) == 0) {
2842 handled = gdb_handle_vcont_packet(connection, packet, packet_size);
2844 gdb_put_packet(connection, "", 0);
2849 /* if flash programming disabled - send a empty reply */
2851 if (gdb_flash_program == 0) {
2852 gdb_put_packet(connection, "", 0);
2856 if (strncmp(packet, "vFlashErase:", 12) == 0) {
2858 unsigned long length;
2860 char const *parse = packet + 12;
2861 if (*parse == '\0') {
2862 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2863 return ERROR_SERVER_REMOTE_CLOSED;
2866 addr = strtoul(parse, (char **)&parse, 16);
2868 if (*(parse++) != ',' || *parse == '\0') {
2869 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2870 return ERROR_SERVER_REMOTE_CLOSED;
2873 length = strtoul(parse, (char **)&parse, 16);
2875 if (*parse != '\0') {
2876 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2877 return ERROR_SERVER_REMOTE_CLOSED;
2880 /* assume all sectors need erasing - stops any problems
2881 * when flash_write is called multiple times */
2884 /* perform any target specific operations before the erase */
2885 target_call_event_callbacks(target,
2886 TARGET_EVENT_GDB_FLASH_ERASE_START);
2888 /* vFlashErase:addr,length messages require region start and
2889 * end to be "block" aligned ... if padding is ever needed,
2890 * GDB will have become dangerously confused.
2892 result = flash_erase_address_range(target, false, addr,
2895 /* perform any target specific operations after the erase */
2896 target_call_event_callbacks(target,
2897 TARGET_EVENT_GDB_FLASH_ERASE_END);
2900 if (result != ERROR_OK) {
2901 /* GDB doesn't evaluate the actual error number returned,
2902 * treat a failed erase as an I/O error
2904 gdb_send_error(connection, EIO);
2905 LOG_ERROR("flash_erase returned %i", result);
2907 gdb_put_packet(connection, "OK", 2);
2912 if (strncmp(packet, "vFlashWrite:", 12) == 0) {
2915 unsigned long length;
2916 char const *parse = packet + 12;
2918 if (*parse == '\0') {
2919 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2920 return ERROR_SERVER_REMOTE_CLOSED;
2922 addr = strtoul(parse, (char **)&parse, 16);
2923 if (*(parse++) != ':') {
2924 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2925 return ERROR_SERVER_REMOTE_CLOSED;
2927 length = packet_size - (parse - packet);
2929 /* create a new image if there isn't already one */
2930 if (gdb_connection->vflash_image == NULL) {
2931 gdb_connection->vflash_image = malloc(sizeof(struct image));
2932 image_open(gdb_connection->vflash_image, "", "build");
2935 /* create new section with content from packet buffer */
2936 retval = image_add_section(gdb_connection->vflash_image,
2937 addr, length, 0x0, (uint8_t const *)parse);
2938 if (retval != ERROR_OK)
2941 gdb_put_packet(connection, "OK", 2);
2946 if (strncmp(packet, "vFlashDone", 10) == 0) {
2949 /* process the flashing buffer. No need to erase as GDB
2950 * always issues a vFlashErase first. */
2951 target_call_event_callbacks(target,
2952 TARGET_EVENT_GDB_FLASH_WRITE_START);
2953 result = flash_write(target, gdb_connection->vflash_image,
2955 target_call_event_callbacks(target,
2956 TARGET_EVENT_GDB_FLASH_WRITE_END);
2957 if (result != ERROR_OK) {
2958 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
2959 gdb_put_packet(connection, "E.memtype", 9);
2961 gdb_send_error(connection, EIO);
2963 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
2964 gdb_put_packet(connection, "OK", 2);
2967 image_close(gdb_connection->vflash_image);
2968 free(gdb_connection->vflash_image);
2969 gdb_connection->vflash_image = NULL;
2974 gdb_put_packet(connection, "", 0);
2978 static int gdb_detach(struct connection *connection)
2980 target_call_event_callbacks(get_target_from_connection(connection),
2981 TARGET_EVENT_GDB_DETACH);
2983 return gdb_put_packet(connection, "OK", 2);
2986 /* The format of 'F' response packet is
2987 * Fretcode,errno,Ctrl-C flag;call-specific attachment
2989 static int gdb_fileio_response_packet(struct connection *connection,
2990 char const *packet, int packet_size)
2992 struct target *target = get_target_from_connection(connection);
2994 char *parsing_point;
2995 int fileio_retcode = strtoul(packet + 1, &separator, 16);
2996 int fileio_errno = 0;
2997 bool fileio_ctrl_c = false;
3002 if (*separator == ',') {
3003 parsing_point = separator + 1;
3004 fileio_errno = strtoul(parsing_point, &separator, 16);
3005 if (*separator == ',') {
3006 if (*(separator + 1) == 'C') {
3007 /* TODO: process ctrl-c */
3008 fileio_ctrl_c = true;
3013 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3014 fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false");
3016 retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c);
3017 if (retval != ERROR_OK)
3020 /* After File-I/O ends, keep continue or step */
3021 if (gdb_running_type == 'c')
3022 retval = target_resume(target, 1, 0x0, 0, 0);
3023 else if (gdb_running_type == 's')
3024 retval = target_step(target, 1, 0x0, 0);
3026 retval = ERROR_FAIL;
3028 if (retval != ERROR_OK)
3034 static void gdb_log_callback(void *priv, const char *file, unsigned line,
3035 const char *function, const char *string)
3037 struct connection *connection = priv;
3038 struct gdb_connection *gdb_con = connection->priv;
3040 if (gdb_con->busy) {
3041 /* do not reply this using the O packet */
3045 gdb_output_con(connection, string);
3048 static void gdb_sig_halted(struct connection *connection)
3051 snprintf(sig_reply, 4, "T%2.2x", 2);
3052 gdb_put_packet(connection, sig_reply, 3);
3055 static int gdb_input_inner(struct connection *connection)
3057 /* Do not allocate this on the stack */
3058 static char gdb_packet_buffer[GDB_BUFFER_SIZE];
3060 struct target *target;
3061 char const *packet = gdb_packet_buffer;
3064 struct gdb_connection *gdb_con = connection->priv;
3065 static int extended_protocol;
3067 target = get_target_from_connection(connection);
3069 /* drain input buffer. If one of the packets fail, then an error
3070 * packet is replied, if applicable.
3072 * This loop will terminate and the error code is returned.
3074 * The calling fn will check if this error is something that
3075 * can be recovered from, or if the connection must be closed.
3077 * If the error is recoverable, this fn is called again to
3078 * drain the rest of the buffer.
3081 packet_size = GDB_BUFFER_SIZE-1;
3082 retval = gdb_get_packet(connection, gdb_packet_buffer, &packet_size);
3083 if (retval != ERROR_OK)
3086 /* terminate with zero */
3087 gdb_packet_buffer[packet_size] = '\0';
3089 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
3090 if (packet[0] == 'X') {
3091 /* binary packets spew junk into the debug log stream */
3094 for (x = 0; (x < 49) && (packet[x] != ':'); x++)
3097 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
3099 LOG_DEBUG("received packet: '%s'", packet);
3102 if (packet_size > 0) {
3104 switch (packet[0]) {
3105 case 'T': /* Is thread alive? */
3106 gdb_thread_packet(connection, packet, packet_size);
3108 case 'H': /* Set current thread ( 'c' for step and continue,
3109 * 'g' for all other operations ) */
3110 gdb_thread_packet(connection, packet, packet_size);
3114 retval = gdb_thread_packet(connection, packet, packet_size);
3115 if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
3116 retval = gdb_query_packet(connection, packet, packet_size);
3119 retval = gdb_get_registers_packet(connection, packet, packet_size);
3122 retval = gdb_set_registers_packet(connection, packet, packet_size);
3125 retval = gdb_get_register_packet(connection, packet, packet_size);
3128 retval = gdb_set_register_packet(connection, packet, packet_size);
3131 retval = gdb_read_memory_packet(connection, packet, packet_size);
3134 retval = gdb_write_memory_packet(connection, packet, packet_size);
3138 retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
3141 gdb_last_signal_packet(connection, packet, packet_size);
3146 gdb_thread_packet(connection, packet, packet_size);
3147 log_add_callback(gdb_log_callback, connection);
3149 if (gdb_con->mem_write_error) {
3150 LOG_ERROR("Memory write failure!");
3152 /* now that we have reported the memory write error,
3153 * we can clear the condition */
3154 gdb_con->mem_write_error = false;
3157 bool nostep = false;
3158 bool already_running = false;
3159 if (target->state == TARGET_RUNNING) {
3160 LOG_WARNING("WARNING! The target is already running. "
3161 "All changes GDB did to registers will be discarded! "
3162 "Waiting for target to halt.");
3163 already_running = true;
3164 } else if (target->state != TARGET_HALTED) {
3165 LOG_WARNING("The target is not in the halted nor running stated, " \
3166 "stepi/continue ignored.");
3168 } else if ((packet[0] == 's') && gdb_con->sync) {
3169 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3170 * sent by GDB first to OpenOCD, thus defeating the check to
3171 * make only the single stepping have the sync feature...
3174 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
3175 "from the target.");
3177 gdb_con->sync = false;
3179 if (!already_running && nostep) {
3180 /* Either the target isn't in the halted state, then we can't
3181 * step/continue. This might be early setup, etc.
3183 * Or we want to allow GDB to pick up a fresh set of
3184 * register values without modifying the target state.
3187 gdb_sig_halted(connection);
3189 /* stop forwarding log packets! */
3190 log_remove_callback(gdb_log_callback, connection);
3192 /* We're running/stepping, in which case we can
3193 * forward log output until the target is halted
3195 gdb_con->frontend_state = TARGET_RUNNING;
3196 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
3198 if (!already_running) {
3199 /* Here we don't want packet processing to stop even if this fails,
3200 * so we use a local variable instead of retval. */
3201 retval = gdb_step_continue_packet(connection, packet, packet_size);
3202 if (retval != ERROR_OK) {
3203 /* we'll never receive a halted
3204 * condition... issue a false one..
3206 gdb_frontend_halted(target, connection);
3213 retval = gdb_v_packet(connection, packet, packet_size);
3216 retval = gdb_detach(connection);
3217 extended_protocol = 0;
3220 retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
3221 if (retval != ERROR_OK)
3225 if (extended_protocol != 0) {
3226 gdb_con->attached = false;
3229 gdb_put_packet(connection, "OK", 2);
3230 return ERROR_SERVER_REMOTE_CLOSED;
3232 /* handle extended remote protocol */
3233 extended_protocol = 1;
3234 gdb_put_packet(connection, "OK", 2);
3237 /* handle extended restart packet */
3238 breakpoint_clear_target(target);
3239 watchpoint_clear_target(target);
3240 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
3241 target_name(target));
3242 /* set connection as attached after reset */
3243 gdb_con->attached = true;
3244 /* info rtos parts */
3245 gdb_thread_packet(connection, packet, packet_size);
3249 /* packet supported only by smp target i.e cortex_a.c*/
3250 /* handle smp packet replying coreid played to gbd */
3251 gdb_read_smp_packet(connection, packet, packet_size);
3255 /* packet supported only by smp target i.e cortex_a.c */
3256 /* handle smp packet setting coreid to be played at next
3258 gdb_write_smp_packet(connection, packet, packet_size);
3262 /* File-I/O extension */
3263 /* After gdb uses host-side syscall to complete target file
3264 * I/O, gdb sends host-side syscall return value to target
3266 * The format of 'F' response packet is
3267 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3269 gdb_con->frontend_state = TARGET_RUNNING;
3270 log_add_callback(gdb_log_callback, connection);
3271 gdb_fileio_response_packet(connection, packet, packet_size);
3275 /* ignore unknown packets */
3276 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3277 gdb_put_packet(connection, NULL, 0);
3281 /* if a packet handler returned an error, exit input loop */
3282 if (retval != ERROR_OK)
3286 if (gdb_con->ctrl_c) {
3287 if (target->state == TARGET_RUNNING) {
3288 struct target *t = target;
3290 target->rtos->gdb_target_for_threadid(connection, target->rtos->current_threadid, &t);
3291 retval = target_halt(t);
3292 if (retval == ERROR_OK)
3293 retval = target_poll(t);
3294 if (retval != ERROR_OK)
3295 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
3296 gdb_con->ctrl_c = 0;
3298 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3299 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
3303 } while (gdb_con->buf_cnt > 0);
3308 static int gdb_input(struct connection *connection)
3310 int retval = gdb_input_inner(connection);
3311 struct gdb_connection *gdb_con = connection->priv;
3312 if (retval == ERROR_SERVER_REMOTE_CLOSED)
3315 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3316 if (gdb_con->closed)
3317 return ERROR_SERVER_REMOTE_CLOSED;
3319 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3323 static int gdb_target_start(struct target *target, const char *port)
3325 struct gdb_service *gdb_service;
3327 gdb_service = malloc(sizeof(struct gdb_service));
3329 if (NULL == gdb_service)
3332 gdb_service->target = target;
3333 gdb_service->core[0] = -1;
3334 gdb_service->core[1] = -1;
3335 target->gdb_service = gdb_service;
3337 ret = add_service("gdb",
3338 port, 1, &gdb_new_connection, &gdb_input,
3339 &gdb_connection_closed, gdb_service);
3340 /* initialialize all targets gdb service with the same pointer */
3342 struct target_list *head;
3343 struct target *curr;
3344 head = target->head;
3345 while (head != (struct target_list *)NULL) {
3346 curr = head->target;
3348 curr->gdb_service = gdb_service;
3355 static int gdb_target_add_one(struct target *target)
3357 if (strcmp(gdb_port, "disabled") == 0) {
3358 LOG_INFO("gdb port disabled");
3362 /* one gdb instance per smp list */
3363 if ((target->smp) && (target->gdb_service))
3365 int retval = gdb_target_start(target, gdb_port_next);
3366 if (retval == ERROR_OK) {
3368 /* If we can parse the port number
3369 * then we increment the port number for the next target.
3372 portnumber = strtol(gdb_port_next, &end, 0);
3374 if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
3375 free(gdb_port_next);
3377 gdb_port_next = alloc_printf("%d", portnumber+1);
3379 /* Don't increment if gdb_port is 0, since we're just
3380 * trying to allocate an unused port. */
3381 gdb_port_next = strdup("0");
3389 int gdb_target_add_all(struct target *target)
3391 if (strcmp(gdb_port, "disabled") == 0) {
3392 LOG_INFO("gdb server disabled");
3396 if (NULL == target) {
3397 LOG_WARNING("gdb services need one or more targets defined");
3401 while (NULL != target) {
3402 int retval = gdb_target_add_one(target);
3403 if (ERROR_OK != retval)
3406 target = target->next;
3412 COMMAND_HANDLER(handle_gdb_sync_command)
3415 return ERROR_COMMAND_SYNTAX_ERROR;
3417 if (current_gdb_connection == NULL) {
3418 command_print(CMD_CTX,
3419 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3423 current_gdb_connection->sync = true;
3428 /* daemon configuration command gdb_port */
3429 COMMAND_HANDLER(handle_gdb_port_command)
3431 int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
3432 if (ERROR_OK == retval) {
3433 free(gdb_port_next);
3434 gdb_port_next = strdup(gdb_port);
3439 COMMAND_HANDLER(handle_gdb_memory_map_command)
3442 return ERROR_COMMAND_SYNTAX_ERROR;
3444 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
3448 COMMAND_HANDLER(handle_gdb_flash_program_command)
3451 return ERROR_COMMAND_SYNTAX_ERROR;
3453 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
3457 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
3460 return ERROR_COMMAND_SYNTAX_ERROR;
3462 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
3466 /* gdb_breakpoint_override */
3467 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
3469 if (CMD_ARGC == 0) {
3471 } else if (CMD_ARGC == 1) {
3472 gdb_breakpoint_override = 1;
3473 if (strcmp(CMD_ARGV[0], "hard") == 0)
3474 gdb_breakpoint_override_type = BKPT_HARD;
3475 else if (strcmp(CMD_ARGV[0], "soft") == 0)
3476 gdb_breakpoint_override_type = BKPT_SOFT;
3477 else if (strcmp(CMD_ARGV[0], "disable") == 0)
3478 gdb_breakpoint_override = 0;
3480 return ERROR_COMMAND_SYNTAX_ERROR;
3481 if (gdb_breakpoint_override)
3482 LOG_USER("force %s breakpoints",
3483 (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
3485 LOG_USER("breakpoint type is not overridden");
3490 COMMAND_HANDLER(handle_gdb_target_description_command)
3493 return ERROR_COMMAND_SYNTAX_ERROR;
3495 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_target_description);
3499 COMMAND_HANDLER(handle_gdb_save_tdesc_command)
3502 uint32_t tdesc_length;
3503 struct target *target = get_current_target(CMD_CTX);
3505 int retval = gdb_generate_target_description(target, &tdesc);
3506 if (retval != ERROR_OK) {
3507 LOG_ERROR("Unable to Generate Target Description");
3511 tdesc_length = strlen(tdesc);
3513 struct fileio *fileio;
3514 size_t size_written;
3516 char *tdesc_filename = alloc_printf("%s.xml", target_type_name(target));
3517 if (tdesc_filename == NULL) {
3518 retval = ERROR_FAIL;
3522 retval = fileio_open(&fileio, tdesc_filename, FILEIO_WRITE, FILEIO_TEXT);
3524 if (retval != ERROR_OK) {
3525 LOG_ERROR("Can't open %s for writing", tdesc_filename);
3529 retval = fileio_write(fileio, tdesc_length, tdesc, &size_written);
3531 fileio_close(fileio);
3533 if (retval != ERROR_OK)
3534 LOG_ERROR("Error while writing the tdesc file");
3537 free(tdesc_filename);
3543 static const struct command_registration gdb_command_handlers[] = {
3546 .handler = handle_gdb_sync_command,
3547 .mode = COMMAND_ANY,
3548 .help = "next stepi will return immediately allowing "
3549 "GDB to fetch register state without affecting "
3555 .handler = handle_gdb_port_command,
3556 .mode = COMMAND_ANY,
3557 .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
3558 "server listens for the next port number after the "
3559 "base port number specified. "
3560 "No arguments reports GDB port. \"pipe\" means listen to stdin "
3561 "output to stdout, an integer is base port number, \"disabled\" disables "
3562 "port. Any other string is are interpreted as named pipe to listen to. "
3563 "Output pipe is the same name as input pipe, but with 'o' appended.",
3564 .usage = "[port_num]",
3567 .name = "gdb_memory_map",
3568 .handler = handle_gdb_memory_map_command,
3569 .mode = COMMAND_CONFIG,
3570 .help = "enable or disable memory map",
3571 .usage = "('enable'|'disable')"
3574 .name = "gdb_flash_program",
3575 .handler = handle_gdb_flash_program_command,
3576 .mode = COMMAND_CONFIG,
3577 .help = "enable or disable flash program",
3578 .usage = "('enable'|'disable')"
3581 .name = "gdb_report_data_abort",
3582 .handler = handle_gdb_report_data_abort_command,
3583 .mode = COMMAND_CONFIG,
3584 .help = "enable or disable reporting data aborts",
3585 .usage = "('enable'|'disable')"
3588 .name = "gdb_breakpoint_override",
3589 .handler = handle_gdb_breakpoint_override_command,
3590 .mode = COMMAND_ANY,
3591 .help = "Display or specify type of breakpoint "
3592 "to be used by gdb 'break' commands.",
3593 .usage = "('hard'|'soft'|'disable')"
3596 .name = "gdb_target_description",
3597 .handler = handle_gdb_target_description_command,
3598 .mode = COMMAND_CONFIG,
3599 .help = "enable or disable target description",
3600 .usage = "('enable'|'disable')"
3603 .name = "gdb_save_tdesc",
3604 .handler = handle_gdb_save_tdesc_command,
3605 .mode = COMMAND_EXEC,
3606 .help = "Save the target description file",
3608 COMMAND_REGISTRATION_DONE
3611 int gdb_register_commands(struct command_context *cmd_ctx)
3613 gdb_port = strdup("3333");
3614 gdb_port_next = strdup("3333");
3615 return register_commands(cmd_ctx, NULL, gdb_command_handlers);
3618 void gdb_service_free(void)
3621 free(gdb_port_next);