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