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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)