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

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)