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