minor cleanup
[openocd.git] / src / server / gdb_server.c
blobea6f6b75aa7ad2242b28298526d01cc18f009996
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 "replacements.h"
32 #include "gdb_server.h"
34 #include "server.h"
35 #include "log.h"
36 #include "binarybuffer.h"
37 #include "jtag.h"
38 #include "breakpoints.h"
39 #include "flash.h"
40 #include "target.h"
41 #include "target_request.h"
42 #include "configuration.h"
44 #include <string.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <stdlib.h>
49 #if 0
50 #define _DEBUG_GDB_IO_
51 #endif
53 static int gdb_breakpoint_override;
54 static enum breakpoint_type gdb_breakpoint_override_type;
56 extern int gdb_error(connection_t *connection, int retval);
57 static unsigned short gdb_port;
58 static const char *DIGITS = "0123456789abcdef";
60 static void gdb_log_callback(void *priv, const char *file, int line,
61 const char *function, const char *string);
63 enum gdb_detach_mode
65 GDB_DETACH_RESUME,
66 GDB_DETACH_RESET,
67 GDB_DETACH_HALT,
68 GDB_DETACH_NOTHING
71 /* target behaviour on gdb detach */
72 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
74 /* set if we are sending a memory map to gdb
75 * via qXfer:memory-map:read packet */
76 /* enabled by default*/
77 int gdb_use_memory_map = 1;
78 /* enabled by default*/
79 int gdb_flash_program = 1;
81 /* if set, data aborts cause an error to be reported in memory read packets
82 * see the code in gdb_read_memory_packet() for further explanations */
83 int gdb_report_data_abort = 0;
85 int gdb_last_signal(target_t *target)
87 switch (target->debug_reason)
89 case DBG_REASON_DBGRQ:
90 return 0x2; /* SIGINT */
91 case DBG_REASON_BREAKPOINT:
92 case DBG_REASON_WATCHPOINT:
93 case DBG_REASON_WPTANDBKPT:
94 return 0x05; /* SIGTRAP */
95 case DBG_REASON_SINGLESTEP:
96 return 0x05; /* SIGTRAP */
97 case DBG_REASON_NOTHALTED:
98 return 0x0; /* no signal... shouldn't happen */
99 default:
100 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
101 return 0x0;
105 int check_pending(connection_t *connection, int timeout_s, int *got_data)
107 /* a non-blocking socket will block if there is 0 bytes available on the socket,
108 * but return with as many bytes as are available immediately
110 struct timeval tv;
111 fd_set read_fds;
112 gdb_connection_t *gdb_con = connection->priv;
113 int t;
114 if (got_data==NULL)
115 got_data=&t;
116 *got_data=0;
118 if (gdb_con->buf_cnt>0)
120 *got_data = 1;
121 return ERROR_OK;
124 FD_ZERO(&read_fds);
125 FD_SET(connection->fd, &read_fds);
127 tv.tv_sec = timeout_s;
128 tv.tv_usec = 0;
129 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
131 /* This can typically be because a "monitor" command took too long
132 * before printing any progress messages
134 if (timeout_s>0)
136 return ERROR_GDB_TIMEOUT;
137 } else
139 return ERROR_OK;
142 *got_data=FD_ISSET(connection->fd, &read_fds)!=0;
143 return ERROR_OK;
146 int gdb_get_char(connection_t *connection, int* next_char)
148 gdb_connection_t *gdb_con = connection->priv;
149 int retval=ERROR_OK;
151 #ifdef _DEBUG_GDB_IO_
152 char *debug_buffer;
153 #endif
155 if (gdb_con->buf_cnt-- > 0)
157 *next_char = *(gdb_con->buf_p++);
158 if (gdb_con->buf_cnt > 0)
159 connection->input_pending = 1;
160 else
161 connection->input_pending = 0;
163 #ifdef _DEBUG_GDB_IO_
164 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
165 #endif
167 return ERROR_OK;
170 for (;;)
172 retval=check_pending(connection, 1, NULL);
173 if (retval!=ERROR_OK)
174 return retval;
175 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
176 if (gdb_con->buf_cnt > 0)
178 break;
180 if (gdb_con->buf_cnt == 0)
182 gdb_con->closed = 1;
183 return ERROR_SERVER_REMOTE_CLOSED;
186 #ifdef _WIN32
187 errno = WSAGetLastError();
189 switch(errno)
191 case WSAEWOULDBLOCK:
192 usleep(1000);
193 break;
194 case WSAECONNABORTED:
195 gdb_con->closed = 1;
196 return ERROR_SERVER_REMOTE_CLOSED;
197 case WSAECONNRESET:
198 gdb_con->closed = 1;
199 return ERROR_SERVER_REMOTE_CLOSED;
200 default:
201 LOG_ERROR("read: %d", errno);
202 exit(-1);
204 #else
205 switch(errno)
207 case EAGAIN:
208 usleep(1000);
209 break;
210 case ECONNABORTED:
211 gdb_con->closed = 1;
212 return ERROR_SERVER_REMOTE_CLOSED;
213 case ECONNRESET:
214 gdb_con->closed = 1;
215 return ERROR_SERVER_REMOTE_CLOSED;
216 default:
217 LOG_ERROR("read: %s", strerror(errno));
218 gdb_con->closed = 1;
219 return ERROR_SERVER_REMOTE_CLOSED;
221 #endif
224 #ifdef _DEBUG_GDB_IO_
225 debug_buffer = malloc(gdb_con->buf_cnt + 1);
226 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
227 debug_buffer[gdb_con->buf_cnt] = 0;
228 LOG_DEBUG("received '%s'", debug_buffer);
229 free(debug_buffer);
230 #endif
232 gdb_con->buf_p = gdb_con->buffer;
233 gdb_con->buf_cnt--;
234 *next_char = *(gdb_con->buf_p++);
235 if (gdb_con->buf_cnt > 0)
236 connection->input_pending = 1;
237 else
238 connection->input_pending = 0;
239 #ifdef _DEBUG_GDB_IO_
240 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
241 #endif
243 return retval;
246 int gdb_putback_char(connection_t *connection, int last_char)
248 gdb_connection_t *gdb_con = connection->priv;
250 if (gdb_con->buf_p > gdb_con->buffer)
252 *(--gdb_con->buf_p) = last_char;
253 gdb_con->buf_cnt++;
255 else
257 LOG_ERROR("BUG: couldn't put character back");
260 return ERROR_OK;
263 /* The only way we can detect that the socket is closed is the first time
264 * we write to it, we will fail. Subsequent write operations will
265 * succeed. Shudder! */
266 int gdb_write(connection_t *connection, void *data, int len)
268 gdb_connection_t *gdb_con = connection->priv;
269 if (gdb_con->closed)
270 return ERROR_SERVER_REMOTE_CLOSED;
272 if (write_socket(connection->fd, data, len) == len)
274 return ERROR_OK;
276 gdb_con->closed = 1;
277 return ERROR_SERVER_REMOTE_CLOSED;
280 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
282 int i;
283 unsigned char my_checksum = 0;
284 #ifdef _DEBUG_GDB_IO_
285 char *debug_buffer;
286 #endif
287 int reply;
288 int retval;
289 gdb_connection_t *gdb_con = connection->priv;
291 for (i = 0; i < len; i++)
292 my_checksum += buffer[i];
294 #ifdef _DEBUG_GDB_IO_
296 * At this point we should have nothing in the input queue from GDB,
297 * however sometimes '-' is sent even though we've already received
298 * an ACK (+) for everything we've sent off.
300 int gotdata;
301 for (;;)
303 if ((retval=check_pending(connection, 0, &gotdata))!=ERROR_OK)
304 return retval;
305 if (!gotdata)
306 break;
307 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
308 return retval;
309 if( reply == '$' ){
310 // fix a problem with some IAR tools
311 gdb_putback_char( connection, reply );
312 LOG_DEBUG("Unexpected start of new packet");
313 break;
316 LOG_WARNING("Discard unexpected char %c", reply);
318 #endif
320 while (1)
322 #ifdef _DEBUG_GDB_IO_
323 debug_buffer = malloc(len + 1);
324 memcpy(debug_buffer, buffer, len);
325 debug_buffer[len] = 0;
326 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
327 free(debug_buffer);
328 #endif
330 char local_buffer[1024];
331 local_buffer[0] = '$';
332 if (len+4 <= sizeof(local_buffer))
334 /* performance gain on smaller packets by only a single call to gdb_write() */
335 memcpy(local_buffer+1, buffer, len++);
336 local_buffer[len++] = '#';
337 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
338 local_buffer[len++] = DIGITS[my_checksum & 0xf];
339 if((retval = gdb_write(connection, local_buffer, len)) != ERROR_OK)
341 return retval;
344 else
346 /* larger packets are transmitted directly from caller supplied buffer
347 by several calls to gdb_write() to avoid dynamic allocation */
348 local_buffer[1] = '#';
349 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
350 local_buffer[3] = DIGITS[my_checksum & 0xf];
351 if((retval = gdb_write(connection, local_buffer, 1)) != ERROR_OK)
353 return retval;
355 if((retval = gdb_write(connection, buffer, len)) != ERROR_OK)
357 return retval;
359 if((retval = gdb_write(connection, local_buffer+1, 3)) != ERROR_OK)
361 return retval;
365 if (gdb_con->noack_mode)
366 break;
368 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
369 return retval;
371 if (reply == '+')
372 break;
373 else if (reply == '-')
375 /* Stop sending output packets for now */
376 log_remove_callback(gdb_log_callback, connection);
377 LOG_WARNING("negative reply, retrying");
379 else if (reply == 0x3)
381 gdb_con->ctrl_c = 1;
382 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
383 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 == '$' ){
393 LOG_ERROR("GDB missing ack(1) - assumed good");
394 gdb_putback_char( connection, reply );
395 return ERROR_OK;
396 } else {
398 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
399 gdb_con->closed=1;
400 return ERROR_SERVER_REMOTE_CLOSED;
403 else if( reply == '$' ){
404 LOG_ERROR("GDB missing ack(2) - assumed good");
405 gdb_putback_char( connection, reply );
406 return ERROR_OK;
408 else
410 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply);
411 gdb_con->closed=1;
412 return ERROR_SERVER_REMOTE_CLOSED;
415 if (gdb_con->closed)
416 return ERROR_SERVER_REMOTE_CLOSED;
418 return ERROR_OK;
421 int gdb_put_packet(connection_t *connection, char *buffer, int len)
423 gdb_connection_t *gdb_con = connection->priv;
424 gdb_con->busy = 1;
425 int retval = gdb_put_packet_inner(connection, buffer, len);
426 gdb_con->busy = 0;
428 /* we sent some data, reset timer for keep alive messages */
429 kept_alive();
431 return retval;
434 static __inline__ int fetch_packet(connection_t *connection, int *checksum_ok, int noack, int *len, char *buffer)
436 unsigned char my_checksum = 0;
437 char checksum[3];
438 int character;
439 int retval;
441 gdb_connection_t *gdb_con = connection->priv;
442 my_checksum = 0;
443 int count = 0;
444 count = 0;
445 for (;;)
447 /* The common case is that we have an entire packet with no escape chars.
448 * We need to leave at least 2 bytes in the buffer to have
449 * gdb_get_char() update various bits and bobs correctly.
451 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
453 /* The compiler will struggle a bit with constant propagation and
454 * aliasing, so we help it by showing that these values do not
455 * change inside the loop
457 int i;
458 char *buf = gdb_con->buf_p;
459 int run = gdb_con->buf_cnt - 2;
460 i = 0;
461 int done = 0;
462 while (i < run)
464 character = *buf++;
465 i++;
466 if (character == '#')
468 /* Danger! character can be '#' when esc is
469 * used so we need an explicit boolean for done here.
471 done = 1;
472 break;
475 if (character == '}')
477 /* data transmitted in binary mode (X packet)
478 * uses 0x7d as escape character */
479 my_checksum += character & 0xff;
480 character = *buf++;
481 i++;
482 my_checksum += character & 0xff;
483 buffer[count++] = (character ^ 0x20) & 0xff;
485 else
487 my_checksum += character & 0xff;
488 buffer[count++] = character & 0xff;
491 gdb_con->buf_p += i;
492 gdb_con->buf_cnt -= i;
493 if (done)
494 break;
496 if (count > *len)
498 LOG_ERROR("packet buffer too small");
499 return ERROR_GDB_BUFFER_TOO_SMALL;
502 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
503 return retval;
505 if (character == '#')
506 break;
508 if (character == '}')
510 /* data transmitted in binary mode (X packet)
511 * uses 0x7d as escape character */
512 my_checksum += character & 0xff;
513 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
514 return retval;
515 my_checksum += character & 0xff;
516 buffer[count++] = (character ^ 0x20) & 0xff;
518 else
520 my_checksum += character & 0xff;
521 buffer[count++] = character & 0xff;
525 *len = count;
527 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
528 return retval;
529 checksum[0] = character;
530 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
531 return retval;
532 checksum[1] = character;
533 checksum[2] = 0;
535 if (!noack)
537 *checksum_ok=(my_checksum == strtoul(checksum, NULL, 16));
540 return ERROR_OK;
543 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
545 int character;
546 int retval;
547 gdb_connection_t *gdb_con = connection->priv;
549 while (1)
553 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
554 return retval;
556 #ifdef _DEBUG_GDB_IO_
557 LOG_DEBUG("character: '%c'", character);
558 #endif
560 switch (character)
562 case '$':
563 break;
564 case '+':
565 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
566 * incase anyone tries to debug why they receive this warning every time */
567 LOG_WARNING("acknowledgment received, but no packet pending");
568 break;
569 case '-':
570 LOG_WARNING("negative acknowledgment, but no packet pending");
571 break;
572 case 0x3:
573 gdb_con->ctrl_c = 1;
574 *len = 0;
575 return ERROR_OK;
576 default:
577 LOG_WARNING("ignoring character 0x%x", character);
578 break;
580 } while (character != '$');
584 int checksum_ok;
585 /* explicit code expansion here to get faster inlined code in -O3 by not
586 * calculating checksum
588 if (gdb_con->noack_mode)
590 if ((retval=fetch_packet(connection, &checksum_ok, 1, len, buffer))!=ERROR_OK)
591 return retval;
592 } else
594 if ((retval=fetch_packet(connection, &checksum_ok, 0, len, buffer))!=ERROR_OK)
595 return retval;
598 if (gdb_con->noack_mode)
600 /* checksum is not checked in noack mode */
601 break;
603 if (checksum_ok)
605 if ((retval = gdb_write(connection, "+", 1)) != ERROR_OK)
607 return retval;
609 break;
612 if (gdb_con->closed)
613 return ERROR_SERVER_REMOTE_CLOSED;
615 return ERROR_OK;
618 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
620 gdb_connection_t *gdb_con = connection->priv;
621 gdb_con->busy = 1;
622 int retval = gdb_get_packet_inner(connection, buffer, len);
623 gdb_con->busy = 0;
624 return retval;
627 int gdb_output_con(connection_t *connection, const char* line)
629 char *hex_buffer;
630 int i, bin_size;
632 bin_size = strlen(line);
634 hex_buffer = malloc(bin_size*2 + 2);
635 if (hex_buffer == NULL)
636 return ERROR_GDB_BUFFER_TOO_SMALL;
638 hex_buffer[0] = 'O';
639 for (i=0; i<bin_size; i++)
640 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
641 hex_buffer[bin_size*2+1] = 0;
643 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
645 free(hex_buffer);
646 return retval;
649 int gdb_output(struct command_context_s *context, const char* line)
651 /* this will be dumped to the log and also sent as an O packet if possible */
652 LOG_USER_N("%s", line);
653 return ERROR_OK;
657 static void gdb_frontend_halted(struct target_s *target, connection_t *connection)
659 gdb_connection_t *gdb_connection = connection->priv;
661 /* In the GDB protocol when we are stepping or coninuing execution,
662 * we have a lingering reply. Upon receiving a halted event
663 * when we have that lingering packet, we reply to the original
664 * step or continue packet.
666 * Executing monitor commands can bring the target in and
667 * out of the running state so we'll see lots of TARGET_EVENT_XXX
668 * that are to be ignored.
670 if (gdb_connection->frontend_state == TARGET_RUNNING)
672 char sig_reply[4];
673 int signal;
675 /* stop forwarding log packets! */
676 log_remove_callback(gdb_log_callback, connection);
678 if (gdb_connection->ctrl_c)
680 signal = 0x2;
681 gdb_connection->ctrl_c = 0;
683 else
685 signal = gdb_last_signal(target);
688 snprintf(sig_reply, 4, "T%2.2x", signal);
689 gdb_put_packet(connection, sig_reply, 3);
690 gdb_connection->frontend_state = TARGET_HALTED;
694 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
696 int retval;
697 connection_t *connection = priv;
699 target_handle_event( target, event );
700 switch (event)
702 case TARGET_EVENT_EARLY_HALTED:
703 gdb_frontend_halted(target, connection);
704 break;
705 case TARGET_EVENT_HALTED:
706 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
707 break;
708 case TARGET_EVENT_GDB_FLASH_ERASE_START:
709 target_handle_event( target, TARGET_EVENT_OLD_gdb_program_config );
710 if((retval = jtag_execute_queue()) != ERROR_OK)
712 return retval;
714 break;
715 default:
716 break;
719 return ERROR_OK;
723 int gdb_new_connection(connection_t *connection)
725 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
726 gdb_service_t *gdb_service = connection->service->priv;
727 int retval;
728 int initial_ack;
730 connection->priv = gdb_connection;
732 /* initialize gdb connection information */
733 gdb_connection->buf_p = gdb_connection->buffer;
734 gdb_connection->buf_cnt = 0;
735 gdb_connection->ctrl_c = 0;
736 gdb_connection->frontend_state = TARGET_HALTED;
737 gdb_connection->vflash_image = NULL;
738 gdb_connection->closed = 0;
739 gdb_connection->busy = 0;
740 gdb_connection->noack_mode = 0;
742 /* send ACK to GDB for debug request */
743 gdb_write(connection, "+", 1);
745 /* output goes through gdb connection */
746 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
748 /* we must remove all breakpoints registered to the target as a previous
749 * GDB session could leave dangling breakpoints if e.g. communication
750 * timed out.
752 breakpoint_clear_target(gdb_service->target);
753 watchpoint_clear_target(gdb_service->target);
755 /* register callback to be informed about target events */
756 target_register_event_callback(gdb_target_callback_event_handler, connection);
758 /* a gdb session just attached, try to put the target in halt mode.
760 * DANGER!!!!
762 * If the halt fails(e.g. target needs a reset, JTAG communication not
763 * working, etc.), then the GDB connect will succeed as
764 * the get_gdb_reg_list() will lie and return a register list with
765 * dummy values.
767 * This allows GDB monitor commands to be run from a GDB init script to
768 * initialize the target
770 * Also, since the halt() is asynchronous target connect will be
771 * instantaneous and thus avoiding annoying timeout problems during
772 * connect.
774 target_halt(gdb_service->target);
775 /* FIX!!!! could extended-remote work better here?
777 * wait a tiny bit for halted state or we just continue. The
778 * GDB register packet will then contain garbage
780 target_wait_state(gdb_service->target, TARGET_HALTED, 500);
782 /* remove the initial ACK from the incoming buffer */
783 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
784 return retval;
786 /* FIX!!!??? would we actually ever receive a + here???
787 * Not observed.
789 if (initial_ack != '+')
790 gdb_putback_char(connection, initial_ack);
791 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH );
792 return ERROR_OK;
795 int gdb_connection_closed(connection_t *connection)
797 gdb_service_t *gdb_service = connection->service->priv;
798 gdb_connection_t *gdb_connection = connection->priv;
800 /* see if an image built with vFlash commands is left */
801 if (gdb_connection->vflash_image)
803 image_close(gdb_connection->vflash_image);
804 free(gdb_connection->vflash_image);
805 gdb_connection->vflash_image = NULL;
808 /* if this connection registered a debug-message receiver delete it */
809 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
811 if (connection->priv)
813 free(connection->priv);
814 connection->priv = NULL;
816 else
818 LOG_ERROR("BUG: connection->priv == NULL");
821 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
822 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
823 log_remove_callback(gdb_log_callback, connection);
825 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH );
827 return ERROR_OK;
830 void gdb_send_error(connection_t *connection, u8 the_error)
832 char err[4];
833 snprintf(err, 4, "E%2.2X", the_error );
834 gdb_put_packet(connection, err, 3);
837 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
839 char sig_reply[4];
840 int signal;
842 signal = gdb_last_signal(target);
844 snprintf(sig_reply, 4, "S%2.2x", signal);
845 gdb_put_packet(connection, sig_reply, 3);
847 return ERROR_OK;
850 /* Convert register to string of bits. NB! The # of bits in the
851 * register might be non-divisible by 8(a byte), in which
852 * case an entire byte is shown. */
853 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
855 int i;
857 u8 *buf;
858 int buf_len;
859 buf = reg->value;
860 buf_len = CEIL(reg->size, 8);
862 for (i = 0; i < buf_len; i++)
864 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
865 tstr[i*2+1] = DIGITS[buf[i]&0xf];
869 void gdb_target_to_str(target_t *target, char *tstr, char *str)
871 int str_len = strlen(tstr);
872 int i;
874 if (str_len % 2)
876 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
877 exit(-1);
880 for (i = 0; i < str_len; i+=2)
882 str[str_len - i - 1] = tstr[i + 1];
883 str[str_len - i - 2] = tstr[i];
887 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
889 reg_t **reg_list;
890 int reg_list_size;
891 int retval;
892 int reg_packet_size = 0;
893 char *reg_packet;
894 char *reg_packet_p;
895 int i;
897 #ifdef _DEBUG_GDB_IO_
898 LOG_DEBUG("-");
899 #endif
901 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
903 return gdb_error(connection, retval);
906 for (i = 0; i < reg_list_size; i++)
908 reg_packet_size += reg_list[i]->size;
911 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
912 reg_packet_p = reg_packet;
914 for (i = 0; i < reg_list_size; i++)
916 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
917 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
920 #ifdef _DEBUG_GDB_IO_
922 char *reg_packet_p;
923 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
924 LOG_DEBUG("reg_packet: %s", reg_packet_p);
925 free(reg_packet_p);
927 #endif
929 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
930 free(reg_packet);
932 free(reg_list);
934 return ERROR_OK;
937 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
939 int i;
940 reg_t **reg_list;
941 int reg_list_size;
942 int retval;
943 char *packet_p;
945 #ifdef _DEBUG_GDB_IO_
946 LOG_DEBUG("-");
947 #endif
949 /* skip command character */
950 packet++;
951 packet_size--;
953 if (packet_size % 2)
955 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
956 return ERROR_SERVER_REMOTE_CLOSED;
959 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
961 return gdb_error(connection, retval);
964 packet_p = packet;
965 for (i = 0; i < reg_list_size; i++)
967 u8 *bin_buf;
968 char *hex_buf;
969 reg_arch_type_t *arch_type;
971 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
972 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
973 gdb_target_to_str(target, packet_p, hex_buf);
975 /* convert hex-string to binary buffer */
976 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
977 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
979 /* get register arch_type, and call set method */
980 arch_type = register_get_arch_type(reg_list[i]->arch_type);
982 arch_type->set(reg_list[i], bin_buf);
984 /* advance packet pointer */
985 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
987 free(bin_buf);
988 free(hex_buf);
991 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
992 free(reg_list);
994 gdb_put_packet(connection, "OK", 2);
996 return ERROR_OK;
999 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1001 char *reg_packet;
1002 int reg_num = strtoul(packet + 1, NULL, 16);
1003 reg_t **reg_list;
1004 int reg_list_size;
1005 int retval;
1007 #ifdef _DEBUG_GDB_IO_
1008 LOG_DEBUG("-");
1009 #endif
1011 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1013 return gdb_error(connection, retval);
1016 if (reg_list_size <= reg_num)
1018 LOG_ERROR("gdb requested a non-existing register");
1019 exit(-1);
1022 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1024 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1026 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
1028 free(reg_list);
1029 free(reg_packet);
1031 return ERROR_OK;
1034 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1036 char *separator;
1037 char *hex_buf;
1038 u8 *bin_buf;
1039 int reg_num = strtoul(packet + 1, &separator, 16);
1040 reg_t **reg_list;
1041 int reg_list_size;
1042 int retval;
1043 reg_arch_type_t *arch_type;
1045 LOG_DEBUG("-");
1047 if ((retval = target->type->get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1049 return gdb_error(connection, retval);
1052 if (reg_list_size < reg_num)
1054 LOG_ERROR("gdb requested a non-existing register");
1055 return ERROR_SERVER_REMOTE_CLOSED;
1058 if (*separator != '=')
1060 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1061 return ERROR_SERVER_REMOTE_CLOSED;
1064 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1065 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1066 gdb_target_to_str(target, separator + 1, hex_buf);
1068 /* convert hex-string to binary buffer */
1069 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1070 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1072 /* get register arch_type, and call set method */
1073 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1074 arch_type->set(reg_list[reg_num], bin_buf);
1076 gdb_put_packet(connection, "OK", 2);
1078 free(bin_buf);
1079 free(hex_buf);
1080 free(reg_list);
1082 return ERROR_OK;
1085 int gdb_error(connection_t *connection, int retval)
1087 switch (retval)
1089 case ERROR_TARGET_DATA_ABORT:
1090 gdb_send_error(connection, EIO);
1091 break;
1092 case ERROR_TARGET_TRANSLATION_FAULT:
1093 gdb_send_error(connection, EFAULT);
1094 break;
1095 case ERROR_TARGET_UNALIGNED_ACCESS:
1096 gdb_send_error(connection, EFAULT);
1097 break;
1098 case ERROR_TARGET_NOT_HALTED:
1099 gdb_send_error(connection, EFAULT);
1100 break;
1101 default:
1102 /* This could be that the target reset itself. */
1103 LOG_ERROR("unexpected error %i", retval);
1104 gdb_send_error(connection, EFAULT);
1105 break;
1108 return ERROR_OK;
1111 /* We don't have to worry about the default 2 second timeout for GDB packets,
1112 * because GDB breaks up large memory reads into smaller reads.
1114 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1116 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1118 char *separator;
1119 u32 addr = 0;
1120 u32 len = 0;
1122 u8 *buffer;
1123 char *hex_buffer;
1125 int retval = ERROR_OK;
1127 /* skip command character */
1128 packet++;
1130 addr = strtoul(packet, &separator, 16);
1132 if (*separator != ',')
1134 LOG_ERROR("incomplete read memory packet received, dropping connection");
1135 return ERROR_SERVER_REMOTE_CLOSED;
1138 len = strtoul(separator+1, NULL, 16);
1140 buffer = malloc(len);
1142 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1144 retval = target_read_buffer(target, addr, len, buffer);
1146 if ((retval!=ERROR_OK)&&!gdb_report_data_abort)
1148 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1149 * At some point this might be fixed in GDB, in which case this code can be removed.
1151 * OpenOCD developers are acutely aware of this problem, but there is nothing
1152 * gained by involving the user in this problem that hopefully will get resolved
1153 * eventually
1155 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1157 * For now, the default is to fix up things to make current GDB versions work.
1158 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1160 memset(buffer, 0, len);
1161 retval = ERROR_OK;
1164 if (retval == ERROR_OK)
1166 hex_buffer = malloc(len * 2 + 1);
1168 int i;
1169 for (i = 0; i < len; i++)
1171 u8 t = buffer[i];
1172 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1173 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1176 gdb_put_packet(connection, hex_buffer, len * 2);
1178 free(hex_buffer);
1180 else
1182 retval = gdb_error(connection, retval);
1185 free(buffer);
1187 return retval;
1190 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1192 char *separator;
1193 u32 addr = 0;
1194 u32 len = 0;
1196 u8 *buffer;
1198 int i;
1199 int retval;
1201 /* skip command character */
1202 packet++;
1204 addr = strtoul(packet, &separator, 16);
1206 if (*separator != ',')
1208 LOG_ERROR("incomplete write memory packet received, dropping connection");
1209 return ERROR_SERVER_REMOTE_CLOSED;
1212 len = strtoul(separator+1, &separator, 16);
1214 if (*(separator++) != ':')
1216 LOG_ERROR("incomplete write memory packet received, dropping connection");
1217 return ERROR_SERVER_REMOTE_CLOSED;
1220 buffer = malloc(len);
1222 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1224 for (i=0; i<len; i++)
1226 u32 tmp;
1227 sscanf(separator + 2*i, "%2x", &tmp);
1228 buffer[i] = tmp;
1231 retval = target_write_buffer(target, addr, len, buffer);
1233 if (retval == ERROR_OK)
1235 gdb_put_packet(connection, "OK", 2);
1237 else
1239 retval = gdb_error(connection, retval);
1242 free(buffer);
1244 return retval;
1247 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1249 char *separator;
1250 u32 addr = 0;
1251 u32 len = 0;
1253 int retval;
1255 /* skip command character */
1256 packet++;
1258 addr = strtoul(packet, &separator, 16);
1260 if (*separator != ',')
1262 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1263 return ERROR_SERVER_REMOTE_CLOSED;
1266 len = strtoul(separator+1, &separator, 16);
1268 if (*(separator++) != ':')
1270 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1271 return ERROR_SERVER_REMOTE_CLOSED;
1274 retval = ERROR_OK;
1275 if (len)
1277 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1279 retval = target_write_buffer(target, addr, len, (u8*)separator);
1282 if (retval == ERROR_OK)
1284 gdb_put_packet(connection, "OK", 2);
1286 else
1288 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1289 return retval;
1292 return ERROR_OK;
1295 int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1297 int current = 0;
1298 u32 address = 0x0;
1299 int retval=ERROR_OK;
1301 LOG_DEBUG("-");
1303 if (packet_size > 1)
1305 packet[packet_size] = 0;
1306 address = strtoul(packet + 1, NULL, 16);
1308 else
1310 current = 1;
1313 if (packet[0] == 'c')
1315 LOG_DEBUG("continue");
1316 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1317 retval=target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1319 else if (packet[0] == 's')
1321 LOG_DEBUG("step");
1322 retval=target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1324 return retval;
1327 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1329 int type;
1330 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1331 enum watchpoint_rw wp_type;
1332 u32 address;
1333 u32 size;
1334 char *separator;
1335 int retval;
1337 LOG_DEBUG("-");
1339 type = strtoul(packet + 1, &separator, 16);
1341 if (type == 0) /* memory breakpoint */
1342 bp_type = BKPT_SOFT;
1343 else if (type == 1) /* hardware breakpoint */
1344 bp_type = BKPT_HARD;
1345 else if (type == 2) /* write watchpoint */
1346 wp_type = WPT_WRITE;
1347 else if (type == 3) /* read watchpoint */
1348 wp_type = WPT_READ;
1349 else if (type == 4) /* access watchpoint */
1350 wp_type = WPT_ACCESS;
1352 if (gdb_breakpoint_override&&((bp_type==BKPT_SOFT)||(bp_type==BKPT_HARD)))
1354 bp_type=gdb_breakpoint_override_type;
1357 if (*separator != ',')
1359 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1360 return ERROR_SERVER_REMOTE_CLOSED;
1363 address = strtoul(separator+1, &separator, 16);
1365 if (*separator != ',')
1367 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1368 return ERROR_SERVER_REMOTE_CLOSED;
1371 size = strtoul(separator+1, &separator, 16);
1373 switch (type)
1375 case 0:
1376 case 1:
1377 if (packet[0] == 'Z')
1379 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1381 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1382 return retval;
1384 else
1386 gdb_put_packet(connection, "OK", 2);
1389 else
1391 breakpoint_remove(target, address);
1392 gdb_put_packet(connection, "OK", 2);
1394 break;
1395 case 2:
1396 case 3:
1397 case 4:
1399 if (packet[0] == 'Z')
1401 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1403 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1404 return retval;
1406 else
1408 gdb_put_packet(connection, "OK", 2);
1411 else
1413 watchpoint_remove(target, address);
1414 gdb_put_packet(connection, "OK", 2);
1416 break;
1418 default:
1419 break;
1422 return ERROR_OK;
1425 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1426 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1428 if (*retval != ERROR_OK)
1430 return;
1432 int first = 1;
1434 for (;;)
1436 if ((*xml == NULL) || (!first))
1438 /* start by 0 to exercise all the code paths.
1439 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1441 *size = *size * 2 + 2;
1442 char *t = *xml;
1443 *xml = realloc(*xml, *size);
1444 if (*xml == NULL)
1446 if (t)
1447 free(t);
1448 *retval = ERROR_SERVER_REMOTE_CLOSED;
1449 return;
1453 va_list ap;
1454 int ret;
1455 va_start(ap, fmt);
1456 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1457 va_end(ap);
1458 if ((ret > 0) && ((ret + 1) < *size - *pos))
1460 *pos += ret;
1461 return;
1463 /* there was just enough or not enough space, allocate more. */
1464 first = 0;
1468 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1470 char *separator;
1472 /* Extract and NUL-terminate the annex. */
1473 *annex = buf;
1474 while (*buf && *buf != ':')
1475 buf++;
1476 if (*buf == '\0')
1477 return -1;
1478 *buf++ = 0;
1480 /* After the read marker and annex, qXfer looks like a
1481 * traditional 'm' packet. */
1483 *ofs = strtoul(buf, &separator, 16);
1485 if (*separator != ',')
1486 return -1;
1488 *len = strtoul(separator+1, NULL, 16);
1490 return 0;
1493 int gdb_calc_blocksize(flash_bank_t *bank)
1495 int i;
1496 int block_size = 0xffffffff;
1498 /* loop through all sectors and return smallest sector size */
1500 for (i = 0; i < bank->num_sectors; i++)
1502 if (bank->sectors[i].size < block_size)
1503 block_size = bank->sectors[i].size;
1506 return block_size;
1509 static int compare_bank (const void * a, const void * b)
1511 flash_bank_t *b1, *b2;
1512 b1=*((flash_bank_t **)a);
1513 b2=*((flash_bank_t **)b);
1515 if (b1->base==b2->base)
1517 return 0;
1518 } else if (b1->base>b2->base)
1520 return 1;
1521 } else
1523 return -1;
1527 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1529 command_context_t *cmd_ctx = connection->cmd_ctx;
1530 gdb_connection_t *gdb_connection = connection->priv;
1532 if (strstr(packet, "qRcmd,"))
1534 if (packet_size > 6)
1536 char *cmd;
1537 int i;
1538 cmd = malloc((packet_size - 6)/2 + 1);
1539 for (i=0; i < (packet_size - 6)/2; i++)
1541 u32 tmp;
1542 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1543 cmd[i] = tmp;
1545 cmd[(packet_size - 6)/2] = 0x0;
1547 /* We want to print all debug output to GDB connection */
1548 log_add_callback(gdb_log_callback, connection);
1549 target_call_timer_callbacks_now();
1550 command_run_line(cmd_ctx, cmd);
1551 target_call_timer_callbacks_now();
1552 log_remove_callback(gdb_log_callback, connection);
1553 free(cmd);
1555 gdb_put_packet(connection, "OK", 2);
1556 return ERROR_OK;
1558 else if (strstr(packet, "qCRC:"))
1560 if (packet_size > 5)
1562 int retval;
1563 char gdb_reply[10];
1564 char *separator;
1565 u32 checksum;
1566 u32 addr = 0;
1567 u32 len = 0;
1569 /* skip command character */
1570 packet += 5;
1572 addr = strtoul(packet, &separator, 16);
1574 if (*separator != ',')
1576 LOG_ERROR("incomplete read memory packet received, dropping connection");
1577 return ERROR_SERVER_REMOTE_CLOSED;
1580 len = strtoul(separator + 1, NULL, 16);
1582 retval = target_checksum_memory(target, addr, len, &checksum);
1584 if (retval == ERROR_OK)
1586 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1587 gdb_put_packet(connection, gdb_reply, 9);
1589 else
1591 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1592 return retval;
1595 return ERROR_OK;
1598 else if (strstr(packet, "qSupported"))
1600 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1601 * disable qXfer:features:read for the moment */
1602 int retval = ERROR_OK;
1603 char *buffer = NULL;
1604 int pos = 0;
1605 int size = 0;
1607 xml_printf(&retval, &buffer, &pos, &size,
1608 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1609 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1611 if (retval != ERROR_OK)
1613 gdb_send_error(connection, 01);
1614 return ERROR_OK;
1617 gdb_put_packet(connection, buffer, strlen(buffer));
1618 free(buffer);
1620 return ERROR_OK;
1622 else if (strstr(packet, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1624 /* We get away with only specifying flash here. Regions that are not
1625 * specified are treated as if we provided no memory map(if not we
1626 * could detect the holes and mark them as RAM).
1627 * Normally we only execute this code once, but no big deal if we
1628 * have to regenerate it a couple of times. */
1630 flash_bank_t *p;
1631 char *xml = NULL;
1632 int size = 0;
1633 int pos = 0;
1634 int retval = ERROR_OK;
1636 int offset;
1637 int length;
1638 char *separator;
1639 int blocksize;
1641 /* skip command character */
1642 packet += 23;
1644 offset = strtoul(packet, &separator, 16);
1645 length = strtoul(separator + 1, &separator, 16);
1647 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1650 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1651 read/write) by default for GDB.
1652 GDB does not have a concept of non-cacheable read/write memory.
1654 flash_bank_t **banks=malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
1655 int i;
1657 for (i=0; i<flash_get_bank_count(); i++)
1659 p = get_flash_bank_by_num(i);
1660 if (p == NULL)
1662 free(banks);
1663 retval = ERROR_FAIL;
1664 gdb_send_error(connection, retval);
1665 return retval;
1667 banks[i]=p;
1670 qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
1672 u32 ram_start=0;
1673 for (i=0; i<flash_get_bank_count(); i++)
1675 p = banks[i];
1677 if (ram_start<p->base)
1679 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1680 ram_start, p->base-ram_start);
1683 /* if device has uneven sector sizes, eg. str7, lpc
1684 * we pass the smallest sector size to gdb memory map */
1685 blocksize = gdb_calc_blocksize(p);
1687 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1688 "<property name=\"blocksize\">0x%x</property>\n" \
1689 "</memory>\n", \
1690 p->base, p->size, blocksize);
1691 ram_start=p->base+p->size;
1693 if (ram_start!=0)
1695 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1696 ram_start, 0-ram_start);
1697 } else
1699 /* a flash chip could be at the very end of the 32 bit address space, in which case
1700 ram_start will be precisely 0 */
1703 free(banks);
1704 banks = NULL;
1706 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1708 if (retval != ERROR_OK)
1710 gdb_send_error(connection, retval);
1711 return retval;
1714 if (offset + length > pos)
1716 length = pos - offset;
1719 char *t = malloc(length + 1);
1720 t[0] = 'l';
1721 memcpy(t + 1, xml + offset, length);
1722 gdb_put_packet(connection, t, length + 1);
1724 free(t);
1725 free(xml);
1726 return ERROR_OK;
1728 else if (strstr(packet, "qXfer:features:read:"))
1730 char *xml = NULL;
1731 int size = 0;
1732 int pos = 0;
1733 int retval = ERROR_OK;
1735 int offset;
1736 unsigned int length;
1737 char *annex;
1739 /* skip command character */
1740 packet += 20;
1742 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1744 gdb_send_error(connection, 01);
1745 return ERROR_OK;
1748 if (strcmp(annex, "target.xml") != 0)
1750 gdb_send_error(connection, 01);
1751 return ERROR_OK;
1754 xml_printf(&retval, &xml, &pos, &size, \
1755 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1757 if (retval != ERROR_OK)
1759 gdb_send_error(connection, retval);
1760 return retval;
1763 gdb_put_packet(connection, xml, strlen(xml));
1765 free(xml);
1766 return ERROR_OK;
1768 else if (strstr(packet, "QStartNoAckMode"))
1770 gdb_connection->noack_mode = 1;
1771 gdb_put_packet(connection, "OK", 2);
1772 return ERROR_OK;
1775 gdb_put_packet(connection, "", 0);
1776 return ERROR_OK;
1779 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1781 gdb_connection_t *gdb_connection = connection->priv;
1782 gdb_service_t *gdb_service = connection->service->priv;
1783 int result;
1785 /* if flash programming disabled - send a empty reply */
1787 if (gdb_flash_program == 0)
1789 gdb_put_packet(connection, "", 0);
1790 return ERROR_OK;
1793 if (strstr(packet, "vFlashErase:"))
1795 unsigned long addr;
1796 unsigned long length;
1798 char *parse = packet + 12;
1799 if (*parse == '\0')
1801 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1802 return ERROR_SERVER_REMOTE_CLOSED;
1805 addr = strtoul(parse, &parse, 16);
1807 if (*(parse++) != ',' || *parse == '\0')
1809 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1810 return ERROR_SERVER_REMOTE_CLOSED;
1813 length = strtoul(parse, &parse, 16);
1815 if (*parse != '\0')
1817 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1818 return ERROR_SERVER_REMOTE_CLOSED;
1821 /* assume all sectors need erasing - stops any problems
1822 * when flash_write is called multiple times */
1823 flash_set_dirty();
1825 /* perform any target specific operations before the erase */
1826 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1827 result = flash_erase_address_range(gdb_service->target, addr, length );
1828 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
1830 /* perform erase */
1831 if (result != ERROR_OK)
1833 /* GDB doesn't evaluate the actual error number returned,
1834 * treat a failed erase as an I/O error
1836 gdb_send_error(connection, EIO);
1837 LOG_ERROR("flash_erase returned %i", result);
1839 else
1840 gdb_put_packet(connection, "OK", 2);
1842 return ERROR_OK;
1845 if (strstr(packet, "vFlashWrite:"))
1847 int retval;
1848 unsigned long addr;
1849 unsigned long length;
1850 char *parse = packet + 12;
1852 if (*parse == '\0')
1854 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1855 return ERROR_SERVER_REMOTE_CLOSED;
1857 addr = strtoul(parse, &parse, 16);
1858 if (*(parse++) != ':')
1860 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1861 return ERROR_SERVER_REMOTE_CLOSED;
1863 length = packet_size - (parse - packet);
1865 /* create a new image if there isn't already one */
1866 if (gdb_connection->vflash_image == NULL)
1868 gdb_connection->vflash_image = malloc(sizeof(image_t));
1869 image_open(gdb_connection->vflash_image, "", "build");
1872 /* create new section with content from packet buffer */
1873 if((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse)) != ERROR_OK)
1875 return retval;
1878 gdb_put_packet(connection, "OK", 2);
1880 return ERROR_OK;
1883 if (!strcmp(packet, "vFlashDone"))
1885 u32 written;
1887 /* process the flashing buffer. No need to erase as GDB
1888 * always issues a vFlashErase first. */
1889 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
1890 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1891 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1892 if ( result != ERROR_OK)
1894 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1895 gdb_put_packet(connection, "E.memtype", 9);
1896 else
1897 gdb_send_error(connection, EIO);
1899 else
1901 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written);
1902 gdb_put_packet(connection, "OK", 2);
1905 image_close(gdb_connection->vflash_image);
1906 free(gdb_connection->vflash_image);
1907 gdb_connection->vflash_image = NULL;
1909 return ERROR_OK;
1912 gdb_put_packet(connection, "", 0);
1913 return ERROR_OK;
1916 int gdb_detach(connection_t *connection, target_t *target)
1919 switch( detach_mode )
1921 case GDB_DETACH_RESUME:
1922 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1923 target_resume(target, 1, 0, 1, 0);
1924 break;
1926 case GDB_DETACH_RESET:
1927 /* FIX?? make this configurable?? */
1928 target_process_reset(connection->cmd_ctx, RESET_HALT);
1929 break;
1931 case GDB_DETACH_HALT:
1932 target_halt(target);
1933 break;
1935 case GDB_DETACH_NOTHING:
1936 break;
1939 gdb_put_packet(connection, "OK", 2);
1940 return ERROR_OK;
1943 static void gdb_log_callback(void *priv, const char *file, int line,
1944 const char *function, const char *string)
1946 connection_t *connection = priv;
1947 gdb_connection_t *gdb_con = connection->priv;
1949 if (gdb_con->busy)
1951 /* do not reply this using the O packet */
1952 return;
1955 gdb_output_con(connection, string);
1958 /* Do not allocate this on the stack */
1959 char gdb_packet_buffer[GDB_BUFFER_SIZE];
1961 static void gdb_sig_halted(connection_t *connection)
1963 char sig_reply[4];
1964 snprintf(sig_reply, 4, "T%2.2x", 2);
1965 gdb_put_packet(connection, sig_reply, 3);
1969 int gdb_input_inner(connection_t *connection)
1971 gdb_service_t *gdb_service = connection->service->priv;
1972 target_t *target = gdb_service->target;
1973 char *packet=gdb_packet_buffer;
1974 int packet_size;
1975 int retval;
1976 gdb_connection_t *gdb_con = connection->priv;
1977 static int extended_protocol = 0;
1979 /* drain input buffer */
1982 packet_size = GDB_BUFFER_SIZE-1;
1983 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1985 return retval;
1988 /* terminate with zero */
1989 packet[packet_size] = 0;
1991 if( LOG_LEVEL_IS( LOG_LVL_DEBUG ) ){
1992 if( packet[0] == 'X' ){
1993 // binary packets spew junk into the debug log stream
1994 char buf[ 50 ];
1995 int x;
1996 for( x = 0 ; (x < 49) && (packet[x] != ':') ; x++ ){
1997 buf[x] = packet[x];
1999 buf[x] = 0;
2000 LOG_DEBUG("received packet: '%s:<binary-data>'", buf );
2001 } else {
2002 LOG_DEBUG("received packet: '%s'", packet );
2006 if (packet_size > 0)
2008 retval = ERROR_OK;
2009 switch (packet[0])
2011 case 'H':
2012 /* Hct... -- set thread
2013 * we don't have threads, send empty reply */
2014 gdb_put_packet(connection, NULL, 0);
2015 break;
2016 case 'q':
2017 case 'Q':
2018 retval = gdb_query_packet(connection, target, packet, packet_size);
2019 break;
2020 case 'g':
2021 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
2022 break;
2023 case 'G':
2024 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
2025 break;
2026 case 'p':
2027 retval = gdb_get_register_packet(connection, target, packet, packet_size);
2028 break;
2029 case 'P':
2030 retval = gdb_set_register_packet(connection, target, packet, packet_size);
2031 break;
2032 case 'm':
2033 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
2034 break;
2035 case 'M':
2036 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
2037 break;
2038 case 'z':
2039 case 'Z':
2040 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
2041 break;
2042 case '?':
2043 gdb_last_signal_packet(connection, target, packet, packet_size);
2044 break;
2045 case 'c':
2046 case 's':
2048 if (target->state != TARGET_HALTED)
2050 /* If the target isn't in the halted state, then we can't
2051 * step/continue. This might be early setup, etc.
2053 gdb_sig_halted(connection);
2054 } else
2056 /* We're running/stepping, in which case we can
2057 * forward log output until the target is halted
2059 gdb_connection_t *gdb_con = connection->priv;
2060 gdb_con->frontend_state = TARGET_RUNNING;
2061 log_add_callback(gdb_log_callback, connection);
2062 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2063 int retval=gdb_step_continue_packet(connection, target, packet, packet_size);
2064 if (retval!=ERROR_OK)
2066 /* we'll never receive a halted condition... issue a false one.. */
2067 gdb_frontend_halted(target, connection);
2071 break;
2072 case 'v':
2073 retval = gdb_v_packet(connection, target, packet, packet_size);
2074 break;
2075 case 'D':
2076 retval = gdb_detach(connection, target);
2077 extended_protocol = 0;
2078 break;
2079 case 'X':
2080 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2081 return retval;
2082 break;
2083 case 'k':
2084 if (extended_protocol != 0)
2085 break;
2086 gdb_put_packet(connection, "OK", 2);
2087 return ERROR_SERVER_REMOTE_CLOSED;
2088 case '!':
2089 /* handle extended remote protocol */
2090 extended_protocol = 1;
2091 gdb_put_packet(connection, "OK", 2);
2092 break;
2093 case 'R':
2094 /* handle extended restart packet */
2095 breakpoint_clear_target(gdb_service->target);
2096 watchpoint_clear_target(gdb_service->target);
2097 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
2098 break;
2099 default:
2100 /* ignore unkown packets */
2101 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2102 gdb_put_packet(connection, NULL, 0);
2103 break;
2106 /* if a packet handler returned an error, exit input loop */
2107 if (retval != ERROR_OK)
2108 return retval;
2111 if (gdb_con->ctrl_c)
2113 if (target->state == TARGET_RUNNING)
2115 target_halt(target);
2116 gdb_con->ctrl_c = 0;
2120 } while (gdb_con->buf_cnt > 0);
2122 return ERROR_OK;
2125 int gdb_input(connection_t *connection)
2127 int retval = gdb_input_inner(connection);
2128 gdb_connection_t *gdb_con = connection->priv;
2129 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2130 return retval;
2132 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2133 if (gdb_con->closed)
2134 return ERROR_SERVER_REMOTE_CLOSED;
2136 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2137 return ERROR_OK;
2140 int gdb_init(void)
2142 gdb_service_t *gdb_service;
2143 target_t *target = all_targets;
2145 if (!target)
2147 LOG_WARNING("no gdb ports allocated as no target has been specified");
2148 return ERROR_OK;
2151 if (gdb_port == 0)
2153 LOG_WARNING("no gdb port specified, using default port 3333");
2154 gdb_port = 3333;
2157 while (target)
2159 char service_name[8];
2161 snprintf(service_name, 8, "gdb-%2.2i", target->target_number);
2163 gdb_service = malloc(sizeof(gdb_service_t));
2164 gdb_service->target = target;
2166 add_service("gdb", CONNECTION_GDB,
2167 gdb_port + target->target_number,
2168 1, gdb_new_connection, gdb_input,
2169 gdb_connection_closed,
2170 gdb_service);
2172 LOG_DEBUG("gdb service for target %s at port %i",
2173 target->type->name,
2174 gdb_port + target->target_number);
2176 target = target->next;
2179 return ERROR_OK;
2182 /* daemon configuration command gdb_port */
2183 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2185 if (argc == 0)
2187 command_print(cmd_ctx, "%d", gdb_port);
2188 return ERROR_OK;
2191 /* only if the port wasn't overwritten by cmdline */
2192 if (gdb_port == 0)
2193 gdb_port = strtoul(args[0], NULL, 0);
2195 return ERROR_OK;
2198 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2200 if (argc == 1)
2202 if (strcmp(args[0], "resume") == 0)
2204 detach_mode = GDB_DETACH_RESUME;
2205 return ERROR_OK;
2207 else if (strcmp(args[0], "reset") == 0)
2209 detach_mode = GDB_DETACH_RESET;
2210 return ERROR_OK;
2212 else if (strcmp(args[0], "halt") == 0)
2214 detach_mode = GDB_DETACH_HALT;
2215 return ERROR_OK;
2217 else if (strcmp(args[0], "nothing") == 0)
2219 detach_mode = GDB_DETACH_NOTHING;
2220 return ERROR_OK;
2222 else
2223 LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2226 return ERROR_COMMAND_SYNTAX_ERROR;
2229 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2231 if (argc == 1)
2233 if (strcmp(args[0], "enable") == 0)
2235 gdb_use_memory_map = 1;
2236 return ERROR_OK;
2238 else if (strcmp(args[0], "disable") == 0)
2240 gdb_use_memory_map = 0;
2241 return ERROR_OK;
2243 else
2244 LOG_WARNING("invalid gdb_memory_map configuration directive %s", args[0]);
2247 return ERROR_COMMAND_SYNTAX_ERROR;
2250 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2252 if (argc == 1)
2254 if (strcmp(args[0], "enable") == 0)
2256 gdb_flash_program = 1;
2257 return ERROR_OK;
2259 else if (strcmp(args[0], "disable") == 0)
2261 gdb_flash_program = 0;
2262 return ERROR_OK;
2264 else
2265 LOG_WARNING("invalid gdb_flash_program configuration directive: %s", args[0]);
2268 return ERROR_COMMAND_SYNTAX_ERROR;
2271 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2273 if (argc == 1)
2275 if (strcmp(args[0], "enable") == 0)
2277 gdb_report_data_abort = 1;
2278 return ERROR_OK;
2280 else if (strcmp(args[0], "disable") == 0)
2282 gdb_report_data_abort = 0;
2283 return ERROR_OK;
2285 else
2286 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2289 return ERROR_COMMAND_SYNTAX_ERROR;
2292 /* gdb_breakpoint_override */
2293 int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2295 if (argc == 0)
2298 } else if (argc==1)
2300 gdb_breakpoint_override = 1;
2301 if (strcmp(args[0], "hard")==0)
2303 gdb_breakpoint_override_type=BKPT_HARD;
2304 } else if (strcmp(args[0], "soft")==0)
2306 gdb_breakpoint_override_type=BKPT_SOFT;
2307 } else if (strcmp(args[0], "disable") == 0)
2309 gdb_breakpoint_override = 0;
2311 } else
2313 return ERROR_COMMAND_SYNTAX_ERROR;
2315 if (gdb_breakpoint_override)
2317 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type==BKPT_HARD)?"hard":"soft");
2318 } else
2320 LOG_USER("breakpoint type is not overriden");
2323 return ERROR_OK;
2327 int gdb_register_commands(command_context_t *command_context)
2329 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2330 COMMAND_CONFIG, "daemon configuration command gdb_port");
2331 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2332 COMMAND_CONFIG, "");
2333 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2334 COMMAND_CONFIG, "enable or disable memory map");
2335 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2336 COMMAND_CONFIG, "enable or disable flash program");
2337 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2338 COMMAND_CONFIG, "enable or disable report data");
2339 register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
2340 COMMAND_EXEC, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2341 "The raison d'etre for this option is to support GDB GUI's without "
2342 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2343 "is not sufficient");
2344 return ERROR_OK;