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

Linking to existing account procedure

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

SSH host keys fingerprints

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