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 ***************************************************************************/
22 #include "gdb_server.h"
26 #include "binarybuffer.h"
27 #include "breakpoints.h"
38 char * strndup(char * str, int n) {
39 unsigned char * tmp = malloc((size_t)n+1);
40 if (! tmp) perror("gdb_server malloc failed");
41 if (strlcpy(tmp, str, n) > n) perror("gdb_server strndup: too long");
47 #define _DEBUG_GDB_IO_
50 static unsigned short gdb_port;
52 int gdb_last_signal(target_t *target)
54 switch (target->debug_reason)
56 case DBG_REASON_DBGRQ:
57 return 0x2; /* SIGINT */
58 case DBG_REASON_BREAKPOINT:
59 case DBG_REASON_WATCHPOINT:
60 case DBG_REASON_WPTANDBKPT:
61 return 0x05; /* SIGTRAP */
62 case DBG_REASON_SINGLESTEP:
63 return 0x05; /* SIGTRAP */
64 case DBG_REASON_NOTHALTED:
65 return 0x0; /* no signal... shouldn't happen */
67 ERROR("BUG: undefined debug reason");
72 int gdb_get_char(connection_t *connection, int* next_char)
74 gdb_connection_t *gdb_con = connection->priv;
77 if (gdb_con->buf_cnt-- > 0)
79 *next_char = *(gdb_con->buf_p++);
80 if (gdb_con->buf_cnt > 0)
81 connection->input_pending = 1;
83 connection->input_pending = 0;
86 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
92 while ((gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0)
94 if (gdb_con->buf_cnt == 0)
95 return ERROR_SERVER_REMOTE_CLOSED;
103 return ERROR_SERVER_REMOTE_CLOSED;
105 return ERROR_SERVER_REMOTE_CLOSED;
107 ERROR("read: %s", strerror(errno));
112 debug_buffer = malloc(gdb_con->buf_cnt + 1);
113 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
114 debug_buffer[gdb_con->buf_cnt] = 0;
115 DEBUG("received '%s'", debug_buffer);
118 gdb_con->buf_p = gdb_con->buffer;
120 *next_char = *(gdb_con->buf_p++);
121 if (gdb_con->buf_cnt > 0)
122 connection->input_pending = 1;
124 connection->input_pending = 0;
125 #ifdef _DEBUG_GDB_IO_
126 DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
132 int gdb_put_packet(connection_t *connection, char *buffer, int len)
135 unsigned char my_checksum = 0;
140 gdb_connection_t *gdb_con = connection->priv;
142 for (i = 0; i < len; i++)
143 my_checksum += buffer[i];
148 debug_buffer = malloc(len + 1);
149 memcpy(debug_buffer, buffer, len);
150 debug_buffer[len] = 0;
151 DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
154 write(connection->fd, "$", 1);
156 write(connection->fd, buffer, len);
157 write(connection->fd, "#", 1);
159 snprintf(checksum, 3, "%2.2x", my_checksum);
161 write(connection->fd, checksum, 2);
163 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
168 else if (reply == '-')
169 WARNING("negative reply, retrying");
170 else if (reply == 0x3)
173 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
177 else if (reply == '-')
178 WARNING("negative reply, retrying");
181 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
182 return ERROR_SERVER_REMOTE_CLOSED;
187 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply);
188 return ERROR_SERVER_REMOTE_CLOSED;
195 int gdb_get_packet(connection_t *connection, char *buffer, int *len)
203 unsigned char my_checksum = 0;
204 gdb_connection_t *gdb_con = connection->priv;
210 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
218 WARNING("acknowledgment received, but no packet pending");
221 WARNING("negative acknowledgment, but no packet pending");
228 WARNING("ignoring character 0x%x", character);
231 } while (character != '$');
237 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
241 packet_type = character;
245 if( packet_type == 'X' )
252 /* data transmitted in binary mode (X packet)
253 * uses 0x7d as escape character */
254 my_checksum += character & 0xff;
255 gdb_get_char(connection, &character);
256 my_checksum += character & 0xff;
257 buffer[count++] = (character ^ 0x20) & 0xff;
260 ERROR("packet buffer too small");
261 return ERROR_GDB_BUFFER_TOO_SMALL;
265 buffer[count++] = character & 0xff;
266 my_checksum += character & 0xff;
269 ERROR("packet buffer too small");
270 return ERROR_GDB_BUFFER_TOO_SMALL;
285 buffer[count++] = character & 0xff;
286 my_checksum += character & 0xff;
289 ERROR("packet buffer too small");
290 return ERROR_GDB_BUFFER_TOO_SMALL;
295 } while (character != '#');
299 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
301 checksum[0] = character;
302 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
304 checksum[1] = character;
307 if (my_checksum == strtoul(checksum, NULL, 16))
309 write (connection->fd, "+", 1);
313 WARNING("checksum error, requesting retransmission");
314 write(connection->fd, "-", 1);
320 int gdb_output(struct command_context_s *context, char* line)
322 connection_t *connection = context->output_handler_priv;
326 bin_size = strlen(line);
328 hex_buffer = malloc(bin_size*2 + 4);
331 for (i=0; i<bin_size; i++)
332 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
333 hex_buffer[bin_size*2+1] = '0';
334 hex_buffer[bin_size*2+2] = 'a';
335 hex_buffer[bin_size*2+3] = 0x0;
337 gdb_put_packet(connection, hex_buffer, bin_size*2 + 3);
343 int gdb_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
345 connection_t *connection = priv;
346 gdb_connection_t *gdb_connection = connection->priv;
352 case TARGET_EVENT_HALTED:
353 if (gdb_connection->frontend_state == TARGET_RUNNING)
355 if (gdb_connection->ctrl_c)
358 gdb_connection->ctrl_c = 0;
362 signal = gdb_last_signal(target);
365 snprintf(sig_reply, 4, "T%2.2x", signal);
366 gdb_put_packet(connection, sig_reply, 3);
367 gdb_connection->frontend_state = TARGET_HALTED;
370 case TARGET_EVENT_RESUMED:
371 if (gdb_connection->frontend_state == TARGET_HALTED)
373 gdb_connection->frontend_state = TARGET_RUNNING;
383 int gdb_new_connection(connection_t *connection)
385 gdb_connection_t *gdb_connection = malloc(sizeof(gdb_connection_t));
386 gdb_service_t *gdb_service = connection->service->priv;
390 connection->priv = gdb_connection;
392 /* initialize gdb connection information */
393 gdb_connection->buf_p = gdb_connection->buffer;
394 gdb_connection->buf_cnt = 0;
395 gdb_connection->ctrl_c = 0;
396 gdb_connection->frontend_state = TARGET_HALTED;
398 /* output goes through gdb connection */
399 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
401 /* register callback to be informed about target events */
402 target_register_event_callback(gdb_target_callback_event_handler, connection);
404 /* a gdb session just attached, put the target in halt mode */
405 if (((retval = gdb_service->target->type->halt(gdb_service->target)) != ERROR_OK) &&
406 (retval != ERROR_TARGET_ALREADY_HALTED))
408 ERROR("error when trying to halt target");
412 while (gdb_service->target->state != TARGET_HALTED)
414 gdb_service->target->type->poll(gdb_service->target);
417 /* remove the initial ACK from the incoming buffer */
418 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
424 int gdb_connection_closed(connection_t *connection)
426 if (connection->priv)
427 free(connection->priv);
429 ERROR("BUG: connection->priv == NULL");
431 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
436 int gdb_last_signal_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
441 signal = gdb_last_signal(target);
443 snprintf(sig_reply, 4, "S%2.2x", signal);
444 gdb_put_packet(connection, sig_reply, 3);
449 void gdb_get_registers_packet(connection_t *connection, target_t *target, char* packet, int packet_size)
454 int reg_packet_size = 0;
461 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
465 case ERROR_TARGET_NOT_HALTED:
466 ERROR("gdb requested registers, but we're not halted");
469 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
474 for (i = 0; i < reg_list_size; i++)
476 reg_packet_size += reg_list[i]->size;
479 reg_packet = malloc(CEIL(reg_packet_size, 8) * 2);
480 reg_packet_p = reg_packet;
482 for (i = 0; i < reg_list_size; i++)
485 char *hex_buf = buf_to_char(reg_list[i]->value, reg_list[i]->size);
486 DEBUG("hex_buf: %s", hex_buf);
487 for (j = CEIL(reg_list[i]->size, 8) * 2; j > 0; j -= 2)
489 *reg_packet_p++ = hex_buf[j - 2];
490 *reg_packet_p++ = hex_buf[j - 1];
495 reg_packet_p = strndup(reg_packet, CEIL(reg_packet_size, 8) * 2);
496 DEBUG("reg_packet: %s", reg_packet_p);
499 gdb_put_packet(connection, reg_packet, CEIL(reg_packet_size, 8) * 2);
504 void gdb_set_registers_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
514 /* skip command character */
520 WARNING("GDB set_registers packet with uneven characters received");
524 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
528 case ERROR_TARGET_NOT_HALTED:
529 ERROR("gdb requested registers, but we're not halted");
532 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
538 for (i = 0; i < reg_list_size; i++)
540 char_to_buf(packet, CEIL(reg_list[i]->size, 8) * 2, reg_list[i]->value, reg_list[i]->size);
541 reg_list[i]->dirty = 1;
544 gdb_put_packet(connection, "OK", 2);
547 void gdb_get_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
552 int reg_num = strtoul(packet + 1, NULL, 16);
560 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
564 case ERROR_TARGET_NOT_HALTED:
565 ERROR("gdb requested registers, but we're not halted");
568 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
573 if (reg_list_size <= reg_num)
575 ERROR("gdb requested a non-existing register");
579 hex_buf = buf_to_char(reg_list[reg_num]->value, reg_list[reg_num]->size);
580 reg_packet = reg_packet_p = malloc(CEIL(reg_list[reg_num]->size, 8) * 2);
582 for (i = CEIL(reg_list[reg_num]->size, 8) * 2; i > 0; i -= 2)
584 *reg_packet_p++ = hex_buf[i - 2];
585 *reg_packet_p++ = hex_buf[i - 1];
588 gdb_put_packet(connection, reg_packet, CEIL(reg_list[reg_num]->size, 8) * 2);
595 void gdb_set_register_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
598 int reg_num = strtoul(packet + 1, &separator, 16);
605 if ((retval = target->type->get_gdb_reg_list(target, ®_list, ®_list_size)) != ERROR_OK)
609 case ERROR_TARGET_NOT_HALTED:
610 ERROR("gdb requested registers, but we're not halted");
613 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
618 if (reg_list_size < reg_num)
620 ERROR("gdb requested a non-existing register");
624 if (*separator != '=')
626 ERROR("GDB set register packet, but no '=' following the register number");
630 char_to_buf(separator + 1, CEIL(reg_list[reg_num]->size, 8) * 2, reg_list[reg_num]->value, reg_list[reg_num]->size);
631 reg_list[reg_num]->dirty = 1;
633 gdb_put_packet(connection, "OK", 2);
637 void gdb_read_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
648 /* skip command character */
651 addr = strtoul(packet, &separator, 16);
653 if (*separator != ',')
656 len = strtoul(separator+1, NULL, 16);
658 buffer = malloc(len);
660 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
666 target->type->read_memory(target, addr, 4, 1, buffer);
668 target->type->read_memory(target, addr, 1, len, buffer);
672 target->type->read_memory(target, addr, 2, 1, buffer);
674 target->type->read_memory(target, addr, 1, len, buffer);
677 if (((addr % 4) == 0) && ((len % 4) == 0))
678 target->type->read_memory(target, addr, 4, len / 4, buffer);
680 target->type->read_memory(target, addr, 1, len, buffer);
683 hex_buffer = malloc(len * 2 + 1);
685 for (i=0; i<len; i++)
686 snprintf(hex_buffer + 2*i, 3, "%2.2x", buffer[i]);
688 gdb_put_packet(connection, hex_buffer, len * 2);
694 void gdb_write_memory_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
704 /* skip command character */
707 addr = strtoul(packet, &separator, 16);
709 if (*separator != ',')
712 len = strtoul(separator+1, &separator, 16);
714 if (*(separator++) != ':')
717 buffer = malloc(len);
719 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
721 for (i=0; i<len; i++)
724 sscanf(separator + 2*i, "%2x", &tmp);
730 /* handle sized writes */
733 target->type->write_memory(target, addr, 4, 1, buffer);
735 target->type->write_memory(target, addr, 1, len, buffer);
739 target->type->write_memory(target, addr, 2, 1, buffer);
741 target->type->write_memory(target, addr, 1, len, buffer);
745 target->type->write_memory(target, addr, 1, len, buffer);
747 /* handle bulk writes */
749 target_write_buffer(target, addr, len, buffer);
753 gdb_put_packet(connection, "OK", 2);
758 void gdb_write_memory_binary_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
766 /* skip command character */
769 addr = strtoul(packet, &separator, 16);
771 if (*separator != ',')
774 len = strtoul(separator+1, &separator, 16);
776 if (*(separator++) != ':')
781 buffer = malloc(len);
783 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr, len);
785 memcpy( buffer, separator, len );
791 target->type->write_memory(target, addr, 4, 1, buffer);
793 target->type->write_memory(target, addr, 1, len, buffer);
797 target->type->write_memory(target, addr, 2, 1, buffer);
799 target->type->write_memory(target, addr, 1, len, buffer);
803 target->type->write_memory(target, addr, 1, len, buffer);
806 target_write_buffer(target, addr, len, buffer);
813 gdb_put_packet(connection, "OK", 2);
816 void gdb_step_continue_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
826 packet[packet_size] = 0;
827 address = strtoul(packet + 1, NULL, 16);
834 if (packet[0] == 'c')
837 target->type->resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
839 else if (packet[0] == 's')
842 target->type->step(target, current, address, 0); /* step at current or address, don't handle breakpoints */
846 void gdb_breakpoint_watchpoint_packet(connection_t *connection, target_t *target, char *packet, int packet_size)
849 enum breakpoint_type bp_type;
850 enum watchpoint_rw wp_type;
858 type = strtoul(packet + 1, &separator, 16);
860 if (type == 0) /* memory breakpoint */
862 else if (type == 1) /* hardware breakpoint */
864 else if (type == 2) /* write watchpoint */
866 else if (type == 3) /* read watchpoint */
868 else if (type == 4) /* access watchpoint */
869 wp_type = WPT_ACCESS;
871 if (*separator != ',')
874 address = strtoul(separator+1, &separator, 16);
876 if (*separator != ',')
879 size = strtoul(separator+1, &separator, 16);
885 if (packet[0] == 'Z')
887 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
889 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
891 gdb_put_packet(connection, "E00", 3);
898 breakpoint_remove(target, address);
900 gdb_put_packet(connection, "OK", 2);
906 if (packet[0] == 'Z')
907 watchpoint_add(target, address, size, type-2, 0, 0xffffffffu);
909 watchpoint_remove(target, address);
910 gdb_put_packet(connection, "OK", 2);
919 void gdb_query_packet(connection_t *connection, char *packet, int packet_size)
921 command_context_t *cmd_ctx = connection->cmd_ctx;
922 gdb_service_t *gdb_service = connection->service->priv;
923 target_t *target = gdb_service->target;
925 if (strstr(packet, "qRcmd,"))
931 cmd = malloc((packet_size - 6)/2 + 1);
932 for (i=0; i < (packet_size - 6)/2; i++)
935 sscanf(packet + 6 + 2*i, "%2x", &tmp);
938 cmd[(packet_size - 6)/2] = 0x0;
939 command_run_line(cmd_ctx, cmd);
942 gdb_put_packet(connection, "OK", 2);
946 gdb_put_packet(connection, "", 0);
949 int gdb_input(connection_t *connection)
951 gdb_service_t *gdb_service = connection->service->priv;
952 target_t *target = gdb_service->target;
953 char packet[GDB_BUFFER_SIZE];
956 gdb_connection_t *gdb_con = connection->priv;
958 /* drain input buffer */
961 packet_size = GDB_BUFFER_SIZE-1;
962 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
966 case ERROR_GDB_BUFFER_TOO_SMALL:
967 ERROR("BUG: buffer supplied for gdb packet was too small");
969 case ERROR_SERVER_REMOTE_CLOSED:
970 return ERROR_SERVER_REMOTE_CLOSED;
972 ERROR("unexpected error");
977 /* terminate with zero */
978 packet[packet_size] = 0;
980 DEBUG("recevied packet: '%s'", packet);
987 /* Hct... -- set thread
988 * we don't have threads, send empty reply */
989 gdb_put_packet(connection, NULL, 0);
992 gdb_query_packet(connection, packet, packet_size);
995 gdb_get_registers_packet(connection, target, packet, packet_size);
998 gdb_set_registers_packet(connection, target, packet, packet_size);
1001 gdb_get_register_packet(connection, target, packet, packet_size);
1004 gdb_set_register_packet(connection, target, packet, packet_size);
1007 gdb_read_memory_packet(connection, target, packet, packet_size);
1010 gdb_write_memory_packet(connection, target, packet, packet_size);
1014 gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
1017 gdb_last_signal_packet(connection, target, packet, packet_size);
1021 gdb_step_continue_packet(connection, target, packet, packet_size);
1024 target->type->resume(target, 1, 0, 1, 0);
1025 gdb_put_packet(connection, "OK", 2);
1028 gdb_write_memory_binary_packet(connection, target, packet, packet_size);
1031 gdb_put_packet(connection, "OK", 2);
1032 return ERROR_SERVER_REMOTE_CLOSED;
1034 /* ignore unkown packets */
1035 DEBUG("ignoring 0x%2.2x packet", packet[0]);
1036 gdb_put_packet(connection, NULL, 0);
1041 if (gdb_con->ctrl_c)
1043 if (target->state == TARGET_RUNNING)
1045 target->type->halt(target);
1046 gdb_con->ctrl_c = 0;
1050 } while (gdb_con->buf_cnt > 0);
1057 gdb_service_t *gdb_service;
1058 target_t *target = targets;
1063 WARNING("no gdb ports allocated as no target has been specified");
1069 WARNING("no gdb port specified, using default port 3333");
1075 char service_name[8];
1077 snprintf(service_name, 8, "gdb-%2.2i", i);
1079 gdb_service = malloc(sizeof(gdb_service_t));
1080 gdb_service->target = target;
1082 add_service("gdb", CONNECTION_GDB, gdb_port + i, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
1084 DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i);
1086 target = target->next;
1092 /* daemon configuration command gdb_port */
1093 int handle_gdb_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1098 /* only if the port wasn't overwritten by cmdline */
1100 gdb_port = strtoul(args[0], NULL, 0);
1105 int gdb_register_commands(command_context_t *command_context)
1107 register_command(command_context, NULL, "gdb_port", handle_gdb_port_command,
1108 COMMAND_CONFIG, "");