1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2009 Ø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 <target/breakpoints.h>
31 #include <target/target_request.h>
32 #include <target/register.h>
34 #include <flash/nor/core.h>
35 #include "gdb_server.h"
36 #include <target/image.h>
37 #include <jtag/jtag.h>
40 /* private connection data for GDB */
43 char buffer
[GDB_BUFFER_SIZE
];
47 enum target_state frontend_state
;
48 struct image
*vflash_image
;
52 bool sync
; /* set flag to true if you want the next stepi to return immediately.
53 allowing GDB to pick up a fresh set of register values from the target
54 without modifying the target state. */
60 #define _DEBUG_GDB_IO_
63 static struct gdb_connection
*current_gdb_connection
;
65 static int gdb_breakpoint_override
;
66 static enum breakpoint_type gdb_breakpoint_override_type
;
68 extern int gdb_error(struct connection
*connection
, int retval
);
69 static unsigned short gdb_port
= 3333;
70 static unsigned short gdb_port_next
= 0;
71 static const char *DIGITS
= "0123456789abcdef";
73 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
74 const char *function
, const char *string
);
76 /* number of gdb connections, mainly to suppress gdb related debugging spam
77 * in helper/log.c when no gdb connections are actually active */
78 int gdb_actual_connections
;
80 /* set if we are sending a memory map to gdb
81 * via qXfer:memory-map:read packet */
82 /* enabled by default*/
83 int gdb_use_memory_map
= 1;
84 /* enabled by default*/
85 int gdb_flash_program
= 1;
87 /* if set, data aborts cause an error to be reported in memory read packets
88 * see the code in gdb_read_memory_packet() for further explanations */
89 int gdb_report_data_abort
= 0;
91 int gdb_last_signal(struct target
*target
)
93 switch (target
->debug_reason
)
95 case DBG_REASON_DBGRQ
:
96 return 0x2; /* SIGINT */
97 case DBG_REASON_BREAKPOINT
:
98 case DBG_REASON_WATCHPOINT
:
99 case DBG_REASON_WPTANDBKPT
:
100 return 0x05; /* SIGTRAP */
101 case DBG_REASON_SINGLESTEP
:
102 return 0x05; /* SIGTRAP */
103 case DBG_REASON_NOTHALTED
:
104 return 0x0; /* no signal... shouldn't happen */
106 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
111 int check_pending(struct connection
*connection
, int timeout_s
, int *got_data
)
113 /* a non-blocking socket will block if there is 0 bytes available on the socket,
114 * but return with as many bytes as are available immediately
118 struct gdb_connection
*gdb_con
= connection
->priv
;
120 if (got_data
== NULL
)
124 if (gdb_con
->buf_cnt
> 0)
131 FD_SET(connection
->fd
, &read_fds
);
133 tv
.tv_sec
= timeout_s
;
135 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
137 /* This can typically be because a "monitor" command took too long
138 * before printing any progress messages
142 return ERROR_GDB_TIMEOUT
;
148 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
152 int gdb_get_char(struct connection
*connection
, int* next_char
)
154 struct gdb_connection
*gdb_con
= connection
->priv
;
155 int retval
= ERROR_OK
;
157 #ifdef _DEBUG_GDB_IO_
161 if (gdb_con
->buf_cnt
-- > 0)
163 *next_char
= *(gdb_con
->buf_p
++);
164 if (gdb_con
->buf_cnt
> 0)
165 connection
->input_pending
= 1;
167 connection
->input_pending
= 0;
169 #ifdef _DEBUG_GDB_IO_
170 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
178 if (connection
->service
->type
== CONNECTION_PIPE
)
180 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
184 retval
= check_pending(connection
, 1, NULL
);
185 if (retval
!= ERROR_OK
)
187 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
190 if (gdb_con
->buf_cnt
> 0)
194 if (gdb_con
->buf_cnt
== 0)
197 return ERROR_SERVER_REMOTE_CLOSED
;
201 errno
= WSAGetLastError();
208 case WSAECONNABORTED
:
210 return ERROR_SERVER_REMOTE_CLOSED
;
213 return ERROR_SERVER_REMOTE_CLOSED
;
215 LOG_ERROR("read: %d", errno
);
226 return ERROR_SERVER_REMOTE_CLOSED
;
229 return ERROR_SERVER_REMOTE_CLOSED
;
231 LOG_ERROR("read: %s", strerror(errno
));
233 return ERROR_SERVER_REMOTE_CLOSED
;
238 #ifdef _DEBUG_GDB_IO_
239 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
240 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
241 debug_buffer
[gdb_con
->buf_cnt
] = 0;
242 LOG_DEBUG("received '%s'", debug_buffer
);
246 gdb_con
->buf_p
= gdb_con
->buffer
;
248 *next_char
= *(gdb_con
->buf_p
++);
249 if (gdb_con
->buf_cnt
> 0)
250 connection
->input_pending
= 1;
252 connection
->input_pending
= 0;
253 #ifdef _DEBUG_GDB_IO_
254 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
260 int gdb_putback_char(struct connection
*connection
, int last_char
)
262 struct gdb_connection
*gdb_con
= connection
->priv
;
264 if (gdb_con
->buf_p
> gdb_con
->buffer
)
266 *(--gdb_con
->buf_p
) = last_char
;
271 LOG_ERROR("BUG: couldn't put character back");
277 /* The only way we can detect that the socket is closed is the first time
278 * we write to it, we will fail. Subsequent write operations will
279 * succeed. Shudder! */
280 int gdb_write(struct connection
*connection
, void *data
, int len
)
282 struct gdb_connection
*gdb_con
= connection
->priv
;
284 return ERROR_SERVER_REMOTE_CLOSED
;
286 if (connection
->service
->type
== CONNECTION_PIPE
)
288 /* write to stdout */
289 if (write(STDOUT_FILENO
, data
, len
) == len
)
296 if (write_socket(connection
->fd
, data
, len
) == len
)
302 return ERROR_SERVER_REMOTE_CLOSED
;
305 int gdb_put_packet_inner(struct connection
*connection
, char *buffer
, int len
)
308 unsigned char my_checksum
= 0;
309 #ifdef _DEBUG_GDB_IO_
314 struct gdb_connection
*gdb_con
= connection
->priv
;
316 for (i
= 0; i
< len
; i
++)
317 my_checksum
+= buffer
[i
];
319 #ifdef _DEBUG_GDB_IO_
321 * At this point we should have nothing in the input queue from GDB,
322 * however sometimes '-' is sent even though we've already received
323 * an ACK (+) for everything we've sent off.
328 if ((retval
= check_pending(connection
, 0, &gotdata
)) != ERROR_OK
)
332 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
335 /* fix a problem with some IAR tools */
336 gdb_putback_char(connection
, reply
);
337 LOG_DEBUG("Unexpected start of new packet");
341 LOG_WARNING("Discard unexpected char %c", reply
);
347 #ifdef _DEBUG_GDB_IO_
348 debug_buffer
= malloc(len
+ 1);
349 memcpy(debug_buffer
, buffer
, len
);
350 debug_buffer
[len
] = 0;
351 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
355 char local_buffer
[1024];
356 local_buffer
[0] = '$';
357 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
359 /* performance gain on smaller packets by only a single call to gdb_write() */
360 memcpy(local_buffer
+ 1, buffer
, len
++);
361 local_buffer
[len
++] = '#';
362 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
363 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
364 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
371 /* larger packets are transmitted directly from caller supplied buffer
372 by several calls to gdb_write() to avoid dynamic allocation */
373 local_buffer
[1] = '#';
374 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
375 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
376 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
380 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
384 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
390 if (gdb_con
->noack_mode
)
393 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
398 else if (reply
== '-')
400 /* Stop sending output packets for now */
401 log_remove_callback(gdb_log_callback
, connection
);
402 LOG_WARNING("negative reply, retrying");
404 else if (reply
== 0x3)
407 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
411 else if (reply
== '-')
413 /* Stop sending output packets for now */
414 log_remove_callback(gdb_log_callback
, connection
);
415 LOG_WARNING("negative reply, retrying");
417 else if (reply
== '$') {
418 LOG_ERROR("GDB missing ack(1) - assumed good");
419 gdb_putback_char(connection
, reply
);
423 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
425 return ERROR_SERVER_REMOTE_CLOSED
;
428 else if (reply
== '$') {
429 LOG_ERROR("GDB missing ack(2) - assumed good");
430 gdb_putback_char(connection
, reply
);
435 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
437 return ERROR_SERVER_REMOTE_CLOSED
;
441 return ERROR_SERVER_REMOTE_CLOSED
;
446 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
448 struct gdb_connection
*gdb_con
= connection
->priv
;
450 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
453 /* we sent some data, reset timer for keep alive messages */
459 static __inline__
int fetch_packet(struct connection
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
461 unsigned char my_checksum
= 0;
466 struct gdb_connection
*gdb_con
= connection
->priv
;
472 /* The common case is that we have an entire packet with no escape chars.
473 * We need to leave at least 2 bytes in the buffer to have
474 * gdb_get_char() update various bits and bobs correctly.
476 if ((gdb_con
->buf_cnt
> 2) && ((gdb_con
->buf_cnt
+ count
) < *len
))
478 /* The compiler will struggle a bit with constant propagation and
479 * aliasing, so we help it by showing that these values do not
480 * change inside the loop
483 char *buf
= gdb_con
->buf_p
;
484 int run
= gdb_con
->buf_cnt
- 2;
491 if (character
== '#')
493 /* Danger! character can be '#' when esc is
494 * used so we need an explicit boolean for done here.
500 if (character
== '}')
502 /* data transmitted in binary mode (X packet)
503 * uses 0x7d as escape character */
504 my_checksum
+= character
& 0xff;
507 my_checksum
+= character
& 0xff;
508 buffer
[count
++] = (character
^ 0x20) & 0xff;
512 my_checksum
+= character
& 0xff;
513 buffer
[count
++] = character
& 0xff;
517 gdb_con
->buf_cnt
-= i
;
523 LOG_ERROR("packet buffer too small");
524 return ERROR_GDB_BUFFER_TOO_SMALL
;
527 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
530 if (character
== '#')
533 if (character
== '}')
535 /* data transmitted in binary mode (X packet)
536 * uses 0x7d as escape character */
537 my_checksum
+= character
& 0xff;
538 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
540 my_checksum
+= character
& 0xff;
541 buffer
[count
++] = (character
^ 0x20) & 0xff;
545 my_checksum
+= character
& 0xff;
546 buffer
[count
++] = character
& 0xff;
552 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
554 checksum
[0] = character
;
555 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
557 checksum
[1] = character
;
562 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
568 int gdb_get_packet_inner(struct connection
*connection
, char *buffer
, int *len
)
572 struct gdb_connection
*gdb_con
= connection
->priv
;
578 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
581 #ifdef _DEBUG_GDB_IO_
582 LOG_DEBUG("character: '%c'", character
);
590 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
591 * in case anyone tries to debug why they receive this warning every time */
592 LOG_WARNING("acknowledgment received, but no packet pending");
595 LOG_WARNING("negative acknowledgment, but no packet pending");
602 LOG_WARNING("ignoring character 0x%x", character
);
605 } while (character
!= '$');
610 /* explicit code expansion here to get faster inlined code in -O3 by not
611 * calculating checksum
613 if (gdb_con
->noack_mode
)
615 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
619 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
623 if (gdb_con
->noack_mode
)
625 /* checksum is not checked in noack mode */
630 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
638 return ERROR_SERVER_REMOTE_CLOSED
;
643 int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
645 struct gdb_connection
*gdb_con
= connection
->priv
;
647 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
652 int gdb_output_con(struct connection
*connection
, const char* line
)
657 bin_size
= strlen(line
);
659 hex_buffer
= malloc(bin_size
*2 + 2);
660 if (hex_buffer
== NULL
)
661 return ERROR_GDB_BUFFER_TOO_SMALL
;
664 for (i
= 0; i
< bin_size
; i
++)
665 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
666 hex_buffer
[bin_size
*2 + 1] = 0;
668 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
674 int gdb_output(struct command_context
*context
, const char* line
)
676 /* this will be dumped to the log and also sent as an O packet if possible */
677 LOG_USER_N("%s", line
);
682 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
684 struct gdb_connection
*gdb_connection
= connection
->priv
;
686 /* In the GDB protocol when we are stepping or continuing execution,
687 * we have a lingering reply. Upon receiving a halted event
688 * when we have that lingering packet, we reply to the original
689 * step or continue packet.
691 * Executing monitor commands can bring the target in and
692 * out of the running state so we'll see lots of TARGET_EVENT_XXX
693 * that are to be ignored.
695 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
700 /* stop forwarding log packets! */
701 log_remove_callback(gdb_log_callback
, connection
);
703 if (gdb_connection
->ctrl_c
)
706 gdb_connection
->ctrl_c
= 0;
710 signal
= gdb_last_signal(target
);
713 snprintf(sig_reply
, 4, "T%2.2x", signal
);
714 gdb_put_packet(connection
, sig_reply
, 3);
715 gdb_connection
->frontend_state
= TARGET_HALTED
;
719 int gdb_target_callback_event_handler(struct target
*target
, enum target_event event
, void *priv
)
722 struct connection
*connection
= priv
;
724 target_handle_event(target
, event
);
727 case TARGET_EVENT_GDB_HALT
:
728 gdb_frontend_halted(target
, connection
);
730 case TARGET_EVENT_HALTED
:
731 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
733 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
734 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
735 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
747 int gdb_new_connection(struct connection
*connection
)
749 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
750 struct gdb_service
*gdb_service
= connection
->service
->priv
;
754 connection
->priv
= gdb_connection
;
756 /* initialize gdb connection information */
757 gdb_connection
->buf_p
= gdb_connection
->buffer
;
758 gdb_connection
->buf_cnt
= 0;
759 gdb_connection
->ctrl_c
= 0;
760 gdb_connection
->frontend_state
= TARGET_HALTED
;
761 gdb_connection
->vflash_image
= NULL
;
762 gdb_connection
->closed
= 0;
763 gdb_connection
->busy
= 0;
764 gdb_connection
->noack_mode
= 0;
765 gdb_connection
->sync
= true;
767 /* send ACK to GDB for debug request */
768 gdb_write(connection
, "+", 1);
770 /* output goes through gdb connection */
771 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
773 /* we must remove all breakpoints registered to the target as a previous
774 * GDB session could leave dangling breakpoints if e.g. communication
777 breakpoint_clear_target(gdb_service
->target
);
778 watchpoint_clear_target(gdb_service
->target
);
780 /* register callback to be informed about target events */
781 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
783 /* remove the initial ACK from the incoming buffer */
784 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
787 /* FIX!!!??? would we actually ever receive a + here???
790 if (initial_ack
!= '+')
791 gdb_putback_char(connection
, initial_ack
);
792 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
794 gdb_actual_connections
++;
795 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
796 gdb_actual_connections
,
797 target_name(gdb_service
->target
),
798 target_state_name(gdb_service
->target
));
803 int gdb_connection_closed(struct connection
*connection
)
805 struct gdb_service
*gdb_service
= connection
->service
->priv
;
806 struct gdb_connection
*gdb_connection
= connection
->priv
;
808 /* we're done forwarding messages. Tear down callback before
809 * cleaning up connection.
811 log_remove_callback(gdb_log_callback
, connection
);
813 gdb_actual_connections
--;
814 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
815 target_name(gdb_service
->target
),
816 target_state_name(gdb_service
->target
),
817 gdb_actual_connections
);
819 /* see if an image built with vFlash commands is left */
820 if (gdb_connection
->vflash_image
)
822 image_close(gdb_connection
->vflash_image
);
823 free(gdb_connection
->vflash_image
);
824 gdb_connection
->vflash_image
= NULL
;
827 /* if this connection registered a debug-message receiver delete it */
828 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
830 if (connection
->priv
)
832 free(connection
->priv
);
833 connection
->priv
= NULL
;
837 LOG_ERROR("BUG: connection->priv == NULL");
841 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
843 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
845 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
850 void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
853 snprintf(err
, 4, "E%2.2X", the_error
);
854 gdb_put_packet(connection
, err
, 3);
857 int gdb_last_signal_packet(struct connection
*connection
, struct target
*target
, char* packet
, int packet_size
)
862 signal
= gdb_last_signal(target
);
864 snprintf(sig_reply
, 4, "S%2.2x", signal
);
865 gdb_put_packet(connection
, sig_reply
, 3);
870 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
872 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
875 return len
- 1 - pos
;
878 /* Convert register to string of bytes. NB! The # of bits in the
879 * register might be non-divisible by 8(a byte), in which
880 * case an entire byte is shown.
882 * NB! the format on the wire is the target endianness
884 * The format of reg->value is little endian
887 void gdb_str_to_target(struct target
*target
, char *tstr
, struct reg
*reg
)
894 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
896 for (i
= 0; i
< buf_len
; i
++)
898 int j
= gdb_reg_pos(target
, i
, buf_len
);
899 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
900 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
904 static int hextoint(char c
)
915 LOG_ERROR("BUG: invalid register value %08x", c
);
919 /* copy over in register buffer */
920 void gdb_target_to_reg(struct target
*target
, char *tstr
, int str_len
, uint8_t *bin
)
924 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
929 for (i
= 0; i
< str_len
; i
+= 2)
931 uint8_t t
= hextoint(tstr
[i
]) << 4;
932 t
|= hextoint(tstr
[i
+ 1]);
934 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
939 int gdb_get_registers_packet(struct connection
*connection
, struct target
*target
, char* packet
, int packet_size
)
941 struct reg
**reg_list
;
944 int reg_packet_size
= 0;
949 #ifdef _DEBUG_GDB_IO_
953 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
955 return gdb_error(connection
, retval
);
958 for (i
= 0; i
< reg_list_size
; i
++)
960 reg_packet_size
+= reg_list
[i
]->size
;
963 reg_packet
= malloc(DIV_ROUND_UP(reg_packet_size
, 8) * 2);
964 reg_packet_p
= reg_packet
;
966 for (i
= 0; i
< reg_list_size
; i
++)
968 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
969 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
972 #ifdef _DEBUG_GDB_IO_
975 reg_packet_p
= strndup(reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
976 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
981 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
989 int gdb_set_registers_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
992 struct reg
**reg_list
;
997 #ifdef _DEBUG_GDB_IO_
1001 /* skip command character */
1005 if (packet_size
% 2)
1007 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1008 return ERROR_SERVER_REMOTE_CLOSED
;
1011 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1013 return gdb_error(connection
, retval
);
1017 for (i
= 0; i
< reg_list_size
; i
++)
1020 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1022 if (packet_p
+ chars
> packet
+ packet_size
)
1024 LOG_ERROR("BUG: register packet is too small for registers");
1027 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1028 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1030 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1032 /* advance packet pointer */
1039 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1042 gdb_put_packet(connection
, "OK", 2);
1047 int gdb_get_register_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1050 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1051 struct reg
**reg_list
;
1055 #ifdef _DEBUG_GDB_IO_
1059 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1061 return gdb_error(connection
, retval
);
1064 if (reg_list_size
<= reg_num
)
1066 LOG_ERROR("gdb requested a non-existing register");
1070 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1072 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1074 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1082 int gdb_set_register_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1086 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1087 struct reg
**reg_list
;
1093 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1095 return gdb_error(connection
, retval
);
1098 if (reg_list_size
< reg_num
)
1100 LOG_ERROR("gdb requested a non-existing register");
1101 return ERROR_SERVER_REMOTE_CLOSED
;
1104 if (*separator
!= '=')
1106 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1107 return ERROR_SERVER_REMOTE_CLOSED
;
1110 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1111 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1112 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1114 /* fix!!! add some sanity checks on packet size here */
1116 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1118 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1120 gdb_put_packet(connection
, "OK", 2);
1128 int gdb_error(struct connection
*connection
, int retval
)
1132 case ERROR_TARGET_DATA_ABORT
:
1133 gdb_send_error(connection
, EIO
);
1135 case ERROR_TARGET_TRANSLATION_FAULT
:
1136 gdb_send_error(connection
, EFAULT
);
1138 case ERROR_TARGET_UNALIGNED_ACCESS
:
1139 gdb_send_error(connection
, EFAULT
);
1141 case ERROR_TARGET_NOT_HALTED
:
1142 gdb_send_error(connection
, EFAULT
);
1145 /* This could be that the target reset itself. */
1146 LOG_ERROR("unexpected error %i", retval
);
1147 gdb_send_error(connection
, EFAULT
);
1154 /* We don't have to worry about the default 2 second timeout for GDB packets,
1155 * because GDB breaks up large memory reads into smaller reads.
1157 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1159 int gdb_read_memory_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1168 int retval
= ERROR_OK
;
1170 /* skip command character */
1173 addr
= strtoul(packet
, &separator
, 16);
1175 if (*separator
!= ',')
1177 LOG_ERROR("incomplete read memory packet received, dropping connection");
1178 return ERROR_SERVER_REMOTE_CLOSED
;
1181 len
= strtoul(separator
+ 1, NULL
, 16);
1183 buffer
= malloc(len
);
1185 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1187 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1189 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1191 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1192 * At some point this might be fixed in GDB, in which case this code can be removed.
1194 * OpenOCD developers are acutely aware of this problem, but there is nothing
1195 * gained by involving the user in this problem that hopefully will get resolved
1198 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1200 * For now, the default is to fix up things to make current GDB versions work.
1201 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1203 memset(buffer
, 0, len
);
1207 if (retval
== ERROR_OK
)
1209 hex_buffer
= malloc(len
* 2 + 1);
1212 for (i
= 0; i
< len
; i
++)
1214 uint8_t t
= buffer
[i
];
1215 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1216 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1219 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1225 retval
= gdb_error(connection
, retval
);
1233 int gdb_write_memory_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1244 /* skip command character */
1247 addr
= strtoul(packet
, &separator
, 16);
1249 if (*separator
!= ',')
1251 LOG_ERROR("incomplete write memory packet received, dropping connection");
1252 return ERROR_SERVER_REMOTE_CLOSED
;
1255 len
= strtoul(separator
+ 1, &separator
, 16);
1257 if (*(separator
++) != ':')
1259 LOG_ERROR("incomplete write memory packet received, dropping connection");
1260 return ERROR_SERVER_REMOTE_CLOSED
;
1263 buffer
= malloc(len
);
1265 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1267 for (i
= 0; i
< len
; i
++)
1270 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1274 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1276 if (retval
== ERROR_OK
)
1278 gdb_put_packet(connection
, "OK", 2);
1282 retval
= gdb_error(connection
, retval
);
1290 int gdb_write_memory_binary_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1298 /* skip command character */
1301 addr
= strtoul(packet
, &separator
, 16);
1303 if (*separator
!= ',')
1305 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1306 return ERROR_SERVER_REMOTE_CLOSED
;
1309 len
= strtoul(separator
+ 1, &separator
, 16);
1311 if (*(separator
++) != ':')
1313 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1314 return ERROR_SERVER_REMOTE_CLOSED
;
1320 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1322 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1325 if (retval
== ERROR_OK
)
1327 gdb_put_packet(connection
, "OK", 2);
1331 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1338 int gdb_step_continue_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1341 uint32_t address
= 0x0;
1342 int retval
= ERROR_OK
;
1346 if (packet_size
> 1)
1348 packet
[packet_size
] = 0;
1349 address
= strtoul(packet
+ 1, NULL
, 16);
1356 if (packet
[0] == 'c')
1358 LOG_DEBUG("continue");
1359 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1360 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1362 else if (packet
[0] == 's')
1365 /* step at current or address, don't handle breakpoints */
1366 retval
= target_step(target
, current
, address
, 0);
1371 int gdb_breakpoint_watchpoint_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1374 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1375 enum watchpoint_rw wp_type
;
1383 type
= strtoul(packet
+ 1, &separator
, 16);
1385 if (type
== 0) /* memory breakpoint */
1386 bp_type
= BKPT_SOFT
;
1387 else if (type
== 1) /* hardware breakpoint */
1388 bp_type
= BKPT_HARD
;
1389 else if (type
== 2) /* write watchpoint */
1390 wp_type
= WPT_WRITE
;
1391 else if (type
== 3) /* read watchpoint */
1393 else if (type
== 4) /* access watchpoint */
1394 wp_type
= WPT_ACCESS
;
1396 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1398 bp_type
= gdb_breakpoint_override_type
;
1401 if (*separator
!= ',')
1403 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1404 return ERROR_SERVER_REMOTE_CLOSED
;
1407 address
= strtoul(separator
+ 1, &separator
, 16);
1409 if (*separator
!= ',')
1411 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1412 return ERROR_SERVER_REMOTE_CLOSED
;
1415 size
= strtoul(separator
+ 1, &separator
, 16);
1421 if (packet
[0] == 'Z')
1423 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1425 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1430 gdb_put_packet(connection
, "OK", 2);
1435 breakpoint_remove(target
, address
);
1436 gdb_put_packet(connection
, "OK", 2);
1443 if (packet
[0] == 'Z')
1445 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1447 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1452 gdb_put_packet(connection
, "OK", 2);
1457 watchpoint_remove(target
, address
);
1458 gdb_put_packet(connection
, "OK", 2);
1469 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1470 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1472 if (*retval
!= ERROR_OK
)
1480 if ((*xml
== NULL
) || (!first
))
1482 /* start by 0 to exercise all the code paths.
1483 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1485 *size
= *size
* 2 + 2;
1487 *xml
= realloc(*xml
, *size
);
1492 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1500 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1502 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1507 /* there was just enough or not enough space, allocate more. */
1512 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1516 /* Extract and NUL-terminate the annex. */
1518 while (*buf
&& *buf
!= ':')
1524 /* After the read marker and annex, qXfer looks like a
1525 * traditional 'm' packet. */
1527 *ofs
= strtoul(buf
, &separator
, 16);
1529 if (*separator
!= ',')
1532 *len
= strtoul(separator
+ 1, NULL
, 16);
1537 int gdb_calc_blocksize(struct flash_bank
*bank
)
1540 uint32_t block_size
= 0xffffffff;
1542 /* loop through all sectors and return smallest sector size */
1544 for (i
= 0; i
< (uint32_t)bank
->num_sectors
; i
++)
1546 if (bank
->sectors
[i
].size
< block_size
)
1547 block_size
= bank
->sectors
[i
].size
;
1553 static int compare_bank (const void * a
, const void * b
)
1555 struct flash_bank
*b1
, *b2
;
1556 b1
=*((struct flash_bank
**)a
);
1557 b2
=*((struct flash_bank
**)b
);
1559 if (b1
->base
== b2
->base
)
1562 } else if (b1
->base
> b2
->base
)
1571 int gdb_query_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1573 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1574 struct gdb_connection
*gdb_connection
= connection
->priv
;
1576 if (strstr(packet
, "qRcmd,"))
1578 if (packet_size
> 6)
1582 cmd
= malloc((packet_size
- 6)/2 + 1);
1583 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1586 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1589 cmd
[(packet_size
- 6)/2] = 0x0;
1591 /* We want to print all debug output to GDB connection */
1592 log_add_callback(gdb_log_callback
, connection
);
1593 target_call_timer_callbacks_now();
1594 /* some commands need to know the GDB connection, make note of current
1595 * GDB connection. */
1596 current_gdb_connection
= gdb_connection
;
1597 command_run_line(cmd_ctx
, cmd
);
1598 current_gdb_connection
= NULL
;
1599 target_call_timer_callbacks_now();
1600 log_remove_callback(gdb_log_callback
, connection
);
1603 gdb_put_packet(connection
, "OK", 2);
1606 else if (strstr(packet
, "qCRC:"))
1608 if (packet_size
> 5)
1617 /* skip command character */
1620 addr
= strtoul(packet
, &separator
, 16);
1622 if (*separator
!= ',')
1624 LOG_ERROR("incomplete read memory packet received, dropping connection");
1625 return ERROR_SERVER_REMOTE_CLOSED
;
1628 len
= strtoul(separator
+ 1, NULL
, 16);
1630 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1632 if (retval
== ERROR_OK
)
1634 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1635 gdb_put_packet(connection
, gdb_reply
, 9);
1639 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1646 else if (strstr(packet
, "qSupported"))
1648 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1649 * disable qXfer:features:read for the moment */
1650 int retval
= ERROR_OK
;
1651 char *buffer
= NULL
;
1655 xml_printf(&retval
, &buffer
, &pos
, &size
,
1656 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1657 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1659 if (retval
!= ERROR_OK
)
1661 gdb_send_error(connection
, 01);
1665 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1670 else if (strstr(packet
, "qXfer:memory-map:read::") && (flash_get_bank_count() > 0))
1672 /* We get away with only specifying flash here. Regions that are not
1673 * specified are treated as if we provided no memory map(if not we
1674 * could detect the holes and mark them as RAM).
1675 * Normally we only execute this code once, but no big deal if we
1676 * have to regenerate it a couple of times. */
1678 struct flash_bank
*p
;
1682 int retval
= ERROR_OK
;
1689 /* skip command character */
1692 offset
= strtoul(packet
, &separator
, 16);
1693 length
= strtoul(separator
+ 1, &separator
, 16);
1695 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1698 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1699 read/write) by default for GDB.
1700 GDB does not have a concept of non-cacheable read/write memory.
1702 struct flash_bank
**banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1705 for (i
= 0; i
< flash_get_bank_count(); i
++)
1707 p
= get_flash_bank_by_num(i
);
1711 retval
= ERROR_FAIL
;
1712 gdb_send_error(connection
, retval
);
1718 qsort(banks
, flash_get_bank_count(), sizeof(struct flash_bank
*), compare_bank
);
1720 uint32_t ram_start
= 0;
1721 for (i
= 0; i
< flash_get_bank_count(); i
++)
1725 if (ram_start
< p
->base
)
1727 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1728 ram_start
, p
->base
-ram_start
);
1731 /* if device has uneven sector sizes, eg. str7, lpc
1732 * we pass the smallest sector size to gdb memory map */
1733 blocksize
= gdb_calc_blocksize(p
);
1735 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1736 "<property name=\"blocksize\">0x%x</property>\n" \
1738 p
->base
, p
->size
, blocksize
);
1739 ram_start
= p
->base
+ p
->size
;
1743 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1744 ram_start
, 0-ram_start
);
1747 /* a flash chip could be at the very end of the 32 bit address space, in which case
1748 ram_start will be precisely 0 */
1754 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1756 if (retval
!= ERROR_OK
)
1758 gdb_send_error(connection
, retval
);
1762 if (offset
+ length
> pos
)
1764 length
= pos
- offset
;
1767 char *t
= malloc(length
+ 1);
1769 memcpy(t
+ 1, xml
+ offset
, length
);
1770 gdb_put_packet(connection
, t
, length
+ 1);
1776 else if (strstr(packet
, "qXfer:features:read:"))
1781 int retval
= ERROR_OK
;
1784 unsigned int length
;
1787 /* skip command character */
1790 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1792 gdb_send_error(connection
, 01);
1796 if (strcmp(annex
, "target.xml") != 0)
1798 gdb_send_error(connection
, 01);
1802 xml_printf(&retval
, &xml
, &pos
, &size
, \
1803 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1805 if (retval
!= ERROR_OK
)
1807 gdb_send_error(connection
, retval
);
1811 gdb_put_packet(connection
, xml
, strlen(xml
));
1816 else if (strstr(packet
, "QStartNoAckMode"))
1818 gdb_connection
->noack_mode
= 1;
1819 gdb_put_packet(connection
, "OK", 2);
1823 gdb_put_packet(connection
, "", 0);
1827 int gdb_v_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1829 struct gdb_connection
*gdb_connection
= connection
->priv
;
1830 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1833 /* if flash programming disabled - send a empty reply */
1835 if (gdb_flash_program
== 0)
1837 gdb_put_packet(connection
, "", 0);
1841 if (strstr(packet
, "vFlashErase:"))
1844 unsigned long length
;
1846 char *parse
= packet
+ 12;
1849 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1850 return ERROR_SERVER_REMOTE_CLOSED
;
1853 addr
= strtoul(parse
, &parse
, 16);
1855 if (*(parse
++) != ',' || *parse
== '\0')
1857 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1858 return ERROR_SERVER_REMOTE_CLOSED
;
1861 length
= strtoul(parse
, &parse
, 16);
1865 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1866 return ERROR_SERVER_REMOTE_CLOSED
;
1869 /* assume all sectors need erasing - stops any problems
1870 * when flash_write is called multiple times */
1873 /* perform any target specific operations before the erase */
1874 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_START
);
1875 result
= flash_erase_address_range(gdb_service
->target
, addr
, length
);
1876 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_END
);
1879 if (result
!= ERROR_OK
)
1881 /* GDB doesn't evaluate the actual error number returned,
1882 * treat a failed erase as an I/O error
1884 gdb_send_error(connection
, EIO
);
1885 LOG_ERROR("flash_erase returned %i", result
);
1888 gdb_put_packet(connection
, "OK", 2);
1893 if (strstr(packet
, "vFlashWrite:"))
1897 unsigned long length
;
1898 char *parse
= packet
+ 12;
1902 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1903 return ERROR_SERVER_REMOTE_CLOSED
;
1905 addr
= strtoul(parse
, &parse
, 16);
1906 if (*(parse
++) != ':')
1908 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1909 return ERROR_SERVER_REMOTE_CLOSED
;
1911 length
= packet_size
- (parse
- packet
);
1913 /* create a new image if there isn't already one */
1914 if (gdb_connection
->vflash_image
== NULL
)
1916 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
1917 image_open(gdb_connection
->vflash_image
, "", "build");
1920 /* create new section with content from packet buffer */
1921 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
1926 gdb_put_packet(connection
, "OK", 2);
1931 if (!strcmp(packet
, "vFlashDone"))
1935 /* process the flashing buffer. No need to erase as GDB
1936 * always issues a vFlashErase first. */
1937 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
1938 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1939 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1940 if (result
!= ERROR_OK
)
1942 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1943 gdb_put_packet(connection
, "E.memtype", 9);
1945 gdb_send_error(connection
, EIO
);
1949 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
1950 gdb_put_packet(connection
, "OK", 2);
1953 image_close(gdb_connection
->vflash_image
);
1954 free(gdb_connection
->vflash_image
);
1955 gdb_connection
->vflash_image
= NULL
;
1960 gdb_put_packet(connection
, "", 0);
1964 int gdb_detach(struct connection
*connection
, struct target
*target
)
1966 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1968 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
1970 return gdb_put_packet(connection
, "OK", 2);
1973 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
1974 const char *function
, const char *string
)
1976 struct connection
*connection
= priv
;
1977 struct gdb_connection
*gdb_con
= connection
->priv
;
1981 /* do not reply this using the O packet */
1985 gdb_output_con(connection
, string
);
1988 /* Do not allocate this on the stack */
1989 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
1991 static void gdb_sig_halted(struct connection
*connection
)
1994 snprintf(sig_reply
, 4, "T%2.2x", 2);
1995 gdb_put_packet(connection
, sig_reply
, 3);
1999 int gdb_input_inner(struct connection
*connection
)
2001 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2002 struct target
*target
= gdb_service
->target
;
2003 char *packet
= gdb_packet_buffer
;
2006 struct gdb_connection
*gdb_con
= connection
->priv
;
2007 static int extended_protocol
= 0;
2009 /* drain input buffer */
2012 packet_size
= GDB_BUFFER_SIZE
-1;
2013 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
2018 /* terminate with zero */
2019 packet
[packet_size
] = 0;
2021 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2022 if (packet
[0] == 'X') {
2023 // binary packets spew junk into the debug log stream
2026 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2030 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2032 LOG_DEBUG("received packet: '%s'", packet
);
2036 if (packet_size
> 0)
2042 /* Hct... -- set thread
2043 * we don't have threads, send empty reply */
2044 gdb_put_packet(connection
, NULL
, 0);
2048 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
2051 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
2054 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
2057 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
2060 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
2063 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
2066 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
2070 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2073 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
2078 int retval
= ERROR_OK
;
2080 struct gdb_connection
*gdb_con
= connection
->priv
;
2081 log_add_callback(gdb_log_callback
, connection
);
2083 bool nostep
= false;
2084 if (target
->state
== TARGET_RUNNING
)
2086 LOG_WARNING("The target is already running. Halt target before stepi/continue.");
2087 retval
= target_halt(target
);
2088 if (retval
== ERROR_OK
)
2089 retval
= target_wait_state(target
, TARGET_HALTED
, 100);
2090 } else if (target
->state
!= TARGET_HALTED
)
2092 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2094 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2096 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2097 * sent by GDB first to OpenOCD, thus defeating the check to
2098 * make only the single stepping have the sync feature...
2101 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2103 gdb_con
->sync
= false;
2105 if ((retval
!=ERROR_OK
) || nostep
)
2107 /* Either the target isn't in the halted state, then we can't
2108 * step/continue. This might be early setup, etc.
2110 * Or we want to allow GDB to pick up a fresh set of
2111 * register values without modifying the target state.
2114 gdb_sig_halted(connection
);
2116 /* stop forwarding log packets! */
2117 log_remove_callback(gdb_log_callback
, connection
);
2120 /* We're running/stepping, in which case we can
2121 * forward log output until the target is halted
2123 gdb_con
->frontend_state
= TARGET_RUNNING
;
2124 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2125 int retval
= gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2126 if (retval
!= ERROR_OK
)
2128 /* we'll never receive a halted condition... issue a false one.. */
2129 gdb_frontend_halted(target
, connection
);
2135 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
2138 retval
= gdb_detach(connection
, target
);
2139 extended_protocol
= 0;
2142 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
2146 if (extended_protocol
!= 0)
2148 gdb_put_packet(connection
, "OK", 2);
2149 return ERROR_SERVER_REMOTE_CLOSED
;
2151 /* handle extended remote protocol */
2152 extended_protocol
= 1;
2153 gdb_put_packet(connection
, "OK", 2);
2156 /* handle extended restart packet */
2157 breakpoint_clear_target(gdb_service
->target
);
2158 watchpoint_clear_target(gdb_service
->target
);
2159 command_run_linef(connection
->cmd_ctx
,
2160 "ocd_gdb_restart %s",
2161 target_name(target
));
2164 /* ignore unknown packets */
2165 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2166 gdb_put_packet(connection
, NULL
, 0);
2170 /* if a packet handler returned an error, exit input loop */
2171 if (retval
!= ERROR_OK
)
2175 if (gdb_con
->ctrl_c
)
2177 if (target
->state
== TARGET_RUNNING
)
2179 retval
= target_halt(target
);
2180 if (retval
!= ERROR_OK
)
2182 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2184 gdb_con
->ctrl_c
= 0;
2187 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2188 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2192 } while (gdb_con
->buf_cnt
> 0);
2197 int gdb_input(struct connection
*connection
)
2199 int retval
= gdb_input_inner(connection
);
2200 struct gdb_connection
*gdb_con
= connection
->priv
;
2201 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2204 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2205 if (gdb_con
->closed
)
2206 return ERROR_SERVER_REMOTE_CLOSED
;
2208 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2212 static int gdb_target_start(struct target
*target
, uint16_t port
)
2214 bool use_pipes
= 0 == port
;
2215 struct gdb_service
*gdb_service
= malloc(sizeof(struct gdb_service
));
2216 if (NULL
== gdb_service
)
2219 gdb_service
->target
= target
;
2221 add_service("gdb", use_pipes
? CONNECTION_PIPE
: CONNECTION_TCP
,
2222 port
, 1, &gdb_new_connection
, &gdb_input
,
2223 &gdb_connection_closed
, gdb_service
);
2225 const char *name
= target_name(target
);
2227 LOG_DEBUG("gdb service for target '%s' using pipes", name
);
2229 LOG_DEBUG("gdb service for target '%s' on TCP port %u", name
, port
);
2233 int gdb_target_add_one(struct target
*target
)
2235 if (gdb_port
== 0 && server_use_pipes
== 0)
2237 LOG_INFO("gdb port disabled");
2240 if (0 == gdb_port_next
)
2241 gdb_port_next
= gdb_port
;
2243 bool use_pipes
= server_use_pipes
;
2244 static bool server_started_with_pipes
= false;
2245 if (server_started_with_pipes
)
2247 LOG_WARNING("gdb service permits one target when using pipes");
2254 int e
= gdb_target_start(target
, use_pipes
? 0 : gdb_port_next
);
2257 server_started_with_pipes
|= use_pipes
;
2263 int gdb_target_add_all(struct target
*target
)
2267 LOG_WARNING("gdb services need one or more targets defined");
2271 while (NULL
!= target
)
2273 int retval
= gdb_target_add_one(target
);
2274 if (ERROR_OK
!= retval
)
2277 target
= target
->next
;
2283 COMMAND_HANDLER(handle_gdb_sync_command
)
2287 return ERROR_COMMAND_SYNTAX_ERROR
;
2290 if (current_gdb_connection
== NULL
)
2292 command_print(CMD_CTX
,
2293 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2297 current_gdb_connection
->sync
= true;
2302 /* daemon configuration command gdb_port */
2303 COMMAND_HANDLER(handle_gdb_port_command
)
2305 int retval
= CALL_COMMAND_HANDLER(server_port_command
, &gdb_port
);
2306 if (ERROR_OK
== retval
)
2307 gdb_port_next
= gdb_port
;
2311 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2314 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2316 return ERROR_COMMAND_SYNTAX_ERROR
;
2319 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2322 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2324 return ERROR_COMMAND_SYNTAX_ERROR
;
2327 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2330 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2332 return ERROR_COMMAND_SYNTAX_ERROR
;
2335 /* gdb_breakpoint_override */
2336 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2341 } else if (CMD_ARGC
== 1)
2343 gdb_breakpoint_override
= 1;
2344 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2346 gdb_breakpoint_override_type
= BKPT_HARD
;
2347 } else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2349 gdb_breakpoint_override_type
= BKPT_SOFT
;
2350 } else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2352 gdb_breakpoint_override
= 0;
2356 return ERROR_COMMAND_SYNTAX_ERROR
;
2358 if (gdb_breakpoint_override
)
2360 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2363 LOG_USER("breakpoint type is not overridden");
2369 static const struct command_registration gdb_command_handlers
[] = {
2372 .handler
= &handle_gdb_sync_command
,
2373 .mode
= COMMAND_ANY
,
2374 .help
= "next stepi will return immediately allowing "
2375 "GDB to fetch register state without affecting "
2380 .handler
= &handle_gdb_port_command
,
2381 .mode
= COMMAND_ANY
,
2382 .help
= "daemon configuration command gdb_port",
2386 .name
= "gdb_memory_map",
2387 .handler
= &handle_gdb_memory_map_command
,
2388 .mode
= COMMAND_CONFIG
,
2389 .help
= "enable or disable memory map",
2390 .usage
= "enable|disable"
2393 .name
= "gdb_flash_program",
2394 .handler
= &handle_gdb_flash_program_command
,
2395 .mode
= COMMAND_CONFIG
,
2396 .help
= "enable or disable flash program",
2397 .usage
= "enable|disable"
2400 .name
= "gdb_report_data_abort",
2401 .handler
= &handle_gdb_report_data_abort_command
,
2402 .mode
= COMMAND_CONFIG
,
2403 .help
= "enable or disable reporting data aborts",
2404 .usage
= "enable|disable"
2407 .name
= "gdb_breakpoint_override",
2408 .handler
= &handle_gdb_breakpoint_override_command
,
2409 .mode
= COMMAND_EXEC
,
2410 .help
= "force type of breakpoint "
2411 "used by gdb 'break' commands.",
2412 .usage
= "hard|soft|disable",
2414 COMMAND_REGISTRATION_DONE
2417 int gdb_register_commands(struct command_context
*cmd_ctx
)
2419 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);