1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
26 #include "gdb_server.h"
30 #include "binarybuffer.h"
32 #include "breakpoints.h"
34 #include "target_request.h"
42 #define _DEBUG_GDB_IO_
45 static unsigned short gdb_port;
47 static void gdb_log_callback(void *privData, const char *file, int line,
48 const char *function, const char *format, va_list args);
59 /* target behaviour on gdb detach */
60 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
62 /* set if we are sending a memory map to gdb
63 * via qXfer:memory-map:read packet */
64 int gdb_use_memory_map = 0;
65 int gdb_flash_program = 0;
67 int gdb_last_signal(target_t *target)
69 switch (target->debug_reason)
71 case DBG_REASON_DBGRQ:
72 return 0x2; /* SIGINT */
73 case DBG_REASON_BREAKPOINT:
74 case DBG_REASON_WATCHPOINT:
75 case DBG_REASON_WPTANDBKPT:
76 return 0x05; /* SIGTRAP */
77 case DBG_REASON_SINGLESTEP:
78 return 0x05; /* SIGTRAP */
79 case DBG_REASON_NOTHALTED:
80 return 0x0; /* no signal... shouldn't happen */
82 ERROR("BUG: undefined debug reason");
87 int gdb_get_char(connection_t *connection, int* next_char)
89 gdb_connection_t *gdb_con = connection->priv;
95 if (gdb_con->buf_cnt-- > 0)
97 *next_char = *(gdb_con->buf_p++);
98 if (gdb_con->buf_cnt > 0)
99 connection->input_pending = 1;
101 connection->input_pending = 0;
103 #ifdef _DEBUG_GDB_IO_
104 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
110 while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
112 if (gdb_con->buf_cnt == 0)
113 return ERROR_SERVER_REMOTE_CLOSED;
116 errno = WSAGetLastError();
123 case WSAECONNABORTED:
124 return ERROR_SERVER_REMOTE_CLOSED;
126 return ERROR_SERVER_REMOTE_CLOSED;
128 ERROR("read: %d", errno);
138 return ERROR_SERVER_REMOTE_CLOSED;
140 return ERROR_SERVER_REMOTE_CLOSED;
142 ERROR("read: %s", strerror(errno));
148 #ifdef _DEBUG_GDB_IO_
149 debug_buffer = malloc(gdb_con->buf_cnt + 1);
150 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
151 debug_buffer[gdb_con->buf_cnt] = 0;
152 DEBUG("received '%s'", debug_buffer);
156 gdb_con->buf_p = gdb_con->buffer;
158 *next_char = *(gdb_con->buf_p++);
159 if (gdb_con->buf_cnt > 0)
160 connection->input_pending = 1;
162 connection->input_pending = 0;
163 #ifdef _DEBUG_GDB_IO_
164 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
170 int gdb_putback_char(connection_t *connection, int last_char)
172 gdb_connection_t *gdb_con = connection->priv;
174 if (gdb_con->buf_p > gdb_con->buffer)
176 *(--gdb_con->buf_p) = last_char;
181 ERROR("BUG: couldn't put character back");
187 int gdb_put_packet_inner(connection_t *connection, char *buffer, int len)
190 unsigned char my_checksum = 0;
192 #ifdef _DEBUG_GDB_IO_
197 gdb_connection_t *gdb_con = connection->priv;
199 for (i = 0; i < len; i++)
200 my_checksum += buffer[i];
204 #ifdef _DEBUG_GDB_IO_
205 debug_buffer = malloc(len + 1);
206 memcpy(debug_buffer, buffer, len);
207 debug_buffer[len] = 0;
208 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
211 write_socket(connection->fd, "$", 1);
213 write_socket(connection->fd, buffer, len);
214 write_socket(connection->fd, "#", 1);
216 snprintf(checksum, 3, "%2.2x", my_checksum);
218 write_socket(connection->fd, checksum, 2);
220 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
225 else if (reply == '-')
226 WARNING("negative reply, retrying");
227 else if (reply == 0x3)
230 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
234 else if (reply == '-')
235 WARNING("negative reply, retrying");
238 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
239 return ERROR_SERVER_REMOTE_CLOSED;
244 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
245 return ERROR_SERVER_REMOTE_CLOSED;
252 int gdb_put_packet(connection_t *connection, char *buffer, int len)
254 gdb_connection_t *gdb_connection = connection->priv;
255 gdb_connection->output_disable=1;
256 int retval=gdb_put_packet_inner(connection, buffer, len);
257 gdb_connection->output_disable=0;
261 int gdb_get_packet_inner(connection_t *connection, char *buffer, int *len)
267 unsigned char my_checksum = 0;
268 gdb_connection_t *gdb_con = connection->priv;
274 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
277 #ifdef _DEBUG_GDB_IO_
278 DEBUG("character: '%c'", character);
286 WARNING("acknowledgment received, but no packet pending");
289 WARNING("negative acknowledgment, but no packet pending");
296 WARNING("ignoring character 0x%x", character);
299 } while (character != '$');
304 gdb_connection_t *gdb_con = connection->priv;
307 /* The common case is that we have an entire packet with no escape chars.
308 * We need to leave at least 2 bytes in the buffer to have
309 * gdb_get_char() update various bits and bobs correctly.
311 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt+count) < *len))
313 /* The compiler will struggle a bit with constant propagation and
314 * aliasing, so we help it by showing that these values do not
315 * change inside the loop
318 char *buf = gdb_con->buf_p;
319 int run = gdb_con->buf_cnt - 2;
326 if (character == '#')
328 /* Danger! character can be '#' when esc is
329 * used so we need an explicit boolean for done here.
335 if (character == '}')
337 /* data transmitted in binary mode (X packet)
338 * uses 0x7d as escape character */
339 my_checksum += character & 0xff;
342 my_checksum += character & 0xff;
343 buffer[count++] = (character ^ 0x20) & 0xff;
346 my_checksum += character & 0xff;
347 buffer[count++] = character & 0xff;
351 gdb_con->buf_cnt -= i;
357 ERROR("packet buffer too small");
358 return ERROR_GDB_BUFFER_TOO_SMALL;
361 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
364 if (character == '#')
367 if (character == '}')
369 /* data transmitted in binary mode (X packet)
370 * uses 0x7d as escape character */
371 my_checksum += character & 0xff;
372 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
374 my_checksum += character & 0xff;
375 buffer[count++] = (character ^ 0x20) & 0xff;
379 my_checksum += character & 0xff;
380 buffer[count++] = character & 0xff;
387 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
389 checksum[0] = character;
390 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
392 checksum[1] = character;
395 if (my_checksum == strtoul(checksum, NULL, 16))
397 write_socket(connection->fd, "+", 1);
401 WARNING("checksum error, requesting retransmission");
402 write_socket(connection->fd, "-", 1);
408 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
410 gdb_connection_t *gdb_connection = connection->priv;
411 gdb_connection->output_disable=1;
412 int retval=gdb_get_packet_inner(connection, buffer, len);
413 gdb_connection->output_disable=0;
417 int gdb_output_con(connection_t *connection, char* line)
419 gdb_connection_t *gdb_connection = connection->priv;
423 /* check if output is enabled */
424 if (gdb_connection->output_disable)
429 bin_size = strlen(line);
431 hex_buffer = malloc(bin_size*2 + 4);
434 for (i=0; i<bin_size; i++)
435 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
436 hex_buffer[bin_size*2+1] = '0';
437 hex_buffer[bin_size*2+2] = 'a';
438 hex_buffer[bin_size*2+3] = 0x0;
440 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
446 int gdb_output(struct command_context_s *context, char* line)
448 connection_t *connection = context->output_handler_priv;
449 return gdb_output_con(connection, line);
452 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
455 struct command_context_s *cmd_ctx = priv;
457 if (target->gdb_program_script)
459 script = fopen(target->gdb_program_script, "r");
462 ERROR("couldn't open script file %s", target->gdb_program_script);
466 INFO("executing gdb_program script '%s'", target->gdb_program_script);
467 command_run_file(cmd_ctx, script, COMMAND_EXEC);
470 jtag_execute_queue();
476 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
478 connection_t *connection = priv;
479 gdb_connection_t *gdb_connection = connection->priv;
485 case TARGET_EVENT_HALTED:
486 if (gdb_connection->frontend_state == TARGET_RUNNING)
488 // stop forwarding log packets!
489 log_setCallback(NULL, NULL);
491 if (gdb_connection->ctrl_c)
494 gdb_connection->ctrl_c = 0;
498 signal = gdb_last_signal(target);
501 snprintf(sig_reply, 4, "T%2.2x", signal);
502 gdb_put_packet(connection, sig_reply, 3);
503 gdb_connection->frontend_state = TARGET_HALTED;
506 case TARGET_EVENT_RESUMED:
507 if (gdb_connection->frontend_state == TARGET_HALTED)
509 gdb_connection->frontend_state = TARGET_RUNNING;
512 case TARGET_EVENT_GDB_PROGRAM:
513 gdb_program_handler(target, event, connection->cmd_ctx);
522 int gdb_new_connection(connection_t *connection)
524 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
525 gdb_service_t *gdb_service = connection->service->priv;
529 connection->priv = gdb_connection;
531 /* initialize gdb connection information */
532 gdb_connection->buf_p = gdb_connection->buffer;
533 gdb_connection->buf_cnt = 0;
534 gdb_connection->ctrl_c = 0;
535 gdb_connection->frontend_state = TARGET_HALTED;
536 gdb_connection->vflash_image = NULL;
537 gdb_connection->output_disable = 0;
539 /* output goes through gdb connection */
540 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
542 /* register callback to be informed about target events */
543 target_register_event_callback(gdb_target_callback_event_handler, connection);
545 /* a gdb session just attached, put the target in halt mode */
546 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
547 (retval != ERROR_TARGET_ALREADY_HALTED))
549 ERROR("error when trying to halt target");
553 while (gdb_service->target->state != TARGET_HALTED)
555 gdb_service->target->type->poll(gdb_service->target);
558 /* remove the initial ACK from the incoming buffer */
559 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
562 if (initial_ack != '+')
563 gdb_putback_char(connection, initial_ack);
568 int gdb_connection_closed(connection_t *connection)
570 gdb_service_t *gdb_service = connection->service->priv;
571 gdb_connection_t *gdb_connection = connection->priv;
573 /* see if an image built with vFlash commands is left */
574 if (gdb_connection->vflash_image)
576 image_close(gdb_connection->vflash_image);
577 free(gdb_connection->vflash_image);
578 gdb_connection->vflash_image = NULL;
581 /* if this connection registered a debug-message receiver delete it */
582 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
584 if (connection->priv)
586 free(connection->priv);
587 connection->priv = NULL;
591 ERROR("BUG: connection->priv == NULL");
594 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
599 void gdb_send_error(connection_t *connection, u8 the_error)
602 snprintf(err, 4, "E%2.2X", the_error );
603 gdb_put_packet(connection, err, 3);
606 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
611 signal = gdb_last_signal(target);
613 snprintf(sig_reply, 4, "S%2.2x", signal);
614 gdb_put_packet(connection, sig_reply, 3);
619 /* Convert register to string of bits. NB! The # of bits in the
620 * register might be non-divisible by 8(a byte), in which
621 * case an entire byte is shown. */
622 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
624 static const char *DIGITS = "0123456789abcdef";
630 buf_len = CEIL(reg->size, 8);
632 if (target->endianness == TARGET_LITTLE_ENDIAN)
634 for (i = 0; i < buf_len; i++)
636 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
637 tstr[i*2+1] = DIGITS[buf[i]&0xf];
642 for (i = 0; i < buf_len; i++)
644 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
645 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
650 void gdb_target_to_str(target_t *target, char *tstr, char *str)
652 int str_len = strlen(tstr);
657 ERROR("BUG: gdb value with uneven number of characters encountered");
661 if (target->endianness == TARGET_LITTLE_ENDIAN)
663 for (i = 0; i < str_len; i+=2)
665 str[str_len - i - 1] = tstr[i + 1];
666 str[str_len - i - 2] = tstr[i];
671 for (i = 0; i < str_len; i++)
678 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
683 int reg_packet_size = 0;
688 #ifdef _DEBUG_GDB_IO_
692 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
696 case ERROR_TARGET_NOT_HALTED:
697 ERROR("gdb requested registers but we're not halted, dropping connection");
698 return ERROR_SERVER_REMOTE_CLOSED;
700 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
701 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
706 for (i = 0; i < reg_list_size; i++)
708 reg_packet_size += reg_list[i]->size;
711 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
712 reg_packet_p = reg_packet;
714 for (i = 0; i < reg_list_size; i++)
716 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
717 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
720 #ifdef _DEBUG_GDB_IO_
723 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
724 DEBUG("reg_packet: %s", reg_packet_p);
729 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
737 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
745 #ifdef _DEBUG_GDB_IO_
749 /* skip command character */
755 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
756 return ERROR_SERVER_REMOTE_CLOSED;
759 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
763 case ERROR_TARGET_NOT_HALTED:
764 ERROR("gdb tried to registers but we're not halted, dropping connection");
765 return ERROR_SERVER_REMOTE_CLOSED;
767 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
768 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
774 for (i = 0; i < reg_list_size; i++)
778 reg_arch_type_t *arch_type;
780 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
781 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
782 gdb_target_to_str(target, packet_p, hex_buf);
784 /* convert hex-string to binary buffer */
785 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
786 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
788 /* get register arch_type, and call set method */
789 arch_type = register_get_arch_type(reg_list[i]->arch_type);
790 if (arch_type == NULL)
792 ERROR("BUG: encountered unregistered arch type");
795 arch_type->set(reg_list[i], bin_buf);
797 /* advance packet pointer */
798 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
804 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
807 gdb_put_packet(connection, "OK", 2);
812 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
815 int reg_num = strtoul(packet + 1, NULL, 16);
820 #ifdef _DEBUG_GDB_IO_
824 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
828 case ERROR_TARGET_NOT_HALTED:
829 ERROR("gdb requested registers but we're not halted, dropping connection");
830 return ERROR_SERVER_REMOTE_CLOSED;
832 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
833 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
838 if (reg_list_size <= reg_num)
840 ERROR("gdb requested a non-existing register");
844 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
846 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
848 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
856 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
861 int reg_num = strtoul(packet + 1, &separator, 16);
865 reg_arch_type_t *arch_type;
869 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
873 case ERROR_TARGET_NOT_HALTED:
874 ERROR("gdb tried to set a register but we're not halted, dropping connection");
875 return ERROR_SERVER_REMOTE_CLOSED;
877 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
878 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
883 if (reg_list_size < reg_num)
885 ERROR("gdb requested a non-existing register");
886 return ERROR_SERVER_REMOTE_CLOSED;
889 if (*separator != '=')
891 ERROR("GDB 'set register packet', but no '=' following the register number");
892 return ERROR_SERVER_REMOTE_CLOSED;
895 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
896 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
897 gdb_target_to_str(target, separator + 1, hex_buf);
899 /* convert hex-string to binary buffer */
900 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
901 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
903 /* get register arch_type, and call set method */
904 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
905 if (arch_type == NULL)
907 ERROR("BUG: encountered unregistered arch type");
910 arch_type->set(reg_list[reg_num], bin_buf);
912 gdb_put_packet(connection, "OK", 2);
921 int gdb_memory_packet_error(connection_t *connection, int retval)
925 case ERROR_TARGET_NOT_HALTED:
926 ERROR("gdb tried to read memory but we're not halted, dropping connection");
927 return ERROR_SERVER_REMOTE_CLOSED;
928 case ERROR_TARGET_DATA_ABORT:
929 gdb_send_error(connection, EIO);
931 case ERROR_TARGET_TRANSLATION_FAULT:
932 gdb_send_error(connection, EFAULT);
934 case ERROR_TARGET_UNALIGNED_ACCESS:
935 gdb_send_error(connection, EFAULT);
938 ERROR("BUG: unexpected error %i", retval);
945 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
957 /* skip command character */
960 addr = strtoul(packet, &separator, 16);
962 if (*separator != ',')
964 ERROR("incomplete read memory packet received, dropping connection");
965 return ERROR_SERVER_REMOTE_CLOSED;
968 len = strtoul(separator+1, NULL, 16);
970 buffer = malloc(len);
972 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
978 retval = target->type->read_memory(target, addr, 4, 1, buffer);
980 retval = target->type->read_memory(target, addr, 1, len, buffer);
984 retval = target->type->read_memory(target, addr, 2, 1, buffer);
986 retval = target->type->read_memory(target, addr, 1, len, buffer);
989 if (((addr % 4) == 0) && ((len % 4) == 0))
990 retval = target->type->read_memory(target, addr, 4, len / 4, buffer);
992 retval = target->type->read_memory(target, addr, 1, len, buffer);
996 if (retval == ERROR_TARGET_DATA_ABORT)
998 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
999 * At some point this might be fixed in GDB, in which case this code can be removed.
1000 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1002 memset(buffer, 0, len);
1007 if (retval == ERROR_OK)
1009 hex_buffer = malloc(len * 2 + 1);
1011 for (i=0; i<len; i++)
1012 snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
1014 gdb_put_packet(connection, hex_buffer, len * 2);
1020 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1029 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1040 /* skip command character */
1043 addr = strtoul(packet, &separator, 16);
1045 if (*separator != ',')
1047 ERROR("incomplete write memory packet received, dropping connection");
1048 return ERROR_SERVER_REMOTE_CLOSED;
1051 len = strtoul(separator+1, &separator, 16);
1053 if (*(separator++) != ':')
1055 ERROR("incomplete write memory packet received, dropping connection");
1056 return ERROR_SERVER_REMOTE_CLOSED;
1059 buffer = malloc(len);
1061 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1063 for (i=0; i<len; i++)
1066 sscanf(separator + 2*i, "%2x", &tmp);
1073 /* handle sized writes */
1075 if ((addr % 4) == 0)
1076 retval = target->type->write_memory(target, addr, 4, 1, buffer);
1078 retval = target->type->write_memory(target, addr, 1, len, buffer);
1081 if ((addr % 2) == 0)
1082 retval = target->type->write_memory(target, addr, 2, 1, buffer);
1084 retval = target->type->write_memory(target, addr, 1, len, buffer);
1088 retval = target->type->write_memory(target, addr, 1, len, buffer);
1090 /* handle bulk writes */
1092 retval = target_write_buffer(target, addr, len, buffer);
1096 if (retval == ERROR_OK)
1098 gdb_put_packet(connection, "OK", 2);
1102 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1111 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1120 /* skip command character */
1123 addr = strtoul(packet, &separator, 16);
1125 if (*separator != ',')
1127 ERROR("incomplete write memory binary packet received, dropping connection");
1128 return ERROR_SERVER_REMOTE_CLOSED;
1131 len = strtoul(separator+1, &separator, 16);
1133 if (*(separator++) != ':')
1135 ERROR("incomplete write memory binary packet received, dropping connection");
1136 return ERROR_SERVER_REMOTE_CLOSED;
1142 buffer = malloc(len);
1144 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1146 memcpy( buffer, separator, len );
1151 if ((addr % 4) == 0)
1152 retval = target->type->write_memory(target, addr, 4, 1, buffer);
1154 retval = target->type->write_memory(target, addr, 1, len, buffer);
1157 if ((addr % 2) == 0)
1158 retval = target->type->write_memory(target, addr, 2, 1, buffer);
1160 retval = target->type->write_memory(target, addr, 1, len, buffer);
1164 retval = target->type->write_memory(target, addr, 1, len, buffer);
1167 retval = target_write_buffer(target, addr, len, buffer);
1174 if (retval == ERROR_OK)
1176 gdb_put_packet(connection, "OK", 2);
1180 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1187 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1194 if (packet_size > 1)
1196 packet[packet_size] = 0;
1197 address = strtoul(packet + 1, NULL, 16);
1204 if (packet[0] == 'c')
1207 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1209 else if (packet[0] == 's')
1212 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1216 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1220 case ERROR_TARGET_NOT_HALTED:
1221 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1222 return ERROR_SERVER_REMOTE_CLOSED;
1224 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1225 gdb_send_error(connection, EBUSY);
1228 ERROR("BUG: unexpected error %i", retval);
1235 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1238 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1239 enum watchpoint_rw wp_type;
1247 type = strtoul(packet + 1, &separator, 16);
1249 if (type == 0) /* memory breakpoint */
1250 bp_type = BKPT_SOFT;
1251 else if (type == 1) /* hardware breakpoint */
1252 bp_type = BKPT_HARD;
1253 else if (type == 2) /* write watchpoint */
1254 wp_type = WPT_WRITE;
1255 else if (type == 3) /* read watchpoint */
1257 else if (type == 4) /* access watchpoint */
1258 wp_type = WPT_ACCESS;
1260 if (*separator != ',')
1262 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1263 return ERROR_SERVER_REMOTE_CLOSED;
1266 address = strtoul(separator+1, &separator, 16);
1268 if (*separator != ',')
1270 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1271 return ERROR_SERVER_REMOTE_CLOSED;
1274 size = strtoul(separator+1, &separator, 16);
1280 if (packet[0] == 'Z')
1282 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1284 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1289 gdb_put_packet(connection, "OK", 2);
1294 breakpoint_remove(target, address);
1295 gdb_put_packet(connection, "OK", 2);
1302 if (packet[0] == 'Z')
1304 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1306 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1311 gdb_put_packet(connection, "OK", 2);
1316 watchpoint_remove(target, address);
1317 gdb_put_packet(connection, "OK", 2);
1328 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1329 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1331 if (*retval != ERROR_OK)
1339 if ((*xml == NULL) || (!first))
1341 /* start by 0 to exercise all the code paths.
1342 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1344 *size = *size * 2 + 2;
1346 *xml = realloc(*xml, *size);
1351 *retval = ERROR_SERVER_REMOTE_CLOSED;
1359 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1361 if ((ret > 0) && ((ret + 1) < *size - *pos))
1366 /* there was just enough or not enough space, allocate more. */
1371 static int decode_xfer_read (char *buf, char **annex, int *ofs, unsigned int *len)
1375 /* Extract and NUL-terminate the annex. */
1377 while (*buf && *buf != ':')
1383 /* After the read marker and annex, qXfer looks like a
1384 * traditional 'm' packet. */
1386 *ofs = strtoul(buf, &separator, 16);
1388 if (*separator != ',')
1391 *len = strtoul(separator+1, NULL, 16);
1396 int gdb_calc_blocksize(flash_bank_t *bank)
1399 int block_size = 0xffffffff;
1401 /* loop through all sectors and return smallest sector size */
1403 for (i = 0; i < bank->num_sectors; i++)
1405 if (bank->sectors[i].size < block_size)
1406 block_size = bank->sectors[i].size;
1412 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1414 command_context_t *cmd_ctx = connection->cmd_ctx;
1416 if (strstr(packet, "qRcmd,"))
1418 if (packet_size > 6)
1422 cmd = malloc((packet_size - 6)/2 + 1);
1423 for (i=0; i < (packet_size - 6)/2; i++)
1426 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1429 cmd[(packet_size - 6)/2] = 0x0;
1431 /* We want to print all debug output to GDB connection */
1432 log_setCallback(gdb_log_callback, connection);
1433 target_call_timer_callbacks();
1434 command_run_line(cmd_ctx, cmd);
1437 gdb_put_packet(connection, "OK", 2);
1440 else if (strstr(packet, "qCRC:"))
1442 if (packet_size > 5)
1451 /* skip command character */
1454 addr = strtoul(packet, &separator, 16);
1456 if (*separator != ',')
1458 ERROR("incomplete read memory packet received, dropping connection");
1459 return ERROR_SERVER_REMOTE_CLOSED;
1462 len = strtoul(separator + 1, NULL, 16);
1464 retval = target_checksum_memory(target, addr, len, &checksum);
1466 if (retval == ERROR_OK)
1468 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1469 gdb_put_packet(connection, gdb_reply, 9);
1473 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1480 else if (strstr(packet, "qSupported"))
1482 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1483 * disable qXfer:features:read for the moment */
1484 int retval = ERROR_OK;
1485 char *buffer = NULL;
1489 xml_printf(&retval, &buffer, &pos, &size,
1490 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1491 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1493 if (retval != ERROR_OK)
1495 gdb_send_error(connection, 01);
1499 gdb_put_packet(connection, buffer, strlen(buffer));
1504 else if (strstr(packet, "qXfer:memory-map:read::"))
1506 /* We get away with only specifying flash here. Regions that are not
1507 * specified are treated as if we provided no memory map(if not we
1508 * could detect the holes and mark them as RAM).
1509 * Normally we only execute this code once, but no big deal if we
1510 * have to regenerate it a couple of times. */
1516 int retval = ERROR_OK;
1523 /* skip command character */
1526 offset = strtoul(packet, &separator, 16);
1527 length = strtoul(separator + 1, &separator, 16);
1529 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1534 p = get_flash_bank_by_num(i);
1538 /* if device has uneven sector sizes, eg. str7, lpc
1539 * we pass the smallest sector size to gdb memory map */
1540 blocksize = gdb_calc_blocksize(p);
1542 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1543 "<property name=\"blocksize\">0x%x</property>\n" \
1545 p->base, p->size, blocksize);
1549 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1551 if (retval != ERROR_OK)
1553 gdb_send_error(connection, retval);
1557 if (offset + length > pos)
1559 length = pos - offset;
1562 char *t = malloc(length + 1);
1564 memcpy(t + 1, xml + offset, length);
1565 gdb_put_packet(connection, t, length + 1);
1571 else if (strstr(packet, "qXfer:features:read:"))
1576 int retval = ERROR_OK;
1582 /* skip command character */
1585 if (decode_xfer_read( packet, &annex, &offset, &length ) < 0)
1587 gdb_send_error(connection, 01);
1591 if (strcmp(annex, "target.xml") != 0)
1593 gdb_send_error(connection, 01);
1597 xml_printf(&retval, &xml, &pos, &size, \
1598 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1600 if (retval != ERROR_OK)
1602 gdb_send_error(connection, retval);
1606 gdb_put_packet(connection, xml, strlen(xml) + 1);
1612 gdb_put_packet(connection, "", 0);
1616 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1618 gdb_connection_t *gdb_connection = connection->priv;
1619 gdb_service_t *gdb_service = connection->service->priv;
1622 /* if flash programming disabled - send a empty reply */
1624 if (gdb_flash_program == 0)
1626 gdb_put_packet(connection, "", 0);
1630 if (strstr(packet, "vFlashErase:"))
1633 unsigned long length;
1635 char *parse = packet + 12;
1638 ERROR("incomplete vFlashErase packet received, dropping connection");
1639 return ERROR_SERVER_REMOTE_CLOSED;
1642 addr = strtoul(parse, &parse, 16);
1644 if (*(parse++) != ',' || *parse == '\0')
1646 ERROR("incomplete vFlashErase packet received, dropping connection");
1647 return ERROR_SERVER_REMOTE_CLOSED;
1650 length = strtoul(parse, &parse, 16);
1654 ERROR("incomplete vFlashErase packet received, dropping connection");
1655 return ERROR_SERVER_REMOTE_CLOSED;
1658 /* disable gdb output while programming */
1659 gdb_connection->output_disable = 1;
1661 /* assume all sectors need erasing - stops any problems
1662 * when flash_write is called multiple times */
1665 /* perform any target specific operations before the erase */
1666 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1669 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1671 /* GDB doesn't evaluate the actual error number returned,
1672 * treat a failed erase as an I/O error
1674 gdb_send_error(connection, EIO);
1675 ERROR("flash_erase returned %i", result);
1678 gdb_put_packet(connection, "OK", 2);
1680 /* reenable gdb output */
1681 gdb_connection->output_disable = 0;
1686 if (strstr(packet, "vFlashWrite:"))
1689 unsigned long length;
1690 char *parse = packet + 12;
1694 ERROR("incomplete vFlashErase packet received, dropping connection");
1695 return ERROR_SERVER_REMOTE_CLOSED;
1697 addr = strtoul(parse, &parse, 16);
1698 if (*(parse++) != ':')
1700 ERROR("incomplete vFlashErase packet received, dropping connection");
1701 return ERROR_SERVER_REMOTE_CLOSED;
1703 length = packet_size - (parse - packet);
1705 /* disable gdb output while programming */
1706 gdb_connection->output_disable = 1;
1708 /* create a new image if there isn't already one */
1709 if (gdb_connection->vflash_image == NULL)
1711 gdb_connection->vflash_image = malloc(sizeof(image_t));
1712 image_open(gdb_connection->vflash_image, "", "build");
1715 /* create new section with content from packet buffer */
1716 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1718 gdb_put_packet(connection, "OK", 2);
1720 /* reenable gdb output */
1721 gdb_connection->output_disable = 0;
1726 if (!strcmp(packet, "vFlashDone"))
1731 /* disable gdb output while programming */
1732 gdb_connection->output_disable = 1;
1734 /* process the flashing buffer. No need to erase as GDB
1735 * always issues a vFlashErase first. */
1736 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1738 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1739 gdb_put_packet(connection, "E.memtype", 9);
1741 gdb_send_error(connection, EIO);
1745 ERROR("flash writing failed: %s", error_str);
1751 DEBUG("wrote %u bytes from vFlash image to flash", written);
1752 gdb_put_packet(connection, "OK", 2);
1755 image_close(gdb_connection->vflash_image);
1756 free(gdb_connection->vflash_image);
1757 gdb_connection->vflash_image = NULL;
1759 /* reenable gdb output */
1760 gdb_connection->output_disable = 0;
1765 gdb_put_packet(connection, "", 0);
1769 int gdb_detach(connection_t *connection, target_t *target)
1771 switch( detach_mode )
1773 case GDB_DETACH_RESUME:
1774 target->type->resume(target, 1, 0, 1, 0);
1777 case GDB_DETACH_RESET:
1778 target_process_reset(connection->cmd_ctx);
1781 case GDB_DETACH_HALT:
1782 target->type->halt(target);
1785 case GDB_DETACH_NOTHING:
1789 gdb_put_packet(connection, "OK", 2);
1796 static void gdb_log_callback(void *privData, const char *file, int line,
1797 const char *function, const char *format, va_list args)
1799 connection_t *connection=(connection_t *)privData;
1801 char *t=allocPrintf(format, args);
1805 gdb_output_con(connection, t);
1810 int gdb_input(connection_t *connection)
1812 gdb_service_t *gdb_service = connection->service->priv;
1813 target_t *target = gdb_service->target;
1814 char packet[GDB_BUFFER_SIZE];
1817 gdb_connection_t *gdb_con = connection->priv;
1818 static int extended_protocol = 0;
1820 /* drain input buffer */
1823 packet_size = GDB_BUFFER_SIZE-1;
1824 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1828 case ERROR_GDB_BUFFER_TOO_SMALL:
1829 ERROR("BUG: buffer supplied for gdb packet was too small");
1831 case ERROR_SERVER_REMOTE_CLOSED:
1832 return ERROR_SERVER_REMOTE_CLOSED;
1834 ERROR("BUG: unexpected error");
1839 /* terminate with zero */
1840 packet[packet_size] = 0;
1842 DEBUG("received packet: '%s'", packet);
1844 if (packet_size > 0)
1850 /* Hct... -- set thread
1851 * we don't have threads, send empty reply */
1852 gdb_put_packet(connection, NULL, 0);
1855 retval = gdb_query_packet(connection, target, packet, packet_size);
1858 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1861 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1864 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1867 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1870 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1873 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1877 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1880 gdb_last_signal_packet(connection, target, packet, packet_size);
1884 /* We're running/stepping, in which case we can
1885 * forward log output until the target is halted */
1886 log_setCallback(gdb_log_callback, connection);
1887 gdb_step_continue_packet(connection, target, packet, packet_size);
1890 retval = gdb_v_packet(connection, target, packet, packet_size);
1893 retval = gdb_detach(connection, target);
1894 extended_protocol = 0;
1897 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1901 if (extended_protocol != 0)
1903 gdb_put_packet(connection, "OK", 2);
1904 return ERROR_SERVER_REMOTE_CLOSED;
1906 /* handle extended remote protocol */
1907 extended_protocol = 1;
1908 gdb_put_packet(connection, "OK", 2);
1911 /* handle extended restart packet */
1912 target_process_reset(connection->cmd_ctx);
1915 /* ignore unkown packets */
1916 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1917 gdb_put_packet(connection, NULL, 0);
1921 /* if a packet handler returned an error, exit input loop */
1922 if (retval != ERROR_OK)
1926 if (gdb_con->ctrl_c)
1928 if (target->state == TARGET_RUNNING)
1930 target->type->halt(target);
1931 gdb_con->ctrl_c = 0;
1935 } while (gdb_con->buf_cnt > 0);
1942 gdb_service_t *gdb_service;
1943 target_t *target = targets;
1948 WARNING("no gdb ports allocated as no target has been specified");
1954 WARNING("no gdb port specified, using default port 3333");
1960 char service_name[8];
1962 snprintf(service_name, 8, "gdb-%2.2i", i);
1964 gdb_service = malloc(sizeof(gdb_service_t));
1965 gdb_service->target = target;
1967 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1969 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1972 target = target->next;
1978 /* daemon configuration command gdb_port */
1979 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1984 /* only if the port wasn't overwritten by cmdline */
1986 gdb_port = strtoul(args[0], NULL, 0);
1991 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1995 if (strcmp(args[0], "resume") == 0)
1997 detach_mode = GDB_DETACH_RESUME;
2000 else if (strcmp(args[0], "reset") == 0)
2002 detach_mode = GDB_DETACH_RESET;
2005 else if (strcmp(args[0], "halt") == 0)
2007 detach_mode = GDB_DETACH_HALT;
2010 else if (strcmp(args[0], "nothing") == 0)
2012 detach_mode = GDB_DETACH_NOTHING;
2017 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
2021 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2025 if (strcmp(args[0], "enable") == 0)
2027 gdb_use_memory_map = 1;
2030 else if (strcmp(args[0], "disable") == 0)
2032 gdb_use_memory_map = 0;
2037 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2041 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2045 if (strcmp(args[0], "enable") == 0)
2047 gdb_flash_program = 1;
2050 else if (strcmp(args[0], "disable") == 0)
2052 gdb_flash_program = 0;
2057 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
2061 int gdb_register_commands(command_context_t *command_context)
2063 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
2064 COMMAND_CONFIG, "");
2065 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
2066 COMMAND_CONFIG, "");
2067 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
2068 COMMAND_CONFIG, "");
2069 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
2070 COMMAND_CONFIG, "");