server/gdb-server: fix type error.
[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 /* send ACK to GDB for debug request */
1005 gdb_write(connection, "+", 1);
1006
1007 /* output goes through gdb connection */
1008 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
1009
1010 /* we must remove all breakpoints registered to the target as a previous
1011 * GDB session could leave dangling breakpoints if e.g. communication
1012 * timed out.
1013 */
1014 breakpoint_clear_target(target);
1015 watchpoint_clear_target(target);
1016
1017 /* Since version 3.95 (gdb-19990504), with the exclusion of 6.5~6.8, GDB
1018 * sends an ACK at connection with the following comment in its source code:
1019 * "Ack any packet which the remote side has already sent."
1020 * LLDB does the same since the first gdb-remote implementation.
1021 * Remove the initial ACK from the incoming buffer.
1022 */
1023 retval = gdb_get_char(connection, &initial_ack);
1024 if (retval != ERROR_OK)
1025 return retval;
1026
1027 if (initial_ack != '+')
1028 gdb_putback_char(connection, initial_ack);
1029
1030 target_call_event_callbacks(target, TARGET_EVENT_GDB_ATTACH);
1031
1032 if (target->rtos) {
1033 /* clean previous rtos session if supported*/
1034 if (target->rtos->type->clean)
1035 target->rtos->type->clean(target);
1036
1037 /* update threads */
1038 rtos_update_threads(target);
1039 }
1040
1041 if (gdb_use_memory_map) {
1042 /* Connect must fail if the memory map can't be set up correctly.
1043 *
1044 * This will cause an auto_probe to be invoked, which is either
1045 * a no-op or it will fail when the target isn't ready(e.g. not halted).
1046 */
1047 for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
1048 struct flash_bank *p;
1049 p = get_flash_bank_by_num_noprobe(i);
1050 if (p->target != target)
1051 continue;
1052 retval = get_flash_bank_by_num(i, &p);
1053 if (retval != ERROR_OK) {
1054 LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target "
1055 "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1056 return retval;
1057 }
1058 }
1059 }
1060
1061 log_printf_lf(all_targets->next ? LOG_LVL_INFO : LOG_LVL_DEBUG,
1062 __FILE__, __LINE__, __func__,
1063 "New GDB Connection: %d, Target %s, state: %s",
1064 gdb_connection->unique_index,
1065 target_name(target),
1066 target_state_name(target));
1067
1068 if (!target_was_examined(target)) {
1069 LOG_ERROR("Target %s not examined yet, refuse gdb connection %d!",
1070 target_name(target), gdb_connection->unique_index);
1071 return ERROR_TARGET_NOT_EXAMINED;
1072 }
1073 gdb_actual_connections++;
1074
1075 if (target->state != TARGET_HALTED)
1076 LOG_WARNING("GDB connection %d on target %s not halted",
1077 gdb_actual_connections, target_name(target));
1078
1079 /* DANGER! If we fail subsequently, we must remove this handler,
1080 * otherwise we occasionally see crashes as the timer can invoke the
1081 * callback fn.
1082 *
1083 * register callback to be informed about target events */
1084 target_register_event_callback(gdb_target_callback_event_handler, connection);
1085
1086 log_add_callback(gdb_log_callback, connection);
1087
1088 return ERROR_OK;
1089 }
1090
1091 static int gdb_connection_closed(struct connection *connection)
1092 {
1093 struct target *target;
1094 struct gdb_connection *gdb_connection = connection->priv;
1095
1096 target = get_target_from_connection(connection);
1097
1098 /* we're done forwarding messages. Tear down callback before
1099 * cleaning up connection.
1100 */
1101 log_remove_callback(gdb_log_callback, connection);
1102
1103 gdb_actual_connections--;
1104 LOG_DEBUG("{%d} GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1105 gdb_connection->unique_index,
1106 target_name(target),
1107 target_state_name(target),
1108 gdb_actual_connections);
1109
1110 /* see if an image built with vFlash commands is left */
1111 if (gdb_connection->vflash_image) {
1112 image_close(gdb_connection->vflash_image);
1113 free(gdb_connection->vflash_image);
1114 gdb_connection->vflash_image = NULL;
1115 }
1116
1117 /* if this connection registered a debug-message receiver delete it */
1118 delete_debug_msg_receiver(connection->cmd_ctx, target);
1119
1120 free(connection->priv);
1121 connection->priv = NULL;
1122
1123 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
1124
1125 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
1126
1127 target_call_event_callbacks(target, TARGET_EVENT_GDB_DETACH);
1128
1129 return ERROR_OK;
1130 }
1131
1132 static void gdb_send_error(struct connection *connection, uint8_t the_error)
1133 {
1134 char err[4];
1135 snprintf(err, 4, "E%2.2X", the_error);
1136 gdb_put_packet(connection, err, 3);
1137 }
1138
1139 static int gdb_last_signal_packet(struct connection *connection,
1140 char const *packet, int packet_size)
1141 {
1142 struct target *target = get_target_from_connection(connection);
1143 struct gdb_connection *gdb_con = connection->priv;
1144 char sig_reply[4];
1145 int signal_var;
1146
1147 if (!gdb_con->attached) {
1148 /* if we are here we have received a kill packet
1149 * reply W stop reply otherwise gdb gets very unhappy */
1150 gdb_put_packet(connection, "W00", 3);
1151 return ERROR_OK;
1152 }
1153
1154 signal_var = gdb_last_signal(target);
1155
1156 snprintf(sig_reply, 4, "S%2.2x", signal_var);
1157 gdb_put_packet(connection, sig_reply, 3);
1158
1159 return ERROR_OK;
1160 }
1161
1162 static inline int gdb_reg_pos(struct target *target, int pos, int len)
1163 {
1164 if (target->endianness == TARGET_LITTLE_ENDIAN)
1165 return pos;
1166 else
1167 return len - 1 - pos;
1168 }
1169
1170 /* Convert register to string of bytes. NB! The # of bits in the
1171 * register might be non-divisible by 8(a byte), in which
1172 * case an entire byte is shown.
1173 *
1174 * NB! the format on the wire is the target endianness
1175 *
1176 * The format of reg->value is little endian
1177 *
1178 */
1179 static void gdb_str_to_target(struct target *target,
1180 char *tstr, struct reg *reg)
1181 {
1182 int i;
1183
1184 uint8_t *buf;
1185 int buf_len;
1186 buf = reg->value;
1187 buf_len = DIV_ROUND_UP(reg->size, 8);
1188
1189 for (i = 0; i < buf_len; i++) {
1190 int j = gdb_reg_pos(target, i, buf_len);
1191 tstr += sprintf(tstr, "%02x", buf[j]);
1192 }
1193 }
1194
1195 /* copy over in register buffer */
1196 static void gdb_target_to_reg(struct target *target,
1197 char const *tstr, int str_len, uint8_t *bin)
1198 {
1199 if (str_len % 2) {
1200 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1201 exit(-1);
1202 }
1203
1204 int i;
1205 for (i = 0; i < str_len; i += 2) {
1206 unsigned t;
1207 if (sscanf(tstr + i, "%02x", &t) != 1) {
1208 LOG_ERROR("BUG: unable to convert register value");
1209 exit(-1);
1210 }
1211
1212 int j = gdb_reg_pos(target, i/2, str_len/2);
1213 bin[j] = t;
1214 }
1215 }
1216
1217 /* get register value if needed and fill the buffer accordingly */
1218 static int gdb_get_reg_value_as_str(struct target *target, char *tstr, struct reg *reg)
1219 {
1220 int retval = ERROR_OK;
1221
1222 if (!reg->valid)
1223 retval = reg->type->get(reg);
1224
1225 const unsigned int len = DIV_ROUND_UP(reg->size, 8) * 2;
1226 switch (retval) {
1227 case ERROR_OK:
1228 gdb_str_to_target(target, tstr, reg);
1229 return ERROR_OK;
1230 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1231 memset(tstr, 'x', len);
1232 tstr[len] = '\0';
1233 return ERROR_OK;
1234 }
1235 return ERROR_FAIL;
1236 }
1237
1238 static int gdb_get_registers_packet(struct connection *connection,
1239 char const *packet, int packet_size)
1240 {
1241 struct target *target = get_target_from_connection(connection);
1242 struct reg **reg_list;
1243 int reg_list_size;
1244 int retval;
1245 int reg_packet_size = 0;
1246 char *reg_packet;
1247 char *reg_packet_p;
1248 int i;
1249
1250 #ifdef _DEBUG_GDB_IO_
1251 LOG_DEBUG("-");
1252 #endif
1253
1254 if ((target->rtos) && (rtos_get_gdb_reg_list(connection) == ERROR_OK))
1255 return ERROR_OK;
1256
1257 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1258 REG_CLASS_GENERAL);
1259 if (retval != ERROR_OK)
1260 return gdb_error(connection, retval);
1261
1262 for (i = 0; i < reg_list_size; i++) {
1263 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
1264 continue;
1265 reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1266 }
1267
1268 assert(reg_packet_size > 0);
1269
1270 reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
1271 if (!reg_packet)
1272 return ERROR_FAIL;
1273
1274 reg_packet_p = reg_packet;
1275
1276 for (i = 0; i < reg_list_size; i++) {
1277 if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
1278 continue;
1279 if (gdb_get_reg_value_as_str(target, reg_packet_p, reg_list[i]) != ERROR_OK) {
1280 free(reg_packet);
1281 free(reg_list);
1282 return gdb_error(connection, retval);
1283 }
1284 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1285 }
1286
1287 #ifdef _DEBUG_GDB_IO_
1288 {
1289 char *reg_packet_p_debug;
1290 reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1291 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1292 free(reg_packet_p_debug);
1293 }
1294 #endif
1295
1296 gdb_put_packet(connection, reg_packet, reg_packet_size);
1297 free(reg_packet);
1298
1299 free(reg_list);
1300
1301 return ERROR_OK;
1302 }
1303
1304 static int gdb_set_registers_packet(struct connection *connection,
1305 char const *packet, int packet_size)
1306 {
1307 struct target *target = get_target_from_connection(connection);
1308 int i;
1309 struct reg **reg_list;
1310 int reg_list_size;
1311 int retval;
1312 char const *packet_p;
1313
1314 #ifdef _DEBUG_GDB_IO_
1315 LOG_DEBUG("-");
1316 #endif
1317
1318 /* skip command character */
1319 packet++;
1320 packet_size--;
1321
1322 if (packet_size % 2) {
1323 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1324 return ERROR_SERVER_REMOTE_CLOSED;
1325 }
1326
1327 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1328 REG_CLASS_GENERAL);
1329 if (retval != ERROR_OK)
1330 return gdb_error(connection, retval);
1331
1332 packet_p = packet;
1333 for (i = 0; i < reg_list_size; i++) {
1334 uint8_t *bin_buf;
1335 if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden)
1336 continue;
1337 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1338
1339 if (packet_p + chars > packet + packet_size)
1340 LOG_ERROR("BUG: register packet is too small for registers");
1341
1342 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1343 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1344
1345 retval = reg_list[i]->type->set(reg_list[i], bin_buf);
1346 if (retval != ERROR_OK && gdb_report_register_access_error) {
1347 LOG_DEBUG("Couldn't set register %s.", reg_list[i]->name);
1348 free(reg_list);
1349 free(bin_buf);
1350 return gdb_error(connection, retval);
1351 }
1352
1353 /* advance packet pointer */
1354 packet_p += chars;
1355
1356 free(bin_buf);
1357 }
1358
1359 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1360 free(reg_list);
1361
1362 gdb_put_packet(connection, "OK", 2);
1363
1364 return ERROR_OK;
1365 }
1366
1367 static int gdb_get_register_packet(struct connection *connection,
1368 char const *packet, int packet_size)
1369 {
1370 struct target *target = get_target_from_connection(connection);
1371 char *reg_packet;
1372 int reg_num = strtoul(packet + 1, NULL, 16);
1373 struct reg **reg_list;
1374 int reg_list_size;
1375 int retval;
1376
1377 #ifdef _DEBUG_GDB_IO_
1378 LOG_DEBUG("-");
1379 #endif
1380
1381 if ((target->rtos) && (rtos_get_gdb_reg(connection, reg_num) == ERROR_OK))
1382 return ERROR_OK;
1383
1384 retval = target_get_gdb_reg_list_noread(target, &reg_list, &reg_list_size,
1385 REG_CLASS_ALL);
1386 if (retval != ERROR_OK)
1387 return gdb_error(connection, retval);
1388
1389 if ((reg_list_size <= reg_num) || !reg_list[reg_num] ||
1390 !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) {
1391 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num);
1392 return ERROR_SERVER_REMOTE_CLOSED;
1393 }
1394
1395 reg_packet = calloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1, 1); /* plus one for string termination null */
1396
1397 if (gdb_get_reg_value_as_str(target, reg_packet, reg_list[reg_num]) != ERROR_OK) {
1398 free(reg_packet);
1399 free(reg_list);
1400 return gdb_error(connection, retval);
1401 }
1402
1403 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1404
1405 free(reg_list);
1406 free(reg_packet);
1407
1408 return ERROR_OK;
1409 }
1410
1411 static int gdb_set_register_packet(struct connection *connection,
1412 char const *packet, int packet_size)
1413 {
1414 struct target *target = get_target_from_connection(connection);
1415 char *separator;
1416 int reg_num = strtoul(packet + 1, &separator, 16);
1417 struct reg **reg_list;
1418 int reg_list_size;
1419 int retval;
1420
1421 #ifdef _DEBUG_GDB_IO_
1422 LOG_DEBUG("-");
1423 #endif
1424
1425 if (*separator != '=') {
1426 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1427 return ERROR_SERVER_REMOTE_CLOSED;
1428 }
1429 size_t chars = strlen(separator + 1);
1430 uint8_t *bin_buf = malloc(chars / 2);
1431 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1432
1433 if ((target->rtos) &&
1434 (rtos_set_reg(connection, reg_num, bin_buf) == ERROR_OK)) {
1435 free(bin_buf);
1436 gdb_put_packet(connection, "OK", 2);
1437 return ERROR_OK;
1438 }
1439
1440 retval = target_get_gdb_reg_list_noread(target, &reg_list, &reg_list_size,
1441 REG_CLASS_ALL);
1442 if (retval != ERROR_OK) {
1443 free(bin_buf);
1444 return gdb_error(connection, retval);
1445 }
1446
1447 if ((reg_list_size <= reg_num) || !reg_list[reg_num] ||
1448 !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) {
1449 LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num);
1450 free(bin_buf);
1451 free(reg_list);
1452 return ERROR_SERVER_REMOTE_CLOSED;
1453 }
1454
1455 if (chars != (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2)) {
1456 LOG_ERROR("gdb sent %zu bits for a %" PRIu32 "-bit register (%s)",
1457 chars * 4, reg_list[reg_num]->size, reg_list[reg_num]->name);
1458 free(bin_buf);
1459 free(reg_list);
1460 return ERROR_SERVER_REMOTE_CLOSED;
1461 }
1462
1463 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1464
1465 retval = reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1466 if (retval != ERROR_OK && gdb_report_register_access_error) {
1467 LOG_DEBUG("Couldn't set register %s.", reg_list[reg_num]->name);
1468 free(bin_buf);
1469 free(reg_list);
1470 return gdb_error(connection, retval);
1471 }
1472
1473 gdb_put_packet(connection, "OK", 2);
1474
1475 free(bin_buf);
1476 free(reg_list);
1477
1478 return ERROR_OK;
1479 }
1480
1481 /* No attempt is made to translate the "retval" to
1482 * GDB speak. This has to be done at the calling
1483 * site as no mapping really exists.
1484 */
1485 static int gdb_error(struct connection *connection, int retval)
1486 {
1487 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1488 gdb_send_error(connection, EFAULT);
1489 return ERROR_OK;
1490 }
1491
1492 /* We don't have to worry about the default 2 second timeout for GDB packets,
1493 * because GDB breaks up large memory reads into smaller reads.
1494 */
1495 static int gdb_read_memory_packet(struct connection *connection,
1496 char const *packet, int packet_size)
1497 {
1498 struct target *target = get_target_from_connection(connection);
1499 char *separator;
1500 uint64_t addr = 0;
1501 uint32_t len = 0;
1502
1503 uint8_t *buffer;
1504 char *hex_buffer;
1505
1506 int retval = ERROR_OK;
1507
1508 /* skip command character */
1509 packet++;
1510
1511 addr = strtoull(packet, &separator, 16);
1512
1513 if (*separator != ',') {
1514 LOG_ERROR("incomplete read memory packet received, dropping connection");
1515 return ERROR_SERVER_REMOTE_CLOSED;
1516 }
1517
1518 len = strtoul(separator + 1, NULL, 16);
1519
1520 if (!len) {
1521 LOG_WARNING("invalid read memory packet received (len == 0)");
1522 gdb_put_packet(connection, "", 0);
1523 return ERROR_OK;
1524 }
1525
1526 buffer = malloc(len);
1527
1528 LOG_DEBUG("addr: 0x%16.16" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1529
1530 retval = ERROR_NOT_IMPLEMENTED;
1531 if (target->rtos)
1532 retval = rtos_read_buffer(target, addr, len, buffer);
1533 if (retval == ERROR_NOT_IMPLEMENTED)
1534 retval = target_read_buffer(target, addr, len, buffer);
1535
1536 if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1537 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1538 * At some point this might be fixed in GDB, in which case this code can be removed.
1539 *
1540 * OpenOCD developers are acutely aware of this problem, but there is nothing
1541 * gained by involving the user in this problem that hopefully will get resolved
1542 * eventually
1543 *
1544 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1545 * cmd = view%20audit-trail&database = gdb&pr = 2395
1546 *
1547 * For now, the default is to fix up things to make current GDB versions work.
1548 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1549 */
1550 memset(buffer, 0, len);
1551 retval = ERROR_OK;
1552 }
1553
1554 if (retval == ERROR_OK) {
1555 hex_buffer = malloc(len * 2 + 1);
1556
1557 size_t pkt_len = hexify(hex_buffer, buffer, len, len * 2 + 1);
1558
1559 gdb_put_packet(connection, hex_buffer, pkt_len);
1560
1561 free(hex_buffer);
1562 } else
1563 retval = gdb_error(connection, retval);
1564
1565 free(buffer);
1566
1567 return retval;
1568 }
1569
1570 static int gdb_write_memory_packet(struct connection *connection,
1571 char const *packet, int packet_size)
1572 {
1573 struct target *target = get_target_from_connection(connection);
1574 char *separator;
1575 uint64_t addr = 0;
1576 uint32_t len = 0;
1577
1578 uint8_t *buffer;
1579 int retval;
1580
1581 /* skip command character */
1582 packet++;
1583
1584 addr = strtoull(packet, &separator, 16);
1585
1586 if (*separator != ',') {
1587 LOG_ERROR("incomplete write memory packet received, dropping connection");
1588 return ERROR_SERVER_REMOTE_CLOSED;
1589 }
1590
1591 len = strtoul(separator + 1, &separator, 16);
1592
1593 if (*(separator++) != ':') {
1594 LOG_ERROR("incomplete write memory packet received, dropping connection");
1595 return ERROR_SERVER_REMOTE_CLOSED;
1596 }
1597
1598 buffer = malloc(len);
1599
1600 LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1601
1602 if (unhexify(buffer, separator, len) != len)
1603 LOG_ERROR("unable to decode memory packet");
1604
1605 retval = ERROR_NOT_IMPLEMENTED;
1606 if (target->rtos)
1607 retval = rtos_write_buffer(target, addr, len, buffer);
1608 if (retval == ERROR_NOT_IMPLEMENTED)
1609 retval = target_write_buffer(target, addr, len, buffer);
1610
1611 if (retval == ERROR_OK)
1612 gdb_put_packet(connection, "OK", 2);
1613 else
1614 retval = gdb_error(connection, retval);
1615
1616 free(buffer);
1617
1618 return retval;
1619 }
1620
1621 static int gdb_write_memory_binary_packet(struct connection *connection,
1622 char const *packet, int packet_size)
1623 {
1624 struct target *target = get_target_from_connection(connection);
1625 char *separator;
1626 uint64_t addr = 0;
1627 uint32_t len = 0;
1628
1629 int retval = ERROR_OK;
1630 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1631 * the assumption that we're in a download and it's important to go as fast
1632 * as possible. */
1633 uint32_t fast_limit = 8;
1634
1635 /* skip command character */
1636 packet++;
1637
1638 addr = strtoull(packet, &separator, 16);
1639
1640 if (*separator != ',') {
1641 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1642 return ERROR_SERVER_REMOTE_CLOSED;
1643 }
1644
1645 len = strtoul(separator + 1, &separator, 16);
1646
1647 if (*(separator++) != ':') {
1648 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1649 return ERROR_SERVER_REMOTE_CLOSED;
1650 }
1651
1652 struct gdb_connection *gdb_connection = connection->priv;
1653
1654 if (gdb_connection->mem_write_error)
1655 retval = ERROR_FAIL;
1656
1657 if (retval == ERROR_OK) {
1658 if (len >= fast_limit) {
1659 /* By replying the packet *immediately* GDB will send us a new packet
1660 * while we write the last one to the target.
1661 * We only do this for larger writes, so that users who do something like:
1662 * p *((int*)0xdeadbeef)=8675309
1663 * will get immediate feedback that that write failed.
1664 */
1665 gdb_put_packet(connection, "OK", 2);
1666 }
1667 } else {
1668 retval = gdb_error(connection, retval);
1669 /* now that we have reported the memory write error, we can clear the condition */
1670 gdb_connection->mem_write_error = false;
1671 if (retval != ERROR_OK)
1672 return retval;
1673 }
1674
1675 if (len) {
1676 LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1677
1678 retval = ERROR_NOT_IMPLEMENTED;
1679 if (target->rtos)
1680 retval = rtos_write_buffer(target, addr, len, (uint8_t *)separator);
1681 if (retval == ERROR_NOT_IMPLEMENTED)
1682 retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1683
1684 if (retval != ERROR_OK)
1685 gdb_connection->mem_write_error = true;
1686 }
1687
1688 if (len < fast_limit) {
1689 if (retval != ERROR_OK) {
1690 gdb_error(connection, retval);
1691 gdb_connection->mem_write_error = false;
1692 } else {
1693 gdb_put_packet(connection, "OK", 2);
1694 }
1695 }
1696
1697 return ERROR_OK;
1698 }
1699
1700 static int gdb_step_continue_packet(struct connection *connection,
1701 char const *packet, int packet_size)
1702 {
1703 struct target *target = get_target_from_connection(connection);
1704 int current = 0;
1705 uint64_t address = 0x0;
1706 int retval = ERROR_OK;
1707
1708 LOG_DEBUG("-");
1709
1710 if (packet_size > 1)
1711 address = strtoull(packet + 1, NULL, 16);
1712 else
1713 current = 1;
1714
1715 gdb_running_type = packet[0];
1716 if (packet[0] == 'c') {
1717 LOG_DEBUG("continue");
1718 /* resume at current address, don't handle breakpoints, not debugging */
1719 retval = target_resume(target, current, address, 0, 0);
1720 } else if (packet[0] == 's') {
1721 LOG_DEBUG("step");
1722 /* step at current or address, don't handle breakpoints */
1723 retval = target_step(target, current, address, 0);
1724 }
1725 return retval;
1726 }
1727
1728 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1729 char const *packet, int packet_size)
1730 {
1731 struct target *target = get_target_from_connection(connection);
1732 int type;
1733 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1734 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1735 uint64_t address;
1736 uint32_t size;
1737 char *separator;
1738 int retval;
1739
1740 LOG_DEBUG("[%s]", target_name(target));
1741
1742 type = strtoul(packet + 1, &separator, 16);
1743
1744 if (type == 0) /* memory breakpoint */
1745 bp_type = BKPT_SOFT;
1746 else if (type == 1) /* hardware breakpoint */
1747 bp_type = BKPT_HARD;
1748 else if (type == 2) /* write watchpoint */
1749 wp_type = WPT_WRITE;
1750 else if (type == 3) /* read watchpoint */
1751 wp_type = WPT_READ;
1752 else if (type == 4) /* access watchpoint */
1753 wp_type = WPT_ACCESS;
1754 else {
1755 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1756 return ERROR_SERVER_REMOTE_CLOSED;
1757 }
1758
1759 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1760 bp_type = gdb_breakpoint_override_type;
1761
1762 if (*separator != ',') {
1763 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1764 return ERROR_SERVER_REMOTE_CLOSED;
1765 }
1766
1767 address = strtoull(separator + 1, &separator, 16);
1768
1769 if (*separator != ',') {
1770 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1771 return ERROR_SERVER_REMOTE_CLOSED;
1772 }
1773
1774 size = strtoul(separator + 1, &separator, 16);
1775
1776 switch (type) {
1777 case 0:
1778 case 1:
1779 if (packet[0] == 'Z') {
1780 retval = breakpoint_add(target, address, size, bp_type);
1781 if (retval == ERROR_NOT_IMPLEMENTED) {
1782 /* Send empty reply to report that breakpoints of this type are not supported */
1783 gdb_put_packet(connection, "", 0);
1784 } else if (retval != ERROR_OK) {
1785 retval = gdb_error(connection, retval);
1786 if (retval != ERROR_OK)
1787 return retval;
1788 } else
1789 gdb_put_packet(connection, "OK", 2);
1790 } else {
1791 breakpoint_remove(target, address);
1792 gdb_put_packet(connection, "OK", 2);
1793 }
1794 break;
1795 case 2:
1796 case 3:
1797 case 4:
1798 {
1799 if (packet[0] == 'Z') {
1800 retval = watchpoint_add(target, address, size, wp_type, 0, WATCHPOINT_IGNORE_DATA_VALUE_MASK);
1801 if (retval == ERROR_NOT_IMPLEMENTED) {
1802 /* Send empty reply to report that watchpoints of this type are not supported */
1803 gdb_put_packet(connection, "", 0);
1804 } else if (retval != ERROR_OK) {
1805 retval = gdb_error(connection, retval);
1806 if (retval != ERROR_OK)
1807 return retval;
1808 } else
1809 gdb_put_packet(connection, "OK", 2);
1810 } else {
1811 watchpoint_remove(target, address);
1812 gdb_put_packet(connection, "OK", 2);
1813 }
1814 break;
1815 }
1816 default:
1817 break;
1818 }
1819
1820 return ERROR_OK;
1821 }
1822
1823 /* print out a string and allocate more space as needed,
1824 * mainly used for XML at this point
1825 */
1826 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6))) void xml_printf(int *retval,
1827 char **xml, int *pos, int *size, const char *fmt, ...)
1828 {
1829 if (*retval != ERROR_OK)
1830 return;
1831 int first = 1;
1832
1833 for (;; ) {
1834 if ((!*xml) || (!first)) {
1835 /* start by 0 to exercise all the code paths.
1836 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1837
1838 *size = *size * 2 + 2;
1839 char *t = *xml;
1840 *xml = realloc(*xml, *size);
1841 if (!*xml) {
1842 free(t);
1843 *retval = ERROR_SERVER_REMOTE_CLOSED;
1844 return;
1845 }
1846 }
1847
1848 va_list ap;
1849 int ret;
1850 va_start(ap, fmt);
1851 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1852 va_end(ap);
1853 if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1854 *pos += ret;
1855 return;
1856 }
1857 /* there was just enough or not enough space, allocate more. */
1858 first = 0;
1859 }
1860 }
1861
1862 static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
1863 {
1864 /* Locate the annex. */
1865 const char *annex_end = strchr(buf, ':');
1866 if (!annex_end)
1867 return ERROR_FAIL;
1868
1869 /* After the read marker and annex, qXfer looks like a
1870 * traditional 'm' packet. */
1871 char *separator;
1872 *ofs = strtoul(annex_end + 1, &separator, 16);
1873
1874 if (*separator != ',')
1875 return ERROR_FAIL;
1876
1877 *len = strtoul(separator + 1, NULL, 16);
1878
1879 /* Extract the annex if needed */
1880 if (annex) {
1881 *annex = strndup(buf, annex_end - buf);
1882 if (!*annex)
1883 return ERROR_FAIL;
1884 }
1885
1886 return ERROR_OK;
1887 }
1888
1889 static int compare_bank(const void *a, const void *b)
1890 {
1891 struct flash_bank *b1, *b2;
1892 b1 = *((struct flash_bank **)a);
1893 b2 = *((struct flash_bank **)b);
1894
1895 if (b1->base == b2->base)
1896 return 0;
1897 else if (b1->base > b2->base)
1898 return 1;
1899 else
1900 return -1;
1901 }
1902
1903 static int gdb_memory_map(struct connection *connection,
1904 char const *packet, int packet_size)
1905 {
1906 /* We get away with only specifying flash here. Regions that are not
1907 * specified are treated as if we provided no memory map(if not we
1908 * could detect the holes and mark them as RAM).
1909 * Normally we only execute this code once, but no big deal if we
1910 * have to regenerate it a couple of times.
1911 */
1912
1913 struct target *target = get_target_from_connection(connection);
1914 struct flash_bank *p;
1915 char *xml = NULL;
1916 int size = 0;
1917 int pos = 0;
1918 int retval = ERROR_OK;
1919 struct flash_bank **banks;
1920 int offset;
1921 int length;
1922 char *separator;
1923 target_addr_t ram_start = 0;
1924 unsigned int target_flash_banks = 0;
1925
1926 /* skip command character */
1927 packet += 23;
1928
1929 offset = strtoul(packet, &separator, 16);
1930 length = strtoul(separator + 1, &separator, 16);
1931
1932 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1933
1934 /* Sort banks in ascending order. We need to report non-flash
1935 * memory as ram (or rather read/write) by default for GDB, since
1936 * it has no concept of non-cacheable read/write memory (i/o etc).
1937 */
1938 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1939
1940 for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
1941 p = get_flash_bank_by_num_noprobe(i);
1942 if (p->target != target)
1943 continue;
1944 retval = get_flash_bank_by_num(i, &p);
1945 if (retval != ERROR_OK) {
1946 free(banks);
1947 gdb_error(connection, retval);
1948 return retval;
1949 }
1950 banks[target_flash_banks++] = p;
1951 }
1952
1953 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1954 compare_bank);
1955
1956 for (unsigned int i = 0; i < target_flash_banks; i++) {
1957 unsigned sector_size = 0;
1958 unsigned group_len = 0;
1959
1960 p = banks[i];
1961
1962 if (ram_start < p->base)
1963 xml_printf(&retval, &xml, &pos, &size,
1964 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
1965 "length=\"" TARGET_ADDR_FMT "\"/>\n",
1966 ram_start, p->base - ram_start);
1967
1968 /* Report adjacent groups of same-size sectors. So for
1969 * example top boot CFI flash will list an initial region
1970 * with several large sectors (maybe 128KB) and several
1971 * smaller ones at the end (maybe 32KB). STR7 will have
1972 * regions with 8KB, 32KB, and 64KB sectors; etc.
1973 */
1974 for (unsigned int j = 0; j < p->num_sectors; j++) {
1975
1976 /* Maybe start a new group of sectors. */
1977 if (sector_size == 0) {
1978 if (p->sectors[j].offset + p->sectors[j].size > p->size) {
1979 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1980 " overflows the end of %s bank.",
1981 p->sectors[j].offset, p->name);
1982 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1983 break;
1984 }
1985 target_addr_t start;
1986 start = p->base + p->sectors[j].offset;
1987 xml_printf(&retval, &xml, &pos, &size,
1988 "<memory type=\"flash\" "
1989 "start=\"" TARGET_ADDR_FMT "\" ",
1990 start);
1991 sector_size = p->sectors[j].size;
1992 group_len = sector_size;
1993 } else {
1994 group_len += sector_size; /* equal to p->sectors[j].size */
1995 }
1996
1997 /* Does this finish a group of sectors?
1998 * If not, continue an already-started group.
1999 */
2000 if (j < p->num_sectors - 1
2001 && p->sectors[j + 1].size == sector_size
2002 && p->sectors[j + 1].offset == p->sectors[j].offset + sector_size
2003 && p->sectors[j + 1].offset + p->sectors[j + 1].size <= p->size)
2004 continue;
2005
2006 xml_printf(&retval, &xml, &pos, &size,
2007 "length=\"0x%x\">\n"
2008 "<property name=\"blocksize\">"
2009 "0x%x</property>\n"
2010 "</memory>\n",
2011 group_len,
2012 sector_size);
2013 sector_size = 0;
2014 }
2015
2016 ram_start = p->base + p->size;
2017 }
2018
2019 if (ram_start != 0)
2020 xml_printf(&retval, &xml, &pos, &size,
2021 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
2022 "length=\"" TARGET_ADDR_FMT "\"/>\n",
2023 ram_start, target_address_max(target) - ram_start + 1);
2024 /* ELSE a flash chip could be at the very end of the address space, in
2025 * which case ram_start will be precisely 0 */
2026
2027 free(banks);
2028
2029 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
2030
2031 if (retval != ERROR_OK) {
2032 free(xml);
2033 gdb_error(connection, retval);
2034 return retval;
2035 }
2036
2037 if (offset + length > pos)
2038 length = pos - offset;
2039
2040 char *t = malloc(length + 1);
2041 t[0] = 'l';
2042 memcpy(t + 1, xml + offset, length);
2043 gdb_put_packet(connection, t, length + 1);
2044
2045 free(t);
2046 free(xml);
2047 return ERROR_OK;
2048 }
2049
2050 static const char *gdb_get_reg_type_name(enum reg_type type)
2051 {
2052 switch (type) {
2053 case REG_TYPE_BOOL:
2054 return "bool";
2055 case REG_TYPE_INT:
2056 return "int";
2057 case REG_TYPE_INT8:
2058 return "int8";
2059 case REG_TYPE_INT16:
2060 return "int16";
2061 case REG_TYPE_INT32:
2062 return "int32";
2063 case REG_TYPE_INT64:
2064 return "int64";
2065 case REG_TYPE_INT128:
2066 return "int128";
2067 case REG_TYPE_UINT:
2068 return "uint";
2069 case REG_TYPE_UINT8:
2070 return "uint8";
2071 case REG_TYPE_UINT16:
2072 return "uint16";
2073 case REG_TYPE_UINT32:
2074 return "uint32";
2075 case REG_TYPE_UINT64:
2076 return "uint64";
2077 case REG_TYPE_UINT128:
2078 return "uint128";
2079 case REG_TYPE_CODE_PTR:
2080 return "code_ptr";
2081 case REG_TYPE_DATA_PTR:
2082 return "data_ptr";
2083 case REG_TYPE_FLOAT:
2084 return "float";
2085 case REG_TYPE_IEEE_SINGLE:
2086 return "ieee_single";
2087 case REG_TYPE_IEEE_DOUBLE:
2088 return "ieee_double";
2089 case REG_TYPE_ARCH_DEFINED:
2090 return "int"; /* return arbitrary string to avoid compile warning. */
2091 }
2092
2093 return "int"; /* "int" as default value */
2094 }
2095
2096 static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id,
2097 int *num_arch_defined_types)
2098 {
2099 int tbl_sz = *num_arch_defined_types;
2100
2101 if (type_id && (strcmp(type_id, ""))) {
2102 for (int j = 0; j < (tbl_sz + 1); j++) {
2103 if (!((*arch_defined_types_list)[j])) {
2104 (*arch_defined_types_list)[tbl_sz++] = type_id;
2105 *arch_defined_types_list = realloc(*arch_defined_types_list,
2106 sizeof(char *) * (tbl_sz + 1));
2107 (*arch_defined_types_list)[tbl_sz] = NULL;
2108 *num_arch_defined_types = tbl_sz;
2109 return 1;
2110 } else {
2111 if (!strcmp((*arch_defined_types_list)[j], type_id))
2112 return 0;
2113 }
2114 }
2115 }
2116
2117 return -1;
2118 }
2119
2120 static int gdb_generate_reg_type_description(struct target *target,
2121 char **tdesc, int *pos, int *size, struct reg_data_type *type,
2122 char const **arch_defined_types_list[], int *num_arch_defined_types)
2123 {
2124 int retval = ERROR_OK;
2125
2126 if (type->type_class == REG_TYPE_CLASS_VECTOR) {
2127 struct reg_data_type *data_type = type->reg_type_vector->type;
2128 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2129 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2130 num_arch_defined_types))
2131 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2132 arch_defined_types_list,
2133 num_arch_defined_types);
2134 }
2135 /* <vector id="id" type="type" count="count"/> */
2136 xml_printf(&retval, tdesc, pos, size,
2137 "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32 "\"/>\n",
2138 type->id, type->reg_type_vector->type->id,
2139 type->reg_type_vector->count);
2140
2141 } else if (type->type_class == REG_TYPE_CLASS_UNION) {
2142 struct reg_data_type_union_field *field;
2143 field = type->reg_type_union->fields;
2144 while (field) {
2145 struct reg_data_type *data_type = field->type;
2146 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2147 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2148 num_arch_defined_types))
2149 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2150 arch_defined_types_list,
2151 num_arch_defined_types);
2152 }
2153
2154 field = field->next;
2155 }
2156 /* <union id="id">
2157 * <field name="name" type="type"/> ...
2158 * </union> */
2159 xml_printf(&retval, tdesc, pos, size,
2160 "<union id=\"%s\">\n",
2161 type->id);
2162
2163 field = type->reg_type_union->fields;
2164 while (field) {
2165 xml_printf(&retval, tdesc, pos, size,
2166 "<field name=\"%s\" type=\"%s\"/>\n",
2167 field->name, field->type->id);
2168
2169 field = field->next;
2170 }
2171
2172 xml_printf(&retval, tdesc, pos, size,
2173 "</union>\n");
2174
2175 } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
2176 struct reg_data_type_struct_field *field;
2177 field = type->reg_type_struct->fields;
2178
2179 if (field->use_bitfields) {
2180 /* <struct id="id" size="size">
2181 * <field name="name" start="start" end="end"/> ...
2182 * </struct> */
2183 xml_printf(&retval, tdesc, pos, size,
2184 "<struct id=\"%s\" size=\"%" PRIu32 "\">\n",
2185 type->id, type->reg_type_struct->size);
2186 while (field) {
2187 xml_printf(&retval, tdesc, pos, size,
2188 "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2189 field->name, field->bitfield->start, field->bitfield->end,
2190 gdb_get_reg_type_name(field->bitfield->type));
2191
2192 field = field->next;
2193 }
2194 } else {
2195 while (field) {
2196 struct reg_data_type *data_type = field->type;
2197 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2198 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2199 num_arch_defined_types))
2200 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2201 arch_defined_types_list,
2202 num_arch_defined_types);
2203 }
2204 }
2205
2206 /* <struct id="id">
2207 * <field name="name" type="type"/> ...
2208 * </struct> */
2209 xml_printf(&retval, tdesc, pos, size,
2210 "<struct id=\"%s\">\n",
2211 type->id);
2212 while (field) {
2213 xml_printf(&retval, tdesc, pos, size,
2214 "<field name=\"%s\" type=\"%s\"/>\n",
2215 field->name, field->type->id);
2216
2217 field = field->next;
2218 }
2219 }
2220
2221 xml_printf(&retval, tdesc, pos, size,
2222 "</struct>\n");
2223
2224 } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
2225 /* <flags id="id" size="size">
2226 * <field name="name" start="start" end="end"/> ...
2227 * </flags> */
2228 xml_printf(&retval, tdesc, pos, size,
2229 "<flags id=\"%s\" size=\"%" PRIu32 "\">\n",
2230 type->id, type->reg_type_flags->size);
2231
2232 struct reg_data_type_flags_field *field;
2233 field = type->reg_type_flags->fields;
2234 while (field) {
2235 xml_printf(&retval, tdesc, pos, size,
2236 "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2237 field->name, field->bitfield->start, field->bitfield->end,
2238 gdb_get_reg_type_name(field->bitfield->type));
2239
2240 field = field->next;
2241 }
2242
2243 xml_printf(&retval, tdesc, pos, size,
2244 "</flags>\n");
2245
2246 }
2247
2248 return ERROR_OK;
2249 }
2250
2251 /* Get a list of available target registers features. feature_list must
2252 * be freed by caller.
2253 */
2254 static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
2255 struct reg **reg_list, int reg_list_size)
2256 {
2257 int tbl_sz = 0;
2258
2259 /* Start with only one element */
2260 *feature_list = calloc(1, sizeof(char *));
2261
2262 for (int i = 0; i < reg_list_size; i++) {
2263 if (reg_list[i]->exist == false || reg_list[i]->hidden)
2264 continue;
2265
2266 if (reg_list[i]->feature
2267 && reg_list[i]->feature->name
2268 && (strcmp(reg_list[i]->feature->name, ""))) {
2269 /* We found a feature, check if the feature is already in the
2270 * table. If not, allocate a new entry for the table and
2271 * put the new feature in it.
2272 */
2273 for (int j = 0; j < (tbl_sz + 1); j++) {
2274 if (!((*feature_list)[j])) {
2275 (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
2276 *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
2277 (*feature_list)[tbl_sz] = NULL;
2278 break;
2279 } else {
2280 if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
2281 break;
2282 }
2283 }
2284 }
2285 }
2286
2287 if (feature_list_size)
2288 *feature_list_size = tbl_sz;
2289
2290 return ERROR_OK;
2291 }
2292
2293 /* Create a register list that's the union of all the registers of the SMP
2294 * group this target is in. If the target is not part of an SMP group, this
2295 * returns the same as target_get_gdb_reg_list_noread().
2296 */
2297 static int smp_reg_list_noread(struct target *target,
2298 struct reg **combined_list[], int *combined_list_size,
2299 enum target_register_class reg_class)
2300 {
2301 if (!target->smp)
2302 return target_get_gdb_reg_list_noread(target, combined_list,
2303 combined_list_size, REG_CLASS_ALL);
2304
2305 unsigned int combined_allocated = 256;
2306 struct reg **local_list = malloc(combined_allocated * sizeof(struct reg *));
2307 if (!local_list) {
2308 LOG_ERROR("malloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2309 return ERROR_FAIL;
2310 }
2311 unsigned int local_list_size = 0;
2312
2313 struct target_list *head;
2314 foreach_smp_target(head, target->smp_targets) {
2315 if (!target_was_examined(head->target))
2316 continue;
2317
2318 struct reg **reg_list = NULL;
2319 int reg_list_size;
2320 int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2321 &reg_list_size, reg_class);
2322 if (result != ERROR_OK) {
2323 free(local_list);
2324 return result;
2325 }
2326 for (int i = 0; i < reg_list_size; i++) {
2327 bool found = false;
2328 struct reg *a = reg_list[i];
2329 if (a->exist) {
2330 /* Nested loop makes this O(n^2), but this entire function with
2331 * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2332 * for me. */
2333 for (unsigned int j = 0; j < local_list_size; j++) {
2334 struct reg *b = local_list[j];
2335 if (!strcmp(a->name, b->name)) {
2336 found = true;
2337 if (a->size != b->size) {
2338 LOG_ERROR("SMP register %s is %d bits on one "
2339 "target, but %d bits on another target.",
2340 a->name, a->size, b->size);
2341 free(reg_list);
2342 free(local_list);
2343 return ERROR_FAIL;
2344 }
2345 break;
2346 }
2347 }
2348 if (!found) {
2349 LOG_DEBUG("[%s] %s not found in combined list", target_name(target), a->name);
2350 if (local_list_size >= combined_allocated) {
2351 combined_allocated *= 2;
2352 local_list = realloc(local_list, combined_allocated * sizeof(struct reg *));
2353 if (!local_list) {
2354 LOG_ERROR("realloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2355 free(reg_list);
2356 return ERROR_FAIL;
2357 }
2358 }
2359 local_list[local_list_size] = a;
2360 local_list_size++;
2361 }
2362 }
2363 }
2364 free(reg_list);
2365 }
2366
2367 if (local_list_size == 0) {
2368 LOG_ERROR("Unable to get register list");
2369 free(local_list);
2370 return ERROR_FAIL;
2371 }
2372
2373 /* Now warn the user about any registers that weren't found in every target. */
2374 foreach_smp_target(head, target->smp_targets) {
2375 if (!target_was_examined(head->target))
2376 continue;
2377
2378 struct reg **reg_list = NULL;
2379 int reg_list_size;
2380 int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2381 &reg_list_size, reg_class);
2382 if (result != ERROR_OK) {
2383 free(local_list);
2384 return result;
2385 }
2386 for (unsigned int i = 0; i < local_list_size; i++) {
2387 bool found = false;
2388 struct reg *a = local_list[i];
2389 for (int j = 0; j < reg_list_size; j++) {
2390 struct reg *b = reg_list[j];
2391 if (b->exist && !strcmp(a->name, b->name)) {
2392 found = true;
2393 break;
2394 }
2395 }
2396 if (!found) {
2397 LOG_WARNING("Register %s does not exist in %s, which is part of an SMP group where "
2398 "this register does exist.",
2399 a->name, target_name(head->target));
2400 }
2401 }
2402 free(reg_list);
2403 }
2404
2405 *combined_list = local_list;
2406 *combined_list_size = local_list_size;
2407 return ERROR_OK;
2408 }
2409
2410 static int gdb_generate_target_description(struct target *target, char **tdesc_out)
2411 {
2412 int retval = ERROR_OK;
2413 struct reg **reg_list = NULL;
2414 int reg_list_size;
2415 char const *architecture;
2416 char const **features = NULL;
2417 int feature_list_size = 0;
2418 char *tdesc = NULL;
2419 int pos = 0;
2420 int size = 0;
2421
2422
2423 retval = smp_reg_list_noread(target, &reg_list, &reg_list_size,
2424 REG_CLASS_ALL);
2425
2426 if (retval != ERROR_OK) {
2427 LOG_ERROR("get register list failed");
2428 retval = ERROR_FAIL;
2429 goto error;
2430 }
2431
2432 if (reg_list_size <= 0) {
2433 LOG_ERROR("get register list failed");
2434 retval = ERROR_FAIL;
2435 goto error;
2436 }
2437
2438 /* Get a list of available target registers features */
2439 retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2440 if (retval != ERROR_OK) {
2441 LOG_ERROR("Can't get the registers feature list");
2442 retval = ERROR_FAIL;
2443 goto error;
2444 }
2445
2446 /* If we found some features associated with registers, create sections */
2447 int current_feature = 0;
2448
2449 xml_printf(&retval, &tdesc, &pos, &size,
2450 "<?xml version=\"1.0\"?>\n"
2451 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2452 "<target version=\"1.0\">\n");
2453
2454 /* generate architecture element if supported by target */
2455 architecture = target_get_gdb_arch(target);
2456 if (architecture)
2457 xml_printf(&retval, &tdesc, &pos, &size,
2458 "<architecture>%s</architecture>\n", architecture);
2459
2460 /* generate target description according to register list */
2461 if (features) {
2462 while (features[current_feature]) {
2463 char const **arch_defined_types = NULL;
2464 int num_arch_defined_types = 0;
2465
2466 arch_defined_types = calloc(1, sizeof(char *));
2467 xml_printf(&retval, &tdesc, &pos, &size,
2468 "<feature name=\"%s\">\n",
2469 features[current_feature]);
2470
2471 int i;
2472 for (i = 0; i < reg_list_size; i++) {
2473
2474 if (reg_list[i]->exist == false || reg_list[i]->hidden)
2475 continue;
2476
2477 if (strcmp(reg_list[i]->feature->name, features[current_feature]))
2478 continue;
2479
2480 const char *type_str;
2481 if (reg_list[i]->reg_data_type) {
2482 if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
2483 /* generate <type... first, if there are architecture-defined types. */
2484 if (lookup_add_arch_defined_types(&arch_defined_types,
2485 reg_list[i]->reg_data_type->id,
2486 &num_arch_defined_types))
2487 gdb_generate_reg_type_description(target, &tdesc, &pos, &size,
2488 reg_list[i]->reg_data_type,
2489 &arch_defined_types,
2490 &num_arch_defined_types);
2491
2492 type_str = reg_list[i]->reg_data_type->id;
2493 } else {
2494 /* predefined type */
2495 type_str = gdb_get_reg_type_name(
2496 reg_list[i]->reg_data_type->type);
2497 }
2498 } else {
2499 /* Default type is "int" */
2500 type_str = "int";
2501 }
2502
2503 xml_printf(&retval, &tdesc, &pos, &size,
2504 "<reg name=\"%s\"", reg_list[i]->name);
2505 xml_printf(&retval, &tdesc, &pos, &size,
2506 " bitsize=\"%" PRIu32 "\"", reg_list[i]->size);
2507 xml_printf(&retval, &tdesc, &pos, &size,
2508 " regnum=\"%" PRIu32 "\"", reg_list[i]->number);
2509 if (reg_list[i]->caller_save)
2510 xml_printf(&retval, &tdesc, &pos, &size,
2511 " save-restore=\"yes\"");
2512 else
2513 xml_printf(&retval, &tdesc, &pos, &size,
2514 " save-restore=\"no\"");
2515
2516 xml_printf(&retval, &tdesc, &pos, &size,
2517 " type=\"%s\"", type_str);
2518
2519 if (reg_list[i]->group)
2520 xml_printf(&retval, &tdesc, &pos, &size,
2521 " group=\"%s\"", reg_list[i]->group);
2522
2523 xml_printf(&retval, &tdesc, &pos, &size,
2524 "/>\n");
2525 }
2526
2527 xml_printf(&retval, &tdesc, &pos, &size,
2528 "</feature>\n");
2529
2530 current_feature++;
2531 free(arch_defined_types);
2532 }
2533 }
2534
2535 xml_printf(&retval, &tdesc, &pos, &size,
2536 "</target>\n");
2537
2538 error:
2539 free(features);
2540 free(reg_list);
2541
2542 if (retval == ERROR_OK)
2543 *tdesc_out = tdesc;
2544 else
2545 free(tdesc);
2546
2547 return retval;
2548 }
2549
2550 static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
2551 char **chunk, int32_t offset, uint32_t length)
2552 {
2553 if (!target_desc) {
2554 LOG_ERROR("Unable to Generate Target Description");
2555 return ERROR_FAIL;
2556 }
2557
2558 char *tdesc = target_desc->tdesc;
2559 uint32_t tdesc_length = target_desc->tdesc_length;
2560
2561 if (!tdesc) {
2562 int retval = gdb_generate_target_description(target, &tdesc);
2563 if (retval != ERROR_OK) {
2564 LOG_ERROR("Unable to Generate Target Description");
2565 return ERROR_FAIL;
2566 }
2567
2568 tdesc_length = strlen(tdesc);
2569 }
2570
2571 char transfer_type;
2572
2573 if (length < (tdesc_length - offset))
2574 transfer_type = 'm';
2575 else
2576 transfer_type = 'l';
2577
2578 *chunk = malloc(length + 2);
2579 if (!*chunk) {
2580 LOG_ERROR("Unable to allocate memory");
2581 return ERROR_FAIL;
2582 }
2583
2584 (*chunk)[0] = transfer_type;
2585 if (transfer_type == 'm') {
2586 strncpy((*chunk) + 1, tdesc + offset, length);
2587 (*chunk)[1 + length] = '\0';
2588 } else {
2589 strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
2590 (*chunk)[1 + (tdesc_length - offset)] = '\0';
2591
2592 /* After gdb-server sends out last chunk, invalidate tdesc. */
2593 free(tdesc);
2594 tdesc = NULL;
2595 tdesc_length = 0;
2596 }
2597
2598 target_desc->tdesc = tdesc;
2599 target_desc->tdesc_length = tdesc_length;
2600
2601 return ERROR_OK;
2602 }
2603
2604 static int gdb_target_description_supported(struct target *target, int *supported)
2605 {
2606 int retval = ERROR_OK;
2607 struct reg **reg_list = NULL;
2608 int reg_list_size = 0;
2609 char const **features = NULL;
2610 int feature_list_size = 0;
2611
2612 char const *architecture = target_get_gdb_arch(target);
2613
2614 retval = target_get_gdb_reg_list_noread(target, &reg_list,
2615 &reg_list_size, REG_CLASS_ALL);
2616 if (retval != ERROR_OK) {
2617 LOG_ERROR("get register list failed");
2618 goto error;
2619 }
2620
2621 if (reg_list_size <= 0) {
2622 LOG_ERROR("get register list failed");
2623 retval = ERROR_FAIL;
2624 goto error;
2625 }
2626
2627 /* Get a list of available target registers features */
2628 retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2629 if (retval != ERROR_OK) {
2630 LOG_ERROR("Can't get the registers feature list");
2631 goto error;
2632 }
2633
2634 if (supported) {
2635 if (architecture || feature_list_size)
2636 *supported = 1;
2637 else
2638 *supported = 0;
2639 }
2640
2641 error:
2642 free(features);
2643
2644 free(reg_list);
2645
2646 return retval;
2647 }
2648
2649 static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
2650 {
2651 struct rtos *rtos = target->rtos;
2652 int retval = ERROR_OK;
2653 char *thread_list = NULL;
2654 int pos = 0;
2655 int size = 0;
2656
2657 xml_printf(&retval, &thread_list, &pos, &size,
2658 "<?xml version=\"1.0\"?>\n"
2659 "<threads>\n");
2660
2661 if (rtos) {
2662 for (int i = 0; i < rtos->thread_count; i++) {
2663 struct thread_detail *thread_detail = &rtos->thread_details[i];
2664
2665 if (!thread_detail->exists)
2666 continue;
2667
2668 if (thread_detail->thread_name_str)
2669 xml_printf(&retval, &thread_list, &pos, &size,
2670 "<thread id=\"%" PRIx64 "\" name=\"%s\">",
2671 thread_detail->threadid,
2672 thread_detail->thread_name_str);
2673 else
2674 xml_printf(&retval, &thread_list, &pos, &size,
2675 "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
2676
2677 if (thread_detail->thread_name_str)
2678 xml_printf(&retval, &thread_list, &pos, &size,
2679 "Name: %s", thread_detail->thread_name_str);
2680
2681 if (thread_detail->extra_info_str) {
2682 if (thread_detail->thread_name_str)
2683 xml_printf(&retval, &thread_list, &pos, &size,
2684 ", ");
2685 xml_printf(&retval, &thread_list, &pos, &size,
2686 "%s", thread_detail->extra_info_str);
2687 }
2688
2689 xml_printf(&retval, &thread_list, &pos, &size,
2690 "</thread>\n");
2691 }
2692 }
2693
2694 xml_printf(&retval, &thread_list, &pos, &size,
2695 "</threads>\n");
2696
2697 if (retval == ERROR_OK)
2698 *thread_list_out = thread_list;
2699 else
2700 free(thread_list);
2701
2702 return retval;
2703 }
2704
2705 static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
2706 char **chunk, int32_t offset, uint32_t length)
2707 {
2708 if (!*thread_list) {
2709 int retval = gdb_generate_thread_list(target, thread_list);
2710 if (retval != ERROR_OK) {
2711 LOG_ERROR("Unable to Generate Thread List");
2712 return ERROR_FAIL;
2713 }
2714 }
2715
2716 size_t thread_list_length = strlen(*thread_list);
2717 char transfer_type;
2718
2719 length = MIN(length, thread_list_length - offset);
2720 if (length < (thread_list_length - offset))
2721 transfer_type = 'm';
2722 else
2723 transfer_type = 'l';
2724
2725 *chunk = malloc(length + 2 + 3);
2726 /* Allocating extra 3 bytes prevents false positive valgrind report
2727 * of strlen(chunk) word access:
2728 * Invalid read of size 4
2729 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2730 if (!*chunk) {
2731 LOG_ERROR("Unable to allocate memory");
2732 return ERROR_FAIL;
2733 }
2734
2735 (*chunk)[0] = transfer_type;
2736 strncpy((*chunk) + 1, (*thread_list) + offset, length);
2737 (*chunk)[1 + length] = '\0';
2738
2739 /* After gdb-server sends out last chunk, invalidate thread list. */
2740 if (transfer_type == 'l') {
2741 free(*thread_list);
2742 *thread_list = NULL;
2743 }
2744
2745 return ERROR_OK;
2746 }
2747
2748 static int gdb_query_packet(struct connection *connection,
2749 char const *packet, int packet_size)
2750 {
2751 struct command_context *cmd_ctx = connection->cmd_ctx;
2752 struct gdb_connection *gdb_connection = connection->priv;
2753 struct target *target = get_target_from_connection(connection);
2754
2755 if (strncmp(packet, "qRcmd,", 6) == 0) {
2756 if (packet_size > 6) {
2757 Jim_Interp *interp = cmd_ctx->interp;
2758 char *cmd;
2759 cmd = malloc((packet_size - 6) / 2 + 1);
2760 size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
2761 cmd[len] = 0;
2762
2763 /* We want to print all debug output to GDB connection */
2764 gdb_connection->output_flag = GDB_OUTPUT_ALL;
2765 target_call_timer_callbacks_now();
2766 /* some commands need to know the GDB connection, make note of current
2767 * GDB connection. */
2768 current_gdb_connection = gdb_connection;
2769
2770 struct target *saved_target_override = cmd_ctx->current_target_override;
2771 cmd_ctx->current_target_override = NULL;
2772
2773 struct command_context *old_context = Jim_GetAssocData(interp, "context");
2774 Jim_DeleteAssocData(interp, "context");
2775 int retval = Jim_SetAssocData(interp, "context", NULL, cmd_ctx);
2776 if (retval == JIM_OK) {
2777 retval = Jim_EvalObj(interp, Jim_NewStringObj(interp, cmd, -1));
2778 Jim_DeleteAssocData(interp, "context");
2779 }
2780 int inner_retval = Jim_SetAssocData(interp, "context", NULL, old_context);
2781 if (retval == JIM_OK)
2782 retval = inner_retval;
2783
2784 cmd_ctx->current_target_override = saved_target_override;
2785
2786 current_gdb_connection = NULL;
2787 target_call_timer_callbacks_now();
2788 gdb_connection->output_flag = GDB_OUTPUT_NO;
2789 free(cmd);
2790 if (retval == JIM_RETURN)
2791 retval = interp->returnCode;
2792 int lenmsg;
2793 const char *cretmsg = Jim_GetString(Jim_GetResult(interp), &lenmsg);
2794 char *retmsg;
2795 if (lenmsg && cretmsg[lenmsg - 1] != '\n') {
2796 retmsg = alloc_printf("%s\n", cretmsg);
2797 lenmsg++;
2798 } else {
2799 retmsg = strdup(cretmsg);
2800 }
2801 if (!retmsg)
2802 return ERROR_GDB_BUFFER_TOO_SMALL;
2803
2804 if (retval == JIM_OK) {
2805 if (lenmsg) {
2806 char *hex_buffer = malloc(lenmsg * 2 + 1);
2807 if (!hex_buffer) {
2808 free(retmsg);
2809 return ERROR_GDB_BUFFER_TOO_SMALL;
2810 }
2811
2812 size_t pkt_len = hexify(hex_buffer, (const uint8_t *)retmsg, lenmsg,
2813 lenmsg * 2 + 1);
2814 gdb_put_packet(connection, hex_buffer, pkt_len);
2815 free(hex_buffer);
2816 } else {
2817 gdb_put_packet(connection, "OK", 2);
2818 }
2819 } else {
2820 if (lenmsg)
2821 gdb_output_con(connection, retmsg);
2822 gdb_send_error(connection, retval);
2823 }
2824 free(retmsg);
2825 return ERROR_OK;
2826 }
2827 gdb_put_packet(connection, "OK", 2);
2828 return ERROR_OK;
2829 } else if (strncmp(packet, "qCRC:", 5) == 0) {
2830 if (packet_size > 5) {
2831 int retval;
2832 char gdb_reply[10];
2833 char *separator;
2834 uint32_t checksum;
2835 target_addr_t addr = 0;
2836 uint32_t len = 0;
2837
2838 /* skip command character */
2839 packet += 5;
2840
2841 addr = strtoull(packet, &separator, 16);
2842
2843 if (*separator != ',') {
2844 LOG_ERROR("incomplete read memory packet received, dropping connection");
2845 return ERROR_SERVER_REMOTE_CLOSED;
2846 }
2847
2848 len = strtoul(separator + 1, NULL, 16);
2849
2850 retval = target_checksum_memory(target, addr, len, &checksum);
2851
2852 if (retval == ERROR_OK) {
2853 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
2854 gdb_put_packet(connection, gdb_reply, 9);
2855 } else {
2856 retval = gdb_error(connection, retval);
2857 if (retval != ERROR_OK)
2858 return retval;
2859 }
2860
2861 return ERROR_OK;
2862 }
2863 } else if (strncmp(packet, "qSupported", 10) == 0) {
2864 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2865 * qXfer:features:read is supported for some targets */
2866 int retval = ERROR_OK;
2867 char *buffer = NULL;
2868 int pos = 0;
2869 int size = 0;
2870 int gdb_target_desc_supported = 0;
2871
2872 /* we need to test that the target supports target descriptions */
2873 retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
2874 if (retval != ERROR_OK) {
2875 LOG_INFO("Failed detecting Target Description Support, disabling");
2876 gdb_target_desc_supported = 0;
2877 }
2878
2879 /* support may be disabled globally */
2880 if (gdb_use_target_description == 0) {
2881 if (gdb_target_desc_supported)
2882 LOG_WARNING("Target Descriptions Supported, but disabled");
2883 gdb_target_desc_supported = 0;
2884 }
2885
2886 xml_printf(&retval,
2887 &buffer,
2888 &pos,
2889 &size,
2890 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2891 GDB_BUFFER_SIZE,
2892 ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2893 (gdb_target_desc_supported == 1) ? '+' : '-');
2894
2895 if (retval != ERROR_OK) {
2896 gdb_send_error(connection, 01);
2897 return ERROR_OK;
2898 }
2899
2900 gdb_put_packet(connection, buffer, strlen(buffer));
2901 free(buffer);
2902
2903 return ERROR_OK;
2904 } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
2905 && (flash_get_bank_count() > 0))
2906 return gdb_memory_map(connection, packet, packet_size);
2907 else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
2908 char *xml = NULL;
2909 int retval = ERROR_OK;
2910
2911 int offset;
2912 unsigned int length;
2913
2914 /* skip command character */
2915 packet += 20;
2916
2917 if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2918 gdb_send_error(connection, 01);
2919 return ERROR_OK;
2920 }
2921
2922 /* Target should prepare correct target description for annex.
2923 * The first character of returned xml is 'm' or 'l'. 'm' for
2924 * there are *more* chunks to transfer. 'l' for it is the *last*
2925 * chunk of target description.
2926 */
2927 retval = gdb_get_target_description_chunk(target, &gdb_connection->target_desc,
2928 &xml, offset, length);
2929 if (retval != ERROR_OK) {
2930 gdb_error(connection, retval);
2931 return retval;
2932 }
2933
2934 gdb_put_packet(connection, xml, strlen(xml));
2935
2936 free(xml);
2937 return ERROR_OK;
2938 } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
2939 char *xml = NULL;
2940 int retval = ERROR_OK;
2941
2942 int offset;
2943 unsigned int length;
2944
2945 /* skip command character */
2946 packet += 19;
2947
2948 if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2949 gdb_send_error(connection, 01);
2950 return ERROR_OK;
2951 }
2952
2953 /* Target should prepare correct thread list for annex.
2954 * The first character of returned xml is 'm' or 'l'. 'm' for
2955 * there are *more* chunks to transfer. 'l' for it is the *last*
2956 * chunk of target description.
2957 */
2958 retval = gdb_get_thread_list_chunk(target, &gdb_connection->thread_list,
2959 &xml, offset, length);
2960 if (retval != ERROR_OK) {
2961 gdb_error(connection, retval);
2962 return retval;
2963 }
2964
2965 gdb_put_packet(connection, xml, strlen(xml));
2966
2967 free(xml);
2968 return ERROR_OK;
2969 } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
2970 gdb_connection->noack_mode = 1;
2971 gdb_put_packet(connection, "OK", 2);
2972 return ERROR_OK;
2973 } else if (target->type->gdb_query_custom) {
2974 char *buffer = NULL;
2975 int ret = target->type->gdb_query_custom(target, packet, &buffer);
2976 gdb_put_packet(connection, buffer, strlen(buffer));
2977 return ret;
2978 }
2979
2980 gdb_put_packet(connection, "", 0);
2981 return ERROR_OK;
2982 }
2983
2984 static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet,
2985 __attribute__((unused)) int packet_size)
2986 {
2987 struct gdb_connection *gdb_connection = connection->priv;
2988 struct target *target = get_target_from_connection(connection);
2989 const char *parse = packet;
2990 int retval;
2991
2992 /* query for vCont supported */
2993 if (parse[0] == '?') {
2994 if (target->type->step) {
2995 /* gdb doesn't accept c without C and s without S */
2996 gdb_put_packet(connection, "vCont;c;C;s;S", 13);
2997 return true;
2998 }
2999 return false;
3000 }
3001
3002 if (parse[0] == ';') {
3003 ++parse;
3004 }
3005
3006 /* simple case, a continue packet */
3007 if (parse[0] == 'c') {
3008 gdb_running_type = 'c';
3009 LOG_DEBUG("target %s continue", target_name(target));
3010 gdb_connection->output_flag = GDB_OUTPUT_ALL;
3011 retval = target_resume(target, 1, 0, 0, 0);
3012 if (retval == ERROR_TARGET_NOT_HALTED)
3013 LOG_INFO("target %s was not halted when resume was requested", target_name(target));
3014
3015 /* poll target in an attempt to make its internal state consistent */
3016 if (retval != ERROR_OK) {
3017 retval = target_poll(target);
3018 if (retval != ERROR_OK)
3019 LOG_DEBUG("error polling target %s after failed resume", target_name(target));
3020 }
3021
3022 /*
3023 * We don't report errors to gdb here, move frontend_state to
3024 * TARGET_RUNNING to stay in sync with gdb's expectation of the
3025 * target state
3026 */
3027 gdb_connection->frontend_state = TARGET_RUNNING;
3028 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
3029
3030 return true;
3031 }
3032
3033 /* single-step or step-over-breakpoint */
3034 if (parse[0] == 's') {
3035 gdb_running_type = 's';
3036 bool fake_step = false;
3037
3038 struct target *ct = target;
3039 int current_pc = 1;
3040 int64_t thread_id;
3041 parse++;
3042 if (parse[0] == ':') {
3043 char *endp;
3044 parse++;
3045 thread_id = strtoll(parse, &endp, 16);
3046 if (endp) {
3047 parse = endp;
3048 }
3049 } else {
3050 thread_id = 0;
3051 }
3052
3053 if (target->rtos) {
3054 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
3055 rtos_update_threads(target);
3056
3057 target->rtos->gdb_target_for_threadid(connection, thread_id, &ct);
3058
3059 /*
3060 * check if the thread to be stepped is the current rtos thread
3061 * if not, we must fake the step
3062 */
3063 if (target->rtos->current_thread != thread_id)
3064 fake_step = true;
3065 }
3066
3067 if (parse[0] == ';') {
3068 ++parse;
3069
3070 if (parse[0] == 'c') {
3071 parse += 1;
3072
3073 /* check if thread-id follows */
3074 if (parse[0] == ':') {
3075 int64_t tid;
3076 parse += 1;
3077
3078 tid = strtoll(parse, NULL, 16);
3079 if (tid == thread_id) {
3080 /*
3081 * Special case: only step a single thread (core),
3082 * keep the other threads halted. Currently, only
3083 * aarch64 target understands it. Other target types don't
3084 * care (nobody checks the actual value of 'current')
3085 * and it doesn't really matter. This deserves
3086 * a symbolic constant and a formal interface documentation
3087 * at a later time.
3088 */
3089 LOG_DEBUG("request to step current core only");
3090 /* uncomment after checking that indeed other targets are safe */
3091 /*current_pc = 2;*/
3092 }
3093 }
3094 }
3095 }
3096
3097 LOG_DEBUG("target %s single-step thread %"PRIx64, target_name(ct), thread_id);
3098 gdb_connection->output_flag = GDB_OUTPUT_ALL;
3099 target_call_event_callbacks(ct, TARGET_EVENT_GDB_START);
3100
3101 /*
3102 * work around an annoying gdb behaviour: when the current thread
3103 * is changed in gdb, it assumes that the target can follow and also
3104 * make the thread current. This is an assumption that cannot hold
3105 * for a real target running a multi-threading OS. We just fake
3106 * the step to not trigger an internal error in gdb. See
3107 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3108 */
3109 if (fake_step) {
3110 int sig_reply_len;
3111 char sig_reply[128];
3112
3113 LOG_DEBUG("fake step thread %"PRIx64, thread_id);
3114
3115 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply),
3116 "T05thread:%016"PRIx64";", thread_id);
3117
3118 gdb_put_packet(connection, sig_reply, sig_reply_len);
3119 gdb_connection->output_flag = GDB_OUTPUT_NO;
3120
3121 return true;
3122 }
3123
3124 /* support for gdb_sync command */
3125 if (gdb_connection->sync) {
3126 gdb_connection->sync = false;
3127 if (ct->state == TARGET_HALTED) {
3128 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3129 "from the target.");
3130 gdb_sig_halted(connection);
3131 gdb_connection->output_flag = GDB_OUTPUT_NO;
3132 } else
3133 gdb_connection->frontend_state = TARGET_RUNNING;
3134 return true;
3135 }
3136
3137 retval = target_step(ct, current_pc, 0, 0);
3138 if (retval == ERROR_TARGET_NOT_HALTED)
3139 LOG_INFO("target %s was not halted when step was requested", target_name(ct));
3140
3141 /* if step was successful send a reply back to gdb */
3142 if (retval == ERROR_OK) {
3143 retval = target_poll(ct);
3144 if (retval != ERROR_OK)
3145 LOG_DEBUG("error polling target %s after successful step", target_name(ct));
3146 /* send back signal information */
3147 gdb_signal_reply(ct, connection);
3148 /* stop forwarding log packets! */
3149 gdb_connection->output_flag = GDB_OUTPUT_NO;
3150 } else
3151 gdb_connection->frontend_state = TARGET_RUNNING;
3152 return true;
3153 }
3154 LOG_ERROR("Unknown vCont packet");
3155 return false;
3156 }
3157
3158 static char *next_hex_encoded_field(const char **str, char sep)
3159 {
3160 size_t hexlen;
3161 const char *hex = *str;
3162 if (hex[0] == '\0')
3163 return NULL;
3164
3165 const char *end = strchr(hex, sep);
3166 if (!end)
3167 hexlen = strlen(hex);
3168 else
3169 hexlen = end - hex;
3170 *str = hex + hexlen + 1;
3171
3172 if (hexlen % 2 != 0) {
3173 /* Malformed hex data */
3174 return NULL;
3175 }
3176
3177 size_t count = hexlen / 2;
3178 char *decoded = malloc(count + 1);
3179 if (!decoded)
3180 return NULL;
3181
3182 size_t converted = unhexify((void *)decoded, hex, count);
3183 if (converted != count) {
3184 free(decoded);
3185 return NULL;
3186 }
3187
3188 decoded[count] = '\0';
3189 return decoded;
3190 }
3191
3192 /* handle extended restart packet */
3193 static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
3194 {
3195 struct gdb_connection *gdb_con = connection->priv;
3196 struct target *target = get_target_from_connection(connection);
3197
3198 breakpoint_clear_target(target);
3199 watchpoint_clear_target(target);
3200 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
3201 target_name(target));
3202 /* set connection as attached after reset */
3203 gdb_con->attached = true;
3204 /* info rtos parts */
3205 gdb_thread_packet(connection, packet, packet_size);
3206 }
3207
3208 static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
3209 {
3210 struct target *target = get_target_from_connection(connection);
3211 const char *parse = packet;
3212
3213 /* Skip "vRun" */
3214 parse += 4;
3215
3216 if (parse[0] != ';')
3217 return false;
3218 parse++;
3219
3220 /* Skip first field "filename"; don't know what to do with it. */
3221 free(next_hex_encoded_field(&parse, ';'));
3222
3223 char *cmdline = next_hex_encoded_field(&parse, ';');
3224 while (cmdline) {
3225 char *arg = next_hex_encoded_field(&parse, ';');
3226 if (!arg)
3227 break;
3228 char *new_cmdline = alloc_printf("%s %s", cmdline, arg);
3229 free(cmdline);
3230 free(arg);
3231 cmdline = new_cmdline;
3232 }
3233
3234 if (cmdline) {
3235 if (target->semihosting) {
3236 LOG_INFO("GDB set inferior command line to '%s'", cmdline);
3237 free(target->semihosting->cmdline);
3238 target->semihosting->cmdline = cmdline;
3239 } else {
3240 LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline);
3241 free(cmdline);
3242 }
3243 }
3244
3245 gdb_restart_inferior(connection, packet, packet_size);
3246 gdb_put_packet(connection, "S00", 3);
3247 return true;
3248 }
3249
3250 static int gdb_v_packet(struct connection *connection,
3251 char const *packet, int packet_size)
3252 {
3253 struct gdb_connection *gdb_connection = connection->priv;
3254 int result;
3255
3256 struct target *target = get_target_from_connection(connection);
3257
3258 if (strncmp(packet, "vCont", 5) == 0) {
3259 bool handled;
3260
3261 packet += 5;
3262 packet_size -= 5;
3263
3264 handled = gdb_handle_vcont_packet(connection, packet, packet_size);
3265 if (!handled)
3266 gdb_put_packet(connection, "", 0);
3267
3268 return ERROR_OK;
3269 }
3270
3271 if (strncmp(packet, "vRun", 4) == 0) {
3272 bool handled;
3273
3274 handled = gdb_handle_vrun_packet(connection, packet, packet_size);
3275 if (!handled)
3276 gdb_put_packet(connection, "", 0);
3277
3278 return ERROR_OK;
3279 }
3280
3281 /* if flash programming disabled - send a empty reply */
3282
3283 if (gdb_flash_program == 0) {
3284 gdb_put_packet(connection, "", 0);
3285 return ERROR_OK;
3286 }
3287
3288 if (strncmp(packet, "vFlashErase:", 12) == 0) {
3289 target_addr_t addr;
3290 unsigned long length;
3291
3292 char const *parse = packet + 12;
3293 if (*parse == '\0') {
3294 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3295 return ERROR_SERVER_REMOTE_CLOSED;
3296 }
3297
3298 addr = strtoull(parse, (char **)&parse, 16);
3299
3300 if (*(parse++) != ',' || *parse == '\0') {
3301 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3302 return ERROR_SERVER_REMOTE_CLOSED;
3303 }
3304
3305 length = strtoul(parse, (char **)&parse, 16);
3306
3307 if (*parse != '\0') {
3308 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3309 return ERROR_SERVER_REMOTE_CLOSED;
3310 }
3311
3312 /* assume all sectors need erasing - stops any problems
3313 * when flash_write is called multiple times */
3314 flash_set_dirty();
3315
3316 /* perform any target specific operations before the erase */
3317 target_call_event_callbacks(target,
3318 TARGET_EVENT_GDB_FLASH_ERASE_START);
3319
3320 /* vFlashErase:addr,length messages require region start and
3321 * end to be "block" aligned ... if padding is ever needed,
3322 * GDB will have become dangerously confused.
3323 */
3324 result = flash_erase_address_range(target, false, addr,
3325 length);
3326
3327 /* perform any target specific operations after the erase */
3328 target_call_event_callbacks(target,
3329 TARGET_EVENT_GDB_FLASH_ERASE_END);
3330
3331 /* perform erase */
3332 if (result != ERROR_OK) {
3333 /* GDB doesn't evaluate the actual error number returned,
3334 * treat a failed erase as an I/O error
3335 */
3336 gdb_send_error(connection, EIO);
3337 LOG_ERROR("flash_erase returned %i", result);
3338 } else
3339 gdb_put_packet(connection, "OK", 2);
3340
3341 return ERROR_OK;
3342 }
3343
3344 if (strncmp(packet, "vFlashWrite:", 12) == 0) {
3345 int retval;
3346 target_addr_t addr;
3347 unsigned long length;
3348 char const *parse = packet + 12;
3349
3350 if (*parse == '\0') {
3351 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3352 return ERROR_SERVER_REMOTE_CLOSED;
3353 }
3354
3355 addr = strtoull(parse, (char **)&parse, 16);
3356 if (*(parse++) != ':') {
3357 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3358 return ERROR_SERVER_REMOTE_CLOSED;
3359 }
3360 length = packet_size - (parse - packet);
3361
3362 /* create a new image if there isn't already one */
3363 if (!gdb_connection->vflash_image) {
3364 gdb_connection->vflash_image = malloc(sizeof(struct image));
3365 image_open(gdb_connection->vflash_image, "", "build");
3366 }
3367
3368 /* create new section with content from packet buffer */
3369 retval = image_add_section(gdb_connection->vflash_image,
3370 addr, length, 0x0, (uint8_t const *)parse);
3371 if (retval != ERROR_OK)
3372 return retval;
3373
3374 gdb_put_packet(connection, "OK", 2);
3375
3376 return ERROR_OK;
3377 }
3378
3379 if (strncmp(packet, "vFlashDone", 10) == 0) {
3380 uint32_t written;
3381
3382 /* process the flashing buffer. No need to erase as GDB
3383 * always issues a vFlashErase first. */
3384 target_call_event_callbacks(target,
3385 TARGET_EVENT_GDB_FLASH_WRITE_START);
3386 result = flash_write(target, gdb_connection->vflash_image,
3387 &written, false);
3388 target_call_event_callbacks(target,
3389 TARGET_EVENT_GDB_FLASH_WRITE_END);
3390 if (result != ERROR_OK) {
3391 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
3392 gdb_put_packet(connection, "E.memtype", 9);
3393 else
3394 gdb_send_error(connection, EIO);
3395 } else {
3396 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
3397 gdb_put_packet(connection, "OK", 2);
3398 }
3399
3400 image_close(gdb_connection->vflash_image);
3401 free(gdb_connection->vflash_image);
3402 gdb_connection->vflash_image = NULL;
3403
3404 return ERROR_OK;
3405 }
3406
3407 gdb_put_packet(connection, "", 0);
3408 return ERROR_OK;
3409 }
3410
3411 static int gdb_detach(struct connection *connection)
3412 {
3413 /*
3414 * Only reply "OK" to GDB
3415 * it will close the connection and this will trigger a call to
3416 * gdb_connection_closed() that will in turn trigger the event
3417 * TARGET_EVENT_GDB_DETACH
3418 */
3419 return gdb_put_packet(connection, "OK", 2);
3420 }
3421
3422 /* The format of 'F' response packet is
3423 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3424 */
3425 static int gdb_fileio_response_packet(struct connection *connection,
3426 char const *packet, int packet_size)
3427 {
3428 struct target *target = get_target_from_connection(connection);
3429 char *separator;
3430 char *parsing_point;
3431 int fileio_retcode = strtoul(packet + 1, &separator, 16);
3432 int fileio_errno = 0;
3433 bool fileio_ctrl_c = false;
3434 int retval;
3435
3436 LOG_DEBUG("-");
3437
3438 if (*separator == ',') {
3439 parsing_point = separator + 1;
3440 fileio_errno = strtoul(parsing_point, &separator, 16);
3441 if (*separator == ',') {
3442 if (*(separator + 1) == 'C') {
3443 /* TODO: process ctrl-c */
3444 fileio_ctrl_c = true;
3445 }
3446 }
3447 }
3448
3449 LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3450 fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false");
3451
3452 retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c);
3453 if (retval != ERROR_OK)
3454 return ERROR_FAIL;
3455
3456 /* After File-I/O ends, keep continue or step */
3457 if (gdb_running_type == 'c')
3458 retval = target_resume(target, 1, 0x0, 0, 0);
3459 else if (gdb_running_type == 's')
3460 retval = target_step(target, 1, 0x0, 0);
3461 else
3462 retval = ERROR_FAIL;
3463
3464 if (retval != ERROR_OK)
3465 return ERROR_FAIL;
3466
3467 return ERROR_OK;
3468 }
3469
3470 static void gdb_log_callback(void *priv, const char *file, unsigned line,
3471 const char *function, const char *string)
3472 {
3473 struct connection *connection = priv;
3474 struct gdb_connection *gdb_con = connection->priv;
3475
3476 if (gdb_con->output_flag == GDB_OUTPUT_NO)
3477 /* No out allowed */
3478 return;
3479
3480 if (gdb_con->busy) {
3481 /* do not reply this using the O packet */
3482 return;
3483 }
3484
3485 gdb_output_con(connection, string);
3486 }
3487
3488 static void gdb_sig_halted(struct connection *connection)
3489 {
3490 char sig_reply[4];
3491 snprintf(sig_reply, 4, "T%2.2x", 2);
3492 gdb_put_packet(connection, sig_reply, 3);
3493 }
3494
3495 static int gdb_input_inner(struct connection *connection)
3496 {
3497 /* Do not allocate this on the stack */
3498 static char gdb_packet_buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for null-termination */
3499
3500 struct target *target;
3501 char const *packet = gdb_packet_buffer;
3502 int packet_size;
3503 int retval;
3504 struct gdb_connection *gdb_con = connection->priv;
3505 static bool warn_use_ext;
3506
3507 target = get_target_from_connection(connection);
3508
3509 /* drain input buffer. If one of the packets fail, then an error
3510 * packet is replied, if applicable.
3511 *
3512 * This loop will terminate and the error code is returned.
3513 *
3514 * The calling fn will check if this error is something that
3515 * can be recovered from, or if the connection must be closed.
3516 *
3517 * If the error is recoverable, this fn is called again to
3518 * drain the rest of the buffer.
3519 */
3520 do {
3521 packet_size = GDB_BUFFER_SIZE;
3522 retval = gdb_get_packet(connection, gdb_packet_buffer, &packet_size);
3523 if (retval != ERROR_OK)
3524 return retval;
3525
3526 /* terminate with zero */
3527 gdb_packet_buffer[packet_size] = '\0';
3528
3529 if (packet_size > 0) {
3530
3531 gdb_log_incoming_packet(connection, gdb_packet_buffer);
3532
3533 retval = ERROR_OK;
3534 switch (packet[0]) {
3535 case 'T': /* Is thread alive? */
3536 gdb_thread_packet(connection, packet, packet_size);
3537 break;
3538 case 'H': /* Set current thread ( 'c' for step and continue,
3539 * 'g' for all other operations ) */
3540 gdb_thread_packet(connection, packet, packet_size);
3541 break;
3542 case 'q':
3543 case 'Q':
3544 retval = gdb_thread_packet(connection, packet, packet_size);
3545 if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
3546 retval = gdb_query_packet(connection, packet, packet_size);
3547 break;
3548 case 'g':
3549 retval = gdb_get_registers_packet(connection, packet, packet_size);
3550 break;
3551 case 'G':
3552 retval = gdb_set_registers_packet(connection, packet, packet_size);
3553 break;
3554 case 'p':
3555 retval = gdb_get_register_packet(connection, packet, packet_size);
3556 break;
3557 case 'P':
3558 retval = gdb_set_register_packet(connection, packet, packet_size);
3559 break;
3560 case 'm':
3561 retval = gdb_read_memory_packet(connection, packet, packet_size);
3562 break;
3563 case 'M':
3564 retval = gdb_write_memory_packet(connection, packet, packet_size);
3565 break;
3566 case 'z':
3567 case 'Z':
3568 retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
3569 break;
3570 case '?':
3571 gdb_last_signal_packet(connection, packet, packet_size);
3572 /* '?' is sent after the eventual '!' */
3573 if (!warn_use_ext && !gdb_con->extended_protocol) {
3574 warn_use_ext = true;
3575 LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3576 connection->service->port, connection->service->port);
3577 }
3578 break;
3579 case 'c':
3580 case 's':
3581 {
3582 gdb_thread_packet(connection, packet, packet_size);
3583 gdb_con->output_flag = GDB_OUTPUT_ALL;
3584
3585 if (gdb_con->mem_write_error) {
3586 LOG_ERROR("Memory write failure!");
3587
3588 /* now that we have reported the memory write error,
3589 * we can clear the condition */
3590 gdb_con->mem_write_error = false;
3591 }
3592
3593 bool nostep = false;
3594 bool already_running = false;
3595 if (target->state == TARGET_RUNNING) {
3596 LOG_WARNING("WARNING! The target is already running. "
3597 "All changes GDB did to registers will be discarded! "
3598 "Waiting for target to halt.");
3599 already_running = true;
3600 } else if (target->state != TARGET_HALTED) {
3601 LOG_WARNING("The target is not in the halted nor running stated, "
3602 "stepi/continue ignored.");
3603 nostep = true;
3604 } else if ((packet[0] == 's') && gdb_con->sync) {
3605 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3606 * sent by GDB first to OpenOCD, thus defeating the check to
3607 * make only the single stepping have the sync feature...
3608 */
3609 nostep = true;
3610 LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3611 "from the target.");
3612 }
3613 gdb_con->sync = false;
3614
3615 if (!already_running && nostep) {
3616 /* Either the target isn't in the halted state, then we can't
3617 * step/continue. This might be early setup, etc.
3618 *
3619 * Or we want to allow GDB to pick up a fresh set of
3620 * register values without modifying the target state.
3621 *
3622 */
3623 gdb_sig_halted(connection);
3624
3625 /* stop forwarding log packets! */
3626 gdb_con->output_flag = GDB_OUTPUT_NO;
3627 } else {
3628 /* We're running/stepping, in which case we can
3629 * forward log output until the target is halted
3630 */
3631 gdb_con->frontend_state = TARGET_RUNNING;
3632 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
3633
3634 if (!already_running) {
3635 /* Here we don't want packet processing to stop even if this fails,
3636 * so we use a local variable instead of retval. */
3637 retval = gdb_step_continue_packet(connection, packet, packet_size);
3638 if (retval != ERROR_OK) {
3639 /* we'll never receive a halted
3640 * condition... issue a false one..
3641 */
3642 gdb_frontend_halted(target, connection);
3643 }
3644 }
3645 }
3646 }
3647 break;
3648 case 'v':
3649 retval = gdb_v_packet(connection, packet, packet_size);
3650 break;
3651 case 'D':
3652 retval = gdb_detach(connection);
3653 break;
3654 case 'X':
3655 retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
3656 if (retval != ERROR_OK)
3657 return retval;
3658 break;
3659 case 'k':
3660 if (gdb_con->extended_protocol) {
3661 gdb_con->attached = false;
3662 break;
3663 }
3664 gdb_put_packet(connection, "OK", 2);
3665 return ERROR_SERVER_REMOTE_CLOSED;
3666 case '!':
3667 /* handle extended remote protocol */
3668 gdb_con->extended_protocol = true;
3669 gdb_put_packet(connection, "OK", 2);
3670 break;
3671 case 'R':
3672 /* handle extended restart packet */
3673 gdb_restart_inferior(connection, packet, packet_size);
3674 break;
3675
3676 case 'j':
3677 /* DEPRECATED */
3678 /* packet supported only by smp target i.e cortex_a.c*/
3679 /* handle smp packet replying coreid played to gbd */
3680 gdb_read_smp_packet(connection, packet, packet_size);
3681 break;
3682
3683 case 'J':
3684 /* DEPRECATED */
3685 /* packet supported only by smp target i.e cortex_a.c */
3686 /* handle smp packet setting coreid to be played at next
3687 * resume to gdb */
3688 gdb_write_smp_packet(connection, packet, packet_size);
3689 break;
3690
3691 case 'F':
3692 /* File-I/O extension */
3693 /* After gdb uses host-side syscall to complete target file
3694 * I/O, gdb sends host-side syscall return value to target
3695 * by 'F' packet.
3696 * The format of 'F' response packet is
3697 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3698 */
3699 gdb_con->frontend_state = TARGET_RUNNING;
3700 gdb_con->output_flag = GDB_OUTPUT_ALL;
3701 gdb_fileio_response_packet(connection, packet, packet_size);
3702 break;
3703
3704 default:
3705 /* ignore unknown packets */
3706 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3707 gdb_put_packet(connection, "", 0);
3708 break;
3709 }
3710
3711 /* if a packet handler returned an error, exit input loop */
3712 if (retval != ERROR_OK)
3713 return retval;
3714 }
3715
3716 if (gdb_con->ctrl_c) {
3717 if (target->state == TARGET_RUNNING) {
3718 struct target *t = target;
3719 if (target->rtos)
3720 target->rtos->gdb_target_for_threadid(connection, target->rtos->current_threadid, &t);
3721 retval = target_halt(t);
3722 if (retval == ERROR_OK)
3723 retval = target_poll(t);
3724 if (retval != ERROR_OK)
3725 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
3726 gdb_con->ctrl_c = false;
3727 } else {
3728 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3729 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
3730 }
3731 }
3732
3733 } while (gdb_con->buf_cnt > 0);
3734
3735 return ERROR_OK;
3736 }
3737
3738 static int gdb_input(struct connection *connection)
3739 {
3740 int retval = gdb_input_inner(connection);
3741 struct gdb_connection *gdb_con = connection->priv;
3742 if (retval == ERROR_SERVER_REMOTE_CLOSED)
3743 return retval;
3744
3745 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3746 if (gdb_con->closed)
3747 return ERROR_SERVER_REMOTE_CLOSED;
3748
3749 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3750 return ERROR_OK;
3751 }
3752
3753 static void gdb_keep_client_alive(struct connection *connection)
3754 {
3755 struct gdb_connection *gdb_con = connection->priv;
3756
3757 if (gdb_con->busy) {
3758 /* do not send packets, retry asap */
3759 return;
3760 }
3761
3762 switch (gdb_con->output_flag) {
3763 case GDB_OUTPUT_NO:
3764 /* no need for keep-alive */
3765 break;
3766 case GDB_OUTPUT_ALL:
3767 /* send an empty O packet */
3768 gdb_output_con(connection, "");
3769 break;
3770 default:
3771 break;
3772 }
3773 }
3774
3775 static const struct service_driver gdb_service_driver = {
3776 .name = "gdb",
3777 .new_connection_during_keep_alive_handler = NULL,
3778 .new_connection_handler = gdb_new_connection,
3779 .input_handler = gdb_input,
3780 .connection_closed_handler = gdb_connection_closed,
3781 .keep_client_alive_handler = gdb_keep_client_alive,
3782 };
3783
3784 static int gdb_target_start(struct target *target, const char *port)
3785 {
3786 struct gdb_service *gdb_service;
3787 int ret;
3788 gdb_service = malloc(sizeof(struct gdb_service));
3789
3790 if (!gdb_service)
3791 return -ENOMEM;
3792
3793 LOG_INFO("starting gdb server for %s on %s", target_name(target), port);
3794
3795 gdb_service->target = target;
3796 gdb_service->core[0] = -1;
3797 gdb_service->core[1] = -1;
3798 target->gdb_service = gdb_service;
3799
3800 ret = add_service(&gdb_service_driver, port, target->gdb_max_connections, gdb_service);
3801 /* initialize all targets gdb service with the same pointer */
3802 {
3803 struct target_list *head;
3804 foreach_smp_target(head, target->smp_targets) {
3805 struct target *curr = head->target;
3806 if (curr != target)
3807 curr->gdb_service = gdb_service;
3808 }
3809 }
3810 return ret;
3811 }
3812
3813 static int gdb_target_add_one(struct target *target)
3814 {
3815 /* one gdb instance per smp list */
3816 if ((target->smp) && (target->gdb_service))
3817 return ERROR_OK;
3818
3819 /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3820 if (!target_supports_gdb_connection(target)) {
3821 LOG_DEBUG("skip gdb server for target %s", target_name(target));
3822 return ERROR_OK;
3823 }
3824
3825 if (target->gdb_port_override) {
3826 if (strcmp(target->gdb_port_override, "disabled") == 0) {
3827 LOG_INFO("gdb port disabled");
3828 return ERROR_OK;
3829 }
3830 return gdb_target_start(target, target->gdb_port_override);
3831 }
3832
3833 if (strcmp(gdb_port, "disabled") == 0) {
3834 LOG_INFO("gdb port disabled");
3835 return ERROR_OK;
3836 }
3837
3838 int retval = gdb_target_start(target, gdb_port_next);
3839 if (retval == ERROR_OK) {
3840 /* save the port number so can be queried with
3841 * $target_name cget -gdb-port
3842 */
3843 target->gdb_port_override = strdup(gdb_port_next);
3844
3845 long portnumber;
3846 /* If we can parse the port number
3847 * then we increment the port number for the next target.
3848 */
3849 char *end;
3850 portnumber = strtol(gdb_port_next, &end, 0);
3851 if (!*end) {
3852 if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
3853 free(gdb_port_next);
3854 if (portnumber) {
3855 gdb_port_next = alloc_printf("%ld", portnumber+1);
3856 } else {
3857 /* Don't increment if gdb_port is 0, since we're just
3858 * trying to allocate an unused port. */
3859 gdb_port_next = strdup("0");
3860 }
3861 }
3862 }
3863 }
3864 return retval;
3865 }
3866
3867 int gdb_target_add_all(struct target *target)
3868 {
3869 if (!target) {
3870 LOG_WARNING("gdb services need one or more targets defined");
3871 return ERROR_OK;
3872 }
3873
3874 while (target) {
3875 int retval = gdb_target_add_one(target);
3876 if (retval != ERROR_OK)
3877 return retval;
3878
3879 target = target->next;
3880 }
3881
3882 return ERROR_OK;
3883 }
3884
3885 COMMAND_HANDLER(handle_gdb_sync_command)
3886 {
3887 if (CMD_ARGC != 0)
3888 return ERROR_COMMAND_SYNTAX_ERROR;
3889
3890 if (!current_gdb_connection) {
3891 command_print(CMD,
3892 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3893 return ERROR_FAIL;
3894 }
3895
3896 current_gdb_connection->sync = true;
3897
3898 return ERROR_OK;
3899 }
3900
3901 /* daemon configuration command gdb_port */
3902 COMMAND_HANDLER(handle_gdb_port_command)
3903 {
3904 int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
3905 if (retval == ERROR_OK) {
3906 free(gdb_port_next);
3907 gdb_port_next = strdup(gdb_port);
3908 }
3909 return retval;
3910 }
3911
3912 COMMAND_HANDLER(handle_gdb_memory_map_command)
3913 {
3914 if (CMD_ARGC != 1)
3915 return ERROR_COMMAND_SYNTAX_ERROR;
3916
3917 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
3918 return ERROR_OK;
3919 }
3920
3921 COMMAND_HANDLER(handle_gdb_flash_program_command)
3922 {
3923 if (CMD_ARGC != 1)
3924 return ERROR_COMMAND_SYNTAX_ERROR;
3925
3926 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
3927 return ERROR_OK;
3928 }
3929
3930 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
3931 {
3932 if (CMD_ARGC != 1)
3933 return ERROR_COMMAND_SYNTAX_ERROR;
3934
3935 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
3936 return ERROR_OK;
3937 }
3938
3939 COMMAND_HANDLER(handle_gdb_report_register_access_error)
3940 {
3941 if (CMD_ARGC != 1)
3942 return ERROR_COMMAND_SYNTAX_ERROR;
3943
3944 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_register_access_error);
3945 return ERROR_OK;
3946 }
3947
3948 /* gdb_breakpoint_override */
3949 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
3950 {
3951 if (CMD_ARGC == 0) {
3952 /* nothing */
3953 } else if (CMD_ARGC == 1) {
3954 gdb_breakpoint_override = 1;
3955 if (strcmp(CMD_ARGV[0], "hard") == 0)
3956 gdb_breakpoint_override_type = BKPT_HARD;
3957 else if (strcmp(CMD_ARGV[0], "soft") == 0)
3958 gdb_breakpoint_override_type = BKPT_SOFT;
3959 else if (strcmp(CMD_ARGV[0], "disable") == 0)
3960 gdb_breakpoint_override = 0;
3961 } else
3962 return ERROR_COMMAND_SYNTAX_ERROR;
3963 if (gdb_breakpoint_override)
3964 LOG_USER("force %s breakpoints",
3965 (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
3966 else
3967 LOG_USER("breakpoint type is not overridden");
3968
3969 return ERROR_OK;
3970 }
3971
3972 COMMAND_HANDLER(handle_gdb_target_description_command)
3973 {
3974 if (CMD_ARGC != 1)
3975 return ERROR_COMMAND_SYNTAX_ERROR;
3976
3977 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_target_description);
3978 return ERROR_OK;
3979 }
3980
3981 COMMAND_HANDLER(handle_gdb_save_tdesc_command)
3982 {
3983 char *tdesc;
3984 uint32_t tdesc_length;
3985 struct target *target = get_current_target(CMD_CTX);
3986
3987 int retval = gdb_generate_target_description(target, &tdesc);
3988 if (retval != ERROR_OK) {
3989 LOG_ERROR("Unable to Generate Target Description");
3990 return ERROR_FAIL;
3991 }
3992
3993 tdesc_length = strlen(tdesc);
3994
3995 struct fileio *fileio;
3996 size_t size_written;
3997
3998 char *tdesc_filename = alloc_printf("%s.xml", target_type_name(target));
3999 if (!tdesc_filename) {
4000 retval = ERROR_FAIL;
4001 goto out;
4002 }
4003
4004 retval = fileio_open(&fileio, tdesc_filename, FILEIO_WRITE, FILEIO_TEXT);
4005
4006 if (retval != ERROR_OK) {
4007 LOG_ERROR("Can't open %s for writing", tdesc_filename);
4008 goto out;
4009 }
4010
4011 retval = fileio_write(fileio, tdesc_length, tdesc, &size_written);
4012
4013 fileio_close(fileio);
4014
4015 if (retval != ERROR_OK)
4016 LOG_ERROR("Error while writing the tdesc file");
4017
4018 out:
4019 free(tdesc_filename);
4020 free(tdesc);
4021
4022 return retval;
4023 }
4024
4025 static const struct command_registration gdb_command_handlers[] = {
4026 {
4027 .name = "gdb_sync",
4028 .handler = handle_gdb_sync_command,
4029 .mode = COMMAND_ANY,
4030 .help = "next stepi will return immediately allowing "
4031 "GDB to fetch register state without affecting "
4032 "target state",
4033 .usage = ""
4034 },
4035 {
4036 .name = "gdb_port",
4037 .handler = handle_gdb_port_command,
4038 .mode = COMMAND_CONFIG,
4039 .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4040 "server listens for the next port number after the "
4041 "base port number specified. "
4042 "No arguments reports GDB port. \"pipe\" means listen to stdin "
4043 "output to stdout, an integer is base port number, \"disabled\" disables "
4044 "port. Any other string is are interpreted as named pipe to listen to. "
4045 "Output pipe is the same name as input pipe, but with 'o' appended.",
4046 .usage = "[port_num]",
4047 },
4048 {
4049 .name = "gdb_memory_map",
4050 .handler = handle_gdb_memory_map_command,
4051 .mode = COMMAND_CONFIG,
4052 .help = "enable or disable memory map",
4053 .usage = "('enable'|'disable')"
4054 },
4055 {
4056 .name = "gdb_flash_program",
4057 .handler = handle_gdb_flash_program_command,
4058 .mode = COMMAND_CONFIG,
4059 .help = "enable or disable flash program",
4060 .usage = "('enable'|'disable')"
4061 },
4062 {
4063 .name = "gdb_report_data_abort",
4064 .handler = handle_gdb_report_data_abort_command,
4065 .mode = COMMAND_CONFIG,
4066 .help = "enable or disable reporting data aborts",
4067 .usage = "('enable'|'disable')"
4068 },
4069 {
4070 .name = "gdb_report_register_access_error",
4071 .handler = handle_gdb_report_register_access_error,
4072 .mode = COMMAND_CONFIG,
4073 .help = "enable or disable reporting register access errors",
4074 .usage = "('enable'|'disable')"
4075 },
4076 {
4077 .name = "gdb_breakpoint_override",
4078 .handler = handle_gdb_breakpoint_override_command,
4079 .mode = COMMAND_ANY,
4080 .help = "Display or specify type of breakpoint "
4081 "to be used by gdb 'break' commands.",
4082 .usage = "('hard'|'soft'|'disable')"
4083 },
4084 {
4085 .name = "gdb_target_description",
4086 .handler = handle_gdb_target_description_command,
4087 .mode = COMMAND_CONFIG,
4088 .help = "enable or disable target description",
4089 .usage = "('enable'|'disable')"
4090 },
4091 {
4092 .name = "gdb_save_tdesc",
4093 .handler = handle_gdb_save_tdesc_command,
4094 .mode = COMMAND_EXEC,
4095 .help = "Save the target description file",
4096 .usage = "",
4097 },
4098 COMMAND_REGISTRATION_DONE
4099 };
4100
4101 int gdb_register_commands(struct command_context *cmd_ctx)
4102 {
4103 gdb_port = strdup("3333");
4104 gdb_port_next = strdup("3333");
4105 return register_commands(cmd_ctx, NULL, gdb_command_handlers);
4106 }
4107
4108 void gdb_service_free(void)
4109 {
4110 free(gdb_port);
4111 free(gdb_port_next);
4112 }
4113
4114 int gdb_get_actual_connections(void)
4115 {
4116 return gdb_actual_connections;
4117 }

Linking to existing account procedure

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

SSH host keys fingerprints

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