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

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)