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"
31 #include "breakpoints.h"
40 #define _DEBUG_GDB_IO_
43 static unsigned short gdb_port
;
45 int gdb_last_signal(target_t
*target
)
47 switch (target
->debug_reason
)
49 case DBG_REASON_DBGRQ
:
50 return 0x2; /* SIGINT */
51 case DBG_REASON_BREAKPOINT
:
52 case DBG_REASON_WATCHPOINT
:
53 case DBG_REASON_WPTANDBKPT
:
54 return 0x05; /* SIGTRAP */
55 case DBG_REASON_SINGLESTEP
:
56 return 0x05; /* SIGTRAP */
57 case DBG_REASON_NOTHALTED
:
58 return 0x0; /* no signal... shouldn't happen */
60 ERROR("BUG: undefined debug reason");
65 int gdb_get_char(connection_t
*connection
, int* next_char
)
67 gdb_connection_t
*gdb_con
= connection
->priv
;
70 if (gdb_con
->buf_cnt
-- > 0)
72 *next_char
= *(gdb_con
->buf_p
++);
73 if (gdb_con
->buf_cnt
> 0)
74 connection
->input_pending
= 1;
76 connection
->input_pending
= 0;
79 DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
85 while ((gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
)) <= 0)
87 if (gdb_con
->buf_cnt
== 0)
88 return ERROR_SERVER_REMOTE_CLOSED
;
91 errno
= WSAGetLastError();
99 return ERROR_SERVER_REMOTE_CLOSED
;
101 ERROR("read: %d", errno
);
111 return ERROR_SERVER_REMOTE_CLOSED
;
113 return ERROR_SERVER_REMOTE_CLOSED
;
115 ERROR("read: %s", strerror(errno
));
121 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
122 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
123 debug_buffer
[gdb_con
->buf_cnt
] = 0;
124 DEBUG("received '%s'", debug_buffer
);
127 gdb_con
->buf_p
= gdb_con
->buffer
;
129 *next_char
= *(gdb_con
->buf_p
++);
130 if (gdb_con
->buf_cnt
> 0)
131 connection
->input_pending
= 1;
133 connection
->input_pending
= 0;
134 #ifdef _DEBUG_GDB_IO_
135 DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
141 int gdb_putback_char(connection_t
*connection
, int last_char
)
143 gdb_connection_t
*gdb_con
= connection
->priv
;
145 if (gdb_con
->buf_p
> gdb_con
->buffer
)
147 *(--gdb_con
->buf_p
) = last_char
;
152 ERROR("BUG: couldn't put character back");
158 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
161 unsigned char my_checksum
= 0;
166 gdb_connection_t
*gdb_con
= connection
->priv
;
168 for (i
= 0; i
< len
; i
++)
169 my_checksum
+= buffer
[i
];
174 debug_buffer
= malloc(len
+ 1);
175 memcpy(debug_buffer
, buffer
, len
);
176 debug_buffer
[len
] = 0;
177 DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
180 write_socket(connection
->fd
, "$", 1);
182 write_socket(connection
->fd
, buffer
, len
);
183 write_socket(connection
->fd
, "#", 1);
185 snprintf(checksum
, 3, "%2.2x", my_checksum
);
187 write_socket(connection
->fd
, checksum
, 2);
189 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
194 else if (reply
== '-')
195 WARNING("negative reply, retrying");
196 else if (reply
== 0x3)
199 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
203 else if (reply
== '-')
204 WARNING("negative reply, retrying");
207 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
208 return ERROR_SERVER_REMOTE_CLOSED
;
213 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
214 return ERROR_SERVER_REMOTE_CLOSED
;
221 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
229 unsigned char my_checksum
= 0;
230 gdb_connection_t
*gdb_con
= connection
->priv
;
236 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
239 DEBUG("character: '%c'", character
);
246 WARNING("acknowledgment received, but no packet pending");
249 WARNING("negative acknowledgment, but no packet pending");
256 WARNING("ignoring character 0x%x", character
);
259 } while (character
!= '$');
265 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
269 packet_type
= character
;
273 if( packet_type
== 'X' )
280 /* data transmitted in binary mode (X packet)
281 * uses 0x7d as escape character */
282 my_checksum
+= character
& 0xff;
283 gdb_get_char(connection
, &character
);
284 my_checksum
+= character
& 0xff;
285 buffer
[count
++] = (character
^ 0x20) & 0xff;
288 ERROR("packet buffer too small");
289 return ERROR_GDB_BUFFER_TOO_SMALL
;
293 buffer
[count
++] = character
& 0xff;
294 my_checksum
+= character
& 0xff;
297 ERROR("packet buffer too small");
298 return ERROR_GDB_BUFFER_TOO_SMALL
;
313 buffer
[count
++] = character
& 0xff;
314 my_checksum
+= character
& 0xff;
317 ERROR("packet buffer too small");
318 return ERROR_GDB_BUFFER_TOO_SMALL
;
323 } while (character
!= '#');
327 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
329 checksum
[0] = character
;
330 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
332 checksum
[1] = character
;
335 if (my_checksum
== strtoul(checksum
, NULL
, 16))
337 write_socket(connection
->fd
, "+", 1);
341 WARNING("checksum error, requesting retransmission");
342 write_socket(connection
->fd
, "-", 1);
348 int gdb_output(struct command_context_s
*context
, char* line
)
350 connection_t
*connection
= context
->output_handler_priv
;
354 bin_size
= strlen(line
);
356 hex_buffer
= malloc(bin_size
*2 + 4);
359 for (i
=0; i
<bin_size
; i
++)
360 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
361 hex_buffer
[bin_size
*2+1] = '0';
362 hex_buffer
[bin_size
*2+2] = 'a';
363 hex_buffer
[bin_size
*2+3] = 0x0;
365 gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 3);
371 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
373 connection_t
*connection
= priv
;
374 gdb_connection_t
*gdb_connection
= connection
->priv
;
380 case TARGET_EVENT_HALTED
:
381 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
383 if (gdb_connection
->ctrl_c
)
386 gdb_connection
->ctrl_c
= 0;
390 signal
= gdb_last_signal(target
);
393 snprintf(sig_reply
, 4, "T%2.2x", signal
);
394 gdb_put_packet(connection
, sig_reply
, 3);
395 gdb_connection
->frontend_state
= TARGET_HALTED
;
398 case TARGET_EVENT_RESUMED
:
399 if (gdb_connection
->frontend_state
== TARGET_HALTED
)
401 gdb_connection
->frontend_state
= TARGET_RUNNING
;
411 int gdb_new_connection(connection_t
*connection
)
413 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
414 gdb_service_t
*gdb_service
= connection
->service
->priv
;
418 connection
->priv
= gdb_connection
;
420 /* initialize gdb connection information */
421 gdb_connection
->buf_p
= gdb_connection
->buffer
;
422 gdb_connection
->buf_cnt
= 0;
423 gdb_connection
->ctrl_c
= 0;
424 gdb_connection
->frontend_state
= TARGET_HALTED
;
426 /* output goes through gdb connection */
427 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
429 /* register callback to be informed about target events */
430 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
432 /* a gdb session just attached, put the target in halt mode */
433 if (((retval
= gdb_service
->target
->type
->halt(gdb_service
->target
)) != ERROR_OK
) &&
434 (retval
!= ERROR_TARGET_ALREADY_HALTED
))
436 ERROR("error when trying to halt target");
440 while (gdb_service
->target
->state
!= TARGET_HALTED
)
442 gdb_service
->target
->type
->poll(gdb_service
->target
);
445 /* remove the initial ACK from the incoming buffer */
446 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
449 if (initial_ack
!= '+')
450 gdb_putback_char(connection
, initial_ack
);
455 int gdb_connection_closed(connection_t
*connection
)
457 if (connection
->priv
)
458 free(connection
->priv
);
460 ERROR("BUG: connection->priv == NULL");
462 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
467 void gdb_send_error(connection_t
*connection
, u8 the_error
)
470 snprintf(err
, 4, "E%2.2X", the_error
);
471 gdb_put_packet(connection
, err
, 3);
474 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
479 signal
= gdb_last_signal(target
);
481 snprintf(sig_reply
, 4, "S%2.2x", signal
);
482 gdb_put_packet(connection
, sig_reply
, 3);
487 void gdb_str_to_target(target_t
*target
, char *str
, char *tstr
)
489 int str_len
= strlen(str
);
494 ERROR("BUG: gdb value with uneven number of characters encountered: %s", str
);
498 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
500 for (i
= 0; i
< str_len
; i
+=2)
502 tstr
[str_len
- i
- 1] = str
[i
+ 1];
503 tstr
[str_len
- i
- 2] = str
[i
];
508 for (i
= 0; i
< str_len
; i
++)
515 void gdb_target_to_str(target_t
*target
, char *tstr
, char *str
)
517 int str_len
= strlen(tstr
);
522 ERROR("BUG: gdb value with uneven number of characters encountered");
526 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
528 for (i
= 0; i
< str_len
; i
+=2)
530 str
[str_len
- i
- 1] = tstr
[i
+ 1];
531 str
[str_len
- i
- 2] = tstr
[i
];
536 for (i
= 0; i
< str_len
; i
++)
543 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
548 int reg_packet_size
= 0;
555 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
559 case ERROR_TARGET_NOT_HALTED
:
560 ERROR("gdb requested registers but we're not halted, dropping connection");
561 return ERROR_SERVER_REMOTE_CLOSED
;
563 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
564 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
569 for (i
= 0; i
< reg_list_size
; i
++)
571 reg_packet_size
+= reg_list
[i
]->size
;
574 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
575 reg_packet_p
= reg_packet
;
577 for (i
= 0; i
< reg_list_size
; i
++)
579 char *hex_buf
= buf_to_str(reg_list
[i
]->value
, reg_list
[i
]->size
, 16);
580 DEBUG("hex_buf: %s", hex_buf
);
581 gdb_str_to_target(target
, hex_buf
, reg_packet_p
);
582 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
586 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
587 DEBUG("reg_packet: %s", reg_packet_p
);
590 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
598 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
608 /* skip command character */
614 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
615 return ERROR_SERVER_REMOTE_CLOSED
;
618 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
622 case ERROR_TARGET_NOT_HALTED
:
623 ERROR("gdb tried to registers but we're not halted, dropping connection");
624 return ERROR_SERVER_REMOTE_CLOSED
;
626 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
627 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
633 for (i
= 0; i
< reg_list_size
; i
++)
637 reg_arch_type_t
*arch_type
;
639 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
640 hex_buf
= malloc(CEIL(reg_list
[i
]->size
, 8) * 2);
641 gdb_target_to_str(target
, packet_p
, hex_buf
);
643 /* convert hex-string to binary buffer */
644 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
645 str_to_buf(hex_buf
, CEIL(reg_list
[i
]->size
, 8) * 2, bin_buf
, reg_list
[i
]->size
, 16);
647 /* get register arch_type, and call set method */
648 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
649 if (arch_type
== NULL
)
651 ERROR("BUG: encountered unregistered arch type");
654 arch_type
->set(reg_list
[i
], bin_buf
);
656 /* advance packet pointer */
657 packet_p
+= (CEIL(reg_list
[i
]->size
, 8) * 2);
663 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
666 gdb_put_packet(connection
, "OK", 2);
671 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
674 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
682 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
686 case ERROR_TARGET_NOT_HALTED
:
687 ERROR("gdb requested registers but we're not halted, dropping connection");
688 return ERROR_SERVER_REMOTE_CLOSED
;
690 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
691 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
696 if (reg_list_size
<= reg_num
)
698 ERROR("gdb requested a non-existing register");
702 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
704 hex_buf
= buf_to_str(reg_list
[reg_num
]->value
, reg_list
[reg_num
]->size
, 16);
706 gdb_str_to_target(target
, hex_buf
, reg_packet
);
708 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
717 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
722 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
726 reg_arch_type_t
*arch_type
;
730 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
734 case ERROR_TARGET_NOT_HALTED
:
735 ERROR("gdb tried to set a register but we're not halted, dropping connection");
736 return ERROR_SERVER_REMOTE_CLOSED
;
738 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
739 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
744 if (reg_list_size
< reg_num
)
746 ERROR("gdb requested a non-existing register");
747 return ERROR_SERVER_REMOTE_CLOSED
;
750 if (*separator
!= '=')
752 ERROR("GDB 'set register packet', but no '=' following the register number");
753 return ERROR_SERVER_REMOTE_CLOSED
;
756 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
757 hex_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
758 gdb_target_to_str(target
, separator
+ 1, hex_buf
);
760 /* convert hex-string to binary buffer */
761 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
762 str_to_buf(hex_buf
, CEIL(reg_list
[reg_num
]->size
, 8) * 2, bin_buf
, reg_list
[reg_num
]->size
, 16);
764 /* get register arch_type, and call set method */
765 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
766 if (arch_type
== NULL
)
768 ERROR("BUG: encountered unregistered arch type");
771 arch_type
->set(reg_list
[reg_num
], bin_buf
);
773 gdb_put_packet(connection
, "OK", 2);
782 int gdb_memory_packet_error(connection_t
*connection
, int retval
)
786 case ERROR_TARGET_NOT_HALTED
:
787 ERROR("gdb tried to read memory but we're not halted, dropping connection");
788 return ERROR_SERVER_REMOTE_CLOSED
;
790 case ERROR_TARGET_DATA_ABORT
:
791 gdb_send_error(connection
, EIO
);
793 case ERROR_TARGET_TRANSLATION_FAULT
:
794 gdb_send_error(connection
, EFAULT
);
796 case ERROR_TARGET_UNALIGNED_ACCESS
:
797 gdb_send_error(connection
, EFAULT
);
800 ERROR("BUG: unexpected error %i", retval
);
807 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
819 /* skip command character */
822 addr
= strtoul(packet
, &separator
, 16);
824 if (*separator
!= ',')
826 ERROR("incomplete read memory packet received, dropping connection");
827 return ERROR_SERVER_REMOTE_CLOSED
;
830 len
= strtoul(separator
+1, NULL
, 16);
832 buffer
= malloc(len
);
834 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
840 retval
= target
->type
->read_memory(target
, addr
, 4, 1, buffer
);
842 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
846 retval
= target
->type
->read_memory(target
, addr
, 2, 1, buffer
);
848 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
851 if (((addr
% 4) == 0) && ((len
% 4) == 0))
852 retval
= target
->type
->read_memory(target
, addr
, 4, len
/ 4, buffer
);
854 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
857 if (retval
== ERROR_OK
)
859 hex_buffer
= malloc(len
* 2 + 1);
861 for (i
=0; i
<len
; i
++)
862 snprintf(hex_buffer
+ 2*i
, 3, "%2.2x", buffer
[i
]);
864 gdb_put_packet(connection
, hex_buffer
, len
* 2);
870 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
879 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
890 /* skip command character */
893 addr
= strtoul(packet
, &separator
, 16);
895 if (*separator
!= ',')
897 ERROR("incomplete write memory packet received, dropping connection");
898 return ERROR_SERVER_REMOTE_CLOSED
;
901 len
= strtoul(separator
+1, &separator
, 16);
903 if (*(separator
++) != ':')
905 ERROR("incomplete write memory packet received, dropping connection");
906 return ERROR_SERVER_REMOTE_CLOSED
;
909 buffer
= malloc(len
);
911 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
913 for (i
=0; i
<len
; i
++)
916 sscanf(separator
+ 2*i
, "%2x", &tmp
);
923 /* handle sized writes */
926 retval
= target
->type
->write_memory(target
, addr
, 4, 1, buffer
);
928 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
932 retval
= target
->type
->write_memory(target
, addr
, 2, 1, buffer
);
934 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
938 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
940 /* handle bulk writes */
942 retval
= target_write_buffer(target
, addr
, len
, buffer
);
946 if (retval
== ERROR_OK
)
948 gdb_put_packet(connection
, "OK", 2);
952 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
961 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
970 /* skip command character */
973 addr
= strtoul(packet
, &separator
, 16);
975 if (*separator
!= ',')
977 ERROR("incomplete write memory binary packet received, dropping connection");
978 return ERROR_SERVER_REMOTE_CLOSED
;
981 len
= strtoul(separator
+1, &separator
, 16);
983 if (*(separator
++) != ':')
985 ERROR("incomplete write memory binary packet received, dropping connection");
986 return ERROR_SERVER_REMOTE_CLOSED
;
992 buffer
= malloc(len
);
994 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
996 memcpy( buffer
, separator
, len
);
1001 if ((addr
% 4) == 0)
1002 retval
= target
->type
->write_memory(target
, addr
, 4, 1, buffer
);
1004 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1007 if ((addr
% 2) == 0)
1008 retval
= target
->type
->write_memory(target
, addr
, 2, 1, buffer
);
1010 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1014 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1017 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1024 if (retval
== ERROR_OK
)
1026 gdb_put_packet(connection
, "OK", 2);
1030 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
1037 void gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1044 if (packet_size
> 1)
1046 packet
[packet_size
] = 0;
1047 address
= strtoul(packet
+ 1, NULL
, 16);
1054 if (packet
[0] == 'c')
1057 target
->type
->resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1059 else if (packet
[0] == 's')
1062 target
->type
->step(target
, current
, address
, 0); /* step at current or address, don't handle breakpoints */
1066 int gdb_bp_wp_packet_error(connection_t
*connection
, int retval
)
1070 case ERROR_TARGET_NOT_HALTED
:
1071 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1072 return ERROR_SERVER_REMOTE_CLOSED
;
1074 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE
:
1075 gdb_send_error(connection
, EBUSY
);
1078 ERROR("BUG: unexpected error %i", retval
);
1085 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1088 enum breakpoint_type bp_type
;
1089 enum watchpoint_rw wp_type
;
1097 type
= strtoul(packet
+ 1, &separator
, 16);
1099 if (type
== 0) /* memory breakpoint */
1100 bp_type
= BKPT_SOFT
;
1101 else if (type
== 1) /* hardware breakpoint */
1102 bp_type
= BKPT_HARD
;
1103 else if (type
== 2) /* write watchpoint */
1104 wp_type
= WPT_WRITE
;
1105 else if (type
== 3) /* read watchpoint */
1107 else if (type
== 4) /* access watchpoint */
1108 wp_type
= WPT_ACCESS
;
1110 if (*separator
!= ',')
1112 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1113 return ERROR_SERVER_REMOTE_CLOSED
;
1116 address
= strtoul(separator
+1, &separator
, 16);
1118 if (*separator
!= ',')
1120 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1121 return ERROR_SERVER_REMOTE_CLOSED
;
1124 size
= strtoul(separator
+1, &separator
, 16);
1130 if (packet
[0] == 'Z')
1132 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1134 if ((retval
= gdb_bp_wp_packet_error(connection
, retval
)) != ERROR_OK
)
1139 gdb_put_packet(connection
, "OK", 2);
1144 breakpoint_remove(target
, address
);
1145 gdb_put_packet(connection
, "OK", 2);
1152 if (packet
[0] == 'Z')
1154 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1156 if ((retval
= gdb_bp_wp_packet_error(connection
, retval
)) != ERROR_OK
)
1161 gdb_put_packet(connection
, "OK", 2);
1166 watchpoint_remove(target
, address
);
1167 gdb_put_packet(connection
, "OK", 2);
1178 void gdb_query_packet(connection_t
*connection
, char *packet
, int packet_size
)
1180 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1182 if (strstr(packet
, "qRcmd,"))
1184 if (packet_size
> 6)
1188 cmd
= malloc((packet_size
- 6)/2 + 1);
1189 for (i
=0; i
< (packet_size
- 6)/2; i
++)
1192 sscanf(packet
+ 6 + 2*i
, "%2x", &tmp
);
1195 cmd
[(packet_size
- 6)/2] = 0x0;
1196 command_run_line(cmd_ctx
, cmd
);
1199 gdb_put_packet(connection
, "OK", 2);
1203 gdb_put_packet(connection
, "", 0);
1206 int gdb_input(connection_t
*connection
)
1208 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1209 target_t
*target
= gdb_service
->target
;
1210 char packet
[GDB_BUFFER_SIZE
];
1213 gdb_connection_t
*gdb_con
= connection
->priv
;
1215 /* drain input buffer */
1218 packet_size
= GDB_BUFFER_SIZE
-1;
1219 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
1223 case ERROR_GDB_BUFFER_TOO_SMALL
:
1224 ERROR("BUG: buffer supplied for gdb packet was too small");
1226 case ERROR_SERVER_REMOTE_CLOSED
:
1227 return ERROR_SERVER_REMOTE_CLOSED
;
1229 ERROR("BUG: unexpected error");
1234 /* terminate with zero */
1235 packet
[packet_size
] = 0;
1237 DEBUG("recevied packet: '%s'", packet
);
1239 if (packet_size
> 0)
1245 /* Hct... -- set thread
1246 * we don't have threads, send empty reply */
1247 gdb_put_packet(connection
, NULL
, 0);
1250 gdb_query_packet(connection
, packet
, packet_size
);
1253 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
1256 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
1259 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
1262 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
1265 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
1268 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
1272 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
1275 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
1279 gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
1282 target
->type
->resume(target
, 1, 0, 1, 0);
1283 gdb_put_packet(connection
, "OK", 2);
1286 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
1290 gdb_put_packet(connection
, "OK", 2);
1291 return ERROR_SERVER_REMOTE_CLOSED
;
1293 /* ignore unkown packets */
1294 DEBUG("ignoring 0x%2.2x packet", packet
[0]);
1295 gdb_put_packet(connection
, NULL
, 0);
1299 /* if a packet handler returned an error, exit input loop */
1300 if (retval
!= ERROR_OK
)
1304 if (gdb_con
->ctrl_c
)
1306 if (target
->state
== TARGET_RUNNING
)
1308 target
->type
->halt(target
);
1309 gdb_con
->ctrl_c
= 0;
1313 } while (gdb_con
->buf_cnt
> 0);
1320 gdb_service_t
*gdb_service
;
1321 target_t
*target
= targets
;
1326 WARNING("no gdb ports allocated as no target has been specified");
1332 WARNING("no gdb port specified, using default port 3333");
1338 char service_name
[8];
1340 snprintf(service_name
, 8, "gdb-%2.2i", i
);
1342 gdb_service
= malloc(sizeof(gdb_service_t
));
1343 gdb_service
->target
= target
;
1345 add_service("gdb", CONNECTION_GDB
, gdb_port
+ i
, 1, gdb_new_connection
, gdb_input
, gdb_connection_closed
, gdb_service
);
1347 DEBUG("gdb service for target %s at port %i", target
->type
->name
, gdb_port
+ i
);
1350 target
= target
->next
;
1356 /* daemon configuration command gdb_port */
1357 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1362 /* only if the port wasn't overwritten by cmdline */
1364 gdb_port
= strtoul(args
[0], NULL
, 0);
1369 int gdb_register_commands(command_context_t
*command_context
)
1371 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
1372 COMMAND_CONFIG
, "");