TCL/SPEAr: remove code to autodetect DDR
[openocd/andreasf.git] / src / server / gdb_server.c
blob4dd9bd10aec81edf3b90ccdefd0b102df76fc6f1
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 ***************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include <target/breakpoints.h>
37 #include <target/target_request.h>
38 #include <target/register.h>
39 #include "server.h"
40 #include <flash/nor/core.h>
41 #include "gdb_server.h"
42 #include <target/image.h>
43 #include <jtag/jtag.h>
44 #include "rtos/rtos.h"
45 #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
60 char buffer[GDB_BUFFER_SIZE];
61 char *buf_p;
62 int buf_cnt;
63 int ctrl_c;
64 enum target_state frontend_state;
65 struct image *vflash_image;
66 int closed;
67 int busy;
68 int noack_mode;
69 bool sync; /* set flag to true if you want the next stepi to return immediately.
70 allowing GDB to pick up a fresh set of register values from the target
71 without modifying the target state. */
72 /* We delay reporting memory write errors until next step/continue or memory
73 * write. This improves performance of gdb load significantly as the GDB packet
74 * can be replied immediately and a new GDB packet will be ready without delay
75 * (ca. 10% or so...).
77 bool mem_write_error;
81 #if 0
82 #define _DEBUG_GDB_IO_
83 #endif
85 static struct gdb_connection *current_gdb_connection;
87 static int gdb_breakpoint_override;
88 static enum breakpoint_type gdb_breakpoint_override_type;
90 static int gdb_error(struct connection *connection, int retval);
91 static const char *gdb_port;
92 static const char *gdb_port_next;
93 static const char DIGITS[16] = "0123456789abcdef";
95 static void gdb_log_callback(void *priv, const char *file, unsigned line,
96 const char *function, const char *string);
98 /* number of gdb connections, mainly to suppress gdb related debugging spam
99 * in helper/log.c when no gdb connections are actually active */
100 int gdb_actual_connections;
102 /* set if we are sending a memory map to gdb
103 * via qXfer:memory-map:read packet */
104 /* enabled by default*/
105 static int gdb_use_memory_map = 1;
106 /* enabled by default*/
107 static int gdb_flash_program = 1;
109 /* if set, data aborts cause an error to be reported in memory read packets
110 * see the code in gdb_read_memory_packet() for further explanations.
111 * Disabled by default.
113 static int gdb_report_data_abort;
115 static int gdb_last_signal(struct target *target)
117 switch (target->debug_reason)
119 case DBG_REASON_DBGRQ:
120 return 0x2; /* SIGINT */
121 case DBG_REASON_BREAKPOINT:
122 case DBG_REASON_WATCHPOINT:
123 case DBG_REASON_WPTANDBKPT:
124 return 0x05; /* SIGTRAP */
125 case DBG_REASON_SINGLESTEP:
126 return 0x05; /* SIGTRAP */
127 case DBG_REASON_NOTHALTED:
128 return 0x0; /* no signal... shouldn't happen */
129 default:
130 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
131 return 0x0;
135 static int check_pending(struct connection *connection,
136 int timeout_s, int *got_data)
138 /* a non-blocking socket will block if there is 0 bytes available on the socket,
139 * but return with as many bytes as are available immediately
141 struct timeval tv;
142 fd_set read_fds;
143 struct gdb_connection *gdb_con = connection->priv;
144 int t;
145 if (got_data == NULL)
146 got_data=&t;
147 *got_data = 0;
149 if (gdb_con->buf_cnt > 0)
151 *got_data = 1;
152 return ERROR_OK;
155 FD_ZERO(&read_fds);
156 FD_SET(connection->fd, &read_fds);
158 tv.tv_sec = timeout_s;
159 tv.tv_usec = 0;
160 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
162 /* This can typically be because a "monitor" command took too long
163 * before printing any progress messages
165 if (timeout_s > 0)
167 return ERROR_GDB_TIMEOUT;
168 } else
170 return ERROR_OK;
173 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
174 return ERROR_OK;
177 static int gdb_get_char_inner(struct connection *connection, int* next_char)
179 struct gdb_connection *gdb_con = connection->priv;
180 int retval = ERROR_OK;
182 #ifdef _DEBUG_GDB_IO_
183 char *debug_buffer;
184 #endif
185 for (;;)
187 if (connection->service->type != CONNECTION_TCP)
189 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
191 else
193 retval = check_pending(connection, 1, NULL);
194 if (retval != ERROR_OK)
195 return retval;
196 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
199 if (gdb_con->buf_cnt > 0)
201 break;
203 if (gdb_con->buf_cnt == 0)
205 gdb_con->closed = 1;
206 return ERROR_SERVER_REMOTE_CLOSED;
209 #ifdef _WIN32
210 errno = WSAGetLastError();
212 switch (errno)
214 case WSAEWOULDBLOCK:
215 usleep(1000);
216 break;
217 case WSAECONNABORTED:
218 gdb_con->closed = 1;
219 return ERROR_SERVER_REMOTE_CLOSED;
220 case WSAECONNRESET:
221 gdb_con->closed = 1;
222 return ERROR_SERVER_REMOTE_CLOSED;
223 default:
224 LOG_ERROR("read: %d", errno);
225 exit(-1);
227 #else
228 switch (errno)
230 case EAGAIN:
231 usleep(1000);
232 break;
233 case ECONNABORTED:
234 gdb_con->closed = 1;
235 return ERROR_SERVER_REMOTE_CLOSED;
236 case ECONNRESET:
237 gdb_con->closed = 1;
238 return ERROR_SERVER_REMOTE_CLOSED;
239 default:
240 LOG_ERROR("read: %s", strerror(errno));
241 gdb_con->closed = 1;
242 return ERROR_SERVER_REMOTE_CLOSED;
244 #endif
247 #ifdef _DEBUG_GDB_IO_
248 debug_buffer = malloc(gdb_con->buf_cnt + 1);
249 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
250 debug_buffer[gdb_con->buf_cnt] = 0;
251 LOG_DEBUG("received '%s'", debug_buffer);
252 free(debug_buffer);
253 #endif
255 gdb_con->buf_p = gdb_con->buffer;
256 gdb_con->buf_cnt--;
257 *next_char = *(gdb_con->buf_p++);
258 if (gdb_con->buf_cnt > 0)
259 connection->input_pending = 1;
260 else
261 connection->input_pending = 0;
262 #ifdef _DEBUG_GDB_IO_
263 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
264 #endif
266 return retval;
270 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
271 * held in registers in the inner loop.
273 * For small caches and embedded systems this is important!
275 static inline int gdb_get_char_fast(struct connection *connection, int* next_char, char **buf_p, int *buf_cnt)
277 int retval = ERROR_OK;
279 if ((*buf_cnt)-- > 0)
281 *next_char = **buf_p;
282 (*buf_p)++;
283 if (*buf_cnt > 0)
284 connection->input_pending = 1;
285 else
286 connection->input_pending = 0;
288 #ifdef _DEBUG_GDB_IO_
289 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
290 #endif
292 return ERROR_OK;
295 struct gdb_connection *gdb_con = connection->priv;
296 gdb_con->buf_p = *buf_p;
297 gdb_con->buf_cnt = *buf_cnt;
298 retval = gdb_get_char_inner(connection, next_char);
299 *buf_p = gdb_con->buf_p;
300 *buf_cnt = gdb_con->buf_cnt;
302 return retval;
306 static int gdb_get_char(struct connection *connection, int* next_char)
308 struct gdb_connection *gdb_con = connection->priv;
309 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
313 static int gdb_putback_char(struct connection *connection, int last_char)
315 struct gdb_connection *gdb_con = connection->priv;
317 if (gdb_con->buf_p > gdb_con->buffer)
319 *(--gdb_con->buf_p) = last_char;
320 gdb_con->buf_cnt++;
322 else
324 LOG_ERROR("BUG: couldn't put character back");
327 return ERROR_OK;
330 /* The only way we can detect that the socket is closed is the first time
331 * we write to it, we will fail. Subsequent write operations will
332 * succeed. Shudder! */
333 static int gdb_write(struct connection *connection, void *data, int len)
335 struct gdb_connection *gdb_con = connection->priv;
336 if (gdb_con->closed)
337 return ERROR_SERVER_REMOTE_CLOSED;
339 if (connection_write(connection, data, len) == len)
341 return ERROR_OK;
343 gdb_con->closed = 1;
344 return ERROR_SERVER_REMOTE_CLOSED;
347 static int gdb_put_packet_inner(struct connection *connection,
348 char *buffer, int len)
350 int i;
351 unsigned char my_checksum = 0;
352 #ifdef _DEBUG_GDB_IO_
353 char *debug_buffer;
354 #endif
355 int reply;
356 int retval;
357 struct gdb_connection *gdb_con = connection->priv;
359 for (i = 0; i < len; i++)
360 my_checksum += buffer[i];
362 #ifdef _DEBUG_GDB_IO_
364 * At this point we should have nothing in the input queue from GDB,
365 * however sometimes '-' is sent even though we've already received
366 * an ACK (+) for everything we've sent off.
368 int gotdata;
369 for (;;)
371 retval = check_pending(connection, 0, &gotdata);
372 if (retval != ERROR_OK)
373 return retval;
374 if (!gotdata)
375 break;
376 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
377 return retval;
378 if (reply == '$') {
379 /* fix a problem with some IAR tools */
380 gdb_putback_char(connection, reply);
381 LOG_DEBUG("Unexpected start of new packet");
382 break;
385 LOG_WARNING("Discard unexpected char %c", reply);
387 #endif
389 while (1)
391 #ifdef _DEBUG_GDB_IO_
392 debug_buffer = malloc(len + 1);
393 memcpy(debug_buffer, buffer, len);
394 debug_buffer[len] = 0;
395 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
396 free(debug_buffer);
397 #endif
399 char local_buffer[1024];
400 local_buffer[0] = '$';
401 if ((size_t)len + 4 <= sizeof(local_buffer))
403 /* performance gain on smaller packets by only a single call to gdb_write() */
404 memcpy(local_buffer + 1, buffer, len++);
405 local_buffer[len++] = '#';
406 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
407 local_buffer[len++] = DIGITS[my_checksum & 0xf];
408 if ((retval = gdb_write(connection, local_buffer, len)) != ERROR_OK)
410 return retval;
413 else
415 /* larger packets are transmitted directly from caller supplied buffer
416 by several calls to gdb_write() to avoid dynamic allocation */
417 local_buffer[1] = '#';
418 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
419 local_buffer[3] = DIGITS[my_checksum & 0xf];
420 if ((retval = gdb_write(connection, local_buffer, 1)) != ERROR_OK)
422 return retval;
424 if ((retval = gdb_write(connection, buffer, len)) != ERROR_OK)
426 return retval;
428 if ((retval = gdb_write(connection, local_buffer + 1, 3)) != ERROR_OK)
430 return retval;
434 if (gdb_con->noack_mode)
435 break;
437 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
438 return retval;
440 if (reply == '+')
441 break;
442 else if (reply == '-')
444 /* Stop sending output packets for now */
445 log_remove_callback(gdb_log_callback, connection);
446 LOG_WARNING("negative reply, retrying");
448 else if (reply == 0x3)
450 gdb_con->ctrl_c = 1;
451 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
452 return retval;
453 if (reply == '+')
454 break;
455 else if (reply == '-')
457 /* Stop sending output packets for now */
458 log_remove_callback(gdb_log_callback, connection);
459 LOG_WARNING("negative reply, retrying");
461 else if (reply == '$') {
462 LOG_ERROR("GDB missing ack(1) - assumed good");
463 gdb_putback_char(connection, reply);
464 return ERROR_OK;
465 } else {
467 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
468 gdb_con->closed = 1;
469 return ERROR_SERVER_REMOTE_CLOSED;
472 else if (reply == '$') {
473 LOG_ERROR("GDB missing ack(2) - assumed good");
474 gdb_putback_char(connection, reply);
475 return ERROR_OK;
477 else
479 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply);
480 gdb_con->closed = 1;
481 return ERROR_SERVER_REMOTE_CLOSED;
484 if (gdb_con->closed)
485 return ERROR_SERVER_REMOTE_CLOSED;
487 return ERROR_OK;
490 int gdb_put_packet(struct connection *connection, char *buffer, int len)
492 struct gdb_connection *gdb_con = connection->priv;
493 gdb_con->busy = 1;
494 int retval = gdb_put_packet_inner(connection, buffer, len);
495 gdb_con->busy = 0;
497 /* we sent some data, reset timer for keep alive messages */
498 kept_alive();
500 return retval;
503 static __inline__ int fetch_packet(struct connection *connection, int *checksum_ok, int noack, int *len, char *buffer)
505 unsigned char my_checksum = 0;
506 char checksum[3];
507 int character;
508 int retval = ERROR_OK;
510 struct gdb_connection *gdb_con = connection->priv;
511 my_checksum = 0;
512 int count = 0;
513 count = 0;
515 /* move this over into local variables to use registers and give the
516 * more freedom to optimize */
517 char *buf_p = gdb_con->buf_p;
518 int buf_cnt = gdb_con->buf_cnt;
520 for (;;)
522 /* The common case is that we have an entire packet with no escape chars.
523 * We need to leave at least 2 bytes in the buffer to have
524 * gdb_get_char() update various bits and bobs correctly.
526 if ((buf_cnt > 2) && ((buf_cnt + count) < *len))
528 /* The compiler will struggle a bit with constant propagation and
529 * aliasing, so we help it by showing that these values do not
530 * change inside the loop
532 int i;
533 char *buf = buf_p;
534 int run = buf_cnt - 2;
535 i = 0;
536 int done = 0;
537 while (i < run)
539 character = *buf++;
540 i++;
541 if (character == '#')
543 /* Danger! character can be '#' when esc is
544 * used so we need an explicit boolean for done here.
546 done = 1;
547 break;
550 if (character == '}')
552 /* data transmitted in binary mode (X packet)
553 * uses 0x7d as escape character */
554 my_checksum += character & 0xff;
555 character = *buf++;
556 i++;
557 my_checksum += character & 0xff;
558 buffer[count++] = (character ^ 0x20) & 0xff;
560 else
562 my_checksum += character & 0xff;
563 buffer[count++] = character & 0xff;
566 buf_p += i;
567 buf_cnt -= i;
568 if (done)
569 break;
571 if (count > *len)
573 LOG_ERROR("packet buffer too small");
574 retval = ERROR_GDB_BUFFER_TOO_SMALL;
575 break;
578 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
579 if (retval != ERROR_OK)
580 break;
582 if (character == '#')
583 break;
585 if (character == '}')
587 /* data transmitted in binary mode (X packet)
588 * uses 0x7d as escape character */
589 my_checksum += character & 0xff;
591 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
592 if (retval != ERROR_OK)
593 break;
595 my_checksum += character & 0xff;
596 buffer[count++] = (character ^ 0x20) & 0xff;
598 else
600 my_checksum += character & 0xff;
601 buffer[count++] = character & 0xff;
605 gdb_con->buf_p = buf_p;
606 gdb_con->buf_cnt = buf_cnt;
608 if (retval != ERROR_OK)
609 return retval;
611 *len = count;
613 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
614 return retval;
615 checksum[0] = character;
616 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
617 return retval;
618 checksum[1] = character;
619 checksum[2] = 0;
621 if (!noack)
623 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
626 return ERROR_OK;
629 static int gdb_get_packet_inner(struct connection *connection,
630 char *buffer, int *len)
632 int character;
633 int retval;
634 struct gdb_connection *gdb_con = connection->priv;
636 while (1)
640 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
641 return retval;
643 #ifdef _DEBUG_GDB_IO_
644 LOG_DEBUG("character: '%c'", character);
645 #endif
647 switch (character)
649 case '$':
650 break;
651 case '+':
652 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
653 * in case anyone tries to debug why they receive this warning every time */
654 LOG_WARNING("acknowledgment received, but no packet pending");
655 break;
656 case '-':
657 LOG_WARNING("negative acknowledgment, but no packet pending");
658 break;
659 case 0x3:
660 gdb_con->ctrl_c = 1;
661 *len = 0;
662 return ERROR_OK;
663 default:
664 LOG_WARNING("ignoring character 0x%x", character);
665 break;
667 } while (character != '$');
671 int checksum_ok = 0;
672 /* explicit code expansion here to get faster inlined code in -O3 by not
673 * calculating checksum
675 if (gdb_con->noack_mode)
677 if ((retval = fetch_packet(connection, &checksum_ok, 1, len, buffer)) != ERROR_OK)
678 return retval;
679 } else
681 if ((retval = fetch_packet(connection, &checksum_ok, 0, len, buffer)) != ERROR_OK)
682 return retval;
685 if (gdb_con->noack_mode)
687 /* checksum is not checked in noack mode */
688 break;
690 if (checksum_ok)
692 if ((retval = gdb_write(connection, "+", 1)) != ERROR_OK)
694 return retval;
696 break;
699 if (gdb_con->closed)
700 return ERROR_SERVER_REMOTE_CLOSED;
702 return ERROR_OK;
705 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
707 struct gdb_connection *gdb_con = connection->priv;
708 gdb_con->busy = 1;
709 int retval = gdb_get_packet_inner(connection, buffer, len);
710 gdb_con->busy = 0;
711 return retval;
714 static int gdb_output_con(struct connection *connection, const char* line)
716 char *hex_buffer;
717 int i, bin_size;
719 bin_size = strlen(line);
721 hex_buffer = malloc(bin_size*2 + 2);
722 if (hex_buffer == NULL)
723 return ERROR_GDB_BUFFER_TOO_SMALL;
725 hex_buffer[0] = 'O';
726 for (i = 0; i < bin_size; i++)
727 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
728 hex_buffer[bin_size*2 + 1] = 0;
730 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
732 free(hex_buffer);
733 return retval;
736 static int gdb_output(struct command_context *context, const char* line)
738 /* this will be dumped to the log and also sent as an O packet if possible */
739 LOG_USER_N("%s", line);
740 return ERROR_OK;
744 static void gdb_frontend_halted(struct target *target, struct connection *connection)
746 struct gdb_connection *gdb_connection = connection->priv;
748 /* In the GDB protocol when we are stepping or continuing execution,
749 * we have a lingering reply. Upon receiving a halted event
750 * when we have that lingering packet, we reply to the original
751 * step or continue packet.
753 * Executing monitor commands can bring the target in and
754 * out of the running state so we'll see lots of TARGET_EVENT_XXX
755 * that are to be ignored.
757 if (gdb_connection->frontend_state == TARGET_RUNNING)
759 char sig_reply[4];
760 int signal_var;
762 /* stop forwarding log packets! */
763 log_remove_callback(gdb_log_callback, connection);
765 if (gdb_connection->ctrl_c)
767 signal_var = 0x2;
768 gdb_connection->ctrl_c = 0;
770 else
772 signal_var = gdb_last_signal(target);
775 snprintf(sig_reply, 4, "T%2.2x", signal_var);
776 gdb_put_packet(connection, sig_reply, 3);
777 gdb_connection->frontend_state = TARGET_HALTED;
778 rtos_update_threads( target );
782 static int gdb_target_callback_event_handler(struct target *target,
783 enum target_event event, void *priv)
785 int retval;
786 struct connection *connection = priv;
788 target_handle_event(target, event);
789 switch (event)
791 case TARGET_EVENT_GDB_HALT:
792 gdb_frontend_halted(target, connection);
793 break;
794 case TARGET_EVENT_HALTED:
795 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
796 break;
797 case TARGET_EVENT_GDB_FLASH_ERASE_START:
798 target_handle_event(target, TARGET_EVENT_OLD_gdb_program_config);
799 if ((retval = jtag_execute_queue()) != ERROR_OK)
801 return retval;
803 break;
804 default:
805 break;
808 return ERROR_OK;
811 static int gdb_new_connection(struct connection *connection)
813 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
814 struct gdb_service *gdb_service = connection->service->priv;
815 int retval;
816 int initial_ack;
818 connection->priv = gdb_connection;
820 /* initialize gdb connection information */
821 gdb_connection->buf_p = gdb_connection->buffer;
822 gdb_connection->buf_cnt = 0;
823 gdb_connection->ctrl_c = 0;
824 gdb_connection->frontend_state = TARGET_HALTED;
825 gdb_connection->vflash_image = NULL;
826 gdb_connection->closed = 0;
827 gdb_connection->busy = 0;
828 gdb_connection->noack_mode = 0;
829 gdb_connection->sync = true;
830 gdb_connection->mem_write_error = false;
832 /* send ACK to GDB for debug request */
833 gdb_write(connection, "+", 1);
835 /* output goes through gdb connection */
836 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
838 /* we must remove all breakpoints registered to the target as a previous
839 * GDB session could leave dangling breakpoints if e.g. communication
840 * timed out.
842 breakpoint_clear_target(gdb_service->target);
843 watchpoint_clear_target(gdb_service->target);
845 /* remove the initial ACK from the incoming buffer */
846 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
847 return retval;
849 /* FIX!!!??? would we actually ever receive a + here???
850 * Not observed.
852 if (initial_ack != '+')
853 gdb_putback_char(connection, initial_ack);
854 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
856 if (gdb_use_memory_map)
858 /* Connect must fail if the memory map can't be set up correctly.
860 * This will cause an auto_probe to be invoked, which is either
861 * a no-op or it will fail when the target isn't ready(e.g. not halted).
863 int i;
864 for (i = 0; i < flash_get_bank_count(); i++)
866 struct flash_bank *p;
867 retval = get_flash_bank_by_num(i, &p);
868 if (retval != ERROR_OK)
870 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
871 return retval;
876 gdb_actual_connections++;
877 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
878 gdb_actual_connections,
879 target_name(gdb_service->target),
880 target_state_name(gdb_service->target));
882 /* DANGER! If we fail subsequently, we must remove this handler,
883 * otherwise we occasionally see crashes as the timer can invoke the
884 * callback fn.
886 * register callback to be informed about target events */
887 target_register_event_callback(gdb_target_callback_event_handler, connection);
889 return ERROR_OK;
892 static int gdb_connection_closed(struct connection *connection)
894 struct gdb_service *gdb_service = connection->service->priv;
895 struct gdb_connection *gdb_connection = connection->priv;
897 /* we're done forwarding messages. Tear down callback before
898 * cleaning up connection.
900 log_remove_callback(gdb_log_callback, connection);
902 gdb_actual_connections--;
903 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
904 target_name(gdb_service->target),
905 target_state_name(gdb_service->target),
906 gdb_actual_connections);
908 /* see if an image built with vFlash commands is left */
909 if (gdb_connection->vflash_image)
911 image_close(gdb_connection->vflash_image);
912 free(gdb_connection->vflash_image);
913 gdb_connection->vflash_image = NULL;
916 /* if this connection registered a debug-message receiver delete it */
917 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
919 if (connection->priv)
921 free(connection->priv);
922 connection->priv = NULL;
924 else
926 LOG_ERROR("BUG: connection->priv == NULL");
930 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
932 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
934 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
936 return ERROR_OK;
939 static void gdb_send_error(struct connection *connection, uint8_t the_error)
941 char err[4];
942 snprintf(err, 4, "E%2.2X", the_error);
943 gdb_put_packet(connection, err, 3);
946 static int gdb_last_signal_packet(struct connection *connection,
947 char* packet, int packet_size)
949 struct target *target = get_target_from_connection(connection);
950 char sig_reply[4];
951 int signal_var;
953 signal_var = gdb_last_signal(target);
955 snprintf(sig_reply, 4, "S%2.2x", signal_var);
956 gdb_put_packet(connection, sig_reply, 3);
958 return ERROR_OK;
961 static int gdb_reg_pos(struct target *target, int pos, int len)
963 if (target->endianness == TARGET_LITTLE_ENDIAN)
964 return pos;
965 else
966 return len - 1 - pos;
969 /* Convert register to string of bytes. NB! The # of bits in the
970 * register might be non-divisible by 8(a byte), in which
971 * case an entire byte is shown.
973 * NB! the format on the wire is the target endianness
975 * The format of reg->value is little endian
978 static void gdb_str_to_target(struct target *target,
979 char *tstr, struct reg *reg)
981 int i;
983 uint8_t *buf;
984 int buf_len;
985 buf = reg->value;
986 buf_len = DIV_ROUND_UP(reg->size, 8);
988 for (i = 0; i < buf_len; i++)
990 int j = gdb_reg_pos(target, i, buf_len);
991 tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
992 tstr[i*2 + 1] = DIGITS[buf[j]&0xf];
996 static int hextoint(int c)
998 if (c>='0'&&c<='9')
1000 return c-'0';
1002 c = toupper(c);
1003 if (c>='A'&&c<='F')
1005 return c-'A'+10;
1007 LOG_ERROR("BUG: invalid register value %08x", c);
1008 return 0;
1011 /* copy over in register buffer */
1012 static void gdb_target_to_reg(struct target *target,
1013 char *tstr, int str_len, uint8_t *bin)
1015 if (str_len % 2)
1017 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1018 exit(-1);
1021 int i;
1022 for (i = 0; i < str_len; i += 2)
1024 uint8_t t = hextoint(tstr[i]) << 4;
1025 t |= hextoint(tstr[i + 1]);
1027 int j = gdb_reg_pos(target, i/2, str_len/2);
1028 bin[j] = t;
1032 static int gdb_get_registers_packet(struct connection *connection,
1033 char* packet, int packet_size)
1035 struct target *target = get_target_from_connection(connection);
1036 struct reg **reg_list;
1037 int reg_list_size;
1038 int retval;
1039 int reg_packet_size = 0;
1040 char *reg_packet;
1041 char *reg_packet_p;
1042 int i;
1044 #ifdef _DEBUG_GDB_IO_
1045 LOG_DEBUG("-");
1046 #endif
1048 if ( ( target->rtos != NULL ) &&
1049 ( ERROR_FAIL != rtos_get_gdb_reg_list( connection, &reg_list, &reg_list_size) ) )
1051 return ERROR_OK;
1054 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1056 return gdb_error(connection, retval);
1059 for (i = 0; i < reg_list_size; i++)
1061 reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1064 assert(reg_packet_size > 0);
1066 reg_packet = malloc(reg_packet_size);
1067 reg_packet_p = reg_packet;
1069 for (i = 0; i < reg_list_size; i++)
1071 if (!reg_list[i]->valid)
1072 reg_list[i]->type->get(reg_list[i]);
1073 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
1074 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1077 #ifdef _DEBUG_GDB_IO_
1079 char *reg_packet_p;
1080 reg_packet_p = strndup(reg_packet, reg_packet_size);
1081 LOG_DEBUG("reg_packet: %s", reg_packet_p);
1082 free(reg_packet_p);
1084 #endif
1086 gdb_put_packet(connection, reg_packet, reg_packet_size);
1087 free(reg_packet);
1089 free(reg_list);
1091 return ERROR_OK;
1094 static int gdb_set_registers_packet(struct connection *connection,
1095 char *packet, int packet_size)
1097 struct target *target = get_target_from_connection(connection);
1098 int i;
1099 struct reg **reg_list;
1100 int reg_list_size;
1101 int retval;
1102 char *packet_p;
1104 #ifdef _DEBUG_GDB_IO_
1105 LOG_DEBUG("-");
1106 #endif
1108 /* skip command character */
1109 packet++;
1110 packet_size--;
1112 if (packet_size % 2)
1114 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1115 return ERROR_SERVER_REMOTE_CLOSED;
1118 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1120 return gdb_error(connection, retval);
1123 packet_p = packet;
1124 for (i = 0; i < reg_list_size; i++)
1126 uint8_t *bin_buf;
1127 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1129 if (packet_p + chars > packet + packet_size)
1131 LOG_ERROR("BUG: register packet is too small for registers");
1134 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1135 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1137 reg_list[i]->type->set(reg_list[i], bin_buf);
1139 /* advance packet pointer */
1140 packet_p += chars;
1143 free(bin_buf);
1146 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1147 free(reg_list);
1149 gdb_put_packet(connection, "OK", 2);
1151 return ERROR_OK;
1154 static int gdb_get_register_packet(struct connection *connection,
1155 char *packet, int packet_size)
1157 struct target *target = get_target_from_connection(connection);
1158 char *reg_packet;
1159 int reg_num = strtoul(packet + 1, NULL, 16);
1160 struct reg **reg_list;
1161 int reg_list_size;
1162 int retval;
1164 #ifdef _DEBUG_GDB_IO_
1165 LOG_DEBUG("-");
1166 #endif
1168 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1170 return gdb_error(connection, retval);
1173 if (reg_list_size <= reg_num)
1175 LOG_ERROR("gdb requested a non-existing register");
1176 exit(-1);
1179 if (!reg_list[reg_num]->valid)
1180 reg_list[reg_num]->type->get(reg_list[reg_num]);
1182 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1184 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1186 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1188 free(reg_list);
1189 free(reg_packet);
1191 return ERROR_OK;
1194 static int gdb_set_register_packet(struct connection *connection,
1195 char *packet, int packet_size)
1197 struct target *target = get_target_from_connection(connection);
1198 char *separator;
1199 uint8_t *bin_buf;
1200 int reg_num = strtoul(packet + 1, &separator, 16);
1201 struct reg **reg_list;
1202 int reg_list_size;
1203 int retval;
1205 LOG_DEBUG("-");
1207 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1209 return gdb_error(connection, retval);
1212 if (reg_list_size < reg_num)
1214 LOG_ERROR("gdb requested a non-existing register");
1215 return ERROR_SERVER_REMOTE_CLOSED;
1218 if (*separator != '=')
1220 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1221 return ERROR_SERVER_REMOTE_CLOSED;
1224 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1225 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1226 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1228 /* fix!!! add some sanity checks on packet size here */
1230 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1232 reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1234 gdb_put_packet(connection, "OK", 2);
1236 free(bin_buf);
1237 free(reg_list);
1239 return ERROR_OK;
1242 /* No attempt is made to translate the "retval" to
1243 * GDB speak. This has to be done at the calling
1244 * site as no mapping really exists.
1246 static int gdb_error(struct connection *connection, int retval)
1248 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1249 gdb_send_error(connection, EFAULT);
1250 return ERROR_OK;
1253 /* We don't have to worry about the default 2 second timeout for GDB packets,
1254 * because GDB breaks up large memory reads into smaller reads.
1256 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1258 static int gdb_read_memory_packet(struct connection *connection,
1259 char *packet, int packet_size)
1261 struct target *target = get_target_from_connection(connection);
1262 char *separator;
1263 uint32_t addr = 0;
1264 uint32_t len = 0;
1266 uint8_t *buffer;
1267 char *hex_buffer;
1269 int retval = ERROR_OK;
1271 /* skip command character */
1272 packet++;
1274 addr = strtoul(packet, &separator, 16);
1276 if (*separator != ',')
1278 LOG_ERROR("incomplete read memory packet received, dropping connection");
1279 return ERROR_SERVER_REMOTE_CLOSED;
1282 len = strtoul(separator + 1, NULL, 16);
1284 buffer = malloc(len);
1286 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1288 retval = target_read_buffer(target, addr, len, buffer);
1290 if ((retval != ERROR_OK)&&!gdb_report_data_abort)
1292 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1293 * At some point this might be fixed in GDB, in which case this code can be removed.
1295 * OpenOCD developers are acutely aware of this problem, but there is nothing
1296 * gained by involving the user in this problem that hopefully will get resolved
1297 * eventually
1299 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1301 * For now, the default is to fix up things to make current GDB versions work.
1302 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1304 memset(buffer, 0, len);
1305 retval = ERROR_OK;
1308 if (retval == ERROR_OK)
1310 hex_buffer = malloc(len * 2 + 1);
1312 uint32_t i;
1313 for (i = 0; i < len; i++)
1315 uint8_t t = buffer[i];
1316 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1317 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1320 gdb_put_packet(connection, hex_buffer, len * 2);
1322 free(hex_buffer);
1324 else
1326 retval = gdb_error(connection, retval);
1329 free(buffer);
1331 return retval;
1334 static int gdb_write_memory_packet(struct connection *connection,
1335 char *packet, int packet_size)
1337 struct target *target = get_target_from_connection(connection);
1338 char *separator;
1339 uint32_t addr = 0;
1340 uint32_t len = 0;
1342 uint8_t *buffer;
1344 uint32_t i;
1345 int retval;
1347 /* skip command character */
1348 packet++;
1350 addr = strtoul(packet, &separator, 16);
1352 if (*separator != ',')
1354 LOG_ERROR("incomplete write memory packet received, dropping connection");
1355 return ERROR_SERVER_REMOTE_CLOSED;
1358 len = strtoul(separator + 1, &separator, 16);
1360 if (*(separator++) != ':')
1362 LOG_ERROR("incomplete write memory packet received, dropping connection");
1363 return ERROR_SERVER_REMOTE_CLOSED;
1366 buffer = malloc(len);
1368 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1370 for (i = 0; i < len; i++)
1372 uint32_t tmp;
1373 sscanf(separator + 2*i, "%2" SCNx32 , &tmp);
1374 buffer[i] = tmp;
1377 retval = target_write_buffer(target, addr, len, buffer);
1379 if (retval == ERROR_OK)
1381 gdb_put_packet(connection, "OK", 2);
1383 else
1385 retval = gdb_error(connection, retval);
1388 free(buffer);
1390 return retval;
1393 static int gdb_write_memory_binary_packet(struct connection *connection,
1394 char *packet, int packet_size)
1396 struct target *target = get_target_from_connection(connection);
1397 char *separator;
1398 uint32_t addr = 0;
1399 uint32_t len = 0;
1401 int retval = ERROR_OK;
1403 /* skip command character */
1404 packet++;
1406 addr = strtoul(packet, &separator, 16);
1408 if (*separator != ',')
1410 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1411 return ERROR_SERVER_REMOTE_CLOSED;
1414 len = strtoul(separator + 1, &separator, 16);
1416 if (*(separator++) != ':')
1418 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1419 return ERROR_SERVER_REMOTE_CLOSED;
1422 struct gdb_connection *gdb_connection = connection->priv;
1424 if (gdb_connection->mem_write_error)
1426 retval = ERROR_FAIL;
1427 /* now that we have reported the memory write error, we can clear the condition */
1428 gdb_connection->mem_write_error = false;
1431 /* By replying the packet *immediately* GDB will send us a new packet
1432 * while we write the last one to the target.
1434 if (retval == ERROR_OK)
1436 gdb_put_packet(connection, "OK", 2);
1438 else
1440 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1441 return retval;
1444 if (len)
1446 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1448 retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
1449 if (retval != ERROR_OK)
1451 gdb_connection->mem_write_error = true;
1455 return ERROR_OK;
1458 static int gdb_step_continue_packet(struct connection *connection,
1459 char *packet, int packet_size)
1461 struct target *target = get_target_from_connection(connection);
1462 int current = 0;
1463 uint32_t address = 0x0;
1464 int retval = ERROR_OK;
1466 LOG_DEBUG("-");
1468 if (packet_size > 1)
1470 packet[packet_size] = 0;
1471 address = strtoul(packet + 1, NULL, 16);
1473 else
1475 current = 1;
1478 if (packet[0] == 'c')
1480 LOG_DEBUG("continue");
1481 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
1482 retval = target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1484 else if (packet[0] == 's')
1486 LOG_DEBUG("step");
1487 /* step at current or address, don't handle breakpoints */
1488 retval = target_step(target, current, address, 0);
1490 return retval;
1493 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1494 char *packet, int packet_size)
1496 struct target *target = get_target_from_connection(connection);
1497 int type;
1498 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1499 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1500 uint32_t address;
1501 uint32_t size;
1502 char *separator;
1503 int retval;
1505 LOG_DEBUG("-");
1507 type = strtoul(packet + 1, &separator, 16);
1509 if (type == 0) /* memory breakpoint */
1510 bp_type = BKPT_SOFT;
1511 else if (type == 1) /* hardware breakpoint */
1512 bp_type = BKPT_HARD;
1513 else if (type == 2) /* write watchpoint */
1514 wp_type = WPT_WRITE;
1515 else if (type == 3) /* read watchpoint */
1516 wp_type = WPT_READ;
1517 else if (type == 4) /* access watchpoint */
1518 wp_type = WPT_ACCESS;
1519 else
1521 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1522 return ERROR_SERVER_REMOTE_CLOSED;
1526 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT)||(bp_type == BKPT_HARD)))
1528 bp_type = gdb_breakpoint_override_type;
1531 if (*separator != ',')
1533 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1534 return ERROR_SERVER_REMOTE_CLOSED;
1537 address = strtoul(separator + 1, &separator, 16);
1539 if (*separator != ',')
1541 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1542 return ERROR_SERVER_REMOTE_CLOSED;
1545 size = strtoul(separator + 1, &separator, 16);
1547 switch (type)
1549 case 0:
1550 case 1:
1551 if (packet[0] == 'Z')
1553 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1555 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1556 return retval;
1558 else
1560 gdb_put_packet(connection, "OK", 2);
1563 else
1565 breakpoint_remove(target, address);
1566 gdb_put_packet(connection, "OK", 2);
1568 break;
1569 case 2:
1570 case 3:
1571 case 4:
1573 if (packet[0] == 'Z')
1575 if ((retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu)) != ERROR_OK)
1577 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1578 return retval;
1580 else
1582 gdb_put_packet(connection, "OK", 2);
1585 else
1587 watchpoint_remove(target, address);
1588 gdb_put_packet(connection, "OK", 2);
1590 break;
1592 default:
1593 break;
1596 return ERROR_OK;
1599 /* print out a string and allocate more space as needed,
1600 * mainly used for XML at this point
1602 static void xml_printf(int *retval, char **xml, int *pos, int *size,
1603 const char *fmt, ...)
1605 if (*retval != ERROR_OK)
1607 return;
1609 int first = 1;
1611 for (;;)
1613 if ((*xml == NULL) || (!first))
1615 /* start by 0 to exercise all the code paths.
1616 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1618 *size = *size * 2 + 2;
1619 char *t = *xml;
1620 *xml = realloc(*xml, *size);
1621 if (*xml == NULL)
1623 if (t)
1624 free(t);
1625 *retval = ERROR_SERVER_REMOTE_CLOSED;
1626 return;
1630 va_list ap;
1631 int ret;
1632 va_start(ap, fmt);
1633 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1634 va_end(ap);
1635 if ((ret > 0) && ((ret + 1) < *size - *pos))
1637 *pos += ret;
1638 return;
1640 /* there was just enough or not enough space, allocate more. */
1641 first = 0;
1645 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1647 char *separator;
1649 /* Extract and NUL-terminate the annex. */
1650 *annex = buf;
1651 while (*buf && *buf != ':')
1652 buf++;
1653 if (*buf == '\0')
1654 return -1;
1655 *buf++ = 0;
1657 /* After the read marker and annex, qXfer looks like a
1658 * traditional 'm' packet. */
1660 *ofs = strtoul(buf, &separator, 16);
1662 if (*separator != ',')
1663 return -1;
1665 *len = strtoul(separator + 1, NULL, 16);
1667 return 0;
1670 static int compare_bank (const void * a, const void * b)
1672 struct flash_bank *b1, *b2;
1673 b1=*((struct flash_bank **)a);
1674 b2=*((struct flash_bank **)b);
1676 if (b1->base == b2->base)
1678 return 0;
1679 } else if (b1->base > b2->base)
1681 return 1;
1682 } else
1684 return -1;
1688 static int gdb_memory_map(struct connection *connection,
1689 char *packet, int packet_size)
1691 /* We get away with only specifying flash here. Regions that are not
1692 * specified are treated as if we provided no memory map(if not we
1693 * could detect the holes and mark them as RAM).
1694 * Normally we only execute this code once, but no big deal if we
1695 * have to regenerate it a couple of times.
1698 struct target *target = get_target_from_connection(connection);
1699 struct flash_bank *p;
1700 char *xml = NULL;
1701 int size = 0;
1702 int pos = 0;
1703 int retval = ERROR_OK;
1704 struct flash_bank **banks;
1705 int offset;
1706 int length;
1707 char *separator;
1708 uint32_t ram_start = 0;
1709 int i;
1710 int target_flash_banks = 0;
1712 /* skip command character */
1713 packet += 23;
1715 offset = strtoul(packet, &separator, 16);
1716 length = strtoul(separator + 1, &separator, 16);
1718 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1720 /* Sort banks in ascending order. We need to report non-flash
1721 * memory as ram (or rather read/write) by default for GDB, since
1722 * it has no concept of non-cacheable read/write memory (i/o etc).
1724 * FIXME Most non-flash addresses are *NOT* RAM! Don't lie.
1725 * Current versions of GDB assume unlisted addresses are RAM...
1727 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1729 for (i = 0; i < flash_get_bank_count(); i++) {
1730 retval = get_flash_bank_by_num(i, &p);
1731 if (retval != ERROR_OK)
1733 free(banks);
1734 gdb_error(connection, retval);
1735 return retval;
1737 if(p->target == target)
1738 banks[target_flash_banks++] = p;
1741 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1742 compare_bank);
1744 for (i = 0; i < target_flash_banks; i++) {
1745 int j;
1746 unsigned sector_size = 0;
1747 uint32_t start;
1749 p = banks[i];
1750 start = p->base;
1752 if (ram_start < p->base)
1753 xml_printf(&retval, &xml, &pos, &size,
1754 "<memory type=\"ram\" start=\"0x%x\" "
1755 "length=\"0x%x\"/>\n",
1756 ram_start, p->base - ram_start);
1758 /* Report adjacent groups of same-size sectors. So for
1759 * example top boot CFI flash will list an initial region
1760 * with several large sectors (maybe 128KB) and several
1761 * smaller ones at the end (maybe 32KB). STR7 will have
1762 * regions with 8KB, 32KB, and 64KB sectors; etc.
1764 for (j = 0; j < p->num_sectors; j++) {
1765 unsigned group_len;
1767 /* Maybe start a new group of sectors. */
1768 if (sector_size == 0) {
1769 start = p->base + p->sectors[j].offset;
1770 xml_printf(&retval, &xml, &pos, &size,
1771 "<memory type=\"flash\" "
1772 "start=\"0x%x\" ",
1773 start);
1774 sector_size = p->sectors[j].size;
1777 /* Does this finish a group of sectors?
1778 * If not, continue an already-started group.
1780 if (j == p->num_sectors -1)
1781 group_len = (p->base + p->size) - start;
1782 else if (p->sectors[j + 1].size != sector_size)
1783 group_len = p->base + p->sectors[j + 1].offset
1784 - start;
1785 else
1786 continue;
1788 xml_printf(&retval, &xml, &pos, &size,
1789 "length=\"0x%x\">\n"
1790 "<property name=\"blocksize\">"
1791 "0x%x</property>\n"
1792 "</memory>\n",
1793 group_len,
1794 sector_size);
1795 sector_size = 0;
1798 ram_start = p->base + p->size;
1801 if (ram_start != 0)
1802 xml_printf(&retval, &xml, &pos, &size,
1803 "<memory type=\"ram\" start=\"0x%x\" "
1804 "length=\"0x%x\"/>\n",
1805 ram_start, 0-ram_start);
1806 /* ELSE a flash chip could be at the very end of the 32 bit address
1807 * space, in which case ram_start will be precisely 0
1810 free(banks);
1811 banks = NULL;
1813 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1815 if (retval != ERROR_OK) {
1816 gdb_error(connection, retval);
1817 return retval;
1820 if (offset + length > pos)
1821 length = pos - offset;
1823 char *t = malloc(length + 1);
1824 t[0] = 'l';
1825 memcpy(t + 1, xml + offset, length);
1826 gdb_put_packet(connection, t, length + 1);
1828 free(t);
1829 free(xml);
1830 return ERROR_OK;
1833 static int gdb_query_packet(struct connection *connection,
1834 char *packet, int packet_size)
1836 struct command_context *cmd_ctx = connection->cmd_ctx;
1837 struct gdb_connection *gdb_connection = connection->priv;
1838 struct target *target = get_target_from_connection(connection);
1840 if (strstr(packet, "qRcmd,"))
1842 if (packet_size > 6)
1844 char *cmd;
1845 int i;
1846 cmd = malloc((packet_size - 6)/2 + 1);
1847 for (i = 0; i < (packet_size - 6)/2; i++)
1849 uint32_t tmp;
1850 sscanf(packet + 6 + 2*i, "%2" SCNx32 , &tmp);
1851 cmd[i] = tmp;
1853 cmd[(packet_size - 6)/2] = 0x0;
1855 /* We want to print all debug output to GDB connection */
1856 log_add_callback(gdb_log_callback, connection);
1857 target_call_timer_callbacks_now();
1858 /* some commands need to know the GDB connection, make note of current
1859 * GDB connection. */
1860 current_gdb_connection = gdb_connection;
1861 command_run_line(cmd_ctx, cmd);
1862 current_gdb_connection = NULL;
1863 target_call_timer_callbacks_now();
1864 log_remove_callback(gdb_log_callback, connection);
1865 free(cmd);
1867 gdb_put_packet(connection, "OK", 2);
1868 return ERROR_OK;
1870 else if (strstr(packet, "qCRC:"))
1872 if (packet_size > 5)
1874 int retval;
1875 char gdb_reply[10];
1876 char *separator;
1877 uint32_t checksum;
1878 uint32_t addr = 0;
1879 uint32_t len = 0;
1881 /* skip command character */
1882 packet += 5;
1884 addr = strtoul(packet, &separator, 16);
1886 if (*separator != ',')
1888 LOG_ERROR("incomplete read memory packet received, dropping connection");
1889 return ERROR_SERVER_REMOTE_CLOSED;
1892 len = strtoul(separator + 1, NULL, 16);
1894 retval = target_checksum_memory(target, addr, len, &checksum);
1896 if (retval == ERROR_OK)
1898 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
1899 gdb_put_packet(connection, gdb_reply, 9);
1901 else
1903 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1904 return retval;
1907 return ERROR_OK;
1910 else if (strstr(packet, "qSupported"))
1912 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1913 * disable qXfer:features:read for the moment */
1914 int retval = ERROR_OK;
1915 char *buffer = NULL;
1916 int pos = 0;
1917 int size = 0;
1919 xml_printf(&retval, &buffer, &pos, &size,
1920 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1921 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1923 if (retval != ERROR_OK)
1925 gdb_send_error(connection, 01);
1926 return ERROR_OK;
1929 gdb_put_packet(connection, buffer, strlen(buffer));
1930 free(buffer);
1932 return ERROR_OK;
1934 else if (strstr(packet, "qXfer:memory-map:read::")
1935 && (flash_get_bank_count() > 0))
1936 return gdb_memory_map(connection, packet, packet_size);
1937 else if (strstr(packet, "qXfer:features:read:"))
1939 char *xml = NULL;
1940 int size = 0;
1941 int pos = 0;
1942 int retval = ERROR_OK;
1944 int offset;
1945 unsigned int length;
1946 char *annex;
1948 /* skip command character */
1949 packet += 20;
1951 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1953 gdb_send_error(connection, 01);
1954 return ERROR_OK;
1957 if (strcmp(annex, "target.xml") != 0)
1959 gdb_send_error(connection, 01);
1960 return ERROR_OK;
1963 xml_printf(&retval, &xml, &pos, &size, \
1964 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1966 if (retval != ERROR_OK)
1968 gdb_error(connection, retval);
1969 return retval;
1972 gdb_put_packet(connection, xml, strlen(xml));
1974 free(xml);
1975 return ERROR_OK;
1977 else if (strstr(packet, "QStartNoAckMode"))
1979 gdb_connection->noack_mode = 1;
1980 gdb_put_packet(connection, "OK", 2);
1981 return ERROR_OK;
1984 gdb_put_packet(connection, "", 0);
1985 return ERROR_OK;
1988 static int gdb_v_packet(struct connection *connection,
1989 char *packet, int packet_size)
1991 struct gdb_connection *gdb_connection = connection->priv;
1992 struct gdb_service *gdb_service = connection->service->priv;
1993 int result;
1995 /* if flash programming disabled - send a empty reply */
1997 if (gdb_flash_program == 0)
1999 gdb_put_packet(connection, "", 0);
2000 return ERROR_OK;
2003 if (strstr(packet, "vFlashErase:"))
2005 unsigned long addr;
2006 unsigned long length;
2008 char *parse = packet + 12;
2009 if (*parse == '\0')
2011 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2012 return ERROR_SERVER_REMOTE_CLOSED;
2015 addr = strtoul(parse, &parse, 16);
2017 if (*(parse++) != ',' || *parse == '\0')
2019 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2020 return ERROR_SERVER_REMOTE_CLOSED;
2023 length = strtoul(parse, &parse, 16);
2025 if (*parse != '\0')
2027 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2028 return ERROR_SERVER_REMOTE_CLOSED;
2031 /* assume all sectors need erasing - stops any problems
2032 * when flash_write is called multiple times */
2033 flash_set_dirty();
2035 /* perform any target specific operations before the erase */
2036 target_call_event_callbacks(gdb_service->target,
2037 TARGET_EVENT_GDB_FLASH_ERASE_START);
2039 /* vFlashErase:addr,length messages require region start and
2040 * end to be "block" aligned ... if padding is ever needed,
2041 * GDB will have become dangerously confused.
2043 result = flash_erase_address_range(gdb_service->target,
2044 false, addr, length);
2046 /* perform any target specific operations after the erase */
2047 target_call_event_callbacks(gdb_service->target,
2048 TARGET_EVENT_GDB_FLASH_ERASE_END);
2050 /* perform erase */
2051 if (result != ERROR_OK)
2053 /* GDB doesn't evaluate the actual error number returned,
2054 * treat a failed erase as an I/O error
2056 gdb_send_error(connection, EIO);
2057 LOG_ERROR("flash_erase returned %i", result);
2059 else
2060 gdb_put_packet(connection, "OK", 2);
2062 return ERROR_OK;
2065 if (strstr(packet, "vFlashWrite:"))
2067 int retval;
2068 unsigned long addr;
2069 unsigned long length;
2070 char *parse = packet + 12;
2072 if (*parse == '\0')
2074 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2075 return ERROR_SERVER_REMOTE_CLOSED;
2077 addr = strtoul(parse, &parse, 16);
2078 if (*(parse++) != ':')
2080 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2081 return ERROR_SERVER_REMOTE_CLOSED;
2083 length = packet_size - (parse - packet);
2085 /* create a new image if there isn't already one */
2086 if (gdb_connection->vflash_image == NULL)
2088 gdb_connection->vflash_image = malloc(sizeof(struct image));
2089 image_open(gdb_connection->vflash_image, "", "build");
2092 /* create new section with content from packet buffer */
2093 if ((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (uint8_t*)parse)) != ERROR_OK)
2095 return retval;
2098 gdb_put_packet(connection, "OK", 2);
2100 return ERROR_OK;
2103 if (!strcmp(packet, "vFlashDone"))
2105 uint32_t written;
2107 /* process the flashing buffer. No need to erase as GDB
2108 * always issues a vFlashErase first. */
2109 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
2110 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
2111 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
2112 if (result != ERROR_OK)
2114 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
2115 gdb_put_packet(connection, "E.memtype", 9);
2116 else
2117 gdb_send_error(connection, EIO);
2119 else
2121 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
2122 gdb_put_packet(connection, "OK", 2);
2125 image_close(gdb_connection->vflash_image);
2126 free(gdb_connection->vflash_image);
2127 gdb_connection->vflash_image = NULL;
2129 return ERROR_OK;
2132 gdb_put_packet(connection, "", 0);
2133 return ERROR_OK;
2136 static int gdb_detach(struct connection *connection)
2138 struct gdb_service *gdb_service = connection->service->priv;
2140 target_call_event_callbacks(gdb_service->target,
2141 TARGET_EVENT_GDB_DETACH);
2143 return gdb_put_packet(connection, "OK", 2);
2146 static void gdb_log_callback(void *priv, const char *file, unsigned line,
2147 const char *function, const char *string)
2149 struct connection *connection = priv;
2150 struct gdb_connection *gdb_con = connection->priv;
2152 if (gdb_con->busy)
2154 /* do not reply this using the O packet */
2155 return;
2158 gdb_output_con(connection, string);
2161 static void gdb_sig_halted(struct connection *connection)
2163 char sig_reply[4];
2164 snprintf(sig_reply, 4, "T%2.2x", 2);
2165 gdb_put_packet(connection, sig_reply, 3);
2169 static int gdb_input_inner(struct connection *connection)
2171 /* Do not allocate this on the stack */
2172 static char gdb_packet_buffer[GDB_BUFFER_SIZE];
2174 struct gdb_service *gdb_service = connection->service->priv;
2175 struct target *target = gdb_service->target;
2176 char *packet = gdb_packet_buffer;
2177 int packet_size;
2178 int retval;
2179 struct gdb_connection *gdb_con = connection->priv;
2180 static int extended_protocol = 0;
2182 /* drain input buffer. If one of the packets fail, then an error
2183 * packet is replied, if applicable.
2185 * This loop will terminate and the error code is returned.
2187 * The calling fn will check if this error is something that
2188 * can be recovered from, or if the connection must be closed.
2190 * If the error is recoverable, this fn is called again to
2191 * drain the rest of the buffer.
2195 packet_size = GDB_BUFFER_SIZE-1;
2196 retval = gdb_get_packet(connection, packet, &packet_size);
2197 if (retval != ERROR_OK)
2198 return retval;
2200 /* terminate with zero */
2201 packet[packet_size] = 0;
2203 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
2204 if (packet[0] == 'X') {
2205 // binary packets spew junk into the debug log stream
2206 char buf[ 50 ];
2207 int x;
2208 for (x = 0 ; (x < 49) && (packet[x] != ':') ; x++) {
2209 buf[x] = packet[x];
2211 buf[x] = 0;
2212 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
2213 } else {
2214 LOG_DEBUG("received packet: '%s'", packet);
2218 if (packet_size > 0)
2220 retval = ERROR_OK;
2221 switch (packet[0])
2223 case 'T': // Is thread alive?
2224 gdb_thread_packet(connection, packet, packet_size);
2225 break;
2226 case 'H': // Set current thread ( 'c' for step and continue, 'g' for all other operations )
2227 gdb_thread_packet(connection, packet, packet_size);
2228 break;
2229 case 'q':
2230 case 'Q':
2231 retval = gdb_thread_packet(connection, packet, packet_size);
2232 if ( retval == GDB_THREAD_PACKET_NOT_CONSUMED )
2234 retval = gdb_query_packet(connection, packet, packet_size);
2236 break;
2237 case 'g':
2238 retval = gdb_get_registers_packet(connection, packet, packet_size);
2239 break;
2240 case 'G':
2241 retval = gdb_set_registers_packet(connection, packet, packet_size);
2242 break;
2243 case 'p':
2244 retval = gdb_get_register_packet(connection, packet, packet_size);
2245 break;
2246 case 'P':
2247 retval = gdb_set_register_packet(connection, packet, packet_size);
2248 break;
2249 case 'm':
2250 retval = gdb_read_memory_packet(connection, packet, packet_size);
2251 break;
2252 case 'M':
2253 retval = gdb_write_memory_packet(connection, packet, packet_size);
2254 break;
2255 case 'z':
2256 case 'Z':
2257 retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
2258 break;
2259 case '?':
2260 gdb_last_signal_packet(connection, packet, packet_size);
2261 break;
2262 case 'c':
2263 case 's':
2265 log_add_callback(gdb_log_callback, connection);
2267 if (gdb_con->mem_write_error)
2269 LOG_ERROR("Memory write failure!");
2271 /* now that we have reported the memory write error, we can clear the condition */
2272 gdb_con->mem_write_error = false;
2275 bool nostep = false;
2276 bool already_running = false;
2277 if (target->state == TARGET_RUNNING)
2279 LOG_WARNING("WARNING! The target is already running. "
2280 "All changes GDB did to registers will be discarded! "
2281 "Waiting for target to halt.");
2282 already_running = true;
2283 } else if (target->state != TARGET_HALTED)
2285 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2286 nostep = true;
2287 } else if ((packet[0] == 's') && gdb_con->sync)
2289 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2290 * sent by GDB first to OpenOCD, thus defeating the check to
2291 * make only the single stepping have the sync feature...
2293 nostep = true;
2294 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2296 gdb_con->sync = false;
2298 if (!already_running && nostep)
2300 /* Either the target isn't in the halted state, then we can't
2301 * step/continue. This might be early setup, etc.
2303 * Or we want to allow GDB to pick up a fresh set of
2304 * register values without modifying the target state.
2307 gdb_sig_halted(connection);
2309 /* stop forwarding log packets! */
2310 log_remove_callback(gdb_log_callback, connection);
2311 } else
2313 /* We're running/stepping, in which case we can
2314 * forward log output until the target is halted
2316 gdb_con->frontend_state = TARGET_RUNNING;
2317 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2319 if (!already_running)
2321 /* Here we don't want packet processing to stop even if this fails,
2322 * so we use a local variable instead of retval. */
2323 retval = gdb_step_continue_packet(connection, packet, packet_size);
2324 if (retval != ERROR_OK)
2326 /* we'll never receive a halted condition... issue a false one.. */
2327 gdb_frontend_halted(target, connection);
2332 break;
2333 case 'v':
2334 retval = gdb_v_packet(connection, packet, packet_size);
2335 break;
2336 case 'D':
2337 retval = gdb_detach(connection);
2338 extended_protocol = 0;
2339 break;
2340 case 'X':
2341 retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
2342 if (retval != ERROR_OK)
2343 return retval;
2344 break;
2345 case 'k':
2346 if (extended_protocol != 0)
2347 break;
2348 gdb_put_packet(connection, "OK", 2);
2349 return ERROR_SERVER_REMOTE_CLOSED;
2350 case '!':
2351 /* handle extended remote protocol */
2352 extended_protocol = 1;
2353 gdb_put_packet(connection, "OK", 2);
2354 break;
2355 case 'R':
2356 /* handle extended restart packet */
2357 breakpoint_clear_target(gdb_service->target);
2358 watchpoint_clear_target(gdb_service->target);
2359 command_run_linef(connection->cmd_ctx,
2360 "ocd_gdb_restart %s",
2361 target_name(target));
2362 gdb_put_packet(connection, "OK", 2);
2363 break;
2365 case 'j':
2366 /* packet supported only by smp target i.e cortex_a.c*/
2367 /* handle smp packet replying coreid played to gbd */
2368 gdb_read_smp_packet(connection, packet, packet_size);
2369 break;
2371 case 'J':
2372 /* packet supported only by smp target i.e cortex_a.c */
2373 /* handle smp packet setting coreid to be played at next
2374 * resume to gdb */
2375 gdb_write_smp_packet(connection, packet, packet_size);
2376 break;
2378 default:
2379 /* ignore unknown packets */
2380 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2381 gdb_put_packet(connection, NULL, 0);
2382 break;
2385 /* if a packet handler returned an error, exit input loop */
2386 if (retval != ERROR_OK)
2387 return retval;
2390 if (gdb_con->ctrl_c)
2392 if (target->state == TARGET_RUNNING)
2394 retval = target_halt(target);
2395 if (retval != ERROR_OK)
2397 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2399 gdb_con->ctrl_c = 0;
2400 } else
2402 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2403 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2407 } while (gdb_con->buf_cnt > 0);
2409 return ERROR_OK;
2412 static int gdb_input(struct connection *connection)
2414 int retval = gdb_input_inner(connection);
2415 struct gdb_connection *gdb_con = connection->priv;
2416 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2417 return retval;
2419 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2420 if (gdb_con->closed)
2421 return ERROR_SERVER_REMOTE_CLOSED;
2423 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2424 return ERROR_OK;
2427 static int gdb_target_start(struct target *target, const char *port)
2430 struct gdb_service *gdb_service;
2431 int ret;
2432 gdb_service = malloc(sizeof(struct gdb_service));
2434 if (NULL == gdb_service)
2435 return -ENOMEM;
2437 gdb_service->target = target;
2438 gdb_service->core[0] = -1;
2439 gdb_service->core[1] = -1;
2440 target->gdb_service = gdb_service;
2442 ret = add_service("gdb",
2443 port, 1, &gdb_new_connection, &gdb_input,
2444 &gdb_connection_closed, gdb_service);
2445 /* initialialize all targets gdb service with the same pointer */
2447 struct target_list *head;
2448 struct target *curr;
2449 head = target->head;
2450 while(head != (struct target_list*)NULL)
2452 curr = head->target;
2453 if (curr != target) curr->gdb_service = gdb_service;
2454 head = head->next;
2457 return ret;
2460 static int gdb_target_add_one(struct target *target)
2462 /* one gdb instance per smp list */
2463 if ((target->smp) && (target->gdb_service)) return ERROR_OK;
2464 int retval = gdb_target_start(target, gdb_port_next);
2465 if (retval == ERROR_OK)
2467 long portnumber;
2468 /* If we can parse the port number
2469 * then we increment the port number for the next target.
2471 char *end;
2472 portnumber = strtol(gdb_port_next, &end, 0);
2473 if (!*end)
2475 if (parse_long(gdb_port_next, &portnumber) == ERROR_OK)
2477 free((void *)gdb_port_next);
2478 gdb_port_next = alloc_printf("%d", portnumber+1);
2482 return retval;
2485 int gdb_target_add_all(struct target *target)
2487 if (NULL == target)
2489 LOG_WARNING("gdb services need one or more targets defined");
2490 return ERROR_OK;
2493 while (NULL != target)
2495 int retval = gdb_target_add_one(target);
2496 if (ERROR_OK != retval)
2497 return retval;
2499 target = target->next;
2502 return ERROR_OK;
2505 COMMAND_HANDLER(handle_gdb_sync_command)
2507 if (CMD_ARGC != 0)
2509 return ERROR_COMMAND_SYNTAX_ERROR;
2512 if (current_gdb_connection == NULL)
2514 command_print(CMD_CTX,
2515 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2516 return ERROR_FAIL;
2519 current_gdb_connection->sync = true;
2521 return ERROR_OK;
2524 /* daemon configuration command gdb_port */
2525 COMMAND_HANDLER(handle_gdb_port_command)
2527 int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
2528 if (ERROR_OK == retval) {
2529 free((void*)gdb_port_next);
2530 gdb_port_next = strdup(gdb_port);
2532 return retval;
2535 COMMAND_HANDLER(handle_gdb_memory_map_command)
2537 if (CMD_ARGC != 1)
2538 return ERROR_COMMAND_SYNTAX_ERROR;
2540 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
2541 return ERROR_OK;
2544 COMMAND_HANDLER(handle_gdb_flash_program_command)
2546 if (CMD_ARGC != 1)
2547 return ERROR_COMMAND_SYNTAX_ERROR;
2549 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
2550 return ERROR_OK;
2553 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
2555 if (CMD_ARGC != 1)
2556 return ERROR_COMMAND_SYNTAX_ERROR;
2558 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
2559 return ERROR_OK;
2562 /* gdb_breakpoint_override */
2563 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
2565 if (CMD_ARGC == 0)
2568 } else if (CMD_ARGC == 1)
2570 gdb_breakpoint_override = 1;
2571 if (strcmp(CMD_ARGV[0], "hard") == 0)
2573 gdb_breakpoint_override_type = BKPT_HARD;
2574 } else if (strcmp(CMD_ARGV[0], "soft") == 0)
2576 gdb_breakpoint_override_type = BKPT_SOFT;
2577 } else if (strcmp(CMD_ARGV[0], "disable") == 0)
2579 gdb_breakpoint_override = 0;
2581 } else
2583 return ERROR_COMMAND_SYNTAX_ERROR;
2585 if (gdb_breakpoint_override)
2587 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type == BKPT_HARD)?"hard":"soft");
2588 } else
2590 LOG_USER("breakpoint type is not overridden");
2593 return ERROR_OK;
2596 static const struct command_registration gdb_command_handlers[] = {
2598 .name = "gdb_sync",
2599 .handler = handle_gdb_sync_command,
2600 .mode = COMMAND_ANY,
2601 .help = "next stepi will return immediately allowing "
2602 "GDB to fetch register state without affecting "
2603 "target state",
2606 .name = "gdb_port",
2607 .handler = handle_gdb_port_command,
2608 .mode = COMMAND_ANY,
2609 .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
2610 "server listens for the next port number after the "
2611 "base port number specified. "
2612 "No arguments reports GDB port. \"pipe\" means listen to stdin "
2613 "output to stdout, an integer is base port number, \"disable\" disables "
2614 "port. Any other string is are interpreted as named pipe to listen to. "
2615 "Output pipe is the same name as input pipe, but with 'o' appended.",
2616 .usage = "[port_num]",
2619 .name = "gdb_memory_map",
2620 .handler = handle_gdb_memory_map_command,
2621 .mode = COMMAND_CONFIG,
2622 .help = "enable or disable memory map",
2623 .usage = "('enable'|'disable')"
2626 .name = "gdb_flash_program",
2627 .handler = handle_gdb_flash_program_command,
2628 .mode = COMMAND_CONFIG,
2629 .help = "enable or disable flash program",
2630 .usage = "('enable'|'disable')"
2633 .name = "gdb_report_data_abort",
2634 .handler = handle_gdb_report_data_abort_command,
2635 .mode = COMMAND_CONFIG,
2636 .help = "enable or disable reporting data aborts",
2637 .usage = "('enable'|'disable')"
2640 .name = "gdb_breakpoint_override",
2641 .handler = handle_gdb_breakpoint_override_command,
2642 .mode = COMMAND_ANY,
2643 .help = "Display or specify type of breakpoint "
2644 "to be used by gdb 'break' commands.",
2645 .usage = "('hard'|'soft'|'disable')"
2647 COMMAND_REGISTRATION_DONE
2650 int gdb_register_commands(struct command_context *cmd_ctx)
2652 gdb_port = strdup("3333");
2653 gdb_port_next = strdup("3333");
2654 return register_commands(cmd_ctx, NULL, gdb_command_handlers);