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 static int gdb_get_char_inner(struct connection
*connection
, int* next_char
)
154 struct gdb_connection
*gdb_con
= connection
->priv
;
155 int retval
= ERROR_OK
;
159 if (connection
->service
->type
== CONNECTION_PIPE
)
161 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
165 retval
= check_pending(connection
, 1, NULL
);
166 if (retval
!= ERROR_OK
)
168 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
171 if (gdb_con
->buf_cnt
> 0)
175 if (gdb_con
->buf_cnt
== 0)
178 return ERROR_SERVER_REMOTE_CLOSED
;
182 errno
= WSAGetLastError();
189 case WSAECONNABORTED
:
191 return ERROR_SERVER_REMOTE_CLOSED
;
194 return ERROR_SERVER_REMOTE_CLOSED
;
196 LOG_ERROR("read: %d", errno
);
207 return ERROR_SERVER_REMOTE_CLOSED
;
210 return ERROR_SERVER_REMOTE_CLOSED
;
212 LOG_ERROR("read: %s", strerror(errno
));
214 return ERROR_SERVER_REMOTE_CLOSED
;
219 #ifdef _DEBUG_GDB_IO_
220 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
221 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
222 debug_buffer
[gdb_con
->buf_cnt
] = 0;
223 LOG_DEBUG("received '%s'", debug_buffer
);
227 gdb_con
->buf_p
= gdb_con
->buffer
;
229 *next_char
= *(gdb_con
->buf_p
++);
230 if (gdb_con
->buf_cnt
> 0)
231 connection
->input_pending
= 1;
233 connection
->input_pending
= 0;
234 #ifdef _DEBUG_GDB_IO_
235 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
242 * The cool thing about this fn is that it allows buf_p and buf_cnt to be
243 * held in registers in the inner loop.
245 * For small caches and embedded systems this is important!
247 static inline int gdb_get_char_fast(struct connection
*connection
, int* next_char
, char **buf_p
, int *buf_cnt
)
249 int retval
= ERROR_OK
;
251 if ((*buf_cnt
)-- > 0)
253 *next_char
= **buf_p
;
256 connection
->input_pending
= 1;
258 connection
->input_pending
= 0;
260 #ifdef _DEBUG_GDB_IO_
261 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
267 struct gdb_connection
*gdb_con
= connection
->priv
;
268 gdb_con
->buf_p
= *buf_p
;
269 gdb_con
->buf_cnt
= *buf_cnt
;
270 retval
= gdb_get_char_inner(connection
, next_char
);
271 *buf_p
= gdb_con
->buf_p
;
272 *buf_cnt
= gdb_con
->buf_cnt
;
278 int gdb_get_char(struct connection
*connection
, int* next_char
)
280 struct gdb_connection
*gdb_con
= connection
->priv
;
281 return gdb_get_char_fast(connection
, next_char
, &gdb_con
->buf_p
, &gdb_con
->buf_cnt
);
285 int gdb_putback_char(struct connection
*connection
, int last_char
)
287 struct gdb_connection
*gdb_con
= connection
->priv
;
289 if (gdb_con
->buf_p
> gdb_con
->buffer
)
291 *(--gdb_con
->buf_p
) = last_char
;
296 LOG_ERROR("BUG: couldn't put character back");
302 /* The only way we can detect that the socket is closed is the first time
303 * we write to it, we will fail. Subsequent write operations will
304 * succeed. Shudder! */
305 int gdb_write(struct connection
*connection
, void *data
, int len
)
307 struct gdb_connection
*gdb_con
= connection
->priv
;
309 return ERROR_SERVER_REMOTE_CLOSED
;
311 if (connection
->service
->type
== CONNECTION_PIPE
)
313 /* write to stdout */
314 if (write(STDOUT_FILENO
, data
, len
) == len
)
321 if (write_socket(connection
->fd
, data
, len
) == len
)
327 return ERROR_SERVER_REMOTE_CLOSED
;
330 int gdb_put_packet_inner(struct connection
*connection
, char *buffer
, int len
)
333 unsigned char my_checksum
= 0;
334 #ifdef _DEBUG_GDB_IO_
339 struct gdb_connection
*gdb_con
= connection
->priv
;
341 for (i
= 0; i
< len
; i
++)
342 my_checksum
+= buffer
[i
];
344 #ifdef _DEBUG_GDB_IO_
346 * At this point we should have nothing in the input queue from GDB,
347 * however sometimes '-' is sent even though we've already received
348 * an ACK (+) for everything we've sent off.
353 if ((retval
= check_pending(connection
, 0, &gotdata
)) != ERROR_OK
)
357 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
360 /* fix a problem with some IAR tools */
361 gdb_putback_char(connection
, reply
);
362 LOG_DEBUG("Unexpected start of new packet");
366 LOG_WARNING("Discard unexpected char %c", reply
);
372 #ifdef _DEBUG_GDB_IO_
373 debug_buffer
= malloc(len
+ 1);
374 memcpy(debug_buffer
, buffer
, len
);
375 debug_buffer
[len
] = 0;
376 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
380 char local_buffer
[1024];
381 local_buffer
[0] = '$';
382 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
384 /* performance gain on smaller packets by only a single call to gdb_write() */
385 memcpy(local_buffer
+ 1, buffer
, len
++);
386 local_buffer
[len
++] = '#';
387 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
388 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
389 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
396 /* larger packets are transmitted directly from caller supplied buffer
397 by several calls to gdb_write() to avoid dynamic allocation */
398 local_buffer
[1] = '#';
399 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
400 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
401 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
405 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
409 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
415 if (gdb_con
->noack_mode
)
418 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
423 else if (reply
== '-')
425 /* Stop sending output packets for now */
426 log_remove_callback(gdb_log_callback
, connection
);
427 LOG_WARNING("negative reply, retrying");
429 else if (reply
== 0x3)
432 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
436 else if (reply
== '-')
438 /* Stop sending output packets for now */
439 log_remove_callback(gdb_log_callback
, connection
);
440 LOG_WARNING("negative reply, retrying");
442 else if (reply
== '$') {
443 LOG_ERROR("GDB missing ack(1) - assumed good");
444 gdb_putback_char(connection
, reply
);
448 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
450 return ERROR_SERVER_REMOTE_CLOSED
;
453 else if (reply
== '$') {
454 LOG_ERROR("GDB missing ack(2) - assumed good");
455 gdb_putback_char(connection
, reply
);
460 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
462 return ERROR_SERVER_REMOTE_CLOSED
;
466 return ERROR_SERVER_REMOTE_CLOSED
;
471 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
473 struct gdb_connection
*gdb_con
= connection
->priv
;
475 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
478 /* we sent some data, reset timer for keep alive messages */
484 static __inline__
int fetch_packet(struct connection
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
486 unsigned char my_checksum
= 0;
489 int retval
= ERROR_OK
;
491 struct gdb_connection
*gdb_con
= connection
->priv
;
496 /* move this over into local variables to use registers and give the
497 * more freedom to optimize */
498 char *buf_p
= gdb_con
->buf_p
;
499 int buf_cnt
= gdb_con
->buf_cnt
;
503 /* The common case is that we have an entire packet with no escape chars.
504 * We need to leave at least 2 bytes in the buffer to have
505 * gdb_get_char() update various bits and bobs correctly.
507 if ((buf_cnt
> 2) && ((buf_cnt
+ count
) < *len
))
509 /* The compiler will struggle a bit with constant propagation and
510 * aliasing, so we help it by showing that these values do not
511 * change inside the loop
515 int run
= buf_cnt
- 2;
522 if (character
== '#')
524 /* Danger! character can be '#' when esc is
525 * used so we need an explicit boolean for done here.
531 if (character
== '}')
533 /* data transmitted in binary mode (X packet)
534 * uses 0x7d as escape character */
535 my_checksum
+= character
& 0xff;
538 my_checksum
+= character
& 0xff;
539 buffer
[count
++] = (character
^ 0x20) & 0xff;
543 my_checksum
+= character
& 0xff;
544 buffer
[count
++] = character
& 0xff;
554 LOG_ERROR("packet buffer too small");
555 retval
= ERROR_GDB_BUFFER_TOO_SMALL
;
559 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
560 if (retval
!= ERROR_OK
)
563 if (character
== '#')
566 if (character
== '}')
568 /* data transmitted in binary mode (X packet)
569 * uses 0x7d as escape character */
570 my_checksum
+= character
& 0xff;
572 retval
= gdb_get_char_fast(connection
, &character
, &buf_p
, &buf_cnt
);
573 if (retval
!= ERROR_OK
)
576 my_checksum
+= character
& 0xff;
577 buffer
[count
++] = (character
^ 0x20) & 0xff;
581 my_checksum
+= character
& 0xff;
582 buffer
[count
++] = character
& 0xff;
586 gdb_con
->buf_p
= buf_p
;
587 gdb_con
->buf_cnt
= buf_cnt
;
589 if (retval
!= ERROR_OK
)
594 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
596 checksum
[0] = character
;
597 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
599 checksum
[1] = character
;
604 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
610 int gdb_get_packet_inner(struct connection
*connection
, char *buffer
, int *len
)
614 struct gdb_connection
*gdb_con
= connection
->priv
;
620 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
623 #ifdef _DEBUG_GDB_IO_
624 LOG_DEBUG("character: '%c'", character
);
632 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
633 * in case anyone tries to debug why they receive this warning every time */
634 LOG_WARNING("acknowledgment received, but no packet pending");
637 LOG_WARNING("negative acknowledgment, but no packet pending");
644 LOG_WARNING("ignoring character 0x%x", character
);
647 } while (character
!= '$');
652 /* explicit code expansion here to get faster inlined code in -O3 by not
653 * calculating checksum
655 if (gdb_con
->noack_mode
)
657 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
661 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
665 if (gdb_con
->noack_mode
)
667 /* checksum is not checked in noack mode */
672 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
680 return ERROR_SERVER_REMOTE_CLOSED
;
685 int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
687 struct gdb_connection
*gdb_con
= connection
->priv
;
689 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
694 int gdb_output_con(struct connection
*connection
, const char* line
)
699 bin_size
= strlen(line
);
701 hex_buffer
= malloc(bin_size
*2 + 2);
702 if (hex_buffer
== NULL
)
703 return ERROR_GDB_BUFFER_TOO_SMALL
;
706 for (i
= 0; i
< bin_size
; i
++)
707 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
708 hex_buffer
[bin_size
*2 + 1] = 0;
710 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
716 int gdb_output(struct command_context
*context
, const char* line
)
718 /* this will be dumped to the log and also sent as an O packet if possible */
719 LOG_USER_N("%s", line
);
724 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
726 struct gdb_connection
*gdb_connection
= connection
->priv
;
728 /* In the GDB protocol when we are stepping or continuing execution,
729 * we have a lingering reply. Upon receiving a halted event
730 * when we have that lingering packet, we reply to the original
731 * step or continue packet.
733 * Executing monitor commands can bring the target in and
734 * out of the running state so we'll see lots of TARGET_EVENT_XXX
735 * that are to be ignored.
737 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
742 /* stop forwarding log packets! */
743 log_remove_callback(gdb_log_callback
, connection
);
745 if (gdb_connection
->ctrl_c
)
748 gdb_connection
->ctrl_c
= 0;
752 signal
= gdb_last_signal(target
);
755 snprintf(sig_reply
, 4, "T%2.2x", signal
);
756 gdb_put_packet(connection
, sig_reply
, 3);
757 gdb_connection
->frontend_state
= TARGET_HALTED
;
761 int gdb_target_callback_event_handler(struct target
*target
, enum target_event event
, void *priv
)
764 struct connection
*connection
= priv
;
766 target_handle_event(target
, event
);
769 case TARGET_EVENT_GDB_HALT
:
770 gdb_frontend_halted(target
, connection
);
772 case TARGET_EVENT_HALTED
:
773 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
775 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
776 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
777 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
789 int gdb_new_connection(struct connection
*connection
)
791 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
792 struct gdb_service
*gdb_service
= connection
->service
->priv
;
796 connection
->priv
= gdb_connection
;
798 /* initialize gdb connection information */
799 gdb_connection
->buf_p
= gdb_connection
->buffer
;
800 gdb_connection
->buf_cnt
= 0;
801 gdb_connection
->ctrl_c
= 0;
802 gdb_connection
->frontend_state
= TARGET_HALTED
;
803 gdb_connection
->vflash_image
= NULL
;
804 gdb_connection
->closed
= 0;
805 gdb_connection
->busy
= 0;
806 gdb_connection
->noack_mode
= 0;
807 gdb_connection
->sync
= true;
809 /* send ACK to GDB for debug request */
810 gdb_write(connection
, "+", 1);
812 /* output goes through gdb connection */
813 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
815 /* we must remove all breakpoints registered to the target as a previous
816 * GDB session could leave dangling breakpoints if e.g. communication
819 breakpoint_clear_target(gdb_service
->target
);
820 watchpoint_clear_target(gdb_service
->target
);
822 /* register callback to be informed about target events */
823 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
825 /* remove the initial ACK from the incoming buffer */
826 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
829 /* FIX!!!??? would we actually ever receive a + here???
832 if (initial_ack
!= '+')
833 gdb_putback_char(connection
, initial_ack
);
834 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
836 gdb_actual_connections
++;
837 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
838 gdb_actual_connections
,
839 target_name(gdb_service
->target
),
840 target_state_name(gdb_service
->target
));
845 int gdb_connection_closed(struct connection
*connection
)
847 struct gdb_service
*gdb_service
= connection
->service
->priv
;
848 struct gdb_connection
*gdb_connection
= connection
->priv
;
850 /* we're done forwarding messages. Tear down callback before
851 * cleaning up connection.
853 log_remove_callback(gdb_log_callback
, connection
);
855 gdb_actual_connections
--;
856 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
857 target_name(gdb_service
->target
),
858 target_state_name(gdb_service
->target
),
859 gdb_actual_connections
);
861 /* see if an image built with vFlash commands is left */
862 if (gdb_connection
->vflash_image
)
864 image_close(gdb_connection
->vflash_image
);
865 free(gdb_connection
->vflash_image
);
866 gdb_connection
->vflash_image
= NULL
;
869 /* if this connection registered a debug-message receiver delete it */
870 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
872 if (connection
->priv
)
874 free(connection
->priv
);
875 connection
->priv
= NULL
;
879 LOG_ERROR("BUG: connection->priv == NULL");
883 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
885 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
887 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
892 void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
895 snprintf(err
, 4, "E%2.2X", the_error
);
896 gdb_put_packet(connection
, err
, 3);
899 int gdb_last_signal_packet(struct connection
*connection
, struct target
*target
, char* packet
, int packet_size
)
904 signal
= gdb_last_signal(target
);
906 snprintf(sig_reply
, 4, "S%2.2x", signal
);
907 gdb_put_packet(connection
, sig_reply
, 3);
912 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
914 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
917 return len
- 1 - pos
;
920 /* Convert register to string of bytes. NB! The # of bits in the
921 * register might be non-divisible by 8(a byte), in which
922 * case an entire byte is shown.
924 * NB! the format on the wire is the target endianness
926 * The format of reg->value is little endian
929 void gdb_str_to_target(struct target
*target
, char *tstr
, struct reg
*reg
)
936 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
938 for (i
= 0; i
< buf_len
; i
++)
940 int j
= gdb_reg_pos(target
, i
, buf_len
);
941 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
942 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
946 static int hextoint(int c
)
957 LOG_ERROR("BUG: invalid register value %08x", c
);
961 /* copy over in register buffer */
962 void gdb_target_to_reg(struct target
*target
, char *tstr
, int str_len
, uint8_t *bin
)
966 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
971 for (i
= 0; i
< str_len
; i
+= 2)
973 uint8_t t
= hextoint(tstr
[i
]) << 4;
974 t
|= hextoint(tstr
[i
+ 1]);
976 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
981 int gdb_get_registers_packet(struct connection
*connection
, struct target
*target
, char* packet
, int packet_size
)
983 struct reg
**reg_list
;
986 int reg_packet_size
= 0;
991 #ifdef _DEBUG_GDB_IO_
995 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
997 return gdb_error(connection
, retval
);
1000 for (i
= 0; i
< reg_list_size
; i
++)
1002 reg_packet_size
+= reg_list
[i
]->size
;
1005 reg_packet
= malloc(DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1006 reg_packet_p
= reg_packet
;
1008 for (i
= 0; i
< reg_list_size
; i
++)
1010 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
1011 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
1014 #ifdef _DEBUG_GDB_IO_
1017 reg_packet_p
= strndup(reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1018 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
1023 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
1031 int gdb_set_registers_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1034 struct reg
**reg_list
;
1039 #ifdef _DEBUG_GDB_IO_
1043 /* skip command character */
1047 if (packet_size
% 2)
1049 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1050 return ERROR_SERVER_REMOTE_CLOSED
;
1053 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1055 return gdb_error(connection
, retval
);
1059 for (i
= 0; i
< reg_list_size
; i
++)
1062 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1064 if (packet_p
+ chars
> packet
+ packet_size
)
1066 LOG_ERROR("BUG: register packet is too small for registers");
1069 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1070 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1072 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1074 /* advance packet pointer */
1081 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1084 gdb_put_packet(connection
, "OK", 2);
1089 int gdb_get_register_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1092 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1093 struct reg
**reg_list
;
1097 #ifdef _DEBUG_GDB_IO_
1101 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1103 return gdb_error(connection
, retval
);
1106 if (reg_list_size
<= reg_num
)
1108 LOG_ERROR("gdb requested a non-existing register");
1112 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1114 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1116 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1124 int gdb_set_register_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1128 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1129 struct reg
**reg_list
;
1135 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1137 return gdb_error(connection
, retval
);
1140 if (reg_list_size
< reg_num
)
1142 LOG_ERROR("gdb requested a non-existing register");
1143 return ERROR_SERVER_REMOTE_CLOSED
;
1146 if (*separator
!= '=')
1148 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1149 return ERROR_SERVER_REMOTE_CLOSED
;
1152 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1153 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1154 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1156 /* fix!!! add some sanity checks on packet size here */
1158 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1160 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1162 gdb_put_packet(connection
, "OK", 2);
1170 int gdb_error(struct connection
*connection
, int retval
)
1174 case ERROR_TARGET_DATA_ABORT
:
1175 gdb_send_error(connection
, EIO
);
1177 case ERROR_TARGET_TRANSLATION_FAULT
:
1178 gdb_send_error(connection
, EFAULT
);
1180 case ERROR_TARGET_UNALIGNED_ACCESS
:
1181 gdb_send_error(connection
, EFAULT
);
1183 case ERROR_TARGET_NOT_HALTED
:
1184 gdb_send_error(connection
, EFAULT
);
1187 /* This could be that the target reset itself. */
1188 LOG_ERROR("unexpected error %i", retval
);
1189 gdb_send_error(connection
, EFAULT
);
1196 /* We don't have to worry about the default 2 second timeout for GDB packets,
1197 * because GDB breaks up large memory reads into smaller reads.
1199 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1201 int gdb_read_memory_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1210 int retval
= ERROR_OK
;
1212 /* skip command character */
1215 addr
= strtoul(packet
, &separator
, 16);
1217 if (*separator
!= ',')
1219 LOG_ERROR("incomplete read memory packet received, dropping connection");
1220 return ERROR_SERVER_REMOTE_CLOSED
;
1223 len
= strtoul(separator
+ 1, NULL
, 16);
1225 buffer
= malloc(len
);
1227 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1229 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1231 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1233 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1234 * At some point this might be fixed in GDB, in which case this code can be removed.
1236 * OpenOCD developers are acutely aware of this problem, but there is nothing
1237 * gained by involving the user in this problem that hopefully will get resolved
1240 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1242 * For now, the default is to fix up things to make current GDB versions work.
1243 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1245 memset(buffer
, 0, len
);
1249 if (retval
== ERROR_OK
)
1251 hex_buffer
= malloc(len
* 2 + 1);
1254 for (i
= 0; i
< len
; i
++)
1256 uint8_t t
= buffer
[i
];
1257 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1258 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1261 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1267 retval
= gdb_error(connection
, retval
);
1275 int gdb_write_memory_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1286 /* skip command character */
1289 addr
= strtoul(packet
, &separator
, 16);
1291 if (*separator
!= ',')
1293 LOG_ERROR("incomplete write memory packet received, dropping connection");
1294 return ERROR_SERVER_REMOTE_CLOSED
;
1297 len
= strtoul(separator
+ 1, &separator
, 16);
1299 if (*(separator
++) != ':')
1301 LOG_ERROR("incomplete write memory packet received, dropping connection");
1302 return ERROR_SERVER_REMOTE_CLOSED
;
1305 buffer
= malloc(len
);
1307 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1309 for (i
= 0; i
< len
; i
++)
1312 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1316 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1318 if (retval
== ERROR_OK
)
1320 gdb_put_packet(connection
, "OK", 2);
1324 retval
= gdb_error(connection
, retval
);
1332 int gdb_write_memory_binary_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1340 /* skip command character */
1343 addr
= strtoul(packet
, &separator
, 16);
1345 if (*separator
!= ',')
1347 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1348 return ERROR_SERVER_REMOTE_CLOSED
;
1351 len
= strtoul(separator
+ 1, &separator
, 16);
1353 if (*(separator
++) != ':')
1355 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1356 return ERROR_SERVER_REMOTE_CLOSED
;
1362 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1364 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1367 if (retval
== ERROR_OK
)
1369 gdb_put_packet(connection
, "OK", 2);
1373 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1380 int gdb_step_continue_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1383 uint32_t address
= 0x0;
1384 int retval
= ERROR_OK
;
1388 if (packet_size
> 1)
1390 packet
[packet_size
] = 0;
1391 address
= strtoul(packet
+ 1, NULL
, 16);
1398 if (packet
[0] == 'c')
1400 LOG_DEBUG("continue");
1401 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1402 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1404 else if (packet
[0] == 's')
1407 /* step at current or address, don't handle breakpoints */
1408 retval
= target_step(target
, current
, address
, 0);
1413 int gdb_breakpoint_watchpoint_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1416 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1417 enum watchpoint_rw wp_type
;
1425 type
= strtoul(packet
+ 1, &separator
, 16);
1427 if (type
== 0) /* memory breakpoint */
1428 bp_type
= BKPT_SOFT
;
1429 else if (type
== 1) /* hardware breakpoint */
1430 bp_type
= BKPT_HARD
;
1431 else if (type
== 2) /* write watchpoint */
1432 wp_type
= WPT_WRITE
;
1433 else if (type
== 3) /* read watchpoint */
1435 else if (type
== 4) /* access watchpoint */
1436 wp_type
= WPT_ACCESS
;
1438 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1440 bp_type
= gdb_breakpoint_override_type
;
1443 if (*separator
!= ',')
1445 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1446 return ERROR_SERVER_REMOTE_CLOSED
;
1449 address
= strtoul(separator
+ 1, &separator
, 16);
1451 if (*separator
!= ',')
1453 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1454 return ERROR_SERVER_REMOTE_CLOSED
;
1457 size
= strtoul(separator
+ 1, &separator
, 16);
1463 if (packet
[0] == 'Z')
1465 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1467 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1472 gdb_put_packet(connection
, "OK", 2);
1477 breakpoint_remove(target
, address
);
1478 gdb_put_packet(connection
, "OK", 2);
1485 if (packet
[0] == 'Z')
1487 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1489 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1494 gdb_put_packet(connection
, "OK", 2);
1499 watchpoint_remove(target
, address
);
1500 gdb_put_packet(connection
, "OK", 2);
1511 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1512 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1514 if (*retval
!= ERROR_OK
)
1522 if ((*xml
== NULL
) || (!first
))
1524 /* start by 0 to exercise all the code paths.
1525 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1527 *size
= *size
* 2 + 2;
1529 *xml
= realloc(*xml
, *size
);
1534 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1542 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1544 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1549 /* there was just enough or not enough space, allocate more. */
1554 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1558 /* Extract and NUL-terminate the annex. */
1560 while (*buf
&& *buf
!= ':')
1566 /* After the read marker and annex, qXfer looks like a
1567 * traditional 'm' packet. */
1569 *ofs
= strtoul(buf
, &separator
, 16);
1571 if (*separator
!= ',')
1574 *len
= strtoul(separator
+ 1, NULL
, 16);
1579 int gdb_calc_blocksize(struct flash_bank
*bank
)
1582 uint32_t block_size
= 0xffffffff;
1584 /* loop through all sectors and return smallest sector size */
1586 for (i
= 0; i
< (uint32_t)bank
->num_sectors
; i
++)
1588 if (bank
->sectors
[i
].size
< block_size
)
1589 block_size
= bank
->sectors
[i
].size
;
1595 static int compare_bank (const void * a
, const void * b
)
1597 struct flash_bank
*b1
, *b2
;
1598 b1
=*((struct flash_bank
**)a
);
1599 b2
=*((struct flash_bank
**)b
);
1601 if (b1
->base
== b2
->base
)
1604 } else if (b1
->base
> b2
->base
)
1613 int gdb_query_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1615 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1616 struct gdb_connection
*gdb_connection
= connection
->priv
;
1618 if (strstr(packet
, "qRcmd,"))
1620 if (packet_size
> 6)
1624 cmd
= malloc((packet_size
- 6)/2 + 1);
1625 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1628 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1631 cmd
[(packet_size
- 6)/2] = 0x0;
1633 /* We want to print all debug output to GDB connection */
1634 log_add_callback(gdb_log_callback
, connection
);
1635 target_call_timer_callbacks_now();
1636 /* some commands need to know the GDB connection, make note of current
1637 * GDB connection. */
1638 current_gdb_connection
= gdb_connection
;
1639 command_run_line(cmd_ctx
, cmd
);
1640 current_gdb_connection
= NULL
;
1641 target_call_timer_callbacks_now();
1642 log_remove_callback(gdb_log_callback
, connection
);
1645 gdb_put_packet(connection
, "OK", 2);
1648 else if (strstr(packet
, "qCRC:"))
1650 if (packet_size
> 5)
1659 /* skip command character */
1662 addr
= strtoul(packet
, &separator
, 16);
1664 if (*separator
!= ',')
1666 LOG_ERROR("incomplete read memory packet received, dropping connection");
1667 return ERROR_SERVER_REMOTE_CLOSED
;
1670 len
= strtoul(separator
+ 1, NULL
, 16);
1672 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1674 if (retval
== ERROR_OK
)
1676 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1677 gdb_put_packet(connection
, gdb_reply
, 9);
1681 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1688 else if (strstr(packet
, "qSupported"))
1690 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1691 * disable qXfer:features:read for the moment */
1692 int retval
= ERROR_OK
;
1693 char *buffer
= NULL
;
1697 xml_printf(&retval
, &buffer
, &pos
, &size
,
1698 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1699 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1701 if (retval
!= ERROR_OK
)
1703 gdb_send_error(connection
, 01);
1707 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1712 else if (strstr(packet
, "qXfer:memory-map:read::") && (flash_get_bank_count() > 0))
1714 /* We get away with only specifying flash here. Regions that are not
1715 * specified are treated as if we provided no memory map(if not we
1716 * could detect the holes and mark them as RAM).
1717 * Normally we only execute this code once, but no big deal if we
1718 * have to regenerate it a couple of times. */
1720 struct flash_bank
*p
;
1724 int retval
= ERROR_OK
;
1731 /* skip command character */
1734 offset
= strtoul(packet
, &separator
, 16);
1735 length
= strtoul(separator
+ 1, &separator
, 16);
1737 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1740 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1741 read/write) by default for GDB.
1742 GDB does not have a concept of non-cacheable read/write memory.
1744 struct flash_bank
**banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1747 for (i
= 0; i
< flash_get_bank_count(); i
++)
1749 p
= get_flash_bank_by_num(i
);
1753 retval
= ERROR_FAIL
;
1754 gdb_send_error(connection
, retval
);
1760 qsort(banks
, flash_get_bank_count(), sizeof(struct flash_bank
*), compare_bank
);
1762 uint32_t ram_start
= 0;
1763 for (i
= 0; i
< flash_get_bank_count(); i
++)
1767 if (ram_start
< p
->base
)
1769 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1770 ram_start
, p
->base
-ram_start
);
1773 /* if device has uneven sector sizes, eg. str7, lpc
1774 * we pass the smallest sector size to gdb memory map */
1775 blocksize
= gdb_calc_blocksize(p
);
1777 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1778 "<property name=\"blocksize\">0x%x</property>\n" \
1780 p
->base
, p
->size
, blocksize
);
1781 ram_start
= p
->base
+ p
->size
;
1785 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1786 ram_start
, 0-ram_start
);
1789 /* a flash chip could be at the very end of the 32 bit address space, in which case
1790 ram_start will be precisely 0 */
1796 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1798 if (retval
!= ERROR_OK
)
1800 gdb_send_error(connection
, retval
);
1804 if (offset
+ length
> pos
)
1806 length
= pos
- offset
;
1809 char *t
= malloc(length
+ 1);
1811 memcpy(t
+ 1, xml
+ offset
, length
);
1812 gdb_put_packet(connection
, t
, length
+ 1);
1818 else if (strstr(packet
, "qXfer:features:read:"))
1823 int retval
= ERROR_OK
;
1826 unsigned int length
;
1829 /* skip command character */
1832 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1834 gdb_send_error(connection
, 01);
1838 if (strcmp(annex
, "target.xml") != 0)
1840 gdb_send_error(connection
, 01);
1844 xml_printf(&retval
, &xml
, &pos
, &size
, \
1845 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1847 if (retval
!= ERROR_OK
)
1849 gdb_send_error(connection
, retval
);
1853 gdb_put_packet(connection
, xml
, strlen(xml
));
1858 else if (strstr(packet
, "QStartNoAckMode"))
1860 gdb_connection
->noack_mode
= 1;
1861 gdb_put_packet(connection
, "OK", 2);
1865 gdb_put_packet(connection
, "", 0);
1869 int gdb_v_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1871 struct gdb_connection
*gdb_connection
= connection
->priv
;
1872 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1875 /* if flash programming disabled - send a empty reply */
1877 if (gdb_flash_program
== 0)
1879 gdb_put_packet(connection
, "", 0);
1883 if (strstr(packet
, "vFlashErase:"))
1886 unsigned long length
;
1888 char *parse
= packet
+ 12;
1891 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1892 return ERROR_SERVER_REMOTE_CLOSED
;
1895 addr
= strtoul(parse
, &parse
, 16);
1897 if (*(parse
++) != ',' || *parse
== '\0')
1899 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1900 return ERROR_SERVER_REMOTE_CLOSED
;
1903 length
= strtoul(parse
, &parse
, 16);
1907 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1908 return ERROR_SERVER_REMOTE_CLOSED
;
1911 /* assume all sectors need erasing - stops any problems
1912 * when flash_write is called multiple times */
1915 /* perform any target specific operations before the erase */
1916 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_START
);
1917 result
= flash_erase_address_range(gdb_service
->target
, addr
, length
);
1918 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_END
);
1921 if (result
!= ERROR_OK
)
1923 /* GDB doesn't evaluate the actual error number returned,
1924 * treat a failed erase as an I/O error
1926 gdb_send_error(connection
, EIO
);
1927 LOG_ERROR("flash_erase returned %i", result
);
1930 gdb_put_packet(connection
, "OK", 2);
1935 if (strstr(packet
, "vFlashWrite:"))
1939 unsigned long length
;
1940 char *parse
= packet
+ 12;
1944 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1945 return ERROR_SERVER_REMOTE_CLOSED
;
1947 addr
= strtoul(parse
, &parse
, 16);
1948 if (*(parse
++) != ':')
1950 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1951 return ERROR_SERVER_REMOTE_CLOSED
;
1953 length
= packet_size
- (parse
- packet
);
1955 /* create a new image if there isn't already one */
1956 if (gdb_connection
->vflash_image
== NULL
)
1958 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
1959 image_open(gdb_connection
->vflash_image
, "", "build");
1962 /* create new section with content from packet buffer */
1963 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
1968 gdb_put_packet(connection
, "OK", 2);
1973 if (!strcmp(packet
, "vFlashDone"))
1977 /* process the flashing buffer. No need to erase as GDB
1978 * always issues a vFlashErase first. */
1979 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
1980 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1981 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1982 if (result
!= ERROR_OK
)
1984 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1985 gdb_put_packet(connection
, "E.memtype", 9);
1987 gdb_send_error(connection
, EIO
);
1991 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
1992 gdb_put_packet(connection
, "OK", 2);
1995 image_close(gdb_connection
->vflash_image
);
1996 free(gdb_connection
->vflash_image
);
1997 gdb_connection
->vflash_image
= NULL
;
2002 gdb_put_packet(connection
, "", 0);
2006 int gdb_detach(struct connection
*connection
, struct target
*target
)
2008 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2010 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
2012 return gdb_put_packet(connection
, "OK", 2);
2015 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
2016 const char *function
, const char *string
)
2018 struct connection
*connection
= priv
;
2019 struct gdb_connection
*gdb_con
= connection
->priv
;
2023 /* do not reply this using the O packet */
2027 gdb_output_con(connection
, string
);
2030 /* Do not allocate this on the stack */
2031 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2033 static void gdb_sig_halted(struct connection
*connection
)
2036 snprintf(sig_reply
, 4, "T%2.2x", 2);
2037 gdb_put_packet(connection
, sig_reply
, 3);
2041 int gdb_input_inner(struct connection
*connection
)
2043 struct gdb_service
*gdb_service
= connection
->service
->priv
;
2044 struct target
*target
= gdb_service
->target
;
2045 char *packet
= gdb_packet_buffer
;
2048 struct gdb_connection
*gdb_con
= connection
->priv
;
2049 static int extended_protocol
= 0;
2051 /* drain input buffer */
2054 packet_size
= GDB_BUFFER_SIZE
-1;
2055 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
2060 /* terminate with zero */
2061 packet
[packet_size
] = 0;
2063 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2064 if (packet
[0] == 'X') {
2065 // binary packets spew junk into the debug log stream
2068 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2072 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2074 LOG_DEBUG("received packet: '%s'", packet
);
2078 if (packet_size
> 0)
2084 /* Hct... -- set thread
2085 * we don't have threads, send empty reply */
2086 gdb_put_packet(connection
, NULL
, 0);
2090 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
2093 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
2096 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
2099 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
2102 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
2105 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
2108 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
2112 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2115 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
2120 int retval
= ERROR_OK
;
2122 struct gdb_connection
*gdb_con
= connection
->priv
;
2123 log_add_callback(gdb_log_callback
, connection
);
2125 bool nostep
= false;
2126 if (target
->state
== TARGET_RUNNING
)
2128 LOG_WARNING("The target is already running. Halt target before stepi/continue.");
2129 retval
= target_halt(target
);
2130 if (retval
== ERROR_OK
)
2131 retval
= target_wait_state(target
, TARGET_HALTED
, 100);
2132 } else if (target
->state
!= TARGET_HALTED
)
2134 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2136 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2138 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2139 * sent by GDB first to OpenOCD, thus defeating the check to
2140 * make only the single stepping have the sync feature...
2143 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2145 gdb_con
->sync
= false;
2147 if ((retval
!=ERROR_OK
) || nostep
)
2149 /* Either the target isn't in the halted state, then we can't
2150 * step/continue. This might be early setup, etc.
2152 * Or we want to allow GDB to pick up a fresh set of
2153 * register values without modifying the target state.
2156 gdb_sig_halted(connection
);
2158 /* stop forwarding log packets! */
2159 log_remove_callback(gdb_log_callback
, connection
);
2162 /* We're running/stepping, in which case we can
2163 * forward log output until the target is halted
2165 gdb_con
->frontend_state
= TARGET_RUNNING
;
2166 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2167 int retval
= gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2168 if (retval
!= ERROR_OK
)
2170 /* we'll never receive a halted condition... issue a false one.. */
2171 gdb_frontend_halted(target
, connection
);
2177 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
2180 retval
= gdb_detach(connection
, target
);
2181 extended_protocol
= 0;
2184 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
2188 if (extended_protocol
!= 0)
2190 gdb_put_packet(connection
, "OK", 2);
2191 return ERROR_SERVER_REMOTE_CLOSED
;
2193 /* handle extended remote protocol */
2194 extended_protocol
= 1;
2195 gdb_put_packet(connection
, "OK", 2);
2198 /* handle extended restart packet */
2199 breakpoint_clear_target(gdb_service
->target
);
2200 watchpoint_clear_target(gdb_service
->target
);
2201 command_run_linef(connection
->cmd_ctx
,
2202 "ocd_gdb_restart %s",
2203 target_name(target
));
2206 /* ignore unknown packets */
2207 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2208 gdb_put_packet(connection
, NULL
, 0);
2212 /* if a packet handler returned an error, exit input loop */
2213 if (retval
!= ERROR_OK
)
2217 if (gdb_con
->ctrl_c
)
2219 if (target
->state
== TARGET_RUNNING
)
2221 retval
= target_halt(target
);
2222 if (retval
!= ERROR_OK
)
2224 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2226 gdb_con
->ctrl_c
= 0;
2229 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2230 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2234 } while (gdb_con
->buf_cnt
> 0);
2239 int gdb_input(struct connection
*connection
)
2241 int retval
= gdb_input_inner(connection
);
2242 struct gdb_connection
*gdb_con
= connection
->priv
;
2243 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2246 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2247 if (gdb_con
->closed
)
2248 return ERROR_SERVER_REMOTE_CLOSED
;
2250 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2254 static int gdb_target_start(struct target
*target
, uint16_t port
)
2256 bool use_pipes
= 0 == port
;
2257 struct gdb_service
*gdb_service
= malloc(sizeof(struct gdb_service
));
2258 if (NULL
== gdb_service
)
2261 gdb_service
->target
= target
;
2263 add_service("gdb", use_pipes
? CONNECTION_PIPE
: CONNECTION_TCP
,
2264 port
, 1, &gdb_new_connection
, &gdb_input
,
2265 &gdb_connection_closed
, gdb_service
);
2267 const char *name
= target_name(target
);
2269 LOG_DEBUG("gdb service for target '%s' using pipes", name
);
2271 LOG_DEBUG("gdb service for target '%s' on TCP port %u", name
, port
);
2276 int gdb_target_add_one(struct target
*target
)
2278 if (gdb_port
== 0 && server_use_pipes
== 0)
2280 LOG_INFO("gdb port disabled");
2283 if (0 == gdb_port_next
)
2284 gdb_port_next
= gdb_port
;
2286 bool use_pipes
= server_use_pipes
;
2287 static bool server_started_with_pipes
= false;
2288 if (server_started_with_pipes
)
2290 LOG_WARNING("gdb service permits one target when using pipes");
2297 int e
= gdb_target_start(target
, use_pipes
? 0 : gdb_port_next
);
2300 server_started_with_pipes
|= use_pipes
;
2306 int gdb_target_add_all(struct target
*target
)
2310 LOG_WARNING("gdb services need one or more targets defined");
2314 while (NULL
!= target
)
2316 int retval
= gdb_target_add_one(target
);
2317 if (ERROR_OK
!= retval
)
2320 target
= target
->next
;
2326 COMMAND_HANDLER(handle_gdb_sync_command
)
2330 return ERROR_COMMAND_SYNTAX_ERROR
;
2333 if (current_gdb_connection
== NULL
)
2335 command_print(CMD_CTX
,
2336 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2340 current_gdb_connection
->sync
= true;
2345 /* daemon configuration command gdb_port */
2346 COMMAND_HANDLER(handle_gdb_port_command
)
2348 int retval
= CALL_COMMAND_HANDLER(server_port_command
, &gdb_port
);
2349 if (ERROR_OK
== retval
)
2350 gdb_port_next
= gdb_port
;
2354 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2357 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2359 return ERROR_COMMAND_SYNTAX_ERROR
;
2362 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2365 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2367 return ERROR_COMMAND_SYNTAX_ERROR
;
2370 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2373 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2375 return ERROR_COMMAND_SYNTAX_ERROR
;
2378 /* gdb_breakpoint_override */
2379 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2384 } else if (CMD_ARGC
== 1)
2386 gdb_breakpoint_override
= 1;
2387 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2389 gdb_breakpoint_override_type
= BKPT_HARD
;
2390 } else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2392 gdb_breakpoint_override_type
= BKPT_SOFT
;
2393 } else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2395 gdb_breakpoint_override
= 0;
2399 return ERROR_COMMAND_SYNTAX_ERROR
;
2401 if (gdb_breakpoint_override
)
2403 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2406 LOG_USER("breakpoint type is not overridden");
2412 static const struct command_registration gdb_command_handlers
[] = {
2415 .handler
= &handle_gdb_sync_command
,
2416 .mode
= COMMAND_ANY
,
2417 .help
= "next stepi will return immediately allowing "
2418 "GDB to fetch register state without affecting "
2423 .handler
= &handle_gdb_port_command
,
2424 .mode
= COMMAND_ANY
,
2425 .help
= "daemon configuration command gdb_port. No arguments reports "
2430 .name
= "gdb_memory_map",
2431 .handler
= &handle_gdb_memory_map_command
,
2432 .mode
= COMMAND_CONFIG
,
2433 .help
= "enable or disable memory map",
2434 .usage
= "enable|disable"
2437 .name
= "gdb_flash_program",
2438 .handler
= &handle_gdb_flash_program_command
,
2439 .mode
= COMMAND_CONFIG
,
2440 .help
= "enable or disable flash program",
2441 .usage
= "enable|disable"
2444 .name
= "gdb_report_data_abort",
2445 .handler
= &handle_gdb_report_data_abort_command
,
2446 .mode
= COMMAND_CONFIG
,
2447 .help
= "enable or disable reporting data aborts",
2448 .usage
= "enable|disable"
2451 .name
= "gdb_breakpoint_override",
2452 .handler
= &handle_gdb_breakpoint_override_command
,
2453 .mode
= COMMAND_EXEC
,
2454 .help
= "force type of breakpoint "
2455 "used by gdb 'break' commands.",
2456 .usage
= "hard|soft|disable",
2458 COMMAND_REGISTRATION_DONE
2461 int gdb_register_commands(struct command_context
*cmd_ctx
)
2463 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);