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

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)