69afb2fd4e95b9d8483722d4cbbb73d103b931ff
[openocd.git] / src / server / gdb_server.c
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 * Copyright (C) 2013 Andes Technology *
18 * Hsiangkai Wang <hkwang@andestech.com> *
19 * *
20 * Copyright (C) 2013 Franck Jullien *
21 * elec4fun@gmail.com *
22 * *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
27 * *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
32 * *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
35 ***************************************************************************/
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <target/breakpoints.h>
42 #include <target/target_request.h>
43 #include <target/register.h>
44 #include <target/target.h>
45 #include <target/target_type.h>
46 #include "server.h"
47 #include <flash/nor/core.h>
48 #include "gdb_server.h"
49 #include <target/image.h>
50 #include <jtag/jtag.h>
51 #include "rtos/rtos.h"
52 #include "target/smp.h"
53
54 /**
55 * @file
56 * GDB server implementation.
57 *
58 * This implements the GDB Remote Serial Protocol, over TCP connections,
59 * giving GDB access to the JTAG or other hardware debugging facilities
60 * found in most modern embedded processors.
61 */
62
63 struct target_desc_format {
64 char *tdesc;
65 uint32_t tdesc_length;
66 };
67
68 /* private connection data for GDB */
69 struct gdb_connection {
70 char buffer[GDB_BUFFER_SIZE];
71 char *buf_p;
72 int buf_cnt;
73 int ctrl_c;
74 enum target_state frontend_state;
75 struct image *vflash_image;
76 bool closed;
77 bool busy;
78 int noack_mode;
79 /* set flag to true if you want the next stepi to return immediately.
80 * allowing GDB to pick up a fresh set of register values from the target
81 * without modifying the target state. */
82 bool sync;
83 /* We delay reporting memory write errors until next step/continue or memory
84 * write. This improves performance of gdb load significantly as the GDB packet
85 * can be replied immediately and a new GDB packet will be ready without delay
86 * (ca. 10% or so...). */
87 bool mem_write_error;
88 /* with extended-remote it seems we need to better emulate attach/detach.
89 * what this means is we reply with a W stop reply after a kill packet,
90 * normally we reply with a S reply via gdb_last_signal_packet.
91 * as a side note this behaviour only effects gdb > 6.8 */
92 bool attached;
93 /* temporarily used for target description support */
94 struct target_desc_format target_desc;
95 /* temporarily used for thread list support */
96 char *thread_list;
97 };
98
99 #if 0
100 #define _DEBUG_GDB_IO_
101 #endif
102
103 static struct gdb_connection *current_gdb_connection;
104
105 static int gdb_breakpoint_override;
106 static enum breakpoint_type gdb_breakpoint_override_type;
107
108 static int gdb_error(struct connection *connection, int retval);
109 static char *gdb_port;
110 static char *gdb_port_next;
111
112 static void gdb_log_callback(void *priv, const char *file, unsigned line,
113 const char *function, const char *string);
114
115 static void gdb_sig_halted(struct connection *connection);
116
117 /* number of gdb connections, mainly to suppress gdb related debugging spam
118 * in helper/log.c when no gdb connections are actually active */
119 int gdb_actual_connections;
120
121 /* set if we are sending a memory map to gdb
122 * via qXfer:memory-map:read packet */
123 /* enabled by default*/
124 static int gdb_use_memory_map = 1;
125 /* enabled by default*/
126 static int gdb_flash_program = 1;
127
128 /* if set, data aborts cause an error to be reported in memory read packets
129 * see the code in gdb_read_memory_packet() for further explanations.
130 * Disabled by default.
131 */
132 static int gdb_report_data_abort;
133 /* If set, errors when accessing registers are reported to gdb. Disabled by
134 * default. */
135 static int gdb_report_register_access_error;
136
137 /* set if we are sending target descriptions to gdb
138 * via qXfer:features:read packet */
139 /* enabled by default */
140 static int gdb_use_target_description = 1;
141
142 /* current processing free-run type, used by file-I/O */
143 static char gdb_running_type;
144
145 static int gdb_last_signal(struct target *target)
146 {
147 switch (target->debug_reason) {
148 case DBG_REASON_DBGRQ:
149 return 0x2; /* SIGINT */
150 case DBG_REASON_BREAKPOINT:
151 case DBG_REASON_WATCHPOINT:
152 case DBG_REASON_WPTANDBKPT:
153 return 0x05; /* SIGTRAP */
154 case DBG_REASON_SINGLESTEP:
155 return 0x05; /* SIGTRAP */
156 case DBG_REASON_NOTHALTED:
157 return 0x0; /* no signal... shouldn't happen */
158 default:
159 LOG_USER("undefined debug reason %d - target needs reset",
160 target->debug_reason);
161 return 0x0;
162 }
163 }
164
165 static int check_pending(struct connection *connection,
166 int timeout_s, int *got_data)
167 {
168 /* a non-blocking socket will block if there is 0 bytes available on the socket,
169 * but return with as many bytes as are available immediately
170 */
171 struct timeval tv;
172 fd_set read_fds;
173 struct gdb_connection *gdb_con = connection->priv;
174 int t;
175 if (got_data == NULL)
176 got_data = &t;
177 *got_data = 0;
178
179 if (gdb_con->buf_cnt > 0) {
180 *got_data = 1;
181 return ERROR_OK;
182 }
183
184 FD_ZERO(&read_fds);
185 FD_SET(connection->fd, &read_fds);
186
187 tv.tv_sec = timeout_s;
188 tv.tv_usec = 0;
189 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0) {
190 /* This can typically be because a "monitor" command took too long
191 * before printing any progress messages
192 */
193 if (timeout_s > 0)
194 return ERROR_GDB_TIMEOUT;
195 else
196 return ERROR_OK;
197 }
198 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
199 return ERROR_OK;
200 }
201
202 static int gdb_get_char_inner(struct connection *connection, int *next_char)
203 {
204 struct gdb_connection *gdb_con = connection->priv;
205 int retval = ERROR_OK;
206
207 #ifdef _DEBUG_GDB_IO_
208 char *debug_buffer;
209 #endif
210 for (;; ) {
211 if (connection->service->type != CONNECTION_TCP)
212 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
213 else {
214 retval = check_pending(connection, 1, NULL);
215 if (retval != ERROR_OK)
216 return retval;
217 gdb_con->buf_cnt = read_socket(connection->fd,
218 gdb_con->buffer,
219 GDB_BUFFER_SIZE);
220 }
221
222 if (gdb_con->buf_cnt > 0)
223 break;
224 if (gdb_con->buf_cnt == 0) {
225 gdb_con->closed = true;
226 return ERROR_SERVER_REMOTE_CLOSED;
227 }
228
229 #ifdef _WIN32
230 errno = WSAGetLastError();
231
232 switch (errno) {
233 case WSAEWOULDBLOCK:
234 usleep(1000);
235 break;
236 case WSAECONNABORTED:
237 gdb_con->closed = true;
238 return ERROR_SERVER_REMOTE_CLOSED;
239 case WSAECONNRESET:
240 gdb_con->closed = true;
241 return ERROR_SERVER_REMOTE_CLOSED;
242 default:
243 LOG_ERROR("read: %d", errno);
244 exit(-1);
245 }
246 #else
247 switch (errno) {
248 case EAGAIN:
249 usleep(1000);
250 break;
251 case ECONNABORTED:
252 gdb_con->closed = true;
253 return ERROR_SERVER_REMOTE_CLOSED;
254 case ECONNRESET:
255 gdb_con->closed = true;
256 return ERROR_SERVER_REMOTE_CLOSED;
257 default:
258 LOG_ERROR("read: %s", strerror(errno));
259 gdb_con->closed = true;
260 return ERROR_SERVER_REMOTE_CLOSED;
261 }
262 #endif
263 }
264
265 #ifdef _DEBUG_GDB_IO_
266 debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
267 LOG_DEBUG("received '%s'", debug_buffer);
268 free(debug_buffer);
269 #endif
270
271 gdb_con->buf_p = gdb_con->buffer;
272 gdb_con->buf_cnt--;
273 *next_char = *(gdb_con->buf_p++);
274 if (gdb_con->buf_cnt > 0)
275 connection->input_pending = 1;
276 else
277 connection->input_pending = 0;
278 #ifdef _DEBUG_GDB_IO_
279 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
280 #endif
281
282 return retval;
283 }
284
285 /**
286 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
287 * held in registers in the inner loop.
288 *
289 * For small caches and embedded systems this is important!
290 */
291 static inline int gdb_get_char_fast(struct connection *connection,
292 int *next_char, char **buf_p, int *buf_cnt)
293 {
294 int retval = ERROR_OK;
295
296 if ((*buf_cnt)-- > 0) {
297 *next_char = **buf_p;
298 (*buf_p)++;
299 if (*buf_cnt > 0)
300 connection->input_pending = 1;
301 else
302 connection->input_pending = 0;
303
304 #ifdef _DEBUG_GDB_IO_
305 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
306 #endif
307
308 return ERROR_OK;
309 }
310
311 struct gdb_connection *gdb_con = connection->priv;
312 gdb_con->buf_p = *buf_p;
313 gdb_con->buf_cnt = *buf_cnt;
314 retval = gdb_get_char_inner(connection, next_char);
315 *buf_p = gdb_con->buf_p;
316 *buf_cnt = gdb_con->buf_cnt;
317
318 return retval;
319 }
320
321 static int gdb_get_char(struct connection *connection, int *next_char)
322 {
323 struct gdb_connection *gdb_con = connection->priv;
324 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
325 }
326
327 static int gdb_putback_char(struct connection *connection, int last_char)
328 {
329 struct gdb_connection *gdb_con = connection->priv;
330
331 if (gdb_con->buf_p > gdb_con->buffer) {
332 *(--gdb_con->buf_p) = last_char;
333 gdb_con->buf_cnt++;
334 } else
335 LOG_ERROR("BUG: couldn't put character back");
336
337 return ERROR_OK;
338 }
339
340 /* The only way we can detect that the socket is closed is the first time
341 * we write to it, we will fail. Subsequent write operations will
342 * succeed. Shudder! */
343 static int gdb_write(struct connection *connection, void *data, int len)
344 {
345 struct gdb_connection *gdb_con = connection->priv;
346 if (gdb_con->closed)
347 return ERROR_SERVER_REMOTE_CLOSED;
348
349 if (connection_write(connection, data, len) == len)
350 return ERROR_OK;
351 gdb_con->closed = true;
352 return ERROR_SERVER_REMOTE_CLOSED;
353 }
354
355 static int gdb_put_packet_inner(struct connection *connection,
356 char *buffer, int len)
357 {
358 int i;
359 unsigned char my_checksum = 0;
360 #ifdef _DEBUG_GDB_IO_
361 char *debug_buffer;
362 #endif
363 int reply;
364 int retval;
365 struct gdb_connection *gdb_con = connection->priv;
366
367 for (i = 0; i < len; i++)
368 my_checksum += buffer[i];
369
370 #ifdef _DEBUG_GDB_IO_
371 /*
372 * At this point we should have nothing in the input queue from GDB,
373 * however sometimes '-' is sent even though we've already received
374 * an ACK (+) for everything we've sent off.
375 */
376 int gotdata;
377 for (;; ) {
378 retval = check_pending(connection, 0, &gotdata);
379 if (retval != ERROR_OK)
380 return retval;
381 if (!gotdata)
382 break;
383 retval = gdb_get_char(connection, &reply);
384 if (retval != ERROR_OK)
385 return retval;
386 if (reply == '$') {
387 /* fix a problem with some IAR tools */
388 gdb_putback_char(connection, reply);
389 LOG_DEBUG("Unexpected start of new packet");
390 break;
391 }
392
393 LOG_WARNING("Discard unexpected char %c", reply);
394 }
395 #endif
396
397 while (1) {
398 #ifdef _DEBUG_GDB_IO_
399 debug_buffer = strndup(buffer, len);
400 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
401 free(debug_buffer);
402 #endif
403
404 char local_buffer[1024];
405 local_buffer[0] = '$';
406 if ((size_t)len + 4 <= sizeof(local_buffer)) {
407 /* performance gain on smaller packets by only a single call to gdb_write() */
408 memcpy(local_buffer + 1, buffer, len++);
409 len += snprintf(local_buffer + len, sizeof(local_buffer) - len, "#%02x", my_checksum);
410 retval = gdb_write(connection, local_buffer, len);
411 if (retval != ERROR_OK)
412 return retval;
413 } else {
414 /* larger packets are transmitted directly from caller supplied buffer
415 * by several calls to gdb_write() to avoid dynamic allocation */
416 snprintf(local_buffer + 1, sizeof(local_buffer) - 1, "#%02x", my_checksum);
417 retval = gdb_write(connection, local_buffer, 1);
418 if (retval != ERROR_OK)
419 return retval;
420 retval = gdb_write(connection, buffer, len);
421 if (retval != ERROR_OK)
422 return retval;
423 retval = gdb_write(connection, local_buffer + 1, 3);
424 if (retval != ERROR_OK)
425 return retval;
426 }
427
428 if (gdb_con->noack_mode)
429 break;
430
431 retval = gdb_get_char(connection, &reply);
432 if (retval != ERROR_OK)
433 return retval;
434
435 if (reply == '+')
436 break;
437 else if (reply == '-') {
438 /* Stop sending output packets for now */
439 log_remove_callback(gdb_log_callback, connection);
440 LOG_WARNING("negative reply, retrying");
441 } else if (reply == 0x3) {
442 gdb_con->ctrl_c = 1;
443 retval = gdb_get_char(connection, &reply);
444 if (retval != ERROR_OK)
445 return retval;
446 if (reply == '+')
447 break;
448 else if (reply == '-') {
449 /* Stop sending output packets for now */
450 log_remove_callback(gdb_log_callback, connection);
451 LOG_WARNING("negative reply, retrying");
452 } else if (reply == '$') {
453 LOG_ERROR("GDB missing ack(1) - assumed good");
454 gdb_putback_char(connection, reply);
455 return ERROR_OK;
456 } else {
457 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
458 gdb_con->closed = true;
459 return ERROR_SERVER_REMOTE_CLOSED;
460 }
461 } else if (reply == '$') {
462 LOG_ERROR("GDB missing ack(2) - assumed good");
463 gdb_putback_char(connection, reply);
464 return ERROR_OK;
465 } else {
466 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
467 reply);
468 gdb_con->closed = true;
469 return ERROR_SERVER_REMOTE_CLOSED;
470 }
471 }
472 if (gdb_con->closed)
473 return ERROR_SERVER_REMOTE_CLOSED;
474
475 return ERROR_OK;
476 }
477
478 int gdb_put_packet(struct connection *connection, char *buffer, int len)
479 {
480 struct gdb_connection *gdb_con = connection->priv;
481 gdb_con->busy = true;
482 int retval = gdb_put_packet_inner(connection, buffer, len);
483 gdb_con->busy = false;
484
485 /* we sent some data, reset timer for keep alive messages */
486 kept_alive();
487
488 return retval;
489 }
490
491 static inline int fetch_packet(struct connection *connection,
492 int *checksum_ok, int noack, int *len, char *buffer)
493 {
494 unsigned char my_checksum = 0;
495 char checksum[3];
496 int character;
497 int retval = ERROR_OK;
498
499 struct gdb_connection *gdb_con = connection->priv;
500 my_checksum = 0;
501 int count = 0;
502 count = 0;
503
504 /* move this over into local variables to use registers and give the
505 * more freedom to optimize */
506 char *buf_p = gdb_con->buf_p;
507 int buf_cnt = gdb_con->buf_cnt;
508
509 for (;; ) {
510 /* The common case is that we have an entire packet with no escape chars.
511 * We need to leave at least 2 bytes in the buffer to have
512 * gdb_get_char() update various bits and bobs correctly.
513 */
514 if ((buf_cnt > 2) && ((buf_cnt + count) < *len)) {
515 /* The compiler will struggle a bit with constant propagation and
516 * aliasing, so we help it by showing that these values do not
517 * change inside the loop
518 */
519 int i;
520 char *buf = buf_p;
521 int run = buf_cnt - 2;
522 i = 0;
523 int done = 0;
524 while (i < run) {
525 character = *buf++;
526 i++;
527 if (character == '#') {
528 /* Danger! character can be '#' when esc is
529 * used so we need an explicit boolean for done here. */
530 done = 1;
531 break;
532 }
533
534 if (character == '}') {
535 /* data transmitted in binary mode (X packet)
536 * uses 0x7d as escape character */
537 my_checksum += character & 0xff;
538 character = *buf++;
539 i++;
540 my_checksum += character & 0xff;
541 buffer[count++] = (character ^ 0x20) & 0xff;
542 } else {
543 my_checksum += character & 0xff;
544 buffer[count++] = character & 0xff;
545 }
546 }
547 buf_p += i;
548 buf_cnt -= i;
549 if (done)
550 break;
551 }
552 if (count > *len) {
553 LOG_ERROR("packet buffer too small");
554 retval = ERROR_GDB_BUFFER_TOO_SMALL;
555 break;
556 }
557
558 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
559 if (retval != ERROR_OK)
560 break;
561
562 if (character == '#')
563 break;
564
565 if (character == '}') {
566 /* data transmitted in binary mode (X packet)
567 * uses 0x7d as escape character */
568 my_checksum += character & 0xff;
569
570 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
571 if (retval != ERROR_OK)
572 break;
573
574 my_checksum += character & 0xff;
575 buffer[count++] = (character ^ 0x20) & 0xff;
576 } else {
577 my_checksum += character & 0xff;
578 buffer[count++] = character & 0xff;
579 }
580 }
581
582 gdb_con->buf_p = buf_p;
583 gdb_con->buf_cnt = buf_cnt;
584
585 if (retval != ERROR_OK)
586 return retval;
587
588 *len = count;
589
590 retval = gdb_get_char(connection, &character);
591 if (retval != ERROR_OK)
592 return retval;
593 checksum[0] = character;
594 retval = gdb_get_char(connection, &character);
595 if (retval != ERROR_OK)
596 return retval;
597 checksum[1] = character;
598 checksum[2] = 0;
599
600 if (!noack)
601 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
602
603 return ERROR_OK;
604 }
605
606 static int gdb_get_packet_inner(struct connection *connection,
607 char *buffer, int *len)
608 {
609 int character;
610 int retval;
611 struct gdb_connection *gdb_con = connection->priv;
612
613 while (1) {
614 do {
615 retval = gdb_get_char(connection, &character);
616 if (retval != ERROR_OK)
617 return retval;
618
619 #ifdef _DEBUG_GDB_IO_
620 LOG_DEBUG("character: '%c'", character);
621 #endif
622
623 switch (character) {
624 case '$':
625 break;
626 case '+':
627 /* According to the GDB documentation
628 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
629 * "gdb sends a final `+` acknowledgment of the stub's `OK`
630 * response, which can be safely ignored by the stub."
631 * However OpenOCD server already is in noack mode at this
632 * point and instead of ignoring this it was emitting a
633 * warning. This code makes server ignore the first ACK
634 * that will be received after going into noack mode,
635 * warning only about subsequent ACK's. */
636 if (gdb_con->noack_mode > 1) {
637 LOG_WARNING("acknowledgment received, but no packet pending");
638 } else if (gdb_con->noack_mode) {
639 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
640 gdb_con->noack_mode = 2;
641 }
642 break;
643 case '-':
644 LOG_WARNING("negative acknowledgment, but no packet pending");
645 break;
646 case 0x3:
647 gdb_con->ctrl_c = 1;
648 *len = 0;
649 return ERROR_OK;
650 default:
651 LOG_WARNING("ignoring character 0x%x", character);
652 break;
653 }
654 } while (character != '$');
655
656 int checksum_ok = 0;
657 /* explicit code expansion here to get faster inlined code in -O3 by not
658 * calculating checksum */
659 if (gdb_con->noack_mode) {
660 retval = fetch_packet(connection, &checksum_ok, 1, len, buffer);
661 if (retval != ERROR_OK)
662 return retval;
663 } else {
664 retval = fetch_packet(connection, &checksum_ok, 0, len, buffer);
665 if (retval != ERROR_OK)
666 return retval;
667 }
668
669 if (gdb_con->noack_mode) {
670 /* checksum is not checked in noack mode */
671 break;
672 }
673 if (checksum_ok) {
674 retval = gdb_write(connection, "+", 1);
675 if (retval != ERROR_OK)
676 return retval;
677 break;
678 }
679 }
680 if (gdb_con->closed)
681 return ERROR_SERVER_REMOTE_CLOSED;
682
683 return ERROR_OK;
684 }
685
686 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
687 {
688 struct gdb_connection *gdb_con = connection->priv;
689 gdb_con->busy = true;
690 int retval = gdb_get_packet_inner(connection, buffer, len);
691 gdb_con->busy = false;
692 return retval;
693 }
694
695 static int gdb_output_con(struct connection *connection, const char *line)
696 {
697 char *hex_buffer;
698 int bin_size;
699
700 bin_size = strlen(line);
701
702 hex_buffer = malloc(bin_size * 2 + 2);
703 if (hex_buffer == NULL)
704 return ERROR_GDB_BUFFER_TOO_SMALL;
705
706 hex_buffer[0] = 'O';
707 size_t pkt_len = hexify(hex_buffer + 1, (const uint8_t *)line, bin_size,
708 bin_size * 2 + 1);
709 int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1);
710
711 free(hex_buffer);
712 return retval;
713 }
714
715 static int gdb_output(struct command_context *context, const char *line)
716 {
717 /* this will be dumped to the log and also sent as an O packet if possible */
718 LOG_USER_N("%s", line);
719 return ERROR_OK;
720 }
721
722 static void gdb_signal_reply(struct target *target, struct connection *connection)
723 {
724 struct gdb_connection *gdb_connection = connection->priv;
725 char sig_reply[45];
726 char stop_reason[20];
727 char current_thread[25];
728 int sig_reply_len;
729 int signal_var;
730
731 rtos_update_threads(target);
732
733 if (target->debug_reason == DBG_REASON_EXIT) {
734 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "W00");
735 } else {
736 if (gdb_connection->ctrl_c) {
737 signal_var = 0x2;
738 } else
739 signal_var = gdb_last_signal(target);
740
741 stop_reason[0] = '\0';
742 if (target->debug_reason == DBG_REASON_WATCHPOINT) {
743 enum watchpoint_rw hit_wp_type;
744 target_addr_t hit_wp_address;
745
746 if (watchpoint_hit(target, &hit_wp_type, &hit_wp_address) == ERROR_OK) {
747
748 switch (hit_wp_type) {
749 case WPT_WRITE:
750 snprintf(stop_reason, sizeof(stop_reason),
751 "watch:%08" TARGET_PRIxADDR ";", hit_wp_address);
752 break;
753 case WPT_READ:
754 snprintf(stop_reason, sizeof(stop_reason),
755 "rwatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
756 break;
757 case WPT_ACCESS:
758 snprintf(stop_reason, sizeof(stop_reason),
759 "awatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
760 break;
761 default:
762 break;
763 }
764 }
765 }
766
767 current_thread[0] = '\0';
768 if (target->rtos != NULL) {
769 struct target *ct;
770 snprintf(current_thread, sizeof(current_thread), "thread:%016" PRIx64 ";",
771 target->rtos->current_thread);
772 target->rtos->current_threadid = target->rtos->current_thread;
773 target->rtos->gdb_target_for_threadid(connection, target->rtos->current_threadid, &ct);
774 if (!gdb_connection->ctrl_c)
775 signal_var = gdb_last_signal(ct);
776 }
777
778 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "T%2.2x%s%s",
779 signal_var, stop_reason, current_thread);
780
781 gdb_connection->ctrl_c = 0;
782 }
783
784 gdb_put_packet(connection, sig_reply, sig_reply_len);
785 gdb_connection->frontend_state = TARGET_HALTED;
786 }
787
788 static void gdb_fileio_reply(struct target *target, struct connection *connection)
789 {
790 struct gdb_connection *gdb_connection = connection->priv;
791 char fileio_command[256];
792 int command_len;
793 bool program_exited = false;
794
795 if (strcmp(target->fileio_info->identifier, "open") == 0)
796 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
797 target->fileio_info->param_1,
798 target->fileio_info->param_2,
799 target->fileio_info->param_3,
800 target->fileio_info->param_4);
801 else if (strcmp(target->fileio_info->identifier, "close") == 0)
802 sprintf(fileio_command, "F%s,%" PRIx64, target->fileio_info->identifier,
803 target->fileio_info->param_1);
804 else if (strcmp(target->fileio_info->identifier, "read") == 0)
805 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
806 target->fileio_info->param_1,
807 target->fileio_info->param_2,
808 target->fileio_info->param_3);
809 else if (strcmp(target->fileio_info->identifier, "write") == 0)
810 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
811 target->fileio_info->param_1,
812 target->fileio_info->param_2,
813 target->fileio_info->param_3);
814 else if (strcmp(target->fileio_info->identifier, "lseek") == 0)
815 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
816 target->fileio_info->param_1,
817 target->fileio_info->param_2,
818 target->fileio_info->param_3);
819 else if (strcmp(target->fileio_info->identifier, "rename") == 0)
820 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
821 target->fileio_info->param_1,
822 target->fileio_info->param_2,
823 target->fileio_info->param_3,
824 target->fileio_info->param_4);
825 else if (strcmp(target->fileio_info->identifier, "unlink") == 0)
826 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
827 target->fileio_info->param_1,
828 target->fileio_info->param_2);
829 else if (strcmp(target->fileio_info->identifier, "stat") == 0)
830 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
831 target->fileio_info->param_1,
832 target->fileio_info->param_2,
833 target->fileio_info->param_3);
834 else if (strcmp(target->fileio_info->identifier, "fstat") == 0)
835 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
836 target->fileio_info->param_1,
837 target->fileio_info->param_2);
838 else if (strcmp(target->fileio_info->identifier, "gettimeofday") == 0)
839 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
840 target->fileio_info->param_1,
841 target->fileio_info->param_2);
842 else if (strcmp(target->fileio_info->identifier, "isatty") == 0)
843 sprintf(fileio_command, "F%s,%" PRIx64, target->fileio_info->identifier,
844 target->fileio_info->param_1);
845 else if (strcmp(target->fileio_info->identifier, "system") == 0)
846 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
847 target->fileio_info->param_1,
848 target->fileio_info->param_2);
849 else if (strcmp(target->fileio_info->identifier, "exit") == 0) {
850 /* If target hits exit syscall, report to GDB the program is terminated.
851 * In addition, let target run its own exit syscall handler. */
852 program_exited = true;
853 sprintf(fileio_command, "W%02" PRIx64, target->fileio_info->param_1);
854 } else {
855 LOG_DEBUG("Unknown syscall: %s", target->fileio_info->identifier);
856
857 /* encounter unknown syscall, continue */
858 gdb_connection->frontend_state = TARGET_RUNNING;
859 target_resume(target, 1, 0x0, 0, 0);
860 return;
861 }
862
863 command_len = strlen(fileio_command);
864 gdb_put_packet(connection, fileio_command, command_len);
865
866 if (program_exited) {
867 /* Use target_resume() to let target run its own exit syscall handler. */
868 gdb_connection->frontend_state = TARGET_RUNNING;
869 target_resume(target, 1, 0x0, 0, 0);
870 } else {
871 gdb_connection->frontend_state = TARGET_HALTED;
872 rtos_update_threads(target);
873 }
874 }
875
876 static void gdb_frontend_halted(struct target *target, struct connection *connection)
877 {
878 struct gdb_connection *gdb_connection = connection->priv;
879
880 /* In the GDB protocol when we are stepping or continuing execution,
881 * we have a lingering reply. Upon receiving a halted event
882 * when we have that lingering packet, we reply to the original
883 * step or continue packet.
884 *
885 * Executing monitor commands can bring the target in and
886 * out of the running state so we'll see lots of TARGET_EVENT_XXX
887 * that are to be ignored.
888 */
889 if (gdb_connection->frontend_state == TARGET_RUNNING) {
890 /* stop forwarding log packets! */
891 log_remove_callback(gdb_log_callback, connection);
892
893 /* check fileio first */
894 if (target_get_gdb_fileio_info(target, target->fileio_info) == ERROR_OK)
895 gdb_fileio_reply(target, connection);
896 else
897 gdb_signal_reply(target, connection);
898 }
899 }
900
901 static int gdb_target_callback_event_handler(struct target *target,
902 enum target_event event, void *priv)
903 {
904 int retval;
905 struct connection *connection = priv;
906 struct gdb_service *gdb_service = connection->service->priv;
907
908 if (gdb_service->target != target)
909 return ERROR_OK;
910
911 switch (event) {
912 case TARGET_EVENT_GDB_HALT:
913 gdb_frontend_halted(target, connection);
914 break;
915 case TARGET_EVENT_HALTED:
916 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
917 break;
918 case TARGET_EVENT_GDB_FLASH_ERASE_START:
919 retval = jtag_execute_queue();
920 if (retval != ERROR_OK)
921 return retval;
922 break;
923 default:
924 break;
925 }
926
927 return ERROR_OK;
928 }
929
930 static int gdb_new_connection(struct connection *connection)
931 {
932 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
933 struct target *target;
934 int retval;
935 int initial_ack;
936
937 target = get_target_from_connection(connection);
938 connection->priv = gdb_connection;
939 connection->cmd_ctx->current_target = target;
940
941 /* initialize gdb connection information */
942 gdb_connection->buf_p = gdb_connection->buffer;
943 gdb_connection->buf_cnt = 0;
944 gdb_connection->ctrl_c = 0;
945 gdb_connection->frontend_state = TARGET_HALTED;
946 gdb_connection->vflash_image = NULL;
947 gdb_connection->closed = false;
948 gdb_connection->busy = false;
949 gdb_connection->noack_mode = 0;
950 gdb_connection->sync = false;
951 gdb_connection->mem_write_error = false;
952 gdb_connection->attached = true;
953 gdb_connection->target_desc.tdesc = NULL;
954 gdb_connection->target_desc.tdesc_length = 0;
955 gdb_connection->thread_list = NULL;
956
957 /* send ACK to GDB for debug request */
958 gdb_write(connection, "+", 1);
959
960 /* output goes through gdb connection */
961 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
962
963 /* we must remove all breakpoints registered to the target as a previous
964 * GDB session could leave dangling breakpoints if e.g. communication
965 * timed out.
966 */
967 breakpoint_clear_target(target);
968 watchpoint_clear_target(target);
969
970 if (target->rtos) {
971 /* clean previous rtos session if supported*/
972 if (target->rtos->type->clean)
973 target->rtos->type->clean(target);
974
975 /* update threads */
976 rtos_update_threads(target);
977 }
978
979 /* remove the initial ACK from the incoming buffer */
980 retval = gdb_get_char(connection, &initial_ack);
981 if (retval != ERROR_OK)
982 return retval;
983
984 /* FIX!!!??? would we actually ever receive a + here???
985 * Not observed.
986 */
987 if (initial_ack != '+')
988 gdb_putback_char(connection, initial_ack);
989 target_call_event_callbacks(target, TARGET_EVENT_GDB_ATTACH);
990
991 if (gdb_use_memory_map) {
992 /* Connect must fail if the memory map can't be set up correctly.
993 *
994 * This will cause an auto_probe to be invoked, which is either
995 * a no-op or it will fail when the target isn't ready(e.g. not halted).
996 */
997 int i;
998 for (i = 0; i < flash_get_bank_count(); i++) {
999 struct flash_bank *p;
1000 p = get_flash_bank_by_num_noprobe(i);
1001 if (p->target != target)
1002 continue;
1003 retval = get_flash_bank_by_num(i, &p);
1004 if (retval != ERROR_OK) {
1005 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target " \
1006 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1007 return retval;
1008 }
1009 }
1010 }
1011
1012 gdb_actual_connections++;
1013 log_printf_lf(all_targets->next != NULL ? LOG_LVL_INFO : LOG_LVL_DEBUG,
1014 __FILE__, __LINE__, __func__,
1015 "New GDB Connection: %d, Target %s, state: %s",
1016 gdb_actual_connections,
1017 target_name(target),
1018 target_state_name(target));
1019
1020 /* DANGER! If we fail subsequently, we must remove this handler,
1021 * otherwise we occasionally see crashes as the timer can invoke the
1022 * callback fn.
1023 *
1024 * register callback to be informed about target events */
1025 target_register_event_callback(gdb_target_callback_event_handler, connection);
1026
1027 return ERROR_OK;
1028 }
1029
1030 static int gdb_connection_closed(struct connection *connection)
1031 {
1032 struct target *target;
1033 struct gdb_connection *gdb_connection = connection->priv;
1034
1035 target = get_target_from_connection(connection);
1036
1037 /* we're done forwarding messages. Tear down callback before
1038 * cleaning up connection.
1039 */
1040 log_remove_callback(gdb_log_callback, connection);
1041
1042 gdb_actual_connections--;
1043 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1044 target_name(target),
1045 target_state_name(target),
1046 gdb_actual_connections);
1047
1048 /* see if an image built with vFlash commands is left */
1049 if (gdb_connection->vflash_image) {
1050 image_close(gdb_connection->vflash_image);
1051 free(gdb_connection->vflash_image);
1052 gdb_connection->vflash_image = NULL;
1053 }
1054
1055 /* if this connection registered a debug-message receiver delete it */
1056 delete_debug_msg_receiver(connection->cmd_ctx, target);
1057
1058 if (connection->priv) {
1059 free(connection->priv);
1060 connection->priv = NULL;
1061 } else
1062 LOG_ERROR("BUG: connection->priv == NULL");
1063
1064 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
1065
1066 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
1067
1068 target_call_event_callbacks(target, TARGET_EVENT_GDB_DETACH);
1069
1070 return ERROR_OK;
1071 }
1072
1073 static void gdb_send_error(struct connection *connection, uint8_t the_error)
1074 {
1075 char err[4];
1076 snprintf(err, 4, "E%2.2X", the_error);
1077 gdb_put_packet(connection, err, 3);
1078 }
1079
1080 static int gdb_last_signal_packet(struct connection *connection,
1081 char const *packet, int packet_size)
1082 {
1083 struct target *target = get_target_from_connection(connection);
1084 struct gdb_connection *gdb_con = connection->priv;
1085 char sig_reply[4];
1086 int signal_var;
1087
1088 if (!gdb_con->attached) {
1089 /* if we are here we have received a kill packet
1090 * reply W stop reply otherwise gdb gets very unhappy */
1091 gdb_put_packet(connection, "W00", 3);
1092 return ERROR_OK;
1093 }
1094
1095 signal_var = gdb_last_signal(target);
1096
1097 snprintf(sig_reply, 4, "S%2.2x", signal_var);
1098 gdb_put_packet(connection, sig_reply, 3);
1099
1100 return ERROR_OK;
1101 }
1102
1103 static inline int gdb_reg_pos(struct target *target, int pos, int len)
1104 {
1105 if (target->endianness == TARGET_LITTLE_ENDIAN)
1106 return pos;
1107 else
1108 return len - 1 - pos;
1109 }
1110
1111 /* Convert register to string of bytes. NB! The # of bits in the
1112 * register might be non-divisible by 8(a byte), in which
1113 * case an entire byte is shown.
1114 *
1115 * NB! the format on the wire is the target endianness
1116 *
1117 * The format of reg->value is little endian
1118 *
1119 */
1120 static void gdb_str_to_target(struct target *target,
1121 char *tstr, struct reg *reg)
1122 {
1123 int i;
1124
1125 uint8_t *buf;
1126 int buf_len;
1127 buf = reg->value;
1128 buf_len = DIV_ROUND_UP(reg->size, 8);
1129
1130 for (i = 0; i < buf_len; i++) {
1131 int j = gdb_reg_pos(target, i, buf_len);
1132 tstr += sprintf(tstr, "%02x", buf[j]);
1133 }
1134 }
1135
1136 /* copy over in register buffer */
1137 static void gdb_target_to_reg(struct target *target,
1138 char const *tstr, int str_len, uint8_t *bin)
1139 {
1140 if (str_len % 2) {
1141 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1142 exit(-1);
1143 }
1144
1145 int i;
1146 for (i = 0; i < str_len; i += 2) {
1147 unsigned t;
1148 if (sscanf(tstr + i, "%02x", &t) != 1) {
1149 LOG_ERROR("BUG: unable to convert register value");
1150 exit(-1);
1151 }
1152
1153 int j = gdb_reg_pos(target, i/2, str_len/2);
1154 bin[j] = t;
1155 }
1156 }
1157
1158 static int gdb_get_registers_packet(struct connection *connection,
1159 char const *packet, int packet_size)
1160 {
1161 struct target *target = get_target_from_connection(connection);
1162 struct reg **reg_list;
1163 int reg_list_size;
1164 int retval;
1165 int reg_packet_size = 0;
1166 char *reg_packet;
1167 char *reg_packet_p;
1168 int i;
1169
1170 #ifdef _DEBUG_GDB_IO_
1171 LOG_DEBUG("-");
1172 #endif
1173
1174 if ((target->rtos != NULL) && (ERROR_OK == rtos_get_gdb_reg_list(connection)))
1175 return ERROR_OK;
1176
1177 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1178 REG_CLASS_GENERAL);
1179 if (retval != ERROR_OK)
1180 return gdb_error(connection, retval);
1181
1182 for (i = 0; i < reg_list_size; i++)
1183 reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1184
1185 assert(reg_packet_size > 0);
1186
1187 reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
1188 if (reg_packet == NULL)
1189 return ERROR_FAIL;
1190
1191 reg_packet_p = reg_packet;
1192
1193 for (i = 0; i < reg_list_size; i++) {
1194 if (!reg_list[i]->valid) {
1195 retval = reg_list[i]->type->get(reg_list[i]);
1196 if (retval != ERROR_OK && gdb_report_register_access_error) {
1197 LOG_DEBUG("Couldn't get register %s.", reg_list[i]->name);
1198 free(reg_packet);
1199 free(reg_list);
1200 return gdb_error(connection, retval);
1201 }
1202 }
1203 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
1204 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1205 }
1206
1207 #ifdef _DEBUG_GDB_IO_
1208 {
1209 char *reg_packet_p_debug;
1210 reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1211 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1212 free(reg_packet_p_debug);
1213 }
1214 #endif
1215
1216 gdb_put_packet(connection, reg_packet, reg_packet_size);
1217 free(reg_packet);
1218
1219 free(reg_list);
1220
1221 return ERROR_OK;
1222 }
1223
1224 static int gdb_set_registers_packet(struct connection *connection,
1225 char const *packet, int packet_size)
1226 {
1227 struct target *target = get_target_from_connection(connection);
1228 int i;
1229 struct reg **reg_list;
1230 int reg_list_size;
1231 int retval;
1232 char const *packet_p;
1233
1234 #ifdef _DEBUG_GDB_IO_
1235 LOG_DEBUG("-");
1236 #endif
1237
1238 /* skip command character */
1239 packet++;
1240 packet_size--;
1241
1242 if (packet_size % 2) {
1243 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1244 return ERROR_SERVER_REMOTE_CLOSED;
1245 }
1246
1247 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1248 REG_CLASS_GENERAL);
1249 if (retval != ERROR_OK)
1250 return gdb_error(connection, retval);
1251
1252 packet_p = packet;
1253 for (i = 0; i < reg_list_size; i++) {
1254 uint8_t *bin_buf;
1255 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1256
1257 if (packet_p + chars > packet + packet_size)
1258 LOG_ERROR("BUG: register packet is too small for registers");
1259
1260 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1261 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1262
1263 retval = reg_list[i]->type->set(reg_list[i], bin_buf);
1264 if (retval != ERROR_OK && gdb_report_register_access_error) {
1265 LOG_DEBUG("Couldn't set register %s.", reg_list[i]->name);
1266 free(reg_list);
1267 free(bin_buf);
1268 return gdb_error(connection, retval);
1269 }
1270
1271 /* advance packet pointer */
1272 packet_p += chars;
1273
1274 free(bin_buf);
1275 }
1276
1277 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1278 free(reg_list);
1279
1280 gdb_put_packet(connection, "OK", 2);
1281
1282 return ERROR_OK;
1283 }
1284
1285 static int gdb_get_register_packet(struct connection *connection,
1286 char const *packet, int packet_size)
1287 {
1288 struct target *target = get_target_from_connection(connection);
1289 char *reg_packet;
1290 int reg_num = strtoul(packet + 1, NULL, 16);
1291 struct reg **reg_list;
1292 int reg_list_size;
1293 int retval;
1294
1295 #ifdef _DEBUG_GDB_IO_
1296 LOG_DEBUG("-");
1297 #endif
1298
1299 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1300 REG_CLASS_ALL);
1301 if (retval != ERROR_OK)
1302 return gdb_error(connection, retval);
1303
1304 if (reg_list_size <= reg_num) {
1305 LOG_ERROR("gdb requested a non-existing register");
1306 return ERROR_SERVER_REMOTE_CLOSED;
1307 }
1308
1309 if (!reg_list[reg_num]->valid) {
1310 retval = reg_list[reg_num]->type->get(reg_list[reg_num]);
1311 if (retval != ERROR_OK && gdb_report_register_access_error) {
1312 LOG_DEBUG("Couldn't get register %s.", reg_list[reg_num]->name);
1313 free(reg_list);
1314 return gdb_error(connection, retval);
1315 }
1316 }
1317
1318 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1); /* plus one for string termination null */
1319
1320 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1321
1322 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1323
1324 free(reg_list);
1325 free(reg_packet);
1326
1327 return ERROR_OK;
1328 }
1329
1330 static int gdb_set_register_packet(struct connection *connection,
1331 char const *packet, int packet_size)
1332 {
1333 struct target *target = get_target_from_connection(connection);
1334 char *separator;
1335 uint8_t *bin_buf;
1336 int reg_num = strtoul(packet + 1, &separator, 16);
1337 struct reg **reg_list;
1338 int reg_list_size;
1339 int retval;
1340
1341 LOG_DEBUG("-");
1342
1343 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1344 REG_CLASS_ALL);
1345 if (retval != ERROR_OK)
1346 return gdb_error(connection, retval);
1347
1348 if (reg_list_size <= reg_num) {
1349 LOG_ERROR("gdb requested a non-existing register");
1350 return ERROR_SERVER_REMOTE_CLOSED;
1351 }
1352
1353 if (*separator != '=') {
1354 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1355 return ERROR_SERVER_REMOTE_CLOSED;
1356 }
1357
1358 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1359 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1360 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1361
1362 if ((unsigned int)chars != strlen(separator + 1)) {
1363 LOG_ERROR("gdb sent %zu bits for a %d-bit register (%s)",
1364 strlen(separator + 1) * 4, chars * 4, reg_list[reg_num]->name);
1365 free(bin_buf);
1366 return ERROR_SERVER_REMOTE_CLOSED;
1367 }
1368
1369 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1370
1371 retval = reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1372 if (retval != ERROR_OK && gdb_report_register_access_error) {
1373 LOG_DEBUG("Couldn't set register %s.", reg_list[reg_num]->name);
1374 free(bin_buf);
1375 free(reg_list);
1376 return gdb_error(connection, retval);
1377 }
1378
1379 gdb_put_packet(connection, "OK", 2);
1380
1381 free(bin_buf);
1382 free(reg_list);
1383
1384 return ERROR_OK;
1385 }
1386
1387 /* No attempt is made to translate the "retval" to
1388 * GDB speak. This has to be done at the calling
1389 * site as no mapping really exists.
1390 */
1391 static int gdb_error(struct connection *connection, int retval)
1392 {
1393 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1394 gdb_send_error(connection, EFAULT);
1395 return ERROR_OK;
1396 }
1397
1398 /* We don't have to worry about the default 2 second timeout for GDB packets,
1399 * because GDB breaks up large memory reads into smaller reads.
1400 *
1401 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1402 */
1403 static int gdb_read_memory_packet(struct connection *connection,
1404 char const *packet, int packet_size)
1405 {
1406 struct target *target = get_target_from_connection(connection);
1407 char *separator;
1408 uint64_t addr = 0;
1409 uint32_t len = 0;
1410
1411 uint8_t *buffer;
1412 char *hex_buffer;
1413
1414 int retval = ERROR_OK;
1415
1416 /* skip command character */
1417 packet++;
1418
1419 addr = strtoull(packet, &separator, 16);
1420
1421 if (*separator != ',') {
1422 LOG_ERROR("incomplete read memory packet received, dropping connection");
1423 return ERROR_SERVER_REMOTE_CLOSED;
1424 }
1425
1426 len = strtoul(separator + 1, NULL, 16);
1427
1428 if (!len) {
1429 LOG_WARNING("invalid read memory packet received (len == 0)");
1430 gdb_put_packet(connection, NULL, 0);
1431 return ERROR_OK;
1432 }
1433
1434 buffer = malloc(len);
1435
1436 LOG_DEBUG("addr: 0x%16.16" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1437
1438 retval = target_read_buffer(target, addr, len, buffer);
1439
1440 if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1441 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1442 * At some point this might be fixed in GDB, in which case this code can be removed.
1443 *
1444 * OpenOCD developers are acutely aware of this problem, but there is nothing
1445 * gained by involving the user in this problem that hopefully will get resolved
1446 * eventually
1447 *
1448 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1449 * cmd = view%20audit-trail&database = gdb&pr = 2395
1450 *
1451 * For now, the default is to fix up things to make current GDB versions work.
1452 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1453 */
1454 memset(buffer, 0, len);
1455 retval = ERROR_OK;
1456 }
1457
1458 if (retval == ERROR_OK) {
1459 hex_buffer = malloc(len * 2 + 1);
1460
1461 size_t pkt_len = hexify(hex_buffer, buffer, len, len * 2 + 1);
1462
1463 gdb_put_packet(connection, hex_buffer, pkt_len);
1464
1465 free(hex_buffer);
1466 } else
1467 retval = gdb_error(connection, retval);
1468
1469 free(buffer);
1470
1471 return retval;
1472 }
1473
1474 static int gdb_write_memory_packet(struct connection *connection,
1475 char const *packet, int packet_size)
1476 {
1477 struct target *target = get_target_from_connection(connection);
1478 char *separator;
1479 uint64_t addr = 0;
1480 uint32_t len = 0;
1481
1482 uint8_t *buffer;
1483 int retval;
1484
1485 /* skip command character */
1486 packet++;
1487
1488 addr = strtoull(packet, &separator, 16);
1489
1490 if (*separator != ',') {
1491 LOG_ERROR("incomplete write memory packet received, dropping connection");
1492 return ERROR_SERVER_REMOTE_CLOSED;
1493 }
1494
1495 len = strtoul(separator + 1, &separator, 16);
1496
1497 if (*(separator++) != ':') {
1498 LOG_ERROR("incomplete write memory packet received, dropping connection");
1499 return ERROR_SERVER_REMOTE_CLOSED;
1500 }
1501
1502 buffer = malloc(len);
1503
1504 LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1505
1506 if (unhexify(buffer, separator, len) != len)
1507 LOG_ERROR("unable to decode memory packet");
1508
1509 retval = target_write_buffer(target, addr, len, buffer);
1510
1511 if (retval == ERROR_OK)
1512 gdb_put_packet(connection, "OK", 2);
1513 else
1514 retval = gdb_error(connection, retval);
1515
1516 free(buffer);
1517
1518 return retval;
1519 }
1520
1521 static int gdb_write_memory_binary_packet(struct connection *connection,
1522 char const *packet, int packet_size)
1523 {
1524 struct target *target = get_target_from_connection(connection);
1525 char *separator;
1526 uint64_t addr = 0;
1527 uint32_t len = 0;
1528
1529 int retval = ERROR_OK;
1530 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1531 * the assumption that we're in a download and it's important to go as fast
1532 * as possible. */
1533 uint32_t fast_limit = 8;
1534
1535 /* skip command character */
1536 packet++;
1537
1538 addr = strtoull(packet, &separator, 16);
1539
1540 if (*separator != ',') {
1541 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1542 return ERROR_SERVER_REMOTE_CLOSED;
1543 }
1544
1545 len = strtoul(separator + 1, &separator, 16);
1546
1547 if (*(separator++) != ':') {
1548 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1549 return ERROR_SERVER_REMOTE_CLOSED;
1550 }
1551
1552 struct gdb_connection *gdb_connection = connection->priv;
1553
1554 if (gdb_connection->mem_write_error)
1555 retval = ERROR_FAIL;
1556
1557 if (retval == ERROR_OK) {
1558 if (len >= fast_limit) {
1559 /* By replying the packet *immediately* GDB will send us a new packet
1560 * while we write the last one to the target.
1561 * We only do this for larger writes, so that users who do something like:
1562 * p *((int*)0xdeadbeef)=8675309
1563 * will get immediate feedback that that write failed.
1564 */
1565 gdb_put_packet(connection, "OK", 2);
1566 }
1567 } else {
1568 retval = gdb_error(connection, retval);
1569 /* now that we have reported the memory write error, we can clear the condition */
1570 gdb_connection->mem_write_error = false;
1571 if (retval != ERROR_OK)
1572 return retval;
1573 }
1574
1575 if (len) {
1576 LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1577
1578 retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1579 if (retval != ERROR_OK)
1580 gdb_connection->mem_write_error = true;
1581 }
1582
1583 if (len < fast_limit) {
1584 if (retval != ERROR_OK) {
1585 gdb_error(connection, retval);
1586 gdb_connection->mem_write_error = false;
1587 } else {
1588 gdb_put_packet(connection, "OK", 2);
1589 }
1590 }
1591
1592 return ERROR_OK;
1593 }
1594
1595 static int gdb_step_continue_packet(struct connection *connection,
1596 char const *packet, int packet_size)
1597 {
1598 struct target *target = get_target_from_connection(connection);
1599 int current = 0;
1600 uint64_t address = 0x0;
1601 int retval = ERROR_OK;
1602
1603 LOG_DEBUG("-");
1604
1605 if (packet_size > 1)
1606 address = strtoull(packet + 1, NULL, 16);
1607 else
1608 current = 1;
1609
1610 gdb_running_type = packet[0];
1611 if (packet[0] == 'c') {
1612 LOG_DEBUG("continue");
1613 /* resume at current address, don't handle breakpoints, not debugging */
1614 retval = target_resume(target, current, address, 0, 0);
1615 } else if (packet[0] == 's') {
1616 LOG_DEBUG("step");
1617 /* step at current or address, don't handle breakpoints */
1618 retval = target_step(target, current, address, 0);
1619 }
1620 return retval;
1621 }
1622
1623 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1624 char const *packet, int packet_size)
1625 {
1626 struct target *target = get_target_from_connection(connection);
1627 int type;
1628 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1629 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1630 uint64_t address;
1631 uint32_t size;
1632 char *separator;
1633 int retval;
1634
1635 LOG_DEBUG("-");
1636
1637 type = strtoul(packet + 1, &separator, 16);
1638
1639 if (type == 0) /* memory breakpoint */
1640 bp_type = BKPT_SOFT;
1641 else if (type == 1) /* hardware breakpoint */
1642 bp_type = BKPT_HARD;
1643 else if (type == 2) /* write watchpoint */
1644 wp_type = WPT_WRITE;
1645 else if (type == 3) /* read watchpoint */
1646 wp_type = WPT_READ;
1647 else if (type == 4) /* access watchpoint */
1648 wp_type = WPT_ACCESS;
1649 else {
1650 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1651 return ERROR_SERVER_REMOTE_CLOSED;
1652 }
1653
1654 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1655 bp_type = gdb_breakpoint_override_type;
1656
1657 if (*separator != ',') {
1658 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1659 return ERROR_SERVER_REMOTE_CLOSED;
1660 }
1661
1662 address = strtoull(separator + 1, &separator, 16);
1663
1664 if (*separator != ',') {
1665 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1666 return ERROR_SERVER_REMOTE_CLOSED;
1667 }
1668
1669 size = strtoul(separator + 1, &separator, 16);
1670
1671 switch (type) {
1672 case 0:
1673 case 1:
1674 if (packet[0] == 'Z') {
1675 retval = breakpoint_add(target, address, size, bp_type);
1676 if (retval != ERROR_OK) {
1677 retval = gdb_error(connection, retval);
1678 if (retval != ERROR_OK)
1679 return retval;
1680 } else
1681 gdb_put_packet(connection, "OK", 2);
1682 } else {
1683 breakpoint_remove(target, address);
1684 gdb_put_packet(connection, "OK", 2);
1685 }
1686 break;
1687 case 2:
1688 case 3:
1689 case 4:
1690 {
1691 if (packet[0] == 'Z') {
1692 retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu);
1693 if (retval != ERROR_OK) {
1694 retval = gdb_error(connection, retval);
1695 if (retval != ERROR_OK)
1696 return retval;
1697 } else
1698 gdb_put_packet(connection, "OK", 2);
1699 } else {
1700 watchpoint_remove(target, address);
1701 gdb_put_packet(connection, "OK", 2);
1702 }
1703 break;
1704 }
1705 default:
1706 break;
1707 }
1708
1709 return ERROR_OK;
1710 }
1711
1712 /* print out a string and allocate more space as needed,
1713 * mainly used for XML at this point
1714 */
1715 static void xml_printf(int *retval, char **xml, int *pos, int *size,
1716 const char *fmt, ...)
1717 {
1718 if (*retval != ERROR_OK)
1719 return;
1720 int first = 1;
1721
1722 for (;; ) {
1723 if ((*xml == NULL) || (!first)) {
1724 /* start by 0 to exercise all the code paths.
1725 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1726
1727 *size = *size * 2 + 2;
1728 char *t = *xml;
1729 *xml = realloc(*xml, *size);
1730 if (*xml == NULL) {
1731 if (t)
1732 free(t);
1733 *retval = ERROR_SERVER_REMOTE_CLOSED;
1734 return;
1735 }
1736 }
1737
1738 va_list ap;
1739 int ret;
1740 va_start(ap, fmt);
1741 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1742 va_end(ap);
1743 if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1744 *pos += ret;
1745 return;
1746 }
1747 /* there was just enough or not enough space, allocate more. */
1748 first = 0;
1749 }
1750 }
1751
1752 static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
1753 {
1754 /* Locate the annex. */
1755 const char *annex_end = strchr(buf, ':');
1756 if (annex_end == NULL)
1757 return ERROR_FAIL;
1758
1759 /* After the read marker and annex, qXfer looks like a
1760 * traditional 'm' packet. */
1761 char *separator;
1762 *ofs = strtoul(annex_end + 1, &separator, 16);
1763
1764 if (*separator != ',')
1765 return ERROR_FAIL;
1766
1767 *len = strtoul(separator + 1, NULL, 16);
1768
1769 /* Extract the annex if needed */
1770 if (annex != NULL) {
1771 *annex = strndup(buf, annex_end - buf);
1772 if (*annex == NULL)
1773 return ERROR_FAIL;
1774 }
1775
1776 return ERROR_OK;
1777 }
1778
1779 static int compare_bank(const void *a, const void *b)
1780 {
1781 struct flash_bank *b1, *b2;
1782 b1 = *((struct flash_bank **)a);
1783 b2 = *((struct flash_bank **)b);
1784
1785 if (b1->base == b2->base)
1786 return 0;
1787 else if (b1->base > b2->base)
1788 return 1;
1789 else
1790 return -1;
1791 }
1792
1793 static int gdb_memory_map(struct connection *connection,
1794 char const *packet, int packet_size)
1795 {
1796 /* We get away with only specifying flash here. Regions that are not
1797 * specified are treated as if we provided no memory map(if not we
1798 * could detect the holes and mark them as RAM).
1799 * Normally we only execute this code once, but no big deal if we
1800 * have to regenerate it a couple of times.
1801 */
1802
1803 struct target *target = get_target_from_connection(connection);
1804 struct flash_bank *p;
1805 char *xml = NULL;
1806 int size = 0;
1807 int pos = 0;
1808 int retval = ERROR_OK;
1809 struct flash_bank **banks;
1810 int offset;
1811 int length;
1812 char *separator;
1813 target_addr_t ram_start = 0;
1814 int i;
1815 int target_flash_banks = 0;
1816
1817 /* skip command character */
1818 packet += 23;
1819
1820 offset = strtoul(packet, &separator, 16);
1821 length = strtoul(separator + 1, &separator, 16);
1822
1823 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1824
1825 /* Sort banks in ascending order. We need to report non-flash
1826 * memory as ram (or rather read/write) by default for GDB, since
1827 * it has no concept of non-cacheable read/write memory (i/o etc).
1828 */
1829 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1830
1831 for (i = 0; i < flash_get_bank_count(); i++) {
1832 p = get_flash_bank_by_num_noprobe(i);
1833 if (p->target != target)
1834 continue;
1835 retval = get_flash_bank_by_num(i, &p);
1836 if (retval != ERROR_OK) {
1837 free(banks);
1838 gdb_error(connection, retval);
1839 return retval;
1840 }
1841 banks[target_flash_banks++] = p;
1842 }
1843
1844 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1845 compare_bank);
1846
1847 for (i = 0; i < target_flash_banks; i++) {
1848 int j;
1849 unsigned sector_size = 0;
1850 unsigned group_len = 0;
1851
1852 p = banks[i];
1853
1854 if (ram_start < p->base)
1855 xml_printf(&retval, &xml, &pos, &size,
1856 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
1857 "length=\"0x%x\"/>\n",
1858 ram_start, p->base - ram_start);
1859
1860 /* Report adjacent groups of same-size sectors. So for
1861 * example top boot CFI flash will list an initial region
1862 * with several large sectors (maybe 128KB) and several
1863 * smaller ones at the end (maybe 32KB). STR7 will have
1864 * regions with 8KB, 32KB, and 64KB sectors; etc.
1865 */
1866 for (j = 0; j < p->num_sectors; j++) {
1867
1868 /* Maybe start a new group of sectors. */
1869 if (sector_size == 0) {
1870 if (p->sectors[j].offset + p->sectors[j].size > p->size) {
1871 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1872 " overflows the end of %s bank.",
1873 p->sectors[j].offset, p->name);
1874 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1875 break;
1876 }
1877 target_addr_t start;
1878 start = p->base + p->sectors[j].offset;
1879 xml_printf(&retval, &xml, &pos, &size,
1880 "<memory type=\"flash\" "
1881 "start=\"" TARGET_ADDR_FMT "\" ",
1882 start);
1883 sector_size = p->sectors[j].size;
1884 group_len = sector_size;
1885 } else {
1886 group_len += sector_size; /* equal to p->sectors[j].size */
1887 }
1888
1889 /* Does this finish a group of sectors?
1890 * If not, continue an already-started group.
1891 */
1892 if (j < p->num_sectors - 1
1893 && p->sectors[j + 1].size == sector_size
1894 && p->sectors[j + 1].offset == p->sectors[j].offset + sector_size
1895 && p->sectors[j + 1].offset + p->sectors[j + 1].size <= p->size)
1896 continue;
1897
1898 xml_printf(&retval, &xml, &pos, &size,
1899 "length=\"0x%x\">\n"
1900 "<property name=\"blocksize\">"
1901 "0x%x</property>\n"
1902 "</memory>\n",
1903 group_len,
1904 sector_size);
1905 sector_size = 0;
1906 }
1907
1908 ram_start = p->base + p->size;
1909 }
1910
1911 if (ram_start != 0)
1912 xml_printf(&retval, &xml, &pos, &size,
1913 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
1914 "length=\"0x%x\"/>\n",
1915 ram_start, 0-ram_start);
1916 /* ELSE a flash chip could be at the very end of the 32 bit address
1917 * space, in which case ram_start will be precisely 0
1918 */
1919
1920 free(banks);
1921
1922 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1923
1924 if (retval != ERROR_OK) {
1925 free(xml);
1926 gdb_error(connection, retval);
1927 return retval;
1928 }
1929
1930 if (offset + length > pos)
1931 length = pos - offset;
1932
1933 char *t = malloc(length + 1);
1934 t[0] = 'l';
1935 memcpy(t + 1, xml + offset, length);
1936 gdb_put_packet(connection, t, length + 1);
1937
1938 free(t);
1939 free(xml);
1940 return ERROR_OK;
1941 }
1942
1943 static const char *gdb_get_reg_type_name(enum reg_type type)
1944 {
1945 switch (type) {
1946 case REG_TYPE_BOOL:
1947 return "bool";
1948 case REG_TYPE_INT:
1949 return "int";
1950 case REG_TYPE_INT8:
1951 return "int8";
1952 case REG_TYPE_INT16:
1953 return "int16";
1954 case REG_TYPE_INT32:
1955 return "int32";
1956 case REG_TYPE_INT64:
1957 return "int64";
1958 case REG_TYPE_INT128:
1959 return "int128";
1960 case REG_TYPE_UINT:
1961 return "uint";
1962 case REG_TYPE_UINT8:
1963 return "uint8";
1964 case REG_TYPE_UINT16:
1965 return "uint16";
1966 case REG_TYPE_UINT32:
1967 return "uint32";
1968 case REG_TYPE_UINT64:
1969 return "uint64";
1970 case REG_TYPE_UINT128:
1971 return "uint128";
1972 case REG_TYPE_CODE_PTR:
1973 return "code_ptr";
1974 case REG_TYPE_DATA_PTR:
1975 return "data_ptr";
1976 case REG_TYPE_FLOAT:
1977 return "float";
1978 case REG_TYPE_IEEE_SINGLE:
1979 return "ieee_single";
1980 case REG_TYPE_IEEE_DOUBLE:
1981 return "ieee_double";
1982 case REG_TYPE_ARCH_DEFINED:
1983 return "int"; /* return arbitrary string to avoid compile warning. */
1984 }
1985
1986 return "int"; /* "int" as default value */
1987 }
1988
1989 static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id,
1990 int *num_arch_defined_types)
1991 {
1992 int tbl_sz = *num_arch_defined_types;
1993
1994 if (type_id != NULL && (strcmp(type_id, ""))) {
1995 for (int j = 0; j < (tbl_sz + 1); j++) {
1996 if (!((*arch_defined_types_list)[j])) {
1997 (*arch_defined_types_list)[tbl_sz++] = type_id;
1998 *arch_defined_types_list = realloc(*arch_defined_types_list,
1999 sizeof(char *) * (tbl_sz + 1));
2000 (*arch_defined_types_list)[tbl_sz] = NULL;
2001 *num_arch_defined_types = tbl_sz;
2002 return 1;
2003 } else {
2004 if (!strcmp((*arch_defined_types_list)[j], type_id))
2005 return 0;
2006 }
2007 }
2008 }
2009
2010 return -1;
2011 }
2012
2013 static int gdb_generate_reg_type_description(struct target *target,
2014 char **tdesc, int *pos, int *size, struct reg_data_type *type,
2015 char const **arch_defined_types_list[], int * num_arch_defined_types)
2016 {
2017 int retval = ERROR_OK;
2018
2019 if (type->type_class == REG_TYPE_CLASS_VECTOR) {
2020 struct reg_data_type *data_type = type->reg_type_vector->type;
2021 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2022 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2023 num_arch_defined_types))
2024 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2025 arch_defined_types_list,
2026 num_arch_defined_types);
2027 }
2028 /* <vector id="id" type="type" count="count"/> */
2029 xml_printf(&retval, tdesc, pos, size,
2030 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
2031 type->id, type->reg_type_vector->type->id,
2032 type->reg_type_vector->count);
2033
2034 } else if (type->type_class == REG_TYPE_CLASS_UNION) {
2035 struct reg_data_type_union_field *field;
2036 field = type->reg_type_union->fields;
2037 while (field != NULL) {
2038 struct reg_data_type *data_type = field->type;
2039 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2040 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2041 num_arch_defined_types))
2042 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2043 arch_defined_types_list,
2044 num_arch_defined_types);
2045 }
2046
2047 field = field->next;
2048 }
2049 /* <union id="id">
2050 * <field name="name" type="type"/> ...
2051 * </union> */
2052 xml_printf(&retval, tdesc, pos, size,
2053 "<union id=\"%s\">\n",
2054 type->id);
2055
2056 field = type->reg_type_union->fields;
2057 while (field != NULL) {
2058 xml_printf(&retval, tdesc, pos, size,
2059 "<field name=\"%s\" type=\"%s\"/>\n",
2060 field->name, field->type->id);
2061
2062 field = field->next;
2063 }
2064
2065 xml_printf(&retval, tdesc, pos, size,
2066 "</union>\n");
2067
2068 } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
2069 struct reg_data_type_struct_field *field;
2070 field = type->reg_type_struct->fields;
2071
2072 if (field->use_bitfields) {
2073 /* <struct id="id" size="size">
2074 * <field name="name" start="start" end="end"/> ...
2075 * </struct> */
2076 xml_printf(&retval, tdesc, pos, size,
2077 "<struct id=\"%s\" size=\"%d\">\n",
2078 type->id, type->reg_type_struct->size);
2079 while (field != NULL) {
2080 xml_printf(&retval, tdesc, pos, size,
2081 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2082 field->name, field->bitfield->start, field->bitfield->end,
2083 gdb_get_reg_type_name(field->bitfield->type));
2084
2085 field = field->next;
2086 }
2087 } else {
2088 while (field != NULL) {
2089 struct reg_data_type *data_type = field->type;
2090 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2091 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2092 num_arch_defined_types))
2093 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2094 arch_defined_types_list,
2095 num_arch_defined_types);
2096 }
2097 }
2098
2099 /* <struct id="id">
2100 * <field name="name" type="type"/> ...
2101 * </struct> */
2102 xml_printf(&retval, tdesc, pos, size,
2103 "<struct id=\"%s\">\n",
2104 type->id);
2105 while (field != NULL) {
2106 xml_printf(&retval, tdesc, pos, size,
2107 "<field name=\"%s\" type=\"%s\"/>\n",
2108 field->name, field->type->id);
2109
2110 field = field->next;
2111 }
2112 }
2113
2114 xml_printf(&retval, tdesc, pos, size,
2115 "</struct>\n");
2116
2117 } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
2118 /* <flags id="id" size="size">
2119 * <field name="name" start="start" end="end"/> ...
2120 * </flags> */
2121 xml_printf(&retval, tdesc, pos, size,
2122 "<flags id=\"%s\" size=\"%d\">\n",
2123 type->id, type->reg_type_flags->size);
2124
2125 struct reg_data_type_flags_field *field;
2126 field = type->reg_type_flags->fields;
2127 while (field != NULL) {
2128 xml_printf(&retval, tdesc, pos, size,
2129 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2130 field->name, field->bitfield->start, field->bitfield->end,
2131 gdb_get_reg_type_name(field->bitfield->type));
2132
2133 field = field->next;
2134 }
2135
2136 xml_printf(&retval, tdesc, pos, size,
2137 "</flags>\n");
2138
2139 }
2140
2141 return ERROR_OK;
2142 }
2143
2144 /* Get a list of available target registers features. feature_list must
2145 * be freed by caller.
2146 */
2147 static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
2148 struct reg **reg_list, int reg_list_size)
2149 {
2150 int tbl_sz = 0;
2151
2152 /* Start with only one element */
2153 *feature_list = calloc(1, sizeof(char *));
2154
2155 for (int i = 0; i < reg_list_size; i++) {
2156 if (reg_list[i]->exist == false)
2157 continue;
2158
2159 if (reg_list[i]->feature != NULL
2160 && reg_list[i]->feature->name != NULL
2161 && (strcmp(reg_list[i]->feature->name, ""))) {
2162 /* We found a feature, check if the feature is already in the
2163 * table. If not, allocate a new entry for the table and
2164 * put the new feature in it.
2165 */
2166 for (int j = 0; j < (tbl_sz + 1); j++) {
2167 if (!((*feature_list)[j])) {
2168 (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
2169 *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
2170 (*feature_list)[tbl_sz] = NULL;
2171 break;
2172 } else {
2173 if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
2174 break;
2175 }
2176 }
2177 }
2178 }
2179
2180 if (feature_list_size)
2181 *feature_list_size = tbl_sz;
2182
2183 return ERROR_OK;
2184 }
2185
2186 static int gdb_generate_target_description(struct target *target, char **tdesc_out)
2187 {
2188 int retval = ERROR_OK;
2189 struct reg **reg_list = NULL;
2190 int reg_list_size;
2191 char const **features = NULL;
2192 char const **arch_defined_types = NULL;
2193 int feature_list_size = 0;
2194 int num_arch_defined_types = 0;
2195 char *tdesc = NULL;
2196 int pos = 0;
2197 int size = 0;
2198
2199 arch_defined_types = calloc(1, sizeof(char *));
2200
2201 retval = target_get_gdb_reg_list(target, &reg_list,
2202 &reg_list_size, REG_CLASS_ALL);
2203
2204 if (retval != ERROR_OK) {
2205 LOG_ERROR("get register list failed");
2206 retval = ERROR_FAIL;
2207 goto error;
2208 }
2209
2210 if (reg_list_size <= 0) {
2211 LOG_ERROR("get register list failed");
2212 retval = ERROR_FAIL;
2213 goto error;
2214 }
2215
2216 /* Get a list of available target registers features */
2217 retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2218 if (retval != ERROR_OK) {
2219 LOG_ERROR("Can't get the registers feature list");
2220 retval = ERROR_FAIL;
2221 goto error;
2222 }
2223
2224 /* If we found some features associated with registers, create sections */
2225 int current_feature = 0;
2226
2227 xml_printf(&retval, &tdesc, &pos, &size,
2228 "<?xml version=\"1.0\"?>\n"
2229 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2230 "<target version=\"1.0\">\n");
2231
2232 /* generate target description according to register list */
2233 if (features != NULL) {
2234 while (features[current_feature]) {
2235
2236 xml_printf(&retval, &tdesc, &pos, &size,
2237 "<feature name=\"%s\">\n",
2238 features[current_feature]);
2239
2240 int i;
2241 for (i = 0; i < reg_list_size; i++) {
2242
2243 if (reg_list[i]->exist == false)
2244 continue;
2245
2246 if (strcmp(reg_list[i]->feature->name, features[current_feature]))
2247 continue;
2248
2249 const char *type_str;
2250 if (reg_list[i]->reg_data_type != NULL) {
2251 if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
2252 /* generate <type... first, if there are architecture-defined types. */
2253 if (lookup_add_arch_defined_types(&arch_defined_types,
2254 reg_list[i]->reg_data_type->id,
2255 &num_arch_defined_types))
2256 gdb_generate_reg_type_description(target, &tdesc, &pos, &size,
2257 reg_list[i]->reg_data_type,
2258 &arch_defined_types,
2259 &num_arch_defined_types);
2260
2261 type_str = reg_list[i]->reg_data_type->id;
2262 } else {
2263 /* predefined type */
2264 type_str = gdb_get_reg_type_name(
2265 reg_list[i]->reg_data_type->type);
2266 }
2267 } else {
2268 /* Default type is "int" */
2269 type_str = "int";
2270 }
2271
2272 xml_printf(&retval, &tdesc, &pos, &size,
2273 "<reg name=\"%s\"", reg_list[i]->name);
2274 xml_printf(&retval, &tdesc, &pos, &size,
2275 " bitsize=\"%d\"", reg_list[i]->size);
2276 xml_printf(&retval, &tdesc, &pos, &size,
2277 " regnum=\"%d\"", reg_list[i]->number);
2278 if (reg_list[i]->caller_save)
2279 xml_printf(&retval, &tdesc, &pos, &size,
2280 " save-restore=\"yes\"");
2281 else
2282 xml_printf(&retval, &tdesc, &pos, &size,
2283 " save-restore=\"no\"");
2284
2285 xml_printf(&retval, &tdesc, &pos, &size,
2286 " type=\"%s\"", type_str);
2287
2288 if (reg_list[i]->group != NULL)
2289 xml_printf(&retval, &tdesc, &pos, &size,
2290 " group=\"%s\"", reg_list[i]->group);
2291
2292 xml_printf(&retval, &tdesc, &pos, &size,
2293 "/>\n");
2294 }
2295
2296 xml_printf(&retval, &tdesc, &pos, &size,
2297 "</feature>\n");
2298
2299 current_feature++;
2300 }
2301 }
2302
2303 xml_printf(&retval, &tdesc, &pos, &size,
2304 "</target>\n");
2305
2306 error:
2307 free(features);
2308 free(reg_list);
2309 free(arch_defined_types);
2310
2311 if (retval == ERROR_OK)
2312 *tdesc_out = tdesc;
2313 else
2314 free(tdesc);
2315
2316 return retval;
2317 }
2318
2319 static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
2320 char **chunk, int32_t offset, uint32_t length)
2321 {
2322 if (target_desc == NULL) {
2323 LOG_ERROR("Unable to Generate Target Description");
2324 return ERROR_FAIL;
2325 }
2326
2327 char *tdesc = target_desc->tdesc;
2328 uint32_t tdesc_length = target_desc->tdesc_length;
2329
2330 if (tdesc == NULL) {
2331 int retval = gdb_generate_target_description(target, &tdesc);
2332 if (retval != ERROR_OK) {
2333 LOG_ERROR("Unable to Generate Target Description");
2334 return ERROR_FAIL;
2335 }
2336
2337 tdesc_length = strlen(tdesc);
2338 }
2339
2340 char transfer_type;
2341
2342 if (length < (tdesc_length - offset))
2343 transfer_type = 'm';
2344 else
2345 transfer_type = 'l';
2346
2347 *chunk = malloc(length + 2);
2348 if (*chunk == NULL) {
2349 LOG_ERROR("Unable to allocate memory");
2350 return ERROR_FAIL;
2351 }
2352
2353 (*chunk)[0] = transfer_type;
2354 if (transfer_type == 'm') {
2355 strncpy((*chunk) + 1, tdesc + offset, length);
2356 (*chunk)[1 + length] = '\0';
2357 } else {
2358 strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
2359 (*chunk)[1 + (tdesc_length - offset)] = '\0';
2360
2361 /* After gdb-server sends out last chunk, invalidate tdesc. */
2362 free(tdesc);
2363 tdesc = NULL;
2364 tdesc_length = 0;
2365 }
2366
2367 target_desc->tdesc = tdesc;
2368 target_desc->tdesc_length = tdesc_length;
2369
2370 return ERROR_OK;
2371 }
2372
2373 static int gdb_target_description_supported(struct target *target, int *supported)
2374 {
2375 int retval = ERROR_OK;
2376 struct reg **reg_list = NULL;
2377 int reg_list_size = 0;
2378 char const **features = NULL;
2379 int feature_list_size = 0;
2380
2381 retval = target_get_gdb_reg_list(target, &reg_list,
2382 &reg_list_size, REG_CLASS_ALL);
2383 if (retval != ERROR_OK) {
2384 LOG_ERROR("get register list failed");
2385 goto error;
2386 }
2387
2388 if (reg_list_size <= 0) {
2389 LOG_ERROR("get register list failed");
2390 retval = ERROR_FAIL;
2391 goto error;
2392 }
2393
2394 /* Get a list of available target registers features */
2395 retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2396 if (retval != ERROR_OK) {
2397 LOG_ERROR("Can't get the registers feature list");
2398 goto error;
2399 }
2400
2401 if (supported) {
2402 if (feature_list_size)
2403 *supported = 1;
2404 else
2405 *supported = 0;
2406 }
2407
2408 error:
2409 free(features);
2410
2411 free(reg_list);
2412
2413 return retval;
2414 }
2415
2416 static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
2417 {
2418 struct rtos *rtos = target->rtos;
2419 int retval = ERROR_OK;
2420 char *thread_list = NULL;
2421 int pos = 0;
2422 int size = 0;
2423
2424 xml_printf(&retval, &thread_list, &pos, &size,
2425 "<?xml version=\"1.0\"?>\n"
2426 "<threads>\n");
2427
2428 if (rtos != NULL) {
2429 for (int i = 0; i < rtos->thread_count; i++) {
2430 struct thread_detail *thread_detail = &rtos->thread_details[i];
2431
2432 if (!thread_detail->exists)
2433 continue;
2434
2435 xml_printf(&retval, &thread_list, &pos, &size,
2436 "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
2437
2438 if (thread_detail->thread_name_str != NULL)
2439 xml_printf(&retval, &thread_list, &pos, &size,
2440 "Name: %s", thread_detail->thread_name_str);
2441
2442 if (thread_detail->extra_info_str != NULL) {
2443 if (thread_detail->thread_name_str != NULL)
2444 xml_printf(&retval, &thread_list, &pos, &size,
2445 ", ");
2446 xml_printf(&retval, &thread_list, &pos, &size,
2447 thread_detail->extra_info_str);
2448 }
2449
2450 xml_printf(&retval, &thread_list, &pos, &size,
2451 "</thread>\n");
2452 }
2453 }
2454
2455 xml_printf(&retval, &thread_list, &pos, &size,
2456 "</threads>\n");
2457
2458 if (retval == ERROR_OK)
2459 *thread_list_out = thread_list;
2460 else
2461 free(thread_list);
2462
2463 return retval;
2464 }
2465
2466 static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
2467 char **chunk, int32_t offset, uint32_t length)
2468 {
2469 if (*thread_list == NULL) {
2470 int retval = gdb_generate_thread_list(target, thread_list);
2471 if (retval != ERROR_OK) {
2472 LOG_ERROR("Unable to Generate Thread List");
2473 return ERROR_FAIL;
2474 }
2475 }
2476
2477 size_t thread_list_length = strlen(*thread_list);
2478 char transfer_type;
2479
2480 length = MIN(length, thread_list_length - offset);
2481 if (length < (thread_list_length - offset))
2482 transfer_type = 'm';
2483 else
2484 transfer_type = 'l';
2485
2486 *chunk = malloc(length + 2 + 3);
2487 /* Allocating extra 3 bytes prevents false positive valgrind report
2488 * of strlen(chunk) word access:
2489 * Invalid read of size 4
2490 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2491 if (*chunk == NULL) {
2492 LOG_ERROR("Unable to allocate memory");
2493 return ERROR_FAIL;
2494 }
2495
2496 (*chunk)[0] = transfer_type;
2497 strncpy((*chunk) + 1, (*thread_list) + offset, length);
2498 (*chunk)[1 + length] = '\0';
2499
2500 /* After gdb-server sends out last chunk, invalidate thread list. */
2501 if (transfer_type == 'l') {
2502 free(*thread_list);
2503 *thread_list = NULL;
2504 }
2505
2506 return ERROR_OK;
2507 }
2508
2509 static int gdb_query_packet(struct connection *connection,
2510 char const *packet, int packet_size)
2511 {
2512 struct command_context *cmd_ctx = connection->cmd_ctx;
2513 struct gdb_connection *gdb_connection = connection->priv;
2514 struct target *target = get_target_from_connection(connection);
2515
2516 if (strncmp(packet, "qRcmd,", 6) == 0) {
2517 if (packet_size > 6) {
2518 char *cmd;
2519 cmd = malloc((packet_size - 6) / 2 + 1);
2520 size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
2521 cmd[len] = 0;
2522
2523 /* We want to print all debug output to GDB connection */
2524 log_add_callback(gdb_log_callback, connection);
2525 target_call_timer_callbacks_now();
2526 /* some commands need to know the GDB connection, make note of current
2527 * GDB connection. */
2528 current_gdb_connection = gdb_connection;
2529 command_run_line(cmd_ctx, cmd);
2530 current_gdb_connection = NULL;
2531 target_call_timer_callbacks_now();
2532 log_remove_callback(gdb_log_callback, connection);
2533 free(cmd);
2534 }
2535 gdb_put_packet(connection, "OK", 2);
2536 return ERROR_OK;
2537 } else if (strncmp(packet, "qCRC:", 5) == 0) {
2538 if (packet_size > 5) {
2539 int retval;
2540 char gdb_reply[10];
2541 char *separator;
2542 uint32_t checksum;
2543 target_addr_t addr = 0;
2544 uint32_t len = 0;
2545
2546 /* skip command character */
2547 packet += 5;
2548
2549 addr = strtoull(packet, &separator, 16);
2550
2551 if (*separator != ',') {
2552 LOG_ERROR("incomplete read memory packet received, dropping connection");
2553 return ERROR_SERVER_REMOTE_CLOSED;
2554 }
2555
2556 len = strtoul(separator + 1, NULL, 16);
2557
2558 retval = target_checksum_memory(target, addr, len, &checksum);
2559
2560 if (retval == ERROR_OK) {
2561 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
2562 gdb_put_packet(connection, gdb_reply, 9);
2563 } else {
2564 retval = gdb_error(connection, retval);
2565 if (retval != ERROR_OK)
2566 return retval;
2567 }
2568
2569 return ERROR_OK;
2570 }
2571 } else if (strncmp(packet, "qSupported", 10) == 0) {
2572 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2573 * qXfer:features:read is supported for some targets */
2574 int retval = ERROR_OK;
2575 char *buffer = NULL;
2576 int pos = 0;
2577 int size = 0;
2578 int gdb_target_desc_supported = 0;
2579
2580 /* we need to test that the target supports target descriptions */
2581 retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
2582 if (retval != ERROR_OK) {
2583 LOG_INFO("Failed detecting Target Description Support, disabling");
2584 gdb_target_desc_supported = 0;
2585 }
2586
2587 /* support may be disabled globally */
2588 if (gdb_use_target_description == 0) {
2589 if (gdb_target_desc_supported)
2590 LOG_WARNING("Target Descriptions Supported, but disabled");
2591 gdb_target_desc_supported = 0;
2592 }
2593
2594 xml_printf(&retval,
2595 &buffer,
2596 &pos,
2597 &size,
2598 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2599 (GDB_BUFFER_SIZE - 1),
2600 ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2601 (gdb_target_desc_supported == 1) ? '+' : '-');
2602
2603 if (retval != ERROR_OK) {
2604 gdb_send_error(connection, 01);
2605 return ERROR_OK;
2606 }
2607
2608 gdb_put_packet(connection, buffer, strlen(buffer));
2609 free(buffer);
2610
2611 return ERROR_OK;
2612 } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
2613 && (flash_get_bank_count() > 0))
2614 return gdb_memory_map(connection, packet, packet_size);
2615 else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
2616 char *xml = NULL;
2617 int retval = ERROR_OK;
2618
2619 int offset;
2620 unsigned int length;
2621
2622 /* skip command character */
2623 packet += 20;
2624
2625 if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2626 gdb_send_error(connection, 01);
2627 return ERROR_OK;
2628 }
2629
2630 /* Target should prepare correct target description for annex.
2631 * The first character of returned xml is 'm' or 'l'. 'm' for
2632 * there are *more* chunks to transfer. 'l' for it is the *last*
2633 * chunk of target description.
2634 */
2635 retval = gdb_get_target_description_chunk(target, &gdb_connection->target_desc,
2636 &xml, offset, length);
2637 if (retval != ERROR_OK) {
2638 gdb_error(connection, retval);
2639 return retval;
2640 }
2641
2642 gdb_put_packet(connection, xml, strlen(xml));
2643
2644 free(xml);
2645 return ERROR_OK;
2646 } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
2647 char *xml = NULL;
2648 int retval = ERROR_OK;
2649
2650 int offset;
2651 unsigned int length;
2652
2653 /* skip command character */
2654 packet += 19;
2655
2656 if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2657 gdb_send_error(connection, 01);
2658 return ERROR_OK;
2659 }
2660
2661 /* Target should prepare correct thread list for annex.
2662 * The first character of returned xml is 'm' or 'l'. 'm' for
2663 * there are *more* chunks to transfer. 'l' for it is the *last*
2664 * chunk of target description.
2665 */
2666 retval = gdb_get_thread_list_chunk(target, &gdb_connection->thread_list,
2667 &xml, offset, length);
2668 if (retval != ERROR_OK) {
2669 gdb_error(connection, retval);
2670 return retval;
2671 }
2672
2673 gdb_put_packet(connection, xml, strlen(xml));
2674
2675 free(xml);
2676 return ERROR_OK;
2677 } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
2678 gdb_connection->noack_mode = 1;
2679 gdb_put_packet(connection, "OK", 2);
2680 return ERROR_OK;
2681 }
2682
2683 gdb_put_packet(connection, "", 0);
2684 return ERROR_OK;
2685 }
2686
2687 static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet, int packet_size)
2688 {
2689 struct gdb_connection *gdb_connection = connection->priv;
2690 struct target *target = get_target_from_connection(connection);
2691 const char *parse = packet;
2692 int retval;
2693
2694 /* query for vCont supported */
2695 if (parse[0] == '?') {
2696 if (target->type->step != NULL) {
2697 /* gdb doesn't accept c without C and s without S */
2698 gdb_put_packet(connection, "vCont;c;C;s;S", 13);
2699 return true;
2700 }
2701 return false;
2702 }
2703
2704 if (parse[0] == ';') {
2705 ++parse;
2706 --packet_size;
2707 }
2708
2709 /* simple case, a continue packet */
2710 if (parse[0] == 'c') {
2711 gdb_running_type = 'c';
2712 LOG_DEBUG("target %s continue", target_name(target));
2713 log_add_callback(gdb_log_callback, connection);
2714 retval = target_resume(target, 1, 0, 0, 0);
2715 if (retval == ERROR_TARGET_NOT_HALTED)
2716 LOG_INFO("target %s was not halted when resume was requested", target_name(target));
2717
2718 /* poll target in an attempt to make its internal state consistent */
2719 if (retval != ERROR_OK) {
2720 retval = target_poll(target);
2721 if (retval != ERROR_OK)
2722 LOG_DEBUG("error polling target %s after failed resume", target_name(target));
2723 }
2724
2725 /*
2726 * We don't report errors to gdb here, move frontend_state to
2727 * TARGET_RUNNING to stay in sync with gdb's expectation of the
2728 * target state
2729 */
2730 gdb_connection->frontend_state = TARGET_RUNNING;
2731 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2732
2733 return true;
2734 }
2735
2736 /* single-step or step-over-breakpoint */
2737 if (parse[0] == 's') {
2738 gdb_running_type = 's';
2739 bool fake_step = false;
2740
2741 if (strncmp(parse, "s:", 2) == 0) {
2742 struct target *ct = target;
2743 int current_pc = 1;
2744 int64_t thread_id;
2745 char *endp;
2746
2747 parse += 2;
2748 packet_size -= 2;
2749
2750 thread_id = strtoll(parse, &endp, 16);
2751 if (endp != NULL) {
2752 packet_size -= endp - parse;
2753 parse = endp;
2754 }
2755
2756 if (target->rtos != NULL) {
2757 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
2758 rtos_update_threads(target);
2759
2760 target->rtos->gdb_target_for_threadid(connection, thread_id, &ct);
2761
2762 /*
2763 * check if the thread to be stepped is the current rtos thread
2764 * if not, we must fake the step
2765 */
2766 if (target->rtos->current_thread != thread_id)
2767 fake_step = true;
2768 }
2769
2770 if (parse[0] == ';') {
2771 ++parse;
2772 --packet_size;
2773
2774 if (parse[0] == 'c') {
2775 parse += 1;
2776 packet_size -= 1;
2777
2778 /* check if thread-id follows */
2779 if (parse[0] == ':') {
2780 int64_t tid;
2781 parse += 1;
2782 packet_size -= 1;
2783
2784 tid = strtoll(parse, &endp, 16);
2785 if (tid == thread_id) {
2786 /*
2787 * Special case: only step a single thread (core),
2788 * keep the other threads halted. Currently, only
2789 * aarch64 target understands it. Other target types don't
2790 * care (nobody checks the actual value of 'current')
2791 * and it doesn't really matter. This deserves
2792 * a symbolic constant and a formal interface documentation
2793 * at a later time.
2794 */
2795 LOG_DEBUG("request to step current core only");
2796 /* uncomment after checking that indeed other targets are safe */
2797 /*current_pc = 2;*/
2798 }
2799 }
2800 }
2801 }
2802
2803 LOG_DEBUG("target %s single-step thread %"PRIx64, target_name(ct), thread_id);
2804 log_add_callback(gdb_log_callback, connection);
2805 target_call_event_callbacks(ct, TARGET_EVENT_GDB_START);
2806
2807 /*
2808 * work around an annoying gdb behaviour: when the current thread
2809 * is changed in gdb, it assumes that the target can follow and also
2810 * make the thread current. This is an assumption that cannot hold
2811 * for a real target running a multi-threading OS. We just fake
2812 * the step to not trigger an internal error in gdb. See
2813 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
2814 */
2815 if (fake_step) {
2816 int sig_reply_len;
2817 char sig_reply[128];
2818
2819 LOG_DEBUG("fake step thread %"PRIx64, thread_id);
2820
2821 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply),
2822 "T05thread:%016"PRIx64";", thread_id);
2823
2824 gdb_put_packet(connection, sig_reply, sig_reply_len);
2825 log_remove_callback(gdb_log_callback, connection);
2826
2827 return true;
2828 }
2829
2830 /* support for gdb_sync command */
2831 if (gdb_connection->sync) {
2832 gdb_connection->sync = false;
2833 if (ct->state == TARGET_HALTED) {
2834 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
2835 "from the target.");
2836 gdb_sig_halted(connection);
2837 log_remove_callback(gdb_log_callback, connection);
2838 } else
2839 gdb_connection->frontend_state = TARGET_RUNNING;
2840 return true;
2841 }
2842
2843 retval = target_step(ct, current_pc, 0, 0);
2844 if (retval == ERROR_TARGET_NOT_HALTED)
2845 LOG_INFO("target %s was not halted when step was requested", target_name(ct));
2846
2847 /* if step was successful send a reply back to gdb */
2848 if (retval == ERROR_OK) {
2849 retval = target_poll(ct);
2850 if (retval != ERROR_OK)
2851 LOG_DEBUG("error polling target %s after successful step", target_name(ct));
2852 /* send back signal information */
2853 gdb_signal_reply(ct, connection);
2854 /* stop forwarding log packets! */
2855 log_remove_callback(gdb_log_callback, connection);
2856 } else
2857 gdb_connection->frontend_state = TARGET_RUNNING;
2858 } else {
2859 LOG_ERROR("Unknown vCont packet");
2860 return false;
2861 }
2862 return true;
2863 }
2864
2865 return false;
2866 }
2867
2868 static int gdb_v_packet(struct connection *connection,
2869 char const *packet, int packet_size)
2870 {
2871 struct gdb_connection *gdb_connection = connection->priv;
2872 struct target *target;
2873 int result;
2874
2875 target = get_target_from_connection(connection);
2876
2877 if (strncmp(packet, "vCont", 5) == 0) {
2878 bool handled;
2879
2880 packet += 5;
2881 packet_size -= 5;
2882
2883 handled = gdb_handle_vcont_packet(connection, packet, packet_size);
2884 if (!handled)
2885 gdb_put_packet(connection, "", 0);
2886
2887 return ERROR_OK;
2888 }
2889
2890 /* if flash programming disabled - send a empty reply */
2891
2892 if (gdb_flash_program == 0) {
2893 gdb_put_packet(connection, "", 0);
2894 return ERROR_OK;
2895 }
2896
2897 if (strncmp(packet, "vFlashErase:", 12) == 0) {
2898 unsigned long addr;
2899 unsigned long length;
2900
2901 char const *parse = packet + 12;
2902 if (*parse == '\0') {
2903 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2904 return ERROR_SERVER_REMOTE_CLOSED;
2905 }
2906
2907 addr = strtoul(parse, (char **)&parse, 16);
2908
2909 if (*(parse++) != ',' || *parse == '\0') {
2910 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2911 return ERROR_SERVER_REMOTE_CLOSED;
2912 }
2913
2914 length = strtoul(parse, (char **)&parse, 16);
2915
2916 if (*parse != '\0') {
2917 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2918 return ERROR_SERVER_REMOTE_CLOSED;
2919 }
2920
2921 /* assume all sectors need erasing - stops any problems
2922 * when flash_write is called multiple times */
2923 flash_set_dirty();
2924
2925 /* perform any target specific operations before the erase */
2926 target_call_event_callbacks(target,
2927 TARGET_EVENT_GDB_FLASH_ERASE_START);
2928
2929 /* vFlashErase:addr,length messages require region start and
2930 * end to be "block" aligned ... if padding is ever needed,
2931 * GDB will have become dangerously confused.
2932 */
2933 result = flash_erase_address_range(target, false, addr,
2934 length);
2935
2936 /* perform any target specific operations after the erase */
2937 target_call_event_callbacks(target,
2938 TARGET_EVENT_GDB_FLASH_ERASE_END);
2939
2940 /* perform erase */
2941 if (result != ERROR_OK) {
2942 /* GDB doesn't evaluate the actual error number returned,
2943 * treat a failed erase as an I/O error
2944 */
2945 gdb_send_error(connection, EIO);
2946 LOG_ERROR("flash_erase returned %i", result);
2947 } else
2948 gdb_put_packet(connection, "OK", 2);
2949
2950 return ERROR_OK;
2951 }
2952
2953 if (strncmp(packet, "vFlashWrite:", 12) == 0) {
2954 int retval;
2955 unsigned long addr;
2956 unsigned long length;
2957 char const *parse = packet + 12;
2958
2959 if (*parse == '\0') {
2960 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2961 return ERROR_SERVER_REMOTE_CLOSED;
2962 }
2963 addr = strtoul(parse, (char **)&parse, 16);
2964 if (*(parse++) != ':') {
2965 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2966 return ERROR_SERVER_REMOTE_CLOSED;
2967 }
2968 length = packet_size - (parse - packet);
2969
2970 /* create a new image if there isn't already one */
2971 if (gdb_connection->vflash_image == NULL) {
2972 gdb_connection->vflash_image = malloc(sizeof(struct image));
2973 image_open(gdb_connection->vflash_image, "", "build");
2974 }
2975
2976 /* create new section with content from packet buffer */
2977 retval = image_add_section(gdb_connection->vflash_image,
2978 addr, length, 0x0, (uint8_t const *)parse);
2979 if (retval != ERROR_OK)
2980 return retval;
2981
2982 gdb_put_packet(connection, "OK", 2);
2983
2984 return ERROR_OK;
2985 }
2986
2987 if (strncmp(packet, "vFlashDone", 10) == 0) {
2988 uint32_t written;
2989
2990 /* process the flashing buffer. No need to erase as GDB
2991 * always issues a vFlashErase first. */
2992 target_call_event_callbacks(target,
2993 TARGET_EVENT_GDB_FLASH_WRITE_START);
2994 result = flash_write(target, gdb_connection->vflash_image,
2995 &written, 0);
2996 target_call_event_callbacks(target,
2997 TARGET_EVENT_GDB_FLASH_WRITE_END);
2998 if (result != ERROR_OK) {
2999 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
3000 gdb_put_packet(connection, "E.memtype", 9);
3001 else
3002 gdb_send_error(connection, EIO);
3003 } else {
3004 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
3005 gdb_put_packet(connection, "OK", 2);
3006 }
3007
3008 image_close(gdb_connection->vflash_image);
3009 free(gdb_connection->vflash_image);
3010 gdb_connection->vflash_image = NULL;
3011
3012 return ERROR_OK;
3013 }
3014
3015 gdb_put_packet(connection, "", 0);
3016 return ERROR_OK;
3017 }
3018
3019 static int gdb_detach(struct connection *connection)
3020 {
3021 /*
3022 * Only reply "OK" to GDB
3023 * it will close the connection and this will trigger a call to
3024 * gdb_connection_closed() that will in turn trigger the event
3025 * TARGET_EVENT_GDB_DETACH
3026 */
3027 return gdb_put_packet(connection, "OK", 2);
3028 }
3029
3030 /* The format of 'F' response packet is
3031 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3032 */
3033 static int gdb_fileio_response_packet(struct connection *connection,
3034 char const *packet, int packet_size)
3035 {
3036 struct target *target = get_target_from_connection(connection);
3037 char *separator;
3038 char *parsing_point;
3039 int fileio_retcode = strtoul(packet + 1, &separator, 16);
3040 int fileio_errno = 0;
3041 bool fileio_ctrl_c = false;
3042 int retval;
3043
3044 LOG_DEBUG("-");
3045
3046 if (*separator == ',') {
3047 parsing_point = separator + 1;
3048 fileio_errno = strtoul(parsing_point, &separator, 16);
3049 if (*separator == ',') {
3050 if (*(separator + 1) == 'C') {
3051 /* TODO: process ctrl-c */
3052 fileio_ctrl_c = true;
3053 }
3054 }
3055 }
3056
3057 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3058 fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false");
3059
3060 retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c);
3061 if (retval != ERROR_OK)
3062 return ERROR_FAIL;
3063
3064 /* After File-I/O ends, keep continue or step */
3065 if (gdb_running_type == 'c')
3066 retval = target_resume(target, 1, 0x0, 0, 0);
3067 else if (gdb_running_type == 's')
3068 retval = target_step(target, 1, 0x0, 0);
3069 else
3070 retval = ERROR_FAIL;
3071
3072 if (retval != ERROR_OK)
3073 return ERROR_FAIL;
3074
3075 return ERROR_OK;
3076 }
3077
3078 static void gdb_log_callback(void *priv, const char *file, unsigned line,
3079 const char *function, const char *string)
3080 {
3081 struct connection *connection = priv;
3082 struct gdb_connection *gdb_con = connection->priv;
3083
3084 if (gdb_con->busy) {
3085 /* do not reply this using the O packet */
3086 return;
3087 }
3088
3089 gdb_output_con(connection, string);
3090 }
3091
3092 static void gdb_sig_halted(struct connection *connection)
3093 {
3094 char sig_reply[4];
3095 snprintf(sig_reply, 4, "T%2.2x", 2);
3096 gdb_put_packet(connection, sig_reply, 3);
3097 }
3098
3099 static int gdb_input_inner(struct connection *connection)
3100 {
3101 /* Do not allocate this on the stack */
3102 static char gdb_packet_buffer[GDB_BUFFER_SIZE];
3103
3104 struct target *target;
3105 char const *packet = gdb_packet_buffer;
3106 int packet_size;
3107 int retval;
3108 struct gdb_connection *gdb_con = connection->priv;
3109 static int extended_protocol;
3110
3111 target = get_target_from_connection(connection);
3112
3113 /* drain input buffer. If one of the packets fail, then an error
3114 * packet is replied, if applicable.
3115 *
3116 * This loop will terminate and the error code is returned.
3117 *
3118 * The calling fn will check if this error is something that
3119 * can be recovered from, or if the connection must be closed.
3120 *
3121 * If the error is recoverable, this fn is called again to
3122 * drain the rest of the buffer.
3123 */
3124 do {
3125 packet_size = GDB_BUFFER_SIZE-1;
3126 retval = gdb_get_packet(connection, gdb_packet_buffer, &packet_size);
3127 if (retval != ERROR_OK)
3128 return retval;
3129
3130 /* terminate with zero */
3131 gdb_packet_buffer[packet_size] = '\0';
3132
3133 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
3134 if (packet[0] == 'X') {
3135 /* binary packets spew junk into the debug log stream */
3136 char buf[50];
3137 int x;
3138 for (x = 0; (x < 49) && (packet[x] != ':'); x++)
3139 buf[x] = packet[x];
3140 buf[x] = 0;
3141 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
3142 } else
3143 LOG_DEBUG("received packet: '%s'", packet);
3144 }
3145
3146 if (packet_size > 0) {
3147 retval = ERROR_OK;
3148 switch (packet[0]) {
3149 case 'T': /* Is thread alive? */
3150 gdb_thread_packet(connection, packet, packet_size);
3151 break;
3152 case 'H': /* Set current thread ( 'c' for step and continue,
3153 * 'g' for all other operations ) */
3154 gdb_thread_packet(connection, packet, packet_size);
3155 break;
3156 case 'q':
3157 case 'Q':
3158 retval = gdb_thread_packet(connection, packet, packet_size);
3159 if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
3160 retval = gdb_query_packet(connection, packet, packet_size);
3161 break;
3162 case 'g':
3163 retval = gdb_get_registers_packet(connection, packet, packet_size);
3164 break;
3165 case 'G':
3166 retval = gdb_set_registers_packet(connection, packet, packet_size);
3167 break;
3168 case 'p':
3169 retval = gdb_get_register_packet(connection, packet, packet_size);
3170 break;
3171 case 'P':
3172 retval = gdb_set_register_packet(connection, packet, packet_size);
3173 break;
3174 case 'm':
3175 retval = gdb_read_memory_packet(connection, packet, packet_size);
3176 break;
3177 case 'M':
3178 retval = gdb_write_memory_packet(connection, packet, packet_size);
3179 break;
3180 case 'z':
3181 case 'Z':
3182 retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
3183 break;
3184 case '?':
3185 gdb_last_signal_packet(connection, packet, packet_size);
3186 break;
3187 case 'c':
3188 case 's':
3189 {
3190 gdb_thread_packet(connection, packet, packet_size);
3191 log_add_callback(gdb_log_callback, connection);
3192
3193 if (gdb_con->mem_write_error) {
3194 LOG_ERROR("Memory write failure!");
3195
3196 /* now that we have reported the memory write error,
3197 * we can clear the condition */
3198 gdb_con->mem_write_error = false;
3199 }
3200
3201 bool nostep = false;
3202 bool already_running = false;
3203 if (target->state == TARGET_RUNNING) {
3204 LOG_WARNING("WARNING! The target is already running. "
3205 "All changes GDB did to registers will be discarded! "
3206 "Waiting for target to halt.");
3207 already_running = true;
3208 } else if (target->state != TARGET_HALTED) {
3209 LOG_WARNING("The target is not in the halted nor running stated, " \
3210 "stepi/continue ignored.");
3211 nostep = true;
3212 } else if ((packet[0] == 's') && gdb_con->sync) {
3213 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3214 * sent by GDB first to OpenOCD, thus defeating the check to
3215 * make only the single stepping have the sync feature...
3216 */
3217 nostep = true;
3218 LOG_WARNING("stepi ignored. GDB will now fetch the register state " \
3219 "from the target.");
3220 }
3221 gdb_con->sync = false;
3222
3223 if (!already_running && nostep) {
3224 /* Either the target isn't in the halted state, then we can't
3225 * step/continue. This might be early setup, etc.
3226 *
3227 * Or we want to allow GDB to pick up a fresh set of
3228 * register values without modifying the target state.
3229 *
3230 */
3231 gdb_sig_halted(connection);
3232
3233 /* stop forwarding log packets! */
3234 log_remove_callback(gdb_log_callback, connection);
3235 } else {
3236 /* We're running/stepping, in which case we can
3237 * forward log output until the target is halted
3238 */
3239 gdb_con->frontend_state = TARGET_RUNNING;
3240 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
3241
3242 if (!already_running) {
3243 /* Here we don't want packet processing to stop even if this fails,
3244 * so we use a local variable instead of retval. */
3245 retval = gdb_step_continue_packet(connection, packet, packet_size);
3246 if (retval != ERROR_OK) {
3247 /* we'll never receive a halted
3248 * condition... issue a false one..
3249 */
3250 gdb_frontend_halted(target, connection);
3251 }
3252 }
3253 }
3254 }
3255 break;
3256 case 'v':
3257 retval = gdb_v_packet(connection, packet, packet_size);
3258 break;
3259 case 'D':
3260 retval = gdb_detach(connection);
3261 extended_protocol = 0;
3262 break;
3263 case 'X':
3264 retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
3265 if (retval != ERROR_OK)
3266 return retval;
3267 break;
3268 case 'k':
3269 if (extended_protocol != 0) {
3270 gdb_con->attached = false;
3271 break;
3272 }
3273 gdb_put_packet(connection, "OK", 2);
3274 return ERROR_SERVER_REMOTE_CLOSED;
3275 case '!':
3276 /* handle extended remote protocol */
3277 extended_protocol = 1;
3278 gdb_put_packet(connection, "OK", 2);
3279 break;
3280 case 'R':
3281 /* handle extended restart packet */
3282 breakpoint_clear_target(target);
3283 watchpoint_clear_target(target);
3284 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
3285 target_name(target));
3286 /* set connection as attached after reset */
3287 gdb_con->attached = true;
3288 /* info rtos parts */
3289 gdb_thread_packet(connection, packet, packet_size);
3290 break;
3291
3292 case 'j':
3293 /* packet supported only by smp target i.e cortex_a.c*/
3294 /* handle smp packet replying coreid played to gbd */
3295 gdb_read_smp_packet(connection, packet, packet_size);
3296 break;
3297
3298 case 'J':
3299 /* packet supported only by smp target i.e cortex_a.c */
3300 /* handle smp packet setting coreid to be played at next
3301 * resume to gdb */
3302 gdb_write_smp_packet(connection, packet, packet_size);
3303 break;
3304
3305 case 'F':
3306 /* File-I/O extension */
3307 /* After gdb uses host-side syscall to complete target file
3308 * I/O, gdb sends host-side syscall return value to target
3309 * by 'F' packet.
3310 * The format of 'F' response packet is
3311 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3312 */
3313 gdb_con->frontend_state = TARGET_RUNNING;
3314 log_add_callback(gdb_log_callback, connection);
3315 gdb_fileio_response_packet(connection, packet, packet_size);
3316 break;
3317
3318 default:
3319 /* ignore unknown packets */
3320 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3321 gdb_put_packet(connection, NULL, 0);
3322 break;
3323 }
3324
3325 /* if a packet handler returned an error, exit input loop */
3326 if (retval != ERROR_OK)
3327 return retval;
3328 }
3329
3330 if (gdb_con->ctrl_c) {
3331 if (target->state == TARGET_RUNNING) {
3332 struct target *t = target;
3333 if (target->rtos)
3334 target->rtos->gdb_target_for_threadid(connection, target->rtos->current_threadid, &t);
3335 retval = target_halt(t);
3336 if (retval == ERROR_OK)
3337 retval = target_poll(t);
3338 if (retval != ERROR_OK)
3339 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
3340 gdb_con->ctrl_c = 0;
3341 } else {
3342 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3343 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
3344 }
3345 }
3346
3347 } while (gdb_con->buf_cnt > 0);
3348
3349 return ERROR_OK;
3350 }
3351
3352 static int gdb_input(struct connection *connection)
3353 {
3354 int retval = gdb_input_inner(connection);
3355 struct gdb_connection *gdb_con = connection->priv;
3356 if (retval == ERROR_SERVER_REMOTE_CLOSED)
3357 return retval;
3358
3359 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3360 if (gdb_con->closed)
3361 return ERROR_SERVER_REMOTE_CLOSED;
3362
3363 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3364 return ERROR_OK;
3365 }
3366
3367 static int gdb_target_start(struct target *target, const char *port)
3368 {
3369 struct gdb_service *gdb_service;
3370 int ret;
3371 gdb_service = malloc(sizeof(struct gdb_service));
3372
3373 if (NULL == gdb_service)
3374 return -ENOMEM;
3375
3376 gdb_service->target = target;
3377 gdb_service->core[0] = -1;
3378 gdb_service->core[1] = -1;
3379 target->gdb_service = gdb_service;
3380
3381 ret = add_service("gdb",
3382 port, 1, &gdb_new_connection, &gdb_input,
3383 &gdb_connection_closed, gdb_service);
3384 /* initialialize all targets gdb service with the same pointer */
3385 {
3386 struct target_list *head;
3387 struct target *curr;
3388 head = target->head;
3389 while (head != (struct target_list *)NULL) {
3390 curr = head->target;
3391 if (curr != target)
3392 curr->gdb_service = gdb_service;
3393 head = head->next;
3394 }
3395 }
3396 return ret;
3397 }
3398
3399 static int gdb_target_add_one(struct target *target)
3400 {
3401 if (strcmp(gdb_port, "disabled") == 0) {
3402 LOG_INFO("gdb port disabled");
3403 return ERROR_OK;
3404 }
3405
3406 /* one gdb instance per smp list */
3407 if ((target->smp) && (target->gdb_service))
3408 return ERROR_OK;
3409 int retval = gdb_target_start(target, gdb_port_next);
3410 if (retval == ERROR_OK) {
3411 long portnumber;
3412 /* If we can parse the port number
3413 * then we increment the port number for the next target.
3414 */
3415 char *end;
3416 portnumber = strtol(gdb_port_next, &end, 0);
3417 if (!*end) {
3418 if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
3419 free(gdb_port_next);
3420 if (portnumber) {
3421 gdb_port_next = alloc_printf("%d", portnumber+1);
3422 } else {
3423 /* Don't increment if gdb_port is 0, since we're just
3424 * trying to allocate an unused port. */
3425 gdb_port_next = strdup("0");
3426 }
3427 }
3428 }
3429 }
3430 return retval;
3431 }
3432
3433 int gdb_target_add_all(struct target *target)
3434 {
3435 if (strcmp(gdb_port, "disabled") == 0) {
3436 LOG_INFO("gdb server disabled");
3437 return ERROR_OK;
3438 }
3439
3440 if (NULL == target) {
3441 LOG_WARNING("gdb services need one or more targets defined");
3442 return ERROR_OK;
3443 }
3444
3445 while (NULL != target) {
3446 int retval = gdb_target_add_one(target);
3447 if (ERROR_OK != retval)
3448 return retval;
3449
3450 target = target->next;
3451 }
3452
3453 return ERROR_OK;
3454 }
3455
3456 COMMAND_HANDLER(handle_gdb_sync_command)
3457 {
3458 if (CMD_ARGC != 0)
3459 return ERROR_COMMAND_SYNTAX_ERROR;
3460
3461 if (current_gdb_connection == NULL) {
3462 command_print(CMD_CTX,
3463 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3464 return ERROR_FAIL;
3465 }
3466
3467 current_gdb_connection->sync = true;
3468
3469 return ERROR_OK;
3470 }
3471
3472 /* daemon configuration command gdb_port */
3473 COMMAND_HANDLER(handle_gdb_port_command)
3474 {
3475 int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
3476 if (ERROR_OK == retval) {
3477 free(gdb_port_next);
3478 gdb_port_next = strdup(gdb_port);
3479 }
3480 return retval;
3481 }
3482
3483 COMMAND_HANDLER(handle_gdb_memory_map_command)
3484 {
3485 if (CMD_ARGC != 1)
3486 return ERROR_COMMAND_SYNTAX_ERROR;
3487
3488 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
3489 return ERROR_OK;
3490 }
3491
3492 COMMAND_HANDLER(handle_gdb_flash_program_command)
3493 {
3494 if (CMD_ARGC != 1)
3495 return ERROR_COMMAND_SYNTAX_ERROR;
3496
3497 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
3498 return ERROR_OK;
3499 }
3500
3501 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
3502 {
3503 if (CMD_ARGC != 1)
3504 return ERROR_COMMAND_SYNTAX_ERROR;
3505
3506 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
3507 return ERROR_OK;
3508 }
3509
3510 COMMAND_HANDLER(handle_gdb_report_register_access_error)
3511 {
3512 if (CMD_ARGC != 1)
3513 return ERROR_COMMAND_SYNTAX_ERROR;
3514
3515 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_register_access_error);
3516 return ERROR_OK;
3517 }
3518
3519 /* gdb_breakpoint_override */
3520 COMMAND_HANDLER(handle_gdb_breakp