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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)