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