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

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)