1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
30 #include "gdb_server.h"
31 #include "target_request.h"
40 #define _DEBUG_GDB_IO_
43 static int gdb_breakpoint_override
;
44 static enum breakpoint_type gdb_breakpoint_override_type
;
46 extern int gdb_error(connection_t
*connection
, int retval
);
47 static unsigned short gdb_port
= 3333;
48 static const char *DIGITS
= "0123456789abcdef";
50 static void gdb_log_callback(void *priv
, const char *file
, int line
,
51 const char *function
, const char *string
);
61 /* target behaviour on gdb detach */
62 enum gdb_detach_mode detach_mode
= GDB_DETACH_RESUME
;
64 /* number of gdb connections, mainly to supress gdb related debugging spam
65 * in helper/log.c when no gdb connections are actually active */
66 int gdb_actual_connections
;
68 /* set if we are sending a memory map to gdb
69 * via qXfer:memory-map:read packet */
70 /* enabled by default*/
71 int gdb_use_memory_map
= 1;
72 /* enabled by default*/
73 int gdb_flash_program
= 1;
75 /* if set, data aborts cause an error to be reported in memory read packets
76 * see the code in gdb_read_memory_packet() for further explanations */
77 int gdb_report_data_abort
= 0;
79 int gdb_last_signal(target_t
*target
)
81 switch (target
->debug_reason
)
83 case DBG_REASON_DBGRQ
:
84 return 0x2; /* SIGINT */
85 case DBG_REASON_BREAKPOINT
:
86 case DBG_REASON_WATCHPOINT
:
87 case DBG_REASON_WPTANDBKPT
:
88 return 0x05; /* SIGTRAP */
89 case DBG_REASON_SINGLESTEP
:
90 return 0x05; /* SIGTRAP */
91 case DBG_REASON_NOTHALTED
:
92 return 0x0; /* no signal... shouldn't happen */
94 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
99 int check_pending(connection_t
*connection
, int timeout_s
, int *got_data
)
101 /* a non-blocking socket will block if there is 0 bytes available on the socket,
102 * but return with as many bytes as are available immediately
106 gdb_connection_t
*gdb_con
= connection
->priv
;
108 if (got_data
== NULL
)
112 if (gdb_con
->buf_cnt
> 0)
119 FD_SET(connection
->fd
, &read_fds
);
121 tv
.tv_sec
= timeout_s
;
123 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
125 /* This can typically be because a "monitor" command took too long
126 * before printing any progress messages
130 return ERROR_GDB_TIMEOUT
;
136 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
140 int gdb_get_char(connection_t
*connection
, int* next_char
)
142 gdb_connection_t
*gdb_con
= connection
->priv
;
143 int retval
= ERROR_OK
;
145 #ifdef _DEBUG_GDB_IO_
149 if (gdb_con
->buf_cnt
-- > 0)
151 *next_char
= *(gdb_con
->buf_p
++);
152 if (gdb_con
->buf_cnt
> 0)
153 connection
->input_pending
= 1;
155 connection
->input_pending
= 0;
157 #ifdef _DEBUG_GDB_IO_
158 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
166 if (connection
->service
->type
== CONNECTION_PIPE
)
168 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
172 retval
= check_pending(connection
, 1, NULL
);
173 if (retval
!= ERROR_OK
)
175 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
178 if (gdb_con
->buf_cnt
> 0)
182 if (gdb_con
->buf_cnt
== 0)
185 return ERROR_SERVER_REMOTE_CLOSED
;
189 errno
= WSAGetLastError();
196 case WSAECONNABORTED
:
198 return ERROR_SERVER_REMOTE_CLOSED
;
201 return ERROR_SERVER_REMOTE_CLOSED
;
203 LOG_ERROR("read: %d", errno
);
214 return ERROR_SERVER_REMOTE_CLOSED
;
217 return ERROR_SERVER_REMOTE_CLOSED
;
219 LOG_ERROR("read: %s", strerror(errno
));
221 return ERROR_SERVER_REMOTE_CLOSED
;
226 #ifdef _DEBUG_GDB_IO_
227 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
228 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
229 debug_buffer
[gdb_con
->buf_cnt
] = 0;
230 LOG_DEBUG("received '%s'", debug_buffer
);
234 gdb_con
->buf_p
= gdb_con
->buffer
;
236 *next_char
= *(gdb_con
->buf_p
++);
237 if (gdb_con
->buf_cnt
> 0)
238 connection
->input_pending
= 1;
240 connection
->input_pending
= 0;
241 #ifdef _DEBUG_GDB_IO_
242 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
248 int gdb_putback_char(connection_t
*connection
, int last_char
)
250 gdb_connection_t
*gdb_con
= connection
->priv
;
252 if (gdb_con
->buf_p
> gdb_con
->buffer
)
254 *(--gdb_con
->buf_p
) = last_char
;
259 LOG_ERROR("BUG: couldn't put character back");
265 /* The only way we can detect that the socket is closed is the first time
266 * we write to it, we will fail. Subsequent write operations will
267 * succeed. Shudder! */
268 int gdb_write(connection_t
*connection
, void *data
, int len
)
270 gdb_connection_t
*gdb_con
= connection
->priv
;
272 return ERROR_SERVER_REMOTE_CLOSED
;
274 if (connection
->service
->type
== CONNECTION_PIPE
)
276 /* write to stdout */
277 if (write(STDOUT_FILENO
, data
, len
) == len
)
284 if (write_socket(connection
->fd
, data
, len
) == len
)
290 return ERROR_SERVER_REMOTE_CLOSED
;
293 int gdb_put_packet_inner(connection_t
*connection
, char *buffer
, int len
)
296 unsigned char my_checksum
= 0;
297 #ifdef _DEBUG_GDB_IO_
302 gdb_connection_t
*gdb_con
= connection
->priv
;
304 for (i
= 0; i
< len
; i
++)
305 my_checksum
+= buffer
[i
];
307 #ifdef _DEBUG_GDB_IO_
309 * At this point we should have nothing in the input queue from GDB,
310 * however sometimes '-' is sent even though we've already received
311 * an ACK (+) for everything we've sent off.
316 if ((retval
= check_pending(connection
, 0, &gotdata
)) != ERROR_OK
)
320 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
323 /* fix a problem with some IAR tools */
324 gdb_putback_char(connection
, reply
);
325 LOG_DEBUG("Unexpected start of new packet");
329 LOG_WARNING("Discard unexpected char %c", reply
);
335 #ifdef _DEBUG_GDB_IO_
336 debug_buffer
= malloc(len
+ 1);
337 memcpy(debug_buffer
, buffer
, len
);
338 debug_buffer
[len
] = 0;
339 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
343 char local_buffer
[1024];
344 local_buffer
[0] = '$';
345 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
347 /* performance gain on smaller packets by only a single call to gdb_write() */
348 memcpy(local_buffer
+ 1, buffer
, len
++);
349 local_buffer
[len
++] = '#';
350 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
351 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
352 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
359 /* larger packets are transmitted directly from caller supplied buffer
360 by several calls to gdb_write() to avoid dynamic allocation */
361 local_buffer
[1] = '#';
362 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
363 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
364 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
368 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
372 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
378 if (gdb_con
->noack_mode
)
381 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
386 else if (reply
== '-')
388 /* Stop sending output packets for now */
389 log_remove_callback(gdb_log_callback
, connection
);
390 LOG_WARNING("negative reply, retrying");
392 else if (reply
== 0x3)
395 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
399 else if (reply
== '-')
401 /* Stop sending output packets for now */
402 log_remove_callback(gdb_log_callback
, connection
);
403 LOG_WARNING("negative reply, retrying");
405 else if (reply
== '$') {
406 LOG_ERROR("GDB missing ack(1) - assumed good");
407 gdb_putback_char(connection
, reply
);
411 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
413 return ERROR_SERVER_REMOTE_CLOSED
;
416 else if (reply
== '$') {
417 LOG_ERROR("GDB missing ack(2) - assumed good");
418 gdb_putback_char(connection
, reply
);
423 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
425 return ERROR_SERVER_REMOTE_CLOSED
;
429 return ERROR_SERVER_REMOTE_CLOSED
;
434 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
436 gdb_connection_t
*gdb_con
= connection
->priv
;
438 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
441 /* we sent some data, reset timer for keep alive messages */
447 static __inline__
int fetch_packet(connection_t
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
449 unsigned char my_checksum
= 0;
454 gdb_connection_t
*gdb_con
= connection
->priv
;
460 /* The common case is that we have an entire packet with no escape chars.
461 * We need to leave at least 2 bytes in the buffer to have
462 * gdb_get_char() update various bits and bobs correctly.
464 if ((gdb_con
->buf_cnt
> 2) && ((gdb_con
->buf_cnt
+ count
) < *len
))
466 /* The compiler will struggle a bit with constant propagation and
467 * aliasing, so we help it by showing that these values do not
468 * change inside the loop
471 char *buf
= gdb_con
->buf_p
;
472 int run
= gdb_con
->buf_cnt
- 2;
479 if (character
== '#')
481 /* Danger! character can be '#' when esc is
482 * used so we need an explicit boolean for done here.
488 if (character
== '}')
490 /* data transmitted in binary mode (X packet)
491 * uses 0x7d as escape character */
492 my_checksum
+= character
& 0xff;
495 my_checksum
+= character
& 0xff;
496 buffer
[count
++] = (character
^ 0x20) & 0xff;
500 my_checksum
+= character
& 0xff;
501 buffer
[count
++] = character
& 0xff;
505 gdb_con
->buf_cnt
-= i
;
511 LOG_ERROR("packet buffer too small");
512 return ERROR_GDB_BUFFER_TOO_SMALL
;
515 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
518 if (character
== '#')
521 if (character
== '}')
523 /* data transmitted in binary mode (X packet)
524 * uses 0x7d as escape character */
525 my_checksum
+= character
& 0xff;
526 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
528 my_checksum
+= character
& 0xff;
529 buffer
[count
++] = (character
^ 0x20) & 0xff;
533 my_checksum
+= character
& 0xff;
534 buffer
[count
++] = character
& 0xff;
540 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
542 checksum
[0] = character
;
543 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
545 checksum
[1] = character
;
550 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
556 int gdb_get_packet_inner(connection_t
*connection
, char *buffer
, int *len
)
560 gdb_connection_t
*gdb_con
= connection
->priv
;
566 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
569 #ifdef _DEBUG_GDB_IO_
570 LOG_DEBUG("character: '%c'", character
);
578 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
579 * incase anyone tries to debug why they receive this warning every time */
580 LOG_WARNING("acknowledgment received, but no packet pending");
583 LOG_WARNING("negative acknowledgment, but no packet pending");
590 LOG_WARNING("ignoring character 0x%x", character
);
593 } while (character
!= '$');
598 /* explicit code expansion here to get faster inlined code in -O3 by not
599 * calculating checksum
601 if (gdb_con
->noack_mode
)
603 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
607 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
611 if (gdb_con
->noack_mode
)
613 /* checksum is not checked in noack mode */
618 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
626 return ERROR_SERVER_REMOTE_CLOSED
;
631 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
633 gdb_connection_t
*gdb_con
= connection
->priv
;
635 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
640 int gdb_output_con(connection_t
*connection
, const char* line
)
645 bin_size
= strlen(line
);
647 hex_buffer
= malloc(bin_size
*2 + 2);
648 if (hex_buffer
== NULL
)
649 return ERROR_GDB_BUFFER_TOO_SMALL
;
652 for (i
= 0; i
< bin_size
; i
++)
653 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
654 hex_buffer
[bin_size
*2 + 1] = 0;
656 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
662 int gdb_output(struct command_context_s
*context
, const char* line
)
664 /* this will be dumped to the log and also sent as an O packet if possible */
665 LOG_USER_N("%s", line
);
670 static void gdb_frontend_halted(struct target_s
*target
, connection_t
*connection
)
672 gdb_connection_t
*gdb_connection
= connection
->priv
;
674 /* In the GDB protocol when we are stepping or continuing execution,
675 * we have a lingering reply. Upon receiving a halted event
676 * when we have that lingering packet, we reply to the original
677 * step or continue packet.
679 * Executing monitor commands can bring the target in and
680 * out of the running state so we'll see lots of TARGET_EVENT_XXX
681 * that are to be ignored.
683 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
688 /* stop forwarding log packets! */
689 log_remove_callback(gdb_log_callback
, connection
);
691 if (gdb_connection
->ctrl_c
)
694 gdb_connection
->ctrl_c
= 0;
698 signal
= gdb_last_signal(target
);
701 snprintf(sig_reply
, 4, "T%2.2x", signal
);
702 gdb_put_packet(connection
, sig_reply
, 3);
703 gdb_connection
->frontend_state
= TARGET_HALTED
;
707 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
710 connection_t
*connection
= priv
;
712 target_handle_event(target
, event
);
715 case TARGET_EVENT_EARLY_HALTED
:
716 gdb_frontend_halted(target
, connection
);
718 case TARGET_EVENT_HALTED
:
719 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
721 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
722 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
723 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
735 int gdb_new_connection(connection_t
*connection
)
737 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
738 gdb_service_t
*gdb_service
= connection
->service
->priv
;
742 connection
->priv
= gdb_connection
;
744 /* initialize gdb connection information */
745 gdb_connection
->buf_p
= gdb_connection
->buffer
;
746 gdb_connection
->buf_cnt
= 0;
747 gdb_connection
->ctrl_c
= 0;
748 gdb_connection
->frontend_state
= TARGET_HALTED
;
749 gdb_connection
->vflash_image
= NULL
;
750 gdb_connection
->closed
= 0;
751 gdb_connection
->busy
= 0;
752 gdb_connection
->noack_mode
= 0;
754 /* send ACK to GDB for debug request */
755 gdb_write(connection
, "+", 1);
757 /* output goes through gdb connection */
758 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
760 /* we must remove all breakpoints registered to the target as a previous
761 * GDB session could leave dangling breakpoints if e.g. communication
764 breakpoint_clear_target(gdb_service
->target
);
765 watchpoint_clear_target(gdb_service
->target
);
767 /* register callback to be informed about target events */
768 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
770 /* a gdb session just attached, try to put the target in halt mode.
774 * If the halt fails(e.g. target needs a reset, JTAG communication not
775 * working, etc.), then the GDB connect will succeed as
776 * the get_gdb_reg_list() will lie and return a register list with
779 * This allows GDB monitor commands to be run from a GDB init script to
780 * initialize the target
782 * Also, since the halt() is asynchronous target connect will be
783 * instantaneous and thus avoiding annoying timeout problems during
786 target_halt(gdb_service
->target
);
787 /* FIX!!!! could extended-remote work better here?
789 * wait a tiny bit for halted state or we just continue. The
790 * GDB register packet will then contain garbage
792 target_wait_state(gdb_service
->target
, TARGET_HALTED
, 500);
794 /* remove the initial ACK from the incoming buffer */
795 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
798 /* FIX!!!??? would we actually ever receive a + here???
801 if (initial_ack
!= '+')
802 gdb_putback_char(connection
, initial_ack
);
803 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
805 gdb_actual_connections
++;
806 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
807 gdb_actual_connections
,
808 gdb_service
->target
->cmd_name
,
809 target_state_name(gdb_service
->target
));
814 int gdb_connection_closed(connection_t
*connection
)
816 gdb_service_t
*gdb_service
= connection
->service
->priv
;
817 gdb_connection_t
*gdb_connection
= connection
->priv
;
819 gdb_actual_connections
--;
820 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
821 gdb_service
->target
->cmd_name
,
822 target_state_name(gdb_service
->target
),
823 gdb_actual_connections
);
825 /* see if an image built with vFlash commands is left */
826 if (gdb_connection
->vflash_image
)
828 image_close(gdb_connection
->vflash_image
);
829 free(gdb_connection
->vflash_image
);
830 gdb_connection
->vflash_image
= NULL
;
833 /* if this connection registered a debug-message receiver delete it */
834 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
836 if (connection
->priv
)
838 free(connection
->priv
);
839 connection
->priv
= NULL
;
843 LOG_ERROR("BUG: connection->priv == NULL");
846 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
847 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
848 log_remove_callback(gdb_log_callback
, connection
);
850 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
855 void gdb_send_error(connection_t
*connection
, uint8_t the_error
)
858 snprintf(err
, 4, "E%2.2X", the_error
);
859 gdb_put_packet(connection
, err
, 3);
862 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
867 signal
= gdb_last_signal(target
);
869 snprintf(sig_reply
, 4, "S%2.2x", signal
);
870 gdb_put_packet(connection
, sig_reply
, 3);
875 static int gdb_reg_pos(target_t
*target
, int pos
, int len
)
877 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
880 return len
- 1 - pos
;
883 /* Convert register to string of bytes. NB! The # of bits in the
884 * register might be non-divisible by 8(a byte), in which
885 * case an entire byte is shown.
887 * NB! the format on the wire is the target endianess
889 * The format of reg->value is little endian
892 void gdb_str_to_target(target_t
*target
, char *tstr
, reg_t
*reg
)
899 buf_len
= CEIL(reg
->size
, 8);
901 for (i
= 0; i
< buf_len
; i
++)
903 int j
= gdb_reg_pos(target
, i
, buf_len
);
904 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
905 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
909 static int hextoint(char c
)
920 LOG_ERROR("BUG: invalid register value %08x", c
);
924 /* copy over in register buffer */
925 void gdb_target_to_reg(target_t
*target
, char *tstr
, int str_len
, uint8_t *bin
)
929 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
934 for (i
= 0; i
< str_len
; i
+= 2)
936 uint8_t t
= hextoint(tstr
[i
]) << 4;
937 t
|= hextoint(tstr
[i
+ 1]);
939 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
944 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
949 int reg_packet_size
= 0;
954 #ifdef _DEBUG_GDB_IO_
958 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
960 return gdb_error(connection
, retval
);
963 for (i
= 0; i
< reg_list_size
; i
++)
965 reg_packet_size
+= reg_list
[i
]->size
;
968 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
969 reg_packet_p
= reg_packet
;
971 for (i
= 0; i
< reg_list_size
; i
++)
973 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
974 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
977 #ifdef _DEBUG_GDB_IO_
980 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
981 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
986 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
994 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1002 #ifdef _DEBUG_GDB_IO_
1006 /* skip command character */
1010 if (packet_size
% 2)
1012 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1013 return ERROR_SERVER_REMOTE_CLOSED
;
1016 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1018 return gdb_error(connection
, retval
);
1022 for (i
= 0; i
< reg_list_size
; i
++)
1025 int chars
= (CEIL(reg_list
[i
]->size
, 8) * 2);
1027 if (packet_p
+ chars
> packet
+ packet_size
)
1029 LOG_ERROR("BUG: register packet is too small for registers");
1032 reg_arch_type_t
*arch_type
;
1033 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
1034 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1036 /* get register arch_type, and call set method */
1037 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
1039 arch_type
->set(reg_list
[i
], bin_buf
);
1041 /* advance packet pointer */
1048 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
1051 gdb_put_packet(connection
, "OK", 2);
1056 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1059 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1064 #ifdef _DEBUG_GDB_IO_
1068 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1070 return gdb_error(connection
, retval
);
1073 if (reg_list_size
<= reg_num
)
1075 LOG_ERROR("gdb requested a non-existing register");
1079 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1081 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1083 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1091 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1095 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1099 reg_arch_type_t
*arch_type
;
1103 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1105 return gdb_error(connection
, retval
);
1108 if (reg_list_size
< reg_num
)
1110 LOG_ERROR("gdb requested a non-existing register");
1111 return ERROR_SERVER_REMOTE_CLOSED
;
1114 if (*separator
!= '=')
1116 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1117 return ERROR_SERVER_REMOTE_CLOSED
;
1120 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1121 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
1122 int chars
= (CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1124 /* fix!!! add some sanity checks on packet size here */
1126 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1128 /* get register arch_type, and call set method */
1129 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
1130 arch_type
->set(reg_list
[reg_num
], bin_buf
);
1132 gdb_put_packet(connection
, "OK", 2);
1140 int gdb_error(connection_t
*connection
, int retval
)
1144 case ERROR_TARGET_DATA_ABORT
:
1145 gdb_send_error(connection
, EIO
);
1147 case ERROR_TARGET_TRANSLATION_FAULT
:
1148 gdb_send_error(connection
, EFAULT
);
1150 case ERROR_TARGET_UNALIGNED_ACCESS
:
1151 gdb_send_error(connection
, EFAULT
);
1153 case ERROR_TARGET_NOT_HALTED
:
1154 gdb_send_error(connection
, EFAULT
);
1157 /* This could be that the target reset itself. */
1158 LOG_ERROR("unexpected error %i", retval
);
1159 gdb_send_error(connection
, EFAULT
);
1166 /* We don't have to worry about the default 2 second timeout for GDB packets,
1167 * because GDB breaks up large memory reads into smaller reads.
1169 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1171 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1180 int retval
= ERROR_OK
;
1182 /* skip command character */
1185 addr
= strtoul(packet
, &separator
, 16);
1187 if (*separator
!= ',')
1189 LOG_ERROR("incomplete read memory packet received, dropping connection");
1190 return ERROR_SERVER_REMOTE_CLOSED
;
1193 len
= strtoul(separator
+ 1, NULL
, 16);
1195 buffer
= malloc(len
);
1197 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1199 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1201 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1203 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1204 * At some point this might be fixed in GDB, in which case this code can be removed.
1206 * OpenOCD developers are acutely aware of this problem, but there is nothing
1207 * gained by involving the user in this problem that hopefully will get resolved
1210 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1212 * For now, the default is to fix up things to make current GDB versions work.
1213 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1215 memset(buffer
, 0, len
);
1219 if (retval
== ERROR_OK
)
1221 hex_buffer
= malloc(len
* 2 + 1);
1224 for (i
= 0; i
< len
; i
++)
1226 uint8_t t
= buffer
[i
];
1227 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1228 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1231 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1237 retval
= gdb_error(connection
, retval
);
1245 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1256 /* skip command character */
1259 addr
= strtoul(packet
, &separator
, 16);
1261 if (*separator
!= ',')
1263 LOG_ERROR("incomplete write memory packet received, dropping connection");
1264 return ERROR_SERVER_REMOTE_CLOSED
;
1267 len
= strtoul(separator
+ 1, &separator
, 16);
1269 if (*(separator
++) != ':')
1271 LOG_ERROR("incomplete write memory packet received, dropping connection");
1272 return ERROR_SERVER_REMOTE_CLOSED
;
1275 buffer
= malloc(len
);
1277 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1279 for (i
= 0; i
< len
; i
++)
1282 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1286 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1288 if (retval
== ERROR_OK
)
1290 gdb_put_packet(connection
, "OK", 2);
1294 retval
= gdb_error(connection
, retval
);
1302 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1310 /* skip command character */
1313 addr
= strtoul(packet
, &separator
, 16);
1315 if (*separator
!= ',')
1317 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1318 return ERROR_SERVER_REMOTE_CLOSED
;
1321 len
= strtoul(separator
+ 1, &separator
, 16);
1323 if (*(separator
++) != ':')
1325 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1326 return ERROR_SERVER_REMOTE_CLOSED
;
1332 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1334 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1337 if (retval
== ERROR_OK
)
1339 gdb_put_packet(connection
, "OK", 2);
1343 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1350 int gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1353 uint32_t address
= 0x0;
1354 int retval
= ERROR_OK
;
1358 if (packet_size
> 1)
1360 packet
[packet_size
] = 0;
1361 address
= strtoul(packet
+ 1, NULL
, 16);
1368 if (packet
[0] == 'c')
1370 LOG_DEBUG("continue");
1371 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1372 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1374 else if (packet
[0] == 's')
1377 /* step at current or address, don't handle breakpoints */
1378 retval
= target_step(target
, current
, address
, 0);
1383 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1386 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1387 enum watchpoint_rw wp_type
;
1395 type
= strtoul(packet
+ 1, &separator
, 16);
1397 if (type
== 0) /* memory breakpoint */
1398 bp_type
= BKPT_SOFT
;
1399 else if (type
== 1) /* hardware breakpoint */
1400 bp_type
= BKPT_HARD
;
1401 else if (type
== 2) /* write watchpoint */
1402 wp_type
= WPT_WRITE
;
1403 else if (type
== 3) /* read watchpoint */
1405 else if (type
== 4) /* access watchpoint */
1406 wp_type
= WPT_ACCESS
;
1408 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1410 bp_type
= gdb_breakpoint_override_type
;
1413 if (*separator
!= ',')
1415 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1416 return ERROR_SERVER_REMOTE_CLOSED
;
1419 address
= strtoul(separator
+ 1, &separator
, 16);
1421 if (*separator
!= ',')
1423 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1424 return ERROR_SERVER_REMOTE_CLOSED
;
1427 size
= strtoul(separator
+ 1, &separator
, 16);
1433 if (packet
[0] == 'Z')
1435 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1437 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1442 gdb_put_packet(connection
, "OK", 2);
1447 breakpoint_remove(target
, address
);
1448 gdb_put_packet(connection
, "OK", 2);
1455 if (packet
[0] == 'Z')
1457 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1459 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1464 gdb_put_packet(connection
, "OK", 2);
1469 watchpoint_remove(target
, address
);
1470 gdb_put_packet(connection
, "OK", 2);
1481 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1482 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1484 if (*retval
!= ERROR_OK
)
1492 if ((*xml
== NULL
) || (!first
))
1494 /* start by 0 to exercise all the code paths.
1495 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1497 *size
= *size
* 2 + 2;
1499 *xml
= realloc(*xml
, *size
);
1504 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1512 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1514 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1519 /* there was just enough or not enough space, allocate more. */
1524 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1528 /* Extract and NUL-terminate the annex. */
1530 while (*buf
&& *buf
!= ':')
1536 /* After the read marker and annex, qXfer looks like a
1537 * traditional 'm' packet. */
1539 *ofs
= strtoul(buf
, &separator
, 16);
1541 if (*separator
!= ',')
1544 *len
= strtoul(separator
+ 1, NULL
, 16);
1549 int gdb_calc_blocksize(flash_bank_t
*bank
)
1552 uint32_t block_size
= 0xffffffff;
1554 /* loop through all sectors and return smallest sector size */
1556 for (i
= 0; i
< (uint32_t)bank
->num_sectors
; i
++)
1558 if (bank
->sectors
[i
].size
< block_size
)
1559 block_size
= bank
->sectors
[i
].size
;
1565 static int compare_bank (const void * a
, const void * b
)
1567 flash_bank_t
*b1
, *b2
;
1568 b1
=*((flash_bank_t
**)a
);
1569 b2
=*((flash_bank_t
**)b
);
1571 if (b1
->base
== b2
->base
)
1574 } else if (b1
->base
> b2
->base
)
1583 int gdb_query_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1585 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1586 gdb_connection_t
*gdb_connection
= connection
->priv
;
1588 if (strstr(packet
, "qRcmd,"))
1590 if (packet_size
> 6)
1594 cmd
= malloc((packet_size
- 6)/2 + 1);
1595 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1598 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1601 cmd
[(packet_size
- 6)/2] = 0x0;
1603 /* We want to print all debug output to GDB connection */
1604 log_add_callback(gdb_log_callback
, connection
);
1605 target_call_timer_callbacks_now();
1606 command_run_line(cmd_ctx
, cmd
);
1607 target_call_timer_callbacks_now();
1608 log_remove_callback(gdb_log_callback
, connection
);
1611 gdb_put_packet(connection
, "OK", 2);
1614 else if (strstr(packet
, "qCRC:"))
1616 if (packet_size
> 5)
1625 /* skip command character */
1628 addr
= strtoul(packet
, &separator
, 16);
1630 if (*separator
!= ',')
1632 LOG_ERROR("incomplete read memory packet received, dropping connection");
1633 return ERROR_SERVER_REMOTE_CLOSED
;
1636 len
= strtoul(separator
+ 1, NULL
, 16);
1638 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1640 if (retval
== ERROR_OK
)
1642 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1643 gdb_put_packet(connection
, gdb_reply
, 9);
1647 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1654 else if (strstr(packet
, "qSupported"))
1656 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1657 * disable qXfer:features:read for the moment */
1658 int retval
= ERROR_OK
;
1659 char *buffer
= NULL
;
1663 xml_printf(&retval
, &buffer
, &pos
, &size
,
1664 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1665 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1667 if (retval
!= ERROR_OK
)
1669 gdb_send_error(connection
, 01);
1673 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1678 else if (strstr(packet
, "qXfer:memory-map:read::") && (flash_get_bank_count() > 0))
1680 /* We get away with only specifying flash here. Regions that are not
1681 * specified are treated as if we provided no memory map(if not we
1682 * could detect the holes and mark them as RAM).
1683 * Normally we only execute this code once, but no big deal if we
1684 * have to regenerate it a couple of times. */
1690 int retval
= ERROR_OK
;
1697 /* skip command character */
1700 offset
= strtoul(packet
, &separator
, 16);
1701 length
= strtoul(separator
+ 1, &separator
, 16);
1703 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1706 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1707 read/write) by default for GDB.
1708 GDB does not have a concept of non-cacheable read/write memory.
1710 flash_bank_t
**banks
= malloc(sizeof(flash_bank_t
*)*flash_get_bank_count());
1713 for (i
= 0; i
< flash_get_bank_count(); i
++)
1715 p
= get_flash_bank_by_num(i
);
1719 retval
= ERROR_FAIL
;
1720 gdb_send_error(connection
, retval
);
1726 qsort(banks
, flash_get_bank_count(), sizeof(flash_bank_t
*), compare_bank
);
1728 uint32_t ram_start
= 0;
1729 for (i
= 0; i
< flash_get_bank_count(); i
++)
1733 if (ram_start
< p
->base
)
1735 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1736 ram_start
, p
->base
-ram_start
);
1739 /* if device has uneven sector sizes, eg. str7, lpc
1740 * we pass the smallest sector size to gdb memory map */
1741 blocksize
= gdb_calc_blocksize(p
);
1743 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1744 "<property name=\"blocksize\">0x%x</property>\n" \
1746 p
->base
, p
->size
, blocksize
);
1747 ram_start
= p
->base
+ p
->size
;
1751 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1752 ram_start
, 0-ram_start
);
1755 /* a flash chip could be at the very end of the 32 bit address space, in which case
1756 ram_start will be precisely 0 */
1762 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1764 if (retval
!= ERROR_OK
)
1766 gdb_send_error(connection
, retval
);
1770 if (offset
+ length
> pos
)
1772 length
= pos
- offset
;
1775 char *t
= malloc(length
+ 1);
1777 memcpy(t
+ 1, xml
+ offset
, length
);
1778 gdb_put_packet(connection
, t
, length
+ 1);
1784 else if (strstr(packet
, "qXfer:features:read:"))
1789 int retval
= ERROR_OK
;
1792 unsigned int length
;
1795 /* skip command character */
1798 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1800 gdb_send_error(connection
, 01);
1804 if (strcmp(annex
, "target.xml") != 0)
1806 gdb_send_error(connection
, 01);
1810 xml_printf(&retval
, &xml
, &pos
, &size
, \
1811 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1813 if (retval
!= ERROR_OK
)
1815 gdb_send_error(connection
, retval
);
1819 gdb_put_packet(connection
, xml
, strlen(xml
));
1824 else if (strstr(packet
, "QStartNoAckMode"))
1826 gdb_connection
->noack_mode
= 1;
1827 gdb_put_packet(connection
, "OK", 2);
1831 gdb_put_packet(connection
, "", 0);
1835 int gdb_v_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1837 gdb_connection_t
*gdb_connection
= connection
->priv
;
1838 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1841 /* if flash programming disabled - send a empty reply */
1843 if (gdb_flash_program
== 0)
1845 gdb_put_packet(connection
, "", 0);
1849 if (strstr(packet
, "vFlashErase:"))
1852 unsigned long length
;
1854 char *parse
= packet
+ 12;
1857 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1858 return ERROR_SERVER_REMOTE_CLOSED
;
1861 addr
= strtoul(parse
, &parse
, 16);
1863 if (*(parse
++) != ',' || *parse
== '\0')
1865 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1866 return ERROR_SERVER_REMOTE_CLOSED
;
1869 length
= strtoul(parse
, &parse
, 16);
1873 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1874 return ERROR_SERVER_REMOTE_CLOSED
;
1877 /* assume all sectors need erasing - stops any problems
1878 * when flash_write is called multiple times */
1881 /* perform any target specific operations before the erase */
1882 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_START
);
1883 result
= flash_erase_address_range(gdb_service
->target
, addr
, length
);
1884 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_END
);
1887 if (result
!= ERROR_OK
)
1889 /* GDB doesn't evaluate the actual error number returned,
1890 * treat a failed erase as an I/O error
1892 gdb_send_error(connection
, EIO
);
1893 LOG_ERROR("flash_erase returned %i", result
);
1896 gdb_put_packet(connection
, "OK", 2);
1901 if (strstr(packet
, "vFlashWrite:"))
1905 unsigned long length
;
1906 char *parse
= packet
+ 12;
1910 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1911 return ERROR_SERVER_REMOTE_CLOSED
;
1913 addr
= strtoul(parse
, &parse
, 16);
1914 if (*(parse
++) != ':')
1916 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1917 return ERROR_SERVER_REMOTE_CLOSED
;
1919 length
= packet_size
- (parse
- packet
);
1921 /* create a new image if there isn't already one */
1922 if (gdb_connection
->vflash_image
== NULL
)
1924 gdb_connection
->vflash_image
= malloc(sizeof(image_t
));
1925 image_open(gdb_connection
->vflash_image
, "", "build");
1928 /* create new section with content from packet buffer */
1929 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
1934 gdb_put_packet(connection
, "OK", 2);
1939 if (!strcmp(packet
, "vFlashDone"))
1943 /* process the flashing buffer. No need to erase as GDB
1944 * always issues a vFlashErase first. */
1945 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
1946 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1947 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1948 if (result
!= ERROR_OK
)
1950 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1951 gdb_put_packet(connection
, "E.memtype", 9);
1953 gdb_send_error(connection
, EIO
);
1957 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
1958 gdb_put_packet(connection
, "OK", 2);
1961 image_close(gdb_connection
->vflash_image
);
1962 free(gdb_connection
->vflash_image
);
1963 gdb_connection
->vflash_image
= NULL
;
1968 gdb_put_packet(connection
, "", 0);
1972 int gdb_detach(connection_t
*connection
, target_t
*target
)
1975 switch (detach_mode
)
1977 case GDB_DETACH_RESUME
:
1978 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1979 target_resume(target
, 1, 0, 1, 0);
1982 case GDB_DETACH_RESET
:
1983 /* FIX?? make this configurable?? */
1984 target_process_reset(connection
->cmd_ctx
, RESET_HALT
);
1987 case GDB_DETACH_HALT
:
1988 target_halt(target
);
1991 case GDB_DETACH_NOTHING
:
1995 gdb_put_packet(connection
, "OK", 2);
1999 static void gdb_log_callback(void *priv
, const char *file
, int line
,
2000 const char *function
, const char *string
)
2002 connection_t
*connection
= priv
;
2003 gdb_connection_t
*gdb_con
= connection
->priv
;
2007 /* do not reply this using the O packet */
2011 gdb_output_con(connection
, string
);
2014 /* Do not allocate this on the stack */
2015 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2017 static void gdb_sig_halted(connection_t
*connection
)
2020 snprintf(sig_reply
, 4, "T%2.2x", 2);
2021 gdb_put_packet(connection
, sig_reply
, 3);
2025 int gdb_input_inner(connection_t
*connection
)
2027 gdb_service_t
*gdb_service
= connection
->service
->priv
;
2028 target_t
*target
= gdb_service
->target
;
2029 char *packet
= gdb_packet_buffer
;
2032 gdb_connection_t
*gdb_con
= connection
->priv
;
2033 static int extended_protocol
= 0;
2035 /* drain input buffer */
2038 packet_size
= GDB_BUFFER_SIZE
-1;
2039 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
2044 /* terminate with zero */
2045 packet
[packet_size
] = 0;
2047 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2048 if (packet
[0] == 'X') {
2049 // binary packets spew junk into the debug log stream
2052 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2056 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2058 LOG_DEBUG("received packet: '%s'", packet
);
2062 if (packet_size
> 0)
2068 /* Hct... -- set thread
2069 * we don't have threads, send empty reply */
2070 gdb_put_packet(connection
, NULL
, 0);
2074 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
2077 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
2080 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
2083 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
2086 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
2089 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
2092 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
2096 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2099 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
2104 if (target
->state
!= TARGET_HALTED
)
2106 /* If the target isn't in the halted state, then we can't
2107 * step/continue. This might be early setup, etc.
2109 gdb_sig_halted(connection
);
2112 /* We're running/stepping, in which case we can
2113 * forward log output until the target is halted
2115 gdb_connection_t
*gdb_con
= connection
->priv
;
2116 gdb_con
->frontend_state
= TARGET_RUNNING
;
2117 log_add_callback(gdb_log_callback
, connection
);
2118 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2119 int retval
= gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2120 if (retval
!= ERROR_OK
)
2122 /* we'll never receive a halted condition... issue a false one.. */
2123 gdb_frontend_halted(target
, connection
);
2129 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
2132 retval
= gdb_detach(connection
, target
);
2133 extended_protocol
= 0;
2136 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
2140 if (extended_protocol
!= 0)
2142 gdb_put_packet(connection
, "OK", 2);
2143 return ERROR_SERVER_REMOTE_CLOSED
;
2145 /* handle extended remote protocol */
2146 extended_protocol
= 1;
2147 gdb_put_packet(connection
, "OK", 2);
2150 /* handle extended restart packet */
2151 breakpoint_clear_target(gdb_service
->target
);
2152 watchpoint_clear_target(gdb_service
->target
);
2153 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %d", get_num_by_target(target
));
2156 /* ignore unkown packets */
2157 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2158 gdb_put_packet(connection
, NULL
, 0);
2162 /* if a packet handler returned an error, exit input loop */
2163 if (retval
!= ERROR_OK
)
2167 if (gdb_con
->ctrl_c
)
2169 if (target
->state
== TARGET_RUNNING
)
2171 target_halt(target
);
2172 gdb_con
->ctrl_c
= 0;
2176 } while (gdb_con
->buf_cnt
> 0);
2181 int gdb_input(connection_t
*connection
)
2183 int retval
= gdb_input_inner(connection
);
2184 gdb_connection_t
*gdb_con
= connection
->priv
;
2185 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2188 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2189 if (gdb_con
->closed
)
2190 return ERROR_SERVER_REMOTE_CLOSED
;
2192 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2198 gdb_service_t
*gdb_service
;
2199 target_t
*target
= all_targets
;
2203 LOG_WARNING("no gdb ports allocated as no target has been specified");
2207 if (gdb_port
== 0 && server_use_pipes
== 0)
2209 LOG_INFO("gdb port disabled");
2213 if (server_use_pipes
)
2215 /* only a single gdb connection when using a pipe */
2217 gdb_service
= malloc(sizeof(gdb_service_t
));
2218 gdb_service
->target
= target
;
2220 add_service("gdb", CONNECTION_PIPE
, 0, 1, gdb_new_connection
, gdb_input
, gdb_connection_closed
, gdb_service
);
2222 LOG_DEBUG("gdb service for target %s using pipes",
2223 target_get_name(target
));
2229 gdb_service
= malloc(sizeof(gdb_service_t
));
2230 gdb_service
->target
= target
;
2232 add_service("gdb", CONNECTION_TCP
, gdb_port
+ target
->target_number
, 1, gdb_new_connection
, gdb_input
, gdb_connection_closed
, gdb_service
);
2234 LOG_DEBUG("gdb service for target %s at port %i",
2235 target_get_name(target
),
2236 gdb_port
+ target
->target_number
);
2237 target
= target
->next
;
2244 /* daemon configuration command gdb_port */
2245 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2249 command_print(cmd_ctx
, "%d", gdb_port
);
2253 gdb_port
= strtoul(args
[0], NULL
, 0);
2258 int handle_gdb_detach_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2262 if (strcmp(args
[0], "resume") == 0)
2264 detach_mode
= GDB_DETACH_RESUME
;
2267 else if (strcmp(args
[0], "reset") == 0)
2269 detach_mode
= GDB_DETACH_RESET
;
2272 else if (strcmp(args
[0], "halt") == 0)
2274 detach_mode
= GDB_DETACH_HALT
;
2277 else if (strcmp(args
[0], "nothing") == 0)
2279 detach_mode
= GDB_DETACH_NOTHING
;
2283 LOG_WARNING("invalid gdb_detach configuration directive: %s", args
[0]);
2286 return ERROR_COMMAND_SYNTAX_ERROR
;
2289 int handle_gdb_memory_map_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2293 if (strcmp(args
[0], "enable") == 0)
2295 gdb_use_memory_map
= 1;
2298 else if (strcmp(args
[0], "disable") == 0)
2300 gdb_use_memory_map
= 0;
2304 LOG_WARNING("invalid gdb_memory_map configuration directive %s", args
[0]);
2307 return ERROR_COMMAND_SYNTAX_ERROR
;
2310 int handle_gdb_flash_program_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2314 if (strcmp(args
[0], "enable") == 0)
2316 gdb_flash_program
= 1;
2319 else if (strcmp(args
[0], "disable") == 0)
2321 gdb_flash_program
= 0;
2325 LOG_WARNING("invalid gdb_flash_program configuration directive: %s", args
[0]);
2328 return ERROR_COMMAND_SYNTAX_ERROR
;
2331 int handle_gdb_report_data_abort_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2335 if (strcmp(args
[0], "enable") == 0)
2337 gdb_report_data_abort
= 1;
2340 else if (strcmp(args
[0], "disable") == 0)
2342 gdb_report_data_abort
= 0;
2346 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args
[0]);
2349 return ERROR_COMMAND_SYNTAX_ERROR
;
2352 /* gdb_breakpoint_override */
2353 int handle_gdb_breakpoint_override_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2358 } else if (argc
== 1)
2360 gdb_breakpoint_override
= 1;
2361 if (strcmp(args
[0], "hard") == 0)
2363 gdb_breakpoint_override_type
= BKPT_HARD
;
2364 } else if (strcmp(args
[0], "soft") == 0)
2366 gdb_breakpoint_override_type
= BKPT_SOFT
;
2367 } else if (strcmp(args
[0], "disable") == 0)
2369 gdb_breakpoint_override
= 0;
2373 return ERROR_COMMAND_SYNTAX_ERROR
;
2375 if (gdb_breakpoint_override
)
2377 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2380 LOG_USER("breakpoint type is not overriden");
2386 int gdb_register_commands(command_context_t
*command_context
)
2388 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
2389 COMMAND_ANY
, "daemon configuration command gdb_port");
2390 register_command(command_context
, NULL
, "gdb_detach", handle_gdb_detach_command
,
2391 COMMAND_CONFIG
, "resume/reset/halt/nothing - "
2392 "specify behavior when GDB detaches from the target");
2393 register_command(command_context
, NULL
, "gdb_memory_map", handle_gdb_memory_map_command
,
2394 COMMAND_CONFIG
, "enable or disable memory map");
2395 register_command(command_context
, NULL
, "gdb_flash_program", handle_gdb_flash_program_command
,
2396 COMMAND_CONFIG
, "enable or disable flash program");
2397 register_command(command_context
, NULL
, "gdb_report_data_abort", handle_gdb_report_data_abort_command
,
2398 COMMAND_CONFIG
, "enable or disable reporting data aborts");
2399 register_command(command_context
, NULL
, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command
,
2400 COMMAND_EXEC
, "hard/soft/disable - force breakpoint type for gdb 'break' commands.");
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)