build: cleanup src/server directory
[openocd/andreasf.git] / src / server / gdb_server.c
blob238273bb0c9f09bb1bd922451a13ec9239c5095d
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * Copyright (C) 2011 by Broadcom Corporation *
12 * Evan Hunter - ehunter@broadcom.com *
13 * *
14 * Copyright (C) ST-Ericsson SA 2011 *
15 * michel.jaouen@stericsson.com : smp minimum support *
16 * *
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. *
21 * *
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. *
26 * *
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 ***************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
37 #include <target/breakpoints.h>
38 #include <target/target_request.h>
39 #include <target/register.h>
40 #include "server.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"
48 /**
49 * @file
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];
60 char *buf_p;
61 int buf_cnt;
62 int ctrl_c;
63 enum target_state frontend_state;
64 struct image *vflash_image;
65 int closed;
66 int busy;
67 int noack_mode;
68 bool sync; /* 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. */
71 /* We delay reporting memory write errors until next step/continue or memory
72 * write. This improves performance of gdb load significantly as the GDB packet
73 * can be replied immediately and a new GDB packet will be ready without delay
74 * (ca. 10% or so...).
76 bool mem_write_error;
79 #if 0
80 #define _DEBUG_GDB_IO_
81 #endif
83 static struct gdb_connection *current_gdb_connection;
85 static int gdb_breakpoint_override;
86 static enum breakpoint_type gdb_breakpoint_override_type;
88 static int gdb_error(struct connection *connection, int retval);
89 static const char *gdb_port;
90 static const char *gdb_port_next;
91 static const char DIGITS[16] = "0123456789abcdef";
93 static void gdb_log_callback(void *priv, const char *file, unsigned line,
94 const char *function, const char *string);
96 /* number of gdb connections, mainly to suppress gdb related debugging spam
97 * in helper/log.c when no gdb connections are actually active */
98 int gdb_actual_connections;
100 /* set if we are sending a memory map to gdb
101 * via qXfer:memory-map:read packet */
102 /* enabled by default*/
103 static int gdb_use_memory_map = 1;
104 /* enabled by default*/
105 static int gdb_flash_program = 1;
107 /* if set, data aborts cause an error to be reported in memory read packets
108 * see the code in gdb_read_memory_packet() for further explanations.
109 * Disabled by default.
111 static int gdb_report_data_abort;
113 static int gdb_last_signal(struct target *target)
115 switch (target->debug_reason) {
116 case DBG_REASON_DBGRQ:
117 return 0x2; /* SIGINT */
118 case DBG_REASON_BREAKPOINT:
119 case DBG_REASON_WATCHPOINT:
120 case DBG_REASON_WPTANDBKPT:
121 return 0x05; /* SIGTRAP */
122 case DBG_REASON_SINGLESTEP:
123 return 0x05; /* SIGTRAP */
124 case DBG_REASON_NOTHALTED:
125 return 0x0; /* no signal... shouldn't happen */
126 default:
127 LOG_USER("undefined debug reason %d - target needs reset",
128 target->debug_reason);
129 return 0x0;
133 static int check_pending(struct connection *connection,
134 int timeout_s, int *got_data)
136 /* a non-blocking socket will block if there is 0 bytes available on the socket,
137 * but return with as many bytes as are available immediately
139 struct timeval tv;
140 fd_set read_fds;
141 struct gdb_connection *gdb_con = connection->priv;
142 int t;
143 if (got_data == NULL)
144 got_data = &t;
145 *got_data = 0;
147 if (gdb_con->buf_cnt > 0) {
148 *got_data = 1;
149 return ERROR_OK;
152 FD_ZERO(&read_fds);
153 FD_SET(connection->fd, &read_fds);
155 tv.tv_sec = timeout_s;
156 tv.tv_usec = 0;
157 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0) {
158 /* This can typically be because a "monitor" command took too long
159 * before printing any progress messages
161 if (timeout_s > 0)
162 return ERROR_GDB_TIMEOUT;
163 else
164 return ERROR_OK;
166 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
167 return ERROR_OK;
170 static int gdb_get_char_inner(struct connection *connection, int *next_char)
172 struct gdb_connection *gdb_con = connection->priv;
173 int retval = ERROR_OK;
175 #ifdef _DEBUG_GDB_IO_
176 char *debug_buffer;
177 #endif
178 for (;; ) {
179 if (connection->service->type != CONNECTION_TCP)
180 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
181 else {
182 retval = check_pending(connection, 1, NULL);
183 if (retval != ERROR_OK)
184 return retval;
185 gdb_con->buf_cnt = read_socket(connection->fd,
186 gdb_con->buffer,
187 GDB_BUFFER_SIZE);
190 if (gdb_con->buf_cnt > 0)
191 break;
192 if (gdb_con->buf_cnt == 0) {
193 gdb_con->closed = 1;
194 return ERROR_SERVER_REMOTE_CLOSED;
197 #ifdef _WIN32
198 errno = WSAGetLastError();
200 switch (errno) {
201 case WSAEWOULDBLOCK:
202 usleep(1000);
203 break;
204 case WSAECONNABORTED:
205 gdb_con->closed = 1;
206 return ERROR_SERVER_REMOTE_CLOSED;
207 case WSAECONNRESET:
208 gdb_con->closed = 1;
209 return ERROR_SERVER_REMOTE_CLOSED;
210 default:
211 LOG_ERROR("read: %d", errno);
212 exit(-1);
214 #else
215 switch (errno) {
216 case EAGAIN:
217 usleep(1000);
218 break;
219 case ECONNABORTED:
220 gdb_con->closed = 1;
221 return ERROR_SERVER_REMOTE_CLOSED;
222 case ECONNRESET:
223 gdb_con->closed = 1;
224 return ERROR_SERVER_REMOTE_CLOSED;
225 default:
226 LOG_ERROR("read: %s", strerror(errno));
227 gdb_con->closed = 1;
228 return ERROR_SERVER_REMOTE_CLOSED;
230 #endif
233 #ifdef _DEBUG_GDB_IO_
234 debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
235 LOG_DEBUG("received '%s'", debug_buffer);
236 free(debug_buffer);
237 #endif
239 gdb_con->buf_p = gdb_con->buffer;
240 gdb_con->buf_cnt--;
241 *next_char = *(gdb_con->buf_p++);
242 if (gdb_con->buf_cnt > 0)
243 connection->input_pending = 1;
244 else
245 connection->input_pending = 0;
246 #ifdef _DEBUG_GDB_IO_
247 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
248 #endif
250 return retval;
254 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
255 * held in registers in the inner loop.
257 * For small caches and embedded systems this is important!
259 static inline int gdb_get_char_fast(struct connection *connection,
260 int *next_char, char **buf_p, int *buf_cnt)
262 int retval = ERROR_OK;
264 if ((*buf_cnt)-- > 0) {
265 *next_char = **buf_p;
266 (*buf_p)++;
267 if (*buf_cnt > 0)
268 connection->input_pending = 1;
269 else
270 connection->input_pending = 0;
272 #ifdef _DEBUG_GDB_IO_
273 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
274 #endif
276 return ERROR_OK;
279 struct gdb_connection *gdb_con = connection->priv;
280 gdb_con->buf_p = *buf_p;
281 gdb_con->buf_cnt = *buf_cnt;
282 retval = gdb_get_char_inner(connection, next_char);
283 *buf_p = gdb_con->buf_p;
284 *buf_cnt = gdb_con->buf_cnt;
286 return retval;
289 static int gdb_get_char(struct connection *connection, int *next_char)
291 struct gdb_connection *gdb_con = connection->priv;
292 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
295 static int gdb_putback_char(struct connection *connection, int last_char)
297 struct gdb_connection *gdb_con = connection->priv;
299 if (gdb_con->buf_p > gdb_con->buffer) {
300 *(--gdb_con->buf_p) = last_char;
301 gdb_con->buf_cnt++;
302 } else
303 LOG_ERROR("BUG: couldn't put character back");
305 return ERROR_OK;
308 /* The only way we can detect that the socket is closed is the first time
309 * we write to it, we will fail. Subsequent write operations will
310 * succeed. Shudder! */
311 static int gdb_write(struct connection *connection, void *data, int len)
313 struct gdb_connection *gdb_con = connection->priv;
314 if (gdb_con->closed)
315 return ERROR_SERVER_REMOTE_CLOSED;
317 if (connection_write(connection, data, len) == len)
318 return ERROR_OK;
319 gdb_con->closed = 1;
320 return ERROR_SERVER_REMOTE_CLOSED;
323 static int gdb_put_packet_inner(struct connection *connection,
324 char *buffer, int len)
326 int i;
327 unsigned char my_checksum = 0;
328 #ifdef _DEBUG_GDB_IO_
329 char *debug_buffer;
330 #endif
331 int reply;
332 int retval;
333 struct gdb_connection *gdb_con = connection->priv;
335 for (i = 0; i < len; i++)
336 my_checksum += buffer[i];
338 #ifdef _DEBUG_GDB_IO_
340 * At this point we should have nothing in the input queue from GDB,
341 * however sometimes '-' is sent even though we've already received
342 * an ACK (+) for everything we've sent off.
344 int gotdata;
345 for (;; ) {
346 retval = check_pending(connection, 0, &gotdata);
347 if (retval != ERROR_OK)
348 return retval;
349 if (!gotdata)
350 break;
351 retval = gdb_get_char(connection, &reply);
352 if (retval != ERROR_OK)
353 return retval;
354 if (reply == '$') {
355 /* fix a problem with some IAR tools */
356 gdb_putback_char(connection, reply);
357 LOG_DEBUG("Unexpected start of new packet");
358 break;
361 LOG_WARNING("Discard unexpected char %c", reply);
363 #endif
365 while (1) {
366 #ifdef _DEBUG_GDB_IO_
367 debug_buffer = strndup(buffer, len);
368 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
369 free(debug_buffer);
370 #endif
372 char local_buffer[1024];
373 local_buffer[0] = '$';
374 if ((size_t)len + 4 <= sizeof(local_buffer)) {
375 /* performance gain on smaller packets by only a single call to gdb_write()
377 memcpy(local_buffer + 1, buffer, len++);
378 local_buffer[len++] = '#';
379 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
380 local_buffer[len++] = DIGITS[my_checksum & 0xf];
381 retval = gdb_write(connection, local_buffer, len);
382 if (retval != ERROR_OK)
383 return retval;
384 } else {
385 /* larger packets are transmitted directly from caller supplied buffer
386 by several calls to gdb_write() to avoid dynamic allocation */
387 local_buffer[1] = '#';
388 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
389 local_buffer[3] = DIGITS[my_checksum & 0xf];
390 retval = gdb_write(connection, local_buffer, 1);
391 if (retval != ERROR_OK)
392 return retval;
393 retval = gdb_write(connection, buffer, len);
394 if (retval != ERROR_OK)
395 return retval;
396 retval = gdb_write(connection, local_buffer + 1, 3);
397 if (retval != ERROR_OK)
398 return retval;
401 if (gdb_con->noack_mode)
402 break;
404 retval = gdb_get_char(connection, &reply);
405 if (retval != ERROR_OK)
406 return retval;
408 if (reply == '+')
409 break;
410 else if (reply == '-') {
411 /* Stop sending output packets for now */
412 log_remove_callback(gdb_log_callback, connection);
413 LOG_WARNING("negative reply, retrying");
414 } else if (reply == 0x3) {
415 gdb_con->ctrl_c = 1;
416 retval = gdb_get_char(connection, &reply);
417 if (retval != ERROR_OK)
418 return retval;
419 if (reply == '+')
420 break;
421 else if (reply == '-') {
422 /* Stop sending output packets for now */
423 log_remove_callback(gdb_log_callback, connection);
424 LOG_WARNING("negative reply, retrying");
425 } else if (reply == '$') {
426 LOG_ERROR("GDB missing ack(1) - assumed good");
427 gdb_putback_char(connection, reply);
428 return ERROR_OK;
429 } else {
430 LOG_ERROR(
431 "unknown character(1) 0x%2.2x in reply, dropping connection", reply);
432 gdb_con->closed = 1;
433 return ERROR_SERVER_REMOTE_CLOSED;
435 } else if (reply == '$') {
436 LOG_ERROR("GDB missing ack(2) - assumed good");
437 gdb_putback_char(connection, reply);
438 return ERROR_OK;
439 } else {
440 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
441 reply);
442 gdb_con->closed = 1;
443 return ERROR_SERVER_REMOTE_CLOSED;
446 if (gdb_con->closed)
447 return ERROR_SERVER_REMOTE_CLOSED;
449 return ERROR_OK;
452 int gdb_put_packet(struct connection *connection, char *buffer, int len)
454 struct gdb_connection *gdb_con = connection->priv;
455 gdb_con->busy = 1;
456 int retval = gdb_put_packet_inner(connection, buffer, len);
457 gdb_con->busy = 0;
459 /* we sent some data, reset timer for keep alive messages */
460 kept_alive();
462 return retval;
465 static inline int fetch_packet(struct connection *connection,
466 int *checksum_ok, int noack, int *len, char *buffer)
468 unsigned char my_checksum = 0;
469 char checksum[3];
470 int character;
471 int retval = ERROR_OK;
473 struct gdb_connection *gdb_con = connection->priv;
474 my_checksum = 0;
475 int count = 0;
476 count = 0;
478 /* move this over into local variables to use registers and give the
479 * more freedom to optimize */
480 char *buf_p = gdb_con->buf_p;
481 int buf_cnt = gdb_con->buf_cnt;
483 for (;; ) {
484 /* The common case is that we have an entire packet with no escape chars.
485 * We need to leave at least 2 bytes in the buffer to have
486 * gdb_get_char() update various bits and bobs correctly.
488 if ((buf_cnt > 2) && ((buf_cnt + count) < *len)) {
489 /* The compiler will struggle a bit with constant propagation and
490 * aliasing, so we help it by showing that these values do not
491 * change inside the loop
493 int i;
494 char *buf = buf_p;
495 int run = buf_cnt - 2;
496 i = 0;
497 int done = 0;
498 while (i < run) {
499 character = *buf++;
500 i++;
501 if (character == '#') {
502 /* Danger! character can be '#' when esc is
503 * used so we need an explicit boolean for done here.
505 done = 1;
506 break;
509 if (character == '}') {
510 /* data transmitted in binary mode (X packet)
511 * uses 0x7d as escape character */
512 my_checksum += character & 0xff;
513 character = *buf++;
514 i++;
515 my_checksum += character & 0xff;
516 buffer[count++] = (character ^ 0x20) & 0xff;
517 } else {
518 my_checksum += character & 0xff;
519 buffer[count++] = character & 0xff;
522 buf_p += i;
523 buf_cnt -= i;
524 if (done)
525 break;
527 if (count > *len) {
528 LOG_ERROR("packet buffer too small");
529 retval = ERROR_GDB_BUFFER_TOO_SMALL;
530 break;
533 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
534 if (retval != ERROR_OK)
535 break;
537 if (character == '#')
538 break;
540 if (character == '}') {
541 /* data transmitted in binary mode (X packet)
542 * uses 0x7d as escape character */
543 my_checksum += character & 0xff;
545 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
546 if (retval != ERROR_OK)
547 break;
549 my_checksum += character & 0xff;
550 buffer[count++] = (character ^ 0x20) & 0xff;
551 } else {
552 my_checksum += character & 0xff;
553 buffer[count++] = character & 0xff;
557 gdb_con->buf_p = buf_p;
558 gdb_con->buf_cnt = buf_cnt;
560 if (retval != ERROR_OK)
561 return retval;
563 *len = count;
565 retval = gdb_get_char(connection, &character);
566 if (retval != ERROR_OK)
567 return retval;
568 checksum[0] = character;
569 retval = gdb_get_char(connection, &character);
570 if (retval != ERROR_OK)
571 return retval;
572 checksum[1] = character;
573 checksum[2] = 0;
575 if (!noack)
576 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
578 return ERROR_OK;
581 static int gdb_get_packet_inner(struct connection *connection,
582 char *buffer, int *len)
584 int character;
585 int retval;
586 struct gdb_connection *gdb_con = connection->priv;
588 while (1) {
589 do {
590 retval = gdb_get_char(connection, &character);
591 if (retval != ERROR_OK)
592 return retval;
594 #ifdef _DEBUG_GDB_IO_
595 LOG_DEBUG("character: '%c'", character);
596 #endif
598 switch (character) {
599 case '$':
600 break;
601 case '+':
602 /* gdb sends a dummy ack '+' at every remote connect - see
603 * remote_start_remote (remote.c)
604 * in case anyone tries to debug why they receive this
605 * warning every time */
606 LOG_WARNING("acknowledgment received, but no packet pending");
607 break;
608 case '-':
609 LOG_WARNING("negative acknowledgment, but no packet pending");
610 break;
611 case 0x3:
612 gdb_con->ctrl_c = 1;
613 *len = 0;
614 return ERROR_OK;
615 default:
616 LOG_WARNING("ignoring character 0x%x", character);
617 break;
619 } while (character != '$');
621 int checksum_ok = 0;
622 /* explicit code expansion here to get faster inlined code in -O3 by not
623 * calculating checksum
625 if (gdb_con->noack_mode) {
626 retval = fetch_packet(connection, &checksum_ok, 1, len, buffer);
627 if (retval != ERROR_OK)
628 return retval;
629 } else {
630 retval = fetch_packet(connection, &checksum_ok, 0, len, buffer);
631 if (retval != ERROR_OK)
632 return retval;
635 if (gdb_con->noack_mode) {
636 /* checksum is not checked in noack mode */
637 break;
639 if (checksum_ok) {
640 retval = gdb_write(connection, "+", 1);
641 if (retval != ERROR_OK)
642 return retval;
643 break;
646 if (gdb_con->closed)
647 return ERROR_SERVER_REMOTE_CLOSED;
649 return ERROR_OK;
652 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
654 struct gdb_connection *gdb_con = connection->priv;
655 gdb_con->busy = 1;
656 int retval = gdb_get_packet_inner(connection, buffer, len);
657 gdb_con->busy = 0;
658 return retval;
661 static int gdb_output_con(struct connection *connection, const char *line)
663 char *hex_buffer;
664 int i, bin_size;
666 bin_size = strlen(line);
668 hex_buffer = malloc(bin_size*2 + 2);
669 if (hex_buffer == NULL)
670 return ERROR_GDB_BUFFER_TOO_SMALL;
672 hex_buffer[0] = 'O';
673 for (i = 0; i < bin_size; i++)
674 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
675 hex_buffer[bin_size*2 + 1] = 0;
677 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
679 free(hex_buffer);
680 return retval;
683 static int gdb_output(struct command_context *context, const char *line)
685 /* this will be dumped to the log and also sent as an O packet if possible */
686 LOG_USER_N("%s", line);
687 return ERROR_OK;
690 static void gdb_frontend_halted(struct target *target, struct connection *connection)
692 struct gdb_connection *gdb_connection = connection->priv;
694 /* In the GDB protocol when we are stepping or continuing execution,
695 * we have a lingering reply. Upon receiving a halted event
696 * when we have that lingering packet, we reply to the original
697 * step or continue packet.
699 * Executing monitor commands can bring the target in and
700 * out of the running state so we'll see lots of TARGET_EVENT_XXX
701 * that are to be ignored.
703 if (gdb_connection->frontend_state == TARGET_RUNNING) {
704 char sig_reply[4];
705 int signal_var;
707 /* stop forwarding log packets! */
708 log_remove_callback(gdb_log_callback, connection);
710 if (gdb_connection->ctrl_c) {
711 signal_var = 0x2;
712 gdb_connection->ctrl_c = 0;
713 } else
714 signal_var = gdb_last_signal(target);
716 snprintf(sig_reply, 4, "T%2.2x", signal_var);
717 gdb_put_packet(connection, sig_reply, 3);
718 gdb_connection->frontend_state = TARGET_HALTED;
719 rtos_update_threads(target);
723 static int gdb_target_callback_event_handler(struct target *target,
724 enum target_event event, void *priv)
726 int retval;
727 struct connection *connection = priv;
729 target_handle_event(target, event);
730 switch (event) {
731 case TARGET_EVENT_GDB_HALT:
732 gdb_frontend_halted(target, connection);
733 break;
734 case TARGET_EVENT_HALTED:
735 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
736 break;
737 case TARGET_EVENT_GDB_FLASH_ERASE_START:
738 target_handle_event(target, TARGET_EVENT_OLD_gdb_program_config);
739 retval = jtag_execute_queue();
740 if (retval != ERROR_OK)
741 return retval;
742 break;
743 default:
744 break;
747 return ERROR_OK;
750 static int gdb_new_connection(struct connection *connection)
752 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
753 struct gdb_service *gdb_service = connection->service->priv;
754 int retval;
755 int initial_ack;
757 connection->priv = gdb_connection;
759 /* initialize gdb connection information */
760 gdb_connection->buf_p = gdb_connection->buffer;
761 gdb_connection->buf_cnt = 0;
762 gdb_connection->ctrl_c = 0;
763 gdb_connection->frontend_state = TARGET_HALTED;
764 gdb_connection->vflash_image = NULL;
765 gdb_connection->closed = 0;
766 gdb_connection->busy = 0;
767 gdb_connection->noack_mode = 0;
768 gdb_connection->sync = true;
769 gdb_connection->mem_write_error = false;
771 /* send ACK to GDB for debug request */
772 gdb_write(connection, "+", 1);
774 /* output goes through gdb connection */
775 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
777 /* we must remove all breakpoints registered to the target as a previous
778 * GDB session could leave dangling breakpoints if e.g. communication
779 * timed out.
781 breakpoint_clear_target(gdb_service->target);
782 watchpoint_clear_target(gdb_service->target);
784 /* clean previous rtos session if supported*/
785 if ((gdb_service->target->rtos) && (gdb_service->target->rtos->type->clean))
786 gdb_service->target->rtos->type->clean(gdb_service->target);
788 /* remove the initial ACK from the incoming buffer */
789 retval = gdb_get_char(connection, &initial_ack);
790 if (retval != ERROR_OK)
791 return retval;
793 /* FIX!!!??? would we actually ever receive a + here???
794 * Not observed.
796 if (initial_ack != '+')
797 gdb_putback_char(connection, initial_ack);
798 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
800 if (gdb_use_memory_map) {
801 /* Connect must fail if the memory map can't be set up correctly.
803 * This will cause an auto_probe to be invoked, which is either
804 * a no-op or it will fail when the target isn't ready(e.g. not halted).
806 int i;
807 for (i = 0; i < flash_get_bank_count(); i++) {
808 struct flash_bank *p;
809 retval = get_flash_bank_by_num(i, &p);
810 if (retval != ERROR_OK) {
811 LOG_ERROR(
812 "Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
813 return retval;
818 gdb_actual_connections++;
819 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
820 gdb_actual_connections,
821 target_name(gdb_service->target),
822 target_state_name(gdb_service->target));
824 /* DANGER! If we fail subsequently, we must remove this handler,
825 * otherwise we occasionally see crashes as the timer can invoke the
826 * callback fn.
828 * register callback to be informed about target events */
829 target_register_event_callback(gdb_target_callback_event_handler, connection);
831 return ERROR_OK;
834 static int gdb_connection_closed(struct connection *connection)
836 struct gdb_service *gdb_service = connection->service->priv;
837 struct gdb_connection *gdb_connection = connection->priv;
839 /* we're done forwarding messages. Tear down callback before
840 * cleaning up connection.
842 log_remove_callback(gdb_log_callback, connection);
844 gdb_actual_connections--;
845 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
846 target_name(gdb_service->target),
847 target_state_name(gdb_service->target),
848 gdb_actual_connections);
850 /* see if an image built with vFlash commands is left */
851 if (gdb_connection->vflash_image) {
852 image_close(gdb_connection->vflash_image);
853 free(gdb_connection->vflash_image);
854 gdb_connection->vflash_image = NULL;
857 /* if this connection registered a debug-message receiver delete it */
858 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
860 if (connection->priv) {
861 free(connection->priv);
862 connection->priv = NULL;
863 } else
864 LOG_ERROR("BUG: connection->priv == NULL");
866 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
868 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
870 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
872 return ERROR_OK;
875 static void gdb_send_error(struct connection *connection, uint8_t the_error)
877 char err[4];
878 snprintf(err, 4, "E%2.2X", the_error);
879 gdb_put_packet(connection, err, 3);
882 static int gdb_last_signal_packet(struct connection *connection,
883 char *packet, int packet_size)
885 struct target *target = get_target_from_connection(connection);
886 char sig_reply[4];
887 int signal_var;
889 signal_var = gdb_last_signal(target);
891 snprintf(sig_reply, 4, "S%2.2x", signal_var);
892 gdb_put_packet(connection, sig_reply, 3);
894 return ERROR_OK;
897 static int gdb_reg_pos(struct target *target, int pos, int len)
899 if (target->endianness == TARGET_LITTLE_ENDIAN)
900 return pos;
901 else
902 return len - 1 - pos;
905 /* Convert register to string of bytes. NB! The # of bits in the
906 * register might be non-divisible by 8(a byte), in which
907 * case an entire byte is shown.
909 * NB! the format on the wire is the target endianness
911 * The format of reg->value is little endian
914 static void gdb_str_to_target(struct target *target,
915 char *tstr, struct reg *reg)
917 int i;
919 uint8_t *buf;
920 int buf_len;
921 buf = reg->value;
922 buf_len = DIV_ROUND_UP(reg->size, 8);
924 for (i = 0; i < buf_len; i++) {
925 int j = gdb_reg_pos(target, i, buf_len);
926 tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
927 tstr[i*2 + 1] = DIGITS[buf[j]&0xf];
931 static int hextoint(int c)
933 if (c >= '0' && c <= '9')
934 return c - '0';
935 c = toupper(c);
936 if (c >= 'A' && c <= 'F')
937 return c - 'A' + 10;
938 LOG_ERROR("BUG: invalid register value %08x", c);
939 return 0;
942 /* copy over in register buffer */
943 static void gdb_target_to_reg(struct target *target,
944 char *tstr, int str_len, uint8_t *bin)
946 if (str_len % 2) {
947 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
948 exit(-1);
951 int i;
952 for (i = 0; i < str_len; i += 2) {
953 uint8_t t = hextoint(tstr[i]) << 4;
954 t |= hextoint(tstr[i + 1]);
956 int j = gdb_reg_pos(target, i/2, str_len/2);
957 bin[j] = t;
961 static int gdb_get_registers_packet(struct connection *connection,
962 char *packet, int packet_size)
964 struct target *target = get_target_from_connection(connection);
965 struct reg **reg_list;
966 int reg_list_size;
967 int retval;
968 int reg_packet_size = 0;
969 char *reg_packet;
970 char *reg_packet_p;
971 int i;
973 #ifdef _DEBUG_GDB_IO_
974 LOG_DEBUG("-");
975 #endif
977 if ((target->rtos != NULL) && (ERROR_OK == rtos_get_gdb_reg_list(connection)))
978 return ERROR_OK;
980 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size);
981 if (retval != ERROR_OK)
982 return gdb_error(connection, retval);
984 for (i = 0; i < reg_list_size; i++)
985 reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
987 assert(reg_packet_size > 0);
989 reg_packet = malloc(reg_packet_size);
990 reg_packet_p = reg_packet;
992 for (i = 0; i < reg_list_size; i++) {
993 if (!reg_list[i]->valid)
994 reg_list[i]->type->get(reg_list[i]);
995 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
996 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
999 #ifdef _DEBUG_GDB_IO_
1001 char *reg_packet_p_debug;
1002 reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1003 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1004 free(reg_packet_p_debug);
1006 #endif
1008 gdb_put_packet(connection, reg_packet, reg_packet_size);
1009 free(reg_packet);
1011 free(reg_list);
1013 return ERROR_OK;
1016 static int gdb_set_registers_packet(struct connection *connection,
1017 char *packet, int packet_size)
1019 struct target *target = get_target_from_connection(connection);
1020 int i;
1021 struct reg **reg_list;
1022 int reg_list_size;
1023 int retval;
1024 char *packet_p;
1026 #ifdef _DEBUG_GDB_IO_
1027 LOG_DEBUG("-");
1028 #endif
1030 /* skip command character */
1031 packet++;
1032 packet_size--;
1034 if (packet_size % 2) {
1035 LOG_WARNING(
1036 "GDB set_registers packet with uneven characters received, dropping connection");
1037 return ERROR_SERVER_REMOTE_CLOSED;
1040 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size);
1041 if (retval != ERROR_OK)
1042 return gdb_error(connection, retval);
1044 packet_p = packet;
1045 for (i = 0; i < reg_list_size; i++) {
1046 uint8_t *bin_buf;
1047 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1049 if (packet_p + chars > packet + packet_size)
1050 LOG_ERROR("BUG: register packet is too small for registers");
1052 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1053 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1055 reg_list[i]->type->set(reg_list[i], bin_buf);
1057 /* advance packet pointer */
1058 packet_p += chars;
1060 free(bin_buf);
1063 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1064 free(reg_list);
1066 gdb_put_packet(connection, "OK", 2);
1068 return ERROR_OK;
1071 static int gdb_get_register_packet(struct connection *connection,
1072 char *packet, int packet_size)
1074 struct target *target = get_target_from_connection(connection);
1075 char *reg_packet;
1076 int reg_num = strtoul(packet + 1, NULL, 16);
1077 struct reg **reg_list;
1078 int reg_list_size;
1079 int retval;
1081 #ifdef _DEBUG_GDB_IO_
1082 LOG_DEBUG("-");
1083 #endif
1085 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size);
1086 if (retval != ERROR_OK)
1087 return gdb_error(connection, retval);
1089 if (reg_list_size <= reg_num) {
1090 LOG_ERROR("gdb requested a non-existing register");
1091 exit(-1);
1094 if (!reg_list[reg_num]->valid)
1095 reg_list[reg_num]->type->get(reg_list[reg_num]);
1097 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1099 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1101 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1103 free(reg_list);
1104 free(reg_packet);
1106 return ERROR_OK;
1109 static int gdb_set_register_packet(struct connection *connection,
1110 char *packet, int packet_size)
1112 struct target *target = get_target_from_connection(connection);
1113 char *separator;
1114 uint8_t *bin_buf;
1115 int reg_num = strtoul(packet + 1, &separator, 16);
1116 struct reg **reg_list;
1117 int reg_list_size;
1118 int retval;
1120 LOG_DEBUG("-");
1122 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size);
1123 if (retval != ERROR_OK)
1124 return gdb_error(connection, retval);
1126 if (reg_list_size < reg_num) {
1127 LOG_ERROR("gdb requested a non-existing register");
1128 return ERROR_SERVER_REMOTE_CLOSED;
1131 if (*separator != '=') {
1132 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1133 return ERROR_SERVER_REMOTE_CLOSED;
1136 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1137 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1138 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1140 /* fix!!! add some sanity checks on packet size here */
1142 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1144 reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1146 gdb_put_packet(connection, "OK", 2);
1148 free(bin_buf);
1149 free(reg_list);
1151 return ERROR_OK;
1154 /* No attempt is made to translate the "retval" to
1155 * GDB speak. This has to be done at the calling
1156 * site as no mapping really exists.
1158 static int gdb_error(struct connection *connection, int retval)
1160 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1161 gdb_send_error(connection, EFAULT);
1162 return ERROR_OK;
1165 /* We don't have to worry about the default 2 second timeout for GDB packets,
1166 * because GDB breaks up large memory reads into smaller reads.
1168 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1170 static int gdb_read_memory_packet(struct connection *connection,
1171 char *packet, int packet_size)
1173 struct target *target = get_target_from_connection(connection);
1174 char *separator;
1175 uint32_t addr = 0;
1176 uint32_t len = 0;
1178 uint8_t *buffer;
1179 char *hex_buffer;
1181 int retval = ERROR_OK;
1183 /* skip command character */
1184 packet++;
1186 addr = strtoul(packet, &separator, 16);
1188 if (*separator != ',') {
1189 LOG_ERROR("incomplete read memory packet received, dropping connection");
1190 return ERROR_SERVER_REMOTE_CLOSED;
1193 len = strtoul(separator + 1, NULL, 16);
1195 buffer = malloc(len);
1197 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1199 retval = target_read_buffer(target, addr, len, buffer);
1201 if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1202 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1203 * At some point this might be fixed in GDB, in which case this code can be removed.
1205 * OpenOCD developers are acutely aware of this problem, but there is nothing
1206 * gained by involving the user in this problem that hopefully will get resolved
1207 * eventually
1209 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1210 * cmd = view%20audit-trail&database = gdb&pr = 2395
1212 * For now, the default is to fix up things to make current GDB versions work.
1213 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1215 memset(buffer, 0, len);
1216 retval = ERROR_OK;
1219 if (retval == ERROR_OK) {
1220 hex_buffer = malloc(len * 2 + 1);
1222 uint32_t i;
1223 for (i = 0; i < len; i++) {
1224 uint8_t t = buffer[i];
1225 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1226 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1229 gdb_put_packet(connection, hex_buffer, len * 2);
1231 free(hex_buffer);
1232 } else
1233 retval = gdb_error(connection, retval);
1235 free(buffer);
1237 return retval;
1240 static int gdb_write_memory_packet(struct connection *connection,
1241 char *packet, int packet_size)
1243 struct target *target = get_target_from_connection(connection);
1244 char *separator;
1245 uint32_t addr = 0;
1246 uint32_t len = 0;
1248 uint8_t *buffer;
1250 uint32_t i;
1251 int retval;
1253 /* skip command character */
1254 packet++;
1256 addr = strtoul(packet, &separator, 16);
1258 if (*separator != ',') {
1259 LOG_ERROR("incomplete write memory packet received, dropping connection");
1260 return ERROR_SERVER_REMOTE_CLOSED;
1263 len = strtoul(separator + 1, &separator, 16);
1265 if (*(separator++) != ':') {
1266 LOG_ERROR("incomplete write memory packet received, dropping connection");
1267 return ERROR_SERVER_REMOTE_CLOSED;
1270 buffer = malloc(len);
1272 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1274 for (i = 0; i < len; i++) {
1275 uint32_t tmp;
1276 sscanf(separator + 2*i, "%2" SCNx32, &tmp);
1277 buffer[i] = tmp;
1280 retval = target_write_buffer(target, addr, len, buffer);
1282 if (retval == ERROR_OK)
1283 gdb_put_packet(connection, "OK", 2);
1284 else
1285 retval = gdb_error(connection, retval);
1287 free(buffer);
1289 return retval;
1292 static int gdb_write_memory_binary_packet(struct connection *connection,
1293 char *packet, int packet_size)
1295 struct target *target = get_target_from_connection(connection);
1296 char *separator;
1297 uint32_t addr = 0;
1298 uint32_t len = 0;
1300 int retval = ERROR_OK;
1302 /* skip command character */
1303 packet++;
1305 addr = strtoul(packet, &separator, 16);
1307 if (*separator != ',') {
1308 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1309 return ERROR_SERVER_REMOTE_CLOSED;
1312 len = strtoul(separator + 1, &separator, 16);
1314 if (*(separator++) != ':') {
1315 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1316 return ERROR_SERVER_REMOTE_CLOSED;
1319 struct gdb_connection *gdb_connection = connection->priv;
1321 if (gdb_connection->mem_write_error) {
1322 retval = ERROR_FAIL;
1323 /* now that we have reported the memory write error, we can clear the condition */
1324 gdb_connection->mem_write_error = false;
1327 /* By replying the packet *immediately* GDB will send us a new packet
1328 * while we write the last one to the target.
1330 if (retval == ERROR_OK)
1331 gdb_put_packet(connection, "OK", 2);
1332 else {
1333 retval = gdb_error(connection, retval);
1334 if (retval != ERROR_OK)
1335 return retval;
1338 if (len) {
1339 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1341 retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1342 if (retval != ERROR_OK)
1343 gdb_connection->mem_write_error = true;
1346 return ERROR_OK;
1349 static int gdb_step_continue_packet(struct connection *connection,
1350 char *packet, int packet_size)
1352 struct target *target = get_target_from_connection(connection);
1353 int current = 0;
1354 uint32_t address = 0x0;
1355 int retval = ERROR_OK;
1357 LOG_DEBUG("-");
1359 if (packet_size > 1) {
1360 packet[packet_size] = 0;
1361 address = strtoul(packet + 1, NULL, 16);
1362 } else
1363 current = 1;
1365 if (packet[0] == 'c') {
1366 LOG_DEBUG("continue");
1367 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
1368 retval = target_resume(target, current, address, 0, 0); /* resume at current
1369 *address, don't handle
1370 *breakpoints, not debugging
1372 } else if (packet[0] == 's') {
1373 LOG_DEBUG("step");
1374 /* step at current or address, don't handle breakpoints */
1375 retval = target_step(target, current, address, 0);
1377 return retval;
1380 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1381 char *packet, int packet_size)
1383 struct target *target = get_target_from_connection(connection);
1384 int type;
1385 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1386 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1387 uint32_t address;
1388 uint32_t size;
1389 char *separator;
1390 int retval;
1392 LOG_DEBUG("-");
1394 type = strtoul(packet + 1, &separator, 16);
1396 if (type == 0) /* memory breakpoint */
1397 bp_type = BKPT_SOFT;
1398 else if (type == 1) /* hardware breakpoint */
1399 bp_type = BKPT_HARD;
1400 else if (type == 2) /* write watchpoint */
1401 wp_type = WPT_WRITE;
1402 else if (type == 3) /* read watchpoint */
1403 wp_type = WPT_READ;
1404 else if (type == 4) /* access watchpoint */
1405 wp_type = WPT_ACCESS;
1406 else {
1407 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1408 return ERROR_SERVER_REMOTE_CLOSED;
1411 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1412 bp_type = gdb_breakpoint_override_type;
1414 if (*separator != ',') {
1415 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1416 return ERROR_SERVER_REMOTE_CLOSED;
1419 address = strtoul(separator + 1, &separator, 16);
1421 if (*separator != ',') {
1422 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1423 return ERROR_SERVER_REMOTE_CLOSED;
1426 size = strtoul(separator + 1, &separator, 16);
1428 switch (type) {
1429 case 0:
1430 case 1:
1431 if (packet[0] == 'Z') {
1432 retval = breakpoint_add(target, address, size, bp_type);
1433 if (retval != ERROR_OK) {
1434 retval = gdb_error(connection, retval);
1435 if (retval != ERROR_OK)
1436 return retval;
1437 } else
1438 gdb_put_packet(connection, "OK", 2);
1439 } else {
1440 breakpoint_remove(target, address);
1441 gdb_put_packet(connection, "OK", 2);
1443 break;
1444 case 2:
1445 case 3:
1446 case 4:
1448 if (packet[0] == 'Z') {
1449 retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu);
1450 if (retval != ERROR_OK) {
1451 retval = gdb_error(connection, retval);
1452 if (retval != ERROR_OK)
1453 return retval;
1454 } else
1455 gdb_put_packet(connection, "OK", 2);
1456 } else {
1457 watchpoint_remove(target, address);
1458 gdb_put_packet(connection, "OK", 2);
1460 break;
1462 default:
1463 break;
1466 return ERROR_OK;
1469 /* print out a string and allocate more space as needed,
1470 * mainly used for XML at this point
1472 static void xml_printf(int *retval, char **xml, int *pos, int *size,
1473 const char *fmt, ...)
1475 if (*retval != ERROR_OK)
1476 return;
1477 int first = 1;
1479 for (;; ) {
1480 if ((*xml == NULL) || (!first)) {
1481 /* start by 0 to exercise all the code paths.
1482 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1484 *size = *size * 2 + 2;
1485 char *t = *xml;
1486 *xml = realloc(*xml, *size);
1487 if (*xml == NULL) {
1488 if (t)
1489 free(t);
1490 *retval = ERROR_SERVER_REMOTE_CLOSED;
1491 return;
1495 va_list ap;
1496 int ret;
1497 va_start(ap, fmt);
1498 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1499 va_end(ap);
1500 if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1501 *pos += ret;
1502 return;
1504 /* there was just enough or not enough space, allocate more. */
1505 first = 0;
1509 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1511 char *separator;
1513 /* Extract and NUL-terminate the annex. */
1514 *annex = buf;
1515 while (*buf && *buf != ':')
1516 buf++;
1517 if (*buf == '\0')
1518 return -1;
1519 *buf++ = 0;
1521 /* After the read marker and annex, qXfer looks like a
1522 * traditional 'm' packet. */
1524 *ofs = strtoul(buf, &separator, 16);
1526 if (*separator != ',')
1527 return -1;
1529 *len = strtoul(separator + 1, NULL, 16);
1531 return 0;
1534 static int compare_bank(const void *a, const void *b)
1536 struct flash_bank *b1, *b2;
1537 b1 = *((struct flash_bank **)a);
1538 b2 = *((struct flash_bank **)b);
1540 if (b1->base == b2->base)
1541 return 0;
1542 else if (b1->base > b2->base)
1543 return 1;
1544 else
1545 return -1;
1548 static int gdb_memory_map(struct connection *connection,
1549 char *packet, int packet_size)
1551 /* We get away with only specifying flash here. Regions that are not
1552 * specified are treated as if we provided no memory map(if not we
1553 * could detect the holes and mark them as RAM).
1554 * Normally we only execute this code once, but no big deal if we
1555 * have to regenerate it a couple of times.
1558 struct target *target = get_target_from_connection(connection);
1559 struct flash_bank *p;
1560 char *xml = NULL;
1561 int size = 0;
1562 int pos = 0;
1563 int retval = ERROR_OK;
1564 struct flash_bank **banks;
1565 int offset;
1566 int length;
1567 char *separator;
1568 uint32_t ram_start = 0;
1569 int i;
1570 int target_flash_banks = 0;
1572 /* skip command character */
1573 packet += 23;
1575 offset = strtoul(packet, &separator, 16);
1576 length = strtoul(separator + 1, &separator, 16);
1578 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1580 /* Sort banks in ascending order. We need to report non-flash
1581 * memory as ram (or rather read/write) by default for GDB, since
1582 * it has no concept of non-cacheable read/write memory (i/o etc).
1584 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1585 * Current versions of GDB assume unlisted addresses are RAM...
1587 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1589 for (i = 0; i < flash_get_bank_count(); i++) {
1590 retval = get_flash_bank_by_num(i, &p);
1591 if (retval != ERROR_OK) {
1592 free(banks);
1593 gdb_error(connection, retval);
1594 return retval;
1596 if (p->target == target)
1597 banks[target_flash_banks++] = p;
1600 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1601 compare_bank);
1603 for (i = 0; i < target_flash_banks; i++) {
1604 int j;
1605 unsigned sector_size = 0;
1606 uint32_t start;
1608 p = banks[i];
1609 start = p->base;
1611 if (ram_start < p->base)
1612 xml_printf(&retval, &xml, &pos, &size,
1613 "<memory type=\"ram\" start=\"0x%x\" "
1614 "length=\"0x%x\"/>\n",
1615 ram_start, p->base - ram_start);
1617 /* Report adjacent groups of same-size sectors. So for
1618 * example top boot CFI flash will list an initial region
1619 * with several large sectors (maybe 128KB) and several
1620 * smaller ones at the end (maybe 32KB). STR7 will have
1621 * regions with 8KB, 32KB, and 64KB sectors; etc.
1623 for (j = 0; j < p->num_sectors; j++) {
1624 unsigned group_len;
1626 /* Maybe start a new group of sectors. */
1627 if (sector_size == 0) {
1628 start = p->base + p->sectors[j].offset;
1629 xml_printf(&retval, &xml, &pos, &size,
1630 "<memory type=\"flash\" "
1631 "start=\"0x%x\" ",
1632 start);
1633 sector_size = p->sectors[j].size;
1636 /* Does this finish a group of sectors?
1637 * If not, continue an already-started group.
1639 if (j == p->num_sectors - 1)
1640 group_len = (p->base + p->size) - start;
1641 else if (p->sectors[j + 1].size != sector_size)
1642 group_len = p->base + p->sectors[j + 1].offset
1643 - start;
1644 else
1645 continue;
1647 xml_printf(&retval, &xml, &pos, &size,
1648 "length=\"0x%x\">\n"
1649 "<property name=\"blocksize\">"
1650 "0x%x</property>\n"
1651 "</memory>\n",
1652 group_len,
1653 sector_size);
1654 sector_size = 0;
1657 ram_start = p->base + p->size;
1660 if (ram_start != 0)
1661 xml_printf(&retval, &xml, &pos, &size,
1662 "<memory type=\"ram\" start=\"0x%x\" "
1663 "length=\"0x%x\"/>\n",
1664 ram_start, 0-ram_start);
1665 /* ELSE a flash chip could be at the very end of the 32 bit address
1666 * space, in which case ram_start will be precisely 0
1669 free(banks);
1670 banks = NULL;
1672 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1674 if (retval != ERROR_OK) {
1675 gdb_error(connection, retval);
1676 return retval;
1679 if (offset + length > pos)
1680 length = pos - offset;
1682 char *t = malloc(length + 1);
1683 t[0] = 'l';
1684 memcpy(t + 1, xml + offset, length);
1685 gdb_put_packet(connection, t, length + 1);
1687 free(t);
1688 free(xml);
1689 return ERROR_OK;
1692 static int gdb_query_packet(struct connection *connection,
1693 char *packet, int packet_size)
1695 struct command_context *cmd_ctx = connection->cmd_ctx;
1696 struct gdb_connection *gdb_connection = connection->priv;
1697 struct target *target = get_target_from_connection(connection);
1699 if (strstr(packet, "qRcmd,")) {
1700 if (packet_size > 6) {
1701 char *cmd;
1702 int i;
1703 cmd = malloc((packet_size - 6)/2 + 1);
1704 for (i = 0; i < (packet_size - 6)/2; i++) {
1705 uint32_t tmp;
1706 sscanf(packet + 6 + 2*i, "%2" SCNx32, &tmp);
1707 cmd[i] = tmp;
1709 cmd[(packet_size - 6)/2] = 0x0;
1711 /* We want to print all debug output to GDB connection */
1712 log_add_callback(gdb_log_callback, connection);
1713 target_call_timer_callbacks_now();
1714 /* some commands need to know the GDB connection, make note of current
1715 * GDB connection. */
1716 current_gdb_connection = gdb_connection;
1717 command_run_line(cmd_ctx, cmd);
1718 current_gdb_connection = NULL;
1719 target_call_timer_callbacks_now();
1720 log_remove_callback(gdb_log_callback, connection);
1721 free(cmd);
1723 gdb_put_packet(connection, "OK", 2);
1724 return ERROR_OK;
1725 } else if (strstr(packet, "qCRC:")) {
1726 if (packet_size > 5) {
1727 int retval;
1728 char gdb_reply[10];
1729 char *separator;
1730 uint32_t checksum;
1731 uint32_t addr = 0;
1732 uint32_t len = 0;
1734 /* skip command character */
1735 packet += 5;
1737 addr = strtoul(packet, &separator, 16);
1739 if (*separator != ',') {
1740 LOG_ERROR(
1741 "incomplete read memory packet received, dropping connection");
1742 return ERROR_SERVER_REMOTE_CLOSED;
1745 len = strtoul(separator + 1, NULL, 16);
1747 retval = target_checksum_memory(target, addr, len, &checksum);
1749 if (retval == ERROR_OK) {
1750 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
1751 gdb_put_packet(connection, gdb_reply, 9);
1752 } else {
1753 retval = gdb_error(connection, retval);
1754 if (retval != ERROR_OK)
1755 return retval;
1758 return ERROR_OK;
1760 } else if (strstr(packet, "qSupported")) {
1761 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1762 * disable qXfer:features:read for the moment */
1763 int retval = ERROR_OK;
1764 char *buffer = NULL;
1765 int pos = 0;
1766 int size = 0;
1768 xml_printf(&retval,
1769 &buffer,
1770 &pos,
1771 &size,
1772 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1773 (GDB_BUFFER_SIZE - 1),
1774 ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1776 if (retval != ERROR_OK) {
1777 gdb_send_error(connection, 01);
1778 return ERROR_OK;
1781 gdb_put_packet(connection, buffer, strlen(buffer));
1782 free(buffer);
1784 return ERROR_OK;
1785 } else if (strstr(packet, "qXfer:memory-map:read::")
1786 && (flash_get_bank_count() > 0))
1787 return gdb_memory_map(connection, packet, packet_size);
1788 else if (strstr(packet, "qXfer:features:read:")) {
1789 char *xml = NULL;
1790 int size = 0;
1791 int pos = 0;
1792 int retval = ERROR_OK;
1794 int offset;
1795 unsigned int length;
1796 char *annex;
1798 /* skip command character */
1799 packet += 20;
1801 if (decode_xfer_read(packet, &annex, &offset, &length) < 0) {
1802 gdb_send_error(connection, 01);
1803 return ERROR_OK;
1806 if (strcmp(annex, "target.xml") != 0) {
1807 gdb_send_error(connection, 01);
1808 return ERROR_OK;
1811 xml_printf(&retval,
1812 &xml,
1813 &pos,
1814 &size, \
1815 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1817 if (retval != ERROR_OK) {
1818 gdb_error(connection, retval);
1819 return retval;
1822 gdb_put_packet(connection, xml, strlen(xml));
1824 free(xml);
1825 return ERROR_OK;
1826 } else if (strstr(packet, "QStartNoAckMode")) {
1827 gdb_connection->noack_mode = 1;
1828 gdb_put_packet(connection, "OK", 2);
1829 return ERROR_OK;
1832 gdb_put_packet(connection, "", 0);
1833 return ERROR_OK;
1836 static int gdb_v_packet(struct connection *connection,
1837 char *packet, int packet_size)
1839 struct gdb_connection *gdb_connection = connection->priv;
1840 struct gdb_service *gdb_service = connection->service->priv;
1841 int result;
1843 /* if flash programming disabled - send a empty reply */
1845 if (gdb_flash_program == 0) {
1846 gdb_put_packet(connection, "", 0);
1847 return ERROR_OK;
1850 if (strstr(packet, "vFlashErase:")) {
1851 unsigned long addr;
1852 unsigned long length;
1854 char *parse = packet + 12;
1855 if (*parse == '\0') {
1856 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1857 return ERROR_SERVER_REMOTE_CLOSED;
1860 addr = strtoul(parse, &parse, 16);
1862 if (*(parse++) != ',' || *parse == '\0') {
1863 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1864 return ERROR_SERVER_REMOTE_CLOSED;
1867 length = strtoul(parse, &parse, 16);
1869 if (*parse != '\0') {
1870 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1871 return ERROR_SERVER_REMOTE_CLOSED;
1874 /* assume all sectors need erasing - stops any problems
1875 * when flash_write is called multiple times */
1876 flash_set_dirty();
1878 /* perform any target specific operations before the erase */
1879 target_call_event_callbacks(gdb_service->target,
1880 TARGET_EVENT_GDB_FLASH_ERASE_START);
1882 /* vFlashErase:addr,length messages require region start and
1883 * end to be "block" aligned ... if padding is ever needed,
1884 * GDB will have become dangerously confused.
1886 result = flash_erase_address_range(gdb_service->target,
1887 false, addr, length);
1889 /* perform any target specific operations after the erase */
1890 target_call_event_callbacks(gdb_service->target,
1891 TARGET_EVENT_GDB_FLASH_ERASE_END);
1893 /* perform erase */
1894 if (result != ERROR_OK) {
1895 /* GDB doesn't evaluate the actual error number returned,
1896 * treat a failed erase as an I/O error
1898 gdb_send_error(connection, EIO);
1899 LOG_ERROR("flash_erase returned %i", result);
1900 } else
1901 gdb_put_packet(connection, "OK", 2);
1903 return ERROR_OK;
1906 if (strstr(packet, "vFlashWrite:")) {
1907 int retval;
1908 unsigned long addr;
1909 unsigned long length;
1910 char *parse = packet + 12;
1912 if (*parse == '\0') {
1913 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1914 return ERROR_SERVER_REMOTE_CLOSED;
1916 addr = strtoul(parse, &parse, 16);
1917 if (*(parse++) != ':') {
1918 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1919 return ERROR_SERVER_REMOTE_CLOSED;
1921 length = packet_size - (parse - packet);
1923 /* create a new image if there isn't already one */
1924 if (gdb_connection->vflash_image == NULL) {
1925 gdb_connection->vflash_image = malloc(sizeof(struct image));
1926 image_open(gdb_connection->vflash_image, "", "build");
1929 /* create new section with content from packet buffer */
1930 retval = image_add_section(gdb_connection->vflash_image,
1931 addr, length, 0x0, (uint8_t *)parse);
1932 if (retval != ERROR_OK)
1933 return retval;
1935 gdb_put_packet(connection, "OK", 2);
1937 return ERROR_OK;
1940 if (!strcmp(packet, "vFlashDone")) {
1941 uint32_t written;
1943 /* process the flashing buffer. No need to erase as GDB
1944 * always issues a vFlashErase first. */
1945 target_call_event_callbacks(gdb_service->target,
1946 TARGET_EVENT_GDB_FLASH_WRITE_START);
1947 result =
1948 flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1949 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1950 if (result != ERROR_OK) {
1951 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1952 gdb_put_packet(connection, "E.memtype", 9);
1953 else
1954 gdb_send_error(connection, EIO);
1955 } else {
1956 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
1957 gdb_put_packet(connection, "OK", 2);
1960 image_close(gdb_connection->vflash_image);
1961 free(gdb_connection->vflash_image);
1962 gdb_connection->vflash_image = NULL;
1964 return ERROR_OK;
1967 gdb_put_packet(connection, "", 0);
1968 return ERROR_OK;
1971 static int gdb_detach(struct connection *connection)
1973 struct gdb_service *gdb_service = connection->service->priv;
1975 target_call_event_callbacks(gdb_service->target,
1976 TARGET_EVENT_GDB_DETACH);
1978 return gdb_put_packet(connection, "OK", 2);
1981 static void gdb_log_callback(void *priv, const char *file, unsigned line,
1982 const char *function, const char *string)
1984 struct connection *connection = priv;
1985 struct gdb_connection *gdb_con = connection->priv;
1987 if (gdb_con->busy) {
1988 /* do not reply this using the O packet */
1989 return;
1992 gdb_output_con(connection, string);
1995 static void gdb_sig_halted(struct connection *connection)
1997 char sig_reply[4];
1998 snprintf(sig_reply, 4, "T%2.2x", 2);
1999 gdb_put_packet(connection, sig_reply, 3);
2003 static int gdb_input_inner(struct connection *connection)
2005 /* Do not allocate this on the stack */
2006 static char gdb_packet_buffer[GDB_BUFFER_SIZE];
2008 struct gdb_service *gdb_service = connection->service->priv;
2009 struct target *target = gdb_service->target;
2010 char *packet = gdb_packet_buffer;
2011 int packet_size;
2012 int retval;
2013 struct gdb_connection *gdb_con = connection->priv;
2014 static int extended_protocol;
2016 /* drain input buffer. If one of the packets fail, then an error
2017 * packet is replied, if applicable.
2019 * This loop will terminate and the error code is returned.
2021 * The calling fn will check if this error is something that
2022 * can be recovered from, or if the connection must be closed.
2024 * If the error is recoverable, this fn is called again to
2025 * drain the rest of the buffer.
2027 do {
2028 packet_size = GDB_BUFFER_SIZE-1;
2029 retval = gdb_get_packet(connection, packet, &packet_size);
2030 if (retval != ERROR_OK)
2031 return retval;
2033 /* terminate with zero */
2034 packet[packet_size] = 0;
2036 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
2037 if (packet[0] == 'X') {
2038 /* binary packets spew junk into the debug log stream */
2039 char buf[50];
2040 int x;
2041 for (x = 0; (x < 49) && (packet[x] != ':'); x++)
2042 buf[x] = packet[x];
2043 buf[x] = 0;
2044 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
2045 } else
2046 LOG_DEBUG("received packet: '%s'", packet);
2049 if (packet_size > 0) {
2050 retval = ERROR_OK;
2051 switch (packet[0]) {
2052 case 'T': /* Is thread alive? */
2053 gdb_thread_packet(connection, packet, packet_size);
2054 break;
2055 case 'H': /* Set current thread ( 'c' for step and continue, 'g' for
2056 * all other operations ) */
2057 gdb_thread_packet(connection, packet, packet_size);
2058 break;
2059 case 'q':
2060 case 'Q':
2061 retval = gdb_thread_packet(connection, packet, packet_size);
2062 if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
2063 retval = gdb_query_packet(connection, packet, packet_size);
2064 break;
2065 case 'g':
2066 retval = gdb_get_registers_packet(connection, packet, packet_size);
2067 break;
2068 case 'G':
2069 retval = gdb_set_registers_packet(connection, packet, packet_size);
2070 break;
2071 case 'p':
2072 retval = gdb_get_register_packet(connection, packet, packet_size);
2073 break;
2074 case 'P':
2075 retval = gdb_set_register_packet(connection, packet, packet_size);
2076 break;
2077 case 'm':
2078 retval = gdb_read_memory_packet(connection, packet, packet_size);
2079 break;
2080 case 'M':
2081 retval = gdb_write_memory_packet(connection, packet, packet_size);
2082 break;
2083 case 'z':
2084 case 'Z':
2085 retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
2086 break;
2087 case '?':
2088 gdb_last_signal_packet(connection, packet, packet_size);
2089 break;
2090 case 'c':
2091 case 's':
2093 gdb_thread_packet(connection, packet, packet_size);
2094 log_add_callback(gdb_log_callback, connection);
2096 if (gdb_con->mem_write_error) {
2097 LOG_ERROR("Memory write failure!");
2099 /* now that we have reported the memory write error,
2100 *we can clear the condition */
2101 gdb_con->mem_write_error = false;
2104 bool nostep = false;
2105 bool already_running = false;
2106 if (target->state == TARGET_RUNNING) {
2107 LOG_WARNING("WARNING! The target is already running. "
2108 "All changes GDB did to registers will be discarded! "
2109 "Waiting for target to halt.");
2110 already_running = true;
2111 } else if (target->state != TARGET_HALTED) {
2112 LOG_WARNING("The target is not in the halted nor running stated, " \
2113 "stepi/continue ignored.");
2114 nostep = true;
2115 } else if ((packet[0] == 's') && gdb_con->sync) {
2116 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2117 * sent by GDB first to OpenOCD, thus defeating the check to
2118 * make only the single stepping have the sync feature...
2120 nostep = true;
2121 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2122 "from the target.");
2124 gdb_con->sync = false;
2126 if (!already_running && nostep) {
2127 /* Either the target isn't in the halted state, then we can't
2128 * step/continue. This might be early setup, etc.
2130 * Or we want to allow GDB to pick up a fresh set of
2131 * register values without modifying the target state.
2134 gdb_sig_halted(connection);
2136 /* stop forwarding log packets! */
2137 log_remove_callback(gdb_log_callback, connection);
2138 } else {
2139 /* We're running/stepping, in which case we can
2140 * forward log output until the target is halted
2142 gdb_con->frontend_state = TARGET_RUNNING;
2143 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2145 if (!already_running) {
2146 /* Here we don't want packet processing to stop even if this fails,
2147 * so we use a local variable instead of retval. */
2148 retval = gdb_step_continue_packet(connection, packet, packet_size);
2149 if (retval != ERROR_OK) {
2150 /* we'll never receive a halted
2151 *condition... issue a false one..
2153 gdb_frontend_halted(target, connection);
2158 break;
2159 case 'v':
2160 retval = gdb_v_packet(connection, packet, packet_size);
2161 break;
2162 case 'D':
2163 retval = gdb_detach(connection);
2164 extended_protocol = 0;
2165 break;
2166 case 'X':
2167 retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
2168 if (retval != ERROR_OK)
2169 return retval;
2170 break;
2171 case 'k':
2172 if (extended_protocol != 0)
2173 break;
2174 gdb_put_packet(connection, "OK", 2);
2175 return ERROR_SERVER_REMOTE_CLOSED;
2176 case '!':
2177 /* handle extended remote protocol */
2178 extended_protocol = 1;
2179 gdb_put_packet(connection, "OK", 2);
2180 break;
2181 case 'R':
2182 /* handle extended restart packet */
2183 breakpoint_clear_target(gdb_service->target);
2184 watchpoint_clear_target(gdb_service->target);
2185 command_run_linef(connection->cmd_ctx,
2186 "ocd_gdb_restart %s",
2187 target_name(target));
2188 /* info rtos parts */
2189 gdb_thread_packet(connection, packet, packet_size);
2190 gdb_put_packet(connection, "OK", 2);
2191 break;
2193 case 'j':
2194 /* packet supported only by smp target i.e cortex_a.c*/
2195 /* handle smp packet replying coreid played to gbd */
2196 gdb_read_smp_packet(connection, packet, packet_size);
2197 break;
2199 case 'J':
2200 /* packet supported only by smp target i.e cortex_a.c */
2201 /* handle smp packet setting coreid to be played at next
2202 * resume to gdb */
2203 gdb_write_smp_packet(connection, packet, packet_size);
2204 break;
2206 default:
2207 /* ignore unknown packets */
2208 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2209 gdb_put_packet(connection, NULL, 0);
2210 break;
2213 /* if a packet handler returned an error, exit input loop */
2214 if (retval != ERROR_OK)
2215 return retval;
2218 if (gdb_con->ctrl_c) {
2219 if (target->state == TARGET_RUNNING) {
2220 retval = target_halt(target);
2221 if (retval != ERROR_OK)
2222 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2223 gdb_con->ctrl_c = 0;
2224 } else {
2225 LOG_INFO(
2226 "The target is not running when halt was requested, stopping GDB.");
2227 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2231 } while (gdb_con->buf_cnt > 0);
2233 return ERROR_OK;
2236 static int gdb_input(struct connection *connection)
2238 int retval = gdb_input_inner(connection);
2239 struct gdb_connection *gdb_con = connection->priv;
2240 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2241 return retval;
2243 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2244 if (gdb_con->closed)
2245 return ERROR_SERVER_REMOTE_CLOSED;
2247 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2248 return ERROR_OK;
2251 static int gdb_target_start(struct target *target, const char *port)
2253 struct gdb_service *gdb_service;
2254 int ret;
2255 gdb_service = malloc(sizeof(struct gdb_service));
2257 if (NULL == gdb_service)
2258 return -ENOMEM;
2260 gdb_service->target = target;
2261 gdb_service->core[0] = -1;
2262 gdb_service->core[1] = -1;
2263 target->gdb_service = gdb_service;
2265 ret = add_service("gdb",
2266 port, 1, &gdb_new_connection, &gdb_input,
2267 &gdb_connection_closed, gdb_service);
2268 /* initialialize all targets gdb service with the same pointer */
2270 struct target_list *head;
2271 struct target *curr;
2272 head = target->head;
2273 while (head != (struct target_list *)NULL) {
2274 curr = head->target;
2275 if (curr != target)
2276 curr->gdb_service = gdb_service;
2277 head = head->next;
2280 return ret;
2283 static int gdb_target_add_one(struct target *target)
2285 /* one gdb instance per smp list */
2286 if ((target->smp) && (target->gdb_service))
2287 return ERROR_OK;
2288 int retval = gdb_target_start(target, gdb_port_next);
2289 if (retval == ERROR_OK) {
2290 long portnumber;
2291 /* If we can parse the port number
2292 * then we increment the port number for the next target.
2294 char *end;
2295 portnumber = strtol(gdb_port_next, &end, 0);
2296 if (!*end) {
2297 if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
2298 free((void *)gdb_port_next);
2299 gdb_port_next = alloc_printf("%d", portnumber+1);
2303 return retval;
2306 int gdb_target_add_all(struct target *target)
2308 if (NULL == target) {
2309 LOG_WARNING("gdb services need one or more targets defined");
2310 return ERROR_OK;
2313 while (NULL != target) {
2314 int retval = gdb_target_add_one(target);
2315 if (ERROR_OK != retval)
2316 return retval;
2318 target = target->next;
2321 return ERROR_OK;
2324 COMMAND_HANDLER(handle_gdb_sync_command)
2326 if (CMD_ARGC != 0)
2327 return ERROR_COMMAND_SYNTAX_ERROR;
2329 if (current_gdb_connection == NULL) {
2330 command_print(CMD_CTX,
2331 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2332 return ERROR_FAIL;
2335 current_gdb_connection->sync = true;
2337 return ERROR_OK;
2340 /* daemon configuration command gdb_port */
2341 COMMAND_HANDLER(handle_gdb_port_command)
2343 int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
2344 if (ERROR_OK == retval) {
2345 free((void *)gdb_port_next);
2346 gdb_port_next = strdup(gdb_port);
2348 return retval;
2351 COMMAND_HANDLER(handle_gdb_memory_map_command)
2353 if (CMD_ARGC != 1)
2354 return ERROR_COMMAND_SYNTAX_ERROR;
2356 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
2357 return ERROR_OK;
2360 COMMAND_HANDLER(handle_gdb_flash_program_command)
2362 if (CMD_ARGC != 1)
2363 return ERROR_COMMAND_SYNTAX_ERROR;
2365 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
2366 return ERROR_OK;
2369 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
2371 if (CMD_ARGC != 1)
2372 return ERROR_COMMAND_SYNTAX_ERROR;
2374 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
2375 return ERROR_OK;
2378 /* gdb_breakpoint_override */
2379 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
2381 if (CMD_ARGC == 0) {
2382 /* nothing */
2383 } else if (CMD_ARGC == 1) {
2384 gdb_breakpoint_override = 1;
2385 if (strcmp(CMD_ARGV[0], "hard") == 0)
2386 gdb_breakpoint_override_type = BKPT_HARD;
2387 else if (strcmp(CMD_ARGV[0], "soft") == 0)
2388 gdb_breakpoint_override_type = BKPT_SOFT;
2389 else if (strcmp(CMD_ARGV[0], "disable") == 0)
2390 gdb_breakpoint_override = 0;
2391 } else
2392 return ERROR_COMMAND_SYNTAX_ERROR;
2393 if (gdb_breakpoint_override)
2394 LOG_USER("force %s breakpoints",
2395 (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
2396 else
2397 LOG_USER("breakpoint type is not overridden");
2399 return ERROR_OK;
2402 static const struct command_registration gdb_command_handlers[] = {
2404 .name = "gdb_sync",
2405 .handler = handle_gdb_sync_command,
2406 .mode = COMMAND_ANY,
2407 .help = "next stepi will return immediately allowing "
2408 "GDB to fetch register state without affecting "
2409 "target state",
2410 .usage = ""
2413 .name = "gdb_port",
2414 .handler = handle_gdb_port_command,
2415 .mode = COMMAND_ANY,
2416 .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2417 "server listens for the next port number after the "
2418 "base port number specified. "
2419 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2420 "output to stdout, an integer is base port number, \"disable\" disables "
2421 "port. Any other string is are interpreted as named pipe to listen to. "
2422 "Output pipe is the same name as input pipe, but with 'o' appended.",
2423 .usage = "[port_num]",
2426 .name = "gdb_memory_map",
2427 .handler = handle_gdb_memory_map_command,
2428 .mode = COMMAND_CONFIG,
2429 .help = "enable or disable memory map",
2430 .usage = "('enable'|'disable')"
2433 .name = "gdb_flash_program",
2434 .handler = handle_gdb_flash_program_command,
2435 .mode = COMMAND_CONFIG,
2436 .help = "enable or disable flash program",
2437 .usage = "('enable'|'disable')"
2440 .name = "gdb_report_data_abort",
2441 .handler = handle_gdb_report_data_abort_command,
2442 .mode = COMMAND_CONFIG,
2443 .help = "enable or disable reporting data aborts",
2444 .usage = "('enable'|'disable')"
2447 .name = "gdb_breakpoint_override",
2448 .handler = handle_gdb_breakpoint_override_command,
2449 .mode = COMMAND_ANY,
2450 .help = "Display or specify type of breakpoint "
2451 "to be used by gdb 'break' commands.",
2452 .usage = "('hard'|'soft'|'disable')"
2454 COMMAND_REGISTRATION_DONE
2457 int gdb_register_commands(struct command_context *cmd_ctx)
2459 gdb_port = strdup("3333");
2460 gdb_port_next = strdup("3333");
2461 return register_commands(cmd_ctx, NULL, gdb_command_handlers);