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

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)