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"
33 #include "target_request.h"
42 #define _DEBUG_GDB_IO_
45 static unsigned short gdb_port
;
55 enum gdb_detach_mode detach_mode
= GDB_DETACH_RESUME
;
57 int gdb_last_signal(target_t
*target
)
59 switch (target
->debug_reason
)
61 case DBG_REASON_DBGRQ
:
62 return 0x2; /* SIGINT */
63 case DBG_REASON_BREAKPOINT
:
64 case DBG_REASON_WATCHPOINT
:
65 case DBG_REASON_WPTANDBKPT
:
66 return 0x05; /* SIGTRAP */
67 case DBG_REASON_SINGLESTEP
:
68 return 0x05; /* SIGTRAP */
69 case DBG_REASON_NOTHALTED
:
70 return 0x0; /* no signal... shouldn't happen */
72 ERROR("BUG: undefined debug reason");
77 int gdb_get_char(connection_t
*connection
, int* next_char
)
79 gdb_connection_t
*gdb_con
= connection
->priv
;
82 if (gdb_con
->buf_cnt
-- > 0)
84 *next_char
= *(gdb_con
->buf_p
++);
85 if (gdb_con
->buf_cnt
> 0)
86 connection
->input_pending
= 1;
88 connection
->input_pending
= 0;
91 DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
97 while ((gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
)) <= 0)
99 if (gdb_con
->buf_cnt
== 0)
100 return ERROR_SERVER_REMOTE_CLOSED
;
103 errno
= WSAGetLastError();
110 case WSAECONNABORTED
:
111 return ERROR_SERVER_REMOTE_CLOSED
;
113 ERROR("read: %d", errno
);
123 return ERROR_SERVER_REMOTE_CLOSED
;
125 return ERROR_SERVER_REMOTE_CLOSED
;
127 ERROR("read: %s", strerror(errno
));
133 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
134 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
135 debug_buffer
[gdb_con
->buf_cnt
] = 0;
136 DEBUG("received '%s'", debug_buffer
);
139 gdb_con
->buf_p
= gdb_con
->buffer
;
141 *next_char
= *(gdb_con
->buf_p
++);
142 if (gdb_con
->buf_cnt
> 0)
143 connection
->input_pending
= 1;
145 connection
->input_pending
= 0;
146 #ifdef _DEBUG_GDB_IO_
147 DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
153 int gdb_putback_char(connection_t
*connection
, int last_char
)
155 gdb_connection_t
*gdb_con
= connection
->priv
;
157 if (gdb_con
->buf_p
> gdb_con
->buffer
)
159 *(--gdb_con
->buf_p
) = last_char
;
164 ERROR("BUG: couldn't put character back");
170 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
173 unsigned char my_checksum
= 0;
178 gdb_connection_t
*gdb_con
= connection
->priv
;
180 for (i
= 0; i
< len
; i
++)
181 my_checksum
+= buffer
[i
];
185 debug_buffer
= malloc(len
+ 1);
186 memcpy(debug_buffer
, buffer
, len
);
187 debug_buffer
[len
] = 0;
188 DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
191 write_socket(connection
->fd
, "$", 1);
193 write_socket(connection
->fd
, buffer
, len
);
194 write_socket(connection
->fd
, "#", 1);
196 snprintf(checksum
, 3, "%2.2x", my_checksum
);
198 write_socket(connection
->fd
, checksum
, 2);
200 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
205 else if (reply
== '-')
206 WARNING("negative reply, retrying");
207 else if (reply
== 0x3)
210 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
214 else if (reply
== '-')
215 WARNING("negative reply, retrying");
218 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
219 return ERROR_SERVER_REMOTE_CLOSED
;
224 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
225 return ERROR_SERVER_REMOTE_CLOSED
;
232 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
238 unsigned char my_checksum
= 0;
239 gdb_connection_t
*gdb_con
= connection
->priv
;
245 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
248 DEBUG("character: '%c'", character
);
255 WARNING("acknowledgment received, but no packet pending");
258 WARNING("negative acknowledgment, but no packet pending");
265 WARNING("ignoring character 0x%x", character
);
268 } while (character
!= '$');
274 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
277 if (character
== '#') break;
279 if (character
== '}')
281 /* data transmitted in binary mode (X packet)
282 * uses 0x7d as escape character */
283 my_checksum
+= character
& 0xff;
284 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
286 my_checksum
+= character
& 0xff;
287 buffer
[count
++] = (character
^ 0x20) & 0xff;
291 my_checksum
+= character
& 0xff;
292 buffer
[count
++] = character
& 0xff;
297 ERROR("packet buffer too small");
298 return ERROR_GDB_BUFFER_TOO_SMALL
;
304 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
306 checksum
[0] = character
;
307 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
309 checksum
[1] = character
;
312 if (my_checksum
== strtoul(checksum
, NULL
, 16))
314 write_socket(connection
->fd
, "+", 1);
318 WARNING("checksum error, requesting retransmission");
319 write_socket(connection
->fd
, "-", 1);
325 int gdb_output(struct command_context_s
*context
, char* line
)
327 connection_t
*connection
= context
->output_handler_priv
;
331 bin_size
= strlen(line
);
333 hex_buffer
= malloc(bin_size
*2 + 4);
336 for (i
=0; i
<bin_size
; i
++)
337 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
338 hex_buffer
[bin_size
*2+1] = '0';
339 hex_buffer
[bin_size
*2+2] = 'a';
340 hex_buffer
[bin_size
*2+3] = 0x0;
342 gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 3);
348 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
350 connection_t
*connection
= priv
;
351 gdb_connection_t
*gdb_connection
= connection
->priv
;
357 case TARGET_EVENT_HALTED
:
358 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
360 if (gdb_connection
->ctrl_c
)
363 gdb_connection
->ctrl_c
= 0;
367 signal
= gdb_last_signal(target
);
370 snprintf(sig_reply
, 4, "T%2.2x", signal
);
371 gdb_put_packet(connection
, sig_reply
, 3);
372 gdb_connection
->frontend_state
= TARGET_HALTED
;
375 case TARGET_EVENT_RESUMED
:
376 if (gdb_connection
->frontend_state
== TARGET_HALTED
)
378 gdb_connection
->frontend_state
= TARGET_RUNNING
;
388 int gdb_new_connection(connection_t
*connection
)
390 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
391 gdb_service_t
*gdb_service
= connection
->service
->priv
;
395 connection
->priv
= gdb_connection
;
397 /* initialize gdb connection information */
398 gdb_connection
->buf_p
= gdb_connection
->buffer
;
399 gdb_connection
->buf_cnt
= 0;
400 gdb_connection
->ctrl_c
= 0;
401 gdb_connection
->frontend_state
= TARGET_HALTED
;
402 gdb_connection
->vflash_image
= NULL
;
404 /* output goes through gdb connection */
405 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
407 /* register callback to be informed about target events */
408 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
410 /* a gdb session just attached, put the target in halt mode */
411 if (((retval
= gdb_service
->target
->type
->halt(gdb_service
->target
)) != ERROR_OK
) &&
412 (retval
!= ERROR_TARGET_ALREADY_HALTED
))
414 ERROR("error when trying to halt target");
418 while (gdb_service
->target
->state
!= TARGET_HALTED
)
420 gdb_service
->target
->type
->poll(gdb_service
->target
);
423 /* remove the initial ACK from the incoming buffer */
424 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
427 if (initial_ack
!= '+')
428 gdb_putback_char(connection
, initial_ack
);
433 int gdb_connection_closed(connection_t
*connection
)
435 gdb_service_t
*gdb_service
= connection
->service
->priv
;
436 gdb_connection_t
*gdb_connection
= connection
->priv
;
438 /* see if an image built with vFlash commands is left */
439 if (gdb_connection
->vflash_image
)
441 image_close(gdb_connection
->vflash_image
);
442 free(gdb_connection
->vflash_image
);
443 gdb_connection
->vflash_image
= NULL
;
446 /* if this connection registered a debug-message receiver delete it */
447 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
449 if (connection
->priv
)
451 free(connection
->priv
);
452 connection
->priv
= NULL
;
456 ERROR("BUG: connection->priv == NULL");
459 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
464 void gdb_send_error(connection_t
*connection
, u8 the_error
)
467 snprintf(err
, 4, "E%2.2X", the_error
);
468 gdb_put_packet(connection
, err
, 3);
471 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
476 signal
= gdb_last_signal(target
);
478 snprintf(sig_reply
, 4, "S%2.2x", signal
);
479 gdb_put_packet(connection
, sig_reply
, 3);
484 void gdb_str_to_target(target_t
*target
, char *str
, char *tstr
)
486 int str_len
= strlen(str
);
491 ERROR("BUG: gdb value with uneven number of characters encountered: %s", str
);
495 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
497 for (i
= 0; i
< str_len
; i
+=2)
499 tstr
[str_len
- i
- 1] = str
[i
+ 1];
500 tstr
[str_len
- i
- 2] = str
[i
];
505 for (i
= 0; i
< str_len
; i
++)
512 void gdb_target_to_str(target_t
*target
, char *tstr
, char *str
)
514 int str_len
= strlen(tstr
);
519 ERROR("BUG: gdb value with uneven number of characters encountered");
523 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
525 for (i
= 0; i
< str_len
; i
+=2)
527 str
[str_len
- i
- 1] = tstr
[i
+ 1];
528 str
[str_len
- i
- 2] = tstr
[i
];
533 for (i
= 0; i
< str_len
; i
++)
540 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
545 int reg_packet_size
= 0;
552 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
556 case ERROR_TARGET_NOT_HALTED
:
557 ERROR("gdb requested registers but we're not halted, dropping connection");
558 return ERROR_SERVER_REMOTE_CLOSED
;
560 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
561 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
566 for (i
= 0; i
< reg_list_size
; i
++)
568 reg_packet_size
+= reg_list
[i
]->size
;
571 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
572 reg_packet_p
= reg_packet
;
574 for (i
= 0; i
< reg_list_size
; i
++)
576 char *hex_buf
= buf_to_str(reg_list
[i
]->value
, reg_list
[i
]->size
, 16);
577 DEBUG("hex_buf: %s", hex_buf
);
578 gdb_str_to_target(target
, hex_buf
, reg_packet_p
);
579 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
583 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
584 DEBUG("reg_packet: %s", reg_packet_p
);
587 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
595 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
605 /* skip command character */
611 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
612 return ERROR_SERVER_REMOTE_CLOSED
;
615 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
619 case ERROR_TARGET_NOT_HALTED
:
620 ERROR("gdb tried to registers but we're not halted, dropping connection");
621 return ERROR_SERVER_REMOTE_CLOSED
;
623 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
624 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
630 for (i
= 0; i
< reg_list_size
; i
++)
634 reg_arch_type_t
*arch_type
;
636 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
637 hex_buf
= malloc(CEIL(reg_list
[i
]->size
, 8) * 2);
638 gdb_target_to_str(target
, packet_p
, hex_buf
);
640 /* convert hex-string to binary buffer */
641 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
642 str_to_buf(hex_buf
, CEIL(reg_list
[i
]->size
, 8) * 2, bin_buf
, reg_list
[i
]->size
, 16);
644 /* get register arch_type, and call set method */
645 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
646 if (arch_type
== NULL
)
648 ERROR("BUG: encountered unregistered arch type");
651 arch_type
->set(reg_list
[i
], bin_buf
);
653 /* advance packet pointer */
654 packet_p
+= (CEIL(reg_list
[i
]->size
, 8) * 2);
660 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
663 gdb_put_packet(connection
, "OK", 2);
668 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
671 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
679 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
683 case ERROR_TARGET_NOT_HALTED
:
684 ERROR("gdb requested registers but we're not halted, dropping connection");
685 return ERROR_SERVER_REMOTE_CLOSED
;
687 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
688 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
693 if (reg_list_size
<= reg_num
)
695 ERROR("gdb requested a non-existing register");
699 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
701 hex_buf
= buf_to_str(reg_list
[reg_num
]->value
, reg_list
[reg_num
]->size
, 16);
703 gdb_str_to_target(target
, hex_buf
, reg_packet
);
705 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
714 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
719 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
723 reg_arch_type_t
*arch_type
;
727 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
731 case ERROR_TARGET_NOT_HALTED
:
732 ERROR("gdb tried to set a register but we're not halted, dropping connection");
733 return ERROR_SERVER_REMOTE_CLOSED
;
735 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
736 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
741 if (reg_list_size
< reg_num
)
743 ERROR("gdb requested a non-existing register");
744 return ERROR_SERVER_REMOTE_CLOSED
;
747 if (*separator
!= '=')
749 ERROR("GDB 'set register packet', but no '=' following the register number");
750 return ERROR_SERVER_REMOTE_CLOSED
;
753 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
754 hex_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
755 gdb_target_to_str(target
, separator
+ 1, hex_buf
);
757 /* convert hex-string to binary buffer */
758 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
759 str_to_buf(hex_buf
, CEIL(reg_list
[reg_num
]->size
, 8) * 2, bin_buf
, reg_list
[reg_num
]->size
, 16);
761 /* get register arch_type, and call set method */
762 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
763 if (arch_type
== NULL
)
765 ERROR("BUG: encountered unregistered arch type");
768 arch_type
->set(reg_list
[reg_num
], bin_buf
);
770 gdb_put_packet(connection
, "OK", 2);
779 int gdb_memory_packet_error(connection_t
*connection
, int retval
)
783 case ERROR_TARGET_NOT_HALTED
:
784 ERROR("gdb tried to read memory but we're not halted, dropping connection");
785 return ERROR_SERVER_REMOTE_CLOSED
;
787 case ERROR_TARGET_DATA_ABORT
:
788 gdb_send_error(connection
, EIO
);
790 case ERROR_TARGET_TRANSLATION_FAULT
:
791 gdb_send_error(connection
, EFAULT
);
793 case ERROR_TARGET_UNALIGNED_ACCESS
:
794 gdb_send_error(connection
, EFAULT
);
797 ERROR("BUG: unexpected error %i", retval
);
804 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
816 /* skip command character */
819 addr
= strtoul(packet
, &separator
, 16);
821 if (*separator
!= ',')
823 ERROR("incomplete read memory packet received, dropping connection");
824 return ERROR_SERVER_REMOTE_CLOSED
;
827 len
= strtoul(separator
+1, NULL
, 16);
829 buffer
= malloc(len
);
831 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
837 retval
= target
->type
->read_memory(target
, addr
, 4, 1, buffer
);
839 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
843 retval
= target
->type
->read_memory(target
, addr
, 2, 1, buffer
);
845 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
848 if (((addr
% 4) == 0) && ((len
% 4) == 0))
849 retval
= target
->type
->read_memory(target
, addr
, 4, len
/ 4, buffer
);
851 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
854 if (retval
== ERROR_OK
)
856 hex_buffer
= malloc(len
* 2 + 1);
858 for (i
=0; i
<len
; i
++)
859 snprintf(hex_buffer
+ 2*i
, 3, "%2.2x", buffer
[i
]);
861 gdb_put_packet(connection
, hex_buffer
, len
* 2);
867 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
876 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
887 /* skip command character */
890 addr
= strtoul(packet
, &separator
, 16);
892 if (*separator
!= ',')
894 ERROR("incomplete write memory packet received, dropping connection");
895 return ERROR_SERVER_REMOTE_CLOSED
;
898 len
= strtoul(separator
+1, &separator
, 16);
900 if (*(separator
++) != ':')
902 ERROR("incomplete write memory packet received, dropping connection");
903 return ERROR_SERVER_REMOTE_CLOSED
;
906 buffer
= malloc(len
);
908 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
910 for (i
=0; i
<len
; i
++)
913 sscanf(separator
+ 2*i
, "%2x", &tmp
);
920 /* handle sized writes */
923 retval
= target
->type
->write_memory(target
, addr
, 4, 1, buffer
);
925 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
929 retval
= target
->type
->write_memory(target
, addr
, 2, 1, buffer
);
931 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
935 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
937 /* handle bulk writes */
939 retval
= target_write_buffer(target
, addr
, len
, buffer
);
943 if (retval
== ERROR_OK
)
945 gdb_put_packet(connection
, "OK", 2);
949 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
958 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
967 /* skip command character */
970 addr
= strtoul(packet
, &separator
, 16);
972 if (*separator
!= ',')
974 ERROR("incomplete write memory binary packet received, dropping connection");
975 return ERROR_SERVER_REMOTE_CLOSED
;
978 len
= strtoul(separator
+1, &separator
, 16);
980 if (*(separator
++) != ':')
982 ERROR("incomplete write memory binary packet received, dropping connection");
983 return ERROR_SERVER_REMOTE_CLOSED
;
989 buffer
= malloc(len
);
991 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
993 memcpy( buffer
, separator
, len
);
999 retval
= target
->type
->write_memory(target
, addr
, 4, 1, buffer
);
1001 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1004 if ((addr
% 2) == 0)
1005 retval
= target
->type
->write_memory(target
, addr
, 2, 1, buffer
);
1007 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1011 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1014 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1021 if (retval
== ERROR_OK
)
1023 gdb_put_packet(connection
, "OK", 2);
1027 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
1034 void gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1041 if (packet_size
> 1)
1043 packet
[packet_size
] = 0;
1044 address
= strtoul(packet
+ 1, NULL
, 16);
1051 if (packet
[0] == 'c')
1054 target
->type
->resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1056 else if (packet
[0] == 's')
1059 target
->type
->step(target
, current
, address
, 0); /* step at current or address, don't handle breakpoints */
1063 int gdb_bp_wp_packet_error(connection_t
*connection
, int retval
)
1067 case ERROR_TARGET_NOT_HALTED
:
1068 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1069 return ERROR_SERVER_REMOTE_CLOSED
;
1071 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE
:
1072 gdb_send_error(connection
, EBUSY
);
1075 ERROR("BUG: unexpected error %i", retval
);
1082 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1085 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1086 enum watchpoint_rw wp_type
;
1094 type
= strtoul(packet
+ 1, &separator
, 16);
1096 if (type
== 0) /* memory breakpoint */
1097 bp_type
= BKPT_SOFT
;
1098 else if (type
== 1) /* hardware breakpoint */
1099 bp_type
= BKPT_HARD
;
1100 else if (type
== 2) /* write watchpoint */
1101 wp_type
= WPT_WRITE
;
1102 else if (type
== 3) /* read watchpoint */
1104 else if (type
== 4) /* access watchpoint */
1105 wp_type
= WPT_ACCESS
;
1107 if (*separator
!= ',')
1109 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1110 return ERROR_SERVER_REMOTE_CLOSED
;
1113 address
= strtoul(separator
+1, &separator
, 16);
1115 if (*separator
!= ',')
1117 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1118 return ERROR_SERVER_REMOTE_CLOSED
;
1121 size
= strtoul(separator
+1, &separator
, 16);
1127 if (packet
[0] == 'Z')
1129 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1131 if ((retval
= gdb_bp_wp_packet_error(connection
, retval
)) != ERROR_OK
)
1136 gdb_put_packet(connection
, "OK", 2);
1141 breakpoint_remove(target
, address
);
1142 gdb_put_packet(connection
, "OK", 2);
1149 if (packet
[0] == 'Z')
1151 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1153 if ((retval
= gdb_bp_wp_packet_error(connection
, retval
)) != ERROR_OK
)
1158 gdb_put_packet(connection
, "OK", 2);
1163 watchpoint_remove(target
, address
);
1164 gdb_put_packet(connection
, "OK", 2);
1175 int gdb_query_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1177 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1179 if (strstr(packet
, "qRcmd,"))
1181 if (packet_size
> 6)
1185 cmd
= malloc((packet_size
- 6)/2 + 1);
1186 for (i
=0; i
< (packet_size
- 6)/2; i
++)
1189 sscanf(packet
+ 6 + 2*i
, "%2x", &tmp
);
1192 cmd
[(packet_size
- 6)/2] = 0x0;
1193 command_run_line(cmd_ctx
, cmd
);
1196 gdb_put_packet(connection
, "OK", 2);
1200 if (strstr(packet
, "qCRC:"))
1202 if (packet_size
> 5)
1211 /* skip command character */
1214 addr
= strtoul(packet
, &separator
, 16);
1216 if (*separator
!= ',')
1218 ERROR("incomplete read memory packet received, dropping connection");
1219 return ERROR_SERVER_REMOTE_CLOSED
;
1222 len
= strtoul(separator
+1, NULL
, 16);
1224 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1226 if (retval
== ERROR_OK
)
1228 snprintf(gdb_reply
, 9, "C%2.2x", checksum
);
1229 gdb_put_packet(connection
, gdb_reply
, 9);
1233 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
1241 gdb_put_packet(connection
, "", 0);
1245 int gdb_v_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1247 gdb_connection_t
*gdb_connection
= connection
->priv
;
1248 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1251 if (strstr(packet
, "vFlashErase:"))
1254 unsigned long length
;
1255 char *parse
= packet
+ 12;
1258 ERROR("incomplete vFlashErase packet received, dropping connection");
1259 return ERROR_SERVER_REMOTE_CLOSED
;
1262 addr
= strtoul(parse
, &parse
, 16);
1264 if (*(parse
++) != ',' || *parse
== '\0')
1266 ERROR("incomplete vFlashErase packet received, dropping connection");
1267 return ERROR_SERVER_REMOTE_CLOSED
;
1270 length
= strtoul(parse
, &parse
, 16);
1274 ERROR("incomplete vFlashErase packet received, dropping connection");
1275 return ERROR_SERVER_REMOTE_CLOSED
;
1279 if ((result
= flash_erase(gdb_service
->target
, addr
, length
)) != ERROR_OK
)
1281 /* GDB doesn't evaluate the actual error number returned,
1282 * treat a failed erase as an I/O error
1284 gdb_send_error(connection
, EIO
);
1285 ERROR("flash_erase returned %i", result
);
1288 gdb_put_packet(connection
, "OK", 2);
1293 if (strstr(packet
, "vFlashWrite:"))
1296 unsigned long length
;
1297 char *parse
= packet
+ 12;
1301 ERROR("incomplete vFlashErase packet received, dropping connection");
1302 return ERROR_SERVER_REMOTE_CLOSED
;
1304 addr
= strtoul(parse
, &parse
, 16);
1305 if (*(parse
++) != ':')
1307 ERROR("incomplete vFlashErase packet received, dropping connection");
1308 return ERROR_SERVER_REMOTE_CLOSED
;
1310 length
= packet_size
- (parse
- packet
);
1312 /* create a new image if there isn't already one */
1313 if (gdb_connection
->vflash_image
== NULL
)
1315 gdb_connection
->vflash_image
= malloc(sizeof(image_t
));
1316 image_open(gdb_connection
->vflash_image
, "", "build");
1319 /* create new section with content from packet buffer */
1320 image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (u8
*)parse
);
1322 gdb_put_packet(connection
, "OK", 2);
1327 if (!strcmp(packet
, "vFlashDone"))
1332 /* process the flashing buffer */
1333 if ((result
= flash_image_operation(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, &error_str
, NULL
, flash_image_op_write
)) != ERROR_OK
)
1335 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1336 gdb_put_packet(connection
, "E.memtype", 9);
1338 gdb_send_error(connection
, EIO
);
1342 ERROR("flash writing failed: %s", error_str
);
1348 DEBUG("wrote %u bytes from vFlash image to flash", written
);
1349 gdb_put_packet(connection
, "OK", 2);
1352 image_close(gdb_connection
->vflash_image
);
1353 free(gdb_connection
->vflash_image
);
1354 gdb_connection
->vflash_image
= NULL
;
1359 gdb_put_packet(connection
, "", 0);
1363 int gdb_detach(connection_t
*connection
, target_t
*target
)
1365 switch( detach_mode
)
1367 case GDB_DETACH_RESUME
:
1368 target
->type
->resume(target
, 1, 0, 1, 0);
1371 case GDB_DETACH_RESET
:
1372 target_process_reset(connection
->cmd_ctx
);
1375 case GDB_DETACH_HALT
:
1376 target
->type
->halt(target
);
1379 case GDB_DETACH_NOTHING
:
1383 gdb_put_packet(connection
, "OK", 2);
1388 int gdb_input(connection_t
*connection
)
1390 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1391 target_t
*target
= gdb_service
->target
;
1392 char packet
[GDB_BUFFER_SIZE
];
1395 gdb_connection_t
*gdb_con
= connection
->priv
;
1397 /* drain input buffer */
1400 packet_size
= GDB_BUFFER_SIZE
-1;
1401 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
1405 case ERROR_GDB_BUFFER_TOO_SMALL
:
1406 ERROR("BUG: buffer supplied for gdb packet was too small");
1408 case ERROR_SERVER_REMOTE_CLOSED
:
1409 return ERROR_SERVER_REMOTE_CLOSED
;
1411 ERROR("BUG: unexpected error");
1416 /* terminate with zero */
1417 packet
[packet_size
] = 0;
1419 DEBUG("received packet: '%s'", packet
);
1421 if (packet_size
> 0)
1427 /* Hct... -- set thread
1428 * we don't have threads, send empty reply */
1429 gdb_put_packet(connection
, NULL
, 0);
1432 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
1435 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
1438 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
1441 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
1444 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
1447 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
1450 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
1454 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
1457 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
1461 gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
1464 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
1467 retval
= gdb_detach(connection
, target
);
1470 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
1474 gdb_put_packet(connection
, "OK", 2);
1475 return ERROR_SERVER_REMOTE_CLOSED
;
1477 /* ignore unkown packets */
1478 DEBUG("ignoring 0x%2.2x packet", packet
[0]);
1479 gdb_put_packet(connection
, NULL
, 0);
1483 /* if a packet handler returned an error, exit input loop */
1484 if (retval
!= ERROR_OK
)
1488 if (gdb_con
->ctrl_c
)
1490 if (target
->state
== TARGET_RUNNING
)
1492 target
->type
->halt(target
);
1493 gdb_con
->ctrl_c
= 0;
1497 } while (gdb_con
->buf_cnt
> 0);
1504 gdb_service_t
*gdb_service
;
1505 target_t
*target
= targets
;
1510 WARNING("no gdb ports allocated as no target has been specified");
1516 WARNING("no gdb port specified, using default port 3333");
1522 char service_name
[8];
1524 snprintf(service_name
, 8, "gdb-%2.2i", i
);
1526 gdb_service
= malloc(sizeof(gdb_service_t
));
1527 gdb_service
->target
= target
;
1529 add_service("gdb", CONNECTION_GDB
, gdb_port
+ i
, 1, gdb_new_connection
, gdb_input
, gdb_connection_closed
, gdb_service
);
1531 DEBUG("gdb service for target %s at port %i", target
->type
->name
, gdb_port
+ i
);
1534 target
= target
->next
;
1540 /* daemon configuration command gdb_port */
1541 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1546 /* only if the port wasn't overwritten by cmdline */
1548 gdb_port
= strtoul(args
[0], NULL
, 0);
1553 int handle_gdb_detach_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1557 if (strcmp(args
[0], "resume") == 0)
1559 detach_mode
= GDB_DETACH_RESUME
;
1562 else if (strcmp(args
[0], "reset") == 0)
1564 detach_mode
= GDB_DETACH_RESET
;
1567 else if (strcmp(args
[0], "halt") == 0)
1569 detach_mode
= GDB_DETACH_HALT
;
1572 else if (strcmp(args
[0], "nothing") == 0)
1574 detach_mode
= GDB_DETACH_NOTHING
;
1579 WARNING("invalid gdb_detach configuration directive: %s", args
[0]);
1583 int gdb_register_commands(command_context_t
*command_context
)
1585 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
1586 COMMAND_CONFIG
, "");
1587 register_command(command_context
, NULL
, "gdb_detach", handle_gdb_detach_command
,
1588 COMMAND_CONFIG
, "");
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)