gdbserver: use common hexify/unhexify routines
[openocd.git] / src / server / gdb_server.c
blob5e570f5d5302df3903bb81f05e347a7b4e5bb336
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 /* 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 bool sync;
72 /* We delay reporting memory write errors until next step/continue or memory
73 * write. This improves performance of gdb load significantly as the GDB packet
74 * can be replied immediately and a new GDB packet will be ready without delay
75 * (ca. 10% or so...). */
76 bool mem_write_error;
77 /* with extended-remote it seems we need to better emulate attach/detach.
78 * what this means is we reply with a W stop reply after a kill packet,
79 * normally we reply with a S reply via gdb_last_signal_packet.
80 * as a side note this behaviour only effects gdb > 6.8 */
81 bool attached;
84 #if 0
85 #define _DEBUG_GDB_IO_
86 #endif
88 static struct gdb_connection *current_gdb_connection;
90 static int gdb_breakpoint_override;
91 static enum breakpoint_type gdb_breakpoint_override_type;
93 static int gdb_error(struct connection *connection, int retval);
94 static const char *gdb_port;
95 static const char *gdb_port_next;
96 static const char DIGITS[16] = "0123456789abcdef";
98 static void gdb_log_callback(void *priv, const char *file, unsigned line,
99 const char *function, const char *string);
101 /* number of gdb connections, mainly to suppress gdb related debugging spam
102 * in helper/log.c when no gdb connections are actually active */
103 int gdb_actual_connections;
105 /* set if we are sending a memory map to gdb
106 * via qXfer:memory-map:read packet */
107 /* enabled by default*/
108 static int gdb_use_memory_map = 1;
109 /* enabled by default*/
110 static int gdb_flash_program = 1;
112 /* if set, data aborts cause an error to be reported in memory read packets
113 * see the code in gdb_read_memory_packet() for further explanations.
114 * Disabled by default.
116 static int gdb_report_data_abort;
118 static int gdb_last_signal(struct target *target)
120 switch (target->debug_reason) {
121 case DBG_REASON_DBGRQ:
122 return 0x2; /* SIGINT */
123 case DBG_REASON_BREAKPOINT:
124 case DBG_REASON_WATCHPOINT:
125 case DBG_REASON_WPTANDBKPT:
126 return 0x05; /* SIGTRAP */
127 case DBG_REASON_SINGLESTEP:
128 return 0x05; /* SIGTRAP */
129 case DBG_REASON_NOTHALTED:
130 return 0x0; /* no signal... shouldn't happen */
131 default:
132 LOG_USER("undefined debug reason %d - target needs reset",
133 target->debug_reason);
134 return 0x0;
138 static int check_pending(struct connection *connection,
139 int timeout_s, int *got_data)
141 /* a non-blocking socket will block if there is 0 bytes available on the socket,
142 * but return with as many bytes as are available immediately
144 struct timeval tv;
145 fd_set read_fds;
146 struct gdb_connection *gdb_con = connection->priv;
147 int t;
148 if (got_data == NULL)
149 got_data = &t;
150 *got_data = 0;
152 if (gdb_con->buf_cnt > 0) {
153 *got_data = 1;
154 return ERROR_OK;
157 FD_ZERO(&read_fds);
158 FD_SET(connection->fd, &read_fds);
160 tv.tv_sec = timeout_s;
161 tv.tv_usec = 0;
162 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0) {
163 /* This can typically be because a "monitor" command took too long
164 * before printing any progress messages
166 if (timeout_s > 0)
167 return ERROR_GDB_TIMEOUT;
168 else
169 return ERROR_OK;
171 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
172 return ERROR_OK;
175 static int gdb_get_char_inner(struct connection *connection, int *next_char)
177 struct gdb_connection *gdb_con = connection->priv;
178 int retval = ERROR_OK;
180 #ifdef _DEBUG_GDB_IO_
181 char *debug_buffer;
182 #endif
183 for (;; ) {
184 if (connection->service->type != CONNECTION_TCP)
185 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
186 else {
187 retval = check_pending(connection, 1, NULL);
188 if (retval != ERROR_OK)
189 return retval;
190 gdb_con->buf_cnt = read_socket(connection->fd,
191 gdb_con->buffer,
192 GDB_BUFFER_SIZE);
195 if (gdb_con->buf_cnt > 0)
196 break;
197 if (gdb_con->buf_cnt == 0) {
198 gdb_con->closed = 1;
199 return ERROR_SERVER_REMOTE_CLOSED;
202 #ifdef _WIN32
203 errno = WSAGetLastError();
205 switch (errno) {
206 case WSAEWOULDBLOCK:
207 usleep(1000);
208 break;
209 case WSAECONNABORTED:
210 gdb_con->closed = 1;
211 return ERROR_SERVER_REMOTE_CLOSED;
212 case WSAECONNRESET:
213 gdb_con->closed = 1;
214 return ERROR_SERVER_REMOTE_CLOSED;
215 default:
216 LOG_ERROR("read: %d", errno);
217 exit(-1);
219 #else
220 switch (errno) {
221 case EAGAIN:
222 usleep(1000);
223 break;
224 case ECONNABORTED:
225 gdb_con->closed = 1;
226 return ERROR_SERVER_REMOTE_CLOSED;
227 case ECONNRESET:
228 gdb_con->closed = 1;
229 return ERROR_SERVER_REMOTE_CLOSED;
230 default:
231 LOG_ERROR("read: %s", strerror(errno));
232 gdb_con->closed = 1;
233 return ERROR_SERVER_REMOTE_CLOSED;
235 #endif
238 #ifdef _DEBUG_GDB_IO_
239 debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
240 LOG_DEBUG("received '%s'", debug_buffer);
241 free(debug_buffer);
242 #endif
244 gdb_con->buf_p = gdb_con->buffer;
245 gdb_con->buf_cnt--;
246 *next_char = *(gdb_con->buf_p++);
247 if (gdb_con->buf_cnt > 0)
248 connection->input_pending = 1;
249 else
250 connection->input_pending = 0;
251 #ifdef _DEBUG_GDB_IO_
252 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
253 #endif
255 return retval;
259 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
260 * held in registers in the inner loop.
262 * For small caches and embedded systems this is important!
264 static inline int gdb_get_char_fast(struct connection *connection,
265 int *next_char, char **buf_p, int *buf_cnt)
267 int retval = ERROR_OK;
269 if ((*buf_cnt)-- > 0) {
270 *next_char = **buf_p;
271 (*buf_p)++;
272 if (*buf_cnt > 0)
273 connection->input_pending = 1;
274 else
275 connection->input_pending = 0;
277 #ifdef _DEBUG_GDB_IO_
278 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
279 #endif
281 return ERROR_OK;
284 struct gdb_connection *gdb_con = connection->priv;
285 gdb_con->buf_p = *buf_p;
286 gdb_con->buf_cnt = *buf_cnt;
287 retval = gdb_get_char_inner(connection, next_char);
288 *buf_p = gdb_con->buf_p;
289 *buf_cnt = gdb_con->buf_cnt;
291 return retval;
294 static int gdb_get_char(struct connection *connection, int *next_char)
296 struct gdb_connection *gdb_con = connection->priv;
297 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
300 static int gdb_putback_char(struct connection *connection, int last_char)
302 struct gdb_connection *gdb_con = connection->priv;
304 if (gdb_con->buf_p > gdb_con->buffer) {
305 *(--gdb_con->buf_p) = last_char;
306 gdb_con->buf_cnt++;
307 } else
308 LOG_ERROR("BUG: couldn't put character back");
310 return ERROR_OK;
313 /* The only way we can detect that the socket is closed is the first time
314 * we write to it, we will fail. Subsequent write operations will
315 * succeed. Shudder! */
316 static int gdb_write(struct connection *connection, void *data, int len)
318 struct gdb_connection *gdb_con = connection->priv;
319 if (gdb_con->closed)
320 return ERROR_SERVER_REMOTE_CLOSED;
322 if (connection_write(connection, data, len) == len)
323 return ERROR_OK;
324 gdb_con->closed = 1;
325 return ERROR_SERVER_REMOTE_CLOSED;
328 static int gdb_put_packet_inner(struct connection *connection,
329 char *buffer, int len)
331 int i;
332 unsigned char my_checksum = 0;
333 #ifdef _DEBUG_GDB_IO_
334 char *debug_buffer;
335 #endif
336 int reply;
337 int retval;
338 struct gdb_connection *gdb_con = connection->priv;
340 for (i = 0; i < len; i++)
341 my_checksum += buffer[i];
343 #ifdef _DEBUG_GDB_IO_
345 * At this point we should have nothing in the input queue from GDB,
346 * however sometimes '-' is sent even though we've already received
347 * an ACK (+) for everything we've sent off.
349 int gotdata;
350 for (;; ) {
351 retval = check_pending(connection, 0, &gotdata);
352 if (retval != ERROR_OK)
353 return retval;
354 if (!gotdata)
355 break;
356 retval = gdb_get_char(connection, &reply);
357 if (retval != ERROR_OK)
358 return retval;
359 if (reply == '$') {
360 /* fix a problem with some IAR tools */
361 gdb_putback_char(connection, reply);
362 LOG_DEBUG("Unexpected start of new packet");
363 break;
366 LOG_WARNING("Discard unexpected char %c", reply);
368 #endif
370 while (1) {
371 #ifdef _DEBUG_GDB_IO_
372 debug_buffer = strndup(buffer, len);
373 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
374 free(debug_buffer);
375 #endif
377 char local_buffer[1024];
378 local_buffer[0] = '$';
379 if ((size_t)len + 4 <= sizeof(local_buffer)) {
380 /* performance gain on smaller packets by only a single call to gdb_write() */
381 memcpy(local_buffer + 1, buffer, len++);
382 local_buffer[len++] = '#';
383 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
384 local_buffer[len++] = DIGITS[my_checksum & 0xf];
385 retval = gdb_write(connection, local_buffer, len);
386 if (retval != ERROR_OK)
387 return retval;
388 } else {
389 /* larger packets are transmitted directly from caller supplied buffer
390 * by several calls to gdb_write() to avoid dynamic allocation */
391 local_buffer[1] = '#';
392 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
393 local_buffer[3] = DIGITS[my_checksum & 0xf];
394 retval = gdb_write(connection, local_buffer, 1);
395 if (retval != ERROR_OK)
396 return retval;
397 retval = gdb_write(connection, buffer, len);
398 if (retval != ERROR_OK)
399 return retval;
400 retval = gdb_write(connection, local_buffer + 1, 3);
401 if (retval != ERROR_OK)
402 return retval;
405 if (gdb_con->noack_mode)
406 break;
408 retval = gdb_get_char(connection, &reply);
409 if (retval != ERROR_OK)
410 return retval;
412 if (reply == '+')
413 break;
414 else if (reply == '-') {
415 /* Stop sending output packets for now */
416 log_remove_callback(gdb_log_callback, connection);
417 LOG_WARNING("negative reply, retrying");
418 } else if (reply == 0x3) {
419 gdb_con->ctrl_c = 1;
420 retval = gdb_get_char(connection, &reply);
421 if (retval != ERROR_OK)
422 return retval;
423 if (reply == '+')
424 break;
425 else if (reply == '-') {
426 /* Stop sending output packets for now */
427 log_remove_callback(gdb_log_callback, connection);
428 LOG_WARNING("negative reply, retrying");
429 } else if (reply == '$') {
430 LOG_ERROR("GDB missing ack(1) - assumed good");
431 gdb_putback_char(connection, reply);
432 return ERROR_OK;
433 } else {
434 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
435 gdb_con->closed = 1;
436 return ERROR_SERVER_REMOTE_CLOSED;
438 } else if (reply == '$') {
439 LOG_ERROR("GDB missing ack(2) - assumed good");
440 gdb_putback_char(connection, reply);
441 return ERROR_OK;
442 } else {
443 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
444 reply);
445 gdb_con->closed = 1;
446 return ERROR_SERVER_REMOTE_CLOSED;
449 if (gdb_con->closed)
450 return ERROR_SERVER_REMOTE_CLOSED;
452 return ERROR_OK;
455 int gdb_put_packet(struct connection *connection, char *buffer, int len)
457 struct gdb_connection *gdb_con = connection->priv;
458 gdb_con->busy = 1;
459 int retval = gdb_put_packet_inner(connection, buffer, len);
460 gdb_con->busy = 0;
462 /* we sent some data, reset timer for keep alive messages */
463 kept_alive();
465 return retval;
468 static inline int fetch_packet(struct connection *connection,
469 int *checksum_ok, int noack, int *len, char *buffer)
471 unsigned char my_checksum = 0;
472 char checksum[3];
473 int character;
474 int retval = ERROR_OK;
476 struct gdb_connection *gdb_con = connection->priv;
477 my_checksum = 0;
478 int count = 0;
479 count = 0;
481 /* move this over into local variables to use registers and give the
482 * more freedom to optimize */
483 char *buf_p = gdb_con->buf_p;
484 int buf_cnt = gdb_con->buf_cnt;
486 for (;; ) {
487 /* The common case is that we have an entire packet with no escape chars.
488 * We need to leave at least 2 bytes in the buffer to have
489 * gdb_get_char() update various bits and bobs correctly.
491 if ((buf_cnt > 2) && ((buf_cnt + count) < *len)) {
492 /* The compiler will struggle a bit with constant propagation and
493 * aliasing, so we help it by showing that these values do not
494 * change inside the loop
496 int i;
497 char *buf = buf_p;
498 int run = buf_cnt - 2;
499 i = 0;
500 int done = 0;
501 while (i < run) {
502 character = *buf++;
503 i++;
504 if (character == '#') {
505 /* Danger! character can be '#' when esc is
506 * used so we need an explicit boolean for done here. */
507 done = 1;
508 break;
511 if (character == '}') {
512 /* data transmitted in binary mode (X packet)
513 * uses 0x7d as escape character */
514 my_checksum += character & 0xff;
515 character = *buf++;
516 i++;
517 my_checksum += character & 0xff;
518 buffer[count++] = (character ^ 0x20) & 0xff;
519 } else {
520 my_checksum += character & 0xff;
521 buffer[count++] = character & 0xff;
524 buf_p += i;
525 buf_cnt -= i;
526 if (done)
527 break;
529 if (count > *len) {
530 LOG_ERROR("packet buffer too small");
531 retval = ERROR_GDB_BUFFER_TOO_SMALL;
532 break;
535 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
536 if (retval != ERROR_OK)
537 break;
539 if (character == '#')
540 break;
542 if (character == '}') {
543 /* data transmitted in binary mode (X packet)
544 * uses 0x7d as escape character */
545 my_checksum += character & 0xff;
547 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
548 if (retval != ERROR_OK)
549 break;
551 my_checksum += character & 0xff;
552 buffer[count++] = (character ^ 0x20) & 0xff;
553 } else {
554 my_checksum += character & 0xff;
555 buffer[count++] = character & 0xff;
559 gdb_con->buf_p = buf_p;
560 gdb_con->buf_cnt = buf_cnt;
562 if (retval != ERROR_OK)
563 return retval;
565 *len = count;
567 retval = gdb_get_char(connection, &character);
568 if (retval != ERROR_OK)
569 return retval;
570 checksum[0] = character;
571 retval = gdb_get_char(connection, &character);
572 if (retval != ERROR_OK)
573 return retval;
574 checksum[1] = character;
575 checksum[2] = 0;
577 if (!noack)
578 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
580 return ERROR_OK;
583 static int gdb_get_packet_inner(struct connection *connection,
584 char *buffer, int *len)
586 int character;
587 int retval;
588 struct gdb_connection *gdb_con = connection->priv;
590 while (1) {
591 do {
592 retval = gdb_get_char(connection, &character);
593 if (retval != ERROR_OK)
594 return retval;
596 #ifdef _DEBUG_GDB_IO_
597 LOG_DEBUG("character: '%c'", character);
598 #endif
600 switch (character) {
601 case '$':
602 break;
603 case '+':
604 /* gdb sends a dummy ack '+' at every remote connect - see
605 * remote_start_remote (remote.c)
606 * in case anyone tries to debug why they receive this
607 * warning every time */
608 LOG_WARNING("acknowledgment received, but no packet pending");
609 break;
610 case '-':
611 LOG_WARNING("negative acknowledgment, but no packet pending");
612 break;
613 case 0x3:
614 gdb_con->ctrl_c = 1;
615 *len = 0;
616 return ERROR_OK;
617 default:
618 LOG_WARNING("ignoring character 0x%x", character);
619 break;
621 } while (character != '$');
623 int checksum_ok = 0;
624 /* explicit code expansion here to get faster inlined code in -O3 by not
625 * calculating checksum */
626 if (gdb_con->noack_mode) {
627 retval = fetch_packet(connection, &checksum_ok, 1, len, buffer);
628 if (retval != ERROR_OK)
629 return retval;
630 } else {
631 retval = fetch_packet(connection, &checksum_ok, 0, len, buffer);
632 if (retval != ERROR_OK)
633 return retval;
636 if (gdb_con->noack_mode) {
637 /* checksum is not checked in noack mode */
638 break;
640 if (checksum_ok) {
641 retval = gdb_write(connection, "+", 1);
642 if (retval != ERROR_OK)
643 return retval;
644 break;
647 if (gdb_con->closed)
648 return ERROR_SERVER_REMOTE_CLOSED;
650 return ERROR_OK;
653 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
655 struct gdb_connection *gdb_con = connection->priv;
656 gdb_con->busy = 1;
657 int retval = gdb_get_packet_inner(connection, buffer, len);
658 gdb_con->busy = 0;
659 return retval;
662 static int gdb_output_con(struct connection *connection, const char *line)
664 char *hex_buffer;
665 int bin_size;
667 bin_size = strlen(line);
669 hex_buffer = malloc(bin_size * 2 + 2);
670 if (hex_buffer == NULL)
671 return ERROR_GDB_BUFFER_TOO_SMALL;
673 hex_buffer[0] = 'O';
674 int pkt_len = hexify(hex_buffer + 1, line, bin_size, bin_size * 2 + 1);
675 int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1);
677 free(hex_buffer);
678 return retval;
681 static int gdb_output(struct command_context *context, const char *line)
683 /* this will be dumped to the log and also sent as an O packet if possible */
684 LOG_USER_N("%s", line);
685 return ERROR_OK;
688 static void gdb_frontend_halted(struct target *target, struct connection *connection)
690 struct gdb_connection *gdb_connection = connection->priv;
692 /* In the GDB protocol when we are stepping or continuing execution,
693 * we have a lingering reply. Upon receiving a halted event
694 * when we have that lingering packet, we reply to the original
695 * step or continue packet.
697 * Executing monitor commands can bring the target in and
698 * out of the running state so we'll see lots of TARGET_EVENT_XXX
699 * that are to be ignored.
701 if (gdb_connection->frontend_state == TARGET_RUNNING) {
702 char sig_reply[4];
703 int signal_var;
705 /* stop forwarding log packets! */
706 log_remove_callback(gdb_log_callback, connection);
708 if (gdb_connection->ctrl_c) {
709 signal_var = 0x2;
710 gdb_connection->ctrl_c = 0;
711 } else
712 signal_var = gdb_last_signal(target);
714 snprintf(sig_reply, 4, "T%2.2x", signal_var);
715 gdb_put_packet(connection, sig_reply, 3);
716 gdb_connection->frontend_state = TARGET_HALTED;
717 rtos_update_threads(target);
721 static int gdb_target_callback_event_handler(struct target *target,
722 enum target_event event, void *priv)
724 int retval;
725 struct connection *connection = priv;
727 target_handle_event(target, event);
728 switch (event) {
729 case TARGET_EVENT_GDB_HALT:
730 gdb_frontend_halted(target, connection);
731 break;
732 case TARGET_EVENT_HALTED:
733 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
734 break;
735 case TARGET_EVENT_GDB_FLASH_ERASE_START:
736 retval = jtag_execute_queue();
737 if (retval != ERROR_OK)
738 return retval;
739 break;
740 default:
741 break;
744 return ERROR_OK;
747 static int gdb_new_connection(struct connection *connection)
749 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
750 struct gdb_service *gdb_service = connection->service->priv;
751 int retval;
752 int initial_ack;
754 connection->priv = gdb_connection;
756 /* initialize gdb connection information */
757 gdb_connection->buf_p = gdb_connection->buffer;
758 gdb_connection->buf_cnt = 0;
759 gdb_connection->ctrl_c = 0;
760 gdb_connection->frontend_state = TARGET_HALTED;
761 gdb_connection->vflash_image = NULL;
762 gdb_connection->closed = 0;
763 gdb_connection->busy = 0;
764 gdb_connection->noack_mode = 0;
765 gdb_connection->sync = true;
766 gdb_connection->mem_write_error = false;
767 gdb_connection->attached = true;
769 /* send ACK to GDB for debug request */
770 gdb_write(connection, "+", 1);
772 /* output goes through gdb connection */
773 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
775 /* we must remove all breakpoints registered to the target as a previous
776 * GDB session could leave dangling breakpoints if e.g. communication
777 * timed out.
779 breakpoint_clear_target(gdb_service->target);
780 watchpoint_clear_target(gdb_service->target);
782 /* clean previous rtos session if supported*/
783 if ((gdb_service->target->rtos) && (gdb_service->target->rtos->type->clean))
784 gdb_service->target->rtos->type->clean(gdb_service->target);
786 /* remove the initial ACK from the incoming buffer */
787 retval = gdb_get_char(connection, &initial_ack);
788 if (retval != ERROR_OK)
789 return retval;
791 /* FIX!!!??? would we actually ever receive a + here???
792 * Not observed.
794 if (initial_ack != '+')
795 gdb_putback_char(connection, initial_ack);
796 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
798 if (gdb_use_memory_map) {
799 /* Connect must fail if the memory map can't be set up correctly.
801 * This will cause an auto_probe to be invoked, which is either
802 * a no-op or it will fail when the target isn't ready(e.g. not halted).
804 int i;
805 for (i = 0; i < flash_get_bank_count(); i++) {
806 struct flash_bank *p;
807 retval = get_flash_bank_by_num(i, &p);
808 if (retval != ERROR_OK) {
809 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
810 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
811 return retval;
816 gdb_actual_connections++;
817 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
818 gdb_actual_connections,
819 target_name(gdb_service->target),
820 target_state_name(gdb_service->target));
822 /* DANGER! If we fail subsequently, we must remove this handler,
823 * otherwise we occasionally see crashes as the timer can invoke the
824 * callback fn.
826 * register callback to be informed about target events */
827 target_register_event_callback(gdb_target_callback_event_handler, connection);
829 return ERROR_OK;
832 static int gdb_connection_closed(struct connection *connection)
834 struct gdb_service *gdb_service = connection->service->priv;
835 struct gdb_connection *gdb_connection = connection->priv;
837 /* we're done forwarding messages. Tear down callback before
838 * cleaning up connection.
840 log_remove_callback(gdb_log_callback, connection);
842 gdb_actual_connections--;
843 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
844 target_name(gdb_service->target),
845 target_state_name(gdb_service->target),
846 gdb_actual_connections);
848 /* see if an image built with vFlash commands is left */
849 if (gdb_connection->vflash_image) {
850 image_close(gdb_connection->vflash_image);
851 free(gdb_connection->vflash_image);
852 gdb_connection->vflash_image = NULL;
855 /* if this connection registered a debug-message receiver delete it */
856 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
858 if (connection->priv) {
859 free(connection->priv);
860 connection->priv = NULL;
861 } else
862 LOG_ERROR("BUG: connection->priv == NULL");
864 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
866 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
868 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
870 return ERROR_OK;
873 static void gdb_send_error(struct connection *connection, uint8_t the_error)
875 char err[4];
876 snprintf(err, 4, "E%2.2X", the_error);
877 gdb_put_packet(connection, err, 3);
880 static int gdb_last_signal_packet(struct connection *connection,
881 char *packet, int packet_size)
883 struct target *target = get_target_from_connection(connection);
884 struct gdb_connection *gdb_con = connection->priv;
885 char sig_reply[4];
886 int signal_var;
888 if (!gdb_con->attached) {
889 /* if we are here we have received a kill packet
890 * reply W stop reply otherwise gdb gets very unhappy */
891 gdb_put_packet(connection, "W00", 3);
892 return ERROR_OK;
895 signal_var = gdb_last_signal(target);
897 snprintf(sig_reply, 4, "S%2.2x", signal_var);
898 gdb_put_packet(connection, sig_reply, 3);
900 return ERROR_OK;
903 static int gdb_reg_pos(struct target *target, int pos, int len)
905 if (target->endianness == TARGET_LITTLE_ENDIAN)
906 return pos;
907 else
908 return len - 1 - pos;
911 /* Convert register to string of bytes. NB! The # of bits in the
912 * register might be non-divisible by 8(a byte), in which
913 * case an entire byte is shown.
915 * NB! the format on the wire is the target endianness
917 * The format of reg->value is little endian
920 static void gdb_str_to_target(struct target *target,
921 char *tstr, struct reg *reg)
923 int i;
925 uint8_t *buf;
926 int buf_len;
927 buf = reg->value;
928 buf_len = DIV_ROUND_UP(reg->size, 8);
930 for (i = 0; i < buf_len; i++) {
931 int j = gdb_reg_pos(target, i, buf_len);
932 tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
933 tstr[i*2 + 1] = DIGITS[buf[j]&0xf];
937 static int hextoint(int c)
939 if (c >= '0' && c <= '9')
940 return c - '0';
941 c = toupper(c);
942 if (c >= 'A' && c <= 'F')
943 return c - 'A' + 10;
944 LOG_ERROR("BUG: invalid register value %08x", c);
945 return 0;
948 /* copy over in register buffer */
949 static void gdb_target_to_reg(struct target *target,
950 char *tstr, int str_len, uint8_t *bin)
952 if (str_len % 2) {
953 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
954 exit(-1);
957 int i;
958 for (i = 0; i < str_len; i += 2) {
959 uint8_t t = hextoint(tstr[i]) << 4;
960 t |= hextoint(tstr[i + 1]);
962 int j = gdb_reg_pos(target, i/2, str_len/2);
963 bin[j] = t;
967 static int gdb_get_registers_packet(struct connection *connection,
968 char *packet, int packet_size)
970 struct target *target = get_target_from_connection(connection);
971 struct reg **reg_list;
972 int reg_list_size;
973 int retval;
974 int reg_packet_size = 0;
975 char *reg_packet;
976 char *reg_packet_p;
977 int i;
979 #ifdef _DEBUG_GDB_IO_
980 LOG_DEBUG("-");
981 #endif
983 if ((target->rtos != NULL) && (ERROR_OK == rtos_get_gdb_reg_list(connection)))
984 return ERROR_OK;
986 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size);
987 if (retval != ERROR_OK)
988 return gdb_error(connection, retval);
990 for (i = 0; i < reg_list_size; i++)
991 reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
993 assert(reg_packet_size > 0);
995 reg_packet = malloc(reg_packet_size);
996 reg_packet_p = reg_packet;
998 for (i = 0; i < reg_list_size; i++) {
999 if (!reg_list[i]->valid)
1000 reg_list[i]->type->get(reg_list[i]);
1001 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
1002 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1005 #ifdef _DEBUG_GDB_IO_
1007 char *reg_packet_p_debug;
1008 reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1009 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1010 free(reg_packet_p_debug);
1012 #endif
1014 gdb_put_packet(connection, reg_packet, reg_packet_size);
1015 free(reg_packet);
1017 free(reg_list);
1019 return ERROR_OK;
1022 static int gdb_set_registers_packet(struct connection *connection,
1023 char *packet, int packet_size)
1025 struct target *target = get_target_from_connection(connection);
1026 int i;
1027 struct reg **reg_list;
1028 int reg_list_size;
1029 int retval;
1030 char *packet_p;
1032 #ifdef _DEBUG_GDB_IO_
1033 LOG_DEBUG("-");
1034 #endif
1036 /* skip command character */
1037 packet++;
1038 packet_size--;
1040 if (packet_size % 2) {
1041 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1042 return ERROR_SERVER_REMOTE_CLOSED;
1045 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size);
1046 if (retval != ERROR_OK)
1047 return gdb_error(connection, retval);
1049 packet_p = packet;
1050 for (i = 0; i < reg_list_size; i++) {
1051 uint8_t *bin_buf;
1052 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1054 if (packet_p + chars > packet + packet_size)
1055 LOG_ERROR("BUG: register packet is too small for registers");
1057 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1058 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1060 reg_list[i]->type->set(reg_list[i], bin_buf);
1062 /* advance packet pointer */
1063 packet_p += chars;
1065 free(bin_buf);
1068 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1069 free(reg_list);
1071 gdb_put_packet(connection, "OK", 2);
1073 return ERROR_OK;
1076 static int gdb_get_register_packet(struct connection *connection,
1077 char *packet, int packet_size)
1079 struct target *target = get_target_from_connection(connection);
1080 char *reg_packet;
1081 int reg_num = strtoul(packet + 1, NULL, 16);
1082 struct reg **reg_list;
1083 int reg_list_size;
1084 int retval;
1086 #ifdef _DEBUG_GDB_IO_
1087 LOG_DEBUG("-");
1088 #endif
1090 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size);
1091 if (retval != ERROR_OK)
1092 return gdb_error(connection, retval);
1094 if (reg_list_size <= reg_num) {
1095 LOG_ERROR("gdb requested a non-existing register");
1096 return ERROR_SERVER_REMOTE_CLOSED;
1099 if (!reg_list[reg_num]->valid)
1100 reg_list[reg_num]->type->get(reg_list[reg_num]);
1102 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1104 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1106 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1108 free(reg_list);
1109 free(reg_packet);
1111 return ERROR_OK;
1114 static int gdb_set_register_packet(struct connection *connection,
1115 char *packet, int packet_size)
1117 struct target *target = get_target_from_connection(connection);
1118 char *separator;
1119 uint8_t *bin_buf;
1120 int reg_num = strtoul(packet + 1, &separator, 16);
1121 struct reg **reg_list;
1122 int reg_list_size;
1123 int retval;
1125 LOG_DEBUG("-");
1127 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size);
1128 if (retval != ERROR_OK)
1129 return gdb_error(connection, retval);
1131 if (reg_list_size <= reg_num) {
1132 LOG_ERROR("gdb requested a non-existing register");
1133 return ERROR_SERVER_REMOTE_CLOSED;
1136 if (*separator != '=') {
1137 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1138 return ERROR_SERVER_REMOTE_CLOSED;
1141 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1142 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1143 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1145 if ((unsigned int)chars != strlen(separator + 1)) {
1146 LOG_ERROR("gdb sent a packet with wrong register size");
1147 free(bin_buf);
1148 return ERROR_SERVER_REMOTE_CLOSED;
1151 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1153 reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1155 gdb_put_packet(connection, "OK", 2);
1157 free(bin_buf);
1158 free(reg_list);
1160 return ERROR_OK;
1163 /* No attempt is made to translate the "retval" to
1164 * GDB speak. This has to be done at the calling
1165 * site as no mapping really exists.
1167 static int gdb_error(struct connection *connection, int retval)
1169 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1170 gdb_send_error(connection, EFAULT);
1171 return ERROR_OK;
1174 /* We don't have to worry about the default 2 second timeout for GDB packets,
1175 * because GDB breaks up large memory reads into smaller reads.
1177 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1179 static int gdb_read_memory_packet(struct connection *connection,
1180 char *packet, int packet_size)
1182 struct target *target = get_target_from_connection(connection);
1183 char *separator;
1184 uint32_t addr = 0;
1185 uint32_t len = 0;
1187 uint8_t *buffer;
1188 char *hex_buffer;
1190 int retval = ERROR_OK;
1192 /* skip command character */
1193 packet++;
1195 addr = strtoul(packet, &separator, 16);
1197 if (*separator != ',') {
1198 LOG_ERROR("incomplete read memory packet received, dropping connection");
1199 return ERROR_SERVER_REMOTE_CLOSED;
1202 len = strtoul(separator + 1, NULL, 16);
1204 buffer = malloc(len);
1206 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1208 retval = target_read_buffer(target, addr, len, buffer);
1210 if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1211 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1212 * At some point this might be fixed in GDB, in which case this code can be removed.
1214 * OpenOCD developers are acutely aware of this problem, but there is nothing
1215 * gained by involving the user in this problem that hopefully will get resolved
1216 * eventually
1218 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1219 * cmd = view%20audit-trail&database = gdb&pr = 2395
1221 * For now, the default is to fix up things to make current GDB versions work.
1222 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1224 memset(buffer, 0, len);
1225 retval = ERROR_OK;
1228 if (retval == ERROR_OK) {
1229 hex_buffer = malloc(len * 2 + 1);
1231 int pkt_len = hexify(hex_buffer, (char *)buffer, len, len * 2 + 1);
1233 gdb_put_packet(connection, hex_buffer, pkt_len);
1235 free(hex_buffer);
1236 } else
1237 retval = gdb_error(connection, retval);
1239 free(buffer);
1241 return retval;
1244 static int gdb_write_memory_packet(struct connection *connection,
1245 char *packet, int packet_size)
1247 struct target *target = get_target_from_connection(connection);
1248 char *separator;
1249 uint32_t addr = 0;
1250 uint32_t len = 0;
1252 uint8_t *buffer;
1253 int retval;
1255 /* skip command character */
1256 packet++;
1258 addr = strtoul(packet, &separator, 16);
1260 if (*separator != ',') {
1261 LOG_ERROR("incomplete write memory packet received, dropping connection");
1262 return ERROR_SERVER_REMOTE_CLOSED;
1265 len = strtoul(separator + 1, &separator, 16);
1267 if (*(separator++) != ':') {
1268 LOG_ERROR("incomplete write memory packet received, dropping connection");
1269 return ERROR_SERVER_REMOTE_CLOSED;
1272 buffer = malloc(len);
1274 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1276 if (unhexify((char *)buffer, separator + 2, len) != (int)len)
1277 LOG_ERROR("unable to decode memory packet");
1279 retval = target_write_buffer(target, addr, len, buffer);
1281 if (retval == ERROR_OK)
1282 gdb_put_packet(connection, "OK", 2);
1283 else
1284 retval = gdb_error(connection, retval);
1286 free(buffer);
1288 return retval;
1291 static int gdb_write_memory_binary_packet(struct connection *connection,
1292 char *packet, int packet_size)
1294 struct target *target = get_target_from_connection(connection);
1295 char *separator;
1296 uint32_t addr = 0;
1297 uint32_t len = 0;
1299 int retval = ERROR_OK;
1301 /* skip command character */
1302 packet++;
1304 addr = strtoul(packet, &separator, 16);
1306 if (*separator != ',') {
1307 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1308 return ERROR_SERVER_REMOTE_CLOSED;
1311 len = strtoul(separator + 1, &separator, 16);
1313 if (*(separator++) != ':') {
1314 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1315 return ERROR_SERVER_REMOTE_CLOSED;
1318 struct gdb_connection *gdb_connection = connection->priv;
1320 if (gdb_connection->mem_write_error) {
1321 retval = ERROR_FAIL;
1322 /* now that we have reported the memory write error, we can clear the condition */
1323 gdb_connection->mem_write_error = false;
1326 /* By replying the packet *immediately* GDB will send us a new packet
1327 * while we write the last one to the target.
1329 if (retval == ERROR_OK)
1330 gdb_put_packet(connection, "OK", 2);
1331 else {
1332 retval = gdb_error(connection, retval);
1333 if (retval != ERROR_OK)
1334 return retval;
1337 if (len) {
1338 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1340 retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1341 if (retval != ERROR_OK)
1342 gdb_connection->mem_write_error = true;
1345 return ERROR_OK;
1348 static int gdb_step_continue_packet(struct connection *connection,
1349 char *packet, int packet_size)
1351 struct target *target = get_target_from_connection(connection);
1352 int current = 0;
1353 uint32_t address = 0x0;
1354 int retval = ERROR_OK;
1356 LOG_DEBUG("-");
1358 if (packet_size > 1) {
1359 packet[packet_size] = 0;
1360 address = strtoul(packet + 1, NULL, 16);
1361 } else
1362 current = 1;
1364 if (packet[0] == 'c') {
1365 LOG_DEBUG("continue");
1366 /* resume at current address, don't handle breakpoints, not debugging */
1367 retval = target_resume(target, current, address, 0, 0);
1368 } else if (packet[0] == 's') {
1369 LOG_DEBUG("step");
1370 /* step at current or address, don't handle breakpoints */
1371 retval = target_step(target, current, address, 0);
1373 return retval;
1376 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1377 char *packet, int packet_size)
1379 struct target *target = get_target_from_connection(connection);
1380 int type;
1381 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1382 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1383 uint32_t address;
1384 uint32_t size;
1385 char *separator;
1386 int retval;
1388 LOG_DEBUG("-");
1390 type = strtoul(packet + 1, &separator, 16);
1392 if (type == 0) /* memory breakpoint */
1393 bp_type = BKPT_SOFT;
1394 else if (type == 1) /* hardware breakpoint */
1395 bp_type = BKPT_HARD;
1396 else if (type == 2) /* write watchpoint */
1397 wp_type = WPT_WRITE;
1398 else if (type == 3) /* read watchpoint */
1399 wp_type = WPT_READ;
1400 else if (type == 4) /* access watchpoint */
1401 wp_type = WPT_ACCESS;
1402 else {
1403 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1404 return ERROR_SERVER_REMOTE_CLOSED;
1407 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1408 bp_type = gdb_breakpoint_override_type;
1410 if (*separator != ',') {
1411 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1412 return ERROR_SERVER_REMOTE_CLOSED;
1415 address = strtoul(separator + 1, &separator, 16);
1417 if (*separator != ',') {
1418 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1419 return ERROR_SERVER_REMOTE_CLOSED;
1422 size = strtoul(separator + 1, &separator, 16);
1424 switch (type) {
1425 case 0:
1426 case 1:
1427 if (packet[0] == 'Z') {
1428 retval = breakpoint_add(target, address, size, bp_type);
1429 if (retval != ERROR_OK) {
1430 retval = gdb_error(connection, retval);
1431 if (retval != ERROR_OK)
1432 return retval;
1433 } else
1434 gdb_put_packet(connection, "OK", 2);
1435 } else {
1436 breakpoint_remove(target, address);
1437 gdb_put_packet(connection, "OK", 2);
1439 break;
1440 case 2:
1441 case 3:
1442 case 4:
1444 if (packet[0] == 'Z') {
1445 retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu);
1446 if (retval != ERROR_OK) {
1447 retval = gdb_error(connection, retval);
1448 if (retval != ERROR_OK)
1449 return retval;
1450 } else
1451 gdb_put_packet(connection, "OK", 2);
1452 } else {
1453 watchpoint_remove(target, address);
1454 gdb_put_packet(connection, "OK", 2);
1456 break;
1458 default:
1459 break;
1462 return ERROR_OK;
1465 /* print out a string and allocate more space as needed,
1466 * mainly used for XML at this point
1468 static void xml_printf(int *retval, char **xml, int *pos, int *size,
1469 const char *fmt, ...)
1471 if (*retval != ERROR_OK)
1472 return;
1473 int first = 1;
1475 for (;; ) {
1476 if ((*xml == NULL) || (!first)) {
1477 /* start by 0 to exercise all the code paths.
1478 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1480 *size = *size * 2 + 2;
1481 char *t = *xml;
1482 *xml = realloc(*xml, *size);
1483 if (*xml == NULL) {
1484 if (t)
1485 free(t);
1486 *retval = ERROR_SERVER_REMOTE_CLOSED;
1487 return;
1491 va_list ap;
1492 int ret;
1493 va_start(ap, fmt);
1494 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1495 va_end(ap);
1496 if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1497 *pos += ret;
1498 return;
1500 /* there was just enough or not enough space, allocate more. */
1501 first = 0;
1505 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1507 char *separator;
1509 /* Extract and NUL-terminate the annex. */
1510 *annex = buf;
1511 while (*buf && *buf != ':')
1512 buf++;
1513 if (*buf == '\0')
1514 return -1;
1515 *buf++ = 0;
1517 /* After the read marker and annex, qXfer looks like a
1518 * traditional 'm' packet. */
1520 *ofs = strtoul(buf, &separator, 16);
1522 if (*separator != ',')
1523 return -1;
1525 *len = strtoul(separator + 1, NULL, 16);
1527 return 0;
1530 static int compare_bank(const void *a, const void *b)
1532 struct flash_bank *b1, *b2;
1533 b1 = *((struct flash_bank **)a);
1534 b2 = *((struct flash_bank **)b);
1536 if (b1->base == b2->base)
1537 return 0;
1538 else if (b1->base > b2->base)
1539 return 1;
1540 else
1541 return -1;
1544 static int gdb_memory_map(struct connection *connection,
1545 char *packet, int packet_size)
1547 /* We get away with only specifying flash here. Regions that are not
1548 * specified are treated as if we provided no memory map(if not we
1549 * could detect the holes and mark them as RAM).
1550 * Normally we only execute this code once, but no big deal if we
1551 * have to regenerate it a couple of times.
1554 struct target *target = get_target_from_connection(connection);
1555 struct flash_bank *p;
1556 char *xml = NULL;
1557 int size = 0;
1558 int pos = 0;
1559 int retval = ERROR_OK;
1560 struct flash_bank **banks;
1561 int offset;
1562 int length;
1563 char *separator;
1564 uint32_t ram_start = 0;
1565 int i;
1566 int target_flash_banks = 0;
1568 /* skip command character */
1569 packet += 23;
1571 offset = strtoul(packet, &separator, 16);
1572 length = strtoul(separator + 1, &separator, 16);
1574 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1576 /* Sort banks in ascending order. We need to report non-flash
1577 * memory as ram (or rather read/write) by default for GDB, since
1578 * it has no concept of non-cacheable read/write memory (i/o etc).
1580 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1581 * Current versions of GDB assume unlisted addresses are RAM...
1583 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1585 for (i = 0; i < flash_get_bank_count(); i++) {
1586 retval = get_flash_bank_by_num(i, &p);
1587 if (retval != ERROR_OK) {
1588 free(banks);
1589 gdb_error(connection, retval);
1590 return retval;
1592 if (p->target == target)
1593 banks[target_flash_banks++] = p;
1596 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1597 compare_bank);
1599 for (i = 0; i < target_flash_banks; i++) {
1600 int j;
1601 unsigned sector_size = 0;
1602 uint32_t start;
1604 p = banks[i];
1605 start = p->base;
1607 if (ram_start < p->base)
1608 xml_printf(&retval, &xml, &pos, &size,
1609 "<memory type=\"ram\" start=\"0x%x\" "
1610 "length=\"0x%x\"/>\n",
1611 ram_start, p->base - ram_start);
1613 /* Report adjacent groups of same-size sectors. So for
1614 * example top boot CFI flash will list an initial region
1615 * with several large sectors (maybe 128KB) and several
1616 * smaller ones at the end (maybe 32KB). STR7 will have
1617 * regions with 8KB, 32KB, and 64KB sectors; etc.
1619 for (j = 0; j < p->num_sectors; j++) {
1620 unsigned group_len;
1622 /* Maybe start a new group of sectors. */
1623 if (sector_size == 0) {
1624 start = p->base + p->sectors[j].offset;
1625 xml_printf(&retval, &xml, &pos, &size,
1626 "<memory type=\"flash\" "
1627 "start=\"0x%x\" ",
1628 start);
1629 sector_size = p->sectors[j].size;
1632 /* Does this finish a group of sectors?
1633 * If not, continue an already-started group.
1635 if (j == p->num_sectors - 1)
1636 group_len = (p->base + p->size) - start;
1637 else if (p->sectors[j + 1].size != sector_size)
1638 group_len = p->base + p->sectors[j + 1].offset
1639 - start;
1640 else
1641 continue;
1643 xml_printf(&retval, &xml, &pos, &size,
1644 "length=\"0x%x\">\n"
1645 "<property name=\"blocksize\">"
1646 "0x%x</property>\n"
1647 "</memory>\n",
1648 group_len,
1649 sector_size);
1650 sector_size = 0;
1653 ram_start = p->base + p->size;
1656 if (ram_start != 0)
1657 xml_printf(&retval, &xml, &pos, &size,
1658 "<memory type=\"ram\" start=\"0x%x\" "
1659 "length=\"0x%x\"/>\n",
1660 ram_start, 0-ram_start);
1661 /* ELSE a flash chip could be at the very end of the 32 bit address
1662 * space, in which case ram_start will be precisely 0
1665 free(banks);
1666 banks = NULL;
1668 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1670 if (retval != ERROR_OK) {
1671 gdb_error(connection, retval);
1672 return retval;
1675 if (offset + length > pos)
1676 length = pos - offset;
1678 char *t = malloc(length + 1);
1679 t[0] = 'l';
1680 memcpy(t + 1, xml + offset, length);
1681 gdb_put_packet(connection, t, length + 1);
1683 free(t);
1684 free(xml);
1685 return ERROR_OK;
1688 static int gdb_query_packet(struct connection *connection,
1689 char *packet, int packet_size)
1691 struct command_context *cmd_ctx = connection->cmd_ctx;
1692 struct gdb_connection *gdb_connection = connection->priv;
1693 struct target *target = get_target_from_connection(connection);
1695 if (strncmp(packet, "qRcmd,", 6) == 0) {
1696 if (packet_size > 6) {
1697 char *cmd;
1698 cmd = malloc((packet_size - 6) / 2 + 1);
1699 int len = unhexify(cmd, packet + 6, (packet_size - 6) / 2);
1700 cmd[len] = 0;
1702 /* We want to print all debug output to GDB connection */
1703 log_add_callback(gdb_log_callback, connection);
1704 target_call_timer_callbacks_now();
1705 /* some commands need to know the GDB connection, make note of current
1706 * GDB connection. */
1707 current_gdb_connection = gdb_connection;
1708 command_run_line(cmd_ctx, cmd);
1709 current_gdb_connection = NULL;
1710 target_call_timer_callbacks_now();
1711 log_remove_callback(gdb_log_callback, connection);
1712 free(cmd);
1714 gdb_put_packet(connection, "OK", 2);
1715 return ERROR_OK;
1716 } else if (strncmp(packet, "qCRC:", 5) == 0) {
1717 if (packet_size > 5) {
1718 int retval;
1719 char gdb_reply[10];
1720 char *separator;
1721 uint32_t checksum;
1722 uint32_t addr = 0;
1723 uint32_t len = 0;
1725 /* skip command character */
1726 packet += 5;
1728 addr = strtoul(packet, &separator, 16);
1730 if (*separator != ',') {
1731 LOG_ERROR("incomplete read memory packet received, dropping connection");
1732 return ERROR_SERVER_REMOTE_CLOSED;
1735 len = strtoul(separator + 1, NULL, 16);
1737 retval = target_checksum_memory(target, addr, len, &checksum);
1739 if (retval == ERROR_OK) {
1740 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
1741 gdb_put_packet(connection, gdb_reply, 9);
1742 } else {
1743 retval = gdb_error(connection, retval);
1744 if (retval != ERROR_OK)
1745 return retval;
1748 return ERROR_OK;
1750 } else if (strncmp(packet, "qSupported", 10) == 0) {
1751 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1752 * disable qXfer:features:read for the moment */
1753 int retval = ERROR_OK;
1754 char *buffer = NULL;
1755 int pos = 0;
1756 int size = 0;
1758 xml_printf(&retval,
1759 &buffer,
1760 &pos,
1761 &size,
1762 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1763 (GDB_BUFFER_SIZE - 1),
1764 ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1766 if (retval != ERROR_OK) {
1767 gdb_send_error(connection, 01);
1768 return ERROR_OK;
1771 gdb_put_packet(connection, buffer, strlen(buffer));
1772 free(buffer);
1774 return ERROR_OK;
1775 } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
1776 && (flash_get_bank_count() > 0))
1777 return gdb_memory_map(connection, packet, packet_size);
1778 else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
1779 char *xml = NULL;
1780 int size = 0;
1781 int pos = 0;
1782 int retval = ERROR_OK;
1784 int offset;
1785 unsigned int length;
1786 char *annex;
1788 /* skip command character */
1789 packet += 20;
1791 if (decode_xfer_read(packet, &annex, &offset, &length) < 0) {
1792 gdb_send_error(connection, 01);
1793 return ERROR_OK;
1796 if (strcmp(annex, "target.xml") != 0) {
1797 gdb_send_error(connection, 01);
1798 return ERROR_OK;
1801 xml_printf(&retval,
1802 &xml,
1803 &pos,
1804 &size, \
1805 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1807 if (retval != ERROR_OK) {
1808 gdb_error(connection, retval);
1809 return retval;
1812 gdb_put_packet(connection, xml, strlen(xml));
1814 free(xml);
1815 return ERROR_OK;
1816 } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
1817 gdb_connection->noack_mode = 1;
1818 gdb_put_packet(connection, "OK", 2);
1819 return ERROR_OK;
1822 gdb_put_packet(connection, "", 0);
1823 return ERROR_OK;
1826 static int gdb_v_packet(struct connection *connection,
1827 char *packet, int packet_size)
1829 struct gdb_connection *gdb_connection = connection->priv;
1830 struct gdb_service *gdb_service = connection->service->priv;
1831 int result;
1833 /* if flash programming disabled - send a empty reply */
1835 if (gdb_flash_program == 0) {
1836 gdb_put_packet(connection, "", 0);
1837 return ERROR_OK;
1840 if (strncmp(packet, "vFlashErase:", 12) == 0) {
1841 unsigned long addr;
1842 unsigned long length;
1844 char *parse = packet + 12;
1845 if (*parse == '\0') {
1846 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1847 return ERROR_SERVER_REMOTE_CLOSED;
1850 addr = strtoul(parse, &parse, 16);
1852 if (*(parse++) != ',' || *parse == '\0') {
1853 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1854 return ERROR_SERVER_REMOTE_CLOSED;
1857 length = strtoul(parse, &parse, 16);
1859 if (*parse != '\0') {
1860 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1861 return ERROR_SERVER_REMOTE_CLOSED;
1864 /* assume all sectors need erasing - stops any problems
1865 * when flash_write is called multiple times */
1866 flash_set_dirty();
1868 /* perform any target specific operations before the erase */
1869 target_call_event_callbacks(gdb_service->target,
1870 TARGET_EVENT_GDB_FLASH_ERASE_START);
1872 /* vFlashErase:addr,length messages require region start and
1873 * end to be "block" aligned ... if padding is ever needed,
1874 * GDB will have become dangerously confused.
1876 result = flash_erase_address_range(gdb_service->target,
1877 false, addr, length);
1879 /* perform any target specific operations after the erase */
1880 target_call_event_callbacks(gdb_service->target,
1881 TARGET_EVENT_GDB_FLASH_ERASE_END);
1883 /* perform erase */
1884 if (result != ERROR_OK) {
1885 /* GDB doesn't evaluate the actual error number returned,
1886 * treat a failed erase as an I/O error
1888 gdb_send_error(connection, EIO);
1889 LOG_ERROR("flash_erase returned %i", result);
1890 } else
1891 gdb_put_packet(connection, "OK", 2);
1893 return ERROR_OK;
1896 if (strncmp(packet, "vFlashWrite:", 12) == 0) {
1897 int retval;
1898 unsigned long addr;
1899 unsigned long length;
1900 char *parse = packet + 12;
1902 if (*parse == '\0') {
1903 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1904 return ERROR_SERVER_REMOTE_CLOSED;
1906 addr = strtoul(parse, &parse, 16);
1907 if (*(parse++) != ':') {
1908 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1909 return ERROR_SERVER_REMOTE_CLOSED;
1911 length = packet_size - (parse - packet);
1913 /* create a new image if there isn't already one */
1914 if (gdb_connection->vflash_image == NULL) {
1915 gdb_connection->vflash_image = malloc(sizeof(struct image));
1916 image_open(gdb_connection->vflash_image, "", "build");
1919 /* create new section with content from packet buffer */
1920 retval = image_add_section(gdb_connection->vflash_image,
1921 addr, length, 0x0, (uint8_t *)parse);
1922 if (retval != ERROR_OK)
1923 return retval;
1925 gdb_put_packet(connection, "OK", 2);
1927 return ERROR_OK;
1930 if (strncmp(packet, "vFlashDone", 10) == 0) {
1931 uint32_t written;
1933 /* process the flashing buffer. No need to erase as GDB
1934 * always issues a vFlashErase first. */
1935 target_call_event_callbacks(gdb_service->target,
1936 TARGET_EVENT_GDB_FLASH_WRITE_START);
1937 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1938 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1939 if (result != ERROR_OK) {
1940 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1941 gdb_put_packet(connection, "E.memtype", 9);
1942 else
1943 gdb_send_error(connection, EIO);
1944 } else {
1945 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
1946 gdb_put_packet(connection, "OK", 2);
1949 image_close(gdb_connection->vflash_image);
1950 free(gdb_connection->vflash_image);
1951 gdb_connection->vflash_image = NULL;
1953 return ERROR_OK;
1956 gdb_put_packet(connection, "", 0);
1957 return ERROR_OK;
1960 static int gdb_detach(struct connection *connection)
1962 struct gdb_service *gdb_service = connection->service->priv;
1964 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
1966 return gdb_put_packet(connection, "OK", 2);
1969 static void gdb_log_callback(void *priv, const char *file, unsigned line,
1970 const char *function, const char *string)
1972 struct connection *connection = priv;
1973 struct gdb_connection *gdb_con = connection->priv;
1975 if (gdb_con->busy) {
1976 /* do not reply this using the O packet */
1977 return;
1980 gdb_output_con(connection, string);
1983 static void gdb_sig_halted(struct connection *connection)
1985 char sig_reply[4];
1986 snprintf(sig_reply, 4, "T%2.2x", 2);
1987 gdb_put_packet(connection, sig_reply, 3);
1990 static int gdb_input_inner(struct connection *connection)
1992 /* Do not allocate this on the stack */
1993 static char gdb_packet_buffer[GDB_BUFFER_SIZE];
1995 struct gdb_service *gdb_service = connection->service->priv;
1996 struct target *target = gdb_service->target;
1997 char *packet = gdb_packet_buffer;
1998 int packet_size;
1999 int retval;
2000 struct gdb_connection *gdb_con = connection->priv;
2001 static int extended_protocol;
2003 /* drain input buffer. If one of the packets fail, then an error
2004 * packet is replied, if applicable.
2006 * This loop will terminate and the error code is returned.
2008 * The calling fn will check if this error is something that
2009 * can be recovered from, or if the connection must be closed.
2011 * If the error is recoverable, this fn is called again to
2012 * drain the rest of the buffer.
2014 do {
2015 packet_size = GDB_BUFFER_SIZE-1;
2016 retval = gdb_get_packet(connection, packet, &packet_size);
2017 if (retval != ERROR_OK)
2018 return retval;
2020 /* terminate with zero */
2021 packet[packet_size] = 0;
2023 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
2024 if (packet[0] == 'X') {
2025 /* binary packets spew junk into the debug log stream */
2026 char buf[50];
2027 int x;
2028 for (x = 0; (x < 49) && (packet[x] != ':'); x++)
2029 buf[x] = packet[x];
2030 buf[x] = 0;
2031 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
2032 } else
2033 LOG_DEBUG("received packet: '%s'", packet);
2036 if (packet_size > 0) {
2037 retval = ERROR_OK;
2038 switch (packet[0]) {
2039 case 'T': /* Is thread alive? */
2040 gdb_thread_packet(connection, packet, packet_size);
2041 break;
2042 case 'H': /* Set current thread ( 'c' for step and continue,
2043 * 'g' for all other operations ) */
2044 gdb_thread_packet(connection, packet, packet_size);
2045 break;
2046 case 'q':
2047 case 'Q':
2048 retval = gdb_thread_packet(connection, packet, packet_size);
2049 if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
2050 retval = gdb_query_packet(connection, packet, packet_size);
2051 break;
2052 case 'g':
2053 retval = gdb_get_registers_packet(connection, packet, packet_size);
2054 break;
2055 case 'G':
2056 retval = gdb_set_registers_packet(connection, packet, packet_size);
2057 break;
2058 case 'p':
2059 retval = gdb_get_register_packet(connection, packet, packet_size);
2060 break;
2061 case 'P':
2062 retval = gdb_set_register_packet(connection, packet, packet_size);
2063 break;
2064 case 'm':
2065 retval = gdb_read_memory_packet(connection, packet, packet_size);
2066 break;
2067 case 'M':
2068 retval = gdb_write_memory_packet(connection, packet, packet_size);
2069 break;
2070 case 'z':
2071 case 'Z':
2072 retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
2073 break;
2074 case '?':
2075 gdb_last_signal_packet(connection, packet, packet_size);
2076 break;
2077 case 'c':
2078 case 's':
2080 gdb_thread_packet(connection, packet, packet_size);
2081 log_add_callback(gdb_log_callback, connection);
2083 if (gdb_con->mem_write_error) {
2084 LOG_ERROR("Memory write failure!");
2086 /* now that we have reported the memory write error,
2087 * we can clear the condition */
2088 gdb_con->mem_write_error = false;
2091 bool nostep = false;
2092 bool already_running = false;
2093 if (target->state == TARGET_RUNNING) {
2094 LOG_WARNING("WARNING! The target is already running. "
2095 "All changes GDB did to registers will be discarded! "
2096 "Waiting for target to halt.");
2097 already_running = true;
2098 } else if (target->state != TARGET_HALTED) {
2099 LOG_WARNING("The target is not in the halted nor running stated, " \
2100 "stepi/continue ignored.");
2101 nostep = true;
2102 } else if ((packet[0] == 's') && gdb_con->sync) {
2103 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2104 * sent by GDB first to OpenOCD, thus defeating the check to
2105 * make only the single stepping have the sync feature...
2107 nostep = true;
2108 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2109 "from the target.");
2111 gdb_con->sync = false;
2113 if (!already_running && nostep) {
2114 /* Either the target isn't in the halted state, then we can't
2115 * step/continue. This might be early setup, etc.
2117 * Or we want to allow GDB to pick up a fresh set of
2118 * register values without modifying the target state.
2121 gdb_sig_halted(connection);
2123 /* stop forwarding log packets! */
2124 log_remove_callback(gdb_log_callback, connection);
2125 } else {
2126 /* We're running/stepping, in which case we can
2127 * forward log output until the target is halted
2129 gdb_con->frontend_state = TARGET_RUNNING;
2130 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2132 if (!already_running) {
2133 /* Here we don't want packet processing to stop even if this fails,
2134 * so we use a local variable instead of retval. */
2135 retval = gdb_step_continue_packet(connection, packet, packet_size);
2136 if (retval != ERROR_OK) {
2137 /* we'll never receive a halted
2138 * condition... issue a false one..
2140 gdb_frontend_halted(target, connection);
2145 break;
2146 case 'v':
2147 retval = gdb_v_packet(connection, packet, packet_size);
2148 break;
2149 case 'D':
2150 retval = gdb_detach(connection);
2151 extended_protocol = 0;
2152 break;
2153 case 'X':
2154 retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
2155 if (retval != ERROR_OK)
2156 return retval;
2157 break;
2158 case 'k':
2159 if (extended_protocol != 0) {
2160 gdb_con->attached = false;
2161 break;
2163 gdb_put_packet(connection, "OK", 2);
2164 return ERROR_SERVER_REMOTE_CLOSED;
2165 case '!':
2166 /* handle extended remote protocol */
2167 extended_protocol = 1;
2168 gdb_put_packet(connection, "OK", 2);
2169 break;
2170 case 'R':
2171 /* handle extended restart packet */
2172 breakpoint_clear_target(gdb_service->target);
2173 watchpoint_clear_target(gdb_service->target);
2174 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
2175 target_name(target));
2176 /* set connection as attached after reset */
2177 gdb_con->attached = true;
2178 /* info rtos parts */
2179 gdb_thread_packet(connection, packet, packet_size);
2180 break;
2182 case 'j':
2183 /* packet supported only by smp target i.e cortex_a.c*/
2184 /* handle smp packet replying coreid played to gbd */
2185 gdb_read_smp_packet(connection, packet, packet_size);
2186 break;
2188 case 'J':
2189 /* packet supported only by smp target i.e cortex_a.c */
2190 /* handle smp packet setting coreid to be played at next
2191 * resume to gdb */
2192 gdb_write_smp_packet(connection, packet, packet_size);
2193 break;
2195 default:
2196 /* ignore unknown packets */
2197 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2198 gdb_put_packet(connection, NULL, 0);
2199 break;
2202 /* if a packet handler returned an error, exit input loop */
2203 if (retval != ERROR_OK)
2204 return retval;
2207 if (gdb_con->ctrl_c) {
2208 if (target->state == TARGET_RUNNING) {
2209 retval = target_halt(target);
2210 if (retval != ERROR_OK)
2211 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2212 gdb_con->ctrl_c = 0;
2213 } else {
2214 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2215 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2219 } while (gdb_con->buf_cnt > 0);
2221 return ERROR_OK;
2224 static int gdb_input(struct connection *connection)
2226 int retval = gdb_input_inner(connection);
2227 struct gdb_connection *gdb_con = connection->priv;
2228 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2229 return retval;
2231 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2232 if (gdb_con->closed)
2233 return ERROR_SERVER_REMOTE_CLOSED;
2235 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2236 return ERROR_OK;
2239 static int gdb_target_start(struct target *target, const char *port)
2241 struct gdb_service *gdb_service;
2242 int ret;
2243 gdb_service = malloc(sizeof(struct gdb_service));
2245 if (NULL == gdb_service)
2246 return -ENOMEM;
2248 gdb_service->target = target;
2249 gdb_service->core[0] = -1;
2250 gdb_service->core[1] = -1;
2251 target->gdb_service = gdb_service;
2253 ret = add_service("gdb",
2254 port, 1, &gdb_new_connection, &gdb_input,
2255 &gdb_connection_closed, gdb_service);
2256 /* initialialize all targets gdb service with the same pointer */
2258 struct target_list *head;
2259 struct target *curr;
2260 head = target->head;
2261 while (head != (struct target_list *)NULL) {
2262 curr = head->target;
2263 if (curr != target)
2264 curr->gdb_service = gdb_service;
2265 head = head->next;
2268 return ret;
2271 static int gdb_target_add_one(struct target *target)
2273 /* one gdb instance per smp list */
2274 if ((target->smp) && (target->gdb_service))
2275 return ERROR_OK;
2276 int retval = gdb_target_start(target, gdb_port_next);
2277 if (retval == ERROR_OK) {
2278 long portnumber;
2279 /* If we can parse the port number
2280 * then we increment the port number for the next target.
2282 char *end;
2283 portnumber = strtol(gdb_port_next, &end, 0);
2284 if (!*end) {
2285 if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
2286 free((void *)gdb_port_next);
2287 gdb_port_next = alloc_printf("%d", portnumber+1);
2291 return retval;
2294 int gdb_target_add_all(struct target *target)
2296 if (NULL == target) {
2297 LOG_WARNING("gdb services need one or more targets defined");
2298 return ERROR_OK;
2301 while (NULL != target) {
2302 int retval = gdb_target_add_one(target);
2303 if (ERROR_OK != retval)
2304 return retval;
2306 target = target->next;
2309 return ERROR_OK;
2312 COMMAND_HANDLER(handle_gdb_sync_command)
2314 if (CMD_ARGC != 0)
2315 return ERROR_COMMAND_SYNTAX_ERROR;
2317 if (current_gdb_connection == NULL) {
2318 command_print(CMD_CTX,
2319 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2320 return ERROR_FAIL;
2323 current_gdb_connection->sync = true;
2325 return ERROR_OK;
2328 /* daemon configuration command gdb_port */
2329 COMMAND_HANDLER(handle_gdb_port_command)
2331 int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
2332 if (ERROR_OK == retval) {
2333 free((void *)gdb_port_next);
2334 gdb_port_next = strdup(gdb_port);
2336 return retval;
2339 COMMAND_HANDLER(handle_gdb_memory_map_command)
2341 if (CMD_ARGC != 1)
2342 return ERROR_COMMAND_SYNTAX_ERROR;
2344 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
2345 return ERROR_OK;
2348 COMMAND_HANDLER(handle_gdb_flash_program_command)
2350 if (CMD_ARGC != 1)
2351 return ERROR_COMMAND_SYNTAX_ERROR;
2353 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
2354 return ERROR_OK;
2357 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
2359 if (CMD_ARGC != 1)
2360 return ERROR_COMMAND_SYNTAX_ERROR;
2362 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
2363 return ERROR_OK;
2366 /* gdb_breakpoint_override */
2367 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
2369 if (CMD_ARGC == 0) {
2370 /* nothing */
2371 } else if (CMD_ARGC == 1) {
2372 gdb_breakpoint_override = 1;
2373 if (strcmp(CMD_ARGV[0], "hard") == 0)
2374 gdb_breakpoint_override_type = BKPT_HARD;
2375 else if (strcmp(CMD_ARGV[0], "soft") == 0)
2376 gdb_breakpoint_override_type = BKPT_SOFT;
2377 else if (strcmp(CMD_ARGV[0], "disable") == 0)
2378 gdb_breakpoint_override = 0;
2379 } else
2380 return ERROR_COMMAND_SYNTAX_ERROR;
2381 if (gdb_breakpoint_override)
2382 LOG_USER("force %s breakpoints",
2383 (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
2384 else
2385 LOG_USER("breakpoint type is not overridden");
2387 return ERROR_OK;
2390 static const struct command_registration gdb_command_handlers[] = {
2392 .name = "gdb_sync",
2393 .handler = handle_gdb_sync_command,
2394 .mode = COMMAND_ANY,
2395 .help = "next stepi will return immediately allowing "
2396 "GDB to fetch register state without affecting "
2397 "target state",
2398 .usage = ""
2401 .name = "gdb_port",
2402 .handler = handle_gdb_port_command,
2403 .mode = COMMAND_ANY,
2404 .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2405 "server listens for the next port number after the "
2406 "base port number specified. "
2407 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2408 "output to stdout, an integer is base port number, \"disable\" disables "
2409 "port. Any other string is are interpreted as named pipe to listen to. "
2410 "Output pipe is the same name as input pipe, but with 'o' appended.",
2411 .usage = "[port_num]",
2414 .name = "gdb_memory_map",
2415 .handler = handle_gdb_memory_map_command,
2416 .mode = COMMAND_CONFIG,
2417 .help = "enable or disable memory map",
2418 .usage = "('enable'|'disable')"
2421 .name = "gdb_flash_program",
2422 .handler = handle_gdb_flash_program_command,
2423 .mode = COMMAND_CONFIG,
2424 .help = "enable or disable flash program",
2425 .usage = "('enable'|'disable')"
2428 .name = "gdb_report_data_abort",
2429 .handler = handle_gdb_report_data_abort_command,
2430 .mode = COMMAND_CONFIG,
2431 .help = "enable or disable reporting data aborts",
2432 .usage = "('enable'|'disable')"
2435 .name = "gdb_breakpoint_override",
2436 .handler = handle_gdb_breakpoint_override_command,
2437 .mode = COMMAND_ANY,
2438 .help = "Display or specify type of breakpoint "
2439 "to be used by gdb 'break' commands.",
2440 .usage = "('hard'|'soft'|'disable')"
2442 COMMAND_REGISTRATION_DONE
2445 int gdb_register_commands(struct command_context *cmd_ctx)
2447 gdb_port = strdup("3333");
2448 gdb_port_next = strdup("3333");
2449 return register_commands(cmd_ctx, NULL, gdb_command_handlers);