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

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)