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

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)