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

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)