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

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)