1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
30 #include <target/breakpoints.h>
31 #include <target/target_request.h>
32 #include <target/register.h>
34 #include <flash/flash.h>
35 #include "gdb_server.h"
36 #include <target/image.h>
37 #include <jtag/jtag.h>
41 #define _DEBUG_GDB_IO_
44 static struct gdb_connection
*current_gdb_connection
;
46 static int gdb_breakpoint_override
;
47 static enum breakpoint_type gdb_breakpoint_override_type
;
49 extern int gdb_error(struct connection
*connection
, int retval
);
50 static unsigned short gdb_port
= 3333;
51 static unsigned short gdb_port_next
= 0;
52 static const char *DIGITS
= "0123456789abcdef";
54 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
55 const char *function
, const char *string
);
57 /* number of gdb connections, mainly to suppress gdb related debugging spam
58 * in helper/log.c when no gdb connections are actually active */
59 int gdb_actual_connections
;
61 /* set if we are sending a memory map to gdb
62 * via qXfer:memory-map:read packet */
63 /* enabled by default*/
64 int gdb_use_memory_map
= 1;
65 /* enabled by default*/
66 int gdb_flash_program
= 1;
68 /* if set, data aborts cause an error to be reported in memory read packets
69 * see the code in gdb_read_memory_packet() for further explanations */
70 int gdb_report_data_abort
= 0;
72 int gdb_last_signal(struct target
*target
)
74 switch (target
->debug_reason
)
76 case DBG_REASON_DBGRQ
:
77 return 0x2; /* SIGINT */
78 case DBG_REASON_BREAKPOINT
:
79 case DBG_REASON_WATCHPOINT
:
80 case DBG_REASON_WPTANDBKPT
:
81 return 0x05; /* SIGTRAP */
82 case DBG_REASON_SINGLESTEP
:
83 return 0x05; /* SIGTRAP */
84 case DBG_REASON_NOTHALTED
:
85 return 0x0; /* no signal... shouldn't happen */
87 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
92 int check_pending(struct connection
*connection
, int timeout_s
, int *got_data
)
94 /* a non-blocking socket will block if there is 0 bytes available on the socket,
95 * but return with as many bytes as are available immediately
99 struct gdb_connection
*gdb_con
= connection
->priv
;
101 if (got_data
== NULL
)
105 if (gdb_con
->buf_cnt
> 0)
112 FD_SET(connection
->fd
, &read_fds
);
114 tv
.tv_sec
= timeout_s
;
116 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
118 /* This can typically be because a "monitor" command took too long
119 * before printing any progress messages
123 return ERROR_GDB_TIMEOUT
;
129 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
133 int gdb_get_char(struct connection
*connection
, int* next_char
)
135 struct gdb_connection
*gdb_con
= connection
->priv
;
136 int retval
= ERROR_OK
;
138 #ifdef _DEBUG_GDB_IO_
142 if (gdb_con
->buf_cnt
-- > 0)
144 *next_char
= *(gdb_con
->buf_p
++);
145 if (gdb_con
->buf_cnt
> 0)
146 connection
->input_pending
= 1;
148 connection
->input_pending
= 0;
150 #ifdef _DEBUG_GDB_IO_
151 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
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
);
241 int gdb_putback_char(struct connection
*connection
, int last_char
)
243 struct gdb_connection
*gdb_con
= connection
->priv
;
245 if (gdb_con
->buf_p
> gdb_con
->buffer
)
247 *(--gdb_con
->buf_p
) = last_char
;
252 LOG_ERROR("BUG: couldn't put character back");
258 /* The only way we can detect that the socket is closed is the first time
259 * we write to it, we will fail. Subsequent write operations will
260 * succeed. Shudder! */
261 int gdb_write(struct connection
*connection
, void *data
, int len
)
263 struct gdb_connection
*gdb_con
= connection
->priv
;
265 return ERROR_SERVER_REMOTE_CLOSED
;
267 if (connection
->service
->type
== CONNECTION_PIPE
)
269 /* write to stdout */
270 if (write(STDOUT_FILENO
, data
, len
) == len
)
277 if (write_socket(connection
->fd
, data
, len
) == len
)
283 return ERROR_SERVER_REMOTE_CLOSED
;
286 int gdb_put_packet_inner(struct connection
*connection
, char *buffer
, int len
)
289 unsigned char my_checksum
= 0;
290 #ifdef _DEBUG_GDB_IO_
295 struct gdb_connection
*gdb_con
= connection
->priv
;
297 for (i
= 0; i
< len
; i
++)
298 my_checksum
+= buffer
[i
];
300 #ifdef _DEBUG_GDB_IO_
302 * At this point we should have nothing in the input queue from GDB,
303 * however sometimes '-' is sent even though we've already received
304 * an ACK (+) for everything we've sent off.
309 if ((retval
= check_pending(connection
, 0, &gotdata
)) != ERROR_OK
)
313 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
316 /* fix a problem with some IAR tools */
317 gdb_putback_char(connection
, reply
);
318 LOG_DEBUG("Unexpected start of new packet");
322 LOG_WARNING("Discard unexpected char %c", reply
);
328 #ifdef _DEBUG_GDB_IO_
329 debug_buffer
= malloc(len
+ 1);
330 memcpy(debug_buffer
, buffer
, len
);
331 debug_buffer
[len
] = 0;
332 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
336 char local_buffer
[1024];
337 local_buffer
[0] = '$';
338 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
340 /* performance gain on smaller packets by only a single call to gdb_write() */
341 memcpy(local_buffer
+ 1, buffer
, len
++);
342 local_buffer
[len
++] = '#';
343 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
344 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
345 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
352 /* larger packets are transmitted directly from caller supplied buffer
353 by several calls to gdb_write() to avoid dynamic allocation */
354 local_buffer
[1] = '#';
355 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
356 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
357 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
361 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
365 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
371 if (gdb_con
->noack_mode
)
374 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
379 else if (reply
== '-')
381 /* Stop sending output packets for now */
382 log_remove_callback(gdb_log_callback
, connection
);
383 LOG_WARNING("negative reply, retrying");
385 else if (reply
== 0x3)
388 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
392 else if (reply
== '-')
394 /* Stop sending output packets for now */
395 log_remove_callback(gdb_log_callback
, connection
);
396 LOG_WARNING("negative reply, retrying");
398 else if (reply
== '$') {
399 LOG_ERROR("GDB missing ack(1) - assumed good");
400 gdb_putback_char(connection
, reply
);
404 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
406 return ERROR_SERVER_REMOTE_CLOSED
;
409 else if (reply
== '$') {
410 LOG_ERROR("GDB missing ack(2) - assumed good");
411 gdb_putback_char(connection
, reply
);
416 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
418 return ERROR_SERVER_REMOTE_CLOSED
;
422 return ERROR_SERVER_REMOTE_CLOSED
;
427 int gdb_put_packet(struct connection
*connection
, char *buffer
, int len
)
429 struct gdb_connection
*gdb_con
= connection
->priv
;
431 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
434 /* we sent some data, reset timer for keep alive messages */
440 static __inline__
int fetch_packet(struct connection
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
442 unsigned char my_checksum
= 0;
447 struct gdb_connection
*gdb_con
= connection
->priv
;
453 /* The common case is that we have an entire packet with no escape chars.
454 * We need to leave at least 2 bytes in the buffer to have
455 * gdb_get_char() update various bits and bobs correctly.
457 if ((gdb_con
->buf_cnt
> 2) && ((gdb_con
->buf_cnt
+ count
) < *len
))
459 /* The compiler will struggle a bit with constant propagation and
460 * aliasing, so we help it by showing that these values do not
461 * change inside the loop
464 char *buf
= gdb_con
->buf_p
;
465 int run
= gdb_con
->buf_cnt
- 2;
472 if (character
== '#')
474 /* Danger! character can be '#' when esc is
475 * used so we need an explicit boolean for done here.
481 if (character
== '}')
483 /* data transmitted in binary mode (X packet)
484 * uses 0x7d as escape character */
485 my_checksum
+= character
& 0xff;
488 my_checksum
+= character
& 0xff;
489 buffer
[count
++] = (character
^ 0x20) & 0xff;
493 my_checksum
+= character
& 0xff;
494 buffer
[count
++] = character
& 0xff;
498 gdb_con
->buf_cnt
-= i
;
504 LOG_ERROR("packet buffer too small");
505 return ERROR_GDB_BUFFER_TOO_SMALL
;
508 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
511 if (character
== '#')
514 if (character
== '}')
516 /* data transmitted in binary mode (X packet)
517 * uses 0x7d as escape character */
518 my_checksum
+= character
& 0xff;
519 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
521 my_checksum
+= character
& 0xff;
522 buffer
[count
++] = (character
^ 0x20) & 0xff;
526 my_checksum
+= character
& 0xff;
527 buffer
[count
++] = character
& 0xff;
533 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
535 checksum
[0] = character
;
536 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
538 checksum
[1] = character
;
543 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
549 int gdb_get_packet_inner(struct connection
*connection
, char *buffer
, int *len
)
553 struct gdb_connection
*gdb_con
= connection
->priv
;
559 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
562 #ifdef _DEBUG_GDB_IO_
563 LOG_DEBUG("character: '%c'", character
);
571 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
572 * in case anyone tries to debug why they receive this warning every time */
573 LOG_WARNING("acknowledgment received, but no packet pending");
576 LOG_WARNING("negative acknowledgment, but no packet pending");
583 LOG_WARNING("ignoring character 0x%x", character
);
586 } while (character
!= '$');
591 /* explicit code expansion here to get faster inlined code in -O3 by not
592 * calculating checksum
594 if (gdb_con
->noack_mode
)
596 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
600 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
604 if (gdb_con
->noack_mode
)
606 /* checksum is not checked in noack mode */
611 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
619 return ERROR_SERVER_REMOTE_CLOSED
;
624 int gdb_get_packet(struct connection
*connection
, char *buffer
, int *len
)
626 struct gdb_connection
*gdb_con
= connection
->priv
;
628 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
633 int gdb_output_con(struct connection
*connection
, const char* line
)
638 bin_size
= strlen(line
);
640 hex_buffer
= malloc(bin_size
*2 + 2);
641 if (hex_buffer
== NULL
)
642 return ERROR_GDB_BUFFER_TOO_SMALL
;
645 for (i
= 0; i
< bin_size
; i
++)
646 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
647 hex_buffer
[bin_size
*2 + 1] = 0;
649 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
655 int gdb_output(struct command_context
*context
, const char* line
)
657 /* this will be dumped to the log and also sent as an O packet if possible */
658 LOG_USER_N("%s", line
);
663 static void gdb_frontend_halted(struct target
*target
, struct connection
*connection
)
665 struct gdb_connection
*gdb_connection
= connection
->priv
;
667 /* In the GDB protocol when we are stepping or continuing execution,
668 * we have a lingering reply. Upon receiving a halted event
669 * when we have that lingering packet, we reply to the original
670 * step or continue packet.
672 * Executing monitor commands can bring the target in and
673 * out of the running state so we'll see lots of TARGET_EVENT_XXX
674 * that are to be ignored.
676 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
681 /* stop forwarding log packets! */
682 log_remove_callback(gdb_log_callback
, connection
);
684 if (gdb_connection
->ctrl_c
)
687 gdb_connection
->ctrl_c
= 0;
691 signal
= gdb_last_signal(target
);
694 snprintf(sig_reply
, 4, "T%2.2x", signal
);
695 gdb_put_packet(connection
, sig_reply
, 3);
696 gdb_connection
->frontend_state
= TARGET_HALTED
;
700 int gdb_target_callback_event_handler(struct target
*target
, enum target_event event
, void *priv
)
703 struct connection
*connection
= priv
;
705 target_handle_event(target
, event
);
708 case TARGET_EVENT_GDB_HALT
:
709 gdb_frontend_halted(target
, connection
);
711 case TARGET_EVENT_HALTED
:
712 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
714 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
715 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
716 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
728 int gdb_new_connection(struct connection
*connection
)
730 struct gdb_connection
*gdb_connection
= malloc(sizeof(struct gdb_connection
));
731 struct gdb_service
*gdb_service
= connection
->service
->priv
;
735 connection
->priv
= gdb_connection
;
737 /* initialize gdb connection information */
738 gdb_connection
->buf_p
= gdb_connection
->buffer
;
739 gdb_connection
->buf_cnt
= 0;
740 gdb_connection
->ctrl_c
= 0;
741 gdb_connection
->frontend_state
= TARGET_HALTED
;
742 gdb_connection
->vflash_image
= NULL
;
743 gdb_connection
->closed
= 0;
744 gdb_connection
->busy
= 0;
745 gdb_connection
->noack_mode
= 0;
746 gdb_connection
->sync
= true;
748 /* send ACK to GDB for debug request */
749 gdb_write(connection
, "+", 1);
751 /* output goes through gdb connection */
752 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
754 /* we must remove all breakpoints registered to the target as a previous
755 * GDB session could leave dangling breakpoints if e.g. communication
758 breakpoint_clear_target(gdb_service
->target
);
759 watchpoint_clear_target(gdb_service
->target
);
761 /* register callback to be informed about target events */
762 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
764 /* remove the initial ACK from the incoming buffer */
765 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
768 /* FIX!!!??? would we actually ever receive a + here???
771 if (initial_ack
!= '+')
772 gdb_putback_char(connection
, initial_ack
);
773 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
775 gdb_actual_connections
++;
776 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
777 gdb_actual_connections
,
778 target_name(gdb_service
->target
),
779 target_state_name(gdb_service
->target
));
784 int gdb_connection_closed(struct connection
*connection
)
786 struct gdb_service
*gdb_service
= connection
->service
->priv
;
787 struct gdb_connection
*gdb_connection
= connection
->priv
;
789 /* we're done forwarding messages. Tear down callback before
790 * cleaning up connection.
792 log_remove_callback(gdb_log_callback
, connection
);
794 gdb_actual_connections
--;
795 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
796 target_name(gdb_service
->target
),
797 target_state_name(gdb_service
->target
),
798 gdb_actual_connections
);
800 /* see if an image built with vFlash commands is left */
801 if (gdb_connection
->vflash_image
)
803 image_close(gdb_connection
->vflash_image
);
804 free(gdb_connection
->vflash_image
);
805 gdb_connection
->vflash_image
= NULL
;
808 /* if this connection registered a debug-message receiver delete it */
809 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
811 if (connection
->priv
)
813 free(connection
->priv
);
814 connection
->priv
= NULL
;
818 LOG_ERROR("BUG: connection->priv == NULL");
822 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
824 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
826 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
831 void gdb_send_error(struct connection
*connection
, uint8_t the_error
)
834 snprintf(err
, 4, "E%2.2X", the_error
);
835 gdb_put_packet(connection
, err
, 3);
838 int gdb_last_signal_packet(struct connection
*connection
, struct target
*target
, char* packet
, int packet_size
)
843 signal
= gdb_last_signal(target
);
845 snprintf(sig_reply
, 4, "S%2.2x", signal
);
846 gdb_put_packet(connection
, sig_reply
, 3);
851 static int gdb_reg_pos(struct target
*target
, int pos
, int len
)
853 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
856 return len
- 1 - pos
;
859 /* Convert register to string of bytes. NB! The # of bits in the
860 * register might be non-divisible by 8(a byte), in which
861 * case an entire byte is shown.
863 * NB! the format on the wire is the target endianness
865 * The format of reg->value is little endian
868 void gdb_str_to_target(struct target
*target
, char *tstr
, struct reg
*reg
)
875 buf_len
= DIV_ROUND_UP(reg
->size
, 8);
877 for (i
= 0; i
< buf_len
; i
++)
879 int j
= gdb_reg_pos(target
, i
, buf_len
);
880 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
881 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
885 static int hextoint(char c
)
896 LOG_ERROR("BUG: invalid register value %08x", c
);
900 /* copy over in register buffer */
901 void gdb_target_to_reg(struct target
*target
, char *tstr
, int str_len
, uint8_t *bin
)
905 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
910 for (i
= 0; i
< str_len
; i
+= 2)
912 uint8_t t
= hextoint(tstr
[i
]) << 4;
913 t
|= hextoint(tstr
[i
+ 1]);
915 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
920 int gdb_get_registers_packet(struct connection
*connection
, struct target
*target
, char* packet
, int packet_size
)
922 struct reg
**reg_list
;
925 int reg_packet_size
= 0;
930 #ifdef _DEBUG_GDB_IO_
934 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
936 return gdb_error(connection
, retval
);
939 for (i
= 0; i
< reg_list_size
; i
++)
941 reg_packet_size
+= reg_list
[i
]->size
;
944 reg_packet
= malloc(DIV_ROUND_UP(reg_packet_size
, 8) * 2);
945 reg_packet_p
= reg_packet
;
947 for (i
= 0; i
< reg_list_size
; i
++)
949 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
950 reg_packet_p
+= DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2;
953 #ifdef _DEBUG_GDB_IO_
956 reg_packet_p
= strndup(reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
957 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
962 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_packet_size
, 8) * 2);
970 int gdb_set_registers_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
973 struct reg
**reg_list
;
978 #ifdef _DEBUG_GDB_IO_
982 /* skip command character */
988 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
989 return ERROR_SERVER_REMOTE_CLOSED
;
992 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
994 return gdb_error(connection
, retval
);
998 for (i
= 0; i
< reg_list_size
; i
++)
1001 int chars
= (DIV_ROUND_UP(reg_list
[i
]->size
, 8) * 2);
1003 if (packet_p
+ chars
> packet
+ packet_size
)
1005 LOG_ERROR("BUG: register packet is too small for registers");
1008 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[i
]->size
, 8));
1009 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1011 reg_list
[i
]->type
->set(reg_list
[i
], bin_buf
);
1013 /* advance packet pointer */
1020 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1023 gdb_put_packet(connection
, "OK", 2);
1028 int gdb_get_register_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1031 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1032 struct reg
**reg_list
;
1036 #ifdef _DEBUG_GDB_IO_
1040 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1042 return gdb_error(connection
, retval
);
1045 if (reg_list_size
<= reg_num
)
1047 LOG_ERROR("gdb requested a non-existing register");
1051 reg_packet
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1053 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1055 gdb_put_packet(connection
, reg_packet
, DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1063 int gdb_set_register_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1067 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1068 struct reg
**reg_list
;
1074 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1076 return gdb_error(connection
, retval
);
1079 if (reg_list_size
< reg_num
)
1081 LOG_ERROR("gdb requested a non-existing register");
1082 return ERROR_SERVER_REMOTE_CLOSED
;
1085 if (*separator
!= '=')
1087 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1088 return ERROR_SERVER_REMOTE_CLOSED
;
1091 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1092 bin_buf
= malloc(DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8));
1093 int chars
= (DIV_ROUND_UP(reg_list
[reg_num
]->size
, 8) * 2);
1095 /* fix!!! add some sanity checks on packet size here */
1097 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1099 reg_list
[reg_num
]->type
->set(reg_list
[reg_num
], bin_buf
);
1101 gdb_put_packet(connection
, "OK", 2);
1109 int gdb_error(struct connection
*connection
, int retval
)
1113 case ERROR_TARGET_DATA_ABORT
:
1114 gdb_send_error(connection
, EIO
);
1116 case ERROR_TARGET_TRANSLATION_FAULT
:
1117 gdb_send_error(connection
, EFAULT
);
1119 case ERROR_TARGET_UNALIGNED_ACCESS
:
1120 gdb_send_error(connection
, EFAULT
);
1122 case ERROR_TARGET_NOT_HALTED
:
1123 gdb_send_error(connection
, EFAULT
);
1126 /* This could be that the target reset itself. */
1127 LOG_ERROR("unexpected error %i", retval
);
1128 gdb_send_error(connection
, EFAULT
);
1135 /* We don't have to worry about the default 2 second timeout for GDB packets,
1136 * because GDB breaks up large memory reads into smaller reads.
1138 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1140 int gdb_read_memory_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1149 int retval
= ERROR_OK
;
1151 /* skip command character */
1154 addr
= strtoul(packet
, &separator
, 16);
1156 if (*separator
!= ',')
1158 LOG_ERROR("incomplete read memory packet received, dropping connection");
1159 return ERROR_SERVER_REMOTE_CLOSED
;
1162 len
= strtoul(separator
+ 1, NULL
, 16);
1164 buffer
= malloc(len
);
1166 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1168 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1170 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1172 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1173 * At some point this might be fixed in GDB, in which case this code can be removed.
1175 * OpenOCD developers are acutely aware of this problem, but there is nothing
1176 * gained by involving the user in this problem that hopefully will get resolved
1179 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1181 * For now, the default is to fix up things to make current GDB versions work.
1182 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1184 memset(buffer
, 0, len
);
1188 if (retval
== ERROR_OK
)
1190 hex_buffer
= malloc(len
* 2 + 1);
1193 for (i
= 0; i
< len
; i
++)
1195 uint8_t t
= buffer
[i
];
1196 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1197 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1200 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1206 retval
= gdb_error(connection
, retval
);
1214 int gdb_write_memory_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1225 /* skip command character */
1228 addr
= strtoul(packet
, &separator
, 16);
1230 if (*separator
!= ',')
1232 LOG_ERROR("incomplete write memory packet received, dropping connection");
1233 return ERROR_SERVER_REMOTE_CLOSED
;
1236 len
= strtoul(separator
+ 1, &separator
, 16);
1238 if (*(separator
++) != ':')
1240 LOG_ERROR("incomplete write memory packet received, dropping connection");
1241 return ERROR_SERVER_REMOTE_CLOSED
;
1244 buffer
= malloc(len
);
1246 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1248 for (i
= 0; i
< len
; i
++)
1251 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1255 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1257 if (retval
== ERROR_OK
)
1259 gdb_put_packet(connection
, "OK", 2);
1263 retval
= gdb_error(connection
, retval
);
1271 int gdb_write_memory_binary_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1279 /* skip command character */
1282 addr
= strtoul(packet
, &separator
, 16);
1284 if (*separator
!= ',')
1286 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1287 return ERROR_SERVER_REMOTE_CLOSED
;
1290 len
= strtoul(separator
+ 1, &separator
, 16);
1292 if (*(separator
++) != ':')
1294 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1295 return ERROR_SERVER_REMOTE_CLOSED
;
1301 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1303 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1306 if (retval
== ERROR_OK
)
1308 gdb_put_packet(connection
, "OK", 2);
1312 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1319 int gdb_step_continue_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1322 uint32_t address
= 0x0;
1323 int retval
= ERROR_OK
;
1327 if (packet_size
> 1)
1329 packet
[packet_size
] = 0;
1330 address
= strtoul(packet
+ 1, NULL
, 16);
1337 if (packet
[0] == 'c')
1339 LOG_DEBUG("continue");
1340 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1341 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1343 else if (packet
[0] == 's')
1346 /* step at current or address, don't handle breakpoints */
1347 retval
= target_step(target
, current
, address
, 0);
1352 int gdb_breakpoint_watchpoint_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1355 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1356 enum watchpoint_rw wp_type
;
1364 type
= strtoul(packet
+ 1, &separator
, 16);
1366 if (type
== 0) /* memory breakpoint */
1367 bp_type
= BKPT_SOFT
;
1368 else if (type
== 1) /* hardware breakpoint */
1369 bp_type
= BKPT_HARD
;
1370 else if (type
== 2) /* write watchpoint */
1371 wp_type
= WPT_WRITE
;
1372 else if (type
== 3) /* read watchpoint */
1374 else if (type
== 4) /* access watchpoint */
1375 wp_type
= WPT_ACCESS
;
1377 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1379 bp_type
= gdb_breakpoint_override_type
;
1382 if (*separator
!= ',')
1384 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1385 return ERROR_SERVER_REMOTE_CLOSED
;
1388 address
= strtoul(separator
+ 1, &separator
, 16);
1390 if (*separator
!= ',')
1392 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1393 return ERROR_SERVER_REMOTE_CLOSED
;
1396 size
= strtoul(separator
+ 1, &separator
, 16);
1402 if (packet
[0] == 'Z')
1404 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1406 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1411 gdb_put_packet(connection
, "OK", 2);
1416 breakpoint_remove(target
, address
);
1417 gdb_put_packet(connection
, "OK", 2);
1424 if (packet
[0] == 'Z')
1426 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1428 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1433 gdb_put_packet(connection
, "OK", 2);
1438 watchpoint_remove(target
, address
);
1439 gdb_put_packet(connection
, "OK", 2);
1450 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1451 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1453 if (*retval
!= ERROR_OK
)
1461 if ((*xml
== NULL
) || (!first
))
1463 /* start by 0 to exercise all the code paths.
1464 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1466 *size
= *size
* 2 + 2;
1468 *xml
= realloc(*xml
, *size
);
1473 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1481 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1483 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1488 /* there was just enough or not enough space, allocate more. */
1493 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1497 /* Extract and NUL-terminate the annex. */
1499 while (*buf
&& *buf
!= ':')
1505 /* After the read marker and annex, qXfer looks like a
1506 * traditional 'm' packet. */
1508 *ofs
= strtoul(buf
, &separator
, 16);
1510 if (*separator
!= ',')
1513 *len
= strtoul(separator
+ 1, NULL
, 16);
1518 int gdb_calc_blocksize(struct flash_bank
*bank
)
1521 uint32_t block_size
= 0xffffffff;
1523 /* loop through all sectors and return smallest sector size */
1525 for (i
= 0; i
< (uint32_t)bank
->num_sectors
; i
++)
1527 if (bank
->sectors
[i
].size
< block_size
)
1528 block_size
= bank
->sectors
[i
].size
;
1534 static int compare_bank (const void * a
, const void * b
)
1536 struct flash_bank
*b1
, *b2
;
1537 b1
=*((struct flash_bank
**)a
);
1538 b2
=*((struct flash_bank
**)b
);
1540 if (b1
->base
== b2
->base
)
1543 } else if (b1
->base
> b2
->base
)
1552 int gdb_query_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1554 struct command_context
*cmd_ctx
= connection
->cmd_ctx
;
1555 struct gdb_connection
*gdb_connection
= connection
->priv
;
1557 if (strstr(packet
, "qRcmd,"))
1559 if (packet_size
> 6)
1563 cmd
= malloc((packet_size
- 6)/2 + 1);
1564 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1567 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1570 cmd
[(packet_size
- 6)/2] = 0x0;
1572 /* We want to print all debug output to GDB connection */
1573 log_add_callback(gdb_log_callback
, connection
);
1574 target_call_timer_callbacks_now();
1575 /* some commands need to know the GDB connection, make note of current
1576 * GDB connection. */
1577 current_gdb_connection
= gdb_connection
;
1578 command_run_line(cmd_ctx
, cmd
);
1579 current_gdb_connection
= NULL
;
1580 target_call_timer_callbacks_now();
1581 log_remove_callback(gdb_log_callback
, connection
);
1584 gdb_put_packet(connection
, "OK", 2);
1587 else if (strstr(packet
, "qCRC:"))
1589 if (packet_size
> 5)
1598 /* skip command character */
1601 addr
= strtoul(packet
, &separator
, 16);
1603 if (*separator
!= ',')
1605 LOG_ERROR("incomplete read memory packet received, dropping connection");
1606 return ERROR_SERVER_REMOTE_CLOSED
;
1609 len
= strtoul(separator
+ 1, NULL
, 16);
1611 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1613 if (retval
== ERROR_OK
)
1615 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1616 gdb_put_packet(connection
, gdb_reply
, 9);
1620 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1627 else if (strstr(packet
, "qSupported"))
1629 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1630 * disable qXfer:features:read for the moment */
1631 int retval
= ERROR_OK
;
1632 char *buffer
= NULL
;
1636 xml_printf(&retval
, &buffer
, &pos
, &size
,
1637 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1638 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1640 if (retval
!= ERROR_OK
)
1642 gdb_send_error(connection
, 01);
1646 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1651 else if (strstr(packet
, "qXfer:memory-map:read::") && (flash_get_bank_count() > 0))
1653 /* We get away with only specifying flash here. Regions that are not
1654 * specified are treated as if we provided no memory map(if not we
1655 * could detect the holes and mark them as RAM).
1656 * Normally we only execute this code once, but no big deal if we
1657 * have to regenerate it a couple of times. */
1659 struct flash_bank
*p
;
1663 int retval
= ERROR_OK
;
1670 /* skip command character */
1673 offset
= strtoul(packet
, &separator
, 16);
1674 length
= strtoul(separator
+ 1, &separator
, 16);
1676 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1679 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1680 read/write) by default for GDB.
1681 GDB does not have a concept of non-cacheable read/write memory.
1683 struct flash_bank
**banks
= malloc(sizeof(struct flash_bank
*)*flash_get_bank_count());
1686 for (i
= 0; i
< flash_get_bank_count(); i
++)
1688 p
= get_flash_bank_by_num(i
);
1692 retval
= ERROR_FAIL
;
1693 gdb_send_error(connection
, retval
);
1699 qsort(banks
, flash_get_bank_count(), sizeof(struct flash_bank
*), compare_bank
);
1701 uint32_t ram_start
= 0;
1702 for (i
= 0; i
< flash_get_bank_count(); i
++)
1706 if (ram_start
< p
->base
)
1708 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1709 ram_start
, p
->base
-ram_start
);
1712 /* if device has uneven sector sizes, eg. str7, lpc
1713 * we pass the smallest sector size to gdb memory map */
1714 blocksize
= gdb_calc_blocksize(p
);
1716 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1717 "<property name=\"blocksize\">0x%x</property>\n" \
1719 p
->base
, p
->size
, blocksize
);
1720 ram_start
= p
->base
+ p
->size
;
1724 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1725 ram_start
, 0-ram_start
);
1728 /* a flash chip could be at the very end of the 32 bit address space, in which case
1729 ram_start will be precisely 0 */
1735 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1737 if (retval
!= ERROR_OK
)
1739 gdb_send_error(connection
, retval
);
1743 if (offset
+ length
> pos
)
1745 length
= pos
- offset
;
1748 char *t
= malloc(length
+ 1);
1750 memcpy(t
+ 1, xml
+ offset
, length
);
1751 gdb_put_packet(connection
, t
, length
+ 1);
1757 else if (strstr(packet
, "qXfer:features:read:"))
1762 int retval
= ERROR_OK
;
1765 unsigned int length
;
1768 /* skip command character */
1771 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1773 gdb_send_error(connection
, 01);
1777 if (strcmp(annex
, "target.xml") != 0)
1779 gdb_send_error(connection
, 01);
1783 xml_printf(&retval
, &xml
, &pos
, &size
, \
1784 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1786 if (retval
!= ERROR_OK
)
1788 gdb_send_error(connection
, retval
);
1792 gdb_put_packet(connection
, xml
, strlen(xml
));
1797 else if (strstr(packet
, "QStartNoAckMode"))
1799 gdb_connection
->noack_mode
= 1;
1800 gdb_put_packet(connection
, "OK", 2);
1804 gdb_put_packet(connection
, "", 0);
1808 int gdb_v_packet(struct connection
*connection
, struct target
*target
, char *packet
, int packet_size
)
1810 struct gdb_connection
*gdb_connection
= connection
->priv
;
1811 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1814 /* if flash programming disabled - send a empty reply */
1816 if (gdb_flash_program
== 0)
1818 gdb_put_packet(connection
, "", 0);
1822 if (strstr(packet
, "vFlashErase:"))
1825 unsigned long length
;
1827 char *parse
= packet
+ 12;
1830 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1831 return ERROR_SERVER_REMOTE_CLOSED
;
1834 addr
= strtoul(parse
, &parse
, 16);
1836 if (*(parse
++) != ',' || *parse
== '\0')
1838 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1839 return ERROR_SERVER_REMOTE_CLOSED
;
1842 length
= strtoul(parse
, &parse
, 16);
1846 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1847 return ERROR_SERVER_REMOTE_CLOSED
;
1850 /* assume all sectors need erasing - stops any problems
1851 * when flash_write is called multiple times */
1854 /* perform any target specific operations before the erase */
1855 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_START
);
1856 result
= flash_erase_address_range(gdb_service
->target
, addr
, length
);
1857 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_END
);
1860 if (result
!= ERROR_OK
)
1862 /* GDB doesn't evaluate the actual error number returned,
1863 * treat a failed erase as an I/O error
1865 gdb_send_error(connection
, EIO
);
1866 LOG_ERROR("flash_erase returned %i", result
);
1869 gdb_put_packet(connection
, "OK", 2);
1874 if (strstr(packet
, "vFlashWrite:"))
1878 unsigned long length
;
1879 char *parse
= packet
+ 12;
1883 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1884 return ERROR_SERVER_REMOTE_CLOSED
;
1886 addr
= strtoul(parse
, &parse
, 16);
1887 if (*(parse
++) != ':')
1889 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1890 return ERROR_SERVER_REMOTE_CLOSED
;
1892 length
= packet_size
- (parse
- packet
);
1894 /* create a new image if there isn't already one */
1895 if (gdb_connection
->vflash_image
== NULL
)
1897 gdb_connection
->vflash_image
= malloc(sizeof(struct image
));
1898 image_open(gdb_connection
->vflash_image
, "", "build");
1901 /* create new section with content from packet buffer */
1902 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
1907 gdb_put_packet(connection
, "OK", 2);
1912 if (!strcmp(packet
, "vFlashDone"))
1916 /* process the flashing buffer. No need to erase as GDB
1917 * always issues a vFlashErase first. */
1918 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
1919 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1920 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1921 if (result
!= ERROR_OK
)
1923 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1924 gdb_put_packet(connection
, "E.memtype", 9);
1926 gdb_send_error(connection
, EIO
);
1930 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
1931 gdb_put_packet(connection
, "OK", 2);
1934 image_close(gdb_connection
->vflash_image
);
1935 free(gdb_connection
->vflash_image
);
1936 gdb_connection
->vflash_image
= NULL
;
1941 gdb_put_packet(connection
, "", 0);
1945 int gdb_detach(struct connection
*connection
, struct target
*target
)
1947 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1949 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
1951 return gdb_put_packet(connection
, "OK", 2);
1954 static void gdb_log_callback(void *priv
, const char *file
, unsigned line
,
1955 const char *function
, const char *string
)
1957 struct connection
*connection
= priv
;
1958 struct gdb_connection
*gdb_con
= connection
->priv
;
1962 /* do not reply this using the O packet */
1966 gdb_output_con(connection
, string
);
1969 /* Do not allocate this on the stack */
1970 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
1972 static void gdb_sig_halted(struct connection
*connection
)
1975 snprintf(sig_reply
, 4, "T%2.2x", 2);
1976 gdb_put_packet(connection
, sig_reply
, 3);
1980 int gdb_input_inner(struct connection
*connection
)
1982 struct gdb_service
*gdb_service
= connection
->service
->priv
;
1983 struct target
*target
= gdb_service
->target
;
1984 char *packet
= gdb_packet_buffer
;
1987 struct gdb_connection
*gdb_con
= connection
->priv
;
1988 static int extended_protocol
= 0;
1990 /* drain input buffer */
1993 packet_size
= GDB_BUFFER_SIZE
-1;
1994 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
1999 /* terminate with zero */
2000 packet
[packet_size
] = 0;
2002 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2003 if (packet
[0] == 'X') {
2004 // binary packets spew junk into the debug log stream
2007 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2011 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2013 LOG_DEBUG("received packet: '%s'", packet
);
2017 if (packet_size
> 0)
2023 /* Hct... -- set thread
2024 * we don't have threads, send empty reply */
2025 gdb_put_packet(connection
, NULL
, 0);
2029 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
2032 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
2035 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
2038 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
2041 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
2044 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
2047 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
2051 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2054 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
2059 int retval
= ERROR_OK
;
2061 struct gdb_connection
*gdb_con
= connection
->priv
;
2062 log_add_callback(gdb_log_callback
, connection
);
2064 bool nostep
= false;
2065 if (target
->state
== TARGET_RUNNING
)
2067 LOG_WARNING("The target is already running. Halt target before stepi/continue.");
2068 retval
= target_halt(target
);
2069 if (retval
== ERROR_OK
)
2070 retval
= target_wait_state(target
, TARGET_HALTED
, 100);
2071 } else if (target
->state
!= TARGET_HALTED
)
2073 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2075 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2077 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2078 * sent by GDB first to OpenOCD, thus defeating the check to
2079 * make only the single stepping have the sync feature...
2082 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2084 gdb_con
->sync
= false;
2086 if ((retval
!=ERROR_OK
) || nostep
)
2088 /* Either the target isn't in the halted state, then we can't
2089 * step/continue. This might be early setup, etc.
2091 * Or we want to allow GDB to pick up a fresh set of
2092 * register values without modifying the target state.
2095 gdb_sig_halted(connection
);
2097 /* stop forwarding log packets! */
2098 log_remove_callback(gdb_log_callback
, connection
);
2101 /* We're running/stepping, in which case we can
2102 * forward log output until the target is halted
2104 gdb_con
->frontend_state
= TARGET_RUNNING
;
2105 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2106 int retval
= gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2107 if (retval
!= ERROR_OK
)
2109 /* we'll never receive a halted condition... issue a false one.. */
2110 gdb_frontend_halted(target
, connection
);
2116 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
2119 retval
= gdb_detach(connection
, target
);
2120 extended_protocol
= 0;
2123 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
2127 if (extended_protocol
!= 0)
2129 gdb_put_packet(connection
, "OK", 2);
2130 return ERROR_SERVER_REMOTE_CLOSED
;
2132 /* handle extended remote protocol */
2133 extended_protocol
= 1;
2134 gdb_put_packet(connection
, "OK", 2);
2137 /* handle extended restart packet */
2138 breakpoint_clear_target(gdb_service
->target
);
2139 watchpoint_clear_target(gdb_service
->target
);
2140 command_run_linef(connection
->cmd_ctx
,
2141 "ocd_gdb_restart %s",
2142 target_name(target
));
2145 /* ignore unknown packets */
2146 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2147 gdb_put_packet(connection
, NULL
, 0);
2151 /* if a packet handler returned an error, exit input loop */
2152 if (retval
!= ERROR_OK
)
2156 if (gdb_con
->ctrl_c
)
2158 if (target
->state
== TARGET_RUNNING
)
2160 retval
= target_halt(target
);
2161 if (retval
!= ERROR_OK
)
2163 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2165 gdb_con
->ctrl_c
= 0;
2168 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2169 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2173 } while (gdb_con
->buf_cnt
> 0);
2178 int gdb_input(struct connection
*connection
)
2180 int retval
= gdb_input_inner(connection
);
2181 struct gdb_connection
*gdb_con
= connection
->priv
;
2182 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2185 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2186 if (gdb_con
->closed
)
2187 return ERROR_SERVER_REMOTE_CLOSED
;
2189 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2193 static int gdb_target_start(struct target
*target
, uint16_t port
)
2195 bool use_pipes
= 0 == port
;
2196 struct gdb_service
*gdb_service
= malloc(sizeof(struct gdb_service
));
2197 if (NULL
== gdb_service
)
2200 gdb_service
->target
= target
;
2202 add_service("gdb", use_pipes
? CONNECTION_PIPE
: CONNECTION_TCP
,
2203 port
, 1, &gdb_new_connection
, &gdb_input
,
2204 &gdb_connection_closed
, gdb_service
);
2206 const char *name
= target_name(target
);
2208 LOG_DEBUG("gdb service for target '%s' using pipes", name
);
2210 LOG_DEBUG("gdb service for target '%s' on TCP port %u", name
, port
);
2214 int gdb_target_add_one(struct target
*target
)
2216 if (gdb_port
== 0 && server_use_pipes
== 0)
2218 LOG_INFO("gdb port disabled");
2221 if (0 == gdb_port_next
)
2222 gdb_port_next
= gdb_port
;
2224 bool use_pipes
= server_use_pipes
;
2225 static bool server_started_with_pipes
= false;
2226 if (server_started_with_pipes
)
2228 LOG_WARNING("gdb service permits one target when using pipes");
2235 int e
= gdb_target_start(target
, use_pipes
? 0 : gdb_port_next
);
2238 server_started_with_pipes
|= use_pipes
;
2244 int gdb_target_add_all(struct target
*target
)
2248 LOG_WARNING("gdb services need one or more targets defined");
2252 while (NULL
!= target
)
2254 int retval
= gdb_target_add_one(target
);
2255 if (ERROR_OK
!= retval
)
2258 target
= target
->next
;
2264 COMMAND_HANDLER(handle_gdb_sync_command
)
2268 return ERROR_COMMAND_SYNTAX_ERROR
;
2271 if (current_gdb_connection
== NULL
)
2273 command_print(CMD_CTX
,
2274 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2278 current_gdb_connection
->sync
= true;
2283 /* daemon configuration command gdb_port */
2284 COMMAND_HANDLER(handle_gdb_port_command
)
2286 int retval
= CALL_COMMAND_HANDLER(server_port_command
, &gdb_port
);
2287 if (ERROR_OK
== retval
)
2288 gdb_port_next
= gdb_port
;
2292 COMMAND_HANDLER(handle_gdb_memory_map_command
)
2295 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_use_memory_map
);
2297 return ERROR_COMMAND_SYNTAX_ERROR
;
2300 COMMAND_HANDLER(handle_gdb_flash_program_command
)
2303 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_flash_program
);
2305 return ERROR_COMMAND_SYNTAX_ERROR
;
2308 COMMAND_HANDLER(handle_gdb_report_data_abort_command
)
2311 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], gdb_report_data_abort
);
2313 return ERROR_COMMAND_SYNTAX_ERROR
;
2316 /* gdb_breakpoint_override */
2317 COMMAND_HANDLER(handle_gdb_breakpoint_override_command
)
2322 } else if (CMD_ARGC
== 1)
2324 gdb_breakpoint_override
= 1;
2325 if (strcmp(CMD_ARGV
[0], "hard") == 0)
2327 gdb_breakpoint_override_type
= BKPT_HARD
;
2328 } else if (strcmp(CMD_ARGV
[0], "soft") == 0)
2330 gdb_breakpoint_override_type
= BKPT_SOFT
;
2331 } else if (strcmp(CMD_ARGV
[0], "disable") == 0)
2333 gdb_breakpoint_override
= 0;
2337 return ERROR_COMMAND_SYNTAX_ERROR
;
2339 if (gdb_breakpoint_override
)
2341 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2344 LOG_USER("breakpoint type is not overridden");
2350 static const struct command_registration gdb_command_handlers
[] = {
2353 .handler
= &handle_gdb_sync_command
,
2354 .mode
= COMMAND_ANY
,
2355 .help
= "next stepi will return immediately allowing "
2356 "GDB to fetch register state without affecting "
2361 .handler
= &handle_gdb_port_command
,
2362 .mode
= COMMAND_ANY
,
2363 .help
= "daemon configuration command gdb_port",
2367 .name
= "gdb_memory_map",
2368 .handler
= &handle_gdb_memory_map_command
,
2369 .mode
= COMMAND_CONFIG
,
2370 .help
= "enable or disable memory map",
2371 .usage
= "enable|disable"
2374 .name
= "gdb_flash_program",
2375 .handler
= &handle_gdb_flash_program_command
,
2376 .mode
= COMMAND_CONFIG
,
2377 .help
= "enable or disable flash program",
2378 .usage
= "enable|disable"
2381 .name
= "gdb_report_data_abort",
2382 .handler
= &handle_gdb_report_data_abort_command
,
2383 .mode
= COMMAND_CONFIG
,
2384 .help
= "enable or disable reporting data aborts",
2385 .usage
= "enable|disable"
2388 .name
= "gdb_breakpoint_override",
2389 .handler
= &handle_gdb_breakpoint_override_command
,
2390 .mode
= COMMAND_EXEC
,
2391 .help
= "force type of breakpoint "
2392 "used by gdb 'break' commands.",
2393 .usage
= "hard|soft|disable",
2395 COMMAND_REGISTRATION_DONE
2398 int gdb_register_commands(struct command_context
*cmd_ctx
)
2400 return register_commands(cmd_ctx
, NULL
, gdb_command_handlers
);