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;
55 /* target behaviour on gdb detach */
56 enum gdb_detach_mode detach_mode = GDB_DETACH_RESUME;
58 /* set if we are sending a memory map to gdb
59 * via qXfer:memory-map:read packet */
60 int gdb_use_memory_map = 0;
61 int gdb_flash_program = 0;
63 int gdb_last_signal(target_t *target)
65 switch (target->debug_reason)
67 case DBG_REASON_DBGRQ:
68 return 0x2; /* SIGINT */
69 case DBG_REASON_BREAKPOINT:
70 case DBG_REASON_WATCHPOINT:
71 case DBG_REASON_WPTANDBKPT:
72 return 0x05; /* SIGTRAP */
73 case DBG_REASON_SINGLESTEP:
74 return 0x05; /* SIGTRAP */
75 case DBG_REASON_NOTHALTED:
76 return 0x0; /* no signal... shouldn't happen */
78 ERROR("BUG: undefined debug reason");
83 int gdb_get_char(connection_t *connection, int* next_char)
85 gdb_connection_t *gdb_con = connection->priv;
91 if (gdb_con->buf_cnt-- > 0)
93 *next_char = *(gdb_con->buf_p++);
94 if (gdb_con->buf_cnt > 0)
95 connection->input_pending = 1;
97 connection->input_pending = 0;
100 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
106 while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
108 if (gdb_con->buf_cnt == 0)
109 return ERROR_SERVER_REMOTE_CLOSED;
112 errno = WSAGetLastError();
119 case WSAECONNABORTED:
120 return ERROR_SERVER_REMOTE_CLOSED;
122 return ERROR_SERVER_REMOTE_CLOSED;
124 ERROR("read: %d", errno);
134 return ERROR_SERVER_REMOTE_CLOSED;
136 return ERROR_SERVER_REMOTE_CLOSED;
138 ERROR("read: %s", strerror(errno));
144 #ifdef _DEBUG_GDB_IO_
145 debug_buffer = malloc(gdb_con->buf_cnt + 1);
146 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
147 debug_buffer[gdb_con->buf_cnt] = 0;
148 DEBUG("received '%s'", debug_buffer);
152 gdb_con->buf_p = gdb_con->buffer;
154 *next_char = *(gdb_con->buf_p++);
155 if (gdb_con->buf_cnt > 0)
156 connection->input_pending = 1;
158 connection->input_pending = 0;
159 #ifdef _DEBUG_GDB_IO_
160 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
166 int gdb_putback_char(connection_t *connection, int last_char)
168 gdb_connection_t *gdb_con = connection->priv;
170 if (gdb_con->buf_p > gdb_con->buffer)
172 *(--gdb_con->buf_p) = last_char;
177 ERROR("BUG: couldn't put character back");
183 int gdb_put_packet(connection_t *connection, char *buffer, int len)
186 unsigned char my_checksum = 0;
191 gdb_connection_t *gdb_con = connection->priv;
193 for (i = 0; i < len; i++)
194 my_checksum += buffer[i];
198 debug_buffer = malloc(len + 1);
199 memcpy(debug_buffer, buffer, len);
200 debug_buffer[len] = 0;
201 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
204 write_socket(connection->fd, "$", 1);
206 write_socket(connection->fd, buffer, len);
207 write_socket(connection->fd, "#", 1);
209 snprintf(checksum, 3, "%2.2x", my_checksum);
211 write_socket(connection->fd, checksum, 2);
213 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
218 else if (reply == '-')
219 WARNING("negative reply, retrying");
220 else if (reply == 0x3)
223 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
227 else if (reply == '-')
228 WARNING("negative reply, retrying");
231 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
232 return ERROR_SERVER_REMOTE_CLOSED;
237 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
238 return ERROR_SERVER_REMOTE_CLOSED;
245 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
251 unsigned char my_checksum = 0;
252 gdb_connection_t *gdb_con = connection->priv;
258 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
261 #ifdef _DEBUG_GDB_IO_
262 DEBUG("character: '%c'", character);
270 WARNING("acknowledgment received, but no packet pending");
273 WARNING("negative acknowledgment, but no packet pending");
280 WARNING("ignoring character 0x%x", character);
283 } while (character != '$');
289 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
292 if (character == '#')
295 if (character == '}')
297 /* data transmitted in binary mode (X packet)
298 * uses 0x7d as escape character */
299 my_checksum += character & 0xff;
300 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
302 my_checksum += character & 0xff;
303 buffer[count++] = (character ^ 0x20) & 0xff;
307 my_checksum += character & 0xff;
308 buffer[count++] = character & 0xff;
313 ERROR("packet buffer too small");
314 return ERROR_GDB_BUFFER_TOO_SMALL;
320 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
322 checksum[0] = character;
323 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
325 checksum[1] = character;
328 if (my_checksum == strtoul(checksum, NULL, 16))
330 write_socket(connection->fd, "+", 1);
334 WARNING("checksum error, requesting retransmission");
335 write_socket(connection->fd, "-", 1);
341 int gdb_output(struct command_context_s *context, char* line)
343 connection_t *connection = context->output_handler_priv;
344 gdb_connection_t *gdb_connection = connection->priv;
349 /* check if output is enabled */
350 if (gdb_connection->output_disable)
355 bin_size = strlen(line);
357 hex_buffer = malloc(bin_size*2 + 4);
360 for (i=0; i<bin_size; i++)
361 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
362 hex_buffer[bin_size*2+1] = '0';
363 hex_buffer[bin_size*2+2] = 'a';
364 hex_buffer[bin_size*2+3] = 0x0;
366 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
372 int gdb_program_handler(struct target_s *target, enum target_event event, void *priv)
375 struct command_context_s *cmd_ctx = priv;
377 if (target->gdb_program_script)
379 script = fopen(target->gdb_program_script, "r");
382 ERROR("couldn't open script file %s", target->gdb_program_script);
386 INFO("executing gdb_program script '%s'", target->gdb_program_script);
387 command_run_file(cmd_ctx, script, COMMAND_EXEC);
390 jtag_execute_queue();
396 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
398 connection_t *connection = priv;
399 gdb_connection_t *gdb_connection = connection->priv;
405 case TARGET_EVENT_HALTED:
406 if (gdb_connection->frontend_state == TARGET_RUNNING)
408 if (gdb_connection->ctrl_c)
411 gdb_connection->ctrl_c = 0;
415 signal = gdb_last_signal(target);
418 snprintf(sig_reply, 4, "T%2.2x", signal);
419 gdb_put_packet(connection, sig_reply, 3);
420 gdb_connection->frontend_state = TARGET_HALTED;
423 case TARGET_EVENT_RESUMED:
424 if (gdb_connection->frontend_state == TARGET_HALTED)
426 gdb_connection->frontend_state = TARGET_RUNNING;
429 case TARGET_EVENT_GDB_PROGRAM:
430 gdb_program_handler(target, event, connection->cmd_ctx);
439 int gdb_new_connection(connection_t *connection)
441 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
442 gdb_service_t *gdb_service = connection->service->priv;
446 connection->priv = gdb_connection;
448 /* initialize gdb connection information */
449 gdb_connection->buf_p = gdb_connection->buffer;
450 gdb_connection->buf_cnt = 0;
451 gdb_connection->ctrl_c = 0;
452 gdb_connection->frontend_state = TARGET_HALTED;
453 gdb_connection->vflash_image = NULL;
454 gdb_connection->output_disable = 0;
456 /* output goes through gdb connection */
457 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
459 /* register callback to be informed about target events */
460 target_register_event_callback(gdb_target_callback_event_handler, connection);
462 /* a gdb session just attached, put the target in halt mode */
463 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
464 (retval != ERROR_TARGET_ALREADY_HALTED))
466 ERROR("error when trying to halt target");
470 while (gdb_service->target->state != TARGET_HALTED)
472 gdb_service->target->type->poll(gdb_service->target);
475 /* remove the initial ACK from the incoming buffer */
476 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
479 if (initial_ack != '+')
480 gdb_putback_char(connection, initial_ack);
485 int gdb_connection_closed(connection_t *connection)
487 gdb_service_t *gdb_service = connection->service->priv;
488 gdb_connection_t *gdb_connection = connection->priv;
490 /* see if an image built with vFlash commands is left */
491 if (gdb_connection->vflash_image)
493 image_close(gdb_connection->vflash_image);
494 free(gdb_connection->vflash_image);
495 gdb_connection->vflash_image = NULL;
498 /* if this connection registered a debug-message receiver delete it */
499 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
501 if (connection->priv)
503 free(connection->priv);
504 connection->priv = NULL;
508 ERROR("BUG: connection->priv == NULL");
511 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
516 void gdb_send_error(connection_t *connection, u8 the_error)
519 snprintf(err, 4, "E%2.2X", the_error );
520 gdb_put_packet(connection, err, 3);
523 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
528 signal = gdb_last_signal(target);
530 snprintf(sig_reply, 4, "S%2.2x", signal);
531 gdb_put_packet(connection, sig_reply, 3);
536 /* Convert register to string of bits. NB! The # of bits in the
537 * register might be non-divisible by 8(a byte), in which
538 * case an entire byte is shown. */
539 void gdb_str_to_target(target_t *target, char *tstr, reg_t *reg)
541 static const char *DIGITS = "0123456789abcdef";
547 buf_len = CEIL(reg->size, 8);
549 if (target->endianness == TARGET_LITTLE_ENDIAN)
551 for (i = 0; i < buf_len; i++)
553 tstr[i*2] = DIGITS[(buf[i]>>4) & 0xf];
554 tstr[i*2+1] = DIGITS[buf[i]&0xf];
559 for (i = 0; i < buf_len; i++)
561 tstr[(buf_len-1-i)*2] = DIGITS[(buf[i]>>4)&0xf];
562 tstr[(buf_len-1-i)*2+1] = DIGITS[buf[i]&0xf];
567 void gdb_target_to_str(target_t *target, char *tstr, char *str)
569 int str_len = strlen(tstr);
574 ERROR("BUG: gdb value with uneven number of characters encountered");
578 if (target->endianness == TARGET_LITTLE_ENDIAN)
580 for (i = 0; i < str_len; i+=2)
582 str[str_len - i - 1] = tstr[i + 1];
583 str[str_len - i - 2] = tstr[i];
588 for (i = 0; i < str_len; i++)
595 int gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
600 int reg_packet_size = 0;
605 #ifdef _DEBUG_GDB_IO_
609 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
613 case ERROR_TARGET_NOT_HALTED:
614 ERROR("gdb requested registers but we're not halted, dropping connection");
615 return ERROR_SERVER_REMOTE_CLOSED;
617 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
618 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
623 for (i = 0; i < reg_list_size; i++)
625 reg_packet_size += reg_list[i]->size;
628 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
629 reg_packet_p = reg_packet;
631 for (i = 0; i < reg_list_size; i++)
633 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
634 reg_packet_p += CEIL(reg_list[i]->size, 8) * 2;
637 #ifdef _DEBUG_GDB_IO_
640 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
641 DEBUG("reg_packet: %s", reg_packet_p);
646 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
654 int gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
662 #ifdef _DEBUG_GDB_IO_
666 /* skip command character */
672 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
673 return ERROR_SERVER_REMOTE_CLOSED;
676 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
680 case ERROR_TARGET_NOT_HALTED:
681 ERROR("gdb tried to registers but we're not halted, dropping connection");
682 return ERROR_SERVER_REMOTE_CLOSED;
684 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
685 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
691 for (i = 0; i < reg_list_size; i++)
695 reg_arch_type_t *arch_type;
697 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
698 hex_buf = malloc(CEIL(reg_list[i]->size, 8) * 2);
699 gdb_target_to_str(target, packet_p, hex_buf);
701 /* convert hex-string to binary buffer */
702 bin_buf = malloc(CEIL(reg_list[i]->size, 8));
703 str_to_buf(hex_buf, CEIL(reg_list[i]->size, 8) * 2, bin_buf, reg_list[i]->size, 16);
705 /* get register arch_type, and call set method */
706 arch_type = register_get_arch_type(reg_list[i]->arch_type);
707 if (arch_type == NULL)
709 ERROR("BUG: encountered unregistered arch type");
712 arch_type->set(reg_list[i], bin_buf);
714 /* advance packet pointer */
715 packet_p += (CEIL(reg_list[i]->size, 8) * 2);
721 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
724 gdb_put_packet(connection, "OK", 2);
729 int gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
732 int reg_num = strtoul(packet + 1, NULL, 16);
737 #ifdef _DEBUG_GDB_IO_
741 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
745 case ERROR_TARGET_NOT_HALTED:
746 ERROR("gdb requested registers but we're not halted, dropping connection");
747 return ERROR_SERVER_REMOTE_CLOSED;
749 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
750 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
755 if (reg_list_size <= reg_num)
757 ERROR("gdb requested a non-existing register");
761 reg_packet = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
763 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
765 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
773 int gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
778 int reg_num = strtoul(packet + 1, &separator, 16);
782 reg_arch_type_t *arch_type;
786 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
790 case ERROR_TARGET_NOT_HALTED:
791 ERROR("gdb tried to set a register but we're not halted, dropping connection");
792 return ERROR_SERVER_REMOTE_CLOSED;
794 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
795 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
800 if (reg_list_size < reg_num)
802 ERROR("gdb requested a non-existing register");
803 return ERROR_SERVER_REMOTE_CLOSED;
806 if (*separator != '=')
808 ERROR("GDB 'set register packet', but no '=' following the register number");
809 return ERROR_SERVER_REMOTE_CLOSED;
812 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
813 hex_buf = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
814 gdb_target_to_str(target, separator + 1, hex_buf);
816 /* convert hex-string to binary buffer */
817 bin_buf = malloc(CEIL(reg_list[reg_num]->size, 8));
818 str_to_buf(hex_buf, CEIL(reg_list[reg_num]->size, 8) * 2, bin_buf, reg_list[reg_num]->size, 16);
820 /* get register arch_type, and call set method */
821 arch_type = register_get_arch_type(reg_list[reg_num]->arch_type);
822 if (arch_type == NULL)
824 ERROR("BUG: encountered unregistered arch type");
827 arch_type->set(reg_list[reg_num], bin_buf);
829 gdb_put_packet(connection, "OK", 2);
838 int gdb_memory_packet_error(connection_t *connection, int retval)
842 case ERROR_TARGET_NOT_HALTED:
843 ERROR("gdb tried to read memory but we're not halted, dropping connection");
844 return ERROR_SERVER_REMOTE_CLOSED;
846 case ERROR_TARGET_DATA_ABORT:
847 gdb_send_error(connection, EIO);
849 case ERROR_TARGET_TRANSLATION_FAULT:
850 gdb_send_error(connection, EFAULT);
852 case ERROR_TARGET_UNALIGNED_ACCESS:
853 gdb_send_error(connection, EFAULT);
856 ERROR("BUG: unexpected error %i", retval);
863 int gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
875 /* skip command character */
878 addr = strtoul(packet, &separator, 16);
880 if (*separator != ',')
882 ERROR("incomplete read memory packet received, dropping connection");
883 return ERROR_SERVER_REMOTE_CLOSED;
886 len = strtoul(separator+1, NULL, 16);
888 buffer = malloc(len);
890 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
896 retval = target->type->read_memory(target, addr, 4, 1, buffer);
898 retval = target->type->read_memory(target, addr, 1, len, buffer);
902 retval = target->type->read_memory(target, addr, 2, 1, buffer);
904 retval = target->type->read_memory(target, addr, 1, len, buffer);
907 if (((addr % 4) == 0) && ((len % 4) == 0))
908 retval = target->type->read_memory(target, addr, 4, len / 4, buffer);
910 retval = target->type->read_memory(target, addr, 1, len, buffer);
913 if (retval == ERROR_OK)
915 hex_buffer = malloc(len * 2 + 1);
917 for (i=0; i<len; i++)
918 snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
920 gdb_put_packet(connection, hex_buffer, len * 2);
926 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
935 int gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
946 /* skip command character */
949 addr = strtoul(packet, &separator, 16);
951 if (*separator != ',')
953 ERROR("incomplete write memory packet received, dropping connection");
954 return ERROR_SERVER_REMOTE_CLOSED;
957 len = strtoul(separator+1, &separator, 16);
959 if (*(separator++) != ':')
961 ERROR("incomplete write memory packet received, dropping connection");
962 return ERROR_SERVER_REMOTE_CLOSED;
965 buffer = malloc(len);
967 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
969 for (i=0; i<len; i++)
972 sscanf(separator + 2*i, "%2x", &tmp);
979 /* handle sized writes */
982 retval = target->type->write_memory(target, addr, 4, 1, buffer);
984 retval = target->type->write_memory(target, addr, 1, len, buffer);
988 retval = target->type->write_memory(target, addr, 2, 1, buffer);
990 retval = target->type->write_memory(target, addr, 1, len, buffer);
994 retval = target->type->write_memory(target, addr, 1, len, buffer);
996 /* handle bulk writes */
998 retval = target_write_buffer(target, addr, len, buffer);
1002 if (retval == ERROR_OK)
1004 gdb_put_packet(connection, "OK", 2);
1008 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1017 int gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1026 /* skip command character */
1029 addr = strtoul(packet, &separator, 16);
1031 if (*separator != ',')
1033 ERROR("incomplete write memory binary packet received, dropping connection");
1034 return ERROR_SERVER_REMOTE_CLOSED;
1037 len = strtoul(separator+1, &separator, 16);
1039 if (*(separator++) != ':')
1041 ERROR("incomplete write memory binary packet received, dropping connection");
1042 return ERROR_SERVER_REMOTE_CLOSED;
1048 buffer = malloc(len);
1050 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
1052 memcpy( buffer, separator, len );
1057 if ((addr % 4) == 0)
1058 retval = target->type->write_memory(target, addr, 4, 1, buffer);
1060 retval = target->type->write_memory(target, addr, 1, len, buffer);
1063 if ((addr % 2) == 0)
1064 retval = target->type->write_memory(target, addr, 2, 1, buffer);
1066 retval = target->type->write_memory(target, addr, 1, len, buffer);
1070 retval = target->type->write_memory(target, addr, 1, len, buffer);
1073 retval = target_write_buffer(target, addr, len, buffer);
1080 if (retval == ERROR_OK)
1082 gdb_put_packet(connection, "OK", 2);
1086 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1093 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1100 if (packet_size > 1)
1102 packet[packet_size] = 0;
1103 address = strtoul(packet + 1, NULL, 16);
1110 if (packet[0] == 'c')
1113 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1115 else if (packet[0] == 's')
1118 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
1122 int gdb_bp_wp_packet_error(connection_t *connection, int retval)
1126 case ERROR_TARGET_NOT_HALTED:
1127 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1128 return ERROR_SERVER_REMOTE_CLOSED;
1130 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE:
1131 gdb_send_error(connection, EBUSY);
1134 ERROR("BUG: unexpected error %i", retval);
1141 int gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1144 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1145 enum watchpoint_rw wp_type;
1153 type = strtoul(packet + 1, &separator, 16);
1155 if (type == 0) /* memory breakpoint */
1156 bp_type = BKPT_SOFT;
1157 else if (type == 1) /* hardware breakpoint */
1158 bp_type = BKPT_HARD;
1159 else if (type == 2) /* write watchpoint */
1160 wp_type = WPT_WRITE;
1161 else if (type == 3) /* read watchpoint */
1163 else if (type == 4) /* access watchpoint */
1164 wp_type = WPT_ACCESS;
1166 if (*separator != ',')
1168 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1169 return ERROR_SERVER_REMOTE_CLOSED;
1172 address = strtoul(separator+1, &separator, 16);
1174 if (*separator != ',')
1176 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1177 return ERROR_SERVER_REMOTE_CLOSED;
1180 size = strtoul(separator+1, &separator, 16);
1186 if (packet[0] == 'Z')
1188 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1190 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1195 gdb_put_packet(connection, "OK", 2);
1200 breakpoint_remove(target, address);
1201 gdb_put_packet(connection, "OK", 2);
1208 if (packet[0] == 'Z')
1210 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1212 if ((retval = gdb_bp_wp_packet_error(connection, retval)) != ERROR_OK)
1217 gdb_put_packet(connection, "OK", 2);
1222 watchpoint_remove(target, address);
1223 gdb_put_packet(connection, "OK", 2);
1234 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1235 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1237 if (*retval != ERROR_OK)
1245 if ((*xml == NULL) || (!first))
1247 /* start by 0 to exercise all the code paths.
1248 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1250 *size = *size * 2 + 2;
1252 *xml = realloc(*xml, *size);
1257 *retval = ERROR_SERVER_REMOTE_CLOSED;
1265 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1267 if ((ret > 0) && ((ret + 1) < *size - *pos))
1272 /* there was just enough or not enough space, allocate more. */
1277 static int decode_xfer_read (char *buf, char **annex, int *ofs, unsigned int *len)
1281 /* Extract and NUL-terminate the annex. */
1283 while (*buf && *buf != ':')
1289 /* After the read marker and annex, qXfer looks like a
1290 * traditional 'm' packet. */
1292 *ofs = strtoul(buf, &separator, 16);
1294 if (*separator != ',')
1297 *len = strtoul(separator+1, NULL, 16);
1302 int gdb_query_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1304 command_context_t *cmd_ctx = connection->cmd_ctx;
1306 if (strstr(packet, "qRcmd,"))
1308 if (packet_size > 6)
1312 cmd = malloc((packet_size - 6)/2 + 1);
1313 for (i=0; i < (packet_size - 6)/2; i++)
1316 sscanf(packet + 6 + 2*i, "%2x", &tmp);
1319 cmd[(packet_size - 6)/2] = 0x0;
1320 target_call_timer_callbacks();
1321 command_run_line(cmd_ctx, cmd);
1324 gdb_put_packet(connection, "OK", 2);
1327 else if (strstr(packet, "qCRC:"))
1329 if (packet_size > 5)
1338 /* skip command character */
1341 addr = strtoul(packet, &separator, 16);
1343 if (*separator != ',')
1345 ERROR("incomplete read memory packet received, dropping connection");
1346 return ERROR_SERVER_REMOTE_CLOSED;
1349 len = strtoul(separator + 1, NULL, 16);
1351 retval = target_checksum_memory(target, addr, len, &checksum);
1353 if (retval == ERROR_OK)
1355 snprintf(gdb_reply, 10, "C%8.8x", checksum);
1356 gdb_put_packet(connection, gdb_reply, 9);
1360 if ((retval = gdb_memory_packet_error(connection, retval)) != ERROR_OK)
1367 else if (strstr(packet, "qSupported"))
1369 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1370 * disable qXfer:features:read for the moment */
1371 int retval = ERROR_OK;
1372 char *buffer = NULL;
1376 xml_printf(&retval, &buffer, &pos, &size,
1377 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1378 (GDB_BUFFER_SIZE - 1), gdb_use_memory_map == 1 ? '+' : '-');
1380 if (retval != ERROR_OK)
1382 gdb_send_error(connection, 01);
1386 gdb_put_packet(connection, buffer, strlen(buffer));
1391 else if (strstr(packet, "qXfer:memory-map:read::"))
1393 /* We get away with only specifying flash here. Regions that are not
1394 * specified are treated as if we provided no memory map(if not we
1395 * could detect the holes and mark them as RAM).
1396 * Normally we only execute this code once, but no big deal if we
1397 * have to regenerate it a couple of times. */
1403 int retval = ERROR_OK;
1409 /* skip command character */
1412 offset = strtoul(packet, &separator, 16);
1413 length = strtoul(separator + 1, &separator, 16);
1415 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1420 p = get_flash_bank_by_num(i);
1424 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1425 "<property name=\"blocksize\">0x%x</property>\n" \
1427 p->base, p->size, p->size/p->num_sectors);
1431 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1433 if (retval != ERROR_OK)
1435 gdb_send_error(connection, retval);
1439 if (offset + length > pos)
1441 length = pos - offset;
1444 char *t = malloc(length + 1);
1446 memcpy(t + 1, xml + offset, length);
1447 gdb_put_packet(connection, t, length + 1);
1453 else if (strstr(packet, "qXfer:features:read:"))
1458 int retval = ERROR_OK;
1464 /* skip command character */
1467 if (decode_xfer_read( packet, &annex, &offset, &length ) < 0)
1469 gdb_send_error(connection, 01);
1473 if (strcmp(annex, "target.xml") != 0)
1475 gdb_send_error(connection, 01);
1479 xml_printf(&retval, &xml, &pos, &size, \
1480 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1482 if (retval != ERROR_OK)
1484 gdb_send_error(connection, retval);
1488 gdb_put_packet(connection, xml, strlen(xml) + 1);
1494 gdb_put_packet(connection, "", 0);
1498 int gdb_v_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
1500 gdb_connection_t *gdb_connection = connection->priv;
1501 gdb_service_t *gdb_service = connection->service->priv;
1504 /* if flash programming disabled - send a empty reply */
1506 if (gdb_flash_program == 0)
1508 gdb_put_packet(connection, "", 0);
1512 if (strstr(packet, "vFlashErase:"))
1515 unsigned long length;
1517 char *parse = packet + 12;
1520 ERROR("incomplete vFlashErase packet received, dropping connection");
1521 return ERROR_SERVER_REMOTE_CLOSED;
1524 addr = strtoul(parse, &parse, 16);
1526 if (*(parse++) != ',' || *parse == '\0')
1528 ERROR("incomplete vFlashErase packet received, dropping connection");
1529 return ERROR_SERVER_REMOTE_CLOSED;
1532 length = strtoul(parse, &parse, 16);
1536 ERROR("incomplete vFlashErase packet received, dropping connection");
1537 return ERROR_SERVER_REMOTE_CLOSED;
1540 /* disable gdb output while programming */
1541 gdb_connection->output_disable = 1;
1543 /* assume all sectors need erasing - stops any problems
1544 * when flash_write is called multiple times */
1547 /* perform any target specific operations before the erase */
1548 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_PROGRAM);
1551 if ((result = flash_erase_address_range(gdb_service->target, addr, length)) != ERROR_OK)
1553 /* GDB doesn't evaluate the actual error number returned,
1554 * treat a failed erase as an I/O error
1556 gdb_send_error(connection, EIO);
1557 ERROR("flash_erase returned %i", result);
1560 gdb_put_packet(connection, "OK", 2);
1562 /* reenable gdb output */
1563 gdb_connection->output_disable = 0;
1568 if (strstr(packet, "vFlashWrite:"))
1571 unsigned long length;
1572 char *parse = packet + 12;
1576 ERROR("incomplete vFlashErase packet received, dropping connection");
1577 return ERROR_SERVER_REMOTE_CLOSED;
1579 addr = strtoul(parse, &parse, 16);
1580 if (*(parse++) != ':')
1582 ERROR("incomplete vFlashErase packet received, dropping connection");
1583 return ERROR_SERVER_REMOTE_CLOSED;
1585 length = packet_size - (parse - packet);
1587 /* disable gdb output while programming */
1588 gdb_connection->output_disable = 1;
1590 /* create a new image if there isn't already one */
1591 if (gdb_connection->vflash_image == NULL)
1593 gdb_connection->vflash_image = malloc(sizeof(image_t));
1594 image_open(gdb_connection->vflash_image, "", "build");
1597 /* create new section with content from packet buffer */
1598 image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (u8*)parse);
1600 gdb_put_packet(connection, "OK", 2);
1602 /* reenable gdb output */
1603 gdb_connection->output_disable = 0;
1608 if (!strcmp(packet, "vFlashDone"))
1613 /* disable gdb output while programming */
1614 gdb_connection->output_disable = 1;
1616 /* process the flashing buffer. No need to erase as GDB
1617 * always issues a vFlashErase first. */
1618 if ((result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, &error_str, NULL, 0)) != ERROR_OK)
1620 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1621 gdb_put_packet(connection, "E.memtype", 9);
1623 gdb_send_error(connection, EIO);
1627 ERROR("flash writing failed: %s", error_str);
1633 DEBUG("wrote %u bytes from vFlash image to flash", written);
1634 gdb_put_packet(connection, "OK", 2);
1637 image_close(gdb_connection->vflash_image);
1638 free(gdb_connection->vflash_image);
1639 gdb_connection->vflash_image = NULL;
1641 /* reenable gdb output */
1642 gdb_connection->output_disable = 0;
1647 gdb_put_packet(connection, "", 0);
1651 int gdb_detach(connection_t *connection, target_t *target)
1653 switch( detach_mode )
1655 case GDB_DETACH_RESUME:
1656 target->type->resume(target, 1, 0, 1, 0);
1659 case GDB_DETACH_RESET:
1660 target_process_reset(connection->cmd_ctx);
1663 case GDB_DETACH_HALT:
1664 target->type->halt(target);
1667 case GDB_DETACH_NOTHING:
1671 gdb_put_packet(connection, "OK", 2);
1676 int gdb_input(connection_t *connection)
1678 gdb_service_t *gdb_service = connection->service->priv;
1679 target_t *target = gdb_service->target;
1680 char packet[GDB_BUFFER_SIZE];
1683 gdb_connection_t *gdb_con = connection->priv;
1684 static int extended_protocol = 0;
1686 /* drain input buffer */
1689 packet_size = GDB_BUFFER_SIZE-1;
1690 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1694 case ERROR_GDB_BUFFER_TOO_SMALL:
1695 ERROR("BUG: buffer supplied for gdb packet was too small");
1697 case ERROR_SERVER_REMOTE_CLOSED:
1698 return ERROR_SERVER_REMOTE_CLOSED;
1700 ERROR("BUG: unexpected error");
1705 /* terminate with zero */
1706 packet[packet_size] = 0;
1708 DEBUG("received packet: '%s'", packet);
1710 if (packet_size > 0)
1716 /* Hct... -- set thread
1717 * we don't have threads, send empty reply */
1718 gdb_put_packet(connection, NULL, 0);
1721 retval = gdb_query_packet(connection, target, packet, packet_size);
1724 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
1727 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
1730 retval = gdb_get_register_packet(connection, target, packet, packet_size);
1733 retval = gdb_set_register_packet(connection, target, packet, packet_size);
1736 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
1739 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
1743 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1746 gdb_last_signal_packet(connection, target, packet, packet_size);
1750 gdb_step_continue_packet(connection, target, packet, packet_size);
1753 retval = gdb_v_packet(connection, target, packet, packet_size);
1756 retval = gdb_detach(connection, target);
1757 extended_protocol = 0;
1760 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
1764 if (extended_protocol != 0)
1766 gdb_put_packet(connection, "OK", 2);
1767 return ERROR_SERVER_REMOTE_CLOSED;
1769 /* handle extended remote protocol */
1770 extended_protocol = 1;
1771 gdb_put_packet(connection, "OK", 2);
1774 /* handle extended restart packet */
1775 target_process_reset(connection->cmd_ctx);
1778 /* ignore unkown packets */
1779 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1780 gdb_put_packet(connection, NULL, 0);
1784 /* if a packet handler returned an error, exit input loop */
1785 if (retval != ERROR_OK)
1789 if (gdb_con->ctrl_c)
1791 if (target->state == TARGET_RUNNING)
1793 target->type->halt(target);
1794 gdb_con->ctrl_c = 0;
1798 } while (gdb_con->buf_cnt > 0);
1805 gdb_service_t *gdb_service;
1806 target_t *target = targets;
1811 WARNING("no gdb ports allocated as no target has been specified");
1817 WARNING("no gdb port specified, using default port 3333");
1823 char service_name[8];
1825 snprintf(service_name, 8, "gdb-%2.2i", i);
1827 gdb_service = malloc(sizeof(gdb_service_t));
1828 gdb_service->target = target;
1830 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1832 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1835 target = target->next;
1841 /* daemon configuration command gdb_port */
1842 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1847 /* only if the port wasn't overwritten by cmdline */
1849 gdb_port = strtoul(args[0], NULL, 0);
1854 int handle_gdb_detach_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1858 if (strcmp(args[0], "resume") == 0)
1860 detach_mode = GDB_DETACH_RESUME;
1863 else if (strcmp(args[0], "reset") == 0)
1865 detach_mode = GDB_DETACH_RESET;
1868 else if (strcmp(args[0], "halt") == 0)
1870 detach_mode = GDB_DETACH_HALT;
1873 else if (strcmp(args[0], "nothing") == 0)
1875 detach_mode = GDB_DETACH_NOTHING;
1880 WARNING("invalid gdb_detach configuration directive: %s", args[0]);
1884 int handle_gdb_memory_map_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1888 if (strcmp(args[0], "enable") == 0)
1890 gdb_use_memory_map = 1;
1893 else if (strcmp(args[0], "disable") == 0)
1895 gdb_use_memory_map = 0;
1900 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1904 int handle_gdb_flash_program_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1908 if (strcmp(args[0], "enable") == 0)
1910 gdb_flash_program = 1;
1913 else if (strcmp(args[0], "disable") == 0)
1915 gdb_flash_program = 0;
1920 WARNING("invalid gdb_memory_map configuration directive: %s", args[0]);
1924 int gdb_register_commands(command_context_t *command_context)
1926 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
1927 COMMAND_CONFIG, "");
1928 register_command(command_context, NULL, "gdb_detach", handle_gdb_detach_command,
1929 COMMAND_CONFIG, "");
1930 register_command(command_context, NULL, "gdb_memory_map", handle_gdb_memory_map_command,
1931 COMMAND_CONFIG, "");
1932 register_command(command_context, NULL, "gdb_flash_program", handle_gdb_flash_program_command,
1933 COMMAND_CONFIG, "");