gdb_server: remove call to jtag_execute_queue()
[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 + 1]; /* Extra byte for nul-termination */
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 /* If set, errors when accessing registers are reported to gdb. Disabled by
134 * default. */
135 static int gdb_report_register_access_error;
136
137 /* set if we are sending target descriptions to gdb
138 * via qXfer:features:read packet */
139 /* enabled by default */
140 static int gdb_use_target_description = 1;
141
142 /* current processing free-run type, used by file-I/O */
143 static char gdb_running_type;
144
145 static int gdb_last_signal(struct target *target)
146 {
147 switch (target->debug_reason) {
148 case DBG_REASON_DBGRQ:
149 return 0x2; /* SIGINT */
150 case DBG_REASON_BREAKPOINT:
151 case DBG_REASON_WATCHPOINT:
152 case DBG_REASON_WPTANDBKPT:
153 return 0x05; /* SIGTRAP */
154 case DBG_REASON_SINGLESTEP:
155 return 0x05; /* SIGTRAP */
156 case DBG_REASON_EXC_CATCH:
157 return 0x05;
158 case DBG_REASON_NOTHALTED:
159 return 0x0; /* no signal... shouldn't happen */
160 default:
161 LOG_USER("undefined debug reason %d - target needs reset",
162 target->debug_reason);
163 return 0x0;
164 }
165 }
166
167 static int check_pending(struct connection *connection,
168 int timeout_s, int *got_data)
169 {
170 /* a non-blocking socket will block if there is 0 bytes available on the socket,
171 * but return with as many bytes as are available immediately
172 */
173 struct timeval tv;
174 fd_set read_fds;
175 struct gdb_connection *gdb_con = connection->priv;
176 int t;
177 if (got_data == NULL)
178 got_data = &t;
179 *got_data = 0;
180
181 if (gdb_con->buf_cnt > 0) {
182 *got_data = 1;
183 return ERROR_OK;
184 }
185
186 FD_ZERO(&read_fds);
187 FD_SET(connection->fd, &read_fds);
188
189 tv.tv_sec = timeout_s;
190 tv.tv_usec = 0;
191 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0) {
192 /* This can typically be because a "monitor" command took too long
193 * before printing any progress messages
194 */
195 if (timeout_s > 0)
196 return ERROR_GDB_TIMEOUT;
197 else
198 return ERROR_OK;
199 }
200 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
201 return ERROR_OK;
202 }
203
204 static int gdb_get_char_inner(struct connection *connection, int *next_char)
205 {
206 struct gdb_connection *gdb_con = connection->priv;
207 int retval = ERROR_OK;
208
209 #ifdef _DEBUG_GDB_IO_
210 char *debug_buffer;
211 #endif
212 for (;; ) {
213 if (connection->service->type != CONNECTION_TCP)
214 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
215 else {
216 retval = check_pending(connection, 1, NULL);
217 if (retval != ERROR_OK)
218 return retval;
219 gdb_con->buf_cnt = read_socket(connection->fd,
220 gdb_con->buffer,
221 GDB_BUFFER_SIZE);
222 }
223
224 if (gdb_con->buf_cnt > 0)
225 break;
226 if (gdb_con->buf_cnt == 0) {
227 gdb_con->closed = true;
228 return ERROR_SERVER_REMOTE_CLOSED;
229 }
230
231 #ifdef _WIN32
232 errno = WSAGetLastError();
233
234 switch (errno) {
235 case WSAEWOULDBLOCK:
236 usleep(1000);
237 break;
238 case WSAECONNABORTED:
239 gdb_con->closed = true;
240 return ERROR_SERVER_REMOTE_CLOSED;
241 case WSAECONNRESET:
242 gdb_con->closed = true;
243 return ERROR_SERVER_REMOTE_CLOSED;
244 default:
245 LOG_ERROR("read: %d", errno);
246 exit(-1);
247 }
248 #else
249 switch (errno) {
250 case EAGAIN:
251 usleep(1000);
252 break;
253 case ECONNABORTED:
254 gdb_con->closed = true;
255 return ERROR_SERVER_REMOTE_CLOSED;
256 case ECONNRESET:
257 gdb_con->closed = true;
258 return ERROR_SERVER_REMOTE_CLOSED;
259 default:
260 LOG_ERROR("read: %s", strerror(errno));
261 gdb_con->closed = true;
262 return ERROR_SERVER_REMOTE_CLOSED;
263 }
264 #endif
265 }
266
267 #ifdef _DEBUG_GDB_IO_
268 debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
269 LOG_DEBUG("received '%s'", debug_buffer);
270 free(debug_buffer);
271 #endif
272
273 gdb_con->buf_p = gdb_con->buffer;
274 gdb_con->buf_cnt--;
275 *next_char = *(gdb_con->buf_p++);
276 if (gdb_con->buf_cnt > 0)
277 connection->input_pending = 1;
278 else
279 connection->input_pending = 0;
280 #ifdef _DEBUG_GDB_IO_
281 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
282 #endif
283
284 return retval;
285 }
286
287 /**
288 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
289 * held in registers in the inner loop.
290 *
291 * For small caches and embedded systems this is important!
292 */
293 static inline int gdb_get_char_fast(struct connection *connection,
294 int *next_char, char **buf_p, int *buf_cnt)
295 {
296 int retval = ERROR_OK;
297
298 if ((*buf_cnt)-- > 0) {
299 *next_char = **buf_p;
300 (*buf_p)++;
301 if (*buf_cnt > 0)
302 connection->input_pending = 1;
303 else
304 connection->input_pending = 0;
305
306 #ifdef _DEBUG_GDB_IO_
307 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
308 #endif
309
310 return ERROR_OK;
311 }
312
313 struct gdb_connection *gdb_con = connection->priv;
314 gdb_con->buf_p = *buf_p;
315 gdb_con->buf_cnt = *buf_cnt;
316 retval = gdb_get_char_inner(connection, next_char);
317 *buf_p = gdb_con->buf_p;
318 *buf_cnt = gdb_con->buf_cnt;
319
320 return retval;
321 }
322
323 static int gdb_get_char(struct connection *connection, int *next_char)
324 {
325 struct gdb_connection *gdb_con = connection->priv;
326 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
327 }
328
329 static int gdb_putback_char(struct connection *connection, int last_char)
330 {
331 struct gdb_connection *gdb_con = connection->priv;
332
333 if (gdb_con->buf_p > gdb_con->buffer) {
334 *(--gdb_con->buf_p) = last_char;
335 gdb_con->buf_cnt++;
336 } else
337 LOG_ERROR("BUG: couldn't put character back");
338
339 return ERROR_OK;
340 }
341
342 /* The only way we can detect that the socket is closed is the first time
343 * we write to it, we will fail. Subsequent write operations will
344 * succeed. Shudder! */
345 static int gdb_write(struct connection *connection, void *data, int len)
346 {
347 struct gdb_connection *gdb_con = connection->priv;
348 if (gdb_con->closed)
349 return ERROR_SERVER_REMOTE_CLOSED;
350
351 if (connection_write(connection, data, len) == len)
352 return ERROR_OK;
353 gdb_con->closed = true;
354 return ERROR_SERVER_REMOTE_CLOSED;
355 }
356
357 static int gdb_put_packet_inner(struct connection *connection,
358 char *buffer, int len)
359 {
360 int i;
361 unsigned char my_checksum = 0;
362 #ifdef _DEBUG_GDB_IO_
363 char *debug_buffer;
364 #endif
365 int reply;
366 int retval;
367 struct gdb_connection *gdb_con = connection->priv;
368
369 for (i = 0; i < len; i++)
370 my_checksum += buffer[i];
371
372 #ifdef _DEBUG_GDB_IO_
373 /*
374 * At this point we should have nothing in the input queue from GDB,
375 * however sometimes '-' is sent even though we've already received
376 * an ACK (+) for everything we've sent off.
377 */
378 int gotdata;
379 for (;; ) {
380 retval = check_pending(connection, 0, &gotdata);
381 if (retval != ERROR_OK)
382 return retval;
383 if (!gotdata)
384 break;
385 retval = gdb_get_char(connection, &reply);
386 if (retval != ERROR_OK)
387 return retval;
388 if (reply == '$') {
389 /* fix a problem with some IAR tools */
390 gdb_putback_char(connection, reply);
391 LOG_DEBUG("Unexpected start of new packet");
392 break;
393 }
394
395 LOG_WARNING("Discard unexpected char %c", reply);
396 }
397 #endif
398
399 while (1) {
400 #ifdef _DEBUG_GDB_IO_
401 debug_buffer = strndup(buffer, len);
402 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
403 free(debug_buffer);
404 #endif
405
406 char local_buffer[1024];
407 local_buffer[0] = '$';
408 if ((size_t)len + 4 <= sizeof(local_buffer)) {
409 /* performance gain on smaller packets by only a single call to gdb_write() */
410 memcpy(local_buffer + 1, buffer, len++);
411 len += snprintf(local_buffer + len, sizeof(local_buffer) - len, "#%02x", my_checksum);
412 retval = gdb_write(connection, local_buffer, len);
413 if (retval != ERROR_OK)
414 return retval;
415 } else {
416 /* larger packets are transmitted directly from caller supplied buffer
417 * by several calls to gdb_write() to avoid dynamic allocation */
418 snprintf(local_buffer + 1, sizeof(local_buffer) - 1, "#%02x", my_checksum);
419 retval = gdb_write(connection, local_buffer, 1);
420 if (retval != ERROR_OK)
421 return retval;
422 retval = gdb_write(connection, buffer, len);
423 if (retval != ERROR_OK)
424 return retval;
425 retval = gdb_write(connection, local_buffer + 1, 3);
426 if (retval != ERROR_OK)
427 return retval;
428 }
429
430 if (gdb_con->noack_mode)
431 break;
432
433 retval = gdb_get_char(connection, &reply);
434 if (retval != ERROR_OK)
435 return retval;
436
437 if (reply == '+')
438 break;
439 else if (reply == '-') {
440 /* Stop sending output packets for now */
441 log_remove_callback(gdb_log_callback, connection);
442 LOG_WARNING("negative reply, retrying");
443 } else if (reply == 0x3) {
444 gdb_con->ctrl_c = 1;
445 retval = gdb_get_char(connection, &reply);
446 if (retval != ERROR_OK)
447 return retval;
448 if (reply == '+')
449 break;
450 else if (reply == '-') {
451 /* Stop sending output packets for now */
452 log_remove_callback(gdb_log_callback, connection);
453 LOG_WARNING("negative reply, retrying");
454 } else if (reply == '$') {
455 LOG_ERROR("GDB missing ack(1) - assumed good");
456 gdb_putback_char(connection, reply);
457 return ERROR_OK;
458 } else {
459 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
460 gdb_con->closed = true;
461 return ERROR_SERVER_REMOTE_CLOSED;
462 }
463 } else if (reply == '$') {
464 LOG_ERROR("GDB missing ack(2) - assumed good");
465 gdb_putback_char(connection, reply);
466 return ERROR_OK;
467 } else {
468 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
469 reply);
470 gdb_con->closed = true;
471 return ERROR_SERVER_REMOTE_CLOSED;
472 }
473 }
474 if (gdb_con->closed)
475 return ERROR_SERVER_REMOTE_CLOSED;
476
477 return ERROR_OK;
478 }
479
480 int gdb_put_packet(struct connection *connection, char *buffer, int len)
481 {
482 struct gdb_connection *gdb_con = connection->priv;
483 gdb_con->busy = true;
484 int retval = gdb_put_packet_inner(connection, buffer, len);
485 gdb_con->busy = false;
486
487 /* we sent some data, reset timer for keep alive messages */
488 kept_alive();
489
490 return retval;
491 }
492
493 static inline int fetch_packet(struct connection *connection,
494 int *checksum_ok, int noack, int *len, char *buffer)
495 {
496 unsigned char my_checksum = 0;
497 char checksum[3];
498 int character;
499 int retval = ERROR_OK;
500
501 struct gdb_connection *gdb_con = connection->priv;
502 my_checksum = 0;
503 int count = 0;
504 count = 0;
505
506 /* move this over into local variables to use registers and give the
507 * more freedom to optimize */
508 char *buf_p = gdb_con->buf_p;
509 int buf_cnt = gdb_con->buf_cnt;
510
511 for (;; ) {
512 /* The common case is that we have an entire packet with no escape chars.
513 * We need to leave at least 2 bytes in the buffer to have
514 * gdb_get_char() update various bits and bobs correctly.
515 */
516 if ((buf_cnt > 2) && ((buf_cnt + count) < *len)) {
517 /* The compiler will struggle a bit with constant propagation and
518 * aliasing, so we help it by showing that these values do not
519 * change inside the loop
520 */
521 int i;
522 char *buf = buf_p;
523 int run = buf_cnt - 2;
524 i = 0;
525 int done = 0;
526 while (i < run) {
527 character = *buf++;
528 i++;
529 if (character == '#') {
530 /* Danger! character can be '#' when esc is
531 * used so we need an explicit boolean for done here. */
532 done = 1;
533 break;
534 }
535
536 if (character == '}') {
537 /* data transmitted in binary mode (X packet)
538 * uses 0x7d as escape character */
539 my_checksum += character & 0xff;
540 character = *buf++;
541 i++;
542 my_checksum += character & 0xff;
543 buffer[count++] = (character ^ 0x20) & 0xff;
544 } else {
545 my_checksum += character & 0xff;
546 buffer[count++] = character & 0xff;
547 }
548 }
549 buf_p += i;
550 buf_cnt -= i;
551 if (done)
552 break;
553 }
554 if (count > *len) {
555 LOG_ERROR("packet buffer too small");
556 retval = ERROR_GDB_BUFFER_TOO_SMALL;
557 break;
558 }
559
560 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
561 if (retval != ERROR_OK)
562 break;
563
564 if (character == '#')
565 break;
566
567 if (character == '}') {
568 /* data transmitted in binary mode (X packet)
569 * uses 0x7d as escape character */
570 my_checksum += character & 0xff;
571
572 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
573 if (retval != ERROR_OK)
574 break;
575
576 my_checksum += character & 0xff;
577 buffer[count++] = (character ^ 0x20) & 0xff;
578 } else {
579 my_checksum += character & 0xff;
580 buffer[count++] = character & 0xff;
581 }
582 }
583
584 gdb_con->buf_p = buf_p;
585 gdb_con->buf_cnt = buf_cnt;
586
587 if (retval != ERROR_OK)
588 return retval;
589
590 *len = count;
591
592 retval = gdb_get_char(connection, &character);
593 if (retval != ERROR_OK)
594 return retval;
595 checksum[0] = character;
596 retval = gdb_get_char(connection, &character);
597 if (retval != ERROR_OK)
598 return retval;
599 checksum[1] = character;
600 checksum[2] = 0;
601
602 if (!noack)
603 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
604
605 return ERROR_OK;
606 }
607
608 static int gdb_get_packet_inner(struct connection *connection,
609 char *buffer, int *len)
610 {
611 int character;
612 int retval;
613 struct gdb_connection *gdb_con = connection->priv;
614
615 while (1) {
616 do {
617 retval = gdb_get_char(connection, &character);
618 if (retval != ERROR_OK)
619 return retval;
620
621 #ifdef _DEBUG_GDB_IO_
622 LOG_DEBUG("character: '%c'", character);
623 #endif
624
625 switch (character) {
626 case '$':
627 break;
628 case '+':
629 /* According to the GDB documentation
630 * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
631 * "gdb sends a final `+` acknowledgment of the stub's `OK`
632 * response, which can be safely ignored by the stub."
633 * However OpenOCD server already is in noack mode at this
634 * point and instead of ignoring this it was emitting a
635 * warning. This code makes server ignore the first ACK
636 * that will be received after going into noack mode,
637 * warning only about subsequent ACK's. */
638 if (gdb_con->noack_mode > 1) {
639 LOG_WARNING("acknowledgment received, but no packet pending");
640 } else if (gdb_con->noack_mode) {
641 LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
642 gdb_con->noack_mode = 2;
643 }
644 break;
645 case '-':
646 LOG_WARNING("negative acknowledgment, but no packet pending");
647 break;
648 case 0x3:
649 gdb_con->ctrl_c = 1;
650 *len = 0;
651 return ERROR_OK;
652 default:
653 LOG_WARNING("ignoring character 0x%x", character);
654 break;
655 }
656 } while (character != '$');
657
658 int checksum_ok = 0;
659 /* explicit code expansion here to get faster inlined code in -O3 by not
660 * calculating checksum */
661 if (gdb_con->noack_mode) {
662 retval = fetch_packet(connection, &checksum_ok, 1, len, buffer);
663 if (retval != ERROR_OK)
664 return retval;
665 } else {
666 retval = fetch_packet(connection, &checksum_ok, 0, len, buffer);
667 if (retval != ERROR_OK)
668 return retval;
669 }
670
671 if (gdb_con->noack_mode) {
672 /* checksum is not checked in noack mode */
673 break;
674 }
675 if (checksum_ok) {
676 retval = gdb_write(connection, "+", 1);
677 if (retval != ERROR_OK)
678 return retval;
679 break;
680 }
681 }
682 if (gdb_con->closed)
683 return ERROR_SERVER_REMOTE_CLOSED;
684
685 return ERROR_OK;
686 }
687
688 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
689 {
690 struct gdb_connection *gdb_con = connection->priv;
691 gdb_con->busy = true;
692 int retval = gdb_get_packet_inner(connection, buffer, len);
693 gdb_con->busy = false;
694 return retval;
695 }
696
697 static int gdb_output_con(struct connection *connection, const char *line)
698 {
699 char *hex_buffer;
700 int bin_size;
701
702 bin_size = strlen(line);
703
704 hex_buffer = malloc(bin_size * 2 + 2);
705 if (hex_buffer == NULL)
706 return ERROR_GDB_BUFFER_TOO_SMALL;
707
708 hex_buffer[0] = 'O';
709 size_t pkt_len = hexify(hex_buffer + 1, (const uint8_t *)line, bin_size,
710 bin_size * 2 + 1);
711 int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1);
712
713 free(hex_buffer);
714 return retval;
715 }
716
717 static int gdb_output(struct command_context *context, const char *line)
718 {
719 /* this will be dumped to the log and also sent as an O packet if possible */
720 LOG_USER_N("%s", line);
721 return ERROR_OK;
722 }
723
724 static void gdb_signal_reply(struct target *target, struct connection *connection)
725 {
726 struct gdb_connection *gdb_connection = connection->priv;
727 char sig_reply[45];
728 char stop_reason[20];
729 char current_thread[25];
730 int sig_reply_len;
731 int signal_var;
732
733 rtos_update_threads(target);
734
735 if (target->debug_reason == DBG_REASON_EXIT) {
736 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "W00");
737 } else {
738 if (gdb_connection->ctrl_c) {
739 signal_var = 0x2;
740 } else
741 signal_var = gdb_last_signal(target);
742
743 stop_reason[0] = '\0';
744 if (target->debug_reason == DBG_REASON_WATCHPOINT) {
745 enum watchpoint_rw hit_wp_type;
746 target_addr_t hit_wp_address;
747
748 if (watchpoint_hit(target, &hit_wp_type, &hit_wp_address) == ERROR_OK) {
749
750 switch (hit_wp_type) {
751 case WPT_WRITE:
752 snprintf(stop_reason, sizeof(stop_reason),
753 "watch:%08" TARGET_PRIxADDR ";", hit_wp_address);
754 break;
755 case WPT_READ:
756 snprintf(stop_reason, sizeof(stop_reason),
757 "rwatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
758 break;
759 case WPT_ACCESS:
760 snprintf(stop_reason, sizeof(stop_reason),
761 "awatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
762 break;
763 default:
764 break;
765 }
766 }
767 }
768
769 current_thread[0] = '\0';
770 if (target->rtos != NULL) {
771 struct target *ct;
772 snprintf(current_thread, sizeof(current_thread), "thread:%016" PRIx64 ";",
773 target->rtos->current_thread);
774 target->rtos->current_threadid = target->rtos->current_thread;
775 target->rtos->gdb_target_for_threadid(connection, target->rtos->current_threadid, &ct);
776 if (!gdb_connection->ctrl_c)
777 signal_var = gdb_last_signal(ct);
778 }
779
780 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "T%2.2x%s%s",
781 signal_var, stop_reason, current_thread);
782
783 gdb_connection->ctrl_c = 0;
784 }
785
786 gdb_put_packet(connection, sig_reply, sig_reply_len);
787 gdb_connection->frontend_state = TARGET_HALTED;
788 }
789
790 static void gdb_fileio_reply(struct target *target, struct connection *connection)
791 {
792 struct gdb_connection *gdb_connection = connection->priv;
793 char fileio_command[256];
794 int command_len;
795 bool program_exited = false;
796
797 if (strcmp(target->fileio_info->identifier, "open") == 0)
798 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
799 target->fileio_info->param_1,
800 target->fileio_info->param_2,
801 target->fileio_info->param_3,
802 target->fileio_info->param_4);
803 else if (strcmp(target->fileio_info->identifier, "close") == 0)
804 sprintf(fileio_command, "F%s,%" PRIx64, target->fileio_info->identifier,
805 target->fileio_info->param_1);
806 else if (strcmp(target->fileio_info->identifier, "read") == 0)
807 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, 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, "write") == 0)
812 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, 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, "lseek") == 0)
817 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
818 target->fileio_info->param_1,
819 target->fileio_info->param_2,
820 target->fileio_info->param_3);
821 else if (strcmp(target->fileio_info->identifier, "rename") == 0)
822 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
823 target->fileio_info->param_1,
824 target->fileio_info->param_2,
825 target->fileio_info->param_3,
826 target->fileio_info->param_4);
827 else if (strcmp(target->fileio_info->identifier, "unlink") == 0)
828 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
829 target->fileio_info->param_1,
830 target->fileio_info->param_2);
831 else if (strcmp(target->fileio_info->identifier, "stat") == 0)
832 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
833 target->fileio_info->param_1,
834 target->fileio_info->param_2,
835 target->fileio_info->param_3);
836 else if (strcmp(target->fileio_info->identifier, "fstat") == 0)
837 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
838 target->fileio_info->param_1,
839 target->fileio_info->param_2);
840 else if (strcmp(target->fileio_info->identifier, "gettimeofday") == 0)
841 sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
842 target->fileio_info->param_1,
843 target->fileio_info->param_2);
844 else if (strcmp(target->fileio_info->identifier, "isatty") == 0)
845 sprintf(fileio_command, "F%s,%" PRIx64, target->fileio_info->identifier,
846 target->fileio_info->param_1);
847 else if (strcmp(target->fileio_info->identifier, "system") == 0)
848 sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
849 target->fileio_info->param_1,
850 target->fileio_info->param_2);
851 else if (strcmp(target->fileio_info->identifier, "exit") == 0) {
852 /* If target hits exit syscall, report to GDB the program is terminated.
853 * In addition, let target run its own exit syscall handler. */
854 program_exited = true;
855 sprintf(fileio_command, "W%02" PRIx64, target->fileio_info->param_1);
856 } else {
857 LOG_DEBUG("Unknown syscall: %s", target->fileio_info->identifier);
858
859 /* encounter unknown syscall, continue */
860 gdb_connection->frontend_state = TARGET_RUNNING;
861 target_resume(target, 1, 0x0, 0, 0);
862 return;
863 }
864
865 command_len = strlen(fileio_command);
866 gdb_put_packet(connection, fileio_command, command_len);
867
868 if (program_exited) {
869 /* Use target_resume() to let target run its own exit syscall handler. */
870 gdb_connection->frontend_state = TARGET_RUNNING;
871 target_resume(target, 1, 0x0, 0, 0);
872 } else {
873 gdb_connection->frontend_state = TARGET_HALTED;
874 rtos_update_threads(target);
875 }
876 }
877
878 static void gdb_frontend_halted(struct target *target, struct connection *connection)
879 {
880 struct gdb_connection *gdb_connection = connection->priv;
881
882 /* In the GDB protocol when we are stepping or continuing execution,
883 * we have a lingering reply. Upon receiving a halted event
884 * when we have that lingering packet, we reply to the original
885 * step or continue packet.
886 *
887 * Executing monitor commands can bring the target in and
888 * out of the running state so we'll see lots of TARGET_EVENT_XXX
889 * that are to be ignored.
890 */
891 if (gdb_connection->frontend_state == TARGET_RUNNING) {
892 /* stop forwarding log packets! */
893 log_remove_callback(gdb_log_callback, connection);
894
895 /* check fileio first */
896 if (target_get_gdb_fileio_info(target, target->fileio_info) == ERROR_OK)
897 gdb_fileio_reply(target, connection);
898 else
899 gdb_signal_reply(target, connection);
900 }
901 }
902
903 static int gdb_target_callback_event_handler(struct target *target,
904 enum target_event event, void *priv)
905 {
906 struct connection *connection = priv;
907 struct gdb_service *gdb_service = connection->service->priv;
908
909 if (gdb_service->target != target)
910 return ERROR_OK;
911
912 switch (event) {
913 case TARGET_EVENT_GDB_HALT:
914 gdb_frontend_halted(target, connection);
915 break;
916 case TARGET_EVENT_HALTED:
917 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
918 break;
919 default:
920 break;
921 }
922
923 return ERROR_OK;
924 }
925
926 static int gdb_new_connection(struct connection *connection)
927 {
928 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
929 struct target *target;
930 int retval;
931 int initial_ack;
932
933 target = get_target_from_connection(connection);
934 connection->priv = gdb_connection;
935 connection->cmd_ctx->current_target = target;
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 if (reg_list[i] == NULL || reg_list[i]->exist == false)
1180 continue;
1181 reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1182 }
1183
1184 assert(reg_packet_size > 0);
1185
1186 reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
1187 if (reg_packet == NULL)
1188 return ERROR_FAIL;
1189
1190 reg_packet_p = reg_packet;
1191
1192 for (i = 0; i < reg_list_size; i++) {
1193 if (reg_list[i] == NULL || reg_list[i]->exist == false)
1194 continue;
1195 if (!reg_list[i]->valid) {
1196 retval = reg_list[i]->type->get(reg_list[i]);
1197 if (retval != ERROR_OK && gdb_report_register_access_error) {
1198 LOG_DEBUG("Couldn't get register %s.", reg_list[i]->name);
1199 free(reg_packet);
1200 free(reg_list);
1201 return gdb_error(connection, retval);
1202 }
1203 }
1204 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
1205 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1206 }
1207
1208 #ifdef _DEBUG_GDB_IO_
1209 {
1210 char *reg_packet_p_debug;
1211 reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1212 LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1213 free(reg_packet_p_debug);
1214 }
1215 #endif
1216
1217 gdb_put_packet(connection, reg_packet, reg_packet_size);
1218 free(reg_packet);
1219
1220 free(reg_list);
1221
1222 return ERROR_OK;
1223 }
1224
1225 static int gdb_set_registers_packet(struct connection *connection,
1226 char const *packet, int packet_size)
1227 {
1228 struct target *target = get_target_from_connection(connection);
1229 int i;
1230 struct reg **reg_list;
1231 int reg_list_size;
1232 int retval;
1233 char const *packet_p;
1234
1235 #ifdef _DEBUG_GDB_IO_
1236 LOG_DEBUG("-");
1237 #endif
1238
1239 /* skip command character */
1240 packet++;
1241 packet_size--;
1242
1243 if (packet_size % 2) {
1244 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1245 return ERROR_SERVER_REMOTE_CLOSED;
1246 }
1247
1248 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1249 REG_CLASS_GENERAL);
1250 if (retval != ERROR_OK)
1251 return gdb_error(connection, retval);
1252
1253 packet_p = packet;
1254 for (i = 0; i < reg_list_size; i++) {
1255 uint8_t *bin_buf;
1256 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1257
1258 if (packet_p + chars > packet + packet_size)
1259 LOG_ERROR("BUG: register packet is too small for registers");
1260
1261 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1262 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1263
1264 retval = reg_list[i]->type->set(reg_list[i], bin_buf);
1265 if (retval != ERROR_OK && gdb_report_register_access_error) {
1266 LOG_DEBUG("Couldn't set register %s.", reg_list[i]->name);
1267 free(reg_list);
1268 free(bin_buf);
1269 return gdb_error(connection, retval);
1270 }
1271
1272 /* advance packet pointer */
1273 packet_p += chars;
1274
1275 free(bin_buf);
1276 }
1277
1278 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1279 free(reg_list);
1280
1281 gdb_put_packet(connection, "OK", 2);
1282
1283 return ERROR_OK;
1284 }
1285
1286 static int gdb_get_register_packet(struct connection *connection,
1287 char const *packet, int packet_size)
1288 {
1289 struct target *target = get_target_from_connection(connection);
1290 char *reg_packet;
1291 int reg_num = strtoul(packet + 1, NULL, 16);
1292 struct reg **reg_list;
1293 int reg_list_size;
1294 int retval;
1295
1296 #ifdef _DEBUG_GDB_IO_
1297 LOG_DEBUG("-");
1298 #endif
1299
1300 if ((target->rtos != NULL) && (ERROR_OK == rtos_get_gdb_reg(connection, reg_num)))
1301 return ERROR_OK;
1302
1303 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1304 REG_CLASS_ALL);
1305 if (retval != ERROR_OK)
1306 return gdb_error(connection, retval);
1307
1308 if (reg_list_size <= reg_num) {
1309 LOG_ERROR("gdb requested a non-existing register");
1310 return ERROR_SERVER_REMOTE_CLOSED;
1311 }
1312
1313 if (!reg_list[reg_num]->valid) {
1314 retval = reg_list[reg_num]->type->get(reg_list[reg_num]);
1315 if (retval != ERROR_OK && gdb_report_register_access_error) {
1316 LOG_DEBUG("Couldn't get register %s.", reg_list[reg_num]->name);
1317 free(reg_list);
1318 return gdb_error(connection, retval);
1319 }
1320 }
1321
1322 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1); /* plus one for string termination null */
1323
1324 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1325
1326 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1327
1328 free(reg_list);
1329 free(reg_packet);
1330
1331 return ERROR_OK;
1332 }
1333
1334 static int gdb_set_register_packet(struct connection *connection,
1335 char const *packet, int packet_size)
1336 {
1337 struct target *target = get_target_from_connection(connection);
1338 char *separator;
1339 uint8_t *bin_buf;
1340 int reg_num = strtoul(packet + 1, &separator, 16);
1341 struct reg **reg_list;
1342 int reg_list_size;
1343 int retval;
1344
1345 LOG_DEBUG("-");
1346
1347 retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1348 REG_CLASS_ALL);
1349 if (retval != ERROR_OK)
1350 return gdb_error(connection, retval);
1351
1352 if (reg_list_size <= reg_num) {
1353 LOG_ERROR("gdb requested a non-existing register");
1354 return ERROR_SERVER_REMOTE_CLOSED;
1355 }
1356
1357 if (*separator != '=') {
1358 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1359 return ERROR_SERVER_REMOTE_CLOSED;
1360 }
1361
1362 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1363 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1364 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1365
1366 if ((unsigned int)chars != strlen(separator + 1)) {
1367 LOG_ERROR("gdb sent %zu bits for a %d-bit register (%s)",
1368 strlen(separator + 1) * 4, chars * 4, reg_list[reg_num]->name);
1369 free(bin_buf);
1370 return ERROR_SERVER_REMOTE_CLOSED;
1371 }
1372
1373 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1374
1375 retval = reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1376 if (retval != ERROR_OK && gdb_report_register_access_error) {
1377 LOG_DEBUG("Couldn't set register %s.", reg_list[reg_num]->name);
1378 free(bin_buf);
1379 free(reg_list);
1380 return gdb_error(connection, retval);
1381 }
1382
1383 gdb_put_packet(connection, "OK", 2);
1384
1385 free(bin_buf);
1386 free(reg_list);
1387
1388 return ERROR_OK;
1389 }
1390
1391 /* No attempt is made to translate the "retval" to
1392 * GDB speak. This has to be done at the calling
1393 * site as no mapping really exists.
1394 */
1395 static int gdb_error(struct connection *connection, int retval)
1396 {
1397 LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1398 gdb_send_error(connection, EFAULT);
1399 return ERROR_OK;
1400 }
1401
1402 /* We don't have to worry about the default 2 second timeout for GDB packets,
1403 * because GDB breaks up large memory reads into smaller reads.
1404 */
1405 static int gdb_read_memory_packet(struct connection *connection,
1406 char const *packet, int packet_size)
1407 {
1408 struct target *target = get_target_from_connection(connection);
1409 char *separator;
1410 uint64_t addr = 0;
1411 uint32_t len = 0;
1412
1413 uint8_t *buffer;
1414 char *hex_buffer;
1415
1416 int retval = ERROR_OK;
1417
1418 /* skip command character */
1419 packet++;
1420
1421 addr = strtoull(packet, &separator, 16);
1422
1423 if (*separator != ',') {
1424 LOG_ERROR("incomplete read memory packet received, dropping connection");
1425 return ERROR_SERVER_REMOTE_CLOSED;
1426 }
1427
1428 len = strtoul(separator + 1, NULL, 16);
1429
1430 if (!len) {
1431 LOG_WARNING("invalid read memory packet received (len == 0)");
1432 gdb_put_packet(connection, "", 0);
1433 return ERROR_OK;
1434 }
1435
1436 buffer = malloc(len);
1437
1438 LOG_DEBUG("addr: 0x%16.16" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1439
1440 retval = target_read_buffer(target, addr, len, buffer);
1441
1442 if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1443 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1444 * At some point this might be fixed in GDB, in which case this code can be removed.
1445 *
1446 * OpenOCD developers are acutely aware of this problem, but there is nothing
1447 * gained by involving the user in this problem that hopefully will get resolved
1448 * eventually
1449 *
1450 * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1451 * cmd = view%20audit-trail&database = gdb&pr = 2395
1452 *
1453 * For now, the default is to fix up things to make current GDB versions work.
1454 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1455 */
1456 memset(buffer, 0, len);
1457 retval = ERROR_OK;
1458 }
1459
1460 if (retval == ERROR_OK) {
1461 hex_buffer = malloc(len * 2 + 1);
1462
1463 size_t pkt_len = hexify(hex_buffer, buffer, len, len * 2 + 1);
1464
1465 gdb_put_packet(connection, hex_buffer, pkt_len);
1466
1467 free(hex_buffer);
1468 } else
1469 retval = gdb_error(connection, retval);
1470
1471 free(buffer);
1472
1473 return retval;
1474 }
1475
1476 static int gdb_write_memory_packet(struct connection *connection,
1477 char const *packet, int packet_size)
1478 {
1479 struct target *target = get_target_from_connection(connection);
1480 char *separator;
1481 uint64_t addr = 0;
1482 uint32_t len = 0;
1483
1484 uint8_t *buffer;
1485 int retval;
1486
1487 /* skip command character */
1488 packet++;
1489
1490 addr = strtoull(packet, &separator, 16);
1491
1492 if (*separator != ',') {
1493 LOG_ERROR("incomplete write memory packet received, dropping connection");
1494 return ERROR_SERVER_REMOTE_CLOSED;
1495 }
1496
1497 len = strtoul(separator + 1, &separator, 16);
1498
1499 if (*(separator++) != ':') {
1500 LOG_ERROR("incomplete write memory packet received, dropping connection");
1501 return ERROR_SERVER_REMOTE_CLOSED;
1502 }
1503
1504 buffer = malloc(len);
1505
1506 LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1507
1508 if (unhexify(buffer, separator, len) != len)
1509 LOG_ERROR("unable to decode memory packet");
1510
1511 retval = target_write_buffer(target, addr, len, buffer);
1512
1513 if (retval == ERROR_OK)
1514 gdb_put_packet(connection, "OK", 2);
1515 else
1516 retval = gdb_error(connection, retval);
1517
1518 free(buffer);
1519
1520 return retval;
1521 }
1522
1523 static int gdb_write_memory_binary_packet(struct connection *connection,
1524 char const *packet, int packet_size)
1525 {
1526 struct target *target = get_target_from_connection(connection);
1527 char *separator;
1528 uint64_t addr = 0;
1529 uint32_t len = 0;
1530
1531 int retval = ERROR_OK;
1532 /* Packets larger than fast_limit bytes will be acknowledged instantly on
1533 * the assumption that we're in a download and it's important to go as fast
1534 * as possible. */
1535 uint32_t fast_limit = 8;
1536
1537 /* skip command character */
1538 packet++;
1539
1540 addr = strtoull(packet, &separator, 16);
1541
1542 if (*separator != ',') {
1543 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1544 return ERROR_SERVER_REMOTE_CLOSED;
1545 }
1546
1547 len = strtoul(separator + 1, &separator, 16);
1548
1549 if (*(separator++) != ':') {
1550 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1551 return ERROR_SERVER_REMOTE_CLOSED;
1552 }
1553
1554 struct gdb_connection *gdb_connection = connection->priv;
1555
1556 if (gdb_connection->mem_write_error)
1557 retval = ERROR_FAIL;
1558
1559 if (retval == ERROR_OK) {
1560 if (len >= fast_limit) {
1561 /* By replying the packet *immediately* GDB will send us a new packet
1562 * while we write the last one to the target.
1563 * We only do this for larger writes, so that users who do something like:
1564 * p *((int*)0xdeadbeef)=8675309
1565 * will get immediate feedback that that write failed.
1566 */
1567 gdb_put_packet(connection, "OK", 2);
1568 }
1569 } else {
1570 retval = gdb_error(connection, retval);
1571 /* now that we have reported the memory write error, we can clear the condition */
1572 gdb_connection->mem_write_error = false;
1573 if (retval != ERROR_OK)
1574 return retval;
1575 }
1576
1577 if (len) {
1578 LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1579
1580 retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1581 if (retval != ERROR_OK)
1582 gdb_connection->mem_write_error = true;
1583 }
1584
1585 if (len < fast_limit) {
1586 if (retval != ERROR_OK) {
1587 gdb_error(connection, retval);
1588 gdb_connection->mem_write_error = false;
1589 } else {
1590 gdb_put_packet(connection, "OK", 2);
1591 }
1592 }
1593
1594 return ERROR_OK;
1595 }
1596
1597 static int gdb_step_continue_packet(struct connection *connection,
1598 char const *packet, int packet_size)
1599 {
1600 struct target *target = get_target_from_connection(connection);
1601 int current = 0;
1602 uint64_t address = 0x0;
1603 int retval = ERROR_OK;
1604
1605 LOG_DEBUG("-");
1606
1607 if (packet_size > 1)
1608 address = strtoull(packet + 1, NULL, 16);
1609 else
1610 current = 1;
1611
1612 gdb_running_type = packet[0];
1613 if (packet[0] == 'c') {
1614 LOG_DEBUG("continue");
1615 /* resume at current address, don't handle breakpoints, not debugging */
1616 retval = target_resume(target, current, address, 0, 0);
1617 } else if (packet[0] == 's') {
1618 LOG_DEBUG("step");
1619 /* step at current or address, don't handle breakpoints */
1620 retval = target_step(target, current, address, 0);
1621 }
1622 return retval;
1623 }
1624
1625 static int gdb_breakpoint_watchpoint_packet(struct connection *connection,
1626 char const *packet, int packet_size)
1627 {
1628 struct target *target = get_target_from_connection(connection);
1629 int type;
1630 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1631 enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1632 uint64_t address;
1633 uint32_t size;
1634 char *separator;
1635 int retval;
1636
1637 LOG_DEBUG("-");
1638
1639 type = strtoul(packet + 1, &separator, 16);
1640
1641 if (type == 0) /* memory breakpoint */
1642 bp_type = BKPT_SOFT;
1643 else if (type == 1) /* hardware breakpoint */
1644 bp_type = BKPT_HARD;
1645 else if (type == 2) /* write watchpoint */
1646 wp_type = WPT_WRITE;
1647 else if (type == 3) /* read watchpoint */
1648 wp_type = WPT_READ;
1649 else if (type == 4) /* access watchpoint */
1650 wp_type = WPT_ACCESS;
1651 else {
1652 LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1653 return ERROR_SERVER_REMOTE_CLOSED;
1654 }
1655
1656 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1657 bp_type = gdb_breakpoint_override_type;
1658
1659 if (*separator != ',') {
1660 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1661 return ERROR_SERVER_REMOTE_CLOSED;
1662 }
1663
1664 address = strtoull(separator + 1, &separator, 16);
1665
1666 if (*separator != ',') {
1667 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1668 return ERROR_SERVER_REMOTE_CLOSED;
1669 }
1670
1671 size = strtoul(separator + 1, &separator, 16);
1672
1673 switch (type) {
1674 case 0:
1675 case 1:
1676 if (packet[0] == 'Z') {
1677 retval = breakpoint_add(target, address, size, bp_type);
1678 if (retval != ERROR_OK) {
1679 retval = gdb_error(connection, retval);
1680 if (retval != ERROR_OK)
1681 return retval;
1682 } else
1683 gdb_put_packet(connection, "OK", 2);
1684 } else {
1685 breakpoint_remove(target, address);
1686 gdb_put_packet(connection, "OK", 2);
1687 }
1688 break;
1689 case 2:
1690 case 3:
1691 case 4:
1692 {
1693 if (packet[0] == 'Z') {
1694 retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu);
1695 if (retval != ERROR_OK) {
1696 retval = gdb_error(connection, retval);
1697 if (retval != ERROR_OK)
1698 return retval;
1699 } else
1700 gdb_put_packet(connection, "OK", 2);
1701 } else {
1702 watchpoint_remove(target, address);
1703 gdb_put_packet(connection, "OK", 2);
1704 }
1705 break;
1706 }
1707 default:
1708 break;
1709 }
1710
1711 return ERROR_OK;
1712 }
1713
1714 /* print out a string and allocate more space as needed,
1715 * mainly used for XML at this point
1716 */
1717 static void xml_printf(int *retval, char **xml, int *pos, int *size,
1718 const char *fmt, ...)
1719 {
1720 if (*retval != ERROR_OK)
1721 return;
1722 int first = 1;
1723
1724 for (;; ) {
1725 if ((*xml == NULL) || (!first)) {
1726 /* start by 0 to exercise all the code paths.
1727 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1728
1729 *size = *size * 2 + 2;
1730 char *t = *xml;
1731 *xml = realloc(*xml, *size);
1732 if (*xml == NULL) {
1733 if (t)
1734 free(t);
1735 *retval = ERROR_SERVER_REMOTE_CLOSED;
1736 return;
1737 }
1738 }
1739
1740 va_list ap;
1741 int ret;
1742 va_start(ap, fmt);
1743 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1744 va_end(ap);
1745 if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1746 *pos += ret;
1747 return;
1748 }
1749 /* there was just enough or not enough space, allocate more. */
1750 first = 0;
1751 }
1752 }
1753
1754 static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
1755 {
1756 /* Locate the annex. */
1757 const char *annex_end = strchr(buf, ':');
1758 if (annex_end == NULL)
1759 return ERROR_FAIL;
1760
1761 /* After the read marker and annex, qXfer looks like a
1762 * traditional 'm' packet. */
1763 char *separator;
1764 *ofs = strtoul(annex_end + 1, &separator, 16);
1765
1766 if (*separator != ',')
1767 return ERROR_FAIL;
1768
1769 *len = strtoul(separator + 1, NULL, 16);
1770
1771 /* Extract the annex if needed */
1772 if (annex != NULL) {
1773 *annex = strndup(buf, annex_end - buf);
1774 if (*annex == NULL)
1775 return ERROR_FAIL;
1776 }
1777
1778 return ERROR_OK;
1779 }
1780
1781 static int compare_bank(const void *a, const void *b)
1782 {
1783 struct flash_bank *b1, *b2;
1784 b1 = *((struct flash_bank **)a);
1785 b2 = *((struct flash_bank **)b);
1786
1787 if (b1->base == b2->base)
1788 return 0;
1789 else if (b1->base > b2->base)
1790 return 1;
1791 else
1792 return -1;
1793 }
1794
1795 static int gdb_memory_map(struct connection *connection,
1796 char const *packet, int packet_size)
1797 {
1798 /* We get away with only specifying flash here. Regions that are not
1799 * specified are treated as if we provided no memory map(if not we
1800 * could detect the holes and mark them as RAM).
1801 * Normally we only execute this code once, but no big deal if we
1802 * have to regenerate it a couple of times.
1803 */
1804
1805 struct target *target = get_target_from_connection(connection);
1806 struct flash_bank *p;
1807 char *xml = NULL;
1808 int size = 0;
1809 int pos = 0;
1810 int retval = ERROR_OK;
1811 struct flash_bank **banks;
1812 int offset;
1813 int length;
1814 char *separator;
1815 target_addr_t ram_start = 0;
1816 int i;
1817 int target_flash_banks = 0;
1818
1819 /* skip command character */
1820 packet += 23;
1821
1822 offset = strtoul(packet, &separator, 16);
1823 length = strtoul(separator + 1, &separator, 16);
1824
1825 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1826
1827 /* Sort banks in ascending order. We need to report non-flash
1828 * memory as ram (or rather read/write) by default for GDB, since
1829 * it has no concept of non-cacheable read/write memory (i/o etc).
1830 */
1831 banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1832
1833 for (i = 0; i < flash_get_bank_count(); i++) {
1834 p = get_flash_bank_by_num_noprobe(i);
1835 if (p->target != target)
1836 continue;
1837 retval = get_flash_bank_by_num(i, &p);
1838 if (retval != ERROR_OK) {
1839 free(banks);
1840 gdb_error(connection, retval);
1841 return retval;
1842 }
1843 banks[target_flash_banks++] = p;
1844 }
1845
1846 qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1847 compare_bank);
1848
1849 for (i = 0; i < target_flash_banks; i++) {
1850 int j;
1851 unsigned sector_size = 0;
1852 unsigned group_len = 0;
1853
1854 p = banks[i];
1855
1856 if (ram_start < p->base)
1857 xml_printf(&retval, &xml, &pos, &size,
1858 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
1859 "length=\"0x%x\"/>\n",
1860 ram_start, p->base - ram_start);
1861
1862 /* Report adjacent groups of same-size sectors. So for
1863 * example top boot CFI flash will list an initial region
1864 * with several large sectors (maybe 128KB) and several
1865 * smaller ones at the end (maybe 32KB). STR7 will have
1866 * regions with 8KB, 32KB, and 64KB sectors; etc.
1867 */
1868 for (j = 0; j < p->num_sectors; j++) {
1869
1870 /* Maybe start a new group of sectors. */
1871 if (sector_size == 0) {
1872 if (p->sectors[j].offset + p->sectors[j].size > p->size) {
1873 LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1874 " overflows the end of %s bank.",
1875 p->sectors[j].offset, p->name);
1876 LOG_WARNING("The rest of bank will not show in gdb memory map.");
1877 break;
1878 }
1879 target_addr_t start;
1880 start = p->base + p->sectors[j].offset;
1881 xml_printf(&retval, &xml, &pos, &size,
1882 "<memory type=\"flash\" "
1883 "start=\"" TARGET_ADDR_FMT "\" ",
1884 start);
1885 sector_size = p->sectors[j].size;
1886 group_len = sector_size;
1887 } else {
1888 group_len += sector_size; /* equal to p->sectors[j].size */
1889 }
1890
1891 /* Does this finish a group of sectors?
1892 * If not, continue an already-started group.
1893 */
1894 if (j < p->num_sectors - 1
1895 && p->sectors[j + 1].size == sector_size
1896 && p->sectors[j + 1].offset == p->sectors[j].offset + sector_size
1897 && p->sectors[j + 1].offset + p->sectors[j + 1].size <= p->size)
1898 continue;
1899
1900 xml_printf(&retval, &xml, &pos, &size,
1901 "length=\"0x%x\">\n"
1902 "<property name=\"blocksize\">"
1903 "0x%x</property>\n"
1904 "</memory>\n",
1905 group_len,
1906 sector_size);
1907 sector_size = 0;
1908 }
1909
1910 ram_start = p->base + p->size;
1911 }
1912
1913 if (ram_start != 0)
1914 xml_printf(&retval, &xml, &pos, &size,
1915 "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
1916 "length=\"" TARGET_ADDR_FMT "\"/>\n",
1917 ram_start, target_address_max(target) - ram_start + 1);
1918 /* ELSE a flash chip could be at the very end of the address space, in
1919 * which case ram_start will be precisely 0 */
1920
1921 free(banks);
1922
1923 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1924
1925 if (retval != ERROR_OK) {
1926 free(xml);
1927 gdb_error(connection, retval);
1928 return retval;
1929 }
1930
1931 if (offset + length > pos)
1932 length = pos - offset;
1933
1934 char *t = malloc(length + 1);
1935 t[0] = 'l';
1936 memcpy(t + 1, xml + offset, length);
1937 gdb_put_packet(connection, t, length + 1);
1938
1939 free(t);
1940 free(xml);
1941 return ERROR_OK;
1942 }
1943
1944 static const char *gdb_get_reg_type_name(enum reg_type type)
1945 {
1946 switch (type) {
1947 case REG_TYPE_BOOL:
1948 return "bool";
1949 case REG_TYPE_INT:
1950 return "int";
1951 case REG_TYPE_INT8:
1952 return "int8";
1953 case REG_TYPE_INT16:
1954 return "int16";
1955 case REG_TYPE_INT32:
1956 return "int32";
1957 case REG_TYPE_INT64:
1958 return "int64";
1959 case REG_TYPE_INT128:
1960 return "int128";
1961 case REG_TYPE_UINT:
1962 return "uint";
1963 case REG_TYPE_UINT8:
1964 return "uint8";
1965 case REG_TYPE_UINT16:
1966 return "uint16";
1967 case REG_TYPE_UINT32:
1968 return "uint32";
1969 case REG_TYPE_UINT64:
1970 return "uint64";
1971 case REG_TYPE_UINT128:
1972 return "uint128";
1973 case REG_TYPE_CODE_PTR:
1974 return "code_ptr";
1975 case REG_TYPE_DATA_PTR:
1976 return "data_ptr";
1977 case REG_TYPE_FLOAT:
1978 return "float";
1979 case REG_TYPE_IEEE_SINGLE:
1980 return "ieee_single";
1981 case REG_TYPE_IEEE_DOUBLE:
1982 return "ieee_double";
1983 case REG_TYPE_ARCH_DEFINED:
1984 return "int"; /* return arbitrary string to avoid compile warning. */
1985 }
1986
1987 return "int"; /* "int" as default value */
1988 }
1989
1990 static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id,
1991 int *num_arch_defined_types)
1992 {
1993 int tbl_sz = *num_arch_defined_types;
1994
1995 if (type_id != NULL && (strcmp(type_id, ""))) {
1996 for (int j = 0; j < (tbl_sz + 1); j++) {
1997 if (!((*arch_defined_types_list)[j])) {
1998 (*arch_defined_types_list)[tbl_sz++] = type_id;
1999 *arch_defined_types_list = realloc(*arch_defined_types_list,
2000 sizeof(char *) * (tbl_sz + 1));
2001 (*arch_defined_types_list)[tbl_sz] = NULL;
2002 *num_arch_defined_types = tbl_sz;
2003 return 1;
2004 } else {
2005 if (!strcmp((*arch_defined_types_list)[j], type_id))
2006 return 0;
2007 }
2008 }
2009 }
2010
2011 return -1;
2012 }
2013
2014 static int gdb_generate_reg_type_description(struct target *target,
2015 char **tdesc, int *pos, int *size, struct reg_data_type *type,
2016 char const **arch_defined_types_list[], int * num_arch_defined_types)
2017 {
2018 int retval = ERROR_OK;
2019
2020 if (type->type_class == REG_TYPE_CLASS_VECTOR) {
2021 struct reg_data_type *data_type = type->reg_type_vector->type;
2022 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2023 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2024 num_arch_defined_types))
2025 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2026 arch_defined_types_list,
2027 num_arch_defined_types);
2028 }
2029 /* <vector id="id" type="type" count="count"/> */
2030 xml_printf(&retval, tdesc, pos, size,
2031 "<vector id=\"%s\" type=\"%s\" count=\"%d\"/>\n",
2032 type->id, type->reg_type_vector->type->id,
2033 type->reg_type_vector->count);
2034
2035 } else if (type->type_class == REG_TYPE_CLASS_UNION) {
2036 struct reg_data_type_union_field *field;
2037 field = type->reg_type_union->fields;
2038 while (field != NULL) {
2039 struct reg_data_type *data_type = field->type;
2040 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2041 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2042 num_arch_defined_types))
2043 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2044 arch_defined_types_list,
2045 num_arch_defined_types);
2046 }
2047
2048 field = field->next;
2049 }
2050 /* <union id="id">
2051 * <field name="name" type="type"/> ...
2052 * </union> */
2053 xml_printf(&retval, tdesc, pos, size,
2054 "<union id=\"%s\">\n",
2055 type->id);
2056
2057 field = type->reg_type_union->fields;
2058 while (field != NULL) {
2059 xml_printf(&retval, tdesc, pos, size,
2060 "<field name=\"%s\" type=\"%s\"/>\n",
2061 field->name, field->type->id);
2062
2063 field = field->next;
2064 }
2065
2066 xml_printf(&retval, tdesc, pos, size,
2067 "</union>\n");
2068
2069 } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
2070 struct reg_data_type_struct_field *field;
2071 field = type->reg_type_struct->fields;
2072
2073 if (field->use_bitfields) {
2074 /* <struct id="id" size="size">
2075 * <field name="name" start="start" end="end"/> ...
2076 * </struct> */
2077 xml_printf(&retval, tdesc, pos, size,
2078 "<struct id=\"%s\" size=\"%d\">\n",
2079 type->id, type->reg_type_struct->size);
2080 while (field != NULL) {
2081 xml_printf(&retval, tdesc, pos, size,
2082 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2083 field->name, field->bitfield->start, field->bitfield->end,
2084 gdb_get_reg_type_name(field->bitfield->type));
2085
2086 field = field->next;
2087 }
2088 } else {
2089 while (field != NULL) {
2090 struct reg_data_type *data_type = field->type;
2091 if (data_type->type == REG_TYPE_ARCH_DEFINED) {
2092 if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2093 num_arch_defined_types))
2094 gdb_generate_reg_type_description(target, tdesc, pos, size, data_type,
2095 arch_defined_types_list,
2096 num_arch_defined_types);
2097 }
2098 }
2099
2100 /* <struct id="id">
2101 * <field name="name" type="type"/> ...
2102 * </struct> */
2103 xml_printf(&retval, tdesc, pos, size,
2104 "<struct id=\"%s\">\n",
2105 type->id);
2106 while (field != NULL) {
2107 xml_printf(&retval, tdesc, pos, size,
2108 "<field name=\"%s\" type=\"%s\"/>\n",
2109 field->name, field->type->id);
2110
2111 field = field->next;
2112 }
2113 }
2114
2115 xml_printf(&retval, tdesc, pos, size,
2116 "</struct>\n");
2117
2118 } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
2119 /* <flags id="id" size="size">
2120 * <field name="name" start="start" end="end"/> ...
2121 * </flags> */
2122 xml_printf(&retval, tdesc, pos, size,
2123 "<flags id=\"%s\" size=\"%d\">\n",
2124 type->id, type->reg_type_flags->size);
2125
2126 struct reg_data_type_flags_field *field;
2127 field = type->reg_type_flags->fields;
2128 while (field != NULL) {
2129 xml_printf(&retval, tdesc, pos, size,
2130 "<field name=\"%s\" start=\"%d\" end=\"%d\" type=\"%s\" />\n",
2131 field->name, field->bitfield->start, field->bitfield->end,
2132 gdb_get_reg_type_name(field->bitfield->type));
2133
2134 field = field->next;
2135 }
2136
2137 xml_printf(&retval, tdesc, pos, size,
2138 "</flags>\n");
2139
2140 }
2141
2142 return ERROR_OK;
2143 }
2144
2145 /* Get a list of available target registers features. feature_list must
2146 * be freed by caller.
2147 */
2148 static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
2149 struct reg **reg_list, int reg_list_size)
2150 {
2151 int tbl_sz = 0;
2152
2153 /* Start with only one element */
2154 *feature_list = calloc(1, sizeof(char *));
2155
2156 for (int i = 0; i < reg_list_size; i++) {
2157 if (reg_list[i]->exist == false)
2158 continue;
2159
2160 if (reg_list[i]->feature != NULL
2161 && reg_list[i]->feature->name != NULL
2162 && (strcmp(reg_list[i]->feature->name, ""))) {
2163 /* We found a feature, check if the feature is already in the
2164 * table. If not, allocate a new entry for the table and
2165 * put the new feature in it.
2166 */
2167 for (int j = 0; j < (tbl_sz + 1); j++) {
2168 if (!((*feature_list)[j])) {
2169 (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
2170 *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
2171 (*feature_list)[tbl_sz] = NULL;
2172 break;
2173 } else {
2174 if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
2175 break;
2176 }
2177 }
2178 }
2179 }
2180
2181 if (feature_list_size)
2182 *feature_list_size = tbl_sz;
2183
2184 return ERROR_OK;
2185 }
2186
2187 static int gdb_generate_target_description(struct target *target, char **tdesc_out)
2188 {
2189 int retval = ERROR_OK;
2190 struct reg **reg_list = NULL;
2191 int reg_list_size;
2192 char const *architecture;
2193 char const **features = NULL;
2194 char const **arch_defined_types = NULL;
2195 int feature_list_size = 0;
2196 int num_arch_defined_types = 0;
2197 char *tdesc = NULL;
2198 int pos = 0;
2199 int size = 0;
2200
2201 arch_defined_types = calloc(1, sizeof(char *));
2202
2203 retval = target_get_gdb_reg_list(target, &reg_list,
2204 &reg_list_size, REG_CLASS_ALL);
2205
2206 if (retval != ERROR_OK) {
2207 LOG_ERROR("get register list failed");
2208 retval = ERROR_FAIL;
2209 goto error;
2210 }
2211
2212 if (reg_list_size <= 0) {
2213 LOG_ERROR("get register list failed");
2214 retval = ERROR_FAIL;
2215 goto error;
2216 }
2217
2218 /* Get a list of available target registers features */
2219 retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2220 if (retval != ERROR_OK) {
2221 LOG_ERROR("Can't get the registers feature list");
2222 retval = ERROR_FAIL;
2223 goto error;
2224 }
2225
2226 /* If we found some features associated with registers, create sections */
2227 int current_feature = 0;
2228
2229 xml_printf(&retval, &tdesc, &pos, &size,
2230 "<?xml version=\"1.0\"?>\n"
2231 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2232 "<target version=\"1.0\">\n");
2233
2234 /* generate architecture element if supported by target */
2235 architecture = target_get_gdb_arch(target);
2236 if (architecture != NULL)
2237 xml_printf(&retval, &tdesc, &pos, &size,
2238 "<architecture>%s</architecture>\n", architecture);
2239
2240 /* generate target description according to register list */
2241 if (features != NULL) {
2242 while (features[current_feature]) {
2243
2244 xml_printf(&retval, &tdesc, &pos, &size,
2245 "<feature name=\"%s\">\n",
2246 features[current_feature]);
2247
2248 int i;
2249 for (i = 0; i < reg_list_size; i++) {
2250
2251 if (reg_list[i]->exist == false)
2252 continue;
2253
2254 if (strcmp(reg_list[i]->feature->name, features[current_feature]))
2255 continue;
2256
2257 const char *type_str;
2258 if (reg_list[i]->reg_data_type != NULL) {
2259 if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
2260 /* generate <type... first, if there are architecture-defined types. */
2261 if (lookup_add_arch_defined_types(&arch_defined_types,
2262 reg_list[i]->reg_data_type->id,
2263 &num_arch_defined_types))
2264 gdb_generate_reg_type_description(target, &tdesc, &pos, &size,
2265 reg_list[i]->reg_data_type,
2266 &arch_defined_types,
2267 &num_arch_defined_types);
2268
2269 type_str = reg_list[i]->reg_data_type->id;
2270 } else {
2271 /* predefined type */
2272 type_str = gdb_get_reg_type_name(
2273 reg_list[i]->reg_data_type->type);
2274 }
2275 } else {
2276 /* Default type is "int" */
2277 type_str = "int";
2278 }
2279
2280 xml_printf(&retval, &tdesc, &pos, &size,
2281 "<reg name=\"%s\"", reg_list[i]->name);
2282 xml_printf(&retval, &tdesc, &pos, &size,
2283 " bitsize=\"%d\"", reg_list[i]->size);
2284 xml_printf(&retval, &tdesc, &pos, &size,
2285 " regnum=\"%d\"", reg_list[i]->number);
2286 if (reg_list[i]->caller_save)
2287 xml_printf(&retval, &tdesc, &pos, &size,
2288 " save-restore=\"yes\"");
2289 else
2290 xml_printf(&retval, &tdesc, &pos, &size,
2291 " save-restore=\"no\"");
2292
2293 xml_printf(&retval, &tdesc, &pos, &size,
2294 " type=\"%s\"", type_str);
2295
2296 if (reg_list[i]->group != NULL)
2297 xml_printf(&retval, &tdesc, &pos, &size,
2298 " group=\"%s\"", reg_list[i]->group);
2299
2300 xml_printf(&retval, &tdesc, &pos, &size,
2301 "/>\n");
2302 }
2303
2304 xml_printf(&retval, &tdesc, &pos, &size,
2305 "</feature>\n");
2306
2307 current_feature++;
2308 }
2309 }
2310
2311 xml_printf(&retval, &tdesc, &pos, &size,
2312 "</target>\n");
2313
2314 error:
2315 free(features);
2316 free(reg_list);
2317 free(arch_defined_types);
2318
2319 if (retval == ERROR_OK)
2320 *tdesc_out = tdesc;
2321 else
2322 free(tdesc);
2323
2324 return retval;
2325 }
2326
2327 static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
2328 char **chunk, int32_t offset, uint32_t length)
2329 {
2330 if (target_desc == NULL) {
2331 LOG_ERROR("Unable to Generate Target Description");
2332 return ERROR_FAIL;
2333 }
2334
2335 char *tdesc = target_desc->tdesc;
2336 uint32_t tdesc_length = target_desc->tdesc_length;
2337
2338 if (tdesc == NULL) {
2339 int retval = gdb_generate_target_description(target, &tdesc);
2340 if (retval != ERROR_OK) {
2341 LOG_ERROR("Unable to Generate Target Description");
2342 return ERROR_FAIL;
2343 }
2344
2345 tdesc_length = strlen(tdesc);
2346 }
2347
2348 char transfer_type;
2349
2350 if (length < (tdesc_length - offset))
2351 transfer_type = 'm';
2352 else
2353 transfer_type = 'l';
2354
2355 *chunk = malloc(length + 2);
2356 if (*chunk == NULL) {
2357 LOG_ERROR("Unable to allocate memory");
2358 return ERROR_FAIL;
2359 }
2360
2361 (*chunk)[0] = transfer_type;
2362 if (transfer_type == 'm') {
2363 strncpy((*chunk) + 1, tdesc + offset, length);
2364 (*chunk)[1 + length] = '\0';
2365 } else {
2366 strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
2367 (*chunk)[1 + (tdesc_length - offset)] = '\0';
2368
2369 /* After gdb-server sends out last chunk, invalidate tdesc. */
2370 free(tdesc);
2371 tdesc = NULL;
2372 tdesc_length = 0;
2373 }
2374
2375 target_desc->tdesc = tdesc;
2376 target_desc->tdesc_length = tdesc_length;
2377
2378 return ERROR_OK;
2379 }
2380
2381 static int gdb_target_description_supported(struct target *target, int *supported)
2382 {
2383 int retval = ERROR_OK;
2384 struct reg **reg_list = NULL;
2385 int reg_list_size = 0;
2386 char const **features = NULL;
2387 int feature_list_size = 0;
2388
2389 char const *architecture = target_get_gdb_arch(target);
2390
2391 retval = target_get_gdb_reg_list(target, &reg_list,
2392 &reg_list_size, REG_CLASS_ALL);
2393 if (retval != ERROR_OK) {
2394 LOG_ERROR("get register list failed");
2395 goto error;
2396 }
2397
2398 if (reg_list_size <= 0) {
2399 LOG_ERROR("get register list failed");
2400 retval = ERROR_FAIL;
2401 goto error;
2402 }
2403
2404 /* Get a list of available target registers features */
2405 retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2406 if (retval != ERROR_OK) {
2407 LOG_ERROR("Can't get the registers feature list");
2408 goto error;
2409 }
2410
2411 if (supported) {
2412 if (architecture || feature_list_size)
2413 *supported = 1;
2414 else
2415 *supported = 0;
2416 }
2417
2418 error:
2419 free(features);
2420
2421 free(reg_list);
2422
2423 return retval;
2424 }
2425
2426 static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
2427 {
2428 struct rtos *rtos = target->rtos;
2429 int retval = ERROR_OK;
2430 char *thread_list = NULL;
2431 int pos = 0;
2432 int size = 0;
2433
2434 xml_printf(&retval, &thread_list, &pos, &size,
2435 "<?xml version=\"1.0\"?>\n"
2436 "<threads>\n");
2437
2438 if (rtos != NULL) {
2439 for (int i = 0; i < rtos->thread_count; i++) {
2440 struct thread_detail *thread_detail = &rtos->thread_details[i];
2441
2442 if (!thread_detail->exists)
2443 continue;
2444
2445 xml_printf(&retval, &thread_list, &pos, &size,
2446 "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
2447
2448 if (thread_detail->thread_name_str != NULL)
2449 xml_printf(&retval, &thread_list, &pos, &size,
2450 "Name: %s", thread_detail->thread_name_str);
2451
2452 if (thread_detail->extra_info_str != NULL) {
2453 if (thread_detail->thread_name_str != NULL)
2454 xml_printf(&retval, &thread_list, &pos, &size,
2455 ", ");
2456 xml_printf(&retval, &thread_list, &pos, &size,
2457 thread_detail->extra_info_str);
2458 }
2459
2460 xml_printf(&retval, &thread_list, &pos, &size,
2461 "</thread>\n");
2462 }
2463 }
2464
2465 xml_printf(&retval, &thread_list, &pos, &size,
2466 "</threads>\n");
2467
2468 if (retval == ERROR_OK)
2469 *thread_list_out = thread_list;
2470 else
2471 free(thread_list);
2472
2473 return retval;
2474 }
2475
2476 static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
2477 char **chunk, int32_t offset, uint32_t length)
2478 {
2479 if (*thread_list == NULL) {
2480 int retval = gdb_generate_thread_list(target, thread_list);
2481 if (retval != ERROR_OK) {
2482 LOG_ERROR("Unable to Generate Thread List");
2483 return ERROR_FAIL;
2484 }
2485 }
2486
2487 size_t thread_list_length = strlen(*thread_list);
2488 char transfer_type;
2489
2490 length = MIN(length, thread_list_length - offset);
2491 if (length < (thread_list_length - offset))
2492 transfer_type = 'm';
2493 else
2494 transfer_type = 'l';
2495
2496 *chunk = malloc(length + 2 + 3);
2497 /* Allocating extra 3 bytes prevents false positive valgrind report
2498 * of strlen(chunk) word access:
2499 * Invalid read of size 4
2500 * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2501 if (*chunk == NULL) {
2502 LOG_ERROR("Unable to allocate memory");
2503 return ERROR_FAIL;
2504 }
2505
2506 (*chunk)[0] = transfer_type;
2507 strncpy((*chunk) + 1, (*thread_list) + offset, length);
2508 (*chunk)[1 + length] = '\0';
2509
2510 /* After gdb-server sends out last chunk, invalidate thread list. */
2511 if (transfer_type == 'l') {
2512 free(*thread_list);
2513 *thread_list = NULL;
2514 }
2515
2516 return ERROR_OK;
2517 }
2518
2519 static int gdb_query_packet(struct connection *connection,
2520 char const *packet, int packet_size)
2521 {
2522 struct command_context *cmd_ctx = connection->cmd_ctx;
2523 struct gdb_connection *gdb_connection = connection->priv;
2524 struct target *target = get_target_from_connection(connection);
2525
2526 if (strncmp(packet, "qRcmd,", 6) == 0) {
2527 if (packet_size > 6) {
2528 char *cmd;
2529 cmd = malloc((packet_size - 6) / 2 + 1);
2530 size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
2531 cmd[len] = 0;
2532
2533 /* We want to print all debug output to GDB connection */
2534 log_add_callback(gdb_log_callback, connection);
2535 target_call_timer_callbacks_now();
2536 /* some commands need to know the GDB connection, make note of current
2537 * GDB connection. */
2538 current_gdb_connection = gdb_connection;
2539 command_run_line(cmd_ctx, cmd);
2540 current_gdb_connection = NULL;
2541 target_call_timer_callbacks_now();
2542 log_remove_callback(gdb_log_callback, connection);
2543 free(cmd);
2544 }
2545 gdb_put_packet(connection, "OK", 2);
2546 return ERROR_OK;
2547 } else if (strncmp(packet, "qCRC:", 5) == 0) {
2548 if (packet_size > 5) {
2549 int retval;
2550 char gdb_reply[10];
2551 char *separator;
2552 uint32_t checksum;
2553 target_addr_t addr = 0;
2554 uint32_t len = 0;
2555
2556 /* skip command character */
2557 packet += 5;
2558
2559 addr = strtoull(packet, &separator, 16);
2560
2561 if (*separator != ',') {
2562 LOG_ERROR("incomplete read memory packet received, dropping connection");
2563 return ERROR_SERVER_REMOTE_CLOSED;
2564 }
2565
2566 len = strtoul(separator + 1, NULL, 16);
2567
2568 retval = target_checksum_memory(target, addr, len, &checksum);
2569
2570 if (retval == ERROR_OK) {
2571 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
2572 gdb_put_packet(connection, gdb_reply, 9);
2573 } else {
2574 retval = gdb_error(connection, retval);
2575 if (retval != ERROR_OK)
2576 return retval;
2577 }
2578
2579 return ERROR_OK;
2580 }
2581 } else if (strncmp(packet, "qSupported", 10) == 0) {
2582 /* we currently support packet size and qXfer:memory-map:read (if enabled)
2583 * qXfer:features:read is supported for some targets */
2584 int retval = ERROR_OK;
2585 char *buffer = NULL;
2586 int pos = 0;
2587 int size = 0;
2588 int gdb_target_desc_supported = 0;
2589
2590 /* we need to test that the target supports target descriptions */
2591 retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
2592 if (retval != ERROR_OK) {
2593 LOG_INFO("Failed detecting Target Description Support, disabling");
2594 gdb_target_desc_supported = 0;
2595 }
2596
2597 /* support may be disabled globally */
2598 if (gdb_use_target_description == 0) {
2599 if (gdb_target_desc_supported)
2600 LOG_WARNING("Target Descriptions Supported, but disabled");
2601 gdb_target_desc_supported = 0;
2602 }
2603
2604 xml_printf(&retval,
2605 &buffer,
2606 &pos,
2607 &size,
2608 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2609 GDB_BUFFER_SIZE,
2610 ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2611 (gdb_target_desc_supported == 1) ? '+' : '-');
2612
2613 if (retval != ERROR_OK) {
2614 gdb_send_error(connection, 01);
2615 return ERROR_OK;
2616 }
2617
2618 gdb_put_packet(connection, buffer, strlen(buffer));
2619 free(buffer);
2620
2621 return ERROR_OK;
2622 } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
2623 && (flash_get_bank_count() > 0))
2624 return gdb_memory_map(connection, packet, packet_size);
2625 else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
2626 char *xml = NULL;
2627 int retval = ERROR_OK;
2628
2629 int offset;
2630 unsigned int length;
2631
2632 /* skip command character */
2633 packet += 20;
2634
2635 if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2636 gdb_send_error(connection, 01);
2637 return ERROR_OK;
2638 }
2639
2640 /* Target should prepare correct target description for annex.
2641 * The first character of returned xml is 'm' or 'l'. 'm' for
2642 * there are *more* chunks to transfer. 'l' for it is the *last*
2643 * chunk of target description.
2644 */
2645 retval = gdb_get_target_description_chunk(target, &gdb_connection->target_desc,
2646 &xml, offset, length);
2647 if (retval != ERROR_OK) {
2648 gdb_error(connection, retval);
2649 return retval;
2650 }
2651
2652 gdb_put_packet(connection, xml, strlen(xml));
2653
2654 free(xml);
2655 return ERROR_OK;
2656 } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
2657 char *xml = NULL;
2658 int retval = ERROR_OK;
2659
2660 int offset;
2661 unsigned int length;
2662
2663 /* skip command character */
2664 packet += 19;
2665
2666 if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2667 gdb_send_error(connection, 01);
2668 return ERROR_OK;
2669 }
2670
2671 /* Target should prepare correct thread list for annex.
2672 * The first character of returned xml is 'm' or 'l'. 'm' for
2673 * there are *more* chunks to transfer. 'l' for it is the *last*
2674 * chunk of target description.
2675 */
2676 retval = gdb_get_thread_list_chunk(target, &gdb_connection->thread_list,
2677 &xml, offset, length);
2678 if (retval != ERROR_OK) {
2679 gdb_error(connection, retval);
2680 return retval;
2681 }
2682
2683 gdb_put_packet(connection, xml, strlen(xml));
2684
2685 free(xml);
2686 return ERROR_OK;
2687 } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
2688 gdb_connection->noack_mode = 1;
2689 gdb_put_packet(connection, "OK", 2);
2690 return ERROR_OK;
2691 }
2692
2693 gdb_put_packet(connection, "", 0);
2694 return ERROR_OK;
2695 }
2696
2697 static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet, int packet_size)
2698 {
2699 struct gdb_connection *gdb_connection = connection->priv;
2700 struct target *target = get_target_from_connection(connection);
2701 const char *parse = packet;
2702 int retval;
2703
2704 /* query for vCont supported */
2705 if (parse[0] == '?') {
2706 if (target->type->step != NULL) {
2707 /* gdb doesn't accept c without C and s without S */
2708 gdb_put_packet(connection, "vCont;c;C;s;S", 13);
2709 return true;
2710 }
2711 return false;
2712 }
2713
2714 if (parse[0] == ';') {
2715 ++parse;
2716 --packet_size;
2717 }
2718
2719 /* simple case, a continue packet */
2720 if (parse[0] == 'c') {
2721 gdb_running_type = 'c';
2722 LOG_DEBUG("target %s continue", target_name(target));
2723 log_add_callback(gdb_log_callback, connection);
2724 retval = target_resume(target, 1, 0, 0, 0);
2725 if (retval == ERROR_TARGET_NOT_HALTED)
2726 LOG_INFO("target %s was not halted when resume was requested", target_name(target));
2727
2728 /* poll target in an attempt to make its internal state consistent */
2729 if (retval != ERROR_OK) {
2730 retval = target_poll(target);
2731 if (retval != ERROR_OK)
2732 LOG_DEBUG("error polling target %s after failed resume", target_name(target));
2733 }
2734
2735 /*
2736 * We don't report errors to gdb here, move frontend_state to
2737 * TARGET_RUNNING to stay in sync with gdb's expectation of the
2738 * target state
2739 */
2740 gdb_connection->frontend_state = TARGET_RUNNING;
2741 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2742
2743 return true;
2744 }
2745
2746 /* single-step or step-over-breakpoint */
2747 if (parse[0] == 's') {
2748 gdb_running_type = 's';
2749 bool fake_step = false;
2750
2751 if (strncmp(parse, "s:", 2) == 0) {
2752 struct target *ct = target;
2753 int current_pc = 1;
2754 int64_t thread_id;
2755 char *endp;
2756
2757 parse += 2;
2758 packet_size -= 2;
2759
2760 thread_id = strtoll(parse, &endp, 16);
2761 if (endp != NULL) {
2762 packet_size -= endp - parse;
2763 parse = endp;
2764 }
2765
2766 if (target->rtos != NULL) {
2767 /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
2768 rtos_update_threads(target);
2769
2770 target->rtos->gdb_target_for_threadid(connection, thread_id, &ct);
2771
2772 /*
2773 * check if the thread to be stepped is the current rtos thread
2774 * if not, we must fake the step
2775 */
2776 if (target->rtos->current_thread != thread_id)
2777 fake_step = true;
2778 }
2779
2780 if (parse[0] == ';') {
2781 ++parse;
2782 --packet_size;
2783
2784 if (parse[0] == 'c') {
2785 parse += 1;
2786 packet_size -= 1;
2787
2788 /* check if thread-id follows */
2789 if (parse[0] == ':') {
2790 int64_t tid;
2791 parse += 1;
2792 packet_size -= 1;
2793
2794 tid = strtoll(parse, &endp, 16);
2795 if (tid == thread_id) {
2796 /*
2797 * Special case: only step a single thread (core),
2798 * keep the other threads halted. Currently, only
2799 * aarch64 target understands it. Other target types don't
2800 * care (nobody checks the actual value of 'current')
2801 * and it doesn't really matter. This deserves
2802 * a symbolic constant and a formal interface documentation
2803 * at a later time.
2804 */
2805 LOG_DEBUG("request to step current core only");
2806 /* uncomment after checking that indeed other targets are safe */
2807 /*current_pc = 2;*/
2808 }
2809 }
2810 }
2811 }
2812
2813 LOG_DEBUG("target %s single-step thread %"PRIx64, target_name(ct), thread_id);
2814 log_add_callback(gdb_log_callback, connection);
2815 target_call_event_callbacks(ct, TARGET_EVENT_GDB_START);
2816
2817 /*
2818 * work around an annoying gdb behaviour: when the current thread
2819 * is changed in gdb, it assumes that the target can follow and also
2820 * make the thread current. This is an assumption that cannot hold
2821 * for a real target running a multi-threading OS. We just fake
2822 * the step to not trigger an internal error in gdb. See
2823 * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
2824 */
2825 if (fake_step) {
2826 int sig_reply_len;
2827 char sig_reply[128];
2828
2829 LOG_DEBUG("fake step thread %"PRIx64, thread_id);
2830
2831 sig_reply_len = snprintf(sig_reply, sizeof(sig_reply),
2832 "T05thread:%016"PRIx64";", thread_id);
2833
2834 gdb_put_packet(connection, sig_reply, sig_reply_len);
2835 log_remove_callback(gdb_log_callback, connection);
2836
2837 return true;
2838 }
2839
2840 /* support for gdb_sync command */
2841 if (gdb_connection->sync) {
2842 gdb_connection->sync = false;
2843 if (ct->state == TARGET_HALTED) {
2844 LOG_DEBUG("stepi ignored. GDB will now fetch the register state " \
2845 "from the target.");
2846 gdb_sig_halted(connection);
2847 log_remove_callback(gdb_log_callback, connection);
2848 } else
2849 gdb_connection->frontend_state = TARGET_RUNNING;
2850 return true;
2851 }
2852
2853 retval = target_step(ct, current_pc, 0, 0);
2854 if (retval == ERROR_TARGET_NOT_HALTED)
2855 LOG_INFO("target %s was not halted when step was requested", target_name(ct));
2856
2857 /* if step was successful send a reply back to gdb */
2858 if (retval == ERROR_OK) {
2859 retval = target_poll(ct);
2860 if (retval != ERROR_OK)
2861 LOG_DEBUG("error polling target %s after successful step", target_name(ct));
2862 /* send back signal information */
2863 gdb_signal_reply(ct, connection);
2864 /* stop forwarding log packets! */
2865 log_remove_callback(gdb_log_callback, connection);
2866 } else
2867 gdb_connection->frontend_state = TARGET_RUNNING;
2868 } else {
2869 LOG_ERROR("Unknown vCont packet");
2870 return false;
2871 }
2872 return true;
2873 }
2874
2875 return false;
2876 }
2877
2878 static int gdb_v_packet(struct connection *connection,
2879 char const *packet, int packet_size)
2880 {
2881 struct gdb_connection *gdb_connection = connection->priv;
2882 struct target *target;
2883 int result;
2884
2885 target = get_target_from_connection(connection);
2886
2887 if (strncmp(packet, "vCont", 5) == 0) {
2888 bool handled;
2889
2890 packet += 5;
2891 packet_size -= 5;
2892
2893 handled = gdb_handle_vcont_packet(connection, packet, packet_size);
2894 if (!handled)
2895 gdb_put_packet(connection, "", 0);
2896
2897 return ERROR_OK;
2898 }
2899
2900 /* if flash programming disabled - send a empty reply */
2901
2902 if (gdb_flash_program == 0) {
2903 gdb_put_packet(connection, "", 0);
2904 return ERROR_OK;
2905 }
2906
2907 if (strncmp(packet, "vFlashErase:", 12) == 0) {
2908 unsigned long addr;
2909 unsigned long length;
2910
2911 char const *parse = packet + 12;
2912 if (*parse == '\0') {
2913 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2914 return ERROR_SERVER_REMOTE_CLOSED;
2915 }
2916
2917 addr = strtoul(parse, (char **)&parse, 16);
2918
2919 if (*(parse++) != ',' || *parse == '\0') {
2920 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2921 return ERROR_SERVER_REMOTE_CLOSED;
2922 }
2923
2924 length = strtoul(parse, (char **)&parse, 16);
2925
2926 if (*parse != '\0') {
2927 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2928 return ERROR_SERVER_REMOTE_CLOSED;
2929 }
2930
2931 /* assume all sectors need erasing - stops any problems
2932 * when flash_write is called multiple times */
2933 flash_set_dirty();
2934
2935 /* perform any target specific operations before the erase */
2936 target_call_event_callbacks(target,
2937 TARGET_EVENT_GDB_FLASH_ERASE_START);
2938
2939 /* vFlashErase:addr,length messages require region start and
2940 * end to be "block" aligned ... if padding is ever needed,
2941 * GDB will have become dangerously confused.
2942 */
2943 result = flash_erase_address_range(target, false, addr,
2944 length);
2945
2946 /* perform any target specific operations after the erase */
2947 target_call_event_callbacks(target,
2948 TARGET_EVENT_GDB_FLASH_ERASE_END);
2949
2950 /* perform erase */
2951 if (result != ERROR_OK) {
2952 /* GDB doesn't evaluate the actual error number returned,
2953 * treat a failed erase as an I/O error
2954 */
2955 gdb_send_error(connection, EIO);
2956 LOG_ERROR("flash_erase returned %i", result);
2957 } else
2958 gdb_put_packet(connection, "OK", 2);
2959
2960 return ERROR_OK;
2961 }
2962
2963 if (strncmp(packet, "vFlashWrite:", 12) == 0) {
2964 int retval;
2965 unsigned long addr;
2966 unsigned long length;
2967 char const *parse = packet + 12;
2968
2969 if (*parse == '\0') {
2970 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2971 return ERROR_SERVER_REMOTE_CLOSED;
2972 }
2973 addr = strtoul(parse, (char **)&parse, 16);
2974 if (*(parse++) != ':') {
2975 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
2976 return ERROR_SERVER_REMOTE_CLOSED;
2977 }
2978 length = packet_size - (parse - packet);
2979
2980 /* create a new image if there isn't already one */
2981 if (gdb_connection->vflash_image == NULL) {
2982 gdb_connection->vflash_image = malloc(sizeof(struct image));
2983 image_open(gdb_connection->vflash_image, "", "build");
2984 }
2985
2986 /* create new section with content from packet buffer */
2987 retval = image_add_section(gdb_connection->vflash_image,
2988 addr, length, 0x0, (uint8_t const *)parse);
2989 if (retval != ERROR_OK)
2990 return retval;
2991
2992 gdb_put_packet(connection, "OK", 2);
2993
2994 return ERROR_OK;
2995 }
2996
2997 if (strncmp(packet, "vFlashDone", 10) == 0) {
2998 uint32_t written;
2999
3000 /* process the flashing buffer. No need to erase as GDB
3001 * always issues a vFlashErase first. */
3002 target_call_event_callbacks(target,
3003 TARGET_EVENT_GDB_FLASH_WRITE_START);
3004 result = flash_write(target, gdb_connection->vflash_image,
3005 &written, 0);
3006 target_call_event_callbacks(target,
3007 TARGET_EVENT_GDB_FLASH_WRITE_END);
3008 if (result != ERROR_OK) {
3009 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
3010 gdb_put_packet(connection, "E.memtype", 9);
3011 else
3012 gdb_send_error(connection, EIO);
3013 } else {
3014 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
3015 gdb_put_packet(connection, "OK", 2);
3016 }
3017
3018 image_close(gdb_connection->vflash_image);
3019 free(gdb_connection->vflash_image);
3020 gdb_connection->vflash_image = NULL;
3021
3022 return ERROR_OK;
3023 }
3024
3025 gdb_put_packet(connection, "", 0);
3026 return ERROR_OK;
3027 }
3028
3029 static int gdb_detach(struct connection *connection)
3030 {
3031 /*
3032 * Only reply "OK" to GDB
3033 * it will close the connection and this will trigger a call to
3034 * gdb_connection_closed() that will in turn trigger the event
3035 * TARGET_EVENT_GDB_DETACH
3036 */
3037 return gdb_put_packet(connection, "OK", 2);
3038 }
3039
3040 /* The format of 'F' response packet is
3041 * Fretcode,errno,Ctrl-C flag;call-specific attachment
3042 */
3043 static int gdb_fileio_response_packet(struct connection *connection,
3044 char const *packet, int packet_size)
3045 {
3046 struct target *target = get_target_from_connection(connection);
3047 char *separator;
3048 char *parsing_point;
3049 int fileio_retcode = strtoul(packet + 1, &separator, 16);
3050 int fileio_errno = 0;
3051 bool fileio_ctrl_c = false;
3052 int retval;
3053
3054 LOG_DEBUG("-");
3055
3056 if (*separator == ',') {
3057 parsing_point = separator + 1;
3058 fileio_errno = strtoul(parsing_point, &separator, 16);
3059 if (*separator == ',') {
3060 if (*(separator + 1) == 'C') {