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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
29 #include "gdb_server.h"
33 #include "binarybuffer.h"
35 #include "breakpoints.h"
37 #include "target_request.h"
38 #include "configuration.h"
46 #define _DEBUG_GDB_IO_
49 static int gdb_breakpoint_override
;
50 static enum breakpoint_type gdb_breakpoint_override_type
;
52 extern int gdb_error(connection_t
*connection
, int retval
);
53 static unsigned short gdb_port
;
54 static const char *DIGITS
= "0123456789abcdef";
56 static void gdb_log_callback(void *priv
, const char *file
, int line
,
57 const char *function
, const char *string
);
67 /* target behaviour on gdb detach */
68 enum gdb_detach_mode detach_mode
= GDB_DETACH_RESUME
;
70 /* set if we are sending a memory map to gdb
71 * via qXfer:memory-map:read packet */
72 /* enabled by default*/
73 int gdb_use_memory_map
= 1;
74 /* enabled by default*/
75 int gdb_flash_program
= 1;
77 /* if set, data aborts cause an error to be reported in memory read packets
78 * see the code in gdb_read_memory_packet() for further explanations */
79 int gdb_report_data_abort
= 0;
81 int gdb_last_signal(target_t
*target
)
83 switch (target
->debug_reason
)
85 case DBG_REASON_DBGRQ
:
86 return 0x2; /* SIGINT */
87 case DBG_REASON_BREAKPOINT
:
88 case DBG_REASON_WATCHPOINT
:
89 case DBG_REASON_WPTANDBKPT
:
90 return 0x05; /* SIGTRAP */
91 case DBG_REASON_SINGLESTEP
:
92 return 0x05; /* SIGTRAP */
93 case DBG_REASON_NOTHALTED
:
94 return 0x0; /* no signal... shouldn't happen */
96 LOG_USER("undefined debug reason %d - target needs reset", target
->debug_reason
);
101 int check_pending(connection_t
*connection
, int timeout_s
, int *got_data
)
103 /* a non-blocking socket will block if there is 0 bytes available on the socket,
104 * but return with as many bytes as are available immediately
108 gdb_connection_t
*gdb_con
= connection
->priv
;
114 if (gdb_con
->buf_cnt
>0)
121 FD_SET(connection
->fd
, &read_fds
);
123 tv
.tv_sec
= timeout_s
;
125 if (select(connection
->fd
+ 1, &read_fds
, NULL
, NULL
, &tv
) == 0)
127 /* This can typically be because a "monitor" command took too long
128 * before printing any progress messages
132 return ERROR_GDB_TIMEOUT
;
138 *got_data
=FD_ISSET(connection
->fd
, &read_fds
)!=0;
142 int gdb_get_char(connection_t
*connection
, int* next_char
)
144 gdb_connection_t
*gdb_con
= connection
->priv
;
147 #ifdef _DEBUG_GDB_IO_
151 if (gdb_con
->buf_cnt
-- > 0)
153 *next_char
= *(gdb_con
->buf_p
++);
154 if (gdb_con
->buf_cnt
> 0)
155 connection
->input_pending
= 1;
157 connection
->input_pending
= 0;
159 #ifdef _DEBUG_GDB_IO_
160 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
168 retval
=check_pending(connection
, 1, NULL
);
169 if (retval
!=ERROR_OK
)
171 gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
);
172 if (gdb_con
->buf_cnt
> 0)
176 if (gdb_con
->buf_cnt
== 0)
179 return ERROR_SERVER_REMOTE_CLOSED
;
183 errno
= WSAGetLastError();
190 case WSAECONNABORTED
:
192 return ERROR_SERVER_REMOTE_CLOSED
;
195 return ERROR_SERVER_REMOTE_CLOSED
;
197 LOG_ERROR("read: %d", errno
);
208 return ERROR_SERVER_REMOTE_CLOSED
;
211 return ERROR_SERVER_REMOTE_CLOSED
;
213 LOG_ERROR("read: %s", strerror(errno
));
215 return ERROR_SERVER_REMOTE_CLOSED
;
220 #ifdef _DEBUG_GDB_IO_
221 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
222 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
223 debug_buffer
[gdb_con
->buf_cnt
] = 0;
224 LOG_DEBUG("received '%s'", debug_buffer
);
228 gdb_con
->buf_p
= gdb_con
->buffer
;
230 *next_char
= *(gdb_con
->buf_p
++);
231 if (gdb_con
->buf_cnt
> 0)
232 connection
->input_pending
= 1;
234 connection
->input_pending
= 0;
235 #ifdef _DEBUG_GDB_IO_
236 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
242 int gdb_putback_char(connection_t
*connection
, int last_char
)
244 gdb_connection_t
*gdb_con
= connection
->priv
;
246 if (gdb_con
->buf_p
> gdb_con
->buffer
)
248 *(--gdb_con
->buf_p
) = last_char
;
253 LOG_ERROR("BUG: couldn't put character back");
259 /* The only way we can detect that the socket is closed is the first time
260 * we write to it, we will fail. Subsequent write operations will
261 * succeed. Shudder! */
262 int gdb_write(connection_t
*connection
, void *data
, int len
)
264 gdb_connection_t
*gdb_con
= connection
->priv
;
266 return ERROR_SERVER_REMOTE_CLOSED
;
268 if (write_socket(connection
->fd
, data
, len
) == len
)
273 return ERROR_SERVER_REMOTE_CLOSED
;
276 int gdb_put_packet_inner(connection_t
*connection
, char *buffer
, int len
)
279 unsigned char my_checksum
= 0;
280 #ifdef _DEBUG_GDB_IO_
285 gdb_connection_t
*gdb_con
= connection
->priv
;
287 for (i
= 0; i
< len
; i
++)
288 my_checksum
+= buffer
[i
];
290 #ifdef _DEBUG_GDB_IO_
292 * At this point we should have nothing in the input queue from GDB,
293 * however sometimes '-' is sent even though we've already received
294 * an ACK (+) for everything we've sent off.
299 if ((retval
=check_pending(connection
, 0, &gotdata
))!=ERROR_OK
)
303 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
305 LOG_WARNING("Discard unexpected char %c", reply
);
311 #ifdef _DEBUG_GDB_IO_
312 debug_buffer
= malloc(len
+ 1);
313 memcpy(debug_buffer
, buffer
, len
);
314 debug_buffer
[len
] = 0;
315 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
319 char local_buffer
[1024];
320 local_buffer
[0] = '$';
321 if (len
+4 <= sizeof(local_buffer
))
323 /* performance gain on smaller packets by only a single call to gdb_write() */
324 memcpy(local_buffer
+1, buffer
, len
++);
325 local_buffer
[len
++] = '#';
326 local_buffer
[len
++] = DIGITS
[(my_checksum
>> 4) & 0xf];
327 local_buffer
[len
++] = DIGITS
[my_checksum
& 0xf];
328 gdb_write(connection
, local_buffer
, len
);
332 /* larger packets are transmitted directly from caller supplied buffer
333 by several calls to gdb_write() to avoid dynamic allocation */
334 local_buffer
[1] = '#';
335 local_buffer
[2] = DIGITS
[(my_checksum
>> 4) & 0xf];
336 local_buffer
[3] = DIGITS
[my_checksum
& 0xf];
337 gdb_write(connection
, local_buffer
, 1);
338 gdb_write(connection
, buffer
, len
);
339 gdb_write(connection
, local_buffer
+1, 3);
342 if (gdb_con
->noack_mode
)
345 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
350 else if (reply
== '-')
352 /* Stop sending output packets for now */
353 log_remove_callback(gdb_log_callback
, connection
);
354 LOG_WARNING("negative reply, retrying");
356 else if (reply
== 0x3)
359 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
363 else if (reply
== '-')
365 /* Stop sending output packets for now */
366 log_remove_callback(gdb_log_callback
, connection
);
367 LOG_WARNING("negative reply, retrying");
371 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
373 return ERROR_SERVER_REMOTE_CLOSED
;
378 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
380 return ERROR_SERVER_REMOTE_CLOSED
;
384 return ERROR_SERVER_REMOTE_CLOSED
;
389 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
391 gdb_connection_t
*gdb_con
= connection
->priv
;
393 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
398 int gdb_get_packet_inner(connection_t
*connection
, char *buffer
, int *len
)
404 unsigned char my_checksum
= 0;
405 gdb_connection_t
*gdb_con
= connection
->priv
;
411 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
414 #ifdef _DEBUG_GDB_IO_
415 LOG_DEBUG("character: '%c'", character
);
423 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
424 * incase anyone tries to debug why they receive this warning every time */
425 LOG_WARNING("acknowledgment received, but no packet pending");
428 LOG_WARNING("negative acknowledgment, but no packet pending");
435 LOG_WARNING("ignoring character 0x%x", character
);
438 } while (character
!= '$');
443 gdb_connection_t
*gdb_con
= connection
->priv
;
446 /* The common case is that we have an entire packet with no escape chars.
447 * We need to leave at least 2 bytes in the buffer to have
448 * gdb_get_char() update various bits and bobs correctly.
450 if ((gdb_con
->buf_cnt
> 2) && ((gdb_con
->buf_cnt
+count
) < *len
))
452 /* The compiler will struggle a bit with constant propagation and
453 * aliasing, so we help it by showing that these values do not
454 * change inside the loop
457 char *buf
= gdb_con
->buf_p
;
458 int run
= gdb_con
->buf_cnt
- 2;
465 if (character
== '#')
467 /* Danger! character can be '#' when esc is
468 * used so we need an explicit boolean for done here.
474 if (character
== '}')
476 /* data transmitted in binary mode (X packet)
477 * uses 0x7d as escape character */
478 my_checksum
+= character
& 0xff;
481 my_checksum
+= character
& 0xff;
482 buffer
[count
++] = (character
^ 0x20) & 0xff;
486 my_checksum
+= character
& 0xff;
487 buffer
[count
++] = character
& 0xff;
491 gdb_con
->buf_cnt
-= i
;
497 LOG_ERROR("packet buffer too small");
498 return ERROR_GDB_BUFFER_TOO_SMALL
;
501 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
504 if (character
== '#')
507 if (character
== '}')
509 /* data transmitted in binary mode (X packet)
510 * uses 0x7d as escape character */
511 my_checksum
+= character
& 0xff;
512 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
514 my_checksum
+= character
& 0xff;
515 buffer
[count
++] = (character
^ 0x20) & 0xff;
519 my_checksum
+= character
& 0xff;
520 buffer
[count
++] = character
& 0xff;
526 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
528 checksum
[0] = character
;
529 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
531 checksum
[1] = character
;
534 if (my_checksum
== strtoul(checksum
, NULL
, 16))
536 if (gdb_con
->noack_mode
)
538 gdb_write(connection
, "+", 1);
542 if (!gdb_con
->noack_mode
)
544 LOG_WARNING("checksum error, requesting retransmission");
545 gdb_write(connection
, "-", 1);
549 LOG_WARNING("checksum error, no-ack-mode");
554 return ERROR_SERVER_REMOTE_CLOSED
;
559 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
561 gdb_connection_t
*gdb_con
= connection
->priv
;
563 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
568 int gdb_output_con(connection_t
*connection
, const char* line
)
573 bin_size
= strlen(line
);
575 hex_buffer
= malloc(bin_size
*2 + 2);
576 if (hex_buffer
== NULL
)
577 return ERROR_GDB_BUFFER_TOO_SMALL
;
580 for (i
=0; i
<bin_size
; i
++)
581 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
582 hex_buffer
[bin_size
*2+1] = 0;
584 gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
590 int gdb_output(struct command_context_s
*context
, const char* line
)
592 /* this will be dumped to the log and also sent as an O packet if possible */
593 LOG_USER_N("%s", line
);
597 int gdb_program_handler(struct target_s
*target
, enum target_event event
, void *priv
)
599 struct command_context_s
*cmd_ctx
= priv
;
601 target_invoke_script(cmd_ctx
, target
, "gdb_program");
602 jtag_execute_queue();
607 static void gdb_frontend_halted(struct target_s
*target
, connection_t
*connection
)
609 gdb_connection_t
*gdb_connection
= connection
->priv
;
611 /* In the GDB protocol when we are stepping or coninuing execution,
612 * we have a lingering reply. Upon receiving a halted event
613 * when we have that lingering packet, we reply to the original
614 * step or continue packet.
616 * Executing monitor commands can bring the target in and
617 * out of the running state so we'll see lots of TARGET_EVENT_XXX
618 * that are to be ignored.
620 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
624 /* stop forwarding log packets! */
625 log_remove_callback(gdb_log_callback
, connection
);
627 if (gdb_connection
->ctrl_c
)
630 gdb_connection
->ctrl_c
= 0;
634 signal
= gdb_last_signal(target
);
637 snprintf(sig_reply
, 4, "T%2.2x", signal
);
638 gdb_put_packet(connection
, sig_reply
, 3);
639 gdb_connection
->frontend_state
= TARGET_HALTED
;
643 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
645 connection_t
*connection
= priv
;
649 case TARGET_EVENT_HALTED
:
650 gdb_frontend_halted(target
, connection
);
652 case TARGET_EVENT_GDB_PROGRAM
:
653 gdb_program_handler(target
, event
, connection
->cmd_ctx
);
663 int gdb_new_connection(connection_t
*connection
)
665 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
666 gdb_service_t
*gdb_service
= connection
->service
->priv
;
670 connection
->priv
= gdb_connection
;
672 /* initialize gdb connection information */
673 gdb_connection
->buf_p
= gdb_connection
->buffer
;
674 gdb_connection
->buf_cnt
= 0;
675 gdb_connection
->ctrl_c
= 0;
676 gdb_connection
->frontend_state
= TARGET_HALTED
;
677 gdb_connection
->vflash_image
= NULL
;
678 gdb_connection
->closed
= 0;
679 gdb_connection
->busy
= 0;
680 gdb_connection
->noack_mode
= 0;
682 /* send ACK to GDB for debug request */
683 gdb_write(connection
, "+", 1);
685 /* output goes through gdb connection */
686 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
688 /* we must remove all breakpoints registered to the target as a previous
689 * GDB session could leave dangling breakpoints if e.g. communication
692 breakpoint_clear_target(gdb_service
->target
);
693 watchpoint_clear_target(gdb_service
->target
);
695 /* register callback to be informed about target events */
696 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
698 /* a gdb session just attached, try to put the target in halt mode.
702 * If the halt fails(e.g. target needs a reset, JTAG communication not
703 * working, etc.), then the GDB connect will succeed as
704 * the get_gdb_reg_list() will lie and return a register list with
707 * This allows GDB monitor commands to be run from a GDB init script to
708 * initialize the target
710 * Also, since the halt() is asynchronous target connect will be
711 * instantaneous and thus avoiding annoying timeout problems during
714 target_halt(gdb_service
->target
);
715 /* FIX!!!! could extended-remote work better here?
717 * wait a tiny bit for halted state or we just continue. The
718 * GDB register packet will then contain garbage
720 target_wait_state(gdb_service
->target
, TARGET_HALTED
, 500);
722 /* remove the initial ACK from the incoming buffer */
723 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
726 /* FIX!!!??? would we actually ever receive a + here???
729 if (initial_ack
!= '+')
730 gdb_putback_char(connection
, initial_ack
);
735 int gdb_connection_closed(connection_t
*connection
)
737 gdb_service_t
*gdb_service
= connection
->service
->priv
;
738 gdb_connection_t
*gdb_connection
= connection
->priv
;
740 /* see if an image built with vFlash commands is left */
741 if (gdb_connection
->vflash_image
)
743 image_close(gdb_connection
->vflash_image
);
744 free(gdb_connection
->vflash_image
);
745 gdb_connection
->vflash_image
= NULL
;
748 /* if this connection registered a debug-message receiver delete it */
749 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
751 if (connection
->priv
)
753 free(connection
->priv
);
754 connection
->priv
= NULL
;
758 LOG_ERROR("BUG: connection->priv == NULL");
761 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
762 log_remove_callback(gdb_log_callback
, connection
);
767 void gdb_send_error(connection_t
*connection
, u8 the_error
)
770 snprintf(err
, 4, "E%2.2X", the_error
);
771 gdb_put_packet(connection
, err
, 3);
774 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
779 signal
= gdb_last_signal(target
);
781 snprintf(sig_reply
, 4, "S%2.2x", signal
);
782 gdb_put_packet(connection
, sig_reply
, 3);
787 /* Convert register to string of bits. NB! The # of bits in the
788 * register might be non-divisible by 8(a byte), in which
789 * case an entire byte is shown. */
790 void gdb_str_to_target(target_t
*target
, char *tstr
, reg_t
*reg
)
797 buf_len
= CEIL(reg
->size
, 8);
799 for (i
= 0; i
< buf_len
; i
++)
801 tstr
[i
*2] = DIGITS
[(buf
[i
]>>4) & 0xf];
802 tstr
[i
*2+1] = DIGITS
[buf
[i
]&0xf];
806 void gdb_target_to_str(target_t
*target
, char *tstr
, char *str
)
808 int str_len
= strlen(tstr
);
813 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
817 for (i
= 0; i
< str_len
; i
+=2)
819 str
[str_len
- i
- 1] = tstr
[i
+ 1];
820 str
[str_len
- i
- 2] = tstr
[i
];
824 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
829 int reg_packet_size
= 0;
834 #ifdef _DEBUG_GDB_IO_
838 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
840 return gdb_error(connection
, retval
);
843 for (i
= 0; i
< reg_list_size
; i
++)
845 reg_packet_size
+= reg_list
[i
]->size
;
848 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
849 reg_packet_p
= reg_packet
;
851 for (i
= 0; i
< reg_list_size
; i
++)
853 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
854 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
857 #ifdef _DEBUG_GDB_IO_
860 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
861 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
866 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
874 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
882 #ifdef _DEBUG_GDB_IO_
886 /* skip command character */
892 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
893 return ERROR_SERVER_REMOTE_CLOSED
;
896 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
898 return gdb_error(connection
, retval
);
902 for (i
= 0; i
< reg_list_size
; i
++)
906 reg_arch_type_t
*arch_type
;
908 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
909 hex_buf
= malloc(CEIL(reg_list
[i
]->size
, 8) * 2);
910 gdb_target_to_str(target
, packet_p
, hex_buf
);
912 /* convert hex-string to binary buffer */
913 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
914 str_to_buf(hex_buf
, CEIL(reg_list
[i
]->size
, 8) * 2, bin_buf
, reg_list
[i
]->size
, 16);
916 /* get register arch_type, and call set method */
917 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
918 if (arch_type
== NULL
)
920 LOG_ERROR("BUG: encountered unregistered arch type");
923 arch_type
->set(reg_list
[i
], bin_buf
);
925 /* advance packet pointer */
926 packet_p
+= (CEIL(reg_list
[i
]->size
, 8) * 2);
932 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
935 gdb_put_packet(connection
, "OK", 2);
940 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
943 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
948 #ifdef _DEBUG_GDB_IO_
952 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
954 return gdb_error(connection
, retval
);
957 if (reg_list_size
<= reg_num
)
959 LOG_ERROR("gdb requested a non-existing register");
963 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
965 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
967 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
975 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
980 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
984 reg_arch_type_t
*arch_type
;
988 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
990 return gdb_error(connection
, retval
);
993 if (reg_list_size
< reg_num
)
995 LOG_ERROR("gdb requested a non-existing register");
996 return ERROR_SERVER_REMOTE_CLOSED
;
999 if (*separator
!= '=')
1001 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1002 return ERROR_SERVER_REMOTE_CLOSED
;
1005 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1006 hex_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
1007 gdb_target_to_str(target
, separator
+ 1, hex_buf
);
1009 /* convert hex-string to binary buffer */
1010 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
1011 str_to_buf(hex_buf
, CEIL(reg_list
[reg_num
]->size
, 8) * 2, bin_buf
, reg_list
[reg_num
]->size
, 16);
1013 /* get register arch_type, and call set method */
1014 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
1015 if (arch_type
== NULL
)
1017 LOG_ERROR("BUG: encountered unregistered arch type");
1020 arch_type
->set(reg_list
[reg_num
], bin_buf
);
1022 gdb_put_packet(connection
, "OK", 2);
1031 int gdb_error(connection_t
*connection
, int retval
)
1035 case ERROR_TARGET_DATA_ABORT
:
1036 gdb_send_error(connection
, EIO
);
1038 case ERROR_TARGET_TRANSLATION_FAULT
:
1039 gdb_send_error(connection
, EFAULT
);
1041 case ERROR_TARGET_UNALIGNED_ACCESS
:
1042 gdb_send_error(connection
, EFAULT
);
1044 case ERROR_TARGET_NOT_HALTED
:
1045 gdb_send_error(connection
, EFAULT
);
1048 /* This could be that the target reset itself. */
1049 LOG_ERROR("unexpected error %i", retval
);
1050 gdb_send_error(connection
, EFAULT
);
1057 /* We don't have to worry about the default 2 second timeout for GDB packets,
1058 * because GDB breaks up large memory reads into smaller reads.
1060 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1062 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1071 int retval
= ERROR_OK
;
1073 /* skip command character */
1076 addr
= strtoul(packet
, &separator
, 16);
1078 if (*separator
!= ',')
1080 LOG_ERROR("incomplete read memory packet received, dropping connection");
1081 return ERROR_SERVER_REMOTE_CLOSED
;
1084 len
= strtoul(separator
+1, NULL
, 16);
1086 buffer
= malloc(len
);
1088 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1090 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1092 if ((retval
== ERROR_TARGET_DATA_ABORT
) && (!gdb_report_data_abort
))
1094 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1095 * At some point this might be fixed in GDB, in which case this code can be removed.
1097 * OpenOCD developers are acutely aware of this problem, but there is nothing
1098 * gained by involving the user in this problem that hopefully will get resolved
1101 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1103 * For now, the default is to fix up things to make current GDB versions work.
1104 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1106 memset(buffer
, 0, len
);
1110 if (retval
== ERROR_OK
)
1112 hex_buffer
= malloc(len
* 2 + 1);
1115 for (i
= 0; i
< len
; i
++)
1118 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1119 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1122 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1128 retval
= gdb_error(connection
, retval
);
1136 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1147 /* skip command character */
1150 addr
= strtoul(packet
, &separator
, 16);
1152 if (*separator
!= ',')
1154 LOG_ERROR("incomplete write memory packet received, dropping connection");
1155 return ERROR_SERVER_REMOTE_CLOSED
;
1158 len
= strtoul(separator
+1, &separator
, 16);
1160 if (*(separator
++) != ':')
1162 LOG_ERROR("incomplete write memory packet received, dropping connection");
1163 return ERROR_SERVER_REMOTE_CLOSED
;
1166 buffer
= malloc(len
);
1168 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1170 for (i
=0; i
<len
; i
++)
1173 sscanf(separator
+ 2*i
, "%2x", &tmp
);
1177 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1179 if (retval
== ERROR_OK
)
1181 gdb_put_packet(connection
, "OK", 2);
1185 retval
= gdb_error(connection
, retval
);
1193 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1201 /* skip command character */
1204 addr
= strtoul(packet
, &separator
, 16);
1206 if (*separator
!= ',')
1208 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1209 return ERROR_SERVER_REMOTE_CLOSED
;
1212 len
= strtoul(separator
+1, &separator
, 16);
1214 if (*(separator
++) != ':')
1216 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1217 return ERROR_SERVER_REMOTE_CLOSED
;
1223 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1225 retval
= target_write_buffer(target
, addr
, len
, (u8
*)separator
);
1228 if (retval
== ERROR_OK
)
1230 gdb_put_packet(connection
, "OK", 2);
1234 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1241 int gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1245 int retval
=ERROR_OK
;
1249 if (packet_size
> 1)
1251 packet
[packet_size
] = 0;
1252 address
= strtoul(packet
+ 1, NULL
, 16);
1259 if (packet
[0] == 'c')
1261 LOG_DEBUG("continue");
1262 target_invoke_script(connection
->cmd_ctx
, target
, "pre_resume");
1263 retval
=target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1265 else if (packet
[0] == 's')
1268 retval
=target
->type
->step(target
, current
, address
, 0); /* step at current or address, don't handle breakpoints */
1273 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1276 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1277 enum watchpoint_rw wp_type
;
1285 type
= strtoul(packet
+ 1, &separator
, 16);
1287 if (type
== 0) /* memory breakpoint */
1288 bp_type
= BKPT_SOFT
;
1289 else if (type
== 1) /* hardware breakpoint */
1290 bp_type
= BKPT_HARD
;
1291 else if (type
== 2) /* write watchpoint */
1292 wp_type
= WPT_WRITE
;
1293 else if (type
== 3) /* read watchpoint */
1295 else if (type
== 4) /* access watchpoint */
1296 wp_type
= WPT_ACCESS
;
1298 if (gdb_breakpoint_override
&&((bp_type
==BKPT_SOFT
)||(bp_type
==BKPT_HARD
)))
1300 bp_type
=gdb_breakpoint_override_type
;
1303 if (*separator
!= ',')
1305 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1306 return ERROR_SERVER_REMOTE_CLOSED
;
1309 address
= strtoul(separator
+1, &separator
, 16);
1311 if (*separator
!= ',')
1313 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1314 return ERROR_SERVER_REMOTE_CLOSED
;
1317 size
= strtoul(separator
+1, &separator
, 16);
1323 if (packet
[0] == 'Z')
1325 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1327 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1332 gdb_put_packet(connection
, "OK", 2);
1337 breakpoint_remove(target
, address
);
1338 gdb_put_packet(connection
, "OK", 2);
1345 if (packet
[0] == 'Z')
1347 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1349 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1354 gdb_put_packet(connection
, "OK", 2);
1359 watchpoint_remove(target
, address
);
1360 gdb_put_packet(connection
, "OK", 2);
1371 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1372 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1374 if (*retval
!= ERROR_OK
)
1382 if ((*xml
== NULL
) || (!first
))
1384 /* start by 0 to exercise all the code paths.
1385 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1387 *size
= *size
* 2 + 2;
1389 *xml
= realloc(*xml
, *size
);
1394 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1402 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1404 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1409 /* there was just enough or not enough space, allocate more. */
1414 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1418 /* Extract and NUL-terminate the annex. */
1420 while (*buf
&& *buf
!= ':')
1426 /* After the read marker and annex, qXfer looks like a
1427 * traditional 'm' packet. */
1429 *ofs
= strtoul(buf
, &separator
, 16);
1431 if (*separator
!= ',')
1434 *len
= strtoul(separator
+1, NULL
, 16);
1439 int gdb_calc_blocksize(flash_bank_t
*bank
)
1442 int block_size
= 0xffffffff;
1444 /* loop through all sectors and return smallest sector size */
1446 for (i
= 0; i
< bank
->num_sectors
; i
++)
1448 if (bank
->sectors
[i
].size
< block_size
)
1449 block_size
= bank
->sectors
[i
].size
;
1455 static int compare_bank (const void * a
, const void * b
)
1457 flash_bank_t
*b1
, *b2
;
1458 b1
=*((flash_bank_t
**)a
);
1459 b2
=*((flash_bank_t
**)b
);
1461 if (b1
->base
==b2
->base
)
1464 } else if (b1
->base
>b2
->base
)
1473 int gdb_query_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1475 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1476 gdb_connection_t
*gdb_connection
= connection
->priv
;
1478 if (strstr(packet
, "qRcmd,"))
1480 if (packet_size
> 6)
1484 cmd
= malloc((packet_size
- 6)/2 + 1);
1485 for (i
=0; i
< (packet_size
- 6)/2; i
++)
1488 sscanf(packet
+ 6 + 2*i
, "%2x", &tmp
);
1491 cmd
[(packet_size
- 6)/2] = 0x0;
1493 /* We want to print all debug output to GDB connection */
1494 log_add_callback(gdb_log_callback
, connection
);
1495 target_call_timer_callbacks_now();
1496 command_run_line(cmd_ctx
, cmd
);
1497 target_call_timer_callbacks_now();
1498 log_remove_callback(gdb_log_callback
, connection
);
1501 gdb_put_packet(connection
, "OK", 2);
1504 else if (strstr(packet
, "qCRC:"))
1506 if (packet_size
> 5)
1515 /* skip command character */
1518 addr
= strtoul(packet
, &separator
, 16);
1520 if (*separator
!= ',')
1522 LOG_ERROR("incomplete read memory packet received, dropping connection");
1523 return ERROR_SERVER_REMOTE_CLOSED
;
1526 len
= strtoul(separator
+ 1, NULL
, 16);
1528 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1530 if (retval
== ERROR_OK
)
1532 snprintf(gdb_reply
, 10, "C%8.8x", checksum
);
1533 gdb_put_packet(connection
, gdb_reply
, 9);
1537 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1544 else if (strstr(packet
, "qSupported"))
1546 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1547 * disable qXfer:features:read for the moment */
1548 int retval
= ERROR_OK
;
1549 char *buffer
= NULL
;
1553 xml_printf(&retval
, &buffer
, &pos
, &size
,
1554 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1555 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1557 if (retval
!= ERROR_OK
)
1559 gdb_send_error(connection
, 01);
1563 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1568 else if (strstr(packet
, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1570 /* We get away with only specifying flash here. Regions that are not
1571 * specified are treated as if we provided no memory map(if not we
1572 * could detect the holes and mark them as RAM).
1573 * Normally we only execute this code once, but no big deal if we
1574 * have to regenerate it a couple of times. */
1580 int retval
= ERROR_OK
;
1587 /* skip command character */
1590 offset
= strtoul(packet
, &separator
, 16);
1591 length
= strtoul(separator
+ 1, &separator
, 16);
1593 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1596 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1597 read/write) by default for GDB.
1598 GDB does not have a concept of non-cacheable read/write memory.
1600 flash_bank_t
**banks
=malloc(sizeof(flash_bank_t
*)*flash_get_bank_count());
1603 for (i
=0; i
<flash_get_bank_count(); i
++)
1605 p
= get_flash_bank_by_num(i
);
1609 retval
= ERROR_FAIL
;
1610 gdb_send_error(connection
, retval
);
1616 qsort(banks
, flash_get_bank_count(), sizeof(flash_bank_t
*), compare_bank
);
1619 for (i
=0; i
<flash_get_bank_count(); i
++)
1623 if (ram_start
<p
->base
)
1625 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1626 ram_start
, p
->base
-ram_start
);
1629 /* if device has uneven sector sizes, eg. str7, lpc
1630 * we pass the smallest sector size to gdb memory map */
1631 blocksize
= gdb_calc_blocksize(p
);
1633 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1634 "<property name=\"blocksize\">0x%x</property>\n" \
1636 p
->base
, p
->size
, blocksize
);
1637 ram_start
=p
->base
+p
->size
;
1641 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1642 ram_start
, 0-ram_start
);
1645 /* a flash chip could be at the very end of the 32 bit address space, in which case
1646 ram_start will be precisely 0 */
1652 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1654 if (retval
!= ERROR_OK
)
1656 gdb_send_error(connection
, retval
);
1660 if (offset
+ length
> pos
)
1662 length
= pos
- offset
;
1665 char *t
= malloc(length
+ 1);
1667 memcpy(t
+ 1, xml
+ offset
, length
);
1668 gdb_put_packet(connection
, t
, length
+ 1);
1674 else if (strstr(packet
, "qXfer:features:read:"))
1679 int retval
= ERROR_OK
;
1682 unsigned int length
;
1685 /* skip command character */
1688 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1690 gdb_send_error(connection
, 01);
1694 if (strcmp(annex
, "target.xml") != 0)
1696 gdb_send_error(connection
, 01);
1700 xml_printf(&retval
, &xml
, &pos
, &size
, \
1701 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1703 if (retval
!= ERROR_OK
)
1705 gdb_send_error(connection
, retval
);
1709 gdb_put_packet(connection
, xml
, strlen(xml
));
1714 else if (strstr(packet
, "QStartNoAckMode"))
1716 gdb_connection
->noack_mode
= 1;
1717 gdb_put_packet(connection
, "OK", 2);
1721 gdb_put_packet(connection
, "", 0);
1725 int gdb_v_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1727 gdb_connection_t
*gdb_connection
= connection
->priv
;
1728 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1731 /* if flash programming disabled - send a empty reply */
1733 if (gdb_flash_program
== 0)
1735 gdb_put_packet(connection
, "", 0);
1739 if (strstr(packet
, "vFlashErase:"))
1742 unsigned long length
;
1744 char *parse
= packet
+ 12;
1747 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1748 return ERROR_SERVER_REMOTE_CLOSED
;
1751 addr
= strtoul(parse
, &parse
, 16);
1753 if (*(parse
++) != ',' || *parse
== '\0')
1755 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1756 return ERROR_SERVER_REMOTE_CLOSED
;
1759 length
= strtoul(parse
, &parse
, 16);
1763 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1764 return ERROR_SERVER_REMOTE_CLOSED
;
1767 /* assume all sectors need erasing - stops any problems
1768 * when flash_write is called multiple times */
1771 /* perform any target specific operations before the erase */
1772 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_PROGRAM
);
1775 if ((result
= flash_erase_address_range(gdb_service
->target
, addr
, length
)) != ERROR_OK
)
1777 /* GDB doesn't evaluate the actual error number returned,
1778 * treat a failed erase as an I/O error
1780 gdb_send_error(connection
, EIO
);
1781 LOG_ERROR("flash_erase returned %i", result
);
1784 gdb_put_packet(connection
, "OK", 2);
1789 if (strstr(packet
, "vFlashWrite:"))
1792 unsigned long length
;
1793 char *parse
= packet
+ 12;
1797 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1798 return ERROR_SERVER_REMOTE_CLOSED
;
1800 addr
= strtoul(parse
, &parse
, 16);
1801 if (*(parse
++) != ':')
1803 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1804 return ERROR_SERVER_REMOTE_CLOSED
;
1806 length
= packet_size
- (parse
- packet
);
1808 /* create a new image if there isn't already one */
1809 if (gdb_connection
->vflash_image
== NULL
)
1811 gdb_connection
->vflash_image
= malloc(sizeof(image_t
));
1812 image_open(gdb_connection
->vflash_image
, "", "build");
1815 /* create new section with content from packet buffer */
1816 image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (u8
*)parse
);
1818 gdb_put_packet(connection
, "OK", 2);
1823 if (!strcmp(packet
, "vFlashDone"))
1827 /* process the flashing buffer. No need to erase as GDB
1828 * always issues a vFlashErase first. */
1829 if ((result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0)) != ERROR_OK
)
1831 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1832 gdb_put_packet(connection
, "E.memtype", 9);
1834 gdb_send_error(connection
, EIO
);
1838 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written
);
1839 gdb_put_packet(connection
, "OK", 2);
1842 image_close(gdb_connection
->vflash_image
);
1843 free(gdb_connection
->vflash_image
);
1844 gdb_connection
->vflash_image
= NULL
;
1849 gdb_put_packet(connection
, "", 0);
1853 int gdb_detach(connection_t
*connection
, target_t
*target
)
1855 switch( detach_mode
)
1857 case GDB_DETACH_RESUME
:
1858 target_invoke_script(connection
->cmd_ctx
, target
, "pre_resume");
1859 target_resume(target
, 1, 0, 1, 0);
1862 case GDB_DETACH_RESET
:
1863 /* FIX?? make this configurable?? */
1864 target_process_reset(connection
->cmd_ctx
, RESET_HALT
);
1867 case GDB_DETACH_HALT
:
1868 target_halt(target
);
1871 case GDB_DETACH_NOTHING
:
1875 gdb_put_packet(connection
, "OK", 2);
1880 static void gdb_log_callback(void *priv
, const char *file
, int line
,
1881 const char *function
, const char *string
)
1883 connection_t
*connection
= priv
;
1884 gdb_connection_t
*gdb_con
= connection
->priv
;
1888 /* do not reply this using the O packet */
1892 gdb_output_con(connection
, string
);
1895 /* Do not allocate this on the stack */
1896 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
1898 static void gdb_sig_halted(connection_t
*connection
)
1901 snprintf(sig_reply
, 4, "T%2.2x", 2);
1902 gdb_put_packet(connection
, sig_reply
, 3);
1906 int gdb_input_inner(connection_t
*connection
)
1908 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1909 target_t
*target
= gdb_service
->target
;
1910 char *packet
=gdb_packet_buffer
;
1913 gdb_connection_t
*gdb_con
= connection
->priv
;
1914 static int extended_protocol
= 0;
1916 /* drain input buffer */
1919 packet_size
= GDB_BUFFER_SIZE
-1;
1920 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
1925 /* terminate with zero */
1926 packet
[packet_size
] = 0;
1928 LOG_DEBUG("received packet: '%s'", packet
);
1930 if (packet_size
> 0)
1936 /* Hct... -- set thread
1937 * we don't have threads, send empty reply */
1938 gdb_put_packet(connection
, NULL
, 0);
1942 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
1945 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
1948 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
1951 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
1954 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
1957 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
1960 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
1964 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
1967 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
1972 if (target
->state
!= TARGET_HALTED
)
1974 /* If the target isn't in the halted state, then we can't
1975 * step/continue. This might be early setup, etc.
1977 gdb_sig_halted(connection
);
1980 /* We're running/stepping, in which case we can
1981 * forward log output until the target is halted
1983 gdb_connection_t
*gdb_con
= connection
->priv
;
1984 gdb_con
->frontend_state
= TARGET_RUNNING
;
1985 log_add_callback(gdb_log_callback
, connection
);
1986 int retval
=gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
1987 if (retval
!=ERROR_OK
)
1989 /* we'll never receive a halted condition... issue a false one.. */
1990 gdb_frontend_halted(target
, connection
);
1996 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
1999 retval
= gdb_detach(connection
, target
);
2000 extended_protocol
= 0;
2003 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
2007 if (extended_protocol
!= 0)
2009 gdb_put_packet(connection
, "OK", 2);
2010 return ERROR_SERVER_REMOTE_CLOSED
;
2012 /* handle extended remote protocol */
2013 extended_protocol
= 1;
2014 gdb_put_packet(connection
, "OK", 2);
2017 /* handle extended restart packet */
2018 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %d", get_num_by_target(target
));
2021 /* ignore unkown packets */
2022 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2023 gdb_put_packet(connection
, NULL
, 0);
2027 /* if a packet handler returned an error, exit input loop */
2028 if (retval
!= ERROR_OK
)
2032 if (gdb_con
->ctrl_c
)
2034 if (target
->state
== TARGET_RUNNING
)
2036 target_halt(target
);
2037 gdb_con
->ctrl_c
= 0;
2041 } while (gdb_con
->buf_cnt
> 0);
2046 int gdb_input(connection_t
*connection
)
2048 int retval
= gdb_input_inner(connection
);
2049 gdb_connection_t
*gdb_con
= connection
->priv
;
2050 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2053 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2054 if (gdb_con
->closed
)
2055 return ERROR_SERVER_REMOTE_CLOSED
;
2057 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2063 gdb_service_t
*gdb_service
;
2064 target_t
*target
= targets
;
2069 LOG_WARNING("no gdb ports allocated as no target has been specified");
2075 LOG_WARNING("no gdb port specified, using default port 3333");
2081 char service_name
[8];
2083 snprintf(service_name
, 8, "gdb-%2.2i", i
);
2085 gdb_service
= malloc(sizeof(gdb_service_t
));
2086 gdb_service
->target
= target
;
2088 add_service("gdb", CONNECTION_GDB
, gdb_port
+ i
, 1, gdb_new_connection
, gdb_input
, gdb_connection_closed
, gdb_service
);
2090 LOG_DEBUG("gdb service for target %s at port %i", target
->type
->name
, gdb_port
+ i
);
2093 target
= target
->next
;
2099 /* daemon configuration command gdb_port */
2100 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2105 /* only if the port wasn't overwritten by cmdline */
2107 gdb_port
= strtoul(args
[0], NULL
, 0);
2112 int handle_gdb_detach_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2116 if (strcmp(args
[0], "resume") == 0)
2118 detach_mode
= GDB_DETACH_RESUME
;
2121 else if (strcmp(args
[0], "reset") == 0)
2123 detach_mode
= GDB_DETACH_RESET
;
2126 else if (strcmp(args
[0], "halt") == 0)
2128 detach_mode
= GDB_DETACH_HALT
;
2131 else if (strcmp(args
[0], "nothing") == 0)
2133 detach_mode
= GDB_DETACH_NOTHING
;
2138 LOG_WARNING("invalid gdb_detach configuration directive: %s", args
[0]);
2142 int handle_gdb_memory_map_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2146 if (strcmp(args
[0], "enable") == 0)
2148 gdb_use_memory_map
= 1;
2151 else if (strcmp(args
[0], "disable") == 0)
2153 gdb_use_memory_map
= 0;
2158 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args
[0]);
2162 int handle_gdb_flash_program_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2166 if (strcmp(args
[0], "enable") == 0)
2168 gdb_flash_program
= 1;
2171 else if (strcmp(args
[0], "disable") == 0)
2173 gdb_flash_program
= 0;
2178 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args
[0]);
2182 int handle_gdb_report_data_abort_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2186 if (strcmp(args
[0], "enable") == 0)
2188 gdb_report_data_abort
= 1;
2191 else if (strcmp(args
[0], "disable") == 0)
2193 gdb_report_data_abort
= 0;
2198 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args
[0]);
2202 /* daemon configuration command gdb_port */
2203 int handle_gdb_breakpoint_override_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2210 gdb_breakpoint_override
= 1;
2211 if (strcmp(args
[0], "hard")==0)
2213 gdb_breakpoint_override_type
=BKPT_HARD
;
2214 } else if (strcmp(args
[0], "soft")==0)
2216 gdb_breakpoint_override_type
=BKPT_SOFT
;
2217 } else if (strcmp(args
[0], "disable") == 0)
2219 gdb_breakpoint_override
= 0;
2223 return ERROR_COMMAND_SYNTAX_ERROR
;
2225 if (gdb_breakpoint_override
)
2227 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
==BKPT_HARD
)?"hard":"soft");
2230 LOG_USER("breakpoint type is not overriden");
2237 int gdb_register_commands(command_context_t
*command_context
)
2239 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
2240 COMMAND_CONFIG
, "");
2241 register_command(command_context
, NULL
, "gdb_detach", handle_gdb_detach_command
,
2242 COMMAND_CONFIG
, "");
2243 register_command(command_context
, NULL
, "gdb_memory_map", handle_gdb_memory_map_command
,
2244 COMMAND_CONFIG
, "");
2245 register_command(command_context
, NULL
, "gdb_flash_program", handle_gdb_flash_program_command
,
2246 COMMAND_CONFIG
, "");
2247 register_command(command_context
, NULL
, "gdb_report_data_abort", handle_gdb_report_data_abort_command
,
2248 COMMAND_CONFIG
, "");
2249 register_command(command_context
, NULL
, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command
,
2250 COMMAND_EXEC
, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2251 "The raison d'etre for this option is to support GDB GUI's without "
2252 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2253 "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)