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 "replacements.h"
32 #include "gdb_server.h"
36 #include "binarybuffer.h"
38 #include "breakpoints.h"
40 #include "target_request.h"
41 #include "configuration.h"
49 #define _DEBUG_GDB_IO_
52 static int gdb_breakpoint_override
;
53 static enum breakpoint_type gdb_breakpoint_override_type
;
55 extern int gdb_error(connection_t
*connection
, int retval
);
56 static unsigned short gdb_port
;
57 static const char *DIGITS
= "0123456789abcdef";
59 static void gdb_log_callback(void *priv
, const char *file
, int line
,
60 const char *function
, const char *string
);
70 /* target behaviour on gdb detach */
71 enum gdb_detach_mode detach_mode
= GDB_DETACH_RESUME
;
73 /* set if we are sending a memory map to gdb
74 * via qXfer:memory-map:read packet */
75 /* enabled by default*/
76 int gdb_use_memory_map
= 1;
77 /* enabled by default*/
78 int gdb_flash_program
= 1;
80 /* if set, data aborts cause an error to be reported in memory read packets
81 * see the code in gdb_read_memory_packet() for further explanations */
82 int gdb_report_data_abort
= 0;
84 int gdb_last_signal(target_t
*target
)
86 switch (target
->debug_reason
)
88 case DBG_REASON_DBGRQ
:
89 return 0x2; /* SIGINT */
90 case DBG_REASON_BREAKPOINT
:
91 case DBG_REASON_WATCHPOINT
:
92 case DBG_REASON_WPTANDBKPT
:
93 return 0x05; /* SIGTRAP */
94 case DBG_REASON_SINGLESTEP
:
95 return 0x05; /* SIGTRAP */
96 case DBG_REASON_NOTHALTED
:
97 return 0x0; /* no signal... shouldn't happen */
99 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
104 int check_pending(connection_t
*connection
, int timeout_s
, int *got_data
)
106 /* a non-blocking socket will block if there is 0 bytes available on the socket,
107 * but return with as many bytes as are available immediately
111 gdb_connection_t
*gdb_con
= connection
->priv
;
117 if (gdb_con
->buf_cnt
>0)
124 FD_SET(connection
->fd
, &read_fds
);
126 tv
.tv_sec
= timeout_s
;
128 if (select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
130 /* This can typically be because a "monitor" command took too long
131 * before printing any progress messages
135 return ERROR_GDB_TIMEOUT
;
141 *got_data
=FD_ISSET(connection
->fd
, &read_fds
)!=0;
145 int gdb_get_char(connection_t
*connection
, int* next_char
)
147 gdb_connection_t
*gdb_con
= connection
->priv
;
150 #ifdef _DEBUG_GDB_IO_
154 if (gdb_con
->buf_cnt
-- > 0)
156 *next_char
= *(gdb_con
->buf_p
++);
157 if (gdb_con
->buf_cnt
> 0)
158 connection
->input_pending
= 1;
160 connection
->input_pending
= 0;
162 #ifdef _DEBUG_GDB_IO_
163 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
171 retval
=check_pending(connection
, 1, NULL
);
172 if (retval
!=ERROR_OK
)
174 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
175 if (gdb_con
->buf_cnt
> 0)
179 if (gdb_con
->buf_cnt
== 0)
182 return ERROR_SERVER_REMOTE_CLOSED
;
186 errno
= WSAGetLastError();
193 case WSAECONNABORTED
:
195 return ERROR_SERVER_REMOTE_CLOSED
;
198 return ERROR_SERVER_REMOTE_CLOSED
;
200 LOG_ERROR("read: %d", errno
);
211 return ERROR_SERVER_REMOTE_CLOSED
;
214 return ERROR_SERVER_REMOTE_CLOSED
;
216 LOG_ERROR("read: %s", strerror(errno
));
218 return ERROR_SERVER_REMOTE_CLOSED
;
223 #ifdef _DEBUG_GDB_IO_
224 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
225 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
226 debug_buffer
[gdb_con
->buf_cnt
] = 0;
227 LOG_DEBUG("received '%s'", debug_buffer
);
231 gdb_con
->buf_p
= gdb_con
->buffer
;
233 *next_char
= *(gdb_con
->buf_p
++);
234 if (gdb_con
->buf_cnt
> 0)
235 connection
->input_pending
= 1;
237 connection
->input_pending
= 0;
238 #ifdef _DEBUG_GDB_IO_
239 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
245 int gdb_putback_char(connection_t
*connection
, int last_char
)
247 gdb_connection_t
*gdb_con
= connection
->priv
;
249 if (gdb_con
->buf_p
> gdb_con
->buffer
)
251 *(--gdb_con
->buf_p
) = last_char
;
256 LOG_ERROR("BUG: couldn't put character back");
262 /* The only way we can detect that the socket is closed is the first time
263 * we write to it, we will fail. Subsequent write operations will
264 * succeed. Shudder! */
265 int gdb_write(connection_t
*connection
, void *data
, int len
)
267 gdb_connection_t
*gdb_con
= connection
->priv
;
269 return ERROR_SERVER_REMOTE_CLOSED
;
271 if (write_socket(connection
->fd
, data
, len
) == len
)
276 return ERROR_SERVER_REMOTE_CLOSED
;
279 int gdb_put_packet_inner(connection_t
*connection
, char *buffer
, int len
)
282 unsigned char my_checksum
= 0;
283 #ifdef _DEBUG_GDB_IO_
288 gdb_connection_t
*gdb_con
= connection
->priv
;
290 for (i
= 0; i
< len
; i
++)
291 my_checksum
+= buffer
[i
];
293 #ifdef _DEBUG_GDB_IO_
295 * At this point we should have nothing in the input queue from GDB,
296 * however sometimes '-' is sent even though we've already received
297 * an ACK (+) for everything we've sent off.
302 if ((retval
=check_pending(connection
, 0, &gotdata
))!=ERROR_OK
)
306 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
309 // fix a problem with some IAR tools
310 gdb_putback_char( connection
, reply
);
311 LOG_DEBUG("Unexpected start of new packet");
315 LOG_WARNING("Discard unexpected char %c", reply
);
321 #ifdef _DEBUG_GDB_IO_
322 debug_buffer
= malloc(len
+ 1);
323 memcpy(debug_buffer
, buffer
, len
);
324 debug_buffer
[len
] = 0;
325 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
329 char local_buffer
[1024];
330 local_buffer
[0] = '$';
331 if (len
+4 <= sizeof(local_buffer
))
333 /* performance gain on smaller packets by only a single call to gdb_write() */
334 memcpy(local_buffer
+1, buffer
, len
++);
335 local_buffer
[len
++] = '#';
336 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
337 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
338 gdb_write(connection
, local_buffer
, len
);
342 /* larger packets are transmitted directly from caller supplied buffer
343 by several calls to gdb_write() to avoid dynamic allocation */
344 local_buffer
[1] = '#';
345 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
346 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
347 gdb_write(connection
, local_buffer
, 1);
348 gdb_write(connection
, buffer
, len
);
349 gdb_write(connection
, local_buffer
+1, 3);
352 if (gdb_con
->noack_mode
)
355 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
360 else if (reply
== '-')
362 /* Stop sending output packets for now */
363 log_remove_callback(gdb_log_callback
, connection
);
364 LOG_WARNING("negative reply, retrying");
366 else if (reply
== 0x3)
369 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
373 else if (reply
== '-')
375 /* Stop sending output packets for now */
376 log_remove_callback(gdb_log_callback
, connection
);
377 LOG_WARNING("negative reply, retrying");
379 else if( reply
== '$' ){
380 LOG_ERROR("GDB missing ack(1) - assumed good");
381 gdb_putback_char( connection
, reply
);
385 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
387 return ERROR_SERVER_REMOTE_CLOSED
;
390 else if( reply
== '$' ){
391 LOG_ERROR("GDB missing ack(2) - assumed good");
392 gdb_putback_char( connection
, reply
);
397 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
399 return ERROR_SERVER_REMOTE_CLOSED
;
403 return ERROR_SERVER_REMOTE_CLOSED
;
408 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
410 gdb_connection_t
*gdb_con
= connection
->priv
;
412 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
415 /* we sent some data, reset timer for keep alive messages */
421 static __inline__
int fetch_packet(connection_t
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
423 unsigned char my_checksum
= 0;
428 gdb_connection_t
*gdb_con
= connection
->priv
;
434 /* The common case is that we have an entire packet with no escape chars.
435 * We need to leave at least 2 bytes in the buffer to have
436 * gdb_get_char() update various bits and bobs correctly.
438 if ((gdb_con
->buf_cnt
> 2) && ((gdb_con
->buf_cnt
+count
) < *len
))
440 /* The compiler will struggle a bit with constant propagation and
441 * aliasing, so we help it by showing that these values do not
442 * change inside the loop
445 char *buf
= gdb_con
->buf_p
;
446 int run
= gdb_con
->buf_cnt
- 2;
453 if (character
== '#')
455 /* Danger! character can be '#' when esc is
456 * used so we need an explicit boolean for done here.
462 if (character
== '}')
464 /* data transmitted in binary mode (X packet)
465 * uses 0x7d as escape character */
466 my_checksum
+= character
& 0xff;
469 my_checksum
+= character
& 0xff;
470 buffer
[count
++] = (character
^ 0x20) & 0xff;
474 my_checksum
+= character
& 0xff;
475 buffer
[count
++] = character
& 0xff;
479 gdb_con
->buf_cnt
-= i
;
485 LOG_ERROR("packet buffer too small");
486 return ERROR_GDB_BUFFER_TOO_SMALL
;
489 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
492 if (character
== '#')
495 if (character
== '}')
497 /* data transmitted in binary mode (X packet)
498 * uses 0x7d as escape character */
499 my_checksum
+= character
& 0xff;
500 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
502 my_checksum
+= character
& 0xff;
503 buffer
[count
++] = (character
^ 0x20) & 0xff;
507 my_checksum
+= character
& 0xff;
508 buffer
[count
++] = character
& 0xff;
514 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
516 checksum
[0] = character
;
517 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
519 checksum
[1] = character
;
524 *checksum_ok
=(my_checksum
== strtoul(checksum
, NULL
, 16));
530 int gdb_get_packet_inner(connection_t
*connection
, char *buffer
, int *len
)
534 gdb_connection_t
*gdb_con
= connection
->priv
;
540 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
543 #ifdef _DEBUG_GDB_IO_
544 LOG_DEBUG("character: '%c'", character
);
552 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
553 * incase anyone tries to debug why they receive this warning every time */
554 LOG_WARNING("acknowledgment received, but no packet pending");
557 LOG_WARNING("negative acknowledgment, but no packet pending");
564 LOG_WARNING("ignoring character 0x%x", character
);
567 } while (character
!= '$');
572 /* explicit code expansion here to get faster inlined code in -O3 by not
573 * calculating checksum
575 if (gdb_con
->noack_mode
)
577 if ((retval
=fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
))!=ERROR_OK
)
581 if ((retval
=fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
))!=ERROR_OK
)
585 if (gdb_con
->noack_mode
)
587 /* checksum is not checked in noack mode */
592 gdb_write(connection
, "+", 1);
597 return ERROR_SERVER_REMOTE_CLOSED
;
602 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
604 gdb_connection_t
*gdb_con
= connection
->priv
;
606 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
611 int gdb_output_con(connection_t
*connection
, const char* line
)
616 bin_size
= strlen(line
);
618 hex_buffer
= malloc(bin_size
*2 + 2);
619 if (hex_buffer
== NULL
)
620 return ERROR_GDB_BUFFER_TOO_SMALL
;
623 for (i
=0; i
<bin_size
; i
++)
624 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
625 hex_buffer
[bin_size
*2+1] = 0;
627 gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
633 int gdb_output(struct command_context_s
*context
, const char* line
)
635 /* this will be dumped to the log and also sent as an O packet if possible */
636 LOG_USER_N("%s", line
);
641 static void gdb_frontend_halted(struct target_s
*target
, connection_t
*connection
)
643 gdb_connection_t
*gdb_connection
= connection
->priv
;
645 /* In the GDB protocol when we are stepping or coninuing execution,
646 * we have a lingering reply. Upon receiving a halted event
647 * when we have that lingering packet, we reply to the original
648 * step or continue packet.
650 * Executing monitor commands can bring the target in and
651 * out of the running state so we'll see lots of TARGET_EVENT_XXX
652 * that are to be ignored.
654 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
658 /* stop forwarding log packets! */
659 log_remove_callback(gdb_log_callback
, connection
);
661 if (gdb_connection
->ctrl_c
)
664 gdb_connection
->ctrl_c
= 0;
668 signal
= gdb_last_signal(target
);
671 snprintf(sig_reply
, 4, "T%2.2x", signal
);
672 gdb_put_packet(connection
, sig_reply
, 3);
673 gdb_connection
->frontend_state
= TARGET_HALTED
;
677 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
679 connection_t
*connection
= priv
;
681 target_handle_event( target
, event
);
684 case TARGET_EVENT_EARLY_HALTED
:
685 gdb_frontend_halted(target
, connection
);
687 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
688 target_handle_event( target
, TARGET_EVENT_OLD_gdb_program_config
);
689 jtag_execute_queue();
699 int gdb_new_connection(connection_t
*connection
)
701 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
702 gdb_service_t
*gdb_service
= connection
->service
->priv
;
706 connection
->priv
= gdb_connection
;
708 /* initialize gdb connection information */
709 gdb_connection
->buf_p
= gdb_connection
->buffer
;
710 gdb_connection
->buf_cnt
= 0;
711 gdb_connection
->ctrl_c
= 0;
712 gdb_connection
->frontend_state
= TARGET_HALTED
;
713 gdb_connection
->vflash_image
= NULL
;
714 gdb_connection
->closed
= 0;
715 gdb_connection
->busy
= 0;
716 gdb_connection
->noack_mode
= 0;
718 /* send ACK to GDB for debug request */
719 gdb_write(connection
, "+", 1);
721 /* output goes through gdb connection */
722 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
724 /* we must remove all breakpoints registered to the target as a previous
725 * GDB session could leave dangling breakpoints if e.g. communication
728 breakpoint_clear_target(gdb_service
->target
);
729 watchpoint_clear_target(gdb_service
->target
);
731 /* register callback to be informed about target events */
732 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
734 /* a gdb session just attached, try to put the target in halt mode.
738 * If the halt fails(e.g. target needs a reset, JTAG communication not
739 * working, etc.), then the GDB connect will succeed as
740 * the get_gdb_reg_list() will lie and return a register list with
743 * This allows GDB monitor commands to be run from a GDB init script to
744 * initialize the target
746 * Also, since the halt() is asynchronous target connect will be
747 * instantaneous and thus avoiding annoying timeout problems during
750 target_halt(gdb_service
->target
);
751 /* FIX!!!! could extended-remote work better here?
753 * wait a tiny bit for halted state or we just continue. The
754 * GDB register packet will then contain garbage
756 target_wait_state(gdb_service
->target
, TARGET_HALTED
, 500);
758 /* remove the initial ACK from the incoming buffer */
759 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
762 /* FIX!!!??? would we actually ever receive a + here???
765 if (initial_ack
!= '+')
766 gdb_putback_char(connection
, initial_ack
);
767 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
771 int gdb_connection_closed(connection_t
*connection
)
773 gdb_service_t
*gdb_service
= connection
->service
->priv
;
774 gdb_connection_t
*gdb_connection
= connection
->priv
;
776 /* see if an image built with vFlash commands is left */
777 if (gdb_connection
->vflash_image
)
779 image_close(gdb_connection
->vflash_image
);
780 free(gdb_connection
->vflash_image
);
781 gdb_connection
->vflash_image
= NULL
;
784 /* if this connection registered a debug-message receiver delete it */
785 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
787 if (connection
->priv
)
789 free(connection
->priv
);
790 connection
->priv
= NULL
;
794 LOG_ERROR("BUG: connection->priv == NULL");
797 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
798 log_remove_callback(gdb_log_callback
, connection
);
800 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
804 void gdb_send_error(connection_t
*connection
, u8 the_error
)
807 snprintf(err
, 4, "E%2.2X", the_error
);
808 gdb_put_packet(connection
, err
, 3);
811 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
816 signal
= gdb_last_signal(target
);
818 snprintf(sig_reply
, 4, "S%2.2x", signal
);
819 gdb_put_packet(connection
, sig_reply
, 3);
824 /* Convert register to string of bits. NB! The # of bits in the
825 * register might be non-divisible by 8(a byte), in which
826 * case an entire byte is shown. */
827 void gdb_str_to_target(target_t
*target
, char *tstr
, reg_t
*reg
)
834 buf_len
= CEIL(reg
->size
, 8);
836 for (i
= 0; i
< buf_len
; i
++)
838 tstr
[i
*2] = DIGITS
[(buf
[i
]>>4) & 0xf];
839 tstr
[i
*2+1] = DIGITS
[buf
[i
]&0xf];
843 void gdb_target_to_str(target_t
*target
, char *tstr
, char *str
)
845 int str_len
= strlen(tstr
);
850 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
854 for (i
= 0; i
< str_len
; i
+=2)
856 str
[str_len
- i
- 1] = tstr
[i
+ 1];
857 str
[str_len
- i
- 2] = tstr
[i
];
861 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
866 int reg_packet_size
= 0;
871 #ifdef _DEBUG_GDB_IO_
875 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
877 return gdb_error(connection
, retval
);
880 for (i
= 0; i
< reg_list_size
; i
++)
882 reg_packet_size
+= reg_list
[i
]->size
;
885 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
886 reg_packet_p
= reg_packet
;
888 for (i
= 0; i
< reg_list_size
; i
++)
890 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
891 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
894 #ifdef _DEBUG_GDB_IO_
897 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
898 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
903 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
911 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
919 #ifdef _DEBUG_GDB_IO_
923 /* skip command character */
929 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
930 return ERROR_SERVER_REMOTE_CLOSED
;
933 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
935 return gdb_error(connection
, retval
);
939 for (i
= 0; i
< reg_list_size
; i
++)
943 reg_arch_type_t
*arch_type
;
945 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
946 hex_buf
= malloc(CEIL(reg_list
[i
]->size
, 8) * 2);
947 gdb_target_to_str(target
, packet_p
, hex_buf
);
949 /* convert hex-string to binary buffer */
950 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
951 str_to_buf(hex_buf
, CEIL(reg_list
[i
]->size
, 8) * 2, bin_buf
, reg_list
[i
]->size
, 16);
953 /* get register arch_type, and call set method */
954 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
956 arch_type
->set(reg_list
[i
], bin_buf
);
958 /* advance packet pointer */
959 packet_p
+= (CEIL(reg_list
[i
]->size
, 8) * 2);
965 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
968 gdb_put_packet(connection
, "OK", 2);
973 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
976 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
981 #ifdef _DEBUG_GDB_IO_
985 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
987 return gdb_error(connection
, retval
);
990 if (reg_list_size
<= reg_num
)
992 LOG_ERROR("gdb requested a non-existing register");
996 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
998 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1000 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1008 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1013 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1017 reg_arch_type_t
*arch_type
;
1021 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1023 return gdb_error(connection
, retval
);
1026 if (reg_list_size
< reg_num
)
1028 LOG_ERROR("gdb requested a non-existing register");
1029 return ERROR_SERVER_REMOTE_CLOSED
;
1032 if (*separator
!= '=')
1034 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1035 return ERROR_SERVER_REMOTE_CLOSED
;
1038 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1039 hex_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1040 gdb_target_to_str(target
, separator
+ 1, hex_buf
);
1042 /* convert hex-string to binary buffer */
1043 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
1044 str_to_buf(hex_buf
, CEIL(reg_list
[reg_num
]->size
, 8) * 2, bin_buf
, reg_list
[reg_num
]->size
, 16);
1046 /* get register arch_type, and call set method */
1047 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
1048 arch_type
->set(reg_list
[reg_num
], bin_buf
);
1050 gdb_put_packet(connection
, "OK", 2);
1059 int gdb_error(connection_t
*connection
, int retval
)
1063 case ERROR_TARGET_DATA_ABORT
:
1064 gdb_send_error(connection
, EIO
);
1066 case ERROR_TARGET_TRANSLATION_FAULT
:
1067 gdb_send_error(connection
, EFAULT
);
1069 case ERROR_TARGET_UNALIGNED_ACCESS
:
1070 gdb_send_error(connection
, EFAULT
);
1072 case ERROR_TARGET_NOT_HALTED
:
1073 gdb_send_error(connection
, EFAULT
);
1076 /* This could be that the target reset itself. */
1077 LOG_ERROR("unexpected error %i", retval
);
1078 gdb_send_error(connection
, EFAULT
);
1085 /* We don't have to worry about the default 2 second timeout for GDB packets,
1086 * because GDB breaks up large memory reads into smaller reads.
1088 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1090 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1099 int retval
= ERROR_OK
;
1101 /* skip command character */
1104 addr
= strtoul(packet
, &separator
, 16);
1106 if (*separator
!= ',')
1108 LOG_ERROR("incomplete read memory packet received, dropping connection");
1109 return ERROR_SERVER_REMOTE_CLOSED
;
1112 len
= strtoul(separator
+1, NULL
, 16);
1114 buffer
= malloc(len
);
1116 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1118 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1120 if ((retval
== ERROR_TARGET_DATA_ABORT
) && (!gdb_report_data_abort
))
1122 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1123 * At some point this might be fixed in GDB, in which case this code can be removed.
1125 * OpenOCD developers are acutely aware of this problem, but there is nothing
1126 * gained by involving the user in this problem that hopefully will get resolved
1129 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1131 * For now, the default is to fix up things to make current GDB versions work.
1132 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1134 memset(buffer
, 0, len
);
1138 if (retval
== ERROR_OK
)
1140 hex_buffer
= malloc(len
* 2 + 1);
1143 for (i
= 0; i
< len
; i
++)
1146 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1147 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1150 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1156 retval
= gdb_error(connection
, retval
);
1164 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1175 /* skip command character */
1178 addr
= strtoul(packet
, &separator
, 16);
1180 if (*separator
!= ',')
1182 LOG_ERROR("incomplete write memory packet received, dropping connection");
1183 return ERROR_SERVER_REMOTE_CLOSED
;
1186 len
= strtoul(separator
+1, &separator
, 16);
1188 if (*(separator
++) != ':')
1190 LOG_ERROR("incomplete write memory packet received, dropping connection");
1191 return ERROR_SERVER_REMOTE_CLOSED
;
1194 buffer
= malloc(len
);
1196 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1198 for (i
=0; i
<len
; i
++)
1201 sscanf(separator
+ 2*i
, "%2x", &tmp
);
1205 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1207 if (retval
== ERROR_OK
)
1209 gdb_put_packet(connection
, "OK", 2);
1213 retval
= gdb_error(connection
, retval
);
1221 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1229 /* skip command character */
1232 addr
= strtoul(packet
, &separator
, 16);
1234 if (*separator
!= ',')
1236 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1237 return ERROR_SERVER_REMOTE_CLOSED
;
1240 len
= strtoul(separator
+1, &separator
, 16);
1242 if (*(separator
++) != ':')
1244 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1245 return ERROR_SERVER_REMOTE_CLOSED
;
1251 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1253 retval
= target_write_buffer(target
, addr
, len
, (u8
*)separator
);
1256 if (retval
== ERROR_OK
)
1258 gdb_put_packet(connection
, "OK", 2);
1262 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1269 int gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1273 int retval
=ERROR_OK
;
1277 if (packet_size
> 1)
1279 packet
[packet_size
] = 0;
1280 address
= strtoul(packet
+ 1, NULL
, 16);
1287 if (packet
[0] == 'c')
1289 LOG_DEBUG("continue");
1290 target_handle_event( target
, TARGET_EVENT_OLD_pre_resume
);
1291 retval
=target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1293 else if (packet
[0] == 's')
1296 retval
=target
->type
->step(target
, current
, address
, 0); /* step at current or address, don't handle breakpoints */
1301 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1304 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1305 enum watchpoint_rw wp_type
;
1313 type
= strtoul(packet
+ 1, &separator
, 16);
1315 if (type
== 0) /* memory breakpoint */
1316 bp_type
= BKPT_SOFT
;
1317 else if (type
== 1) /* hardware breakpoint */
1318 bp_type
= BKPT_HARD
;
1319 else if (type
== 2) /* write watchpoint */
1320 wp_type
= WPT_WRITE
;
1321 else if (type
== 3) /* read watchpoint */
1323 else if (type
== 4) /* access watchpoint */
1324 wp_type
= WPT_ACCESS
;
1326 if (gdb_breakpoint_override
&&((bp_type
==BKPT_SOFT
)||(bp_type
==BKPT_HARD
)))
1328 bp_type
=gdb_breakpoint_override_type
;
1331 if (*separator
!= ',')
1333 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1334 return ERROR_SERVER_REMOTE_CLOSED
;
1337 address
= strtoul(separator
+1, &separator
, 16);
1339 if (*separator
!= ',')
1341 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1342 return ERROR_SERVER_REMOTE_CLOSED
;
1345 size
= strtoul(separator
+1, &separator
, 16);
1351 if (packet
[0] == 'Z')
1353 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1355 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1360 gdb_put_packet(connection
, "OK", 2);
1365 breakpoint_remove(target
, address
);
1366 gdb_put_packet(connection
, "OK", 2);
1373 if (packet
[0] == 'Z')
1375 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1377 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1382 gdb_put_packet(connection
, "OK", 2);
1387 watchpoint_remove(target
, address
);
1388 gdb_put_packet(connection
, "OK", 2);
1399 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1400 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1402 if (*retval
!= ERROR_OK
)
1410 if ((*xml
== NULL
) || (!first
))
1412 /* start by 0 to exercise all the code paths.
1413 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1415 *size
= *size
* 2 + 2;
1417 *xml
= realloc(*xml
, *size
);
1422 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1430 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1432 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1437 /* there was just enough or not enough space, allocate more. */
1442 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1446 /* Extract and NUL-terminate the annex. */
1448 while (*buf
&& *buf
!= ':')
1454 /* After the read marker and annex, qXfer looks like a
1455 * traditional 'm' packet. */
1457 *ofs
= strtoul(buf
, &separator
, 16);
1459 if (*separator
!= ',')
1462 *len
= strtoul(separator
+1, NULL
, 16);
1467 int gdb_calc_blocksize(flash_bank_t
*bank
)
1470 int block_size
= 0xffffffff;
1472 /* loop through all sectors and return smallest sector size */
1474 for (i
= 0; i
< bank
->num_sectors
; i
++)
1476 if (bank
->sectors
[i
].size
< block_size
)
1477 block_size
= bank
->sectors
[i
].size
;
1483 static int compare_bank (const void * a
, const void * b
)
1485 flash_bank_t
*b1
, *b2
;
1486 b1
=*((flash_bank_t
**)a
);
1487 b2
=*((flash_bank_t
**)b
);
1489 if (b1
->base
==b2
->base
)
1492 } else if (b1
->base
>b2
->base
)
1501 int gdb_query_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1503 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1504 gdb_connection_t
*gdb_connection
= connection
->priv
;
1506 if (strstr(packet
, "qRcmd,"))
1508 if (packet_size
> 6)
1512 cmd
= malloc((packet_size
- 6)/2 + 1);
1513 for (i
=0; i
< (packet_size
- 6)/2; i
++)
1516 sscanf(packet
+ 6 + 2*i
, "%2x", &tmp
);
1519 cmd
[(packet_size
- 6)/2] = 0x0;
1521 /* We want to print all debug output to GDB connection */
1522 log_add_callback(gdb_log_callback
, connection
);
1523 target_call_timer_callbacks_now();
1524 command_run_line(cmd_ctx
, cmd
);
1525 target_call_timer_callbacks_now();
1526 log_remove_callback(gdb_log_callback
, connection
);
1529 gdb_put_packet(connection
, "OK", 2);
1532 else if (strstr(packet
, "qCRC:"))
1534 if (packet_size
> 5)
1543 /* skip command character */
1546 addr
= strtoul(packet
, &separator
, 16);
1548 if (*separator
!= ',')
1550 LOG_ERROR("incomplete read memory packet received, dropping connection");
1551 return ERROR_SERVER_REMOTE_CLOSED
;
1554 len
= strtoul(separator
+ 1, NULL
, 16);
1556 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1558 if (retval
== ERROR_OK
)
1560 snprintf(gdb_reply
, 10, "C%8.8x", checksum
);
1561 gdb_put_packet(connection
, gdb_reply
, 9);
1565 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1572 else if (strstr(packet
, "qSupported"))
1574 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1575 * disable qXfer:features:read for the moment */
1576 int retval
= ERROR_OK
;
1577 char *buffer
= NULL
;
1581 xml_printf(&retval
, &buffer
, &pos
, &size
,
1582 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1583 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1585 if (retval
!= ERROR_OK
)
1587 gdb_send_error(connection
, 01);
1591 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1596 else if (strstr(packet
, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1598 /* We get away with only specifying flash here. Regions that are not
1599 * specified are treated as if we provided no memory map(if not we
1600 * could detect the holes and mark them as RAM).
1601 * Normally we only execute this code once, but no big deal if we
1602 * have to regenerate it a couple of times. */
1608 int retval
= ERROR_OK
;
1615 /* skip command character */
1618 offset
= strtoul(packet
, &separator
, 16);
1619 length
= strtoul(separator
+ 1, &separator
, 16);
1621 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1624 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1625 read/write) by default for GDB.
1626 GDB does not have a concept of non-cacheable read/write memory.
1628 flash_bank_t
**banks
=malloc(sizeof(flash_bank_t
*)*flash_get_bank_count());
1631 for (i
=0; i
<flash_get_bank_count(); i
++)
1633 p
= get_flash_bank_by_num(i
);
1637 retval
= ERROR_FAIL
;
1638 gdb_send_error(connection
, retval
);
1644 qsort(banks
, flash_get_bank_count(), sizeof(flash_bank_t
*), compare_bank
);
1647 for (i
=0; i
<flash_get_bank_count(); i
++)
1651 if (ram_start
<p
->base
)
1653 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1654 ram_start
, p
->base
-ram_start
);
1657 /* if device has uneven sector sizes, eg. str7, lpc
1658 * we pass the smallest sector size to gdb memory map */
1659 blocksize
= gdb_calc_blocksize(p
);
1661 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1662 "<property name=\"blocksize\">0x%x</property>\n" \
1664 p
->base
, p
->size
, blocksize
);
1665 ram_start
=p
->base
+p
->size
;
1669 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1670 ram_start
, 0-ram_start
);
1673 /* a flash chip could be at the very end of the 32 bit address space, in which case
1674 ram_start will be precisely 0 */
1680 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1682 if (retval
!= ERROR_OK
)
1684 gdb_send_error(connection
, retval
);
1688 if (offset
+ length
> pos
)
1690 length
= pos
- offset
;
1693 char *t
= malloc(length
+ 1);
1695 memcpy(t
+ 1, xml
+ offset
, length
);
1696 gdb_put_packet(connection
, t
, length
+ 1);
1702 else if (strstr(packet
, "qXfer:features:read:"))
1707 int retval
= ERROR_OK
;
1710 unsigned int length
;
1713 /* skip command character */
1716 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1718 gdb_send_error(connection
, 01);
1722 if (strcmp(annex
, "target.xml") != 0)
1724 gdb_send_error(connection
, 01);
1728 xml_printf(&retval
, &xml
, &pos
, &size
, \
1729 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1731 if (retval
!= ERROR_OK
)
1733 gdb_send_error(connection
, retval
);
1737 gdb_put_packet(connection
, xml
, strlen(xml
));
1742 else if (strstr(packet
, "QStartNoAckMode"))
1744 gdb_connection
->noack_mode
= 1;
1745 gdb_put_packet(connection
, "OK", 2);
1749 gdb_put_packet(connection
, "", 0);
1753 int gdb_v_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1755 gdb_connection_t
*gdb_connection
= connection
->priv
;
1756 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1759 /* if flash programming disabled - send a empty reply */
1761 if (gdb_flash_program
== 0)
1763 gdb_put_packet(connection
, "", 0);
1767 if (strstr(packet
, "vFlashErase:"))
1770 unsigned long length
;
1772 char *parse
= packet
+ 12;
1775 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1776 return ERROR_SERVER_REMOTE_CLOSED
;
1779 addr
= strtoul(parse
, &parse
, 16);
1781 if (*(parse
++) != ',' || *parse
== '\0')
1783 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1784 return ERROR_SERVER_REMOTE_CLOSED
;
1787 length
= strtoul(parse
, &parse
, 16);
1791 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1792 return ERROR_SERVER_REMOTE_CLOSED
;
1795 /* assume all sectors need erasing - stops any problems
1796 * when flash_write is called multiple times */
1799 /* perform any target specific operations before the erase */
1800 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_START
);
1801 result
= flash_erase_address_range(gdb_service
->target
, addr
, length
);
1802 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_END
);
1805 if (result
!= ERROR_OK
)
1807 /* GDB doesn't evaluate the actual error number returned,
1808 * treat a failed erase as an I/O error
1810 gdb_send_error(connection
, EIO
);
1811 LOG_ERROR("flash_erase returned %i", result
);
1814 gdb_put_packet(connection
, "OK", 2);
1819 if (strstr(packet
, "vFlashWrite:"))
1822 unsigned long length
;
1823 char *parse
= packet
+ 12;
1827 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1828 return ERROR_SERVER_REMOTE_CLOSED
;
1830 addr
= strtoul(parse
, &parse
, 16);
1831 if (*(parse
++) != ':')
1833 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1834 return ERROR_SERVER_REMOTE_CLOSED
;
1836 length
= packet_size
- (parse
- packet
);
1838 /* create a new image if there isn't already one */
1839 if (gdb_connection
->vflash_image
== NULL
)
1841 gdb_connection
->vflash_image
= malloc(sizeof(image_t
));
1842 image_open(gdb_connection
->vflash_image
, "", "build");
1845 /* create new section with content from packet buffer */
1846 image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (u8
*)parse
);
1848 gdb_put_packet(connection
, "OK", 2);
1853 if (!strcmp(packet
, "vFlashDone"))
1857 /* process the flashing buffer. No need to erase as GDB
1858 * always issues a vFlashErase first. */
1859 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
1860 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1861 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1862 if ( result
!= ERROR_OK
)
1864 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1865 gdb_put_packet(connection
, "E.memtype", 9);
1867 gdb_send_error(connection
, EIO
);
1871 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written
);
1872 gdb_put_packet(connection
, "OK", 2);
1875 image_close(gdb_connection
->vflash_image
);
1876 free(gdb_connection
->vflash_image
);
1877 gdb_connection
->vflash_image
= NULL
;
1882 gdb_put_packet(connection
, "", 0);
1886 int gdb_detach(connection_t
*connection
, target_t
*target
)
1889 switch( detach_mode
)
1891 case GDB_DETACH_RESUME
:
1892 target_handle_event( target
, TARGET_EVENT_OLD_pre_resume
);
1893 target_resume(target
, 1, 0, 1, 0);
1896 case GDB_DETACH_RESET
:
1897 /* FIX?? make this configurable?? */
1898 target_process_reset(connection
->cmd_ctx
, RESET_HALT
);
1901 case GDB_DETACH_HALT
:
1902 target_halt(target
);
1905 case GDB_DETACH_NOTHING
:
1909 gdb_put_packet(connection
, "OK", 2);
1913 static void gdb_log_callback(void *priv
, const char *file
, int line
,
1914 const char *function
, const char *string
)
1916 connection_t
*connection
= priv
;
1917 gdb_connection_t
*gdb_con
= connection
->priv
;
1921 /* do not reply this using the O packet */
1925 gdb_output_con(connection
, string
);
1928 /* Do not allocate this on the stack */
1929 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
1931 static void gdb_sig_halted(connection_t
*connection
)
1934 snprintf(sig_reply
, 4, "T%2.2x", 2);
1935 gdb_put_packet(connection
, sig_reply
, 3);
1939 int gdb_input_inner(connection_t
*connection
)
1941 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1942 target_t
*target
= gdb_service
->target
;
1943 char *packet
=gdb_packet_buffer
;
1946 gdb_connection_t
*gdb_con
= connection
->priv
;
1947 static int extended_protocol
= 0;
1949 /* drain input buffer */
1952 packet_size
= GDB_BUFFER_SIZE
-1;
1953 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
1958 /* terminate with zero */
1959 packet
[packet_size
] = 0;
1961 if( LOG_LEVEL_IS( LOG_LVL_DEBUG
) ){
1962 if( packet
[0] == 'X' ){
1963 // binary packets spew junk into the debug log stream
1966 for( x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++ ){
1970 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
1972 LOG_DEBUG("received packet: '%s'", packet
);
1976 if (packet_size
> 0)
1982 /* Hct... -- set thread
1983 * we don't have threads, send empty reply */
1984 gdb_put_packet(connection
, NULL
, 0);
1988 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
1991 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
1994 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
1997 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
2000 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
2003 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
2006 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
2010 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2013 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
2018 if (target
->state
!= TARGET_HALTED
)
2020 /* If the target isn't in the halted state, then we can't
2021 * step/continue. This might be early setup, etc.
2023 gdb_sig_halted(connection
);
2026 /* We're running/stepping, in which case we can
2027 * forward log output until the target is halted
2029 gdb_connection_t
*gdb_con
= connection
->priv
;
2030 gdb_con
->frontend_state
= TARGET_RUNNING
;
2031 log_add_callback(gdb_log_callback
, connection
);
2032 int retval
=gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2033 if (retval
!=ERROR_OK
)
2035 /* we'll never receive a halted condition... issue a false one.. */
2036 gdb_frontend_halted(target
, connection
);
2042 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
2045 retval
= gdb_detach(connection
, target
);
2046 extended_protocol
= 0;
2049 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
2053 if (extended_protocol
!= 0)
2055 gdb_put_packet(connection
, "OK", 2);
2056 return ERROR_SERVER_REMOTE_CLOSED
;
2058 /* handle extended remote protocol */
2059 extended_protocol
= 1;
2060 gdb_put_packet(connection
, "OK", 2);
2063 /* handle extended restart packet */
2064 breakpoint_clear_target(gdb_service
->target
);
2065 watchpoint_clear_target(gdb_service
->target
);
2066 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %d", get_num_by_target(target
));
2069 /* ignore unkown packets */
2070 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2071 gdb_put_packet(connection
, NULL
, 0);
2075 /* if a packet handler returned an error, exit input loop */
2076 if (retval
!= ERROR_OK
)
2080 if (gdb_con
->ctrl_c
)
2082 if (target
->state
== TARGET_RUNNING
)
2084 target_halt(target
);
2085 gdb_con
->ctrl_c
= 0;
2089 } while (gdb_con
->buf_cnt
> 0);
2094 int gdb_input(connection_t
*connection
)
2096 int retval
= gdb_input_inner(connection
);
2097 gdb_connection_t
*gdb_con
= connection
->priv
;
2098 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2101 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2102 if (gdb_con
->closed
)
2103 return ERROR_SERVER_REMOTE_CLOSED
;
2105 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2111 gdb_service_t
*gdb_service
;
2112 target_t
*target
= all_targets
;
2116 LOG_WARNING("no gdb ports allocated as no target has been specified");
2122 LOG_WARNING("no gdb port specified, using default port 3333");
2128 char service_name
[8];
2130 snprintf(service_name
, 8, "gdb-%2.2i", target
->target_number
);
2132 gdb_service
= malloc(sizeof(gdb_service_t
));
2133 gdb_service
->target
= target
;
2135 add_service("gdb", CONNECTION_GDB
,
2136 gdb_port
+ target
->target_number
,
2137 1, gdb_new_connection
, gdb_input
,
2138 gdb_connection_closed
,
2141 LOG_DEBUG("gdb service for target %s at port %i",
2143 gdb_port
+ target
->target_number
);
2145 target
= target
->next
;
2151 /* daemon configuration command gdb_port */
2152 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2157 /* only if the port wasn't overwritten by cmdline */
2159 gdb_port
= strtoul(args
[0], NULL
, 0);
2164 int handle_gdb_detach_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2168 if (strcmp(args
[0], "resume") == 0)
2170 detach_mode
= GDB_DETACH_RESUME
;
2173 else if (strcmp(args
[0], "reset") == 0)
2175 detach_mode
= GDB_DETACH_RESET
;
2178 else if (strcmp(args
[0], "halt") == 0)
2180 detach_mode
= GDB_DETACH_HALT
;
2183 else if (strcmp(args
[0], "nothing") == 0)
2185 detach_mode
= GDB_DETACH_NOTHING
;
2190 LOG_WARNING("invalid gdb_detach configuration directive: %s", args
[0]);
2194 int handle_gdb_memory_map_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2198 if (strcmp(args
[0], "enable") == 0)
2200 gdb_use_memory_map
= 1;
2203 else if (strcmp(args
[0], "disable") == 0)
2205 gdb_use_memory_map
= 0;
2210 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args
[0]);
2214 int handle_gdb_flash_program_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2218 if (strcmp(args
[0], "enable") == 0)
2220 gdb_flash_program
= 1;
2223 else if (strcmp(args
[0], "disable") == 0)
2225 gdb_flash_program
= 0;
2230 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args
[0]);
2234 int handle_gdb_report_data_abort_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2238 if (strcmp(args
[0], "enable") == 0)
2240 gdb_report_data_abort
= 1;
2243 else if (strcmp(args
[0], "disable") == 0)
2245 gdb_report_data_abort
= 0;
2250 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args
[0]);
2254 /* daemon configuration command gdb_port */
2255 int handle_gdb_breakpoint_override_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2262 gdb_breakpoint_override
= 1;
2263 if (strcmp(args
[0], "hard")==0)
2265 gdb_breakpoint_override_type
=BKPT_HARD
;
2266 } else if (strcmp(args
[0], "soft")==0)
2268 gdb_breakpoint_override_type
=BKPT_SOFT
;
2269 } else if (strcmp(args
[0], "disable") == 0)
2271 gdb_breakpoint_override
= 0;
2275 return ERROR_COMMAND_SYNTAX_ERROR
;
2277 if (gdb_breakpoint_override
)
2279 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
==BKPT_HARD
)?"hard":"soft");
2282 LOG_USER("breakpoint type is not overriden");
2289 int gdb_register_commands(command_context_t
*command_context
)
2291 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
2292 COMMAND_CONFIG
, "");
2293 register_command(command_context
, NULL
, "gdb_detach", handle_gdb_detach_command
,
2294 COMMAND_CONFIG
, "");
2295 register_command(command_context
, NULL
, "gdb_memory_map", handle_gdb_memory_map_command
,
2296 COMMAND_CONFIG
, "");
2297 register_command(command_context
, NULL
, "gdb_flash_program", handle_gdb_flash_program_command
,
2298 COMMAND_CONFIG
, "");
2299 register_command(command_context
, NULL
, "gdb_report_data_abort", handle_gdb_report_data_abort_command
,
2300 COMMAND_CONFIG
, "");
2301 register_command(command_context
, NULL
, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command
,
2302 COMMAND_EXEC
, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2303 "The raison d'etre for this option is to support GDB GUI's without "
2304 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2305 "is not sufficient");
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)