1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
30 #include "gdb_server.h"
31 #include "target_request.h"
40 #define _DEBUG_GDB_IO_
43 static gdb_connection_t
*current_gdb_connection
;
45 static int gdb_breakpoint_override
;
46 static enum breakpoint_type gdb_breakpoint_override_type
;
48 extern int gdb_error(connection_t
*connection
, int retval
);
49 static unsigned short gdb_port
= 3333;
50 static const char *DIGITS
= "0123456789abcdef";
52 static void gdb_log_callback(void *priv
, const char *file
, int line
,
53 const char *function
, const char *string
);
63 /* target behaviour on gdb detach */
64 enum gdb_detach_mode detach_mode
= GDB_DETACH_RESUME
;
66 /* number of gdb connections, mainly to supress gdb related debugging spam
67 * in helper/log.c when no gdb connections are actually active */
68 int gdb_actual_connections
;
70 /* set if we are sending a memory map to gdb
71 * via qXfer:memory-map:read packet */
72 /* enabled by default*/
73 int gdb_use_memory_map
= 1;
74 /* enabled by default*/
75 int gdb_flash_program
= 1;
77 /* if set, data aborts cause an error to be reported in memory read packets
78 * see the code in gdb_read_memory_packet() for further explanations */
79 int gdb_report_data_abort
= 0;
81 int gdb_last_signal(target_t
*target
)
83 switch (target
->debug_reason
)
85 case DBG_REASON_DBGRQ
:
86 return 0x2; /* SIGINT */
87 case DBG_REASON_BREAKPOINT
:
88 case DBG_REASON_WATCHPOINT
:
89 case DBG_REASON_WPTANDBKPT
:
90 return 0x05; /* SIGTRAP */
91 case DBG_REASON_SINGLESTEP
:
92 return 0x05; /* SIGTRAP */
93 case DBG_REASON_NOTHALTED
:
94 return 0x0; /* no signal... shouldn't happen */
96 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
101 int check_pending(connection_t
*connection
, int timeout_s
, int *got_data
)
103 /* a non-blocking socket will block if there is 0 bytes available on the socket,
104 * but return with as many bytes as are available immediately
108 gdb_connection_t
*gdb_con
= connection
->priv
;
110 if (got_data
== NULL
)
114 if (gdb_con
->buf_cnt
> 0)
121 FD_SET(connection
->fd
, &read_fds
);
123 tv
.tv_sec
= timeout_s
;
125 if (socket_select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
127 /* This can typically be because a "monitor" command took too long
128 * before printing any progress messages
132 return ERROR_GDB_TIMEOUT
;
138 *got_data
= FD_ISSET(connection
->fd
, &read_fds
) != 0;
142 int gdb_get_char(connection_t
*connection
, int* next_char
)
144 gdb_connection_t
*gdb_con
= connection
->priv
;
145 int retval
= ERROR_OK
;
147 #ifdef _DEBUG_GDB_IO_
151 if (gdb_con
->buf_cnt
-- > 0)
153 *next_char
= *(gdb_con
->buf_p
++);
154 if (gdb_con
->buf_cnt
> 0)
155 connection
->input_pending
= 1;
157 connection
->input_pending
= 0;
159 #ifdef _DEBUG_GDB_IO_
160 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
168 if (connection
->service
->type
== CONNECTION_PIPE
)
170 gdb_con
->buf_cnt
= read(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
174 retval
= check_pending(connection
, 1, NULL
);
175 if (retval
!= ERROR_OK
)
177 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
180 if (gdb_con
->buf_cnt
> 0)
184 if (gdb_con
->buf_cnt
== 0)
187 return ERROR_SERVER_REMOTE_CLOSED
;
191 errno
= WSAGetLastError();
198 case WSAECONNABORTED
:
200 return ERROR_SERVER_REMOTE_CLOSED
;
203 return ERROR_SERVER_REMOTE_CLOSED
;
205 LOG_ERROR("read: %d", errno
);
216 return ERROR_SERVER_REMOTE_CLOSED
;
219 return ERROR_SERVER_REMOTE_CLOSED
;
221 LOG_ERROR("read: %s", strerror(errno
));
223 return ERROR_SERVER_REMOTE_CLOSED
;
228 #ifdef _DEBUG_GDB_IO_
229 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
230 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
231 debug_buffer
[gdb_con
->buf_cnt
] = 0;
232 LOG_DEBUG("received '%s'", debug_buffer
);
236 gdb_con
->buf_p
= gdb_con
->buffer
;
238 *next_char
= *(gdb_con
->buf_p
++);
239 if (gdb_con
->buf_cnt
> 0)
240 connection
->input_pending
= 1;
242 connection
->input_pending
= 0;
243 #ifdef _DEBUG_GDB_IO_
244 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
250 int gdb_putback_char(connection_t
*connection
, int last_char
)
252 gdb_connection_t
*gdb_con
= connection
->priv
;
254 if (gdb_con
->buf_p
> gdb_con
->buffer
)
256 *(--gdb_con
->buf_p
) = last_char
;
261 LOG_ERROR("BUG: couldn't put character back");
267 /* The only way we can detect that the socket is closed is the first time
268 * we write to it, we will fail. Subsequent write operations will
269 * succeed. Shudder! */
270 int gdb_write(connection_t
*connection
, void *data
, int len
)
272 gdb_connection_t
*gdb_con
= connection
->priv
;
274 return ERROR_SERVER_REMOTE_CLOSED
;
276 if (connection
->service
->type
== CONNECTION_PIPE
)
278 /* write to stdout */
279 if (write(STDOUT_FILENO
, data
, len
) == len
)
286 if (write_socket(connection
->fd
, data
, len
) == len
)
292 return ERROR_SERVER_REMOTE_CLOSED
;
295 int gdb_put_packet_inner(connection_t
*connection
, char *buffer
, int len
)
298 unsigned char my_checksum
= 0;
299 #ifdef _DEBUG_GDB_IO_
304 gdb_connection_t
*gdb_con
= connection
->priv
;
306 for (i
= 0; i
< len
; i
++)
307 my_checksum
+= buffer
[i
];
309 #ifdef _DEBUG_GDB_IO_
311 * At this point we should have nothing in the input queue from GDB,
312 * however sometimes '-' is sent even though we've already received
313 * an ACK (+) for everything we've sent off.
318 if ((retval
= check_pending(connection
, 0, &gotdata
)) != ERROR_OK
)
322 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
325 /* fix a problem with some IAR tools */
326 gdb_putback_char(connection
, reply
);
327 LOG_DEBUG("Unexpected start of new packet");
331 LOG_WARNING("Discard unexpected char %c", reply
);
337 #ifdef _DEBUG_GDB_IO_
338 debug_buffer
= malloc(len
+ 1);
339 memcpy(debug_buffer
, buffer
, len
);
340 debug_buffer
[len
] = 0;
341 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
345 char local_buffer
[1024];
346 local_buffer
[0] = '$';
347 if ((size_t)len
+ 4 <= sizeof(local_buffer
))
349 /* performance gain on smaller packets by only a single call to gdb_write() */
350 memcpy(local_buffer
+ 1, buffer
, len
++);
351 local_buffer
[len
++] = '#';
352 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
353 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
354 if ((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
361 /* larger packets are transmitted directly from caller supplied buffer
362 by several calls to gdb_write() to avoid dynamic allocation */
363 local_buffer
[1] = '#';
364 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
365 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
366 if ((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
370 if ((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
374 if ((retval
= gdb_write(connection
, local_buffer
+ 1, 3)) != ERROR_OK
)
380 if (gdb_con
->noack_mode
)
383 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
388 else if (reply
== '-')
390 /* Stop sending output packets for now */
391 log_remove_callback(gdb_log_callback
, connection
);
392 LOG_WARNING("negative reply, retrying");
394 else if (reply
== 0x3)
397 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
401 else if (reply
== '-')
403 /* Stop sending output packets for now */
404 log_remove_callback(gdb_log_callback
, connection
);
405 LOG_WARNING("negative reply, retrying");
407 else if (reply
== '$') {
408 LOG_ERROR("GDB missing ack(1) - assumed good");
409 gdb_putback_char(connection
, reply
);
413 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
415 return ERROR_SERVER_REMOTE_CLOSED
;
418 else if (reply
== '$') {
419 LOG_ERROR("GDB missing ack(2) - assumed good");
420 gdb_putback_char(connection
, reply
);
425 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
427 return ERROR_SERVER_REMOTE_CLOSED
;
431 return ERROR_SERVER_REMOTE_CLOSED
;
436 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
438 gdb_connection_t
*gdb_con
= connection
->priv
;
440 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
443 /* we sent some data, reset timer for keep alive messages */
449 static __inline__
int fetch_packet(connection_t
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
451 unsigned char my_checksum
= 0;
456 gdb_connection_t
*gdb_con
= connection
->priv
;
462 /* The common case is that we have an entire packet with no escape chars.
463 * We need to leave at least 2 bytes in the buffer to have
464 * gdb_get_char() update various bits and bobs correctly.
466 if ((gdb_con
->buf_cnt
> 2) && ((gdb_con
->buf_cnt
+ count
) < *len
))
468 /* The compiler will struggle a bit with constant propagation and
469 * aliasing, so we help it by showing that these values do not
470 * change inside the loop
473 char *buf
= gdb_con
->buf_p
;
474 int run
= gdb_con
->buf_cnt
- 2;
481 if (character
== '#')
483 /* Danger! character can be '#' when esc is
484 * used so we need an explicit boolean for done here.
490 if (character
== '}')
492 /* data transmitted in binary mode (X packet)
493 * uses 0x7d as escape character */
494 my_checksum
+= character
& 0xff;
497 my_checksum
+= character
& 0xff;
498 buffer
[count
++] = (character
^ 0x20) & 0xff;
502 my_checksum
+= character
& 0xff;
503 buffer
[count
++] = character
& 0xff;
507 gdb_con
->buf_cnt
-= i
;
513 LOG_ERROR("packet buffer too small");
514 return ERROR_GDB_BUFFER_TOO_SMALL
;
517 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
520 if (character
== '#')
523 if (character
== '}')
525 /* data transmitted in binary mode (X packet)
526 * uses 0x7d as escape character */
527 my_checksum
+= character
& 0xff;
528 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
530 my_checksum
+= character
& 0xff;
531 buffer
[count
++] = (character
^ 0x20) & 0xff;
535 my_checksum
+= character
& 0xff;
536 buffer
[count
++] = character
& 0xff;
542 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
544 checksum
[0] = character
;
545 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
547 checksum
[1] = character
;
552 *checksum_ok
= (my_checksum
== strtoul(checksum
, NULL
, 16));
558 int gdb_get_packet_inner(connection_t
*connection
, char *buffer
, int *len
)
562 gdb_connection_t
*gdb_con
= connection
->priv
;
568 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
571 #ifdef _DEBUG_GDB_IO_
572 LOG_DEBUG("character: '%c'", character
);
580 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
581 * incase anyone tries to debug why they receive this warning every time */
582 LOG_WARNING("acknowledgment received, but no packet pending");
585 LOG_WARNING("negative acknowledgment, but no packet pending");
592 LOG_WARNING("ignoring character 0x%x", character
);
595 } while (character
!= '$');
600 /* explicit code expansion here to get faster inlined code in -O3 by not
601 * calculating checksum
603 if (gdb_con
->noack_mode
)
605 if ((retval
= fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
)) != ERROR_OK
)
609 if ((retval
= fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
)) != ERROR_OK
)
613 if (gdb_con
->noack_mode
)
615 /* checksum is not checked in noack mode */
620 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
628 return ERROR_SERVER_REMOTE_CLOSED
;
633 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
635 gdb_connection_t
*gdb_con
= connection
->priv
;
637 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
642 int gdb_output_con(connection_t
*connection
, const char* line
)
647 bin_size
= strlen(line
);
649 hex_buffer
= malloc(bin_size
*2 + 2);
650 if (hex_buffer
== NULL
)
651 return ERROR_GDB_BUFFER_TOO_SMALL
;
654 for (i
= 0; i
< bin_size
; i
++)
655 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
656 hex_buffer
[bin_size
*2 + 1] = 0;
658 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
664 int gdb_output(struct command_context_s
*context
, const char* line
)
666 /* this will be dumped to the log and also sent as an O packet if possible */
667 LOG_USER_N("%s", line
);
672 static void gdb_frontend_halted(struct target_s
*target
, connection_t
*connection
)
674 gdb_connection_t
*gdb_connection
= connection
->priv
;
676 /* In the GDB protocol when we are stepping or continuing execution,
677 * we have a lingering reply. Upon receiving a halted event
678 * when we have that lingering packet, we reply to the original
679 * step or continue packet.
681 * Executing monitor commands can bring the target in and
682 * out of the running state so we'll see lots of TARGET_EVENT_XXX
683 * that are to be ignored.
685 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
690 /* stop forwarding log packets! */
691 log_remove_callback(gdb_log_callback
, connection
);
693 if (gdb_connection
->ctrl_c
)
696 gdb_connection
->ctrl_c
= 0;
700 signal
= gdb_last_signal(target
);
703 snprintf(sig_reply
, 4, "T%2.2x", signal
);
704 gdb_put_packet(connection
, sig_reply
, 3);
705 gdb_connection
->frontend_state
= TARGET_HALTED
;
709 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
712 connection_t
*connection
= priv
;
714 target_handle_event(target
, event
);
717 case TARGET_EVENT_GDB_HALT
:
718 gdb_frontend_halted(target
, connection
);
720 case TARGET_EVENT_HALTED
:
721 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
723 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
724 target_handle_event(target
, TARGET_EVENT_OLD_gdb_program_config
);
725 if ((retval
= jtag_execute_queue()) != ERROR_OK
)
737 int gdb_new_connection(connection_t
*connection
)
739 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
740 gdb_service_t
*gdb_service
= connection
->service
->priv
;
744 connection
->priv
= gdb_connection
;
746 /* initialize gdb connection information */
747 gdb_connection
->buf_p
= gdb_connection
->buffer
;
748 gdb_connection
->buf_cnt
= 0;
749 gdb_connection
->ctrl_c
= 0;
750 gdb_connection
->frontend_state
= TARGET_HALTED
;
751 gdb_connection
->vflash_image
= NULL
;
752 gdb_connection
->closed
= 0;
753 gdb_connection
->busy
= 0;
754 gdb_connection
->noack_mode
= 0;
755 gdb_connection
->sync
= true;
757 /* send ACK to GDB for debug request */
758 gdb_write(connection
, "+", 1);
760 /* output goes through gdb connection */
761 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
763 /* we must remove all breakpoints registered to the target as a previous
764 * GDB session could leave dangling breakpoints if e.g. communication
767 breakpoint_clear_target(gdb_service
->target
);
768 watchpoint_clear_target(gdb_service
->target
);
770 /* register callback to be informed about target events */
771 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
773 /* remove the initial ACK from the incoming buffer */
774 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
777 /* FIX!!!??? would we actually ever receive a + here???
780 if (initial_ack
!= '+')
781 gdb_putback_char(connection
, initial_ack
);
782 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
784 gdb_actual_connections
++;
785 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
786 gdb_actual_connections
,
787 gdb_service
->target
->cmd_name
,
788 target_state_name(gdb_service
->target
));
793 int gdb_connection_closed(connection_t
*connection
)
795 gdb_service_t
*gdb_service
= connection
->service
->priv
;
796 gdb_connection_t
*gdb_connection
= connection
->priv
;
798 /* we're done forwarding messages. Tear down callback before
799 * cleaning up connection.
801 log_remove_callback(gdb_log_callback
, connection
);
803 gdb_actual_connections
--;
804 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
805 gdb_service
->target
->cmd_name
,
806 target_state_name(gdb_service
->target
),
807 gdb_actual_connections
);
809 /* see if an image built with vFlash commands is left */
810 if (gdb_connection
->vflash_image
)
812 image_close(gdb_connection
->vflash_image
);
813 free(gdb_connection
->vflash_image
);
814 gdb_connection
->vflash_image
= NULL
;
817 /* if this connection registered a debug-message receiver delete it */
818 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
820 if (connection
->priv
)
822 free(connection
->priv
);
823 connection
->priv
= NULL
;
827 LOG_ERROR("BUG: connection->priv == NULL");
831 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
833 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
835 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
840 void gdb_send_error(connection_t
*connection
, uint8_t the_error
)
843 snprintf(err
, 4, "E%2.2X", the_error
);
844 gdb_put_packet(connection
, err
, 3);
847 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
852 signal
= gdb_last_signal(target
);
854 snprintf(sig_reply
, 4, "S%2.2x", signal
);
855 gdb_put_packet(connection
, sig_reply
, 3);
860 static int gdb_reg_pos(target_t
*target
, int pos
, int len
)
862 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
865 return len
- 1 - pos
;
868 /* Convert register to string of bytes. NB! The # of bits in the
869 * register might be non-divisible by 8(a byte), in which
870 * case an entire byte is shown.
872 * NB! the format on the wire is the target endianess
874 * The format of reg->value is little endian
877 void gdb_str_to_target(target_t
*target
, char *tstr
, reg_t
*reg
)
884 buf_len
= CEIL(reg
->size
, 8);
886 for (i
= 0; i
< buf_len
; i
++)
888 int j
= gdb_reg_pos(target
, i
, buf_len
);
889 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
890 tstr
[i
*2 + 1] = DIGITS
[buf
[j
]&0xf];
894 static int hextoint(char c
)
905 LOG_ERROR("BUG: invalid register value %08x", c
);
909 /* copy over in register buffer */
910 void gdb_target_to_reg(target_t
*target
, char *tstr
, int str_len
, uint8_t *bin
)
914 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
919 for (i
= 0; i
< str_len
; i
+= 2)
921 uint8_t t
= hextoint(tstr
[i
]) << 4;
922 t
|= hextoint(tstr
[i
+ 1]);
924 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
929 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
934 int reg_packet_size
= 0;
939 #ifdef _DEBUG_GDB_IO_
943 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
945 return gdb_error(connection
, retval
);
948 for (i
= 0; i
< reg_list_size
; i
++)
950 reg_packet_size
+= reg_list
[i
]->size
;
953 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
954 reg_packet_p
= reg_packet
;
956 for (i
= 0; i
< reg_list_size
; i
++)
958 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
959 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
962 #ifdef _DEBUG_GDB_IO_
965 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
966 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
971 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
979 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
987 #ifdef _DEBUG_GDB_IO_
991 /* skip command character */
997 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
998 return ERROR_SERVER_REMOTE_CLOSED
;
1001 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1003 return gdb_error(connection
, retval
);
1007 for (i
= 0; i
< reg_list_size
; i
++)
1010 int chars
= (CEIL(reg_list
[i
]->size
, 8) * 2);
1012 if (packet_p
+ chars
> packet
+ packet_size
)
1014 LOG_ERROR("BUG: register packet is too small for registers");
1017 reg_arch_type_t
*arch_type
;
1018 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
1019 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1021 /* get register arch_type, and call set method */
1022 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
1024 arch_type
->set(reg_list
[i
], bin_buf
);
1026 /* advance packet pointer */
1033 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
1036 gdb_put_packet(connection
, "OK", 2);
1041 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1044 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1049 #ifdef _DEBUG_GDB_IO_
1053 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1055 return gdb_error(connection
, retval
);
1058 if (reg_list_size
<= reg_num
)
1060 LOG_ERROR("gdb requested a non-existing register");
1064 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1066 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1068 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1076 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1080 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1084 reg_arch_type_t
*arch_type
;
1088 if ((retval
= target_get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1090 return gdb_error(connection
, retval
);
1093 if (reg_list_size
< reg_num
)
1095 LOG_ERROR("gdb requested a non-existing register");
1096 return ERROR_SERVER_REMOTE_CLOSED
;
1099 if (*separator
!= '=')
1101 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1102 return ERROR_SERVER_REMOTE_CLOSED
;
1105 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1106 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
1107 int chars
= (CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1109 /* fix!!! add some sanity checks on packet size here */
1111 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1113 /* get register arch_type, and call set method */
1114 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
1115 arch_type
->set(reg_list
[reg_num
], bin_buf
);
1117 gdb_put_packet(connection
, "OK", 2);
1125 int gdb_error(connection_t
*connection
, int retval
)
1129 case ERROR_TARGET_DATA_ABORT
:
1130 gdb_send_error(connection
, EIO
);
1132 case ERROR_TARGET_TRANSLATION_FAULT
:
1133 gdb_send_error(connection
, EFAULT
);
1135 case ERROR_TARGET_UNALIGNED_ACCESS
:
1136 gdb_send_error(connection
, EFAULT
);
1138 case ERROR_TARGET_NOT_HALTED
:
1139 gdb_send_error(connection
, EFAULT
);
1142 /* This could be that the target reset itself. */
1143 LOG_ERROR("unexpected error %i", retval
);
1144 gdb_send_error(connection
, EFAULT
);
1151 /* We don't have to worry about the default 2 second timeout for GDB packets,
1152 * because GDB breaks up large memory reads into smaller reads.
1154 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1156 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1165 int retval
= ERROR_OK
;
1167 /* skip command character */
1170 addr
= strtoul(packet
, &separator
, 16);
1172 if (*separator
!= ',')
1174 LOG_ERROR("incomplete read memory packet received, dropping connection");
1175 return ERROR_SERVER_REMOTE_CLOSED
;
1178 len
= strtoul(separator
+ 1, NULL
, 16);
1180 buffer
= malloc(len
);
1182 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1184 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1186 if ((retval
!= ERROR_OK
)&&!gdb_report_data_abort
)
1188 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1189 * At some point this might be fixed in GDB, in which case this code can be removed.
1191 * OpenOCD developers are acutely aware of this problem, but there is nothing
1192 * gained by involving the user in this problem that hopefully will get resolved
1195 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1197 * For now, the default is to fix up things to make current GDB versions work.
1198 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1200 memset(buffer
, 0, len
);
1204 if (retval
== ERROR_OK
)
1206 hex_buffer
= malloc(len
* 2 + 1);
1209 for (i
= 0; i
< len
; i
++)
1211 uint8_t t
= buffer
[i
];
1212 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1213 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1216 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1222 retval
= gdb_error(connection
, retval
);
1230 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1241 /* skip command character */
1244 addr
= strtoul(packet
, &separator
, 16);
1246 if (*separator
!= ',')
1248 LOG_ERROR("incomplete write memory packet received, dropping connection");
1249 return ERROR_SERVER_REMOTE_CLOSED
;
1252 len
= strtoul(separator
+ 1, &separator
, 16);
1254 if (*(separator
++) != ':')
1256 LOG_ERROR("incomplete write memory packet received, dropping connection");
1257 return ERROR_SERVER_REMOTE_CLOSED
;
1260 buffer
= malloc(len
);
1262 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1264 for (i
= 0; i
< len
; i
++)
1267 sscanf(separator
+ 2*i
, "%2" SCNx32
, &tmp
);
1271 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1273 if (retval
== ERROR_OK
)
1275 gdb_put_packet(connection
, "OK", 2);
1279 retval
= gdb_error(connection
, retval
);
1287 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1295 /* skip command character */
1298 addr
= strtoul(packet
, &separator
, 16);
1300 if (*separator
!= ',')
1302 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1303 return ERROR_SERVER_REMOTE_CLOSED
;
1306 len
= strtoul(separator
+ 1, &separator
, 16);
1308 if (*(separator
++) != ':')
1310 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1311 return ERROR_SERVER_REMOTE_CLOSED
;
1317 LOG_DEBUG("addr: 0x%8.8" PRIx32
", len: 0x%8.8" PRIx32
"", addr
, len
);
1319 retval
= target_write_buffer(target
, addr
, len
, (uint8_t*)separator
);
1322 if (retval
== ERROR_OK
)
1324 gdb_put_packet(connection
, "OK", 2);
1328 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1335 int gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1338 uint32_t address
= 0x0;
1339 int retval
= ERROR_OK
;
1343 if (packet_size
> 1)
1345 packet
[packet_size
] = 0;
1346 address
= strtoul(packet
+ 1, NULL
, 16);
1353 if (packet
[0] == 'c')
1355 LOG_DEBUG("continue");
1356 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1357 retval
= target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1359 else if (packet
[0] == 's')
1362 /* step at current or address, don't handle breakpoints */
1363 retval
= target_step(target
, current
, address
, 0);
1368 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1371 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1372 enum watchpoint_rw wp_type
;
1380 type
= strtoul(packet
+ 1, &separator
, 16);
1382 if (type
== 0) /* memory breakpoint */
1383 bp_type
= BKPT_SOFT
;
1384 else if (type
== 1) /* hardware breakpoint */
1385 bp_type
= BKPT_HARD
;
1386 else if (type
== 2) /* write watchpoint */
1387 wp_type
= WPT_WRITE
;
1388 else if (type
== 3) /* read watchpoint */
1390 else if (type
== 4) /* access watchpoint */
1391 wp_type
= WPT_ACCESS
;
1393 if (gdb_breakpoint_override
&& ((bp_type
== BKPT_SOFT
)||(bp_type
== BKPT_HARD
)))
1395 bp_type
= gdb_breakpoint_override_type
;
1398 if (*separator
!= ',')
1400 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1401 return ERROR_SERVER_REMOTE_CLOSED
;
1404 address
= strtoul(separator
+ 1, &separator
, 16);
1406 if (*separator
!= ',')
1408 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1409 return ERROR_SERVER_REMOTE_CLOSED
;
1412 size
= strtoul(separator
+ 1, &separator
, 16);
1418 if (packet
[0] == 'Z')
1420 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1422 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1427 gdb_put_packet(connection
, "OK", 2);
1432 breakpoint_remove(target
, address
);
1433 gdb_put_packet(connection
, "OK", 2);
1440 if (packet
[0] == 'Z')
1442 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1444 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1449 gdb_put_packet(connection
, "OK", 2);
1454 watchpoint_remove(target
, address
);
1455 gdb_put_packet(connection
, "OK", 2);
1466 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1467 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1469 if (*retval
!= ERROR_OK
)
1477 if ((*xml
== NULL
) || (!first
))
1479 /* start by 0 to exercise all the code paths.
1480 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1482 *size
= *size
* 2 + 2;
1484 *xml
= realloc(*xml
, *size
);
1489 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1497 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1499 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1504 /* there was just enough or not enough space, allocate more. */
1509 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1513 /* Extract and NUL-terminate the annex. */
1515 while (*buf
&& *buf
!= ':')
1521 /* After the read marker and annex, qXfer looks like a
1522 * traditional 'm' packet. */
1524 *ofs
= strtoul(buf
, &separator
, 16);
1526 if (*separator
!= ',')
1529 *len
= strtoul(separator
+ 1, NULL
, 16);
1534 int gdb_calc_blocksize(flash_bank_t
*bank
)
1537 uint32_t block_size
= 0xffffffff;
1539 /* loop through all sectors and return smallest sector size */
1541 for (i
= 0; i
< (uint32_t)bank
->num_sectors
; i
++)
1543 if (bank
->sectors
[i
].size
< block_size
)
1544 block_size
= bank
->sectors
[i
].size
;
1550 static int compare_bank (const void * a
, const void * b
)
1552 flash_bank_t
*b1
, *b2
;
1553 b1
=*((flash_bank_t
**)a
);
1554 b2
=*((flash_bank_t
**)b
);
1556 if (b1
->base
== b2
->base
)
1559 } else if (b1
->base
> b2
->base
)
1568 int gdb_query_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1570 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1571 gdb_connection_t
*gdb_connection
= connection
->priv
;
1573 if (strstr(packet
, "qRcmd,"))
1575 if (packet_size
> 6)
1579 cmd
= malloc((packet_size
- 6)/2 + 1);
1580 for (i
= 0; i
< (packet_size
- 6)/2; i
++)
1583 sscanf(packet
+ 6 + 2*i
, "%2" SCNx32
, &tmp
);
1586 cmd
[(packet_size
- 6)/2] = 0x0;
1588 /* We want to print all debug output to GDB connection */
1589 log_add_callback(gdb_log_callback
, connection
);
1590 target_call_timer_callbacks_now();
1591 /* some commands need to know the GDB connection, make note of current
1592 * GDB connection. */
1593 current_gdb_connection
= gdb_connection
;
1594 command_run_line(cmd_ctx
, cmd
);
1595 current_gdb_connection
= NULL
;
1596 target_call_timer_callbacks_now();
1597 log_remove_callback(gdb_log_callback
, connection
);
1600 gdb_put_packet(connection
, "OK", 2);
1603 else if (strstr(packet
, "qCRC:"))
1605 if (packet_size
> 5)
1614 /* skip command character */
1617 addr
= strtoul(packet
, &separator
, 16);
1619 if (*separator
!= ',')
1621 LOG_ERROR("incomplete read memory packet received, dropping connection");
1622 return ERROR_SERVER_REMOTE_CLOSED
;
1625 len
= strtoul(separator
+ 1, NULL
, 16);
1627 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1629 if (retval
== ERROR_OK
)
1631 snprintf(gdb_reply
, 10, "C%8.8" PRIx32
"", checksum
);
1632 gdb_put_packet(connection
, gdb_reply
, 9);
1636 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1643 else if (strstr(packet
, "qSupported"))
1645 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1646 * disable qXfer:features:read for the moment */
1647 int retval
= ERROR_OK
;
1648 char *buffer
= NULL
;
1652 xml_printf(&retval
, &buffer
, &pos
, &size
,
1653 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1654 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1656 if (retval
!= ERROR_OK
)
1658 gdb_send_error(connection
, 01);
1662 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1667 else if (strstr(packet
, "qXfer:memory-map:read::") && (flash_get_bank_count() > 0))
1669 /* We get away with only specifying flash here. Regions that are not
1670 * specified are treated as if we provided no memory map(if not we
1671 * could detect the holes and mark them as RAM).
1672 * Normally we only execute this code once, but no big deal if we
1673 * have to regenerate it a couple of times. */
1679 int retval
= ERROR_OK
;
1686 /* skip command character */
1689 offset
= strtoul(packet
, &separator
, 16);
1690 length
= strtoul(separator
+ 1, &separator
, 16);
1692 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1695 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1696 read/write) by default for GDB.
1697 GDB does not have a concept of non-cacheable read/write memory.
1699 flash_bank_t
**banks
= malloc(sizeof(flash_bank_t
*)*flash_get_bank_count());
1702 for (i
= 0; i
< flash_get_bank_count(); i
++)
1704 p
= get_flash_bank_by_num(i
);
1708 retval
= ERROR_FAIL
;
1709 gdb_send_error(connection
, retval
);
1715 qsort(banks
, flash_get_bank_count(), sizeof(flash_bank_t
*), compare_bank
);
1717 uint32_t ram_start
= 0;
1718 for (i
= 0; i
< flash_get_bank_count(); i
++)
1722 if (ram_start
< p
->base
)
1724 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1725 ram_start
, p
->base
-ram_start
);
1728 /* if device has uneven sector sizes, eg. str7, lpc
1729 * we pass the smallest sector size to gdb memory map */
1730 blocksize
= gdb_calc_blocksize(p
);
1732 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1733 "<property name=\"blocksize\">0x%x</property>\n" \
1735 p
->base
, p
->size
, blocksize
);
1736 ram_start
= p
->base
+ p
->size
;
1740 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1741 ram_start
, 0-ram_start
);
1744 /* a flash chip could be at the very end of the 32 bit address space, in which case
1745 ram_start will be precisely 0 */
1751 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1753 if (retval
!= ERROR_OK
)
1755 gdb_send_error(connection
, retval
);
1759 if (offset
+ length
> pos
)
1761 length
= pos
- offset
;
1764 char *t
= malloc(length
+ 1);
1766 memcpy(t
+ 1, xml
+ offset
, length
);
1767 gdb_put_packet(connection
, t
, length
+ 1);
1773 else if (strstr(packet
, "qXfer:features:read:"))
1778 int retval
= ERROR_OK
;
1781 unsigned int length
;
1784 /* skip command character */
1787 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1789 gdb_send_error(connection
, 01);
1793 if (strcmp(annex
, "target.xml") != 0)
1795 gdb_send_error(connection
, 01);
1799 xml_printf(&retval
, &xml
, &pos
, &size
, \
1800 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1802 if (retval
!= ERROR_OK
)
1804 gdb_send_error(connection
, retval
);
1808 gdb_put_packet(connection
, xml
, strlen(xml
));
1813 else if (strstr(packet
, "QStartNoAckMode"))
1815 gdb_connection
->noack_mode
= 1;
1816 gdb_put_packet(connection
, "OK", 2);
1820 gdb_put_packet(connection
, "", 0);
1824 int gdb_v_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1826 gdb_connection_t
*gdb_connection
= connection
->priv
;
1827 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1830 /* if flash programming disabled - send a empty reply */
1832 if (gdb_flash_program
== 0)
1834 gdb_put_packet(connection
, "", 0);
1838 if (strstr(packet
, "vFlashErase:"))
1841 unsigned long length
;
1843 char *parse
= packet
+ 12;
1846 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1847 return ERROR_SERVER_REMOTE_CLOSED
;
1850 addr
= strtoul(parse
, &parse
, 16);
1852 if (*(parse
++) != ',' || *parse
== '\0')
1854 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1855 return ERROR_SERVER_REMOTE_CLOSED
;
1858 length
= strtoul(parse
, &parse
, 16);
1862 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1863 return ERROR_SERVER_REMOTE_CLOSED
;
1866 /* assume all sectors need erasing - stops any problems
1867 * when flash_write is called multiple times */
1870 /* perform any target specific operations before the erase */
1871 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_START
);
1872 result
= flash_erase_address_range(gdb_service
->target
, addr
, length
);
1873 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_END
);
1876 if (result
!= ERROR_OK
)
1878 /* GDB doesn't evaluate the actual error number returned,
1879 * treat a failed erase as an I/O error
1881 gdb_send_error(connection
, EIO
);
1882 LOG_ERROR("flash_erase returned %i", result
);
1885 gdb_put_packet(connection
, "OK", 2);
1890 if (strstr(packet
, "vFlashWrite:"))
1894 unsigned long length
;
1895 char *parse
= packet
+ 12;
1899 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1900 return ERROR_SERVER_REMOTE_CLOSED
;
1902 addr
= strtoul(parse
, &parse
, 16);
1903 if (*(parse
++) != ':')
1905 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1906 return ERROR_SERVER_REMOTE_CLOSED
;
1908 length
= packet_size
- (parse
- packet
);
1910 /* create a new image if there isn't already one */
1911 if (gdb_connection
->vflash_image
== NULL
)
1913 gdb_connection
->vflash_image
= malloc(sizeof(image_t
));
1914 image_open(gdb_connection
->vflash_image
, "", "build");
1917 /* create new section with content from packet buffer */
1918 if ((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (uint8_t*)parse
)) != ERROR_OK
)
1923 gdb_put_packet(connection
, "OK", 2);
1928 if (!strcmp(packet
, "vFlashDone"))
1932 /* process the flashing buffer. No need to erase as GDB
1933 * always issues a vFlashErase first. */
1934 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
1935 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1936 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1937 if (result
!= ERROR_OK
)
1939 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1940 gdb_put_packet(connection
, "E.memtype", 9);
1942 gdb_send_error(connection
, EIO
);
1946 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written
);
1947 gdb_put_packet(connection
, "OK", 2);
1950 image_close(gdb_connection
->vflash_image
);
1951 free(gdb_connection
->vflash_image
);
1952 gdb_connection
->vflash_image
= NULL
;
1957 gdb_put_packet(connection
, "", 0);
1961 int gdb_detach(connection_t
*connection
, target_t
*target
)
1964 switch (detach_mode
)
1966 case GDB_DETACH_RESUME
:
1967 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
1968 target_resume(target
, 1, 0, 1, 0);
1971 case GDB_DETACH_RESET
:
1972 /* FIX?? make this configurable?? */
1973 target_process_reset(connection
->cmd_ctx
, RESET_HALT
);
1976 case GDB_DETACH_HALT
:
1977 target_halt(target
);
1980 case GDB_DETACH_NOTHING
:
1984 gdb_put_packet(connection
, "OK", 2);
1988 static void gdb_log_callback(void *priv
, const char *file
, int line
,
1989 const char *function
, const char *string
)
1991 connection_t
*connection
= priv
;
1992 gdb_connection_t
*gdb_con
= connection
->priv
;
1996 /* do not reply this using the O packet */
2000 gdb_output_con(connection
, string
);
2003 /* Do not allocate this on the stack */
2004 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
2006 static void gdb_sig_halted(connection_t
*connection
)
2009 snprintf(sig_reply
, 4, "T%2.2x", 2);
2010 gdb_put_packet(connection
, sig_reply
, 3);
2014 int gdb_input_inner(connection_t
*connection
)
2016 gdb_service_t
*gdb_service
= connection
->service
->priv
;
2017 target_t
*target
= gdb_service
->target
;
2018 char *packet
= gdb_packet_buffer
;
2021 gdb_connection_t
*gdb_con
= connection
->priv
;
2022 static int extended_protocol
= 0;
2024 /* drain input buffer */
2027 packet_size
= GDB_BUFFER_SIZE
-1;
2028 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
2033 /* terminate with zero */
2034 packet
[packet_size
] = 0;
2036 if (LOG_LEVEL_IS(LOG_LVL_DEBUG
)) {
2037 if (packet
[0] == 'X') {
2038 // binary packets spew junk into the debug log stream
2041 for (x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++) {
2045 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2047 LOG_DEBUG("received packet: '%s'", packet
);
2051 if (packet_size
> 0)
2057 /* Hct... -- set thread
2058 * we don't have threads, send empty reply */
2059 gdb_put_packet(connection
, NULL
, 0);
2063 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
2066 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
2069 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
2072 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
2075 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
2078 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
2081 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
2085 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2088 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
2093 int retval
= ERROR_OK
;
2095 gdb_connection_t
*gdb_con
= connection
->priv
;
2096 log_add_callback(gdb_log_callback
, connection
);
2098 bool nostep
= false;
2099 if (target
->state
== TARGET_RUNNING
)
2101 LOG_WARNING("The target is already running. Halt target before stepi/continue.");
2102 retval
= target_halt(target
);
2103 if (retval
== ERROR_OK
)
2104 retval
= target_wait_state(target
, TARGET_HALTED
, 100);
2105 } else if (target
->state
!= TARGET_HALTED
)
2107 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2109 } else if ((packet
[0] == 's') && gdb_con
->sync
)
2111 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2112 * sent by GDB first to OpenOCD, thus defeating the check to
2113 * make only the single stepping have the sync feature...
2116 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2118 gdb_con
->sync
= false;
2120 if ((retval
!=ERROR_OK
) || nostep
)
2122 /* Either the target isn't in the halted state, then we can't
2123 * step/continue. This might be early setup, etc.
2125 * Or we want to allow GDB to pick up a fresh set of
2126 * register values without modifying the target state.
2129 gdb_sig_halted(connection
);
2131 /* stop forwarding log packets! */
2132 log_remove_callback(gdb_log_callback
, connection
);
2135 /* We're running/stepping, in which case we can
2136 * forward log output until the target is halted
2138 gdb_con
->frontend_state
= TARGET_RUNNING
;
2139 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2140 int retval
= gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2141 if (retval
!= ERROR_OK
)
2143 /* we'll never receive a halted condition... issue a false one.. */
2144 gdb_frontend_halted(target
, connection
);
2150 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
2153 retval
= gdb_detach(connection
, target
);
2154 extended_protocol
= 0;
2157 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
2161 if (extended_protocol
!= 0)
2163 gdb_put_packet(connection
, "OK", 2);
2164 return ERROR_SERVER_REMOTE_CLOSED
;
2166 /* handle extended remote protocol */
2167 extended_protocol
= 1;
2168 gdb_put_packet(connection
, "OK", 2);
2171 /* handle extended restart packet */
2172 breakpoint_clear_target(gdb_service
->target
);
2173 watchpoint_clear_target(gdb_service
->target
);
2174 command_run_linef(connection
->cmd_ctx
,
2175 "ocd_gdb_restart %s",
2179 /* ignore unkown packets */
2180 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2181 gdb_put_packet(connection
, NULL
, 0);
2185 /* if a packet handler returned an error, exit input loop */
2186 if (retval
!= ERROR_OK
)
2190 if (gdb_con
->ctrl_c
)
2192 if (target
->state
== TARGET_RUNNING
)
2194 target_halt(target
);
2195 gdb_con
->ctrl_c
= 0;
2199 } while (gdb_con
->buf_cnt
> 0);
2204 int gdb_input(connection_t
*connection
)
2206 int retval
= gdb_input_inner(connection
);
2207 gdb_connection_t
*gdb_con
= connection
->priv
;
2208 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2211 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2212 if (gdb_con
->closed
)
2213 return ERROR_SERVER_REMOTE_CLOSED
;
2215 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2221 gdb_service_t
*gdb_service
;
2222 target_t
*target
= all_targets
;
2226 LOG_WARNING("no gdb ports allocated as no target has been specified");
2230 if (gdb_port
== 0 && server_use_pipes
== 0)
2232 LOG_INFO("gdb port disabled");
2236 if (server_use_pipes
)
2238 /* only a single gdb connection when using a pipe */
2240 gdb_service
= malloc(sizeof(gdb_service_t
));
2241 gdb_service
->target
= target
;
2243 add_service("gdb", CONNECTION_PIPE
, 0, 1, gdb_new_connection
, gdb_input
, gdb_connection_closed
, gdb_service
);
2245 LOG_DEBUG("gdb service for target %s using pipes",
2246 target_get_name(target
));
2250 unsigned short port
= gdb_port
;
2254 gdb_service
= malloc(sizeof(gdb_service_t
));
2255 gdb_service
->target
= target
;
2257 add_service("gdb", CONNECTION_TCP
,
2259 gdb_new_connection
, gdb_input
,
2260 gdb_connection_closed
, gdb_service
);
2262 LOG_DEBUG("gdb service for target %s at TCP port %i",
2263 target_get_name(target
),
2265 target
= target
->next
;
2273 int handle_gdb_sync_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2277 return ERROR_COMMAND_SYNTAX_ERROR
;
2280 if (current_gdb_connection
== NULL
)
2282 command_print(cmd_ctx
,
2283 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2287 current_gdb_connection
->sync
= true;
2292 /* daemon configuration command gdb_port */
2293 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2297 command_print(cmd_ctx
, "%d", gdb_port
);
2301 gdb_port
= strtoul(args
[0], NULL
, 0);
2306 int handle_gdb_detach_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2310 if (strcmp(args
[0], "resume") == 0)
2312 detach_mode
= GDB_DETACH_RESUME
;
2315 else if (strcmp(args
[0], "reset") == 0)
2317 detach_mode
= GDB_DETACH_RESET
;
2320 else if (strcmp(args
[0], "halt") == 0)
2322 detach_mode
= GDB_DETACH_HALT
;
2325 else if (strcmp(args
[0], "nothing") == 0)
2327 detach_mode
= GDB_DETACH_NOTHING
;
2331 LOG_WARNING("invalid gdb_detach configuration directive: %s", args
[0]);
2334 return ERROR_COMMAND_SYNTAX_ERROR
;
2337 int handle_gdb_memory_map_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2341 if (strcmp(args
[0], "enable") == 0)
2343 gdb_use_memory_map
= 1;
2346 else if (strcmp(args
[0], "disable") == 0)
2348 gdb_use_memory_map
= 0;
2352 LOG_WARNING("invalid gdb_memory_map configuration directive %s", args
[0]);
2355 return ERROR_COMMAND_SYNTAX_ERROR
;
2358 int handle_gdb_flash_program_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2362 if (strcmp(args
[0], "enable") == 0)
2364 gdb_flash_program
= 1;
2367 else if (strcmp(args
[0], "disable") == 0)
2369 gdb_flash_program
= 0;
2373 LOG_WARNING("invalid gdb_flash_program configuration directive: %s", args
[0]);
2376 return ERROR_COMMAND_SYNTAX_ERROR
;
2379 int handle_gdb_report_data_abort_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2383 if (strcmp(args
[0], "enable") == 0)
2385 gdb_report_data_abort
= 1;
2388 else if (strcmp(args
[0], "disable") == 0)
2390 gdb_report_data_abort
= 0;
2394 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args
[0]);
2397 return ERROR_COMMAND_SYNTAX_ERROR
;
2400 /* gdb_breakpoint_override */
2401 int handle_gdb_breakpoint_override_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2406 } else if (argc
== 1)
2408 gdb_breakpoint_override
= 1;
2409 if (strcmp(args
[0], "hard") == 0)
2411 gdb_breakpoint_override_type
= BKPT_HARD
;
2412 } else if (strcmp(args
[0], "soft") == 0)
2414 gdb_breakpoint_override_type
= BKPT_SOFT
;
2415 } else if (strcmp(args
[0], "disable") == 0)
2417 gdb_breakpoint_override
= 0;
2421 return ERROR_COMMAND_SYNTAX_ERROR
;
2423 if (gdb_breakpoint_override
)
2425 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
== BKPT_HARD
)?"hard":"soft");
2428 LOG_USER("breakpoint type is not overriden");
2434 int gdb_register_commands(command_context_t
*command_context
)
2436 register_command(command_context
, NULL
, "gdb_sync", handle_gdb_sync_command
,
2437 COMMAND_ANY
, "next stepi will return immediately allowing GDB fetch register state without affecting target state");
2438 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
2439 COMMAND_ANY
, "daemon configuration command gdb_port");
2440 register_command(command_context
, NULL
, "gdb_detach", handle_gdb_detach_command
,
2441 COMMAND_CONFIG
, "resume/reset/halt/nothing - "
2442 "specify behavior when GDB detaches from the target");
2443 register_command(command_context
, NULL
, "gdb_memory_map", handle_gdb_memory_map_command
,
2444 COMMAND_CONFIG
, "enable or disable memory map");
2445 register_command(command_context
, NULL
, "gdb_flash_program", handle_gdb_flash_program_command
,
2446 COMMAND_CONFIG
, "enable or disable flash program");
2447 register_command(command_context
, NULL
, "gdb_report_data_abort", handle_gdb_report_data_abort_command
,
2448 COMMAND_CONFIG
, "enable or disable reporting data aborts");
2449 register_command(command_context
, NULL
, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command
,
2450 COMMAND_EXEC
, "hard/soft/disable - force breakpoint type for gdb 'break' commands.");
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)