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

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)