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"
41 #include "target_request.h"
42 #include "configuration.h"
50 #define _DEBUG_GDB_IO_
53 static int gdb_breakpoint_override
;
54 static enum breakpoint_type gdb_breakpoint_override_type
;
56 extern int gdb_error(connection_t
*connection
, int retval
);
57 static unsigned short gdb_port
;
58 static const char *DIGITS
= "0123456789abcdef";
60 static void gdb_log_callback(void *priv
, const char *file
, int line
,
61 const char *function
, const char *string
);
71 /* target behaviour on gdb detach */
72 enum gdb_detach_mode detach_mode
= GDB_DETACH_RESUME
;
74 /* set if we are sending a memory map to gdb
75 * via qXfer:memory-map:read packet */
76 /* enabled by default*/
77 int gdb_use_memory_map
= 1;
78 /* enabled by default*/
79 int gdb_flash_program
= 1;
81 /* if set, data aborts cause an error to be reported in memory read packets
82 * see the code in gdb_read_memory_packet() for further explanations */
83 int gdb_report_data_abort
= 0;
85 int gdb_last_signal(target_t
*target
)
87 switch (target
->debug_reason
)
89 case DBG_REASON_DBGRQ
:
90 return 0x2; /* SIGINT */
91 case DBG_REASON_BREAKPOINT
:
92 case DBG_REASON_WATCHPOINT
:
93 case DBG_REASON_WPTANDBKPT
:
94 return 0x05; /* SIGTRAP */
95 case DBG_REASON_SINGLESTEP
:
96 return 0x05; /* SIGTRAP */
97 case DBG_REASON_NOTHALTED
:
98 return 0x0; /* no signal... shouldn't happen */
100 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
105 int check_pending(connection_t
*connection
, int timeout_s
, int *got_data
)
107 /* a non-blocking socket will block if there is 0 bytes available on the socket,
108 * but return with as many bytes as are available immediately
112 gdb_connection_t
*gdb_con
= connection
->priv
;
118 if (gdb_con
->buf_cnt
>0)
125 FD_SET(connection
->fd
, &read_fds
);
127 tv
.tv_sec
= timeout_s
;
129 if (select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
131 /* This can typically be because a "monitor" command took too long
132 * before printing any progress messages
136 return ERROR_GDB_TIMEOUT
;
142 *got_data
=FD_ISSET(connection
->fd
, &read_fds
)!=0;
146 int gdb_get_char(connection_t
*connection
, int* next_char
)
148 gdb_connection_t
*gdb_con
= connection
->priv
;
151 #ifdef _DEBUG_GDB_IO_
155 if (gdb_con
->buf_cnt
-- > 0)
157 *next_char
= *(gdb_con
->buf_p
++);
158 if (gdb_con
->buf_cnt
> 0)
159 connection
->input_pending
= 1;
161 connection
->input_pending
= 0;
163 #ifdef _DEBUG_GDB_IO_
164 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
172 retval
=check_pending(connection
, 1, NULL
);
173 if (retval
!=ERROR_OK
)
175 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
176 if (gdb_con
->buf_cnt
> 0)
180 if (gdb_con
->buf_cnt
== 0)
183 return ERROR_SERVER_REMOTE_CLOSED
;
187 errno
= WSAGetLastError();
194 case WSAECONNABORTED
:
196 return ERROR_SERVER_REMOTE_CLOSED
;
199 return ERROR_SERVER_REMOTE_CLOSED
;
201 LOG_ERROR("read: %d", errno
);
212 return ERROR_SERVER_REMOTE_CLOSED
;
215 return ERROR_SERVER_REMOTE_CLOSED
;
217 LOG_ERROR("read: %s", strerror(errno
));
219 return ERROR_SERVER_REMOTE_CLOSED
;
224 #ifdef _DEBUG_GDB_IO_
225 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
226 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
227 debug_buffer
[gdb_con
->buf_cnt
] = 0;
228 LOG_DEBUG("received '%s'", debug_buffer
);
232 gdb_con
->buf_p
= gdb_con
->buffer
;
234 *next_char
= *(gdb_con
->buf_p
++);
235 if (gdb_con
->buf_cnt
> 0)
236 connection
->input_pending
= 1;
238 connection
->input_pending
= 0;
239 #ifdef _DEBUG_GDB_IO_
240 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
246 int gdb_putback_char(connection_t
*connection
, int last_char
)
248 gdb_connection_t
*gdb_con
= connection
->priv
;
250 if (gdb_con
->buf_p
> gdb_con
->buffer
)
252 *(--gdb_con
->buf_p
) = last_char
;
257 LOG_ERROR("BUG: couldn't put character back");
263 /* The only way we can detect that the socket is closed is the first time
264 * we write to it, we will fail. Subsequent write operations will
265 * succeed. Shudder! */
266 int gdb_write(connection_t
*connection
, void *data
, int len
)
268 gdb_connection_t
*gdb_con
= connection
->priv
;
270 return ERROR_SERVER_REMOTE_CLOSED
;
272 if (write_socket(connection
->fd
, data
, len
) == len
)
277 return ERROR_SERVER_REMOTE_CLOSED
;
280 int gdb_put_packet_inner(connection_t
*connection
, char *buffer
, int len
)
283 unsigned char my_checksum
= 0;
284 #ifdef _DEBUG_GDB_IO_
289 gdb_connection_t
*gdb_con
= connection
->priv
;
291 for (i
= 0; i
< len
; i
++)
292 my_checksum
+= buffer
[i
];
294 #ifdef _DEBUG_GDB_IO_
296 * At this point we should have nothing in the input queue from GDB,
297 * however sometimes '-' is sent even though we've already received
298 * an ACK (+) for everything we've sent off.
303 if ((retval
=check_pending(connection
, 0, &gotdata
))!=ERROR_OK
)
307 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
310 // fix a problem with some IAR tools
311 gdb_putback_char( connection
, reply
);
312 LOG_DEBUG("Unexpected start of new packet");
316 LOG_WARNING("Discard unexpected char %c", reply
);
322 #ifdef _DEBUG_GDB_IO_
323 debug_buffer
= malloc(len
+ 1);
324 memcpy(debug_buffer
, buffer
, len
);
325 debug_buffer
[len
] = 0;
326 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
330 char local_buffer
[1024];
331 local_buffer
[0] = '$';
332 if (len
+4 <= sizeof(local_buffer
))
334 /* performance gain on smaller packets by only a single call to gdb_write() */
335 memcpy(local_buffer
+1, buffer
, len
++);
336 local_buffer
[len
++] = '#';
337 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
338 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
339 if((retval
= gdb_write(connection
, local_buffer
, len
)) != ERROR_OK
)
346 /* larger packets are transmitted directly from caller supplied buffer
347 by several calls to gdb_write() to avoid dynamic allocation */
348 local_buffer
[1] = '#';
349 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
350 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
351 if((retval
= gdb_write(connection
, local_buffer
, 1)) != ERROR_OK
)
355 if((retval
= gdb_write(connection
, buffer
, len
)) != ERROR_OK
)
359 if((retval
= gdb_write(connection
, local_buffer
+1, 3)) != ERROR_OK
)
365 if (gdb_con
->noack_mode
)
368 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
== 0x3)
382 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
386 else if (reply
== '-')
388 /* Stop sending output packets for now */
389 log_remove_callback(gdb_log_callback
, connection
);
390 LOG_WARNING("negative reply, retrying");
392 else if( reply
== '$' ){
393 LOG_ERROR("GDB missing ack(1) - assumed good");
394 gdb_putback_char( connection
, reply
);
398 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply
);
400 return ERROR_SERVER_REMOTE_CLOSED
;
403 else if( reply
== '$' ){
404 LOG_ERROR("GDB missing ack(2) - assumed good");
405 gdb_putback_char( connection
, reply
);
410 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply
);
412 return ERROR_SERVER_REMOTE_CLOSED
;
416 return ERROR_SERVER_REMOTE_CLOSED
;
421 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
423 gdb_connection_t
*gdb_con
= connection
->priv
;
425 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
428 /* we sent some data, reset timer for keep alive messages */
434 static __inline__
int fetch_packet(connection_t
*connection
, int *checksum_ok
, int noack
, int *len
, char *buffer
)
436 unsigned char my_checksum
= 0;
441 gdb_connection_t
*gdb_con
= connection
->priv
;
447 /* The common case is that we have an entire packet with no escape chars.
448 * We need to leave at least 2 bytes in the buffer to have
449 * gdb_get_char() update various bits and bobs correctly.
451 if ((gdb_con
->buf_cnt
> 2) && ((gdb_con
->buf_cnt
+count
) < *len
))
453 /* The compiler will struggle a bit with constant propagation and
454 * aliasing, so we help it by showing that these values do not
455 * change inside the loop
458 char *buf
= gdb_con
->buf_p
;
459 int run
= gdb_con
->buf_cnt
- 2;
466 if (character
== '#')
468 /* Danger! character can be '#' when esc is
469 * used so we need an explicit boolean for done here.
475 if (character
== '}')
477 /* data transmitted in binary mode (X packet)
478 * uses 0x7d as escape character */
479 my_checksum
+= character
& 0xff;
482 my_checksum
+= character
& 0xff;
483 buffer
[count
++] = (character
^ 0x20) & 0xff;
487 my_checksum
+= character
& 0xff;
488 buffer
[count
++] = character
& 0xff;
492 gdb_con
->buf_cnt
-= i
;
498 LOG_ERROR("packet buffer too small");
499 return ERROR_GDB_BUFFER_TOO_SMALL
;
502 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
505 if (character
== '#')
508 if (character
== '}')
510 /* data transmitted in binary mode (X packet)
511 * uses 0x7d as escape character */
512 my_checksum
+= character
& 0xff;
513 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
515 my_checksum
+= character
& 0xff;
516 buffer
[count
++] = (character
^ 0x20) & 0xff;
520 my_checksum
+= character
& 0xff;
521 buffer
[count
++] = character
& 0xff;
527 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
529 checksum
[0] = character
;
530 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
532 checksum
[1] = character
;
537 *checksum_ok
=(my_checksum
== strtoul(checksum
, NULL
, 16));
543 int gdb_get_packet_inner(connection_t
*connection
, char *buffer
, int *len
)
547 gdb_connection_t
*gdb_con
= connection
->priv
;
553 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
556 #ifdef _DEBUG_GDB_IO_
557 LOG_DEBUG("character: '%c'", character
);
565 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
566 * incase anyone tries to debug why they receive this warning every time */
567 LOG_WARNING("acknowledgment received, but no packet pending");
570 LOG_WARNING("negative acknowledgment, but no packet pending");
577 LOG_WARNING("ignoring character 0x%x", character
);
580 } while (character
!= '$');
585 /* explicit code expansion here to get faster inlined code in -O3 by not
586 * calculating checksum
588 if (gdb_con
->noack_mode
)
590 if ((retval
=fetch_packet(connection
, &checksum_ok
, 1, len
, buffer
))!=ERROR_OK
)
594 if ((retval
=fetch_packet(connection
, &checksum_ok
, 0, len
, buffer
))!=ERROR_OK
)
598 if (gdb_con
->noack_mode
)
600 /* checksum is not checked in noack mode */
605 if ((retval
= gdb_write(connection
, "+", 1)) != ERROR_OK
)
613 return ERROR_SERVER_REMOTE_CLOSED
;
618 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
620 gdb_connection_t
*gdb_con
= connection
->priv
;
622 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
627 int gdb_output_con(connection_t
*connection
, const char* line
)
632 bin_size
= strlen(line
);
634 hex_buffer
= malloc(bin_size
*2 + 2);
635 if (hex_buffer
== NULL
)
636 return ERROR_GDB_BUFFER_TOO_SMALL
;
639 for (i
=0; i
<bin_size
; i
++)
640 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
641 hex_buffer
[bin_size
*2+1] = 0;
643 int retval
= gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
649 int gdb_output(struct command_context_s
*context
, const char* line
)
651 /* this will be dumped to the log and also sent as an O packet if possible */
652 LOG_USER_N("%s", line
);
657 static void gdb_frontend_halted(struct target_s
*target
, connection_t
*connection
)
659 gdb_connection_t
*gdb_connection
= connection
->priv
;
661 /* In the GDB protocol when we are stepping or coninuing execution,
662 * we have a lingering reply. Upon receiving a halted event
663 * when we have that lingering packet, we reply to the original
664 * step or continue packet.
666 * Executing monitor commands can bring the target in and
667 * out of the running state so we'll see lots of TARGET_EVENT_XXX
668 * that are to be ignored.
670 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
675 /* stop forwarding log packets! */
676 log_remove_callback(gdb_log_callback
, connection
);
678 if (gdb_connection
->ctrl_c
)
681 gdb_connection
->ctrl_c
= 0;
685 signal
= gdb_last_signal(target
);
688 snprintf(sig_reply
, 4, "T%2.2x", signal
);
689 gdb_put_packet(connection
, sig_reply
, 3);
690 gdb_connection
->frontend_state
= TARGET_HALTED
;
694 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
697 connection_t
*connection
= priv
;
699 target_handle_event( target
, event
);
702 case TARGET_EVENT_EARLY_HALTED
:
703 gdb_frontend_halted(target
, connection
);
705 case TARGET_EVENT_HALTED
:
706 target_call_event_callbacks(target
, TARGET_EVENT_GDB_END
);
708 case TARGET_EVENT_GDB_FLASH_ERASE_START
:
709 target_handle_event( target
, TARGET_EVENT_OLD_gdb_program_config
);
710 if((retval
= jtag_execute_queue()) != ERROR_OK
)
723 int gdb_new_connection(connection_t
*connection
)
725 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
726 gdb_service_t
*gdb_service
= connection
->service
->priv
;
730 connection
->priv
= gdb_connection
;
732 /* initialize gdb connection information */
733 gdb_connection
->buf_p
= gdb_connection
->buffer
;
734 gdb_connection
->buf_cnt
= 0;
735 gdb_connection
->ctrl_c
= 0;
736 gdb_connection
->frontend_state
= TARGET_HALTED
;
737 gdb_connection
->vflash_image
= NULL
;
738 gdb_connection
->closed
= 0;
739 gdb_connection
->busy
= 0;
740 gdb_connection
->noack_mode
= 0;
742 /* send ACK to GDB for debug request */
743 gdb_write(connection
, "+", 1);
745 /* output goes through gdb connection */
746 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
748 /* we must remove all breakpoints registered to the target as a previous
749 * GDB session could leave dangling breakpoints if e.g. communication
752 breakpoint_clear_target(gdb_service
->target
);
753 watchpoint_clear_target(gdb_service
->target
);
755 /* register callback to be informed about target events */
756 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
758 /* a gdb session just attached, try to put the target in halt mode.
762 * If the halt fails(e.g. target needs a reset, JTAG communication not
763 * working, etc.), then the GDB connect will succeed as
764 * the get_gdb_reg_list() will lie and return a register list with
767 * This allows GDB monitor commands to be run from a GDB init script to
768 * initialize the target
770 * Also, since the halt() is asynchronous target connect will be
771 * instantaneous and thus avoiding annoying timeout problems during
774 target_halt(gdb_service
->target
);
775 /* FIX!!!! could extended-remote work better here?
777 * wait a tiny bit for halted state or we just continue. The
778 * GDB register packet will then contain garbage
780 target_wait_state(gdb_service
->target
, TARGET_HALTED
, 500);
782 /* remove the initial ACK from the incoming buffer */
783 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
786 /* FIX!!!??? would we actually ever receive a + here???
789 if (initial_ack
!= '+')
790 gdb_putback_char(connection
, initial_ack
);
791 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_ATTACH
);
795 int gdb_connection_closed(connection_t
*connection
)
797 gdb_service_t
*gdb_service
= connection
->service
->priv
;
798 gdb_connection_t
*gdb_connection
= connection
->priv
;
800 /* see if an image built with vFlash commands is left */
801 if (gdb_connection
->vflash_image
)
803 image_close(gdb_connection
->vflash_image
);
804 free(gdb_connection
->vflash_image
);
805 gdb_connection
->vflash_image
= NULL
;
808 /* if this connection registered a debug-message receiver delete it */
809 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
811 if (connection
->priv
)
813 free(connection
->priv
);
814 connection
->priv
= NULL
;
818 LOG_ERROR("BUG: connection->priv == NULL");
821 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
822 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_END
);
823 log_remove_callback(gdb_log_callback
, connection
);
825 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_DETACH
);
830 void gdb_send_error(connection_t
*connection
, u8 the_error
)
833 snprintf(err
, 4, "E%2.2X", the_error
);
834 gdb_put_packet(connection
, err
, 3);
837 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
842 signal
= gdb_last_signal(target
);
844 snprintf(sig_reply
, 4, "S%2.2x", signal
);
845 gdb_put_packet(connection
, sig_reply
, 3);
851 static int gdb_reg_pos(target_t
*target
, int pos
, int len
)
853 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
856 return len
- 1 - pos
;
859 /* Convert register to string of bytes. NB! The # of bits in the
860 * register might be non-divisible by 8(a byte), in which
861 * case an entire byte is shown.
863 * NB! the format on the wire is the target endianess
865 * The format of reg->value is little endian
868 void gdb_str_to_target(target_t
*target
, char *tstr
, reg_t
*reg
)
875 buf_len
= CEIL(reg
->size
, 8);
877 for (i
= 0; i
< buf_len
; i
++)
879 int j
= gdb_reg_pos(target
, i
, buf_len
);
880 tstr
[i
*2] = DIGITS
[(buf
[j
]>>4) & 0xf];
881 tstr
[i
*2+1] = DIGITS
[buf
[j
]&0xf];
885 static int hextoint(char c
)
896 LOG_ERROR("BUG: invalid register value %08x", c
);
900 /* copy over in register buffer */
901 void gdb_target_to_reg(target_t
*target
, char *tstr
, int str_len
, u8
*bin
)
905 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
910 for (i
= 0; i
< str_len
; i
+=2)
912 u8 t
= hextoint(tstr
[i
])<<4;
913 t
|= hextoint(tstr
[i
+1]);
915 int j
= gdb_reg_pos(target
, i
/2, str_len
/2);
920 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
925 int reg_packet_size
= 0;
930 #ifdef _DEBUG_GDB_IO_
934 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
936 return gdb_error(connection
, retval
);
939 for (i
= 0; i
< reg_list_size
; i
++)
941 reg_packet_size
+= reg_list
[i
]->size
;
944 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
945 reg_packet_p
= reg_packet
;
947 for (i
= 0; i
< reg_list_size
; i
++)
949 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
950 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
953 #ifdef _DEBUG_GDB_IO_
956 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
957 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
962 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
970 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
978 #ifdef _DEBUG_GDB_IO_
982 /* skip command character */
988 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
989 return ERROR_SERVER_REMOTE_CLOSED
;
992 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
994 return gdb_error(connection
, retval
);
998 for (i
= 0; i
< reg_list_size
; i
++)
1001 int chars
= (CEIL(reg_list
[i
]->size
, 8) * 2);
1003 if (packet_p
+ chars
> packet
+ packet_size
)
1005 LOG_ERROR("BUG: register packet is too small for registers");
1008 reg_arch_type_t
*arch_type
;
1009 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
1010 gdb_target_to_reg(target
, packet_p
, chars
, bin_buf
);
1012 /* get register arch_type, and call set method */
1013 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
1015 arch_type
->set(reg_list
[i
], bin_buf
);
1017 /* advance packet pointer */
1024 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
1027 gdb_put_packet(connection
, "OK", 2);
1032 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1035 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
1040 #ifdef _DEBUG_GDB_IO_
1044 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1046 return gdb_error(connection
, retval
);
1049 if (reg_list_size
<= reg_num
)
1051 LOG_ERROR("gdb requested a non-existing register");
1055 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1057 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
1059 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1067 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1071 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
1075 reg_arch_type_t
*arch_type
;
1079 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
1081 return gdb_error(connection
, retval
);
1084 if (reg_list_size
< reg_num
)
1086 LOG_ERROR("gdb requested a non-existing register");
1087 return ERROR_SERVER_REMOTE_CLOSED
;
1090 if (*separator
!= '=')
1092 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1093 return ERROR_SERVER_REMOTE_CLOSED
;
1096 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1097 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
1098 int chars
= (CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1100 /* fix!!! add some sanity checks on packet size here */
1102 gdb_target_to_reg(target
, separator
+ 1, chars
, bin_buf
);
1104 /* get register arch_type, and call set method */
1105 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
1106 arch_type
->set(reg_list
[reg_num
], bin_buf
);
1108 gdb_put_packet(connection
, "OK", 2);
1116 int gdb_error(connection_t
*connection
, int retval
)
1120 case ERROR_TARGET_DATA_ABORT
:
1121 gdb_send_error(connection
, EIO
);
1123 case ERROR_TARGET_TRANSLATION_FAULT
:
1124 gdb_send_error(connection
, EFAULT
);
1126 case ERROR_TARGET_UNALIGNED_ACCESS
:
1127 gdb_send_error(connection
, EFAULT
);
1129 case ERROR_TARGET_NOT_HALTED
:
1130 gdb_send_error(connection
, EFAULT
);
1133 /* This could be that the target reset itself. */
1134 LOG_ERROR("unexpected error %i", retval
);
1135 gdb_send_error(connection
, EFAULT
);
1142 /* We don't have to worry about the default 2 second timeout for GDB packets,
1143 * because GDB breaks up large memory reads into smaller reads.
1145 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1147 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1156 int retval
= ERROR_OK
;
1158 /* skip command character */
1161 addr
= strtoul(packet
, &separator
, 16);
1163 if (*separator
!= ',')
1165 LOG_ERROR("incomplete read memory packet received, dropping connection");
1166 return ERROR_SERVER_REMOTE_CLOSED
;
1169 len
= strtoul(separator
+1, NULL
, 16);
1171 buffer
= malloc(len
);
1173 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1175 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1177 if ((retval
!=ERROR_OK
)&&!gdb_report_data_abort
)
1179 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1180 * At some point this might be fixed in GDB, in which case this code can be removed.
1182 * OpenOCD developers are acutely aware of this problem, but there is nothing
1183 * gained by involving the user in this problem that hopefully will get resolved
1186 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1188 * For now, the default is to fix up things to make current GDB versions work.
1189 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1191 memset(buffer
, 0, len
);
1195 if (retval
== ERROR_OK
)
1197 hex_buffer
= malloc(len
* 2 + 1);
1200 for (i
= 0; i
< len
; i
++)
1203 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1204 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1207 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1213 retval
= gdb_error(connection
, retval
);
1221 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1232 /* skip command character */
1235 addr
= strtoul(packet
, &separator
, 16);
1237 if (*separator
!= ',')
1239 LOG_ERROR("incomplete write memory packet received, dropping connection");
1240 return ERROR_SERVER_REMOTE_CLOSED
;
1243 len
= strtoul(separator
+1, &separator
, 16);
1245 if (*(separator
++) != ':')
1247 LOG_ERROR("incomplete write memory packet received, dropping connection");
1248 return ERROR_SERVER_REMOTE_CLOSED
;
1251 buffer
= malloc(len
);
1253 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1255 for (i
=0; i
<len
; i
++)
1258 sscanf(separator
+ 2*i
, "%2x", &tmp
);
1262 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1264 if (retval
== ERROR_OK
)
1266 gdb_put_packet(connection
, "OK", 2);
1270 retval
= gdb_error(connection
, retval
);
1278 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1286 /* skip command character */
1289 addr
= strtoul(packet
, &separator
, 16);
1291 if (*separator
!= ',')
1293 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1294 return ERROR_SERVER_REMOTE_CLOSED
;
1297 len
= strtoul(separator
+1, &separator
, 16);
1299 if (*(separator
++) != ':')
1301 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1302 return ERROR_SERVER_REMOTE_CLOSED
;
1308 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1310 retval
= target_write_buffer(target
, addr
, len
, (u8
*)separator
);
1313 if (retval
== ERROR_OK
)
1315 gdb_put_packet(connection
, "OK", 2);
1319 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1326 int gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1330 int retval
=ERROR_OK
;
1334 if (packet_size
> 1)
1336 packet
[packet_size
] = 0;
1337 address
= strtoul(packet
+ 1, NULL
, 16);
1344 if (packet
[0] == 'c')
1346 LOG_DEBUG("continue");
1347 target_handle_event( target
, TARGET_EVENT_OLD_pre_resume
);
1348 retval
=target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1350 else if (packet
[0] == 's')
1353 retval
=target
->type
->step(target
, current
, address
, 0); /* step at current or address, don't handle breakpoints */
1358 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1361 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1362 enum watchpoint_rw wp_type
;
1370 type
= strtoul(packet
+ 1, &separator
, 16);
1372 if (type
== 0) /* memory breakpoint */
1373 bp_type
= BKPT_SOFT
;
1374 else if (type
== 1) /* hardware breakpoint */
1375 bp_type
= BKPT_HARD
;
1376 else if (type
== 2) /* write watchpoint */
1377 wp_type
= WPT_WRITE
;
1378 else if (type
== 3) /* read watchpoint */
1380 else if (type
== 4) /* access watchpoint */
1381 wp_type
= WPT_ACCESS
;
1383 if (gdb_breakpoint_override
&&((bp_type
==BKPT_SOFT
)||(bp_type
==BKPT_HARD
)))
1385 bp_type
=gdb_breakpoint_override_type
;
1388 if (*separator
!= ',')
1390 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1391 return ERROR_SERVER_REMOTE_CLOSED
;
1394 address
= strtoul(separator
+1, &separator
, 16);
1396 if (*separator
!= ',')
1398 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1399 return ERROR_SERVER_REMOTE_CLOSED
;
1402 size
= strtoul(separator
+1, &separator
, 16);
1408 if (packet
[0] == 'Z')
1410 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1412 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1417 gdb_put_packet(connection
, "OK", 2);
1422 breakpoint_remove(target
, address
);
1423 gdb_put_packet(connection
, "OK", 2);
1430 if (packet
[0] == 'Z')
1432 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1434 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1439 gdb_put_packet(connection
, "OK", 2);
1444 watchpoint_remove(target
, address
);
1445 gdb_put_packet(connection
, "OK", 2);
1456 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1457 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1459 if (*retval
!= ERROR_OK
)
1467 if ((*xml
== NULL
) || (!first
))
1469 /* start by 0 to exercise all the code paths.
1470 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1472 *size
= *size
* 2 + 2;
1474 *xml
= realloc(*xml
, *size
);
1479 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1487 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1489 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1494 /* there was just enough or not enough space, allocate more. */
1499 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1503 /* Extract and NUL-terminate the annex. */
1505 while (*buf
&& *buf
!= ':')
1511 /* After the read marker and annex, qXfer looks like a
1512 * traditional 'm' packet. */
1514 *ofs
= strtoul(buf
, &separator
, 16);
1516 if (*separator
!= ',')
1519 *len
= strtoul(separator
+1, NULL
, 16);
1524 int gdb_calc_blocksize(flash_bank_t
*bank
)
1527 int block_size
= 0xffffffff;
1529 /* loop through all sectors and return smallest sector size */
1531 for (i
= 0; i
< bank
->num_sectors
; i
++)
1533 if (bank
->sectors
[i
].size
< block_size
)
1534 block_size
= bank
->sectors
[i
].size
;
1540 static int compare_bank (const void * a
, const void * b
)
1542 flash_bank_t
*b1
, *b2
;
1543 b1
=*((flash_bank_t
**)a
);
1544 b2
=*((flash_bank_t
**)b
);
1546 if (b1
->base
==b2
->base
)
1549 } else if (b1
->base
>b2
->base
)
1558 int gdb_query_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1560 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1561 gdb_connection_t
*gdb_connection
= connection
->priv
;
1563 if (strstr(packet
, "qRcmd,"))
1565 if (packet_size
> 6)
1569 cmd
= malloc((packet_size
- 6)/2 + 1);
1570 for (i
=0; i
< (packet_size
- 6)/2; i
++)
1573 sscanf(packet
+ 6 + 2*i
, "%2x", &tmp
);
1576 cmd
[(packet_size
- 6)/2] = 0x0;
1578 /* We want to print all debug output to GDB connection */
1579 log_add_callback(gdb_log_callback
, connection
);
1580 target_call_timer_callbacks_now();
1581 command_run_line(cmd_ctx
, cmd
);
1582 target_call_timer_callbacks_now();
1583 log_remove_callback(gdb_log_callback
, connection
);
1586 gdb_put_packet(connection
, "OK", 2);
1589 else if (strstr(packet
, "qCRC:"))
1591 if (packet_size
> 5)
1600 /* skip command character */
1603 addr
= strtoul(packet
, &separator
, 16);
1605 if (*separator
!= ',')
1607 LOG_ERROR("incomplete read memory packet received, dropping connection");
1608 return ERROR_SERVER_REMOTE_CLOSED
;
1611 len
= strtoul(separator
+ 1, NULL
, 16);
1613 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1615 if (retval
== ERROR_OK
)
1617 snprintf(gdb_reply
, 10, "C%8.8x", checksum
);
1618 gdb_put_packet(connection
, gdb_reply
, 9);
1622 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1629 else if (strstr(packet
, "qSupported"))
1631 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1632 * disable qXfer:features:read for the moment */
1633 int retval
= ERROR_OK
;
1634 char *buffer
= NULL
;
1638 xml_printf(&retval
, &buffer
, &pos
, &size
,
1639 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1640 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1642 if (retval
!= ERROR_OK
)
1644 gdb_send_error(connection
, 01);
1648 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1653 else if (strstr(packet
, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1655 /* We get away with only specifying flash here. Regions that are not
1656 * specified are treated as if we provided no memory map(if not we
1657 * could detect the holes and mark them as RAM).
1658 * Normally we only execute this code once, but no big deal if we
1659 * have to regenerate it a couple of times. */
1665 int retval
= ERROR_OK
;
1672 /* skip command character */
1675 offset
= strtoul(packet
, &separator
, 16);
1676 length
= strtoul(separator
+ 1, &separator
, 16);
1678 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1681 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1682 read/write) by default for GDB.
1683 GDB does not have a concept of non-cacheable read/write memory.
1685 flash_bank_t
**banks
=malloc(sizeof(flash_bank_t
*)*flash_get_bank_count());
1688 for (i
=0; i
<flash_get_bank_count(); i
++)
1690 p
= get_flash_bank_by_num(i
);
1694 retval
= ERROR_FAIL
;
1695 gdb_send_error(connection
, retval
);
1701 qsort(banks
, flash_get_bank_count(), sizeof(flash_bank_t
*), compare_bank
);
1704 for (i
=0; i
<flash_get_bank_count(); i
++)
1708 if (ram_start
<p
->base
)
1710 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1711 ram_start
, p
->base
-ram_start
);
1714 /* if device has uneven sector sizes, eg. str7, lpc
1715 * we pass the smallest sector size to gdb memory map */
1716 blocksize
= gdb_calc_blocksize(p
);
1718 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1719 "<property name=\"blocksize\">0x%x</property>\n" \
1721 p
->base
, p
->size
, blocksize
);
1722 ram_start
=p
->base
+p
->size
;
1726 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1727 ram_start
, 0-ram_start
);
1730 /* a flash chip could be at the very end of the 32 bit address space, in which case
1731 ram_start will be precisely 0 */
1737 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1739 if (retval
!= ERROR_OK
)
1741 gdb_send_error(connection
, retval
);
1745 if (offset
+ length
> pos
)
1747 length
= pos
- offset
;
1750 char *t
= malloc(length
+ 1);
1752 memcpy(t
+ 1, xml
+ offset
, length
);
1753 gdb_put_packet(connection
, t
, length
+ 1);
1759 else if (strstr(packet
, "qXfer:features:read:"))
1764 int retval
= ERROR_OK
;
1767 unsigned int length
;
1770 /* skip command character */
1773 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1775 gdb_send_error(connection
, 01);
1779 if (strcmp(annex
, "target.xml") != 0)
1781 gdb_send_error(connection
, 01);
1785 xml_printf(&retval
, &xml
, &pos
, &size
, \
1786 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1788 if (retval
!= ERROR_OK
)
1790 gdb_send_error(connection
, retval
);
1794 gdb_put_packet(connection
, xml
, strlen(xml
));
1799 else if (strstr(packet
, "QStartNoAckMode"))
1801 gdb_connection
->noack_mode
= 1;
1802 gdb_put_packet(connection
, "OK", 2);
1806 gdb_put_packet(connection
, "", 0);
1810 int gdb_v_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1812 gdb_connection_t
*gdb_connection
= connection
->priv
;
1813 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1816 /* if flash programming disabled - send a empty reply */
1818 if (gdb_flash_program
== 0)
1820 gdb_put_packet(connection
, "", 0);
1824 if (strstr(packet
, "vFlashErase:"))
1827 unsigned long length
;
1829 char *parse
= packet
+ 12;
1832 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1833 return ERROR_SERVER_REMOTE_CLOSED
;
1836 addr
= strtoul(parse
, &parse
, 16);
1838 if (*(parse
++) != ',' || *parse
== '\0')
1840 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1841 return ERROR_SERVER_REMOTE_CLOSED
;
1844 length
= strtoul(parse
, &parse
, 16);
1848 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1849 return ERROR_SERVER_REMOTE_CLOSED
;
1852 /* assume all sectors need erasing - stops any problems
1853 * when flash_write is called multiple times */
1856 /* perform any target specific operations before the erase */
1857 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_START
);
1858 result
= flash_erase_address_range(gdb_service
->target
, addr
, length
);
1859 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_ERASE_END
);
1862 if (result
!= ERROR_OK
)
1864 /* GDB doesn't evaluate the actual error number returned,
1865 * treat a failed erase as an I/O error
1867 gdb_send_error(connection
, EIO
);
1868 LOG_ERROR("flash_erase returned %i", result
);
1871 gdb_put_packet(connection
, "OK", 2);
1876 if (strstr(packet
, "vFlashWrite:"))
1880 unsigned long length
;
1881 char *parse
= packet
+ 12;
1885 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1886 return ERROR_SERVER_REMOTE_CLOSED
;
1888 addr
= strtoul(parse
, &parse
, 16);
1889 if (*(parse
++) != ':')
1891 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1892 return ERROR_SERVER_REMOTE_CLOSED
;
1894 length
= packet_size
- (parse
- packet
);
1896 /* create a new image if there isn't already one */
1897 if (gdb_connection
->vflash_image
== NULL
)
1899 gdb_connection
->vflash_image
= malloc(sizeof(image_t
));
1900 image_open(gdb_connection
->vflash_image
, "", "build");
1903 /* create new section with content from packet buffer */
1904 if((retval
= image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (u8
*)parse
)) != ERROR_OK
)
1909 gdb_put_packet(connection
, "OK", 2);
1914 if (!strcmp(packet
, "vFlashDone"))
1918 /* process the flashing buffer. No need to erase as GDB
1919 * always issues a vFlashErase first. */
1920 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_START
);
1921 result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0);
1922 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_FLASH_WRITE_END
);
1923 if ( result
!= ERROR_OK
)
1925 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1926 gdb_put_packet(connection
, "E.memtype", 9);
1928 gdb_send_error(connection
, EIO
);
1932 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written
);
1933 gdb_put_packet(connection
, "OK", 2);
1936 image_close(gdb_connection
->vflash_image
);
1937 free(gdb_connection
->vflash_image
);
1938 gdb_connection
->vflash_image
= NULL
;
1943 gdb_put_packet(connection
, "", 0);
1947 int gdb_detach(connection_t
*connection
, target_t
*target
)
1950 switch( detach_mode
)
1952 case GDB_DETACH_RESUME
:
1953 target_handle_event( target
, TARGET_EVENT_OLD_pre_resume
);
1954 target_resume(target
, 1, 0, 1, 0);
1957 case GDB_DETACH_RESET
:
1958 /* FIX?? make this configurable?? */
1959 target_process_reset(connection
->cmd_ctx
, RESET_HALT
);
1962 case GDB_DETACH_HALT
:
1963 target_halt(target
);
1966 case GDB_DETACH_NOTHING
:
1970 gdb_put_packet(connection
, "OK", 2);
1974 static void gdb_log_callback(void *priv
, const char *file
, int line
,
1975 const char *function
, const char *string
)
1977 connection_t
*connection
= priv
;
1978 gdb_connection_t
*gdb_con
= connection
->priv
;
1982 /* do not reply this using the O packet */
1986 gdb_output_con(connection
, string
);
1989 /* Do not allocate this on the stack */
1990 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
1992 static void gdb_sig_halted(connection_t
*connection
)
1995 snprintf(sig_reply
, 4, "T%2.2x", 2);
1996 gdb_put_packet(connection
, sig_reply
, 3);
2000 int gdb_input_inner(connection_t
*connection
)
2002 gdb_service_t
*gdb_service
= connection
->service
->priv
;
2003 target_t
*target
= gdb_service
->target
;
2004 char *packet
=gdb_packet_buffer
;
2007 gdb_connection_t
*gdb_con
= connection
->priv
;
2008 static int extended_protocol
= 0;
2010 /* drain input buffer */
2013 packet_size
= GDB_BUFFER_SIZE
-1;
2014 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
2019 /* terminate with zero */
2020 packet
[packet_size
] = 0;
2022 if( LOG_LEVEL_IS( LOG_LVL_DEBUG
) ){
2023 if( packet
[0] == 'X' ){
2024 // binary packets spew junk into the debug log stream
2027 for( x
= 0 ; (x
< 49) && (packet
[x
] != ':') ; x
++ ){
2031 LOG_DEBUG("received packet: '%s:<binary-data>'", buf
);
2033 LOG_DEBUG("received packet: '%s'", packet
);
2037 if (packet_size
> 0)
2043 /* Hct... -- set thread
2044 * we don't have threads, send empty reply */
2045 gdb_put_packet(connection
, NULL
, 0);
2049 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
2052 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
2055 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
2058 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
2061 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
2064 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
2067 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
2071 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
2074 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
2079 if (target
->state
!= TARGET_HALTED
)
2081 /* If the target isn't in the halted state, then we can't
2082 * step/continue. This might be early setup, etc.
2084 gdb_sig_halted(connection
);
2087 /* We're running/stepping, in which case we can
2088 * forward log output until the target is halted
2090 gdb_connection_t
*gdb_con
= connection
->priv
;
2091 gdb_con
->frontend_state
= TARGET_RUNNING
;
2092 log_add_callback(gdb_log_callback
, connection
);
2093 target_call_event_callbacks(target
, TARGET_EVENT_GDB_START
);
2094 int retval
=gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
2095 if (retval
!=ERROR_OK
)
2097 /* we'll never receive a halted condition... issue a false one.. */
2098 gdb_frontend_halted(target
, connection
);
2104 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
2107 retval
= gdb_detach(connection
, target
);
2108 extended_protocol
= 0;
2111 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
2115 if (extended_protocol
!= 0)
2117 gdb_put_packet(connection
, "OK", 2);
2118 return ERROR_SERVER_REMOTE_CLOSED
;
2120 /* handle extended remote protocol */
2121 extended_protocol
= 1;
2122 gdb_put_packet(connection
, "OK", 2);
2125 /* handle extended restart packet */
2126 breakpoint_clear_target(gdb_service
->target
);
2127 watchpoint_clear_target(gdb_service
->target
);
2128 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %d", get_num_by_target(target
));
2131 /* ignore unkown packets */
2132 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2133 gdb_put_packet(connection
, NULL
, 0);
2137 /* if a packet handler returned an error, exit input loop */
2138 if (retval
!= ERROR_OK
)
2142 if (gdb_con
->ctrl_c
)
2144 if (target
->state
== TARGET_RUNNING
)
2146 target_halt(target
);
2147 gdb_con
->ctrl_c
= 0;
2151 } while (gdb_con
->buf_cnt
> 0);
2156 int gdb_input(connection_t
*connection
)
2158 int retval
= gdb_input_inner(connection
);
2159 gdb_connection_t
*gdb_con
= connection
->priv
;
2160 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2163 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2164 if (gdb_con
->closed
)
2165 return ERROR_SERVER_REMOTE_CLOSED
;
2167 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2173 gdb_service_t
*gdb_service
;
2174 target_t
*target
= all_targets
;
2178 LOG_WARNING("no gdb ports allocated as no target has been specified");
2184 LOG_WARNING("no gdb port specified, using default port 3333");
2190 char service_name
[8];
2192 snprintf(service_name
, 8, "gdb-%2.2i", target
->target_number
);
2194 gdb_service
= malloc(sizeof(gdb_service_t
));
2195 gdb_service
->target
= target
;
2197 add_service("gdb", CONNECTION_GDB
,
2198 gdb_port
+ target
->target_number
,
2199 1, gdb_new_connection
, gdb_input
,
2200 gdb_connection_closed
,
2203 LOG_DEBUG("gdb service for target %s at port %i",
2205 gdb_port
+ target
->target_number
);
2207 target
= target
->next
;
2213 /* daemon configuration command gdb_port */
2214 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2218 command_print(cmd_ctx
, "%d", gdb_port
);
2222 /* only if the port wasn't overwritten by cmdline */
2224 gdb_port
= strtoul(args
[0], NULL
, 0);
2229 int handle_gdb_detach_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2233 if (strcmp(args
[0], "resume") == 0)
2235 detach_mode
= GDB_DETACH_RESUME
;
2238 else if (strcmp(args
[0], "reset") == 0)
2240 detach_mode
= GDB_DETACH_RESET
;
2243 else if (strcmp(args
[0], "halt") == 0)
2245 detach_mode
= GDB_DETACH_HALT
;
2248 else if (strcmp(args
[0], "nothing") == 0)
2250 detach_mode
= GDB_DETACH_NOTHING
;
2254 LOG_WARNING("invalid gdb_detach configuration directive: %s", args
[0]);
2257 return ERROR_COMMAND_SYNTAX_ERROR
;
2260 int handle_gdb_memory_map_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2264 if (strcmp(args
[0], "enable") == 0)
2266 gdb_use_memory_map
= 1;
2269 else if (strcmp(args
[0], "disable") == 0)
2271 gdb_use_memory_map
= 0;
2275 LOG_WARNING("invalid gdb_memory_map configuration directive %s", args
[0]);
2278 return ERROR_COMMAND_SYNTAX_ERROR
;
2281 int handle_gdb_flash_program_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2285 if (strcmp(args
[0], "enable") == 0)
2287 gdb_flash_program
= 1;
2290 else if (strcmp(args
[0], "disable") == 0)
2292 gdb_flash_program
= 0;
2296 LOG_WARNING("invalid gdb_flash_program configuration directive: %s", args
[0]);
2299 return ERROR_COMMAND_SYNTAX_ERROR
;
2302 int handle_gdb_report_data_abort_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2306 if (strcmp(args
[0], "enable") == 0)
2308 gdb_report_data_abort
= 1;
2311 else if (strcmp(args
[0], "disable") == 0)
2313 gdb_report_data_abort
= 0;
2317 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args
[0]);
2320 return ERROR_COMMAND_SYNTAX_ERROR
;
2323 /* gdb_breakpoint_override */
2324 int handle_gdb_breakpoint_override_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2331 gdb_breakpoint_override
= 1;
2332 if (strcmp(args
[0], "hard")==0)
2334 gdb_breakpoint_override_type
=BKPT_HARD
;
2335 } else if (strcmp(args
[0], "soft")==0)
2337 gdb_breakpoint_override_type
=BKPT_SOFT
;
2338 } else if (strcmp(args
[0], "disable") == 0)
2340 gdb_breakpoint_override
= 0;
2344 return ERROR_COMMAND_SYNTAX_ERROR
;
2346 if (gdb_breakpoint_override
)
2348 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
==BKPT_HARD
)?"hard":"soft");
2351 LOG_USER("breakpoint type is not overriden");
2358 int gdb_register_commands(command_context_t
*command_context
)
2360 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
2361 COMMAND_CONFIG
, "daemon configuration command gdb_port");
2362 register_command(command_context
, NULL
, "gdb_detach", handle_gdb_detach_command
,
2363 COMMAND_CONFIG
, "");
2364 register_command(command_context
, NULL
, "gdb_memory_map", handle_gdb_memory_map_command
,
2365 COMMAND_CONFIG
, "enable or disable memory map");
2366 register_command(command_context
, NULL
, "gdb_flash_program", handle_gdb_flash_program_command
,
2367 COMMAND_CONFIG
, "enable or disable flash program");
2368 register_command(command_context
, NULL
, "gdb_report_data_abort", handle_gdb_report_data_abort_command
,
2369 COMMAND_CONFIG
, "enable or disable report data");
2370 register_command(command_context
, NULL
, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command
,
2371 COMMAND_EXEC
, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2372 "The raison d'etre for this option is to support GDB GUI's without "
2373 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2374 "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)