PIC32: enable ram execution
[openocd/dnglaze.git] / src / server / gdb_server.c
blob96b9dbf1f730189c56d0997c099fdf0371a519db
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2009 Ø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
30 #include <target/breakpoints.h>
31 #include <target/target_request.h>
32 #include <target/register.h>
33 #include "server.h"
34 #include <flash/nor/core.h>
35 #include "gdb_server.h"
36 #include <target/image.h>
37 #include <jtag/jtag.h>
40 /* private connection data for GDB */
41 struct gdb_connection
43 char buffer[GDB_BUFFER_SIZE];
44 char *buf_p;
45 int buf_cnt;
46 int ctrl_c;
47 enum target_state frontend_state;
48 struct image *vflash_image;
49 int closed;
50 int busy;
51 int noack_mode;
52 bool sync; /* set flag to true if you want the next stepi to return immediately.
53 allowing GDB to pick up a fresh set of register values from the target
54 without modifying the target state. */
59 #if 0
60 #define _DEBUG_GDB_IO_
61 #endif
63 static struct gdb_connection *current_gdb_connection;
65 static int gdb_breakpoint_override;
66 static enum breakpoint_type gdb_breakpoint_override_type;
68 extern int gdb_error(struct connection *connection, int retval);
69 static unsigned short gdb_port = 3333;
70 static unsigned short gdb_port_next = 0;
71 static const char *DIGITS = "0123456789abcdef";
73 static void gdb_log_callback(void *priv, const char *file, unsigned line,
74 const char *function, const char *string);
76 /* number of gdb connections, mainly to suppress gdb related debugging spam
77 * in helper/log.c when no gdb connections are actually active */
78 int gdb_actual_connections;
80 /* set if we are sending a memory map to gdb
81 * via qXfer:memory-map:read packet */
82 /* enabled by default*/
83 int gdb_use_memory_map = 1;
84 /* enabled by default*/
85 int gdb_flash_program = 1;
87 /* if set, data aborts cause an error to be reported in memory read packets
88 * see the code in gdb_read_memory_packet() for further explanations */
89 int gdb_report_data_abort = 0;
91 int gdb_last_signal(struct target *target)
93 switch (target->debug_reason)
95 case DBG_REASON_DBGRQ:
96 return 0x2; /* SIGINT */
97 case DBG_REASON_BREAKPOINT:
98 case DBG_REASON_WATCHPOINT:
99 case DBG_REASON_WPTANDBKPT:
100 return 0x05; /* SIGTRAP */
101 case DBG_REASON_SINGLESTEP:
102 return 0x05; /* SIGTRAP */
103 case DBG_REASON_NOTHALTED:
104 return 0x0; /* no signal... shouldn't happen */
105 default:
106 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
107 return 0x0;
111 int check_pending(struct connection *connection, int timeout_s, int *got_data)
113 /* a non-blocking socket will block if there is 0 bytes available on the socket,
114 * but return with as many bytes as are available immediately
116 struct timeval tv;
117 fd_set read_fds;
118 struct gdb_connection *gdb_con = connection->priv;
119 int t;
120 if (got_data == NULL)
121 got_data=&t;
122 *got_data = 0;
124 if (gdb_con->buf_cnt > 0)
126 *got_data = 1;
127 return ERROR_OK;
130 FD_ZERO(&read_fds);
131 FD_SET(connection->fd, &read_fds);
133 tv.tv_sec = timeout_s;
134 tv.tv_usec = 0;
135 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
137 /* This can typically be because a "monitor" command took too long
138 * before printing any progress messages
140 if (timeout_s > 0)
142 return ERROR_GDB_TIMEOUT;
143 } else
145 return ERROR_OK;
148 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
149 return ERROR_OK;
152 static int gdb_get_char_inner(struct connection *connection, int* next_char)
154 struct gdb_connection *gdb_con = connection->priv;
155 int retval = ERROR_OK;
157 for (;;)
159 if (connection->service->type == CONNECTION_PIPE)
161 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
163 else
165 retval = check_pending(connection, 1, NULL);
166 if (retval != ERROR_OK)
167 return retval;
168 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
171 if (gdb_con->buf_cnt > 0)
173 break;
175 if (gdb_con->buf_cnt == 0)
177 gdb_con->closed = 1;
178 return ERROR_SERVER_REMOTE_CLOSED;
181 #ifdef _WIN32
182 errno = WSAGetLastError();
184 switch (errno)
186 case WSAEWOULDBLOCK:
187 usleep(1000);
188 break;
189 case WSAECONNABORTED:
190 gdb_con->closed = 1;
191 return ERROR_SERVER_REMOTE_CLOSED;
192 case WSAECONNRESET:
193 gdb_con->closed = 1;
194 return ERROR_SERVER_REMOTE_CLOSED;
195 default:
196 LOG_ERROR("read: %d", errno);
197 exit(-1);
199 #else
200 switch (errno)
202 case EAGAIN:
203 usleep(1000);
204 break;
205 case ECONNABORTED:
206 gdb_con->closed = 1;
207 return ERROR_SERVER_REMOTE_CLOSED;
208 case ECONNRESET:
209 gdb_con->closed = 1;
210 return ERROR_SERVER_REMOTE_CLOSED;
211 default:
212 LOG_ERROR("read: %s", strerror(errno));
213 gdb_con->closed = 1;
214 return ERROR_SERVER_REMOTE_CLOSED;
216 #endif
219 #ifdef _DEBUG_GDB_IO_
220 debug_buffer = malloc(gdb_con->buf_cnt + 1);
221 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
222 debug_buffer[gdb_con->buf_cnt] = 0;
223 LOG_DEBUG("received '%s'", debug_buffer);
224 free(debug_buffer);
225 #endif
227 gdb_con->buf_p = gdb_con->buffer;
228 gdb_con->buf_cnt--;
229 *next_char = *(gdb_con->buf_p++);
230 if (gdb_con->buf_cnt > 0)
231 connection->input_pending = 1;
232 else
233 connection->input_pending = 0;
234 #ifdef _DEBUG_GDB_IO_
235 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
236 #endif
238 return retval;
242 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
243 * held in registers in the inner loop.
245 * For small caches and embedded systems this is important!
247 static inline int gdb_get_char_fast(struct connection *connection, int* next_char, char **buf_p, int *buf_cnt)
249 int retval = ERROR_OK;
251 if ((*buf_cnt)-- > 0)
253 *next_char = **buf_p;
254 (*buf_p)++;
255 if (*buf_cnt > 0)
256 connection->input_pending = 1;
257 else
258 connection->input_pending = 0;
260 #ifdef _DEBUG_GDB_IO_
261 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
262 #endif
264 return ERROR_OK;
267 struct gdb_connection *gdb_con = connection->priv;
268 gdb_con->buf_p = *buf_p;
269 gdb_con->buf_cnt = *buf_cnt;
270 retval = gdb_get_char_inner(connection, next_char);
271 *buf_p = gdb_con->buf_p;
272 *buf_cnt = gdb_con->buf_cnt;
274 return retval;
278 int gdb_get_char(struct connection *connection, int* next_char)
280 struct gdb_connection *gdb_con = connection->priv;
281 return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
285 int gdb_putback_char(struct connection *connection, int last_char)
287 struct gdb_connection *gdb_con = connection->priv;
289 if (gdb_con->buf_p > gdb_con->buffer)
291 *(--gdb_con->buf_p) = last_char;
292 gdb_con->buf_cnt++;
294 else
296 LOG_ERROR("BUG: couldn't put character back");
299 return ERROR_OK;
302 /* The only way we can detect that the socket is closed is the first time
303 * we write to it, we will fail. Subsequent write operations will
304 * succeed. Shudder! */
305 int gdb_write(struct connection *connection, void *data, int len)
307 struct gdb_connection *gdb_con = connection->priv;
308 if (gdb_con->closed)
309 return ERROR_SERVER_REMOTE_CLOSED;
311 if (connection->service->type == CONNECTION_PIPE)
313 /* write to stdout */
314 if (write(STDOUT_FILENO, data, len) == len)
316 return ERROR_OK;
319 else
321 if (write_socket(connection->fd, data, len) == len)
323 return ERROR_OK;
326 gdb_con->closed = 1;
327 return ERROR_SERVER_REMOTE_CLOSED;
330 int gdb_put_packet_inner(struct connection *connection, char *buffer, int len)
332 int i;
333 unsigned char my_checksum = 0;
334 #ifdef _DEBUG_GDB_IO_
335 char *debug_buffer;
336 #endif
337 int reply;
338 int retval;
339 struct gdb_connection *gdb_con = connection->priv;
341 for (i = 0; i < len; i++)
342 my_checksum += buffer[i];
344 #ifdef _DEBUG_GDB_IO_
346 * At this point we should have nothing in the input queue from GDB,
347 * however sometimes '-' is sent even though we've already received
348 * an ACK (+) for everything we've sent off.
350 int gotdata;
351 for (;;)
353 if ((retval = check_pending(connection, 0, &gotdata)) != ERROR_OK)
354 return retval;
355 if (!gotdata)
356 break;
357 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
358 return retval;
359 if (reply == '$') {
360 /* fix a problem with some IAR tools */
361 gdb_putback_char(connection, reply);
362 LOG_DEBUG("Unexpected start of new packet");
363 break;
366 LOG_WARNING("Discard unexpected char %c", reply);
368 #endif
370 while (1)
372 #ifdef _DEBUG_GDB_IO_
373 debug_buffer = malloc(len + 1);
374 memcpy(debug_buffer, buffer, len);
375 debug_buffer[len] = 0;
376 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
377 free(debug_buffer);
378 #endif
380 char local_buffer[1024];
381 local_buffer[0] = '$';
382 if ((size_t)len + 4 <= sizeof(local_buffer))
384 /* performance gain on smaller packets by only a single call to gdb_write() */
385 memcpy(local_buffer + 1, buffer, len++);
386 local_buffer[len++] = '#';
387 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
388 local_buffer[len++] = DIGITS[my_checksum & 0xf];
389 if ((retval = gdb_write(connection, local_buffer, len)) != ERROR_OK)
391 return retval;
394 else
396 /* larger packets are transmitted directly from caller supplied buffer
397 by several calls to gdb_write() to avoid dynamic allocation */
398 local_buffer[1] = '#';
399 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
400 local_buffer[3] = DIGITS[my_checksum & 0xf];
401 if ((retval = gdb_write(connection, local_buffer, 1)) != ERROR_OK)
403 return retval;
405 if ((retval = gdb_write(connection, buffer, len)) != ERROR_OK)
407 return retval;
409 if ((retval = gdb_write(connection, local_buffer + 1, 3)) != ERROR_OK)
411 return retval;
415 if (gdb_con->noack_mode)
416 break;
418 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
419 return retval;
421 if (reply == '+')
422 break;
423 else if (reply == '-')
425 /* Stop sending output packets for now */
426 log_remove_callback(gdb_log_callback, connection);
427 LOG_WARNING("negative reply, retrying");
429 else if (reply == 0x3)
431 gdb_con->ctrl_c = 1;
432 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
433 return retval;
434 if (reply == '+')
435 break;
436 else if (reply == '-')
438 /* Stop sending output packets for now */
439 log_remove_callback(gdb_log_callback, connection);
440 LOG_WARNING("negative reply, retrying");
442 else if (reply == '$') {
443 LOG_ERROR("GDB missing ack(1) - assumed good");
444 gdb_putback_char(connection, reply);
445 return ERROR_OK;
446 } else {
448 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
449 gdb_con->closed = 1;
450 return ERROR_SERVER_REMOTE_CLOSED;
453 else if (reply == '$') {
454 LOG_ERROR("GDB missing ack(2) - assumed good");
455 gdb_putback_char(connection, reply);
456 return ERROR_OK;
458 else
460 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply);
461 gdb_con->closed = 1;
462 return ERROR_SERVER_REMOTE_CLOSED;
465 if (gdb_con->closed)
466 return ERROR_SERVER_REMOTE_CLOSED;
468 return ERROR_OK;
471 int gdb_put_packet(struct connection *connection, char *buffer, int len)
473 struct gdb_connection *gdb_con = connection->priv;
474 gdb_con->busy = 1;
475 int retval = gdb_put_packet_inner(connection, buffer, len);
476 gdb_con->busy = 0;
478 /* we sent some data, reset timer for keep alive messages */
479 kept_alive();
481 return retval;
484 static __inline__ int fetch_packet(struct connection *connection, int *checksum_ok, int noack, int *len, char *buffer)
486 unsigned char my_checksum = 0;
487 char checksum[3];
488 int character;
489 int retval = ERROR_OK;
491 struct gdb_connection *gdb_con = connection->priv;
492 my_checksum = 0;
493 int count = 0;
494 count = 0;
496 /* move this over into local variables to use registers and give the
497 * more freedom to optimize */
498 char *buf_p = gdb_con->buf_p;
499 int buf_cnt = gdb_con->buf_cnt;
501 for (;;)
503 /* The common case is that we have an entire packet with no escape chars.
504 * We need to leave at least 2 bytes in the buffer to have
505 * gdb_get_char() update various bits and bobs correctly.
507 if ((buf_cnt > 2) && ((buf_cnt + count) < *len))
509 /* The compiler will struggle a bit with constant propagation and
510 * aliasing, so we help it by showing that these values do not
511 * change inside the loop
513 int i;
514 char *buf = buf_p;
515 int run = buf_cnt - 2;
516 i = 0;
517 int done = 0;
518 while (i < run)
520 character = *buf++;
521 i++;
522 if (character == '#')
524 /* Danger! character can be '#' when esc is
525 * used so we need an explicit boolean for done here.
527 done = 1;
528 break;
531 if (character == '}')
533 /* data transmitted in binary mode (X packet)
534 * uses 0x7d as escape character */
535 my_checksum += character & 0xff;
536 character = *buf++;
537 i++;
538 my_checksum += character & 0xff;
539 buffer[count++] = (character ^ 0x20) & 0xff;
541 else
543 my_checksum += character & 0xff;
544 buffer[count++] = character & 0xff;
547 buf_p += i;
548 buf_cnt -= i;
549 if (done)
550 break;
552 if (count > *len)
554 LOG_ERROR("packet buffer too small");
555 retval = ERROR_GDB_BUFFER_TOO_SMALL;
556 break;
559 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
560 if (retval != ERROR_OK)
561 break;
563 if (character == '#')
564 break;
566 if (character == '}')
568 /* data transmitted in binary mode (X packet)
569 * uses 0x7d as escape character */
570 my_checksum += character & 0xff;
572 retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
573 if (retval != ERROR_OK)
574 break;
576 my_checksum += character & 0xff;
577 buffer[count++] = (character ^ 0x20) & 0xff;
579 else
581 my_checksum += character & 0xff;
582 buffer[count++] = character & 0xff;
586 gdb_con->buf_p = buf_p;
587 gdb_con->buf_cnt = buf_cnt;
589 if (retval != ERROR_OK)
590 return retval;
592 *len = count;
594 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
595 return retval;
596 checksum[0] = character;
597 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
598 return retval;
599 checksum[1] = character;
600 checksum[2] = 0;
602 if (!noack)
604 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
607 return ERROR_OK;
610 int gdb_get_packet_inner(struct connection *connection, char *buffer, int *len)
612 int character;
613 int retval;
614 struct gdb_connection *gdb_con = connection->priv;
616 while (1)
620 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
621 return retval;
623 #ifdef _DEBUG_GDB_IO_
624 LOG_DEBUG("character: '%c'", character);
625 #endif
627 switch (character)
629 case '$':
630 break;
631 case '+':
632 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
633 * in case anyone tries to debug why they receive this warning every time */
634 LOG_WARNING("acknowledgment received, but no packet pending");
635 break;
636 case '-':
637 LOG_WARNING("negative acknowledgment, but no packet pending");
638 break;
639 case 0x3:
640 gdb_con->ctrl_c = 1;
641 *len = 0;
642 return ERROR_OK;
643 default:
644 LOG_WARNING("ignoring character 0x%x", character);
645 break;
647 } while (character != '$');
651 int checksum_ok = 0;
652 /* explicit code expansion here to get faster inlined code in -O3 by not
653 * calculating checksum
655 if (gdb_con->noack_mode)
657 if ((retval = fetch_packet(connection, &checksum_ok, 1, len, buffer)) != ERROR_OK)
658 return retval;
659 } else
661 if ((retval = fetch_packet(connection, &checksum_ok, 0, len, buffer)) != ERROR_OK)
662 return retval;
665 if (gdb_con->noack_mode)
667 /* checksum is not checked in noack mode */
668 break;
670 if (checksum_ok)
672 if ((retval = gdb_write(connection, "+", 1)) != ERROR_OK)
674 return retval;
676 break;
679 if (gdb_con->closed)
680 return ERROR_SERVER_REMOTE_CLOSED;
682 return ERROR_OK;
685 int gdb_get_packet(struct connection *connection, char *buffer, int *len)
687 struct gdb_connection *gdb_con = connection->priv;
688 gdb_con->busy = 1;
689 int retval = gdb_get_packet_inner(connection, buffer, len);
690 gdb_con->busy = 0;
691 return retval;
694 int gdb_output_con(struct connection *connection, const char* line)
696 char *hex_buffer;
697 int i, bin_size;
699 bin_size = strlen(line);
701 hex_buffer = malloc(bin_size*2 + 2);
702 if (hex_buffer == NULL)
703 return ERROR_GDB_BUFFER_TOO_SMALL;
705 hex_buffer[0] = 'O';
706 for (i = 0; i < bin_size; i++)
707 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
708 hex_buffer[bin_size*2 + 1] = 0;
710 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
712 free(hex_buffer);
713 return retval;
716 int gdb_output(struct command_context *context, const char* line)
718 /* this will be dumped to the log and also sent as an O packet if possible */
719 LOG_USER_N("%s", line);
720 return ERROR_OK;
724 static void gdb_frontend_halted(struct target *target, struct connection *connection)
726 struct gdb_connection *gdb_connection = connection->priv;
728 /* In the GDB protocol when we are stepping or continuing execution,
729 * we have a lingering reply. Upon receiving a halted event
730 * when we have that lingering packet, we reply to the original
731 * step or continue packet.
733 * Executing monitor commands can bring the target in and
734 * out of the running state so we'll see lots of TARGET_EVENT_XXX
735 * that are to be ignored.
737 if (gdb_connection->frontend_state == TARGET_RUNNING)
739 char sig_reply[4];
740 int signal;
742 /* stop forwarding log packets! */
743 log_remove_callback(gdb_log_callback, connection);
745 if (gdb_connection->ctrl_c)
747 signal = 0x2;
748 gdb_connection->ctrl_c = 0;
750 else
752 signal = gdb_last_signal(target);
755 snprintf(sig_reply, 4, "T%2.2x", signal);
756 gdb_put_packet(connection, sig_reply, 3);
757 gdb_connection->frontend_state = TARGET_HALTED;
761 int gdb_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
763 int retval;
764 struct connection *connection = priv;
766 target_handle_event(target, event);
767 switch (event)
769 case TARGET_EVENT_GDB_HALT:
770 gdb_frontend_halted(target, connection);
771 break;
772 case TARGET_EVENT_HALTED:
773 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
774 break;
775 case TARGET_EVENT_GDB_FLASH_ERASE_START:
776 target_handle_event(target, TARGET_EVENT_OLD_gdb_program_config);
777 if ((retval = jtag_execute_queue()) != ERROR_OK)
779 return retval;
781 break;
782 default:
783 break;
786 return ERROR_OK;
789 int gdb_new_connection(struct connection *connection)
791 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
792 struct gdb_service *gdb_service = connection->service->priv;
793 int retval;
794 int initial_ack;
796 connection->priv = gdb_connection;
798 /* initialize gdb connection information */
799 gdb_connection->buf_p = gdb_connection->buffer;
800 gdb_connection->buf_cnt = 0;
801 gdb_connection->ctrl_c = 0;
802 gdb_connection->frontend_state = TARGET_HALTED;
803 gdb_connection->vflash_image = NULL;
804 gdb_connection->closed = 0;
805 gdb_connection->busy = 0;
806 gdb_connection->noack_mode = 0;
807 gdb_connection->sync = true;
809 /* send ACK to GDB for debug request */
810 gdb_write(connection, "+", 1);
812 /* output goes through gdb connection */
813 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
815 /* we must remove all breakpoints registered to the target as a previous
816 * GDB session could leave dangling breakpoints if e.g. communication
817 * timed out.
819 breakpoint_clear_target(gdb_service->target);
820 watchpoint_clear_target(gdb_service->target);
822 /* register callback to be informed about target events */
823 target_register_event_callback(gdb_target_callback_event_handler, connection);
825 /* remove the initial ACK from the incoming buffer */
826 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
827 return retval;
829 /* FIX!!!??? would we actually ever receive a + here???
830 * Not observed.
832 if (initial_ack != '+')
833 gdb_putback_char(connection, initial_ack);
834 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
836 gdb_actual_connections++;
837 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
838 gdb_actual_connections,
839 target_name(gdb_service->target),
840 target_state_name(gdb_service->target));
842 return ERROR_OK;
845 int gdb_connection_closed(struct connection *connection)
847 struct gdb_service *gdb_service = connection->service->priv;
848 struct gdb_connection *gdb_connection = connection->priv;
850 /* we're done forwarding messages. Tear down callback before
851 * cleaning up connection.
853 log_remove_callback(gdb_log_callback, connection);
855 gdb_actual_connections--;
856 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
857 target_name(gdb_service->target),
858 target_state_name(gdb_service->target),
859 gdb_actual_connections);
861 /* see if an image built with vFlash commands is left */
862 if (gdb_connection->vflash_image)
864 image_close(gdb_connection->vflash_image);
865 free(gdb_connection->vflash_image);
866 gdb_connection->vflash_image = NULL;
869 /* if this connection registered a debug-message receiver delete it */
870 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
872 if (connection->priv)
874 free(connection->priv);
875 connection->priv = NULL;
877 else
879 LOG_ERROR("BUG: connection->priv == NULL");
883 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
885 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
887 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
889 return ERROR_OK;
892 void gdb_send_error(struct connection *connection, uint8_t the_error)
894 char err[4];
895 snprintf(err, 4, "E%2.2X", the_error);
896 gdb_put_packet(connection, err, 3);
899 int gdb_last_signal_packet(struct connection *connection, struct target *target, char* packet, int packet_size)
901 char sig_reply[4];
902 int signal;
904 signal = gdb_last_signal(target);
906 snprintf(sig_reply, 4, "S%2.2x", signal);
907 gdb_put_packet(connection, sig_reply, 3);
909 return ERROR_OK;
912 static int gdb_reg_pos(struct target *target, int pos, int len)
914 if (target->endianness == TARGET_LITTLE_ENDIAN)
915 return pos;
916 else
917 return len - 1 - pos;
920 /* Convert register to string of bytes. NB! The # of bits in the
921 * register might be non-divisible by 8(a byte), in which
922 * case an entire byte is shown.
924 * NB! the format on the wire is the target endianness
926 * The format of reg->value is little endian
929 void gdb_str_to_target(struct target *target, char *tstr, struct reg *reg)
931 int i;
933 uint8_t *buf;
934 int buf_len;
935 buf = reg->value;
936 buf_len = DIV_ROUND_UP(reg->size, 8);
938 for (i = 0; i < buf_len; i++)
940 int j = gdb_reg_pos(target, i, buf_len);
941 tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
942 tstr[i*2 + 1] = DIGITS[buf[j]&0xf];
946 static int hextoint(int c)
948 if (c>='0'&&c<='9')
950 return c-'0';
952 c = toupper(c);
953 if (c>='A'&&c<='F')
955 return c-'A'+10;
957 LOG_ERROR("BUG: invalid register value %08x", c);
958 return 0;
961 /* copy over in register buffer */
962 void gdb_target_to_reg(struct target *target, char *tstr, int str_len, uint8_t *bin)
964 if (str_len % 2)
966 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
967 exit(-1);
970 int i;
971 for (i = 0; i < str_len; i += 2)
973 uint8_t t = hextoint(tstr[i]) << 4;
974 t |= hextoint(tstr[i + 1]);
976 int j = gdb_reg_pos(target, i/2, str_len/2);
977 bin[j] = t;
981 int gdb_get_registers_packet(struct connection *connection, struct target *target, char* packet, int packet_size)
983 struct reg **reg_list;
984 int reg_list_size;
985 int retval;
986 int reg_packet_size = 0;
987 char *reg_packet;
988 char *reg_packet_p;
989 int i;
991 #ifdef _DEBUG_GDB_IO_
992 LOG_DEBUG("-");
993 #endif
995 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
997 return gdb_error(connection, retval);
1000 for (i = 0; i < reg_list_size; i++)
1002 reg_packet_size += reg_list[i]->size;
1005 reg_packet = malloc(DIV_ROUND_UP(reg_packet_size, 8) * 2);
1006 reg_packet_p = reg_packet;
1008 for (i = 0; i < reg_list_size; i++)
1010 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
1011 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1014 #ifdef _DEBUG_GDB_IO_
1016 char *reg_packet_p;
1017 reg_packet_p = strndup(reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
1018 LOG_DEBUG("reg_packet: %s", reg_packet_p);
1019 free(reg_packet_p);
1021 #endif
1023 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
1024 free(reg_packet);
1026 free(reg_list);
1028 return ERROR_OK;
1031 int gdb_set_registers_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1033 int i;
1034 struct reg **reg_list;
1035 int reg_list_size;
1036 int retval;
1037 char *packet_p;
1039 #ifdef _DEBUG_GDB_IO_
1040 LOG_DEBUG("-");
1041 #endif
1043 /* skip command character */
1044 packet++;
1045 packet_size--;
1047 if (packet_size % 2)
1049 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1050 return ERROR_SERVER_REMOTE_CLOSED;
1053 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1055 return gdb_error(connection, retval);
1058 packet_p = packet;
1059 for (i = 0; i < reg_list_size; i++)
1061 uint8_t *bin_buf;
1062 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1064 if (packet_p + chars > packet + packet_size)
1066 LOG_ERROR("BUG: register packet is too small for registers");
1069 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1070 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1072 reg_list[i]->type->set(reg_list[i], bin_buf);
1074 /* advance packet pointer */
1075 packet_p += chars;
1078 free(bin_buf);
1081 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1082 free(reg_list);
1084 gdb_put_packet(connection, "OK", 2);
1086 return ERROR_OK;
1089 int gdb_get_register_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1091 char *reg_packet;
1092 int reg_num = strtoul(packet + 1, NULL, 16);
1093 struct reg **reg_list;
1094 int reg_list_size;
1095 int retval;
1097 #ifdef _DEBUG_GDB_IO_
1098 LOG_DEBUG("-");
1099 #endif
1101 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1103 return gdb_error(connection, retval);
1106 if (reg_list_size <= reg_num)
1108 LOG_ERROR("gdb requested a non-existing register");
1109 exit(-1);
1112 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1114 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1116 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1118 free(reg_list);
1119 free(reg_packet);
1121 return ERROR_OK;
1124 int gdb_set_register_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1126 char *separator;
1127 uint8_t *bin_buf;
1128 int reg_num = strtoul(packet + 1, &separator, 16);
1129 struct reg **reg_list;
1130 int reg_list_size;
1131 int retval;
1133 LOG_DEBUG("-");
1135 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1137 return gdb_error(connection, retval);
1140 if (reg_list_size < reg_num)
1142 LOG_ERROR("gdb requested a non-existing register");
1143 return ERROR_SERVER_REMOTE_CLOSED;
1146 if (*separator != '=')
1148 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1149 return ERROR_SERVER_REMOTE_CLOSED;
1152 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1153 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1154 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1156 /* fix!!! add some sanity checks on packet size here */
1158 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1160 reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1162 gdb_put_packet(connection, "OK", 2);
1164 free(bin_buf);
1165 free(reg_list);
1167 return ERROR_OK;
1170 int gdb_error(struct connection *connection, int retval)
1172 switch (retval)
1174 case ERROR_TARGET_DATA_ABORT:
1175 gdb_send_error(connection, EIO);
1176 break;
1177 case ERROR_TARGET_TRANSLATION_FAULT:
1178 gdb_send_error(connection, EFAULT);
1179 break;
1180 case ERROR_TARGET_UNALIGNED_ACCESS:
1181 gdb_send_error(connection, EFAULT);
1182 break;
1183 case ERROR_TARGET_NOT_HALTED:
1184 gdb_send_error(connection, EFAULT);
1185 break;
1186 default:
1187 /* This could be that the target reset itself. */
1188 LOG_ERROR("unexpected error %i", retval);
1189 gdb_send_error(connection, EFAULT);
1190 break;
1193 return ERROR_OK;
1196 /* We don't have to worry about the default 2 second timeout for GDB packets,
1197 * because GDB breaks up large memory reads into smaller reads.
1199 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1201 int gdb_read_memory_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1203 char *separator;
1204 uint32_t addr = 0;
1205 uint32_t len = 0;
1207 uint8_t *buffer;
1208 char *hex_buffer;
1210 int retval = ERROR_OK;
1212 /* skip command character */
1213 packet++;
1215 addr = strtoul(packet, &separator, 16);
1217 if (*separator != ',')
1219 LOG_ERROR("incomplete read memory packet received, dropping connection");
1220 return ERROR_SERVER_REMOTE_CLOSED;
1223 len = strtoul(separator + 1, NULL, 16);
1225 buffer = malloc(len);
1227 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1229 retval = target_read_buffer(target, addr, len, buffer);
1231 if ((retval != ERROR_OK)&&!gdb_report_data_abort)
1233 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1234 * At some point this might be fixed in GDB, in which case this code can be removed.
1236 * OpenOCD developers are acutely aware of this problem, but there is nothing
1237 * gained by involving the user in this problem that hopefully will get resolved
1238 * eventually
1240 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1242 * For now, the default is to fix up things to make current GDB versions work.
1243 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1245 memset(buffer, 0, len);
1246 retval = ERROR_OK;
1249 if (retval == ERROR_OK)
1251 hex_buffer = malloc(len * 2 + 1);
1253 uint32_t i;
1254 for (i = 0; i < len; i++)
1256 uint8_t t = buffer[i];
1257 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1258 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1261 gdb_put_packet(connection, hex_buffer, len * 2);
1263 free(hex_buffer);
1265 else
1267 retval = gdb_error(connection, retval);
1270 free(buffer);
1272 return retval;
1275 int gdb_write_memory_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1277 char *separator;
1278 uint32_t addr = 0;
1279 uint32_t len = 0;
1281 uint8_t *buffer;
1283 uint32_t i;
1284 int retval;
1286 /* skip command character */
1287 packet++;
1289 addr = strtoul(packet, &separator, 16);
1291 if (*separator != ',')
1293 LOG_ERROR("incomplete write memory packet received, dropping connection");
1294 return ERROR_SERVER_REMOTE_CLOSED;
1297 len = strtoul(separator + 1, &separator, 16);
1299 if (*(separator++) != ':')
1301 LOG_ERROR("incomplete write memory packet received, dropping connection");
1302 return ERROR_SERVER_REMOTE_CLOSED;
1305 buffer = malloc(len);
1307 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1309 for (i = 0; i < len; i++)
1311 uint32_t tmp;
1312 sscanf(separator + 2*i, "%2" SCNx32 , &tmp);
1313 buffer[i] = tmp;
1316 retval = target_write_buffer(target, addr, len, buffer);
1318 if (retval == ERROR_OK)
1320 gdb_put_packet(connection, "OK", 2);
1322 else
1324 retval = gdb_error(connection, retval);
1327 free(buffer);
1329 return retval;
1332 int gdb_write_memory_binary_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1334 char *separator;
1335 uint32_t addr = 0;
1336 uint32_t len = 0;
1338 int retval;
1340 /* skip command character */
1341 packet++;
1343 addr = strtoul(packet, &separator, 16);
1345 if (*separator != ',')
1347 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1348 return ERROR_SERVER_REMOTE_CLOSED;
1351 len = strtoul(separator + 1, &separator, 16);
1353 if (*(separator++) != ':')
1355 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1356 return ERROR_SERVER_REMOTE_CLOSED;
1359 retval = ERROR_OK;
1360 if (len)
1362 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1364 retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
1367 if (retval == ERROR_OK)
1369 gdb_put_packet(connection, "OK", 2);
1371 else
1373 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1374 return retval;
1377 return ERROR_OK;
1380 int gdb_step_continue_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1382 int current = 0;
1383 uint32_t address = 0x0;
1384 int retval = ERROR_OK;
1386 LOG_DEBUG("-");
1388 if (packet_size > 1)
1390 packet[packet_size] = 0;
1391 address = strtoul(packet + 1, NULL, 16);
1393 else
1395 current = 1;
1398 if (packet[0] == 'c')
1400 LOG_DEBUG("continue");
1401 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
1402 retval = target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1404 else if (packet[0] == 's')
1406 LOG_DEBUG("step");
1407 /* step at current or address, don't handle breakpoints */
1408 retval = target_step(target, current, address, 0);
1410 return retval;
1413 int gdb_breakpoint_watchpoint_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1415 int type;
1416 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1417 enum watchpoint_rw wp_type;
1418 uint32_t address;
1419 uint32_t size;
1420 char *separator;
1421 int retval;
1423 LOG_DEBUG("-");
1425 type = strtoul(packet + 1, &separator, 16);
1427 if (type == 0) /* memory breakpoint */
1428 bp_type = BKPT_SOFT;
1429 else if (type == 1) /* hardware breakpoint */
1430 bp_type = BKPT_HARD;
1431 else if (type == 2) /* write watchpoint */
1432 wp_type = WPT_WRITE;
1433 else if (type == 3) /* read watchpoint */
1434 wp_type = WPT_READ;
1435 else if (type == 4) /* access watchpoint */
1436 wp_type = WPT_ACCESS;
1438 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT)||(bp_type == BKPT_HARD)))
1440 bp_type = gdb_breakpoint_override_type;
1443 if (*separator != ',')
1445 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1446 return ERROR_SERVER_REMOTE_CLOSED;
1449 address = strtoul(separator + 1, &separator, 16);
1451 if (*separator != ',')
1453 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1454 return ERROR_SERVER_REMOTE_CLOSED;
1457 size = strtoul(separator + 1, &separator, 16);
1459 switch (type)
1461 case 0:
1462 case 1:
1463 if (packet[0] == 'Z')
1465 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1467 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1468 return retval;
1470 else
1472 gdb_put_packet(connection, "OK", 2);
1475 else
1477 breakpoint_remove(target, address);
1478 gdb_put_packet(connection, "OK", 2);
1480 break;
1481 case 2:
1482 case 3:
1483 case 4:
1485 if (packet[0] == 'Z')
1487 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1489 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1490 return retval;
1492 else
1494 gdb_put_packet(connection, "OK", 2);
1497 else
1499 watchpoint_remove(target, address);
1500 gdb_put_packet(connection, "OK", 2);
1502 break;
1504 default:
1505 break;
1508 return ERROR_OK;
1511 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1512 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1514 if (*retval != ERROR_OK)
1516 return;
1518 int first = 1;
1520 for (;;)
1522 if ((*xml == NULL) || (!first))
1524 /* start by 0 to exercise all the code paths.
1525 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1527 *size = *size * 2 + 2;
1528 char *t = *xml;
1529 *xml = realloc(*xml, *size);
1530 if (*xml == NULL)
1532 if (t)
1533 free(t);
1534 *retval = ERROR_SERVER_REMOTE_CLOSED;
1535 return;
1539 va_list ap;
1540 int ret;
1541 va_start(ap, fmt);
1542 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1543 va_end(ap);
1544 if ((ret > 0) && ((ret + 1) < *size - *pos))
1546 *pos += ret;
1547 return;
1549 /* there was just enough or not enough space, allocate more. */
1550 first = 0;
1554 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1556 char *separator;
1558 /* Extract and NUL-terminate the annex. */
1559 *annex = buf;
1560 while (*buf && *buf != ':')
1561 buf++;
1562 if (*buf == '\0')
1563 return -1;
1564 *buf++ = 0;
1566 /* After the read marker and annex, qXfer looks like a
1567 * traditional 'm' packet. */
1569 *ofs = strtoul(buf, &separator, 16);
1571 if (*separator != ',')
1572 return -1;
1574 *len = strtoul(separator + 1, NULL, 16);
1576 return 0;
1579 int gdb_calc_blocksize(struct flash_bank *bank)
1581 uint32_t i;
1582 uint32_t block_size = 0xffffffff;
1584 /* loop through all sectors and return smallest sector size */
1586 for (i = 0; i < (uint32_t)bank->num_sectors; i++)
1588 if (bank->sectors[i].size < block_size)
1589 block_size = bank->sectors[i].size;
1592 return block_size;
1595 static int compare_bank (const void * a, const void * b)
1597 struct flash_bank *b1, *b2;
1598 b1=*((struct flash_bank **)a);
1599 b2=*((struct flash_bank **)b);
1601 if (b1->base == b2->base)
1603 return 0;
1604 } else if (b1->base > b2->base)
1606 return 1;
1607 } else
1609 return -1;
1613 int gdb_query_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1615 struct command_context *cmd_ctx = connection->cmd_ctx;
1616 struct gdb_connection *gdb_connection = connection->priv;
1618 if (strstr(packet, "qRcmd,"))
1620 if (packet_size > 6)
1622 char *cmd;
1623 int i;
1624 cmd = malloc((packet_size - 6)/2 + 1);
1625 for (i = 0; i < (packet_size - 6)/2; i++)
1627 uint32_t tmp;
1628 sscanf(packet + 6 + 2*i, "%2" SCNx32 , &tmp);
1629 cmd[i] = tmp;
1631 cmd[(packet_size - 6)/2] = 0x0;
1633 /* We want to print all debug output to GDB connection */
1634 log_add_callback(gdb_log_callback, connection);
1635 target_call_timer_callbacks_now();
1636 /* some commands need to know the GDB connection, make note of current
1637 * GDB connection. */
1638 current_gdb_connection = gdb_connection;
1639 command_run_line(cmd_ctx, cmd);
1640 current_gdb_connection = NULL;
1641 target_call_timer_callbacks_now();
1642 log_remove_callback(gdb_log_callback, connection);
1643 free(cmd);
1645 gdb_put_packet(connection, "OK", 2);
1646 return ERROR_OK;
1648 else if (strstr(packet, "qCRC:"))
1650 if (packet_size > 5)
1652 int retval;
1653 char gdb_reply[10];
1654 char *separator;
1655 uint32_t checksum;
1656 uint32_t addr = 0;
1657 uint32_t len = 0;
1659 /* skip command character */
1660 packet += 5;
1662 addr = strtoul(packet, &separator, 16);
1664 if (*separator != ',')
1666 LOG_ERROR("incomplete read memory packet received, dropping connection");
1667 return ERROR_SERVER_REMOTE_CLOSED;
1670 len = strtoul(separator + 1, NULL, 16);
1672 retval = target_checksum_memory(target, addr, len, &checksum);
1674 if (retval == ERROR_OK)
1676 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
1677 gdb_put_packet(connection, gdb_reply, 9);
1679 else
1681 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1682 return retval;
1685 return ERROR_OK;
1688 else if (strstr(packet, "qSupported"))
1690 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1691 * disable qXfer:features:read for the moment */
1692 int retval = ERROR_OK;
1693 char *buffer = NULL;
1694 int pos = 0;
1695 int size = 0;
1697 xml_printf(&retval, &buffer, &pos, &size,
1698 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1699 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1701 if (retval != ERROR_OK)
1703 gdb_send_error(connection, 01);
1704 return ERROR_OK;
1707 gdb_put_packet(connection, buffer, strlen(buffer));
1708 free(buffer);
1710 return ERROR_OK;
1712 else if (strstr(packet, "qXfer:memory-map:read::") && (flash_get_bank_count() > 0))
1714 /* We get away with only specifying flash here. Regions that are not
1715 * specified are treated as if we provided no memory map(if not we
1716 * could detect the holes and mark them as RAM).
1717 * Normally we only execute this code once, but no big deal if we
1718 * have to regenerate it a couple of times. */
1720 struct flash_bank *p;
1721 char *xml = NULL;
1722 int size = 0;
1723 int pos = 0;
1724 int retval = ERROR_OK;
1726 int offset;
1727 int length;
1728 char *separator;
1729 int blocksize;
1731 /* skip command character */
1732 packet += 23;
1734 offset = strtoul(packet, &separator, 16);
1735 length = strtoul(separator + 1, &separator, 16);
1737 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1740 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1741 read/write) by default for GDB.
1742 GDB does not have a concept of non-cacheable read/write memory.
1744 struct flash_bank **banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1745 int i;
1747 for (i = 0; i < flash_get_bank_count(); i++)
1749 p = get_flash_bank_by_num(i);
1750 if (p == NULL)
1752 free(banks);
1753 retval = ERROR_FAIL;
1754 gdb_send_error(connection, retval);
1755 return retval;
1757 banks[i]=p;
1760 qsort(banks, flash_get_bank_count(), sizeof(struct flash_bank *), compare_bank);
1762 uint32_t ram_start = 0;
1763 for (i = 0; i < flash_get_bank_count(); i++)
1765 p = banks[i];
1767 if (ram_start < p->base)
1769 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1770 ram_start, p->base-ram_start);
1773 /* if device has uneven sector sizes, eg. str7, lpc
1774 * we pass the smallest sector size to gdb memory map */
1775 blocksize = gdb_calc_blocksize(p);
1777 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1778 "<property name=\"blocksize\">0x%x</property>\n" \
1779 "</memory>\n", \
1780 p->base, p->size, blocksize);
1781 ram_start = p->base + p->size;
1783 if (ram_start != 0)
1785 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1786 ram_start, 0-ram_start);
1787 } else
1789 /* a flash chip could be at the very end of the 32 bit address space, in which case
1790 ram_start will be precisely 0 */
1793 free(banks);
1794 banks = NULL;
1796 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1798 if (retval != ERROR_OK)
1800 gdb_send_error(connection, retval);
1801 return retval;
1804 if (offset + length > pos)
1806 length = pos - offset;
1809 char *t = malloc(length + 1);
1810 t[0] = 'l';
1811 memcpy(t + 1, xml + offset, length);
1812 gdb_put_packet(connection, t, length + 1);
1814 free(t);
1815 free(xml);
1816 return ERROR_OK;
1818 else if (strstr(packet, "qXfer:features:read:"))
1820 char *xml = NULL;
1821 int size = 0;
1822 int pos = 0;
1823 int retval = ERROR_OK;
1825 int offset;
1826 unsigned int length;
1827 char *annex;
1829 /* skip command character */
1830 packet += 20;
1832 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1834 gdb_send_error(connection, 01);
1835 return ERROR_OK;
1838 if (strcmp(annex, "target.xml") != 0)
1840 gdb_send_error(connection, 01);
1841 return ERROR_OK;
1844 xml_printf(&retval, &xml, &pos, &size, \
1845 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1847 if (retval != ERROR_OK)
1849 gdb_send_error(connection, retval);
1850 return retval;
1853 gdb_put_packet(connection, xml, strlen(xml));
1855 free(xml);
1856 return ERROR_OK;
1858 else if (strstr(packet, "QStartNoAckMode"))
1860 gdb_connection->noack_mode = 1;
1861 gdb_put_packet(connection, "OK", 2);
1862 return ERROR_OK;
1865 gdb_put_packet(connection, "", 0);
1866 return ERROR_OK;
1869 int gdb_v_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1871 struct gdb_connection *gdb_connection = connection->priv;
1872 struct gdb_service *gdb_service = connection->service->priv;
1873 int result;
1875 /* if flash programming disabled - send a empty reply */
1877 if (gdb_flash_program == 0)
1879 gdb_put_packet(connection, "", 0);
1880 return ERROR_OK;
1883 if (strstr(packet, "vFlashErase:"))
1885 unsigned long addr;
1886 unsigned long length;
1888 char *parse = packet + 12;
1889 if (*parse == '\0')
1891 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1892 return ERROR_SERVER_REMOTE_CLOSED;
1895 addr = strtoul(parse, &parse, 16);
1897 if (*(parse++) != ',' || *parse == '\0')
1899 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1900 return ERROR_SERVER_REMOTE_CLOSED;
1903 length = strtoul(parse, &parse, 16);
1905 if (*parse != '\0')
1907 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1908 return ERROR_SERVER_REMOTE_CLOSED;
1911 /* assume all sectors need erasing - stops any problems
1912 * when flash_write is called multiple times */
1913 flash_set_dirty();
1915 /* perform any target specific operations before the erase */
1916 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1917 result = flash_erase_address_range(gdb_service->target, addr, length);
1918 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
1920 /* perform erase */
1921 if (result != ERROR_OK)
1923 /* GDB doesn't evaluate the actual error number returned,
1924 * treat a failed erase as an I/O error
1926 gdb_send_error(connection, EIO);
1927 LOG_ERROR("flash_erase returned %i", result);
1929 else
1930 gdb_put_packet(connection, "OK", 2);
1932 return ERROR_OK;
1935 if (strstr(packet, "vFlashWrite:"))
1937 int retval;
1938 unsigned long addr;
1939 unsigned long length;
1940 char *parse = packet + 12;
1942 if (*parse == '\0')
1944 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1945 return ERROR_SERVER_REMOTE_CLOSED;
1947 addr = strtoul(parse, &parse, 16);
1948 if (*(parse++) != ':')
1950 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1951 return ERROR_SERVER_REMOTE_CLOSED;
1953 length = packet_size - (parse - packet);
1955 /* create a new image if there isn't already one */
1956 if (gdb_connection->vflash_image == NULL)
1958 gdb_connection->vflash_image = malloc(sizeof(struct image));
1959 image_open(gdb_connection->vflash_image, "", "build");
1962 /* create new section with content from packet buffer */
1963 if ((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (uint8_t*)parse)) != ERROR_OK)
1965 return retval;
1968 gdb_put_packet(connection, "OK", 2);
1970 return ERROR_OK;
1973 if (!strcmp(packet, "vFlashDone"))
1975 uint32_t written;
1977 /* process the flashing buffer. No need to erase as GDB
1978 * always issues a vFlashErase first. */
1979 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
1980 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1981 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1982 if (result != ERROR_OK)
1984 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1985 gdb_put_packet(connection, "E.memtype", 9);
1986 else
1987 gdb_send_error(connection, EIO);
1989 else
1991 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
1992 gdb_put_packet(connection, "OK", 2);
1995 image_close(gdb_connection->vflash_image);
1996 free(gdb_connection->vflash_image);
1997 gdb_connection->vflash_image = NULL;
1999 return ERROR_OK;
2002 gdb_put_packet(connection, "", 0);
2003 return ERROR_OK;
2006 int gdb_detach(struct connection *connection, struct target *target)
2008 struct gdb_service *gdb_service = connection->service->priv;
2010 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
2012 return gdb_put_packet(connection, "OK", 2);
2015 static void gdb_log_callback(void *priv, const char *file, unsigned line,
2016 const char *function, const char *string)
2018 struct connection *connection = priv;
2019 struct gdb_connection *gdb_con = connection->priv;
2021 if (gdb_con->busy)
2023 /* do not reply this using the O packet */
2024 return;
2027 gdb_output_con(connection, string);
2030 /* Do not allocate this on the stack */
2031 char gdb_packet_buffer[GDB_BUFFER_SIZE];
2033 static void gdb_sig_halted(struct connection *connection)
2035 char sig_reply[4];
2036 snprintf(sig_reply, 4, "T%2.2x", 2);
2037 gdb_put_packet(connection, sig_reply, 3);
2041 int gdb_input_inner(struct connection *connection)
2043 struct gdb_service *gdb_service = connection->service->priv;
2044 struct target *target = gdb_service->target;
2045 char *packet = gdb_packet_buffer;
2046 int packet_size;
2047 int retval;
2048 struct gdb_connection *gdb_con = connection->priv;
2049 static int extended_protocol = 0;
2051 /* drain input buffer */
2054 packet_size = GDB_BUFFER_SIZE-1;
2055 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
2057 return retval;
2060 /* terminate with zero */
2061 packet[packet_size] = 0;
2063 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
2064 if (packet[0] == 'X') {
2065 // binary packets spew junk into the debug log stream
2066 char buf[ 50 ];
2067 int x;
2068 for (x = 0 ; (x < 49) && (packet[x] != ':') ; x++) {
2069 buf[x] = packet[x];
2071 buf[x] = 0;
2072 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
2073 } else {
2074 LOG_DEBUG("received packet: '%s'", packet);
2078 if (packet_size > 0)
2080 retval = ERROR_OK;
2081 switch (packet[0])
2083 case 'H':
2084 /* Hct... -- set thread
2085 * we don't have threads, send empty reply */
2086 gdb_put_packet(connection, NULL, 0);
2087 break;
2088 case 'q':
2089 case 'Q':
2090 retval = gdb_query_packet(connection, target, packet, packet_size);
2091 break;
2092 case 'g':
2093 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
2094 break;
2095 case 'G':
2096 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
2097 break;
2098 case 'p':
2099 retval = gdb_get_register_packet(connection, target, packet, packet_size);
2100 break;
2101 case 'P':
2102 retval = gdb_set_register_packet(connection, target, packet, packet_size);
2103 break;
2104 case 'm':
2105 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
2106 break;
2107 case 'M':
2108 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
2109 break;
2110 case 'z':
2111 case 'Z':
2112 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
2113 break;
2114 case '?':
2115 gdb_last_signal_packet(connection, target, packet, packet_size);
2116 break;
2117 case 'c':
2118 case 's':
2120 int retval = ERROR_OK;
2122 struct gdb_connection *gdb_con = connection->priv;
2123 log_add_callback(gdb_log_callback, connection);
2125 bool nostep = false;
2126 if (target->state == TARGET_RUNNING)
2128 LOG_WARNING("The target is already running. Halt target before stepi/continue.");
2129 retval = target_halt(target);
2130 if (retval == ERROR_OK)
2131 retval = target_wait_state(target, TARGET_HALTED, 100);
2132 } else if (target->state != TARGET_HALTED)
2134 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2135 nostep = true;
2136 } else if ((packet[0] == 's') && gdb_con->sync)
2138 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2139 * sent by GDB first to OpenOCD, thus defeating the check to
2140 * make only the single stepping have the sync feature...
2142 nostep = true;
2143 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2145 gdb_con->sync = false;
2147 if ((retval!=ERROR_OK) || nostep)
2149 /* Either the target isn't in the halted state, then we can't
2150 * step/continue. This might be early setup, etc.
2152 * Or we want to allow GDB to pick up a fresh set of
2153 * register values without modifying the target state.
2156 gdb_sig_halted(connection);
2158 /* stop forwarding log packets! */
2159 log_remove_callback(gdb_log_callback, connection);
2160 } else
2162 /* We're running/stepping, in which case we can
2163 * forward log output until the target is halted
2165 gdb_con->frontend_state = TARGET_RUNNING;
2166 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2167 int retval = gdb_step_continue_packet(connection, target, packet, packet_size);
2168 if (retval != ERROR_OK)
2170 /* we'll never receive a halted condition... issue a false one.. */
2171 gdb_frontend_halted(target, connection);
2175 break;
2176 case 'v':
2177 retval = gdb_v_packet(connection, target, packet, packet_size);
2178 break;
2179 case 'D':
2180 retval = gdb_detach(connection, target);
2181 extended_protocol = 0;
2182 break;
2183 case 'X':
2184 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2185 return retval;
2186 break;
2187 case 'k':
2188 if (extended_protocol != 0)
2189 break;
2190 gdb_put_packet(connection, "OK", 2);
2191 return ERROR_SERVER_REMOTE_CLOSED;
2192 case '!':
2193 /* handle extended remote protocol */
2194 extended_protocol = 1;
2195 gdb_put_packet(connection, "OK", 2);
2196 break;
2197 case 'R':
2198 /* handle extended restart packet */
2199 breakpoint_clear_target(gdb_service->target);
2200 watchpoint_clear_target(gdb_service->target);
2201 command_run_linef(connection->cmd_ctx,
2202 "ocd_gdb_restart %s",
2203 target_name(target));
2204 break;
2205 default:
2206 /* ignore unknown packets */
2207 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2208 gdb_put_packet(connection, NULL, 0);
2209 break;
2212 /* if a packet handler returned an error, exit input loop */
2213 if (retval != ERROR_OK)
2214 return retval;
2217 if (gdb_con->ctrl_c)
2219 if (target->state == TARGET_RUNNING)
2221 retval = target_halt(target);
2222 if (retval != ERROR_OK)
2224 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2226 gdb_con->ctrl_c = 0;
2227 } else
2229 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2230 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2234 } while (gdb_con->buf_cnt > 0);
2236 return ERROR_OK;
2239 int gdb_input(struct connection *connection)
2241 int retval = gdb_input_inner(connection);
2242 struct gdb_connection *gdb_con = connection->priv;
2243 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2244 return retval;
2246 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2247 if (gdb_con->closed)
2248 return ERROR_SERVER_REMOTE_CLOSED;
2250 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2251 return ERROR_OK;
2254 static int gdb_target_start(struct target *target, uint16_t port)
2256 bool use_pipes = 0 == port;
2257 struct gdb_service *gdb_service = malloc(sizeof(struct gdb_service));
2258 if (NULL == gdb_service)
2259 return -ENOMEM;
2261 gdb_service->target = target;
2263 add_service("gdb", use_pipes ? CONNECTION_PIPE : CONNECTION_TCP,
2264 port, 1, &gdb_new_connection, &gdb_input,
2265 &gdb_connection_closed, gdb_service);
2267 const char *name = target_name(target);
2268 if (use_pipes)
2269 LOG_DEBUG("gdb service for target '%s' using pipes", name);
2270 else
2271 LOG_DEBUG("gdb service for target '%s' on TCP port %u", name, port);
2272 return ERROR_OK;
2275 /* FIXME static */
2276 int gdb_target_add_one(struct target *target)
2278 if (gdb_port == 0 && server_use_pipes == 0)
2280 LOG_INFO("gdb port disabled");
2281 return ERROR_OK;
2283 if (0 == gdb_port_next)
2284 gdb_port_next = gdb_port;
2286 bool use_pipes = server_use_pipes;
2287 static bool server_started_with_pipes = false;
2288 if (server_started_with_pipes)
2290 LOG_WARNING("gdb service permits one target when using pipes");
2291 if (0 == gdb_port)
2292 return ERROR_OK;
2294 use_pipes = false;
2297 int e = gdb_target_start(target, use_pipes ? 0 : gdb_port_next);
2298 if (ERROR_OK == e)
2300 server_started_with_pipes |= use_pipes;
2301 gdb_port_next++;
2303 return e;
2306 int gdb_target_add_all(struct target *target)
2308 if (NULL == target)
2310 LOG_WARNING("gdb services need one or more targets defined");
2311 return ERROR_OK;
2314 while (NULL != target)
2316 int retval = gdb_target_add_one(target);
2317 if (ERROR_OK != retval)
2318 return retval;
2320 target = target->next;
2323 return ERROR_OK;
2326 COMMAND_HANDLER(handle_gdb_sync_command)
2328 if (CMD_ARGC != 0)
2330 return ERROR_COMMAND_SYNTAX_ERROR;
2333 if (current_gdb_connection == NULL)
2335 command_print(CMD_CTX,
2336 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2337 return ERROR_FAIL;
2340 current_gdb_connection->sync = true;
2342 return ERROR_OK;
2345 /* daemon configuration command gdb_port */
2346 COMMAND_HANDLER(handle_gdb_port_command)
2348 int retval = CALL_COMMAND_HANDLER(server_port_command, &gdb_port);
2349 if (ERROR_OK == retval)
2350 gdb_port_next = gdb_port;
2351 return retval;
2354 COMMAND_HANDLER(handle_gdb_memory_map_command)
2356 if (CMD_ARGC == 1)
2357 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_use_memory_map);
2359 return ERROR_COMMAND_SYNTAX_ERROR;
2362 COMMAND_HANDLER(handle_gdb_flash_program_command)
2364 if (CMD_ARGC == 1)
2365 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_flash_program);
2367 return ERROR_COMMAND_SYNTAX_ERROR;
2370 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
2372 if (CMD_ARGC == 1)
2373 COMMAND_PARSE_ENABLE(CMD_ARGV[0], gdb_report_data_abort);
2375 return ERROR_COMMAND_SYNTAX_ERROR;
2378 /* gdb_breakpoint_override */
2379 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
2381 if (CMD_ARGC == 0)
2384 } else if (CMD_ARGC == 1)
2386 gdb_breakpoint_override = 1;
2387 if (strcmp(CMD_ARGV[0], "hard") == 0)
2389 gdb_breakpoint_override_type = BKPT_HARD;
2390 } else if (strcmp(CMD_ARGV[0], "soft") == 0)
2392 gdb_breakpoint_override_type = BKPT_SOFT;
2393 } else if (strcmp(CMD_ARGV[0], "disable") == 0)
2395 gdb_breakpoint_override = 0;
2397 } else
2399 return ERROR_COMMAND_SYNTAX_ERROR;
2401 if (gdb_breakpoint_override)
2403 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type == BKPT_HARD)?"hard":"soft");
2404 } else
2406 LOG_USER("breakpoint type is not overridden");
2409 return ERROR_OK;
2412 static const struct command_registration gdb_command_handlers[] = {
2414 .name = "gdb_sync",
2415 .handler = &handle_gdb_sync_command,
2416 .mode = COMMAND_ANY,
2417 .help = "next stepi will return immediately allowing "
2418 "GDB to fetch register state without affecting "
2419 "target state",
2422 .name = "gdb_port",
2423 .handler = &handle_gdb_port_command,
2424 .mode = COMMAND_ANY,
2425 .help = "daemon configuration command gdb_port. No arguments reports "
2426 "GDB port.",
2427 .usage = "<port>",
2430 .name = "gdb_memory_map",
2431 .handler = &handle_gdb_memory_map_command,
2432 .mode = COMMAND_CONFIG,
2433 .help = "enable or disable memory map",
2434 .usage = "enable|disable"
2437 .name = "gdb_flash_program",
2438 .handler = &handle_gdb_flash_program_command,
2439 .mode = COMMAND_CONFIG,
2440 .help = "enable or disable flash program",
2441 .usage = "enable|disable"
2444 .name = "gdb_report_data_abort",
2445 .handler = &handle_gdb_report_data_abort_command,
2446 .mode = COMMAND_CONFIG,
2447 .help = "enable or disable reporting data aborts",
2448 .usage = "enable|disable"
2451 .name = "gdb_breakpoint_override",
2452 .handler = &handle_gdb_breakpoint_override_command,
2453 .mode = COMMAND_EXEC,
2454 .help = "force type of breakpoint "
2455 "used by gdb 'break' commands.",
2456 .usage = "hard|soft|disable",
2458 COMMAND_REGISTRATION_DONE
2461 int gdb_register_commands(struct command_context *cmd_ctx)
2463 return register_commands(cmd_ctx, NULL, gdb_command_handlers);