- Fixes '=' whitespace
[openocd.git] / src / server / gdb_server.c
blob4cf187576758639d0a3ab7a975dc7f2715867019
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "gdb_server.h"
31 #include "target_request.h"
32 #include "register.h"
33 #include "server.h"
34 #include "flash.h"
35 #include "image.h"
36 #include "jtag.h"
39 #if 0
40 #define _DEBUG_GDB_IO_
41 #endif
43 static int gdb_breakpoint_override;
44 static enum breakpoint_type gdb_breakpoint_override_type;
46 extern int gdb_error(connection_t *connection, int retval);
47 static unsigned short gdb_port = 3333;
48 static const char *DIGITS = "0123456789abcdef";
50 static void gdb_log_callback(void *priv, const char *file, int line,
51 const char *function, const char *string);
53 enum gdb_detach_mode
55 GDB_DETACH_RESUME,
56 GDB_DETACH_RESET,
57 GDB_DETACH_HALT,
58 GDB_DETACH_NOTHING
61 /* target behaviour on gdb detach */
62 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
64 /* number of gdb connections, mainly to supress gdb related debugging spam
65 * in helper/log.c when no gdb connections are actually active */
66 int gdb_actual_connections;
68 /* set if we are sending a memory map to gdb
69 * via qXfer:memory-map:read packet */
70 /* enabled by default*/
71 int gdb_use_memory_map = 1;
72 /* enabled by default*/
73 int gdb_flash_program = 1;
75 /* if set, data aborts cause an error to be reported in memory read packets
76 * see the code in gdb_read_memory_packet() for further explanations */
77 int gdb_report_data_abort = 0;
79 int gdb_last_signal(target_t *target)
81 switch (target->debug_reason)
83 case DBG_REASON_DBGRQ:
84 return 0x2; /* SIGINT */
85 case DBG_REASON_BREAKPOINT:
86 case DBG_REASON_WATCHPOINT:
87 case DBG_REASON_WPTANDBKPT:
88 return 0x05; /* SIGTRAP */
89 case DBG_REASON_SINGLESTEP:
90 return 0x05; /* SIGTRAP */
91 case DBG_REASON_NOTHALTED:
92 return 0x0; /* no signal... shouldn't happen */
93 default:
94 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
95 return 0x0;
99 int check_pending(connection_t *connection, int timeout_s, int *got_data)
101 /* a non-blocking socket will block if there is 0 bytes available on the socket,
102 * but return with as many bytes as are available immediately
104 struct timeval tv;
105 fd_set read_fds;
106 gdb_connection_t *gdb_con = connection->priv;
107 int t;
108 if (got_data == NULL)
109 got_data=&t;
110 *got_data = 0;
112 if (gdb_con->buf_cnt>0)
114 *got_data = 1;
115 return ERROR_OK;
118 FD_ZERO(&read_fds);
119 FD_SET(connection->fd, &read_fds);
121 tv.tv_sec = timeout_s;
122 tv.tv_usec = 0;
123 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
125 /* This can typically be because a "monitor" command took too long
126 * before printing any progress messages
128 if (timeout_s>0)
130 return ERROR_GDB_TIMEOUT;
131 } else
133 return ERROR_OK;
136 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
137 return ERROR_OK;
140 int gdb_get_char(connection_t *connection, int* next_char)
142 gdb_connection_t *gdb_con = connection->priv;
143 int retval = ERROR_OK;
145 #ifdef _DEBUG_GDB_IO_
146 char *debug_buffer;
147 #endif
149 if (gdb_con->buf_cnt-- > 0)
151 *next_char = *(gdb_con->buf_p++);
152 if (gdb_con->buf_cnt > 0)
153 connection->input_pending = 1;
154 else
155 connection->input_pending = 0;
157 #ifdef _DEBUG_GDB_IO_
158 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
159 #endif
161 return ERROR_OK;
164 for (;;)
166 if (connection->service->type == CONNECTION_PIPE)
168 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
170 else
172 retval = check_pending(connection, 1, NULL);
173 if (retval != ERROR_OK)
174 return retval;
175 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
178 if (gdb_con->buf_cnt > 0)
180 break;
182 if (gdb_con->buf_cnt == 0)
184 gdb_con->closed = 1;
185 return ERROR_SERVER_REMOTE_CLOSED;
188 #ifdef _WIN32
189 errno = WSAGetLastError();
191 switch (errno)
193 case WSAEWOULDBLOCK:
194 usleep(1000);
195 break;
196 case WSAECONNABORTED:
197 gdb_con->closed = 1;
198 return ERROR_SERVER_REMOTE_CLOSED;
199 case WSAECONNRESET:
200 gdb_con->closed = 1;
201 return ERROR_SERVER_REMOTE_CLOSED;
202 default:
203 LOG_ERROR("read: %d", errno);
204 exit(-1);
206 #else
207 switch (errno)
209 case EAGAIN:
210 usleep(1000);
211 break;
212 case ECONNABORTED:
213 gdb_con->closed = 1;
214 return ERROR_SERVER_REMOTE_CLOSED;
215 case ECONNRESET:
216 gdb_con->closed = 1;
217 return ERROR_SERVER_REMOTE_CLOSED;
218 default:
219 LOG_ERROR("read: %s", strerror(errno));
220 gdb_con->closed = 1;
221 return ERROR_SERVER_REMOTE_CLOSED;
223 #endif
226 #ifdef _DEBUG_GDB_IO_
227 debug_buffer = malloc(gdb_con->buf_cnt + 1);
228 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
229 debug_buffer[gdb_con->buf_cnt] = 0;
230 LOG_DEBUG("received '%s'", debug_buffer);
231 free(debug_buffer);
232 #endif
234 gdb_con->buf_p = gdb_con->buffer;
235 gdb_con->buf_cnt--;
236 *next_char = *(gdb_con->buf_p++);
237 if (gdb_con->buf_cnt > 0)
238 connection->input_pending = 1;
239 else
240 connection->input_pending = 0;
241 #ifdef _DEBUG_GDB_IO_
242 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
243 #endif
245 return retval;
248 int gdb_putback_char(connection_t *connection, int last_char)
250 gdb_connection_t *gdb_con = connection->priv;
252 if (gdb_con->buf_p > gdb_con->buffer)
254 *(--gdb_con->buf_p) = last_char;
255 gdb_con->buf_cnt++;
257 else
259 LOG_ERROR("BUG: couldn't put character back");
262 return ERROR_OK;
265 /* The only way we can detect that the socket is closed is the first time
266 * we write to it, we will fail. Subsequent write operations will
267 * succeed. Shudder! */
268 int gdb_write(connection_t *connection, void *data, int len)
270 gdb_connection_t *gdb_con = connection->priv;
271 if (gdb_con->closed)
272 return ERROR_SERVER_REMOTE_CLOSED;
274 if (connection->service->type == CONNECTION_PIPE)
276 /* write to stdout */
277 if (write(STDOUT_FILENO, data, len) == len)
279 return ERROR_OK;
282 else
284 if (write_socket(connection->fd, data, len) == len)
286 return ERROR_OK;
289 gdb_con->closed = 1;
290 return ERROR_SERVER_REMOTE_CLOSED;
293 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
295 int i;
296 unsigned char my_checksum = 0;
297 #ifdef _DEBUG_GDB_IO_
298 char *debug_buffer;
299 #endif
300 int reply;
301 int retval;
302 gdb_connection_t *gdb_con = connection->priv;
304 for (i = 0; i < len; i++)
305 my_checksum += buffer[i];
307 #ifdef _DEBUG_GDB_IO_
309 * At this point we should have nothing in the input queue from GDB,
310 * however sometimes '-' is sent even though we've already received
311 * an ACK (+) for everything we've sent off.
313 int gotdata;
314 for (;;)
316 if ((retval = check_pending(connection, 0, &gotdata)) != ERROR_OK)
317 return retval;
318 if (!gotdata)
319 break;
320 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
321 return retval;
322 if ( reply == '$' ){
323 /* fix a problem with some IAR tools */
324 gdb_putback_char( connection, reply );
325 LOG_DEBUG("Unexpected start of new packet");
326 break;
329 LOG_WARNING("Discard unexpected char %c", reply);
331 #endif
333 while (1)
335 #ifdef _DEBUG_GDB_IO_
336 debug_buffer = malloc(len + 1);
337 memcpy(debug_buffer, buffer, len);
338 debug_buffer[len] = 0;
339 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
340 free(debug_buffer);
341 #endif
343 char local_buffer[1024];
344 local_buffer[0] = '$';
345 if ((size_t)len + 4 <= sizeof(local_buffer))
347 /* performance gain on smaller packets by only a single call to gdb_write() */
348 memcpy(local_buffer+1, buffer, len++);
349 local_buffer[len++] = '#';
350 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
351 local_buffer[len++] = DIGITS[my_checksum & 0xf];
352 if ((retval = gdb_write(connection, local_buffer, len)) != ERROR_OK)
354 return retval;
357 else
359 /* larger packets are transmitted directly from caller supplied buffer
360 by several calls to gdb_write() to avoid dynamic allocation */
361 local_buffer[1] = '#';
362 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
363 local_buffer[3] = DIGITS[my_checksum & 0xf];
364 if ((retval = gdb_write(connection, local_buffer, 1)) != ERROR_OK)
366 return retval;
368 if ((retval = gdb_write(connection, buffer, len)) != ERROR_OK)
370 return retval;
372 if ((retval = gdb_write(connection, local_buffer+1, 3)) != ERROR_OK)
374 return retval;
378 if (gdb_con->noack_mode)
379 break;
381 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
382 return retval;
384 if (reply == '+')
385 break;
386 else if (reply == '-')
388 /* Stop sending output packets for now */
389 log_remove_callback(gdb_log_callback, connection);
390 LOG_WARNING("negative reply, retrying");
392 else if (reply == 0x3)
394 gdb_con->ctrl_c = 1;
395 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
396 return retval;
397 if (reply == '+')
398 break;
399 else if (reply == '-')
401 /* Stop sending output packets for now */
402 log_remove_callback(gdb_log_callback, connection);
403 LOG_WARNING("negative reply, retrying");
405 else if ( reply == '$' ){
406 LOG_ERROR("GDB missing ack(1) - assumed good");
407 gdb_putback_char( connection, reply );
408 return ERROR_OK;
409 } else {
411 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
412 gdb_con->closed = 1;
413 return ERROR_SERVER_REMOTE_CLOSED;
416 else if ( reply == '$' ){
417 LOG_ERROR("GDB missing ack(2) - assumed good");
418 gdb_putback_char( connection, reply );
419 return ERROR_OK;
421 else
423 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply);
424 gdb_con->closed = 1;
425 return ERROR_SERVER_REMOTE_CLOSED;
428 if (gdb_con->closed)
429 return ERROR_SERVER_REMOTE_CLOSED;
431 return ERROR_OK;
434 int gdb_put_packet(connection_t *connection, char *buffer, int len)
436 gdb_connection_t *gdb_con = connection->priv;
437 gdb_con->busy = 1;
438 int retval = gdb_put_packet_inner(connection, buffer, len);
439 gdb_con->busy = 0;
441 /* we sent some data, reset timer for keep alive messages */
442 kept_alive();
444 return retval;
447 static __inline__ int fetch_packet(connection_t *connection, int *checksum_ok, int noack, int *len, char *buffer)
449 unsigned char my_checksum = 0;
450 char checksum[3];
451 int character;
452 int retval;
454 gdb_connection_t *gdb_con = connection->priv;
455 my_checksum = 0;
456 int count = 0;
457 count = 0;
458 for (;;)
460 /* The common case is that we have an entire packet with no escape chars.
461 * We need to leave at least 2 bytes in the buffer to have
462 * gdb_get_char() update various bits and bobs correctly.
464 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
466 /* The compiler will struggle a bit with constant propagation and
467 * aliasing, so we help it by showing that these values do not
468 * change inside the loop
470 int i;
471 char *buf = gdb_con->buf_p;
472 int run = gdb_con->buf_cnt - 2;
473 i = 0;
474 int done = 0;
475 while (i < run)
477 character = *buf++;
478 i++;
479 if (character == '#')
481 /* Danger! character can be '#' when esc is
482 * used so we need an explicit boolean for done here.
484 done = 1;
485 break;
488 if (character == '}')
490 /* data transmitted in binary mode (X packet)
491 * uses 0x7d as escape character */
492 my_checksum += character & 0xff;
493 character = *buf++;
494 i++;
495 my_checksum += character & 0xff;
496 buffer[count++] = (character ^ 0x20) & 0xff;
498 else
500 my_checksum += character & 0xff;
501 buffer[count++] = character & 0xff;
504 gdb_con->buf_p += i;
505 gdb_con->buf_cnt -= i;
506 if (done)
507 break;
509 if (count > *len)
511 LOG_ERROR("packet buffer too small");
512 return ERROR_GDB_BUFFER_TOO_SMALL;
515 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
516 return retval;
518 if (character == '#')
519 break;
521 if (character == '}')
523 /* data transmitted in binary mode (X packet)
524 * uses 0x7d as escape character */
525 my_checksum += character & 0xff;
526 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
527 return retval;
528 my_checksum += character & 0xff;
529 buffer[count++] = (character ^ 0x20) & 0xff;
531 else
533 my_checksum += character & 0xff;
534 buffer[count++] = character & 0xff;
538 *len = count;
540 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
541 return retval;
542 checksum[0] = character;
543 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
544 return retval;
545 checksum[1] = character;
546 checksum[2] = 0;
548 if (!noack)
550 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
553 return ERROR_OK;
556 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
558 int character;
559 int retval;
560 gdb_connection_t *gdb_con = connection->priv;
562 while (1)
566 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
567 return retval;
569 #ifdef _DEBUG_GDB_IO_
570 LOG_DEBUG("character: '%c'", character);
571 #endif
573 switch (character)
575 case '$':
576 break;
577 case '+':
578 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
579 * incase anyone tries to debug why they receive this warning every time */
580 LOG_WARNING("acknowledgment received, but no packet pending");
581 break;
582 case '-':
583 LOG_WARNING("negative acknowledgment, but no packet pending");
584 break;
585 case 0x3:
586 gdb_con->ctrl_c = 1;
587 *len = 0;
588 return ERROR_OK;
589 default:
590 LOG_WARNING("ignoring character 0x%x", character);
591 break;
593 } while (character != '$');
597 int checksum_ok = 0;
598 /* explicit code expansion here to get faster inlined code in -O3 by not
599 * calculating checksum
601 if (gdb_con->noack_mode)
603 if ((retval = fetch_packet(connection, &checksum_ok, 1, len, buffer)) != ERROR_OK)
604 return retval;
605 } else
607 if ((retval = fetch_packet(connection, &checksum_ok, 0, len, buffer)) != ERROR_OK)
608 return retval;
611 if (gdb_con->noack_mode)
613 /* checksum is not checked in noack mode */
614 break;
616 if (checksum_ok)
618 if ((retval = gdb_write(connection, "+", 1)) != ERROR_OK)
620 return retval;
622 break;
625 if (gdb_con->closed)
626 return ERROR_SERVER_REMOTE_CLOSED;
628 return ERROR_OK;
631 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
633 gdb_connection_t *gdb_con = connection->priv;
634 gdb_con->busy = 1;
635 int retval = gdb_get_packet_inner(connection, buffer, len);
636 gdb_con->busy = 0;
637 return retval;
640 int gdb_output_con(connection_t *connection, const char* line)
642 char *hex_buffer;
643 int i, bin_size;
645 bin_size = strlen(line);
647 hex_buffer = malloc(bin_size*2 + 2);
648 if (hex_buffer == NULL)
649 return ERROR_GDB_BUFFER_TOO_SMALL;
651 hex_buffer[0] = 'O';
652 for (i = 0; i<bin_size; i++)
653 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
654 hex_buffer[bin_size*2+1] = 0;
656 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
658 free(hex_buffer);
659 return retval;
662 int gdb_output(struct command_context_s *context, const char* line)
664 /* this will be dumped to the log and also sent as an O packet if possible */
665 LOG_USER_N("%s", line);
666 return ERROR_OK;
670 static void gdb_frontend_halted(struct target_s *target, connection_t *connection)
672 gdb_connection_t *gdb_connection = connection->priv;
674 /* In the GDB protocol when we are stepping or coninuing execution,
675 * we have a lingering reply. Upon receiving a halted event
676 * when we have that lingering packet, we reply to the original
677 * step or continue packet.
679 * Executing monitor commands can bring the target in and
680 * out of the running state so we'll see lots of TARGET_EVENT_XXX
681 * that are to be ignored.
683 if (gdb_connection->frontend_state == TARGET_RUNNING)
685 char sig_reply[4];
686 int signal;
688 /* stop forwarding log packets! */
689 log_remove_callback(gdb_log_callback, connection);
691 if (gdb_connection->ctrl_c)
693 signal = 0x2;
694 gdb_connection->ctrl_c = 0;
696 else
698 signal = gdb_last_signal(target);
701 snprintf(sig_reply, 4, "T%2.2x", signal);
702 gdb_put_packet(connection, sig_reply, 3);
703 gdb_connection->frontend_state = TARGET_HALTED;
707 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
709 int retval;
710 connection_t *connection = priv;
712 target_handle_event( target, event );
713 switch (event)
715 case TARGET_EVENT_EARLY_HALTED:
716 gdb_frontend_halted(target, connection);
717 break;
718 case TARGET_EVENT_HALTED:
719 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
720 break;
721 case TARGET_EVENT_GDB_FLASH_ERASE_START:
722 target_handle_event( target, TARGET_EVENT_OLD_gdb_program_config );
723 if ((retval = jtag_execute_queue()) != ERROR_OK)
725 return retval;
727 break;
728 default:
729 break;
732 return ERROR_OK;
735 int gdb_new_connection(connection_t *connection)
737 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
738 gdb_service_t *gdb_service = connection->service->priv;
739 int retval;
740 int initial_ack;
742 connection->priv = gdb_connection;
744 /* initialize gdb connection information */
745 gdb_connection->buf_p = gdb_connection->buffer;
746 gdb_connection->buf_cnt = 0;
747 gdb_connection->ctrl_c = 0;
748 gdb_connection->frontend_state = TARGET_HALTED;
749 gdb_connection->vflash_image = NULL;
750 gdb_connection->closed = 0;
751 gdb_connection->busy = 0;
752 gdb_connection->noack_mode = 0;
754 /* send ACK to GDB for debug request */
755 gdb_write(connection, "+", 1);
757 /* output goes through gdb connection */
758 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
760 /* we must remove all breakpoints registered to the target as a previous
761 * GDB session could leave dangling breakpoints if e.g. communication
762 * timed out.
764 breakpoint_clear_target(gdb_service->target);
765 watchpoint_clear_target(gdb_service->target);
767 /* register callback to be informed about target events */
768 target_register_event_callback(gdb_target_callback_event_handler, connection);
770 /* a gdb session just attached, try to put the target in halt mode.
772 * DANGER!!!!
774 * If the halt fails(e.g. target needs a reset, JTAG communication not
775 * working, etc.), then the GDB connect will succeed as
776 * the get_gdb_reg_list() will lie and return a register list with
777 * dummy values.
779 * This allows GDB monitor commands to be run from a GDB init script to
780 * initialize the target
782 * Also, since the halt() is asynchronous target connect will be
783 * instantaneous and thus avoiding annoying timeout problems during
784 * connect.
786 target_halt(gdb_service->target);
787 /* FIX!!!! could extended-remote work better here?
789 * wait a tiny bit for halted state or we just continue. The
790 * GDB register packet will then contain garbage
792 target_wait_state(gdb_service->target, TARGET_HALTED, 500);
794 /* remove the initial ACK from the incoming buffer */
795 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
796 return retval;
798 /* FIX!!!??? would we actually ever receive a + here???
799 * Not observed.
801 if (initial_ack != '+')
802 gdb_putback_char(connection, initial_ack);
803 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH );
805 gdb_actual_connections++;
807 return ERROR_OK;
810 int gdb_connection_closed(connection_t *connection)
812 gdb_service_t *gdb_service = connection->service->priv;
813 gdb_connection_t *gdb_connection = connection->priv;
815 gdb_actual_connections--;
817 /* see if an image built with vFlash commands is left */
818 if (gdb_connection->vflash_image)
820 image_close(gdb_connection->vflash_image);
821 free(gdb_connection->vflash_image);
822 gdb_connection->vflash_image = NULL;
825 /* if this connection registered a debug-message receiver delete it */
826 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
828 if (connection->priv)
830 free(connection->priv);
831 connection->priv = NULL;
833 else
835 LOG_ERROR("BUG: connection->priv == NULL");
838 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
839 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
840 log_remove_callback(gdb_log_callback, connection);
842 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH );
844 return ERROR_OK;
847 void gdb_send_error(connection_t *connection, uint8_t the_error)
849 char err[4];
850 snprintf(err, 4, "E%2.2X", the_error );
851 gdb_put_packet(connection, err, 3);
854 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
856 char sig_reply[4];
857 int signal;
859 signal = gdb_last_signal(target);
861 snprintf(sig_reply, 4, "S%2.2x", signal);
862 gdb_put_packet(connection, sig_reply, 3);
864 return ERROR_OK;
867 static int gdb_reg_pos(target_t *target, int pos, int len)
869 if (target->endianness == TARGET_LITTLE_ENDIAN)
870 return pos;
871 else
872 return len - 1 - pos;
875 /* Convert register to string of bytes. NB! The # of bits in the
876 * register might be non-divisible by 8(a byte), in which
877 * case an entire byte is shown.
879 * NB! the format on the wire is the target endianess
881 * The format of reg->value is little endian
884 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
886 int i;
888 uint8_t *buf;
889 int buf_len;
890 buf = reg->value;
891 buf_len = CEIL(reg->size, 8);
893 for (i = 0; i < buf_len; i++)
895 int j = gdb_reg_pos(target, i, buf_len);
896 tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
897 tstr[i*2+1] = DIGITS[buf[j]&0xf];
901 static int hextoint(char c)
903 if (c>='0'&&c<='9')
905 return c-'0';
907 c = toupper(c);
908 if (c>='A'&&c<='F')
910 return c-'A'+10;
912 LOG_ERROR("BUG: invalid register value %08x", c);
913 return 0;
916 /* copy over in register buffer */
917 void gdb_target_to_reg(target_t *target, char *tstr, int str_len, uint8_t *bin)
919 if (str_len % 2)
921 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
922 exit(-1);
925 int i;
926 for (i = 0; i < str_len; i += 2)
928 uint8_t t = hextoint(tstr[i]) << 4;
929 t |= hextoint(tstr[i+1]);
931 int j = gdb_reg_pos(target, i/2, str_len/2);
932 bin[j] = t;
936 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
938 reg_t **reg_list;
939 int reg_list_size;
940 int retval;
941 int reg_packet_size = 0;
942 char *reg_packet;
943 char *reg_packet_p;
944 int i;
946 #ifdef _DEBUG_GDB_IO_
947 LOG_DEBUG("-");
948 #endif
950 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
952 return gdb_error(connection, retval);
955 for (i = 0; i < reg_list_size; i++)
957 reg_packet_size += reg_list[i]->size;
960 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
961 reg_packet_p = reg_packet;
963 for (i = 0; i < reg_list_size; i++)
965 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
966 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
969 #ifdef _DEBUG_GDB_IO_
971 char *reg_packet_p;
972 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
973 LOG_DEBUG("reg_packet: %s", reg_packet_p);
974 free(reg_packet_p);
976 #endif
978 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
979 free(reg_packet);
981 free(reg_list);
983 return ERROR_OK;
986 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
988 int i;
989 reg_t **reg_list;
990 int reg_list_size;
991 int retval;
992 char *packet_p;
994 #ifdef _DEBUG_GDB_IO_
995 LOG_DEBUG("-");
996 #endif
998 /* skip command character */
999 packet++;
1000 packet_size--;
1002 if (packet_size % 2)
1004 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1005 return ERROR_SERVER_REMOTE_CLOSED;
1008 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1010 return gdb_error(connection, retval);
1013 packet_p = packet;
1014 for (i = 0; i < reg_list_size; i++)
1016 uint8_t *bin_buf;
1017 int chars = (CEIL(reg_list[i]->size, 8) * 2);
1019 if (packet_p + chars > packet + packet_size)
1021 LOG_ERROR("BUG: register packet is too small for registers");
1024 reg_arch_type_t *arch_type;
1025 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
1026 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1028 /* get register arch_type, and call set method */
1029 arch_type = register_get_arch_type(reg_list[i]->arch_type);
1031 arch_type->set(reg_list[i], bin_buf);
1033 /* advance packet pointer */
1034 packet_p += chars;
1037 free(bin_buf);
1040 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
1041 free(reg_list);
1043 gdb_put_packet(connection, "OK", 2);
1045 return ERROR_OK;
1048 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1050 char *reg_packet;
1051 int reg_num = strtoul(packet + 1, NULL, 16);
1052 reg_t **reg_list;
1053 int reg_list_size;
1054 int retval;
1056 #ifdef _DEBUG_GDB_IO_
1057 LOG_DEBUG("-");
1058 #endif
1060 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1062 return gdb_error(connection, retval);
1065 if (reg_list_size <= reg_num)
1067 LOG_ERROR("gdb requested a non-existing register");
1068 exit(-1);
1071 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1073 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1075 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
1077 free(reg_list);
1078 free(reg_packet);
1080 return ERROR_OK;
1083 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1085 char *separator;
1086 uint8_t *bin_buf;
1087 int reg_num = strtoul(packet + 1, &separator, 16);
1088 reg_t **reg_list;
1089 int reg_list_size;
1090 int retval;
1091 reg_arch_type_t *arch_type;
1093 LOG_DEBUG("-");
1095 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1097 return gdb_error(connection, retval);
1100 if (reg_list_size < reg_num)
1102 LOG_ERROR("gdb requested a non-existing register");
1103 return ERROR_SERVER_REMOTE_CLOSED;
1106 if (*separator != '=')
1108 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1109 return ERROR_SERVER_REMOTE_CLOSED;
1112 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1113 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1114 int chars = (CEIL(reg_list[reg_num]->size, 8) * 2);
1116 /* fix!!! add some sanity checks on packet size here */
1118 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1120 /* get register arch_type, and call set method */
1121 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1122 arch_type->set(reg_list[reg_num], bin_buf);
1124 gdb_put_packet(connection, "OK", 2);
1126 free(bin_buf);
1127 free(reg_list);
1129 return ERROR_OK;
1132 int gdb_error(connection_t *connection, int retval)
1134 switch (retval)
1136 case ERROR_TARGET_DATA_ABORT:
1137 gdb_send_error(connection, EIO);
1138 break;
1139 case ERROR_TARGET_TRANSLATION_FAULT:
1140 gdb_send_error(connection, EFAULT);
1141 break;
1142 case ERROR_TARGET_UNALIGNED_ACCESS:
1143 gdb_send_error(connection, EFAULT);
1144 break;
1145 case ERROR_TARGET_NOT_HALTED:
1146 gdb_send_error(connection, EFAULT);
1147 break;
1148 default:
1149 /* This could be that the target reset itself. */
1150 LOG_ERROR("unexpected error %i", retval);
1151 gdb_send_error(connection, EFAULT);
1152 break;
1155 return ERROR_OK;
1158 /* We don't have to worry about the default 2 second timeout for GDB packets,
1159 * because GDB breaks up large memory reads into smaller reads.
1161 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1163 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1165 char *separator;
1166 uint32_t addr = 0;
1167 uint32_t len = 0;
1169 uint8_t *buffer;
1170 char *hex_buffer;
1172 int retval = ERROR_OK;
1174 /* skip command character */
1175 packet++;
1177 addr = strtoul(packet, &separator, 16);
1179 if (*separator != ',')
1181 LOG_ERROR("incomplete read memory packet received, dropping connection");
1182 return ERROR_SERVER_REMOTE_CLOSED;
1185 len = strtoul(separator+1, NULL, 16);
1187 buffer = malloc(len);
1189 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1191 retval = target_read_buffer(target, addr, len, buffer);
1193 if ((retval != ERROR_OK)&&!gdb_report_data_abort)
1195 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1196 * At some point this might be fixed in GDB, in which case this code can be removed.
1198 * OpenOCD developers are acutely aware of this problem, but there is nothing
1199 * gained by involving the user in this problem that hopefully will get resolved
1200 * eventually
1202 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1204 * For now, the default is to fix up things to make current GDB versions work.
1205 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1207 memset(buffer, 0, len);
1208 retval = ERROR_OK;
1211 if (retval == ERROR_OK)
1213 hex_buffer = malloc(len * 2 + 1);
1215 uint32_t i;
1216 for (i = 0; i < len; i++)
1218 uint8_t t = buffer[i];
1219 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1220 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1223 gdb_put_packet(connection, hex_buffer, len * 2);
1225 free(hex_buffer);
1227 else
1229 retval = gdb_error(connection, retval);
1232 free(buffer);
1234 return retval;
1237 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1239 char *separator;
1240 uint32_t addr = 0;
1241 uint32_t len = 0;
1243 uint8_t *buffer;
1245 uint32_t i;
1246 int retval;
1248 /* skip command character */
1249 packet++;
1251 addr = strtoul(packet, &separator, 16);
1253 if (*separator != ',')
1255 LOG_ERROR("incomplete write memory packet received, dropping connection");
1256 return ERROR_SERVER_REMOTE_CLOSED;
1259 len = strtoul(separator+1, &separator, 16);
1261 if (*(separator++) != ':')
1263 LOG_ERROR("incomplete write memory packet received, dropping connection");
1264 return ERROR_SERVER_REMOTE_CLOSED;
1267 buffer = malloc(len);
1269 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1271 for (i = 0; i<len; i++)
1273 uint32_t tmp;
1274 sscanf(separator + 2*i, "%2" SCNx32 , &tmp);
1275 buffer[i] = tmp;
1278 retval = target_write_buffer(target, addr, len, buffer);
1280 if (retval == ERROR_OK)
1282 gdb_put_packet(connection, "OK", 2);
1284 else
1286 retval = gdb_error(connection, retval);
1289 free(buffer);
1291 return retval;
1294 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1296 char *separator;
1297 uint32_t addr = 0;
1298 uint32_t len = 0;
1300 int retval;
1302 /* skip command character */
1303 packet++;
1305 addr = strtoul(packet, &separator, 16);
1307 if (*separator != ',')
1309 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1310 return ERROR_SERVER_REMOTE_CLOSED;
1313 len = strtoul(separator+1, &separator, 16);
1315 if (*(separator++) != ':')
1317 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1318 return ERROR_SERVER_REMOTE_CLOSED;
1321 retval = ERROR_OK;
1322 if (len)
1324 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1326 retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
1329 if (retval == ERROR_OK)
1331 gdb_put_packet(connection, "OK", 2);
1333 else
1335 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1336 return retval;
1339 return ERROR_OK;
1342 int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1344 int current = 0;
1345 uint32_t address = 0x0;
1346 int retval = ERROR_OK;
1348 LOG_DEBUG("-");
1350 if (packet_size > 1)
1352 packet[packet_size] = 0;
1353 address = strtoul(packet + 1, NULL, 16);
1355 else
1357 current = 1;
1360 if (packet[0] == 'c')
1362 LOG_DEBUG("continue");
1363 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1364 retval = target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1366 else if (packet[0] == 's')
1368 LOG_DEBUG("step");
1369 /* step at current or address, don't handle breakpoints */
1370 retval = target_step(target, current, address, 0);
1372 return retval;
1375 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1377 int type;
1378 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1379 enum watchpoint_rw wp_type;
1380 uint32_t address;
1381 uint32_t size;
1382 char *separator;
1383 int retval;
1385 LOG_DEBUG("-");
1387 type = strtoul(packet + 1, &separator, 16);
1389 if (type == 0) /* memory breakpoint */
1390 bp_type = BKPT_SOFT;
1391 else if (type == 1) /* hardware breakpoint */
1392 bp_type = BKPT_HARD;
1393 else if (type == 2) /* write watchpoint */
1394 wp_type = WPT_WRITE;
1395 else if (type == 3) /* read watchpoint */
1396 wp_type = WPT_READ;
1397 else if (type == 4) /* access watchpoint */
1398 wp_type = WPT_ACCESS;
1400 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT)||(bp_type == BKPT_HARD)))
1402 bp_type = gdb_breakpoint_override_type;
1405 if (*separator != ',')
1407 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1408 return ERROR_SERVER_REMOTE_CLOSED;
1411 address = strtoul(separator+1, &separator, 16);
1413 if (*separator != ',')
1415 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1416 return ERROR_SERVER_REMOTE_CLOSED;
1419 size = strtoul(separator+1, &separator, 16);
1421 switch (type)
1423 case 0:
1424 case 1:
1425 if (packet[0] == 'Z')
1427 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1429 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1430 return retval;
1432 else
1434 gdb_put_packet(connection, "OK", 2);
1437 else
1439 breakpoint_remove(target, address);
1440 gdb_put_packet(connection, "OK", 2);
1442 break;
1443 case 2:
1444 case 3:
1445 case 4:
1447 if (packet[0] == 'Z')
1449 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1451 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1452 return retval;
1454 else
1456 gdb_put_packet(connection, "OK", 2);
1459 else
1461 watchpoint_remove(target, address);
1462 gdb_put_packet(connection, "OK", 2);
1464 break;
1466 default:
1467 break;
1470 return ERROR_OK;
1473 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1474 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1476 if (*retval != ERROR_OK)
1478 return;
1480 int first = 1;
1482 for (;;)
1484 if ((*xml == NULL) || (!first))
1486 /* start by 0 to exercise all the code paths.
1487 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1489 *size = *size * 2 + 2;
1490 char *t = *xml;
1491 *xml = realloc(*xml, *size);
1492 if (*xml == NULL)
1494 if (t)
1495 free(t);
1496 *retval = ERROR_SERVER_REMOTE_CLOSED;
1497 return;
1501 va_list ap;
1502 int ret;
1503 va_start(ap, fmt);
1504 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1505 va_end(ap);
1506 if ((ret > 0) && ((ret + 1) < *size - *pos))
1508 *pos += ret;
1509 return;
1511 /* there was just enough or not enough space, allocate more. */
1512 first = 0;
1516 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1518 char *separator;
1520 /* Extract and NUL-terminate the annex. */
1521 *annex = buf;
1522 while (*buf && *buf != ':')
1523 buf++;
1524 if (*buf == '\0')
1525 return -1;
1526 *buf++ = 0;
1528 /* After the read marker and annex, qXfer looks like a
1529 * traditional 'm' packet. */
1531 *ofs = strtoul(buf, &separator, 16);
1533 if (*separator != ',')
1534 return -1;
1536 *len = strtoul(separator+1, NULL, 16);
1538 return 0;
1541 int gdb_calc_blocksize(flash_bank_t *bank)
1543 uint32_t i;
1544 uint32_t block_size = 0xffffffff;
1546 /* loop through all sectors and return smallest sector size */
1548 for (i = 0; i < (uint32_t)bank->num_sectors; i++)
1550 if (bank->sectors[i].size < block_size)
1551 block_size = bank->sectors[i].size;
1554 return block_size;
1557 static int compare_bank (const void * a, const void * b)
1559 flash_bank_t *b1, *b2;
1560 b1=*((flash_bank_t **)a);
1561 b2=*((flash_bank_t **)b);
1563 if (b1->base == b2->base)
1565 return 0;
1566 } else if (b1->base>b2->base)
1568 return 1;
1569 } else
1571 return -1;
1575 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1577 command_context_t *cmd_ctx = connection->cmd_ctx;
1578 gdb_connection_t *gdb_connection = connection->priv;
1580 if (strstr(packet, "qRcmd,"))
1582 if (packet_size > 6)
1584 char *cmd;
1585 int i;
1586 cmd = malloc((packet_size - 6)/2 + 1);
1587 for (i = 0; i < (packet_size - 6)/2; i++)
1589 uint32_t tmp;
1590 sscanf(packet + 6 + 2*i, "%2" SCNx32 , &tmp);
1591 cmd[i] = tmp;
1593 cmd[(packet_size - 6)/2] = 0x0;
1595 /* We want to print all debug output to GDB connection */
1596 log_add_callback(gdb_log_callback, connection);
1597 target_call_timer_callbacks_now();
1598 command_run_line(cmd_ctx, cmd);
1599 target_call_timer_callbacks_now();
1600 log_remove_callback(gdb_log_callback, connection);
1601 free(cmd);
1603 gdb_put_packet(connection, "OK", 2);
1604 return ERROR_OK;
1606 else if (strstr(packet, "qCRC:"))
1608 if (packet_size > 5)
1610 int retval;
1611 char gdb_reply[10];
1612 char *separator;
1613 uint32_t checksum;
1614 uint32_t addr = 0;
1615 uint32_t len = 0;
1617 /* skip command character */
1618 packet += 5;
1620 addr = strtoul(packet, &separator, 16);
1622 if (*separator != ',')
1624 LOG_ERROR("incomplete read memory packet received, dropping connection");
1625 return ERROR_SERVER_REMOTE_CLOSED;
1628 len = strtoul(separator + 1, NULL, 16);
1630 retval = target_checksum_memory(target, addr, len, &checksum);
1632 if (retval == ERROR_OK)
1634 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
1635 gdb_put_packet(connection, gdb_reply, 9);
1637 else
1639 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1640 return retval;
1643 return ERROR_OK;
1646 else if (strstr(packet, "qSupported"))
1648 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1649 * disable qXfer:features:read for the moment */
1650 int retval = ERROR_OK;
1651 char *buffer = NULL;
1652 int pos = 0;
1653 int size = 0;
1655 xml_printf(&retval, &buffer, &pos, &size,
1656 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1657 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1) && (flash_get_bank_count()>0)) ? '+' : '-');
1659 if (retval != ERROR_OK)
1661 gdb_send_error(connection, 01);
1662 return ERROR_OK;
1665 gdb_put_packet(connection, buffer, strlen(buffer));
1666 free(buffer);
1668 return ERROR_OK;
1670 else if (strstr(packet, "qXfer:memory-map:read::") && (flash_get_bank_count()>0))
1672 /* We get away with only specifying flash here. Regions that are not
1673 * specified are treated as if we provided no memory map(if not we
1674 * could detect the holes and mark them as RAM).
1675 * Normally we only execute this code once, but no big deal if we
1676 * have to regenerate it a couple of times. */
1678 flash_bank_t *p;
1679 char *xml = NULL;
1680 int size = 0;
1681 int pos = 0;
1682 int retval = ERROR_OK;
1684 int offset;
1685 int length;
1686 char *separator;
1687 int blocksize;
1689 /* skip command character */
1690 packet += 23;
1692 offset = strtoul(packet, &separator, 16);
1693 length = strtoul(separator + 1, &separator, 16);
1695 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1698 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1699 read/write) by default for GDB.
1700 GDB does not have a concept of non-cacheable read/write memory.
1702 flash_bank_t **banks = malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
1703 int i;
1705 for (i = 0; i<flash_get_bank_count(); i++)
1707 p = get_flash_bank_by_num(i);
1708 if (p == NULL)
1710 free(banks);
1711 retval = ERROR_FAIL;
1712 gdb_send_error(connection, retval);
1713 return retval;
1715 banks[i]=p;
1718 qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
1720 uint32_t ram_start = 0;
1721 for (i = 0; i<flash_get_bank_count(); i++)
1723 p = banks[i];
1725 if (ram_start<p->base)
1727 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1728 ram_start, p->base-ram_start);
1731 /* if device has uneven sector sizes, eg. str7, lpc
1732 * we pass the smallest sector size to gdb memory map */
1733 blocksize = gdb_calc_blocksize(p);
1735 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1736 "<property name=\"blocksize\">0x%x</property>\n" \
1737 "</memory>\n", \
1738 p->base, p->size, blocksize);
1739 ram_start = p->base+p->size;
1741 if (ram_start != 0)
1743 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1744 ram_start, 0-ram_start);
1745 } else
1747 /* a flash chip could be at the very end of the 32 bit address space, in which case
1748 ram_start will be precisely 0 */
1751 free(banks);
1752 banks = NULL;
1754 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1756 if (retval != ERROR_OK)
1758 gdb_send_error(connection, retval);
1759 return retval;
1762 if (offset + length > pos)
1764 length = pos - offset;
1767 char *t = malloc(length + 1);
1768 t[0] = 'l';
1769 memcpy(t + 1, xml + offset, length);
1770 gdb_put_packet(connection, t, length + 1);
1772 free(t);
1773 free(xml);
1774 return ERROR_OK;
1776 else if (strstr(packet, "qXfer:features:read:"))
1778 char *xml = NULL;
1779 int size = 0;
1780 int pos = 0;
1781 int retval = ERROR_OK;
1783 int offset;
1784 unsigned int length;
1785 char *annex;
1787 /* skip command character */
1788 packet += 20;
1790 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1792 gdb_send_error(connection, 01);
1793 return ERROR_OK;
1796 if (strcmp(annex, "target.xml") != 0)
1798 gdb_send_error(connection, 01);
1799 return ERROR_OK;
1802 xml_printf(&retval, &xml, &pos, &size, \
1803 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1805 if (retval != ERROR_OK)
1807 gdb_send_error(connection, retval);
1808 return retval;
1811 gdb_put_packet(connection, xml, strlen(xml));
1813 free(xml);
1814 return ERROR_OK;
1816 else if (strstr(packet, "QStartNoAckMode"))
1818 gdb_connection->noack_mode = 1;
1819 gdb_put_packet(connection, "OK", 2);
1820 return ERROR_OK;
1823 gdb_put_packet(connection, "", 0);
1824 return ERROR_OK;
1827 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1829 gdb_connection_t *gdb_connection = connection->priv;
1830 gdb_service_t *gdb_service = connection->service->priv;
1831 int result;
1833 /* if flash programming disabled - send a empty reply */
1835 if (gdb_flash_program == 0)
1837 gdb_put_packet(connection, "", 0);
1838 return ERROR_OK;
1841 if (strstr(packet, "vFlashErase:"))
1843 unsigned long addr;
1844 unsigned long length;
1846 char *parse = packet + 12;
1847 if (*parse == '\0')
1849 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1850 return ERROR_SERVER_REMOTE_CLOSED;
1853 addr = strtoul(parse, &parse, 16);
1855 if (*(parse++) != ',' || *parse == '\0')
1857 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1858 return ERROR_SERVER_REMOTE_CLOSED;
1861 length = strtoul(parse, &parse, 16);
1863 if (*parse != '\0')
1865 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1866 return ERROR_SERVER_REMOTE_CLOSED;
1869 /* assume all sectors need erasing - stops any problems
1870 * when flash_write is called multiple times */
1871 flash_set_dirty();
1873 /* perform any target specific operations before the erase */
1874 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1875 result = flash_erase_address_range(gdb_service->target, addr, length );
1876 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
1878 /* perform erase */
1879 if (result != ERROR_OK)
1881 /* GDB doesn't evaluate the actual error number returned,
1882 * treat a failed erase as an I/O error
1884 gdb_send_error(connection, EIO);
1885 LOG_ERROR("flash_erase returned %i", result);
1887 else
1888 gdb_put_packet(connection, "OK", 2);
1890 return ERROR_OK;
1893 if (strstr(packet, "vFlashWrite:"))
1895 int retval;
1896 unsigned long addr;
1897 unsigned long length;
1898 char *parse = packet + 12;
1900 if (*parse == '\0')
1902 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1903 return ERROR_SERVER_REMOTE_CLOSED;
1905 addr = strtoul(parse, &parse, 16);
1906 if (*(parse++) != ':')
1908 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1909 return ERROR_SERVER_REMOTE_CLOSED;
1911 length = packet_size - (parse - packet);
1913 /* create a new image if there isn't already one */
1914 if (gdb_connection->vflash_image == NULL)
1916 gdb_connection->vflash_image = malloc(sizeof(image_t));
1917 image_open(gdb_connection->vflash_image, "", "build");
1920 /* create new section with content from packet buffer */
1921 if ((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (uint8_t*)parse)) != ERROR_OK)
1923 return retval;
1926 gdb_put_packet(connection, "OK", 2);
1928 return ERROR_OK;
1931 if (!strcmp(packet, "vFlashDone"))
1933 uint32_t written;
1935 /* process the flashing buffer. No need to erase as GDB
1936 * always issues a vFlashErase first. */
1937 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
1938 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1939 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1940 if ( result != ERROR_OK)
1942 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1943 gdb_put_packet(connection, "E.memtype", 9);
1944 else
1945 gdb_send_error(connection, EIO);
1947 else
1949 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
1950 gdb_put_packet(connection, "OK", 2);
1953 image_close(gdb_connection->vflash_image);
1954 free(gdb_connection->vflash_image);
1955 gdb_connection->vflash_image = NULL;
1957 return ERROR_OK;
1960 gdb_put_packet(connection, "", 0);
1961 return ERROR_OK;
1964 int gdb_detach(connection_t *connection, target_t *target)
1967 switch ( detach_mode )
1969 case GDB_DETACH_RESUME:
1970 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1971 target_resume(target, 1, 0, 1, 0);
1972 break;
1974 case GDB_DETACH_RESET:
1975 /* FIX?? make this configurable?? */
1976 target_process_reset(connection->cmd_ctx, RESET_HALT);
1977 break;
1979 case GDB_DETACH_HALT:
1980 target_halt(target);
1981 break;
1983 case GDB_DETACH_NOTHING:
1984 break;
1987 gdb_put_packet(connection, "OK", 2);
1988 return ERROR_OK;
1991 static void gdb_log_callback(void *priv, const char *file, int line,
1992 const char *function, const char *string)
1994 connection_t *connection = priv;
1995 gdb_connection_t *gdb_con = connection->priv;
1997 if (gdb_con->busy)
1999 /* do not reply this using the O packet */
2000 return;
2003 gdb_output_con(connection, string);
2006 /* Do not allocate this on the stack */
2007 char gdb_packet_buffer[GDB_BUFFER_SIZE];
2009 static void gdb_sig_halted(connection_t *connection)
2011 char sig_reply[4];
2012 snprintf(sig_reply, 4, "T%2.2x", 2);
2013 gdb_put_packet(connection, sig_reply, 3);
2017 int gdb_input_inner(connection_t *connection)
2019 gdb_service_t *gdb_service = connection->service->priv;
2020 target_t *target = gdb_service->target;
2021 char *packet = gdb_packet_buffer;
2022 int packet_size;
2023 int retval;
2024 gdb_connection_t *gdb_con = connection->priv;
2025 static int extended_protocol = 0;
2027 /* drain input buffer */
2030 packet_size = GDB_BUFFER_SIZE-1;
2031 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
2033 return retval;
2036 /* terminate with zero */
2037 packet[packet_size] = 0;
2039 if ( LOG_LEVEL_IS( LOG_LVL_DEBUG ) ){
2040 if ( packet[0] == 'X' ){
2041 // binary packets spew junk into the debug log stream
2042 char buf[ 50 ];
2043 int x;
2044 for ( x = 0 ; (x < 49) && (packet[x] != ':') ; x++ ){
2045 buf[x] = packet[x];
2047 buf[x] = 0;
2048 LOG_DEBUG("received packet: '%s:<binary-data>'", buf );
2049 } else {
2050 LOG_DEBUG("received packet: '%s'", packet );
2054 if (packet_size > 0)
2056 retval = ERROR_OK;
2057 switch (packet[0])
2059 case 'H':
2060 /* Hct... -- set thread
2061 * we don't have threads, send empty reply */
2062 gdb_put_packet(connection, NULL, 0);
2063 break;
2064 case 'q':
2065 case 'Q':
2066 retval = gdb_query_packet(connection, target, packet, packet_size);
2067 break;
2068 case 'g':
2069 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
2070 break;
2071 case 'G':
2072 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
2073 break;
2074 case 'p':
2075 retval = gdb_get_register_packet(connection, target, packet, packet_size);
2076 break;
2077 case 'P':
2078 retval = gdb_set_register_packet(connection, target, packet, packet_size);
2079 break;
2080 case 'm':
2081 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
2082 break;
2083 case 'M':
2084 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
2085 break;
2086 case 'z':
2087 case 'Z':
2088 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
2089 break;
2090 case '?':
2091 gdb_last_signal_packet(connection, target, packet, packet_size);
2092 break;
2093 case 'c':
2094 case 's':
2096 if (target->state != TARGET_HALTED)
2098 /* If the target isn't in the halted state, then we can't
2099 * step/continue. This might be early setup, etc.
2101 gdb_sig_halted(connection);
2102 } else
2104 /* We're running/stepping, in which case we can
2105 * forward log output until the target is halted
2107 gdb_connection_t *gdb_con = connection->priv;
2108 gdb_con->frontend_state = TARGET_RUNNING;
2109 log_add_callback(gdb_log_callback, connection);
2110 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2111 int retval = gdb_step_continue_packet(connection, target, packet, packet_size);
2112 if (retval != ERROR_OK)
2114 /* we'll never receive a halted condition... issue a false one.. */
2115 gdb_frontend_halted(target, connection);
2119 break;
2120 case 'v':
2121 retval = gdb_v_packet(connection, target, packet, packet_size);
2122 break;
2123 case 'D':
2124 retval = gdb_detach(connection, target);
2125 extended_protocol = 0;
2126 break;
2127 case 'X':
2128 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2129 return retval;
2130 break;
2131 case 'k':
2132 if (extended_protocol != 0)
2133 break;
2134 gdb_put_packet(connection, "OK", 2);
2135 return ERROR_SERVER_REMOTE_CLOSED;
2136 case '!':
2137 /* handle extended remote protocol */
2138 extended_protocol = 1;
2139 gdb_put_packet(connection, "OK", 2);
2140 break;
2141 case 'R':
2142 /* handle extended restart packet */
2143 breakpoint_clear_target(gdb_service->target);
2144 watchpoint_clear_target(gdb_service->target);
2145 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
2146 break;
2147 default:
2148 /* ignore unkown packets */
2149 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2150 gdb_put_packet(connection, NULL, 0);
2151 break;
2154 /* if a packet handler returned an error, exit input loop */
2155 if (retval != ERROR_OK)
2156 return retval;
2159 if (gdb_con->ctrl_c)
2161 if (target->state == TARGET_RUNNING)
2163 target_halt(target);
2164 gdb_con->ctrl_c = 0;
2168 } while (gdb_con->buf_cnt > 0);
2170 return ERROR_OK;
2173 int gdb_input(connection_t *connection)
2175 int retval = gdb_input_inner(connection);
2176 gdb_connection_t *gdb_con = connection->priv;
2177 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2178 return retval;
2180 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2181 if (gdb_con->closed)
2182 return ERROR_SERVER_REMOTE_CLOSED;
2184 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2185 return ERROR_OK;
2188 int gdb_init(void)
2190 gdb_service_t *gdb_service;
2191 target_t *target = all_targets;
2193 if (!target)
2195 LOG_WARNING("no gdb ports allocated as no target has been specified");
2196 return ERROR_OK;
2199 if (gdb_port == 0 && server_use_pipes == 0)
2201 LOG_INFO("gdb port disabled");
2202 return ERROR_OK;
2205 if (server_use_pipes)
2207 /* only a single gdb connection when using a pipe */
2209 gdb_service = malloc(sizeof(gdb_service_t));
2210 gdb_service->target = target;
2212 add_service("gdb", CONNECTION_PIPE, 0, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
2214 LOG_DEBUG("gdb service for target %s using pipes",
2215 target_get_name(target));
2217 else
2219 while (target)
2221 gdb_service = malloc(sizeof(gdb_service_t));
2222 gdb_service->target = target;
2224 add_service("gdb", CONNECTION_TCP, gdb_port + target->target_number, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
2226 LOG_DEBUG("gdb service for target %s at port %i",
2227 target_get_name(target),
2228 gdb_port + target->target_number);
2229 target = target->next;
2233 return ERROR_OK;
2236 /* daemon configuration command gdb_port */
2237 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2239 if (argc == 0)
2241 command_print(cmd_ctx, "%d", gdb_port);
2242 return ERROR_OK;
2245 /* only if the port wasn't overwritten by cmdline */
2246 if (gdb_port == 0)
2247 gdb_port = strtoul(args[0], NULL, 0);
2249 return ERROR_OK;
2252 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2254 if (argc == 1)
2256 if (strcmp(args[0], "resume") == 0)
2258 detach_mode = GDB_DETACH_RESUME;
2259 return ERROR_OK;
2261 else if (strcmp(args[0], "reset") == 0)
2263 detach_mode = GDB_DETACH_RESET;
2264 return ERROR_OK;
2266 else if (strcmp(args[0], "halt") == 0)
2268 detach_mode = GDB_DETACH_HALT;
2269 return ERROR_OK;
2271 else if (strcmp(args[0], "nothing") == 0)
2273 detach_mode = GDB_DETACH_NOTHING;
2274 return ERROR_OK;
2276 else
2277 LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2280 return ERROR_COMMAND_SYNTAX_ERROR;
2283 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2285 if (argc == 1)
2287 if (strcmp(args[0], "enable") == 0)
2289 gdb_use_memory_map = 1;
2290 return ERROR_OK;
2292 else if (strcmp(args[0], "disable") == 0)
2294 gdb_use_memory_map = 0;
2295 return ERROR_OK;
2297 else
2298 LOG_WARNING("invalid gdb_memory_map configuration directive %s", args[0]);
2301 return ERROR_COMMAND_SYNTAX_ERROR;
2304 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2306 if (argc == 1)
2308 if (strcmp(args[0], "enable") == 0)
2310 gdb_flash_program = 1;
2311 return ERROR_OK;
2313 else if (strcmp(args[0], "disable") == 0)
2315 gdb_flash_program = 0;
2316 return ERROR_OK;
2318 else
2319 LOG_WARNING("invalid gdb_flash_program configuration directive: %s", args[0]);
2322 return ERROR_COMMAND_SYNTAX_ERROR;
2325 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2327 if (argc == 1)
2329 if (strcmp(args[0], "enable") == 0)
2331 gdb_report_data_abort = 1;
2332 return ERROR_OK;
2334 else if (strcmp(args[0], "disable") == 0)
2336 gdb_report_data_abort = 0;
2337 return ERROR_OK;
2339 else
2340 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2343 return ERROR_COMMAND_SYNTAX_ERROR;
2346 /* gdb_breakpoint_override */
2347 int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2349 if (argc == 0)
2352 } else if (argc == 1)
2354 gdb_breakpoint_override = 1;
2355 if (strcmp(args[0], "hard") == 0)
2357 gdb_breakpoint_override_type = BKPT_HARD;
2358 } else if (strcmp(args[0], "soft") == 0)
2360 gdb_breakpoint_override_type = BKPT_SOFT;
2361 } else if (strcmp(args[0], "disable") == 0)
2363 gdb_breakpoint_override = 0;
2365 } else
2367 return ERROR_COMMAND_SYNTAX_ERROR;
2369 if (gdb_breakpoint_override)
2371 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type == BKPT_HARD)?"hard":"soft");
2372 } else
2374 LOG_USER("breakpoint type is not overriden");
2377 return ERROR_OK;
2380 int gdb_register_commands(command_context_t *command_context)
2382 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2383 COMMAND_ANY, "daemon configuration command gdb_port");
2384 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2385 COMMAND_CONFIG, "resume/reset/halt/nothing - "
2386 "specify behavior when GDB detaches from the target");
2387 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2388 COMMAND_CONFIG, "enable or disable memory map");
2389 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2390 COMMAND_CONFIG, "enable or disable flash program");
2391 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2392 COMMAND_CONFIG, "enable or disable reporting data aborts");
2393 register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
2394 COMMAND_EXEC, "hard/soft/disable - force breakpoint type for gdb 'break' commands.");
2395 return ERROR_OK;