1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
30 #include "replacements.h"
32 #include "gdb_server.h"
36 #include "binarybuffer.h"
38 #include "breakpoints.h"
40 #include "target_request.h"
41 #include "configuration.h"
49 #define _DEBUG_GDB_IO_
52 static int gdb_breakpoint_override;
53 static enum breakpoint_type gdb_breakpoint_override_type;
55 extern int gdb_error(connection_t *connection, int retval);
56 static unsigned short gdb_port;
57 static const char *DIGITS = "0123456789abcdef";
59 static void gdb_log_callback(void *priv, const char *file, int line,
60 const char *function, const char *string);
70 /* target behaviour on gdb detach */
71 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
73 /* set if we are sending a memory map to gdb
74 * via qXfer:memory-map:read packet */
75 /* enabled by default*/
76 int gdb_use_memory_map = 1;
77 /* enabled by default*/
78 int gdb_flash_program = 1;
80 /* if set, data aborts cause an error to be reported in memory read packets
81 * see the code in gdb_read_memory_packet() for further explanations */
82 int gdb_report_data_abort = 0;
84 int gdb_last_signal(target_t *target)
86 switch (target->debug_reason)
88 case DBG_REASON_DBGRQ:
89 return 0x2; /* SIGINT */
90 case DBG_REASON_BREAKPOINT:
91 case DBG_REASON_WATCHPOINT:
92 case DBG_REASON_WPTANDBKPT:
93 return 0x05; /* SIGTRAP */
94 case DBG_REASON_SINGLESTEP:
95 return 0x05; /* SIGTRAP */
96 case DBG_REASON_NOTHALTED:
97 return 0x0; /* no signal... shouldn't happen */
99 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
104 int check_pending(connection_t *connection, int timeout_s, int *got_data)
106 /* a non-blocking socket will block if there is 0 bytes available on the socket,
107 * but return with as many bytes as are available immediately
111 gdb_connection_t *gdb_con = connection->priv;
117 if (gdb_con->buf_cnt>0)
124 FD_SET(connection->fd, &read_fds);
126 tv.tv_sec = timeout_s;
128 if (select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
130 /* This can typically be because a "monitor" command took too long
131 * before printing any progress messages
135 return ERROR_GDB_TIMEOUT;
141 *got_data=FD_ISSET(connection->fd, &read_fds)!=0;
145 int gdb_get_char(connection_t *connection, int* next_char)
147 gdb_connection_t *gdb_con = connection->priv;
150 #ifdef _DEBUG_GDB_IO_
154 if (gdb_con->buf_cnt-- > 0)
156 *next_char = *(gdb_con->buf_p++);
157 if (gdb_con->buf_cnt > 0)
158 connection->input_pending = 1;
160 connection->input_pending = 0;
162 #ifdef _DEBUG_GDB_IO_
163 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
171 retval=check_pending(connection, 1, NULL);
172 if (retval!=ERROR_OK)
174 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
175 if (gdb_con->buf_cnt > 0)
179 if (gdb_con->buf_cnt == 0)
182 return ERROR_SERVER_REMOTE_CLOSED;
186 errno = WSAGetLastError();
193 case WSAECONNABORTED:
195 return ERROR_SERVER_REMOTE_CLOSED;
198 return ERROR_SERVER_REMOTE_CLOSED;
200 LOG_ERROR("read: %d", errno);
211 return ERROR_SERVER_REMOTE_CLOSED;
214 return ERROR_SERVER_REMOTE_CLOSED;
216 LOG_ERROR("read: %s", strerror(errno));
218 return ERROR_SERVER_REMOTE_CLOSED;
223 #ifdef _DEBUG_GDB_IO_
224 debug_buffer = malloc(gdb_con->buf_cnt + 1);
225 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
226 debug_buffer[gdb_con->buf_cnt] = 0;
227 LOG_DEBUG("received '%s'", debug_buffer);
231 gdb_con->buf_p = gdb_con->buffer;
233 *next_char = *(gdb_con->buf_p++);
234 if (gdb_con->buf_cnt > 0)
235 connection->input_pending = 1;
237 connection->input_pending = 0;
238 #ifdef _DEBUG_GDB_IO_
239 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
245 int gdb_putback_char(connection_t *connection, int last_char)
247 gdb_connection_t *gdb_con = connection->priv;
249 if (gdb_con->buf_p > gdb_con->buffer)
251 *(--gdb_con->buf_p) = last_char;
256 LOG_ERROR("BUG: couldn't put character back");
262 /* The only way we can detect that the socket is closed is the first time
263 * we write to it, we will fail. Subsequent write operations will
264 * succeed. Shudder! */
265 int gdb_write(connection_t *connection, void *data, int len)
267 gdb_connection_t *gdb_con = connection->priv;
269 return ERROR_SERVER_REMOTE_CLOSED;
271 if (write_socket(connection->fd, data, len) == len)
276 return ERROR_SERVER_REMOTE_CLOSED;
279 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
282 unsigned char my_checksum = 0;
283 #ifdef _DEBUG_GDB_IO_
288 gdb_connection_t *gdb_con = connection->priv;
290 for (i = 0; i < len; i++)
291 my_checksum += buffer[i];
293 #ifdef _DEBUG_GDB_IO_
295 * At this point we should have nothing in the input queue from GDB,
296 * however sometimes '-' is sent even though we've already received
297 * an ACK (+) for everything we've sent off.
302 if ((retval=check_pending(connection, 0, &gotdata))!=ERROR_OK)
306 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
309 // fix a problem with some IAR tools
310 gdb_putback_char( connection, reply );
311 LOG_DEBUG("Unexpected start of new packet");
315 LOG_WARNING("Discard unexpected char %c", reply);
321 #ifdef _DEBUG_GDB_IO_
322 debug_buffer = malloc(len + 1);
323 memcpy(debug_buffer, buffer, len);
324 debug_buffer[len] = 0;
325 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
329 char local_buffer[1024];
330 local_buffer[0] = '$';
331 if (len+4 <= sizeof(local_buffer))
333 /* performance gain on smaller packets by only a single call to gdb_write() */
334 memcpy(local_buffer+1, buffer, len++);
335 local_buffer[len++] = '#';
336 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
337 local_buffer[len++] = DIGITS[my_checksum & 0xf];
338 if((retval = gdb_write(connection, local_buffer, len)) != ERROR_OK)
345 /* larger packets are transmitted directly from caller supplied buffer
346 by several calls to gdb_write() to avoid dynamic allocation */
347 local_buffer[1] = '#';
348 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
349 local_buffer[3] = DIGITS[my_checksum & 0xf];
350 if((retval = gdb_write(connection, local_buffer, 1)) != ERROR_OK)
354 if((retval = gdb_write(connection, buffer, len)) != ERROR_OK)
358 if((retval = gdb_write(connection, local_buffer+1, 3)) != ERROR_OK)
364 if (gdb_con->noack_mode)
367 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
372 else if (reply == '-')
374 /* Stop sending output packets for now */
375 log_remove_callback(gdb_log_callback, connection);
376 LOG_WARNING("negative reply, retrying");
378 else if (reply == 0x3)
381 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
385 else if (reply == '-')
387 /* Stop sending output packets for now */
388 log_remove_callback(gdb_log_callback, connection);
389 LOG_WARNING("negative reply, retrying");
391 else if( reply == '$' ){
392 LOG_ERROR("GDB missing ack(1) - assumed good");
393 gdb_putback_char( connection, reply );
397 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
399 return ERROR_SERVER_REMOTE_CLOSED;
402 else if( reply == '$' ){
403 LOG_ERROR("GDB missing ack(2) - assumed good");
404 gdb_putback_char( connection, reply );
409 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply);
411 return ERROR_SERVER_REMOTE_CLOSED;
415 return ERROR_SERVER_REMOTE_CLOSED;
420 int gdb_put_packet(connection_t *connection, char *buffer, int len)
422 gdb_connection_t *gdb_con = connection->priv;
424 int retval = gdb_put_packet_inner(connection, buffer, len);
427 /* we sent some data, reset timer for keep alive messages */
433 static __inline__ int fetch_packet(connection_t *connection, int *checksum_ok, int noack, int *len, char *buffer)
435 unsigned char my_checksum = 0;
440 gdb_connection_t *gdb_con = connection->priv;
446 /* The common case is that we have an entire packet with no escape chars.
447 * We need to leave at least 2 bytes in the buffer to have
448 * gdb_get_char() update various bits and bobs correctly.
450 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
452 /* The compiler will struggle a bit with constant propagation and
453 * aliasing, so we help it by showing that these values do not
454 * change inside the loop
457 char *buf = gdb_con->buf_p;
458 int run = gdb_con->buf_cnt - 2;
465 if (character == '#')
467 /* Danger! character can be '#' when esc is
468 * used so we need an explicit boolean for done here.
474 if (character == '}')
476 /* data transmitted in binary mode (X packet)
477 * uses 0x7d as escape character */
478 my_checksum += character & 0xff;
481 my_checksum += character & 0xff;
482 buffer[count++] = (character ^ 0x20) & 0xff;
486 my_checksum += character & 0xff;
487 buffer[count++] = character & 0xff;
491 gdb_con->buf_cnt -= i;
497 LOG_ERROR("packet buffer too small");
498 return ERROR_GDB_BUFFER_TOO_SMALL;
501 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
504 if (character == '#')
507 if (character == '}')
509 /* data transmitted in binary mode (X packet)
510 * uses 0x7d as escape character */
511 my_checksum += character & 0xff;
512 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
514 my_checksum += character & 0xff;
515 buffer[count++] = (character ^ 0x20) & 0xff;
519 my_checksum += character & 0xff;
520 buffer[count++] = character & 0xff;
526 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
528 checksum[0] = character;
529 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
531 checksum[1] = character;
536 *checksum_ok=(my_checksum == strtoul(checksum, NULL, 16));
542 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
546 gdb_connection_t *gdb_con = connection->priv;
552 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
555 #ifdef _DEBUG_GDB_IO_
556 LOG_DEBUG("character: '%c'", character);
564 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
565 * incase anyone tries to debug why they receive this warning every time */
566 LOG_WARNING("acknowledgment received, but no packet pending");
569 LOG_WARNING("negative acknowledgment, but no packet pending");
576 LOG_WARNING("ignoring character 0x%x", character);
579 } while (character != '$');
584 /* explicit code expansion here to get faster inlined code in -O3 by not
585 * calculating checksum
587 if (gdb_con->noack_mode)
589 if ((retval=fetch_packet(connection, &checksum_ok, 1, len, buffer))!=ERROR_OK)
593 if ((retval=fetch_packet(connection, &checksum_ok, 0, len, buffer))!=ERROR_OK)
597 if (gdb_con->noack_mode)
599 /* checksum is not checked in noack mode */
604 if ((retval = gdb_write(connection, "+", 1)) != ERROR_OK)
612 return ERROR_SERVER_REMOTE_CLOSED;
617 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
619 gdb_connection_t *gdb_con = connection->priv;
621 int retval = gdb_get_packet_inner(connection, buffer, len);
626 int gdb_output_con(connection_t *connection, const char* line)
631 bin_size = strlen(line);
633 hex_buffer = malloc(bin_size*2 + 2);
634 if (hex_buffer == NULL)
635 return ERROR_GDB_BUFFER_TOO_SMALL;
638 for (i=0; i<bin_size; i++)
639 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
640 hex_buffer[bin_size*2+1] = 0;
642 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
648 int gdb_output(struct command_context_s *context, const char* line)
650 /* this will be dumped to the log and also sent as an O packet if possible */
651 LOG_USER_N("%s", line);
656 static void gdb_frontend_halted(struct target_s *target, connection_t *connection)
658 gdb_connection_t *gdb_connection = connection->priv;
660 /* In the GDB protocol when we are stepping or coninuing execution,
661 * we have a lingering reply. Upon receiving a halted event
662 * when we have that lingering packet, we reply to the original
663 * step or continue packet.
665 * Executing monitor commands can bring the target in and
666 * out of the running state so we'll see lots of TARGET_EVENT_XXX
667 * that are to be ignored.
669 if (gdb_connection->frontend_state == TARGET_RUNNING)
673 /* stop forwarding log packets! */
674 log_remove_callback(gdb_log_callback, connection);
676 if (gdb_connection->ctrl_c)
679 gdb_connection->ctrl_c = 0;
683 signal = gdb_last_signal(target);
686 snprintf(sig_reply, 4, "T%2.2x", signal);
687 gdb_put_packet(connection, sig_reply, 3);
688 gdb_connection->frontend_state = TARGET_HALTED;
692 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
695 connection_t *connection = priv;
697 target_handle_event( target, event );
700 case TARGET_EVENT_EARLY_HALTED:
701 gdb_frontend_halted(target, connection);
703 case TARGET_EVENT_GDB_FLASH_ERASE_START:
704 target_handle_event( target, TARGET_EVENT_OLD_gdb_program_config );
705 if((retval = jtag_execute_queue()) != ERROR_OK)
718 int gdb_new_connection(connection_t *connection)
720 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
721 gdb_service_t *gdb_service = connection->service->priv;
725 connection->priv = gdb_connection;
727 /* initialize gdb connection information */
728 gdb_connection->buf_p = gdb_connection->buffer;
729 gdb_connection->buf_cnt = 0;
730 gdb_connection->ctrl_c = 0;
731 gdb_connection->frontend_state = TARGET_HALTED;
732 gdb_connection->vflash_image = NULL;
733 gdb_connection->closed = 0;
734 gdb_connection->busy = 0;
735 gdb_connection->noack_mode = 0;
737 /* send ACK to GDB for debug request */
738 gdb_write(connection, "+", 1);
740 /* output goes through gdb connection */
741 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
743 /* we must remove all breakpoints registered to the target as a previous
744 * GDB session could leave dangling breakpoints if e.g. communication
747 breakpoint_clear_target(gdb_service->target);
748 watchpoint_clear_target(gdb_service->target);
750 /* register callback to be informed about target events */
751 target_register_event_callback(gdb_target_callback_event_handler, connection);
753 /* a gdb session just attached, try to put the target in halt mode.
757 * If the halt fails(e.g. target needs a reset, JTAG communication not
758 * working, etc.), then the GDB connect will succeed as
759 * the get_gdb_reg_list() will lie and return a register list with
762 * This allows GDB monitor commands to be run from a GDB init script to
763 * initialize the target
765 * Also, since the halt() is asynchronous target connect will be
766 * instantaneous and thus avoiding annoying timeout problems during
769 target_halt(gdb_service->target);
770 /* FIX!!!! could extended-remote work better here?
772 * wait a tiny bit for halted state or we just continue. The
773 * GDB register packet will then contain garbage
775 target_wait_state(gdb_service->target, TARGET_HALTED, 500);
777 /* remove the initial ACK from the incoming buffer */
778 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
781 /* FIX!!!??? would we actually ever receive a + here???
784 if (initial_ack != '+')
785 gdb_putback_char(connection, initial_ack);
786 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH );
790 void gdb_connection_closed(connection_t *connection)
792 gdb_service_t *gdb_service = connection->service->priv;
793 gdb_connection_t *gdb_connection = connection->priv;
795 /* see if an image built with vFlash commands is left */
796 if (gdb_connection->vflash_image)
798 image_close(gdb_connection->vflash_image);
799 free(gdb_connection->vflash_image);
800 gdb_connection->vflash_image = NULL;
803 /* if this connection registered a debug-message receiver delete it */
804 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
806 if (connection->priv)
808 free(connection->priv);
809 connection->priv = NULL;
813 LOG_ERROR("BUG: connection->priv == NULL");
816 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
817 log_remove_callback(gdb_log_callback, connection);
819 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH );
822 void gdb_send_error(connection_t *connection, u8 the_error)
825 snprintf(err, 4, "E%2.2X", the_error );
826 gdb_put_packet(connection, err, 3);
829 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
834 signal = gdb_last_signal(target);
836 snprintf(sig_reply, 4, "S%2.2x", signal);
837 gdb_put_packet(connection, sig_reply, 3);
842 /* Convert register to string of bits. NB! The # of bits in the
843 * register might be non-divisible by 8(a byte), in which
844 * case an entire byte is shown. */
845 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
852 buf_len = CEIL(reg->size, 8);
854 for (i = 0; i < buf_len; i++)
856 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
857 tstr[i*2+1] = DIGITS[buf[i]&0xf];
861 void gdb_target_to_str(target_t *target, char *tstr, char *str)
863 int str_len = strlen(tstr);
868 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
872 for (i = 0; i < str_len; i+=2)
874 str[str_len - i - 1] = tstr[i + 1];
875 str[str_len - i - 2] = tstr[i];
879 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
884 int reg_packet_size = 0;
889 #ifdef _DEBUG_GDB_IO_
893 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
895 return gdb_error(connection, retval);
898 for (i = 0; i < reg_list_size; i++)
900 reg_packet_size += reg_list[i]->size;
903 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
904 reg_packet_p = reg_packet;
906 for (i = 0; i < reg_list_size; i++)
908 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
909 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
912 #ifdef _DEBUG_GDB_IO_
915 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
916 LOG_DEBUG("reg_packet: %s", reg_packet_p);
921 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
929 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
937 #ifdef _DEBUG_GDB_IO_
941 /* skip command character */
947 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
948 return ERROR_SERVER_REMOTE_CLOSED;
951 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
953 return gdb_error(connection, retval);
957 for (i = 0; i < reg_list_size; i++)
961 reg_arch_type_t *arch_type;
963 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
964 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
965 gdb_target_to_str(target, packet_p, hex_buf);
967 /* convert hex-string to binary buffer */
968 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
969 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
971 /* get register arch_type, and call set method */
972 arch_type = register_get_arch_type(reg_list[i]->arch_type);
974 arch_type->set(reg_list[i], bin_buf);
976 /* advance packet pointer */
977 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
983 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
986 gdb_put_packet(connection, "OK", 2);
991 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
994 int reg_num = strtoul(packet + 1, NULL, 16);
999 #ifdef _DEBUG_GDB_IO_
1003 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1005 return gdb_error(connection, retval);
1008 if (reg_list_size <= reg_num)
1010 LOG_ERROR("gdb requested a non-existing register");
1014 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1016 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1018 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
1026 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1031 int reg_num = strtoul(packet + 1, &separator, 16);
1035 reg_arch_type_t *arch_type;
1039 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
1041 return gdb_error(connection, retval);
1044 if (reg_list_size < reg_num)
1046 LOG_ERROR("gdb requested a non-existing register");
1047 return ERROR_SERVER_REMOTE_CLOSED;
1050 if (*separator != '=')
1052 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1053 return ERROR_SERVER_REMOTE_CLOSED;
1056 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1057 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
1058 gdb_target_to_str(target, separator + 1, hex_buf);
1060 /* convert hex-string to binary buffer */
1061 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
1062 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
1064 /* get register arch_type, and call set method */
1065 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
1066 arch_type->set(reg_list[reg_num], bin_buf);
1068 gdb_put_packet(connection, "OK", 2);
1077 int gdb_error(connection_t *connection, int retval)
1081 case ERROR_TARGET_DATA_ABORT:
1082 gdb_send_error(connection, EIO);
1084 case ERROR_TARGET_TRANSLATION_FAULT:
1085 gdb_send_error(connection, EFAULT);
1087 case ERROR_TARGET_UNALIGNED_ACCESS:
1088 gdb_send_error(connection, EFAULT);
1090 case ERROR_TARGET_NOT_HALTED:
1091 gdb_send_error(connection, EFAULT);
1094 /* This could be that the target reset itself. */
1095 LOG_ERROR("unexpected error %i", retval);
1096 gdb_send_error(connection, EFAULT);
1103 /* We don't have to worry about the default 2 second timeout for GDB packets,
1104 * because GDB breaks up large memory reads into smaller reads.
1106 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1108 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1117 int retval = ERROR_OK;
1119 /* skip command character */
1122 addr = strtoul(packet, &separator, 16);
1124 if (*separator != ',')
1126 LOG_ERROR("incomplete read memory packet received, dropping connection");
1127 return ERROR_SERVER_REMOTE_CLOSED;
1130 len = strtoul(separator+1, NULL, 16);
1132 buffer = malloc(len);
1134 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1136 retval = target_read_buffer(target, addr, len, buffer);
1138 if ((retval == ERROR_TARGET_DATA_ABORT) && (!gdb_report_data_abort))
1140 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1141 * At some point this might be fixed in GDB, in which case this code can be removed.
1143 * OpenOCD developers are acutely aware of this problem, but there is nothing
1144 * gained by involving the user in this problem that hopefully will get resolved
1147 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1149 * For now, the default is to fix up things to make current GDB versions work.
1150 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1152 memset(buffer, 0, len);
1156 if (retval == ERROR_OK)
1158 hex_buffer = malloc(len * 2 + 1);
1161 for (i = 0; i < len; i++)
1164 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1165 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1168 gdb_put_packet(connection, hex_buffer, len * 2);
1174 retval = gdb_error(connection, retval);
1182 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1193 /* skip command character */
1196 addr = strtoul(packet, &separator, 16);
1198 if (*separator != ',')
1200 LOG_ERROR("incomplete write memory packet received, dropping connection");
1201 return ERROR_SERVER_REMOTE_CLOSED;
1204 len = strtoul(separator+1, &separator, 16);
1206 if (*(separator++) != ':')
1208 LOG_ERROR("incomplete write memory packet received, dropping connection");
1209 return ERROR_SERVER_REMOTE_CLOSED;
1212 buffer = malloc(len);
1214 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1216 for (i=0; i<len; i++)
1219 sscanf(separator + 2*i, "%2x", &tmp);
1223 retval = target_write_buffer(target, addr, len, buffer);
1225 if (retval == ERROR_OK)
1227 gdb_put_packet(connection, "OK", 2);
1231 retval = gdb_error(connection, retval);
1239 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1247 /* skip command character */
1250 addr = strtoul(packet, &separator, 16);
1252 if (*separator != ',')
1254 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1255 return ERROR_SERVER_REMOTE_CLOSED;
1258 len = strtoul(separator+1, &separator, 16);
1260 if (*(separator++) != ':')
1262 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1263 return ERROR_SERVER_REMOTE_CLOSED;
1269 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1271 retval = target_write_buffer(target, addr, len, (u8*)separator);
1274 if (retval == ERROR_OK)
1276 gdb_put_packet(connection, "OK", 2);
1280 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1287 int gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1291 int retval=ERROR_OK;
1295 if (packet_size > 1)
1297 packet[packet_size] = 0;
1298 address = strtoul(packet + 1, NULL, 16);
1305 if (packet[0] == 'c')
1307 LOG_DEBUG("continue");
1308 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1309 retval=target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1311 else if (packet[0] == 's')
1314 retval=target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1319 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1322 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1323 enum watchpoint_rw wp_type;
1331 type = strtoul(packet + 1, &separator, 16);
1333 if (type == 0) /* memory breakpoint */
1334 bp_type = BKPT_SOFT;
1335 else if (type == 1) /* hardware breakpoint */
1336 bp_type = BKPT_HARD;
1337 else if (type == 2) /* write watchpoint */
1338 wp_type = WPT_WRITE;
1339 else if (type == 3) /* read watchpoint */
1341 else if (type == 4) /* access watchpoint */
1342 wp_type = WPT_ACCESS;
1344 if (gdb_breakpoint_override&&((bp_type==BKPT_SOFT)||(bp_type==BKPT_HARD)))
1346 bp_type=gdb_breakpoint_override_type;
1349 if (*separator != ',')
1351 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1352 return ERROR_SERVER_REMOTE_CLOSED;
1355 address = strtoul(separator+1, &separator, 16);
1357 if (*separator != ',')
1359 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1360 return ERROR_SERVER_REMOTE_CLOSED;
1363 size = strtoul(separator+1, &separator, 16);
1369 if (packet[0] == 'Z')
1371 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1373 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1378 gdb_put_packet(connection, "OK", 2);
1383 breakpoint_remove(target, address);
1384 gdb_put_packet(connection, "OK", 2);
1391 if (packet[0] == 'Z')
1393 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1395 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1400 gdb_put_packet(connection, "OK", 2);
1405 watchpoint_remove(target, address);
1406 gdb_put_packet(connection, "OK", 2);
1417 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1418 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1420 if (*retval != ERROR_OK)
1428 if ((*xml == NULL) || (!first))
1430 /* start by 0 to exercise all the code paths.
1431 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1433 *size = *size * 2 + 2;
1435 *xml = realloc(*xml, *size);
1440 *retval = ERROR_SERVER_REMOTE_CLOSED;
1448 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1450 if ((ret > 0) && ((ret + 1) < *size - *pos))
1455 /* there was just enough or not enough space, allocate more. */
1460 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1464 /* Extract and NUL-terminate the annex. */
1466 while (*buf && *buf != ':')
1472 /* After the read marker and annex, qXfer looks like a
1473 * traditional 'm' packet. */
1475 *ofs = strtoul(buf, &separator, 16);
1477 if (*separator != ',')
1480 *len = strtoul(separator+1, NULL, 16);
1485 int gdb_calc_blocksize(flash_bank_t *bank)
1488 int block_size = 0xffffffff;
1490 /* loop through all sectors and return smallest sector size */
1492 for (i = 0; i < bank->num_sectors; i++)
1494 if (bank->sectors[i].size < block_size)
1495 block_size = bank->sectors[i].size;
1501 static int compare_bank (const void * a, const void * b)
1503 flash_bank_t *b1, *b2;
1504 b1=*((flash_bank_t **)a);
1505 b2=*((flash_bank_t **)b);
1507 if (b1->base==b2->base)
1510 } else if (b1->base>b2->base)
1519 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1521 command_context_t *cmd_ctx = connection->cmd_ctx;
1522 gdb_connection_t *gdb_connection = connection->priv;
1524 if (strstr(packet, "qRcmd,"))
1526 if (packet_size > 6)
1530 cmd = malloc((packet_size - 6)/2 + 1);
1531 for (i=0; i < (packet_size - 6)/2; i++)
1534 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1537 cmd[(packet_size - 6)/2] = 0x0;
1539 /* We want to print all debug output to GDB connection */
1540 log_add_callback(gdb_log_callback, connection);
1541 target_call_timer_callbacks_now();
1542 command_run_line(cmd_ctx, cmd);
1543 target_call_timer_callbacks_now();
1544 log_remove_callback(gdb_log_callback, connection);
1547 gdb_put_packet(connection, "OK", 2);
1550 else if (strstr(packet, "qCRC:"))
1552 if (packet_size > 5)
1561 /* skip command character */
1564 addr = strtoul(packet, &separator, 16);
1566 if (*separator != ',')
1568 LOG_ERROR("incomplete read memory packet received, dropping connection");
1569 return ERROR_SERVER_REMOTE_CLOSED;
1572 len = strtoul(separator + 1, NULL, 16);
1574 retval = target_checksum_memory(target, addr, len, &checksum);
1576 if (retval == ERROR_OK)
1578 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1579 gdb_put_packet(connection, gdb_reply, 9);
1583 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1590 else if (strstr(packet, "qSupported"))
1592 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1593 * disable qXfer:features:read for the moment */
1594 int retval = ERROR_OK;
1595 char *buffer = NULL;
1599 xml_printf(&retval, &buffer, &pos, &size,
1600 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1601 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1603 if (retval != ERROR_OK)
1605 gdb_send_error(connection, 01);
1609 gdb_put_packet(connection, buffer, strlen(buffer));
1614 else if (strstr(packet, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1616 /* We get away with only specifying flash here. Regions that are not
1617 * specified are treated as if we provided no memory map(if not we
1618 * could detect the holes and mark them as RAM).
1619 * Normally we only execute this code once, but no big deal if we
1620 * have to regenerate it a couple of times. */
1626 int retval = ERROR_OK;
1633 /* skip command character */
1636 offset = strtoul(packet, &separator, 16);
1637 length = strtoul(separator + 1, &separator, 16);
1639 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1642 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1643 read/write) by default for GDB.
1644 GDB does not have a concept of non-cacheable read/write memory.
1646 flash_bank_t **banks=malloc(sizeof(flash_bank_t *)*flash_get_bank_count());
1649 for (i=0; i<flash_get_bank_count(); i++)
1651 p = get_flash_bank_by_num(i);
1655 retval = ERROR_FAIL;
1656 gdb_send_error(connection, retval);
1662 qsort(banks, flash_get_bank_count(), sizeof(flash_bank_t *), compare_bank);
1665 for (i=0; i<flash_get_bank_count(); i++)
1669 if (ram_start<p->base)
1671 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1672 ram_start, p->base-ram_start);
1675 /* if device has uneven sector sizes, eg. str7, lpc
1676 * we pass the smallest sector size to gdb memory map */
1677 blocksize = gdb_calc_blocksize(p);
1679 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1680 "<property name=\"blocksize\">0x%x</property>\n" \
1682 p->base, p->size, blocksize);
1683 ram_start=p->base+p->size;
1687 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1688 ram_start, 0-ram_start);
1691 /* a flash chip could be at the very end of the 32 bit address space, in which case
1692 ram_start will be precisely 0 */
1698 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1700 if (retval != ERROR_OK)
1702 gdb_send_error(connection, retval);
1706 if (offset + length > pos)
1708 length = pos - offset;
1711 char *t = malloc(length + 1);
1713 memcpy(t + 1, xml + offset, length);
1714 gdb_put_packet(connection, t, length + 1);
1720 else if (strstr(packet, "qXfer:features:read:"))
1725 int retval = ERROR_OK;
1728 unsigned int length;
1731 /* skip command character */
1734 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1736 gdb_send_error(connection, 01);
1740 if (strcmp(annex, "target.xml") != 0)
1742 gdb_send_error(connection, 01);
1746 xml_printf(&retval, &xml, &pos, &size, \
1747 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1749 if (retval != ERROR_OK)
1751 gdb_send_error(connection, retval);
1755 gdb_put_packet(connection, xml, strlen(xml));
1760 else if (strstr(packet, "QStartNoAckMode"))
1762 gdb_connection->noack_mode = 1;
1763 gdb_put_packet(connection, "OK", 2);
1767 gdb_put_packet(connection, "", 0);
1771 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1773 gdb_connection_t *gdb_connection = connection->priv;
1774 gdb_service_t *gdb_service = connection->service->priv;
1777 /* if flash programming disabled - send a empty reply */
1779 if (gdb_flash_program == 0)
1781 gdb_put_packet(connection, "", 0);
1785 if (strstr(packet, "vFlashErase:"))
1788 unsigned long length;
1790 char *parse = packet + 12;
1793 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1794 return ERROR_SERVER_REMOTE_CLOSED;
1797 addr = strtoul(parse, &parse, 16);
1799 if (*(parse++) != ',' || *parse == '\0')
1801 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1802 return ERROR_SERVER_REMOTE_CLOSED;
1805 length = strtoul(parse, &parse, 16);
1809 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1810 return ERROR_SERVER_REMOTE_CLOSED;
1813 /* assume all sectors need erasing - stops any problems
1814 * when flash_write is called multiple times */
1817 /* perform any target specific operations before the erase */
1818 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1819 result = flash_erase_address_range(gdb_service->target, addr, length );
1820 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
1823 if (result != ERROR_OK)
1825 /* GDB doesn't evaluate the actual error number returned,
1826 * treat a failed erase as an I/O error
1828 gdb_send_error(connection, EIO);
1829 LOG_ERROR("flash_erase returned %i", result);
1832 gdb_put_packet(connection, "OK", 2);
1837 if (strstr(packet, "vFlashWrite:"))
1841 unsigned long length;
1842 char *parse = packet + 12;
1846 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1847 return ERROR_SERVER_REMOTE_CLOSED;
1849 addr = strtoul(parse, &parse, 16);
1850 if (*(parse++) != ':')
1852 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1853 return ERROR_SERVER_REMOTE_CLOSED;
1855 length = packet_size - (parse - packet);
1857 /* create a new image if there isn't already one */
1858 if (gdb_connection->vflash_image == NULL)
1860 gdb_connection->vflash_image = malloc(sizeof(image_t));
1861 image_open(gdb_connection->vflash_image, "", "build");
1864 /* create new section with content from packet buffer */
1865 if((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse)) != ERROR_OK)
1870 gdb_put_packet(connection, "OK", 2);
1875 if (!strcmp(packet, "vFlashDone"))
1879 /* process the flashing buffer. No need to erase as GDB
1880 * always issues a vFlashErase first. */
1881 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
1882 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1883 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1884 if ( result != ERROR_OK)
1886 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1887 gdb_put_packet(connection, "E.memtype", 9);
1889 gdb_send_error(connection, EIO);
1893 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written);
1894 gdb_put_packet(connection, "OK", 2);
1897 image_close(gdb_connection->vflash_image);
1898 free(gdb_connection->vflash_image);
1899 gdb_connection->vflash_image = NULL;
1904 gdb_put_packet(connection, "", 0);
1908 int gdb_detach(connection_t *connection, target_t *target)
1911 switch( detach_mode )
1913 case GDB_DETACH_RESUME:
1914 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1915 target_resume(target, 1, 0, 1, 0);
1918 case GDB_DETACH_RESET:
1919 /* FIX?? make this configurable?? */
1920 target_process_reset(connection->cmd_ctx, RESET_HALT);
1923 case GDB_DETACH_HALT:
1924 target_halt(target);
1927 case GDB_DETACH_NOTHING:
1931 gdb_put_packet(connection, "OK", 2);
1935 static void gdb_log_callback(void *priv, const char *file, int line,
1936 const char *function, const char *string)
1938 connection_t *connection = priv;
1939 gdb_connection_t *gdb_con = connection->priv;
1943 /* do not reply this using the O packet */
1947 gdb_output_con(connection, string);
1950 /* Do not allocate this on the stack */
1951 char gdb_packet_buffer[GDB_BUFFER_SIZE];
1953 static void gdb_sig_halted(connection_t *connection)
1956 snprintf(sig_reply, 4, "T%2.2x", 2);
1957 gdb_put_packet(connection, sig_reply, 3);
1961 int gdb_input_inner(connection_t *connection)
1963 gdb_service_t *gdb_service = connection->service->priv;
1964 target_t *target = gdb_service->target;
1965 char *packet=gdb_packet_buffer;
1968 gdb_connection_t *gdb_con = connection->priv;
1969 static int extended_protocol = 0;
1971 /* drain input buffer */
1974 packet_size = GDB_BUFFER_SIZE-1;
1975 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1980 /* terminate with zero */
1981 packet[packet_size] = 0;
1983 if( LOG_LEVEL_IS( LOG_LVL_DEBUG ) ){
1984 if( packet[0] == 'X' ){
1985 // binary packets spew junk into the debug log stream
1988 for( x = 0 ; (x < 49) && (packet[x] != ':') ; x++ ){
1992 LOG_DEBUG("received packet: '%s:<binary-data>'", buf );
1994 LOG_DEBUG("received packet: '%s'", packet );
1998 if (packet_size > 0)
2004 /* Hct... -- set thread
2005 * we don't have threads, send empty reply */
2006 gdb_put_packet(connection, NULL, 0);
2010 retval = gdb_query_packet(connection, target, packet, packet_size);
2013 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
2016 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
2019 retval = gdb_get_register_packet(connection, target, packet, packet_size);
2022 retval = gdb_set_register_packet(connection, target, packet, packet_size);
2025 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
2028 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
2032 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
2035 gdb_last_signal_packet(connection, target, packet, packet_size);
2040 if (target->state != TARGET_HALTED)
2042 /* If the target isn't in the halted state, then we can't
2043 * step/continue. This might be early setup, etc.
2045 gdb_sig_halted(connection);
2048 /* We're running/stepping, in which case we can
2049 * forward log output until the target is halted
2051 gdb_connection_t *gdb_con = connection->priv;
2052 gdb_con->frontend_state = TARGET_RUNNING;
2053 log_add_callback(gdb_log_callback, connection);
2054 int retval=gdb_step_continue_packet(connection, target, packet, packet_size);
2055 if (retval!=ERROR_OK)
2057 /* we'll never receive a halted condition... issue a false one.. */
2058 gdb_frontend_halted(target, connection);
2064 retval = gdb_v_packet(connection, target, packet, packet_size);
2067 retval = gdb_detach(connection, target);
2068 extended_protocol = 0;
2071 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2075 if (extended_protocol != 0)
2077 gdb_put_packet(connection, "OK", 2);
2078 return ERROR_SERVER_REMOTE_CLOSED;
2080 /* handle extended remote protocol */
2081 extended_protocol = 1;
2082 gdb_put_packet(connection, "OK", 2);
2085 /* handle extended restart packet */
2086 breakpoint_clear_target(gdb_service->target);
2087 watchpoint_clear_target(gdb_service->target);
2088 command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %d", get_num_by_target(target));
2091 /* ignore unkown packets */
2092 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2093 gdb_put_packet(connection, NULL, 0);
2097 /* if a packet handler returned an error, exit input loop */
2098 if (retval != ERROR_OK)
2102 if (gdb_con->ctrl_c)
2104 if (target->state == TARGET_RUNNING)
2106 target_halt(target);
2107 gdb_con->ctrl_c = 0;
2111 } while (gdb_con->buf_cnt > 0);
2116 int gdb_input(connection_t *connection)
2118 int retval = gdb_input_inner(connection);
2119 gdb_connection_t *gdb_con = connection->priv;
2120 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2123 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2124 if (gdb_con->closed)
2125 return ERROR_SERVER_REMOTE_CLOSED;
2127 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2133 gdb_service_t *gdb_service;
2134 target_t *target = all_targets;
2138 LOG_WARNING("no gdb ports allocated as no target has been specified");
2144 LOG_WARNING("no gdb port specified, using default port 3333");
2150 char service_name[8];
2152 snprintf(service_name, 8, "gdb-%2.2i", target->target_number);
2154 gdb_service = malloc(sizeof(gdb_service_t));
2155 gdb_service->target = target;
2157 add_service("gdb", CONNECTION_GDB,
2158 gdb_port + target->target_number,
2159 1, gdb_new_connection, gdb_input,
2160 gdb_connection_closed,
2163 LOG_DEBUG("gdb service for target %s at port %i",
2165 gdb_port + target->target_number);
2167 target = target->next;
2173 /* daemon configuration command gdb_port */
2174 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2179 /* only if the port wasn't overwritten by cmdline */
2181 gdb_port = strtoul(args[0], NULL, 0);
2186 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2190 if (strcmp(args[0], "resume") == 0)
2192 detach_mode = GDB_DETACH_RESUME;
2195 else if (strcmp(args[0], "reset") == 0)
2197 detach_mode = GDB_DETACH_RESET;
2200 else if (strcmp(args[0], "halt") == 0)
2202 detach_mode = GDB_DETACH_HALT;
2205 else if (strcmp(args[0], "nothing") == 0)
2207 detach_mode = GDB_DETACH_NOTHING;
2212 LOG_WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2216 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2220 if (strcmp(args[0], "enable") == 0)
2222 gdb_use_memory_map = 1;
2225 else if (strcmp(args[0], "disable") == 0)
2227 gdb_use_memory_map = 0;
2232 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2236 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2240 if (strcmp(args[0], "enable") == 0)
2242 gdb_flash_program = 1;
2245 else if (strcmp(args[0], "disable") == 0)
2247 gdb_flash_program = 0;
2252 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2256 int handle_gdb_report_data_abort_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2260 if (strcmp(args[0], "enable") == 0)
2262 gdb_report_data_abort = 1;
2265 else if (strcmp(args[0], "disable") == 0)
2267 gdb_report_data_abort = 0;
2272 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2276 /* daemon configuration command gdb_port */
2277 int handle_gdb_breakpoint_override_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2284 gdb_breakpoint_override = 1;
2285 if (strcmp(args[0], "hard")==0)
2287 gdb_breakpoint_override_type=BKPT_HARD;
2288 } else if (strcmp(args[0], "soft")==0)
2290 gdb_breakpoint_override_type=BKPT_SOFT;
2291 } else if (strcmp(args[0], "disable") == 0)
2293 gdb_breakpoint_override = 0;
2297 return ERROR_COMMAND_SYNTAX_ERROR;
2299 if (gdb_breakpoint_override)
2301 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type==BKPT_HARD)?"hard":"soft");
2304 LOG_USER("breakpoint type is not overriden");
2311 int gdb_register_commands(command_context_t *command_context)
2313 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2314 COMMAND_CONFIG, "");
2315 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2316 COMMAND_CONFIG, "");
2317 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2318 COMMAND_CONFIG, "");
2319 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2320 COMMAND_CONFIG, "");
2321 register_command(command_context, NULL, "gdb_report_data_abort", handle_gdb_report_data_abort_command,
2322 COMMAND_CONFIG, "");
2323 register_command(command_context, NULL, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command,
2324 COMMAND_EXEC, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2325 "The raison d'etre for this option is to support GDB GUI's without "
2326 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2327 "is not sufficient");