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 ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
347 else if (reply
== '-')
349 /* Stop sending output packets for now */
350 log_remove_callback(gdb_log_callback
, connection
);
351 LOG_WARNING("negative reply, retrying");
353 else if (reply
== 0x3)
356 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
360 else if (reply
== '-')
362 /* Stop sending output packets for now */
363 log_remove_callback(gdb_log_callback
, connection
);
364 LOG_WARNING("negative reply, retrying");
368 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
370 return ERROR_SERVER_REMOTE_CLOSED
;
375 LOG_ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
377 return ERROR_SERVER_REMOTE_CLOSED
;
381 return ERROR_SERVER_REMOTE_CLOSED
;
386 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
388 gdb_connection_t
*gdb_con
= connection
->priv
;
390 int retval
= gdb_put_packet_inner(connection
, buffer
, len
);
395 int gdb_get_packet_inner(connection_t
*connection
, char *buffer
, int *len
)
401 unsigned char my_checksum
= 0;
402 gdb_connection_t
*gdb_con
= connection
->priv
;
408 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
411 #ifdef _DEBUG_GDB_IO_
412 LOG_DEBUG("character: '%c'", character
);
420 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
421 * incase anyone tries to debug why they receive this warning every time */
422 LOG_WARNING("acknowledgment received, but no packet pending");
425 LOG_WARNING("negative acknowledgment, but no packet pending");
432 LOG_WARNING("ignoring character 0x%x", character
);
435 } while (character
!= '$');
440 gdb_connection_t
*gdb_con
= connection
->priv
;
443 /* The common case is that we have an entire packet with no escape chars.
444 * We need to leave at least 2 bytes in the buffer to have
445 * gdb_get_char() update various bits and bobs correctly.
447 if ((gdb_con
->buf_cnt
> 2) && ((gdb_con
->buf_cnt
+count
) < *len
))
449 /* The compiler will struggle a bit with constant propagation and
450 * aliasing, so we help it by showing that these values do not
451 * change inside the loop
454 char *buf
= gdb_con
->buf_p
;
455 int run
= gdb_con
->buf_cnt
- 2;
462 if (character
== '#')
464 /* Danger! character can be '#' when esc is
465 * used so we need an explicit boolean for done here.
471 if (character
== '}')
473 /* data transmitted in binary mode (X packet)
474 * uses 0x7d as escape character */
475 my_checksum
+= character
& 0xff;
478 my_checksum
+= character
& 0xff;
479 buffer
[count
++] = (character
^ 0x20) & 0xff;
482 my_checksum
+= character
& 0xff;
483 buffer
[count
++] = character
& 0xff;
487 gdb_con
->buf_cnt
-= i
;
493 LOG_ERROR("packet buffer too small");
494 return ERROR_GDB_BUFFER_TOO_SMALL
;
497 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
500 if (character
== '#')
503 if (character
== '}')
505 /* data transmitted in binary mode (X packet)
506 * uses 0x7d as escape character */
507 my_checksum
+= character
& 0xff;
508 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
510 my_checksum
+= character
& 0xff;
511 buffer
[count
++] = (character
^ 0x20) & 0xff;
515 my_checksum
+= character
& 0xff;
516 buffer
[count
++] = character
& 0xff;
523 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
525 checksum
[0] = character
;
526 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
528 checksum
[1] = character
;
531 if (my_checksum
== strtoul(checksum
, NULL
, 16))
533 gdb_write(connection
, "+", 1);
537 LOG_WARNING("checksum error, requesting retransmission");
538 gdb_write(connection
, "-", 1);
541 return ERROR_SERVER_REMOTE_CLOSED
;
546 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
548 gdb_connection_t
*gdb_con
= connection
->priv
;
550 int retval
= gdb_get_packet_inner(connection
, buffer
, len
);
555 int gdb_output_con(connection_t
*connection
, const char* line
)
560 bin_size
= strlen(line
);
562 hex_buffer
= malloc(bin_size
*2 + 2);
563 if (hex_buffer
== NULL
)
564 return ERROR_GDB_BUFFER_TOO_SMALL
;
567 for (i
=0; i
<bin_size
; i
++)
568 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
569 hex_buffer
[bin_size
*2+1] = 0;
571 gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 1);
577 int gdb_output(struct command_context_s
*context
, const char* line
)
579 /* this will be dumped to the log and also sent as an O packet if possible */
580 LOG_USER_N("%s", line
);
584 int gdb_program_handler(struct target_s
*target
, enum target_event event
, void *priv
)
586 struct command_context_s
*cmd_ctx
= priv
;
588 target_invoke_script(cmd_ctx
, target
, "gdb_program");
589 jtag_execute_queue();
594 static void gdb_frontend_halted(struct target_s
*target
, connection_t
*connection
)
596 gdb_connection_t
*gdb_connection
= connection
->priv
;
598 /* In the GDB protocol when we are stepping or coninuing execution,
599 * we have a lingering reply. Upon receiving a halted event
600 * when we have that lingering packet, we reply to the original
601 * step or continue packet.
603 * Executing monitor commands can bring the target in and
604 * out of the running state so we'll see lots of TARGET_EVENT_XXX
605 * that are to be ignored.
607 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
611 /* stop forwarding log packets! */
612 log_remove_callback(gdb_log_callback
, connection
);
614 if (gdb_connection
->ctrl_c
)
617 gdb_connection
->ctrl_c
= 0;
621 signal
= gdb_last_signal(target
);
624 snprintf(sig_reply
, 4, "T%2.2x", signal
);
625 gdb_put_packet(connection
, sig_reply
, 3);
626 gdb_connection
->frontend_state
= TARGET_HALTED
;
630 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
632 connection_t
*connection
= priv
;
636 case TARGET_EVENT_HALTED
:
637 gdb_frontend_halted(target
, connection
);
639 case TARGET_EVENT_GDB_PROGRAM
:
640 gdb_program_handler(target
, event
, connection
->cmd_ctx
);
650 int gdb_new_connection(connection_t
*connection
)
652 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
653 gdb_service_t
*gdb_service
= connection
->service
->priv
;
657 connection
->priv
= gdb_connection
;
659 /* initialize gdb connection information */
660 gdb_connection
->buf_p
= gdb_connection
->buffer
;
661 gdb_connection
->buf_cnt
= 0;
662 gdb_connection
->ctrl_c
= 0;
663 gdb_connection
->frontend_state
= TARGET_HALTED
;
664 gdb_connection
->vflash_image
= NULL
;
665 gdb_connection
->closed
= 0;
666 gdb_connection
->busy
= 0;
668 /* send ACK to GDB for debug request */
669 gdb_write(connection
, "+", 1);
671 /* output goes through gdb connection */
672 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
674 /* we must remove all breakpoints registered to the target as a previous
675 * GDB session could leave dangling breakpoints if e.g. communication
678 breakpoint_clear_target(gdb_service
->target
);
679 watchpoint_clear_target(gdb_service
->target
);
681 /* register callback to be informed about target events */
682 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
684 /* a gdb session just attached, try to put the target in halt mode.
688 * If the halt fails(e.g. target needs a reset, JTAG communication not
689 * working, etc.), then the GDB connect will succeed as
690 * the get_gdb_reg_list() will lie and return a register list with
693 * This allows GDB monitor commands to be run from a GDB init script to
694 * initialize the target
696 * Also, since the halt() is asynchronous target connect will be
697 * instantaneous and thus avoiding annoying timeout problems during
700 target_halt(gdb_service
->target
);
701 /* FIX!!!! could extended-remote work better here?
703 * wait a tiny bit for halted state or we just continue. The
704 * GDB register packet will then contain garbage
706 target_wait_state(gdb_service
->target
, TARGET_HALTED
, 500);
708 /* remove the initial ACK from the incoming buffer */
709 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
712 /* FIX!!!??? would we actually ever receive a + here???
715 if (initial_ack
!= '+')
716 gdb_putback_char(connection
, initial_ack
);
721 int gdb_connection_closed(connection_t
*connection
)
723 gdb_service_t
*gdb_service
= connection
->service
->priv
;
724 gdb_connection_t
*gdb_connection
= connection
->priv
;
726 /* see if an image built with vFlash commands is left */
727 if (gdb_connection
->vflash_image
)
729 image_close(gdb_connection
->vflash_image
);
730 free(gdb_connection
->vflash_image
);
731 gdb_connection
->vflash_image
= NULL
;
734 /* if this connection registered a debug-message receiver delete it */
735 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
737 if (connection
->priv
)
739 free(connection
->priv
);
740 connection
->priv
= NULL
;
744 LOG_ERROR("BUG: connection->priv == NULL");
747 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
748 log_remove_callback(gdb_log_callback
, connection
);
753 void gdb_send_error(connection_t
*connection
, u8 the_error
)
756 snprintf(err
, 4, "E%2.2X", the_error
);
757 gdb_put_packet(connection
, err
, 3);
760 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
765 signal
= gdb_last_signal(target
);
767 snprintf(sig_reply
, 4, "S%2.2x", signal
);
768 gdb_put_packet(connection
, sig_reply
, 3);
773 /* Convert register to string of bits. NB! The # of bits in the
774 * register might be non-divisible by 8(a byte), in which
775 * case an entire byte is shown. */
776 void gdb_str_to_target(target_t
*target
, char *tstr
, reg_t
*reg
)
783 buf_len
= CEIL(reg
->size
, 8);
785 for (i
= 0; i
< buf_len
; i
++)
787 tstr
[i
*2] = DIGITS
[(buf
[i
]>>4) & 0xf];
788 tstr
[i
*2+1] = DIGITS
[buf
[i
]&0xf];
792 void gdb_target_to_str(target_t
*target
, char *tstr
, char *str
)
794 int str_len
= strlen(tstr
);
799 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
803 for (i
= 0; i
< str_len
; i
+=2)
805 str
[str_len
- i
- 1] = tstr
[i
+ 1];
806 str
[str_len
- i
- 2] = tstr
[i
];
810 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
815 int reg_packet_size
= 0;
820 #ifdef _DEBUG_GDB_IO_
824 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
826 return gdb_error(connection
, retval
);
829 for (i
= 0; i
< reg_list_size
; i
++)
831 reg_packet_size
+= reg_list
[i
]->size
;
834 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
835 reg_packet_p
= reg_packet
;
837 for (i
= 0; i
< reg_list_size
; i
++)
839 gdb_str_to_target(target
, reg_packet_p
, reg_list
[i
]);
840 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
843 #ifdef _DEBUG_GDB_IO_
846 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
847 LOG_DEBUG("reg_packet: %s", reg_packet_p
);
852 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
860 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
868 #ifdef _DEBUG_GDB_IO_
872 /* skip command character */
878 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
879 return ERROR_SERVER_REMOTE_CLOSED
;
882 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
884 return gdb_error(connection
, retval
);
888 for (i
= 0; i
< reg_list_size
; i
++)
892 reg_arch_type_t
*arch_type
;
894 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
895 hex_buf
= malloc(CEIL(reg_list
[i
]->size
, 8) * 2);
896 gdb_target_to_str(target
, packet_p
, hex_buf
);
898 /* convert hex-string to binary buffer */
899 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
900 str_to_buf(hex_buf
, CEIL(reg_list
[i
]->size
, 8) * 2, bin_buf
, reg_list
[i
]->size
, 16);
902 /* get register arch_type, and call set method */
903 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
904 if (arch_type
== NULL
)
906 LOG_ERROR("BUG: encountered unregistered arch type");
909 arch_type
->set(reg_list
[i
], bin_buf
);
911 /* advance packet pointer */
912 packet_p
+= (CEIL(reg_list
[i
]->size
, 8) * 2);
918 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
921 gdb_put_packet(connection
, "OK", 2);
926 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
929 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
934 #ifdef _DEBUG_GDB_IO_
938 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
940 return gdb_error(connection
, retval
);
943 if (reg_list_size
<= reg_num
)
945 LOG_ERROR("gdb requested a non-existing register");
949 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
951 gdb_str_to_target(target
, reg_packet
, reg_list
[reg_num
]);
953 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
961 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
966 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
970 reg_arch_type_t
*arch_type
;
974 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
976 return gdb_error(connection
, retval
);
979 if (reg_list_size
< reg_num
)
981 LOG_ERROR("gdb requested a non-existing register");
982 return ERROR_SERVER_REMOTE_CLOSED
;
985 if (*separator
!= '=')
987 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
988 return ERROR_SERVER_REMOTE_CLOSED
;
991 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
992 hex_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
993 gdb_target_to_str(target
, separator
+ 1, hex_buf
);
995 /* convert hex-string to binary buffer */
996 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
997 str_to_buf(hex_buf
, CEIL(reg_list
[reg_num
]->size
, 8) * 2, bin_buf
, reg_list
[reg_num
]->size
, 16);
999 /* get register arch_type, and call set method */
1000 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
1001 if (arch_type
== NULL
)
1003 LOG_ERROR("BUG: encountered unregistered arch type");
1006 arch_type
->set(reg_list
[reg_num
], bin_buf
);
1008 gdb_put_packet(connection
, "OK", 2);
1017 int gdb_error(connection_t
*connection
, int retval
)
1021 case ERROR_TARGET_DATA_ABORT
:
1022 gdb_send_error(connection
, EIO
);
1024 case ERROR_TARGET_TRANSLATION_FAULT
:
1025 gdb_send_error(connection
, EFAULT
);
1027 case ERROR_TARGET_UNALIGNED_ACCESS
:
1028 gdb_send_error(connection
, EFAULT
);
1030 case ERROR_TARGET_NOT_HALTED
:
1031 gdb_send_error(connection
, EFAULT
);
1034 /* This could be that the target reset itself. */
1035 LOG_ERROR("unexpected error %i", retval
);
1036 gdb_send_error(connection
, EFAULT
);
1043 /* We don't have to worry about the default 2 second timeout for GDB packets,
1044 * because GDB breaks up large memory reads into smaller reads.
1046 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1048 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1057 int retval
= ERROR_OK
;
1059 /* skip command character */
1062 addr
= strtoul(packet
, &separator
, 16);
1064 if (*separator
!= ',')
1066 LOG_ERROR("incomplete read memory packet received, dropping connection");
1067 return ERROR_SERVER_REMOTE_CLOSED
;
1070 len
= strtoul(separator
+1, NULL
, 16);
1072 buffer
= malloc(len
);
1074 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1076 retval
= target_read_buffer(target
, addr
, len
, buffer
);
1078 if ((retval
== ERROR_TARGET_DATA_ABORT
) && (!gdb_report_data_abort
))
1080 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1081 * At some point this might be fixed in GDB, in which case this code can be removed.
1083 * OpenOCD developers are acutely aware of this problem, but there is nothing
1084 * gained by involving the user in this problem that hopefully will get resolved
1087 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gdb&pr=2395
1089 * For now, the default is to fix up things to make current GDB versions work.
1090 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1092 memset(buffer
, 0, len
);
1096 if (retval
== ERROR_OK
)
1098 hex_buffer
= malloc(len
* 2 + 1);
1101 for (i
= 0; i
< len
; i
++)
1104 hex_buffer
[2 * i
] = DIGITS
[(t
>> 4) & 0xf];
1105 hex_buffer
[2 * i
+ 1] = DIGITS
[t
& 0xf];
1108 gdb_put_packet(connection
, hex_buffer
, len
* 2);
1114 retval
= gdb_error(connection
, retval
);
1122 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1133 /* skip command character */
1136 addr
= strtoul(packet
, &separator
, 16);
1138 if (*separator
!= ',')
1140 LOG_ERROR("incomplete write memory packet received, dropping connection");
1141 return ERROR_SERVER_REMOTE_CLOSED
;
1144 len
= strtoul(separator
+1, &separator
, 16);
1146 if (*(separator
++) != ':')
1148 LOG_ERROR("incomplete write memory packet received, dropping connection");
1149 return ERROR_SERVER_REMOTE_CLOSED
;
1152 buffer
= malloc(len
);
1154 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1156 for (i
=0; i
<len
; i
++)
1159 sscanf(separator
+ 2*i
, "%2x", &tmp
);
1163 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1165 if (retval
== ERROR_OK
)
1167 gdb_put_packet(connection
, "OK", 2);
1171 retval
= gdb_error(connection
, retval
);
1179 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1187 /* skip command character */
1190 addr
= strtoul(packet
, &separator
, 16);
1192 if (*separator
!= ',')
1194 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1195 return ERROR_SERVER_REMOTE_CLOSED
;
1198 len
= strtoul(separator
+1, &separator
, 16);
1200 if (*(separator
++) != ':')
1202 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1203 return ERROR_SERVER_REMOTE_CLOSED
;
1209 LOG_DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1211 retval
= target_write_buffer(target
, addr
, len
, (u8
*)separator
);
1214 if (retval
== ERROR_OK
)
1216 gdb_put_packet(connection
, "OK", 2);
1220 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1227 int gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1231 int retval
=ERROR_OK
;
1235 if (packet_size
> 1)
1237 packet
[packet_size
] = 0;
1238 address
= strtoul(packet
+ 1, NULL
, 16);
1245 if (packet
[0] == 'c')
1247 LOG_DEBUG("continue");
1248 target_invoke_script(connection
->cmd_ctx
, target
, "pre_resume");
1249 retval
=target_resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1251 else if (packet
[0] == 's')
1254 retval
=target
->type
->step(target
, current
, address
, 0); /* step at current or address, don't handle breakpoints */
1259 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1262 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1263 enum watchpoint_rw wp_type
;
1271 type
= strtoul(packet
+ 1, &separator
, 16);
1273 if (type
== 0) /* memory breakpoint */
1274 bp_type
= BKPT_SOFT
;
1275 else if (type
== 1) /* hardware breakpoint */
1276 bp_type
= BKPT_HARD
;
1277 else if (type
== 2) /* write watchpoint */
1278 wp_type
= WPT_WRITE
;
1279 else if (type
== 3) /* read watchpoint */
1281 else if (type
== 4) /* access watchpoint */
1282 wp_type
= WPT_ACCESS
;
1284 if (gdb_breakpoint_override
&&((bp_type
==BKPT_SOFT
)||(bp_type
==BKPT_SOFT
)))
1286 bp_type
=gdb_breakpoint_override_type
;
1289 if (*separator
!= ',')
1291 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1292 return ERROR_SERVER_REMOTE_CLOSED
;
1295 address
= strtoul(separator
+1, &separator
, 16);
1297 if (*separator
!= ',')
1299 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1300 return ERROR_SERVER_REMOTE_CLOSED
;
1303 size
= strtoul(separator
+1, &separator
, 16);
1309 if (packet
[0] == 'Z')
1311 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1313 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1318 gdb_put_packet(connection
, "OK", 2);
1323 breakpoint_remove(target
, address
);
1324 gdb_put_packet(connection
, "OK", 2);
1331 if (packet
[0] == 'Z')
1333 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1335 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1340 gdb_put_packet(connection
, "OK", 2);
1345 watchpoint_remove(target
, address
);
1346 gdb_put_packet(connection
, "OK", 2);
1357 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1358 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1360 if (*retval
!= ERROR_OK
)
1368 if ((*xml
== NULL
) || (!first
))
1370 /* start by 0 to exercise all the code paths.
1371 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1373 *size
= *size
* 2 + 2;
1375 *xml
= realloc(*xml
, *size
);
1380 *retval
= ERROR_SERVER_REMOTE_CLOSED
;
1388 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1390 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1395 /* there was just enough or not enough space, allocate more. */
1400 static int decode_xfer_read(char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1404 /* Extract and NUL-terminate the annex. */
1406 while (*buf
&& *buf
!= ':')
1412 /* After the read marker and annex, qXfer looks like a
1413 * traditional 'm' packet. */
1415 *ofs
= strtoul(buf
, &separator
, 16);
1417 if (*separator
!= ',')
1420 *len
= strtoul(separator
+1, NULL
, 16);
1425 int gdb_calc_blocksize(flash_bank_t
*bank
)
1428 int block_size
= 0xffffffff;
1430 /* loop through all sectors and return smallest sector size */
1432 for (i
= 0; i
< bank
->num_sectors
; i
++)
1434 if (bank
->sectors
[i
].size
< block_size
)
1435 block_size
= bank
->sectors
[i
].size
;
1441 static int compare_bank (const void * a
, const void * b
)
1443 flash_bank_t
*b1
, *b2
;
1444 b1
=*((flash_bank_t
**)a
);
1445 b2
=*((flash_bank_t
**)b
);
1447 if (b1
->base
==b2
->base
)
1450 } else if (b1
->base
>b2
->base
)
1459 int gdb_query_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1461 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1463 if (strstr(packet
, "qRcmd,"))
1465 if (packet_size
> 6)
1469 cmd
= malloc((packet_size
- 6)/2 + 1);
1470 for (i
=0; i
< (packet_size
- 6)/2; i
++)
1473 sscanf(packet
+ 6 + 2*i
, "%2x", &tmp
);
1476 cmd
[(packet_size
- 6)/2] = 0x0;
1478 /* We want to print all debug output to GDB connection */
1479 log_add_callback(gdb_log_callback
, connection
);
1480 target_call_timer_callbacks_now();
1481 command_run_line(cmd_ctx
, cmd
);
1482 target_call_timer_callbacks_now();
1483 log_remove_callback(gdb_log_callback
, connection
);
1486 gdb_put_packet(connection
, "OK", 2);
1489 else if (strstr(packet
, "qCRC:"))
1491 if (packet_size
> 5)
1500 /* skip command character */
1503 addr
= strtoul(packet
, &separator
, 16);
1505 if (*separator
!= ',')
1507 LOG_ERROR("incomplete read memory packet received, dropping connection");
1508 return ERROR_SERVER_REMOTE_CLOSED
;
1511 len
= strtoul(separator
+ 1, NULL
, 16);
1513 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1515 if (retval
== ERROR_OK
)
1517 snprintf(gdb_reply
, 10, "C%8.8x", checksum
);
1518 gdb_put_packet(connection
, gdb_reply
, 9);
1522 if ((retval
= gdb_error(connection
, retval
)) != ERROR_OK
)
1529 else if (strstr(packet
, "qSupported"))
1531 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1532 * disable qXfer:features:read for the moment */
1533 int retval
= ERROR_OK
;
1534 char *buffer
= NULL
;
1538 xml_printf(&retval
, &buffer
, &pos
, &size
,
1539 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1540 (GDB_BUFFER_SIZE
- 1), ((gdb_use_memory_map
== 1)&&(flash_get_bank_count()>0)) ? '+' : '-');
1542 if (retval
!= ERROR_OK
)
1544 gdb_send_error(connection
, 01);
1548 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1553 else if (strstr(packet
, "qXfer:memory-map:read::")&&(flash_get_bank_count()>0))
1555 /* We get away with only specifying flash here. Regions that are not
1556 * specified are treated as if we provided no memory map(if not we
1557 * could detect the holes and mark them as RAM).
1558 * Normally we only execute this code once, but no big deal if we
1559 * have to regenerate it a couple of times. */
1565 int retval
= ERROR_OK
;
1572 /* skip command character */
1575 offset
= strtoul(packet
, &separator
, 16);
1576 length
= strtoul(separator
+ 1, &separator
, 16);
1578 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1581 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1582 read/write) by default for GDB.
1583 GDB does not have a concept of non-cacheable read/write memory.
1585 flash_bank_t
**banks
=malloc(sizeof(flash_bank_t
*)*flash_get_bank_count());
1588 for (i
=0; i
<flash_get_bank_count(); i
++)
1590 p
= get_flash_bank_by_num(i
);
1594 retval
= ERROR_FAIL
;
1595 gdb_send_error(connection
, retval
);
1601 qsort(banks
, flash_get_bank_count(), sizeof(flash_bank_t
*), compare_bank
);
1604 for (i
=0; i
<flash_get_bank_count(); i
++)
1608 if (ram_start
<p
->base
)
1610 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1611 ram_start
, p
->base
-ram_start
);
1614 /* if device has uneven sector sizes, eg. str7, lpc
1615 * we pass the smallest sector size to gdb memory map */
1616 blocksize
= gdb_calc_blocksize(p
);
1618 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1619 "<property name=\"blocksize\">0x%x</property>\n" \
1621 p
->base
, p
->size
, blocksize
);
1622 ram_start
=p
->base
+p
->size
;
1626 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1627 ram_start
, 0-ram_start
);
1630 /* a flash chip could be at the very end of the 32 bit address space, in which case
1631 ram_start will be precisely 0 */
1637 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1639 if (retval
!= ERROR_OK
)
1641 gdb_send_error(connection
, retval
);
1645 if (offset
+ length
> pos
)
1647 length
= pos
- offset
;
1650 char *t
= malloc(length
+ 1);
1652 memcpy(t
+ 1, xml
+ offset
, length
);
1653 gdb_put_packet(connection
, t
, length
+ 1);
1659 else if (strstr(packet
, "qXfer:features:read:"))
1664 int retval
= ERROR_OK
;
1667 unsigned int length
;
1670 /* skip command character */
1673 if (decode_xfer_read(packet
, &annex
, &offset
, &length
) < 0)
1675 gdb_send_error(connection
, 01);
1679 if (strcmp(annex
, "target.xml") != 0)
1681 gdb_send_error(connection
, 01);
1685 xml_printf(&retval
, &xml
, &pos
, &size
, \
1686 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1688 if (retval
!= ERROR_OK
)
1690 gdb_send_error(connection
, retval
);
1694 gdb_put_packet(connection
, xml
, strlen(xml
));
1700 gdb_put_packet(connection
, "", 0);
1704 int gdb_v_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1706 gdb_connection_t
*gdb_connection
= connection
->priv
;
1707 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1710 /* if flash programming disabled - send a empty reply */
1712 if (gdb_flash_program
== 0)
1714 gdb_put_packet(connection
, "", 0);
1718 if (strstr(packet
, "vFlashErase:"))
1721 unsigned long length
;
1723 char *parse
= packet
+ 12;
1726 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1727 return ERROR_SERVER_REMOTE_CLOSED
;
1730 addr
= strtoul(parse
, &parse
, 16);
1732 if (*(parse
++) != ',' || *parse
== '\0')
1734 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1735 return ERROR_SERVER_REMOTE_CLOSED
;
1738 length
= strtoul(parse
, &parse
, 16);
1742 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1743 return ERROR_SERVER_REMOTE_CLOSED
;
1746 /* assume all sectors need erasing - stops any problems
1747 * when flash_write is called multiple times */
1750 /* perform any target specific operations before the erase */
1751 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_PROGRAM
);
1754 if ((result
= flash_erase_address_range(gdb_service
->target
, addr
, length
)) != ERROR_OK
)
1756 /* GDB doesn't evaluate the actual error number returned,
1757 * treat a failed erase as an I/O error
1759 gdb_send_error(connection
, EIO
);
1760 LOG_ERROR("flash_erase returned %i", result
);
1763 gdb_put_packet(connection
, "OK", 2);
1768 if (strstr(packet
, "vFlashWrite:"))
1771 unsigned long length
;
1772 char *parse
= packet
+ 12;
1776 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1777 return ERROR_SERVER_REMOTE_CLOSED
;
1779 addr
= strtoul(parse
, &parse
, 16);
1780 if (*(parse
++) != ':')
1782 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1783 return ERROR_SERVER_REMOTE_CLOSED
;
1785 length
= packet_size
- (parse
- packet
);
1787 /* create a new image if there isn't already one */
1788 if (gdb_connection
->vflash_image
== NULL
)
1790 gdb_connection
->vflash_image
= malloc(sizeof(image_t
));
1791 image_open(gdb_connection
->vflash_image
, "", "build");
1794 /* create new section with content from packet buffer */
1795 image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (u8
*)parse
);
1797 gdb_put_packet(connection
, "OK", 2);
1802 if (!strcmp(packet
, "vFlashDone"))
1806 /* process the flashing buffer. No need to erase as GDB
1807 * always issues a vFlashErase first. */
1808 if ((result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, 0)) != ERROR_OK
)
1810 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1811 gdb_put_packet(connection
, "E.memtype", 9);
1813 gdb_send_error(connection
, EIO
);
1817 LOG_DEBUG("wrote %u bytes from vFlash image to flash", written
);
1818 gdb_put_packet(connection
, "OK", 2);
1821 image_close(gdb_connection
->vflash_image
);
1822 free(gdb_connection
->vflash_image
);
1823 gdb_connection
->vflash_image
= NULL
;
1828 gdb_put_packet(connection
, "", 0);
1832 int gdb_detach(connection_t
*connection
, target_t
*target
)
1834 switch( detach_mode
)
1836 case GDB_DETACH_RESUME
:
1837 target_invoke_script(connection
->cmd_ctx
, target
, "pre_resume");
1838 target_resume(target
, 1, 0, 1, 0);
1841 case GDB_DETACH_RESET
:
1842 /* FIX?? make this configurable?? */
1843 target_process_reset(connection
->cmd_ctx
, RESET_HALT
);
1846 case GDB_DETACH_HALT
:
1847 target_halt(target
);
1850 case GDB_DETACH_NOTHING
:
1854 gdb_put_packet(connection
, "OK", 2);
1859 static void gdb_log_callback(void *priv
, const char *file
, int line
,
1860 const char *function
, const char *string
)
1862 connection_t
*connection
= priv
;
1863 gdb_connection_t
*gdb_con
= connection
->priv
;
1867 /* do not reply this using the O packet */
1871 gdb_output_con(connection
, string
);
1874 /* Do not allocate this on the stack */
1875 char gdb_packet_buffer
[GDB_BUFFER_SIZE
];
1877 static void gdb_sig_halted(connection_t
*connection
)
1880 snprintf(sig_reply
, 4, "T%2.2x", 2);
1881 gdb_put_packet(connection
, sig_reply
, 3);
1885 int gdb_input_inner(connection_t
*connection
)
1887 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1888 target_t
*target
= gdb_service
->target
;
1889 char *packet
=gdb_packet_buffer
;
1892 gdb_connection_t
*gdb_con
= connection
->priv
;
1893 static int extended_protocol
= 0;
1895 /* drain input buffer */
1898 packet_size
= GDB_BUFFER_SIZE
-1;
1899 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
1904 /* terminate with zero */
1905 packet
[packet_size
] = 0;
1907 LOG_DEBUG("received packet: '%s'", packet
);
1909 if (packet_size
> 0)
1915 /* Hct... -- set thread
1916 * we don't have threads, send empty reply */
1917 gdb_put_packet(connection
, NULL
, 0);
1920 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
1923 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
1926 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
1929 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
1932 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
1935 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
1938 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
1942 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
1945 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
1950 if (target
->state
!= TARGET_HALTED
)
1952 /* If the target isn't in the halted state, then we can't
1953 * step/continue. This might be early setup, etc.
1955 gdb_sig_halted(connection
);
1958 /* We're running/stepping, in which case we can
1959 * forward log output until the target is halted
1961 gdb_connection_t
*gdb_con
= connection
->priv
;
1962 gdb_con
->frontend_state
= TARGET_RUNNING
;
1963 log_add_callback(gdb_log_callback
, connection
);
1964 int retval
=gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
1965 if (retval
!=ERROR_OK
)
1967 /* we'll never receive a halted condition... issue a false one.. */
1968 gdb_frontend_halted(target
, connection
);
1974 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
1977 retval
= gdb_detach(connection
, target
);
1978 extended_protocol
= 0;
1981 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
1985 if (extended_protocol
!= 0)
1987 gdb_put_packet(connection
, "OK", 2);
1988 return ERROR_SERVER_REMOTE_CLOSED
;
1990 /* handle extended remote protocol */
1991 extended_protocol
= 1;
1992 gdb_put_packet(connection
, "OK", 2);
1995 /* handle extended restart packet */
1996 command_run_linef(connection
->cmd_ctx
, "ocd_gdb_restart %d", get_num_by_target(target
));
1999 /* ignore unkown packets */
2000 LOG_DEBUG("ignoring 0x%2.2x packet", packet
[0]);
2001 gdb_put_packet(connection
, NULL
, 0);
2005 /* if a packet handler returned an error, exit input loop */
2006 if (retval
!= ERROR_OK
)
2010 if (gdb_con
->ctrl_c
)
2012 if (target
->state
== TARGET_RUNNING
)
2014 target_halt(target
);
2015 gdb_con
->ctrl_c
= 0;
2019 } while (gdb_con
->buf_cnt
> 0);
2024 int gdb_input(connection_t
*connection
)
2026 int retval
= gdb_input_inner(connection
);
2027 gdb_connection_t
*gdb_con
= connection
->priv
;
2028 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
2031 /* logging does not propagate the error, yet can set th gdb_con->closed flag */
2032 if (gdb_con
->closed
)
2033 return ERROR_SERVER_REMOTE_CLOSED
;
2035 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2041 gdb_service_t
*gdb_service
;
2042 target_t
*target
= targets
;
2047 LOG_WARNING("no gdb ports allocated as no target has been specified");
2053 LOG_WARNING("no gdb port specified, using default port 3333");
2059 char service_name
[8];
2061 snprintf(service_name
, 8, "gdb-%2.2i", i
);
2063 gdb_service
= malloc(sizeof(gdb_service_t
));
2064 gdb_service
->target
= target
;
2066 add_service("gdb", CONNECTION_GDB
, gdb_port
+ i
, 1, gdb_new_connection
, gdb_input
, gdb_connection_closed
, gdb_service
);
2068 LOG_DEBUG("gdb service for target %s at port %i", target
->type
->name
, gdb_port
+ i
);
2071 target
= target
->next
;
2077 /* daemon configuration command gdb_port */
2078 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2083 /* only if the port wasn't overwritten by cmdline */
2085 gdb_port
= strtoul(args
[0], NULL
, 0);
2090 int handle_gdb_detach_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2094 if (strcmp(args
[0], "resume") == 0)
2096 detach_mode
= GDB_DETACH_RESUME
;
2099 else if (strcmp(args
[0], "reset") == 0)
2101 detach_mode
= GDB_DETACH_RESET
;
2104 else if (strcmp(args
[0], "halt") == 0)
2106 detach_mode
= GDB_DETACH_HALT
;
2109 else if (strcmp(args
[0], "nothing") == 0)
2111 detach_mode
= GDB_DETACH_NOTHING
;
2116 LOG_WARNING("invalid gdb_detach configuration directive: %s", args
[0]);
2120 int handle_gdb_memory_map_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2124 if (strcmp(args
[0], "enable") == 0)
2126 gdb_use_memory_map
= 1;
2129 else if (strcmp(args
[0], "disable") == 0)
2131 gdb_use_memory_map
= 0;
2136 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args
[0]);
2140 int handle_gdb_flash_program_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2144 if (strcmp(args
[0], "enable") == 0)
2146 gdb_flash_program
= 1;
2149 else if (strcmp(args
[0], "disable") == 0)
2151 gdb_flash_program
= 0;
2156 LOG_WARNING("invalid gdb_memory_map configuration directive: %s", args
[0]);
2160 int handle_gdb_report_data_abort_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2164 if (strcmp(args
[0], "enable") == 0)
2166 gdb_report_data_abort
= 1;
2169 else if (strcmp(args
[0], "disable") == 0)
2171 gdb_report_data_abort
= 0;
2176 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args
[0]);
2180 /* daemon configuration command gdb_port */
2181 int handle_gdb_breakpoint_override_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2188 gdb_breakpoint_override
= 1;
2189 if (strcmp(args
[0], "hard")==0)
2191 gdb_breakpoint_override_type
=BKPT_HARD
;
2192 } else if (strcmp(args
[0], "soft")==0)
2194 gdb_breakpoint_override_type
=BKPT_SOFT
;
2195 } else if (strcmp(args
[0], "disable") == 0)
2197 gdb_breakpoint_override
= 0;
2201 return ERROR_COMMAND_SYNTAX_ERROR
;
2203 if (gdb_breakpoint_override
)
2205 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type
==BKPT_HARD
)?"hard":"soft");
2208 LOG_USER("breakpoint type is not overriden");
2215 int gdb_register_commands(command_context_t
*command_context
)
2217 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
2218 COMMAND_CONFIG
, "");
2219 register_command(command_context
, NULL
, "gdb_detach", handle_gdb_detach_command
,
2220 COMMAND_CONFIG
, "");
2221 register_command(command_context
, NULL
, "gdb_memory_map", handle_gdb_memory_map_command
,
2222 COMMAND_CONFIG
, "");
2223 register_command(command_context
, NULL
, "gdb_flash_program", handle_gdb_flash_program_command
,
2224 COMMAND_CONFIG
, "");
2225 register_command(command_context
, NULL
, "gdb_report_data_abort", handle_gdb_report_data_abort_command
,
2226 COMMAND_CONFIG
, "");
2227 register_command(command_context
, NULL
, "gdb_breakpoint_override", handle_gdb_breakpoint_override_command
,
2228 COMMAND_EXEC
, "hard/soft/disabled - force breakpoint type for gdb 'break' commands."
2229 "The raison d'etre for this option is to support GDB GUI's without "
2230 "a hard/soft breakpoint concept where the default OpenOCD behaviour "
2231 "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)