1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "replacements.h"
26 #include "gdb_server.h"
30 #include "binarybuffer.h"
32 #include "breakpoints.h"
34 #include "target_request.h"
42 #define _DEBUG_GDB_IO_
45 static unsigned short gdb_port
;
55 /* target behaviour on gdb detach */
56 enum gdb_detach_mode detach_mode
= GDB_DETACH_RESUME
;
58 /* set if we are sending a memory map to gdb
59 * via qXfer:memory-map:read packet */
60 int gdb_use_memory_map
= 0;
61 int gdb_flash_program
= 0;
63 int gdb_last_signal(target_t
*target
)
65 switch (target
->debug_reason
)
67 case DBG_REASON_DBGRQ
:
68 return 0x2; /* SIGINT */
69 case DBG_REASON_BREAKPOINT
:
70 case DBG_REASON_WATCHPOINT
:
71 case DBG_REASON_WPTANDBKPT
:
72 return 0x05; /* SIGTRAP */
73 case DBG_REASON_SINGLESTEP
:
74 return 0x05; /* SIGTRAP */
75 case DBG_REASON_NOTHALTED
:
76 return 0x0; /* no signal... shouldn't happen */
78 ERROR("BUG: undefined debug reason");
83 int gdb_get_char(connection_t
*connection
, int* next_char
)
85 gdb_connection_t
*gdb_con
= connection
->priv
;
91 if (gdb_con
->buf_cnt
-- > 0)
93 *next_char
= *(gdb_con
->buf_p
++);
94 if (gdb_con
->buf_cnt
> 0)
95 connection
->input_pending
= 1;
97 connection
->input_pending
= 0;
100 DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
106 while ((gdb_con
->buf_cnt
= read_socket(connection
->fd
, gdb_con
->buffer
, GDB_BUFFER_SIZE
)) <= 0)
108 if (gdb_con
->buf_cnt
== 0)
109 return ERROR_SERVER_REMOTE_CLOSED
;
112 errno
= WSAGetLastError();
119 case WSAECONNABORTED
:
120 return ERROR_SERVER_REMOTE_CLOSED
;
122 return ERROR_SERVER_REMOTE_CLOSED
;
124 ERROR("read: %d", errno
);
134 return ERROR_SERVER_REMOTE_CLOSED
;
136 return ERROR_SERVER_REMOTE_CLOSED
;
138 ERROR("read: %s", strerror(errno
));
144 #ifdef _DEBUG_GDB_IO_
145 debug_buffer
= malloc(gdb_con
->buf_cnt
+ 1);
146 memcpy(debug_buffer
, gdb_con
->buffer
, gdb_con
->buf_cnt
);
147 debug_buffer
[gdb_con
->buf_cnt
] = 0;
148 DEBUG("received '%s'", debug_buffer
);
152 gdb_con
->buf_p
= gdb_con
->buffer
;
154 *next_char
= *(gdb_con
->buf_p
++);
155 if (gdb_con
->buf_cnt
> 0)
156 connection
->input_pending
= 1;
158 connection
->input_pending
= 0;
159 #ifdef _DEBUG_GDB_IO_
160 DEBUG("returned char '%c' (0x%2.2x)", *next_char
, *next_char
);
166 int gdb_putback_char(connection_t
*connection
, int last_char
)
168 gdb_connection_t
*gdb_con
= connection
->priv
;
170 if (gdb_con
->buf_p
> gdb_con
->buffer
)
172 *(--gdb_con
->buf_p
) = last_char
;
177 ERROR("BUG: couldn't put character back");
183 int gdb_put_packet(connection_t
*connection
, char *buffer
, int len
)
186 unsigned char my_checksum
= 0;
191 gdb_connection_t
*gdb_con
= connection
->priv
;
193 for (i
= 0; i
< len
; i
++)
194 my_checksum
+= buffer
[i
];
198 debug_buffer
= malloc(len
+ 1);
199 memcpy(debug_buffer
, buffer
, len
);
200 debug_buffer
[len
] = 0;
201 DEBUG("sending packet '$%s#%2.2x'", debug_buffer
, my_checksum
);
204 write_socket(connection
->fd
, "$", 1);
206 write_socket(connection
->fd
, buffer
, len
);
207 write_socket(connection
->fd
, "#", 1);
209 snprintf(checksum
, 3, "%2.2x", my_checksum
);
211 write_socket(connection
->fd
, checksum
, 2);
213 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
218 else if (reply
== '-')
219 WARNING("negative reply, retrying");
220 else if (reply
== 0x3)
223 if ((retval
= gdb_get_char(connection
, &reply
)) != ERROR_OK
)
227 else if (reply
== '-')
228 WARNING("negative reply, retrying");
231 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
232 return ERROR_SERVER_REMOTE_CLOSED
;
237 ERROR("unknown character 0x%2.2x in reply, dropping connection", reply
);
238 return ERROR_SERVER_REMOTE_CLOSED
;
245 int gdb_get_packet(connection_t
*connection
, char *buffer
, int *len
)
251 unsigned char my_checksum
= 0;
252 gdb_connection_t
*gdb_con
= connection
->priv
;
258 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
261 #ifdef _DEBUG_GDB_IO_
262 DEBUG("character: '%c'", character
);
270 WARNING("acknowledgment received, but no packet pending");
273 WARNING("negative acknowledgment, but no packet pending");
280 WARNING("ignoring character 0x%x", character
);
283 } while (character
!= '$');
289 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
292 if (character
== '#') break;
294 if (character
== '}')
296 /* data transmitted in binary mode (X packet)
297 * uses 0x7d as escape character */
298 my_checksum
+= character
& 0xff;
299 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
301 my_checksum
+= character
& 0xff;
302 buffer
[count
++] = (character
^ 0x20) & 0xff;
306 my_checksum
+= character
& 0xff;
307 buffer
[count
++] = character
& 0xff;
312 ERROR("packet buffer too small");
313 return ERROR_GDB_BUFFER_TOO_SMALL
;
319 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
321 checksum
[0] = character
;
322 if ((retval
= gdb_get_char(connection
, &character
)) != ERROR_OK
)
324 checksum
[1] = character
;
327 if (my_checksum
== strtoul(checksum
, NULL
, 16))
329 write_socket(connection
->fd
, "+", 1);
333 WARNING("checksum error, requesting retransmission");
334 write_socket(connection
->fd
, "-", 1);
340 int gdb_output(struct command_context_s
*context
, char* line
)
342 connection_t
*connection
= context
->output_handler_priv
;
343 gdb_connection_t
*gdb_connection
= connection
->priv
;
348 /* check if output is enabled */
349 if (gdb_connection
->output_disable
)
354 bin_size
= strlen(line
);
356 hex_buffer
= malloc(bin_size
*2 + 4);
359 for (i
=0; i
<bin_size
; i
++)
360 snprintf(hex_buffer
+ 1 + i
*2, 3, "%2.2x", line
[i
]);
361 hex_buffer
[bin_size
*2+1] = '0';
362 hex_buffer
[bin_size
*2+2] = 'a';
363 hex_buffer
[bin_size
*2+3] = 0x0;
365 gdb_put_packet(connection
, hex_buffer
, bin_size
*2 + 3);
371 int gdb_program_handler(struct target_s
*target
, enum target_event event
, void *priv
)
374 struct command_context_s
*cmd_ctx
= priv
;
376 if (target
->gdb_program_script
)
378 script
= fopen(target
->gdb_program_script
, "r");
381 ERROR("couldn't open script file %s", target
->gdb_program_script
);
385 INFO("executing gdb_program script '%s'", target
->gdb_program_script
);
386 command_run_file(cmd_ctx
, script
, COMMAND_EXEC
);
389 jtag_execute_queue();
395 int gdb_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
)
397 connection_t
*connection
= priv
;
398 gdb_connection_t
*gdb_connection
= connection
->priv
;
404 case TARGET_EVENT_HALTED
:
405 if (gdb_connection
->frontend_state
== TARGET_RUNNING
)
407 if (gdb_connection
->ctrl_c
)
410 gdb_connection
->ctrl_c
= 0;
414 signal
= gdb_last_signal(target
);
417 snprintf(sig_reply
, 4, "T%2.2x", signal
);
418 gdb_put_packet(connection
, sig_reply
, 3);
419 gdb_connection
->frontend_state
= TARGET_HALTED
;
422 case TARGET_EVENT_RESUMED
:
423 if (gdb_connection
->frontend_state
== TARGET_HALTED
)
425 gdb_connection
->frontend_state
= TARGET_RUNNING
;
428 case TARGET_EVENT_GDB_PROGRAM
:
429 gdb_program_handler(target
, event
, connection
->cmd_ctx
);
438 int gdb_new_connection(connection_t
*connection
)
440 gdb_connection_t
*gdb_connection
= malloc(sizeof(gdb_connection_t
));
441 gdb_service_t
*gdb_service
= connection
->service
->priv
;
445 connection
->priv
= gdb_connection
;
447 /* initialize gdb connection information */
448 gdb_connection
->buf_p
= gdb_connection
->buffer
;
449 gdb_connection
->buf_cnt
= 0;
450 gdb_connection
->ctrl_c
= 0;
451 gdb_connection
->frontend_state
= TARGET_HALTED
;
452 gdb_connection
->vflash_image
= NULL
;
453 gdb_connection
->output_disable
= 0;
455 /* output goes through gdb connection */
456 command_set_output_handler(connection
->cmd_ctx
, gdb_output
, connection
);
458 /* register callback to be informed about target events */
459 target_register_event_callback(gdb_target_callback_event_handler
, connection
);
461 /* a gdb session just attached, put the target in halt mode */
462 if (((retval
= gdb_service
->target
->type
->halt(gdb_service
->target
)) != ERROR_OK
) &&
463 (retval
!= ERROR_TARGET_ALREADY_HALTED
))
465 ERROR("error when trying to halt target");
469 while (gdb_service
->target
->state
!= TARGET_HALTED
)
471 gdb_service
->target
->type
->poll(gdb_service
->target
);
474 /* remove the initial ACK from the incoming buffer */
475 if ((retval
= gdb_get_char(connection
, &initial_ack
)) != ERROR_OK
)
478 if (initial_ack
!= '+')
479 gdb_putback_char(connection
, initial_ack
);
484 int gdb_connection_closed(connection_t
*connection
)
486 gdb_service_t
*gdb_service
= connection
->service
->priv
;
487 gdb_connection_t
*gdb_connection
= connection
->priv
;
489 /* see if an image built with vFlash commands is left */
490 if (gdb_connection
->vflash_image
)
492 image_close(gdb_connection
->vflash_image
);
493 free(gdb_connection
->vflash_image
);
494 gdb_connection
->vflash_image
= NULL
;
497 /* if this connection registered a debug-message receiver delete it */
498 delete_debug_msg_receiver(connection
->cmd_ctx
, gdb_service
->target
);
500 if (connection
->priv
)
502 free(connection
->priv
);
503 connection
->priv
= NULL
;
507 ERROR("BUG: connection->priv == NULL");
510 target_unregister_event_callback(gdb_target_callback_event_handler
, connection
);
515 void gdb_send_error(connection_t
*connection
, u8 the_error
)
518 snprintf(err
, 4, "E%2.2X", the_error
);
519 gdb_put_packet(connection
, err
, 3);
522 int gdb_last_signal_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
527 signal
= gdb_last_signal(target
);
529 snprintf(sig_reply
, 4, "S%2.2x", signal
);
530 gdb_put_packet(connection
, sig_reply
, 3);
535 void gdb_str_to_target(target_t
*target
, char *str
, char *tstr
)
537 int str_len
= strlen(str
);
542 ERROR("BUG: gdb value with uneven number of characters encountered: %s", str
);
546 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
548 for (i
= 0; i
< str_len
; i
+=2)
550 tstr
[str_len
- i
- 1] = str
[i
+ 1];
551 tstr
[str_len
- i
- 2] = str
[i
];
556 for (i
= 0; i
< str_len
; i
++)
563 void gdb_target_to_str(target_t
*target
, char *tstr
, char *str
)
565 int str_len
= strlen(tstr
);
570 ERROR("BUG: gdb value with uneven number of characters encountered");
574 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
576 for (i
= 0; i
< str_len
; i
+=2)
578 str
[str_len
- i
- 1] = tstr
[i
+ 1];
579 str
[str_len
- i
- 2] = tstr
[i
];
584 for (i
= 0; i
< str_len
; i
++)
591 int gdb_get_registers_packet(connection_t
*connection
, target_t
*target
, char* packet
, int packet_size
)
596 int reg_packet_size
= 0;
603 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
607 case ERROR_TARGET_NOT_HALTED
:
608 ERROR("gdb requested registers but we're not halted, dropping connection");
609 return ERROR_SERVER_REMOTE_CLOSED
;
611 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
612 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
617 for (i
= 0; i
< reg_list_size
; i
++)
619 reg_packet_size
+= reg_list
[i
]->size
;
622 reg_packet
= malloc(CEIL(reg_packet_size
, 8) * 2);
623 reg_packet_p
= reg_packet
;
625 for (i
= 0; i
< reg_list_size
; i
++)
627 char *hex_buf
= buf_to_str(reg_list
[i
]->value
, reg_list
[i
]->size
, 16);
628 DEBUG("hex_buf: %s", hex_buf
);
629 gdb_str_to_target(target
, hex_buf
, reg_packet_p
);
630 reg_packet_p
+= CEIL(reg_list
[i
]->size
, 8) * 2;
634 reg_packet_p
= strndup(reg_packet
, CEIL(reg_packet_size
, 8) * 2);
635 DEBUG("reg_packet: %s", reg_packet_p
);
638 gdb_put_packet(connection
, reg_packet
, CEIL(reg_packet_size
, 8) * 2);
646 int gdb_set_registers_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
656 /* skip command character */
662 WARNING("GDB set_registers packet with uneven characters received, dropping connection");
663 return ERROR_SERVER_REMOTE_CLOSED
;
666 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
670 case ERROR_TARGET_NOT_HALTED
:
671 ERROR("gdb tried to registers but we're not halted, dropping connection");
672 return ERROR_SERVER_REMOTE_CLOSED
;
674 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
675 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
681 for (i
= 0; i
< reg_list_size
; i
++)
685 reg_arch_type_t
*arch_type
;
687 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
688 hex_buf
= malloc(CEIL(reg_list
[i
]->size
, 8) * 2);
689 gdb_target_to_str(target
, packet_p
, hex_buf
);
691 /* convert hex-string to binary buffer */
692 bin_buf
= malloc(CEIL(reg_list
[i
]->size
, 8));
693 str_to_buf(hex_buf
, CEIL(reg_list
[i
]->size
, 8) * 2, bin_buf
, reg_list
[i
]->size
, 16);
695 /* get register arch_type, and call set method */
696 arch_type
= register_get_arch_type(reg_list
[i
]->arch_type
);
697 if (arch_type
== NULL
)
699 ERROR("BUG: encountered unregistered arch type");
702 arch_type
->set(reg_list
[i
], bin_buf
);
704 /* advance packet pointer */
705 packet_p
+= (CEIL(reg_list
[i
]->size
, 8) * 2);
711 /* free reg_t *reg_list[] array allocated by get_gdb_reg_list */
714 gdb_put_packet(connection
, "OK", 2);
719 int gdb_get_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
722 int reg_num
= strtoul(packet
+ 1, NULL
, 16);
730 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
734 case ERROR_TARGET_NOT_HALTED
:
735 ERROR("gdb requested registers but we're not halted, dropping connection");
736 return ERROR_SERVER_REMOTE_CLOSED
;
738 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
739 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
744 if (reg_list_size
<= reg_num
)
746 ERROR("gdb requested a non-existing register");
750 reg_packet
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
752 hex_buf
= buf_to_str(reg_list
[reg_num
]->value
, reg_list
[reg_num
]->size
, 16);
754 gdb_str_to_target(target
, hex_buf
, reg_packet
);
756 gdb_put_packet(connection
, reg_packet
, CEIL(reg_list
[reg_num
]->size
, 8) * 2);
765 int gdb_set_register_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
770 int reg_num
= strtoul(packet
+ 1, &separator
, 16);
774 reg_arch_type_t
*arch_type
;
778 if ((retval
= target
->type
->get_gdb_reg_list(target
, ®_list
, ®_list_size
)) != ERROR_OK
)
782 case ERROR_TARGET_NOT_HALTED
:
783 ERROR("gdb tried to set a register but we're not halted, dropping connection");
784 return ERROR_SERVER_REMOTE_CLOSED
;
786 /* this is a bug condition - get_gdb_reg_list() may not return any other error */
787 ERROR("BUG: unexpected error returned by get_gdb_reg_list()");
792 if (reg_list_size
< reg_num
)
794 ERROR("gdb requested a non-existing register");
795 return ERROR_SERVER_REMOTE_CLOSED
;
798 if (*separator
!= '=')
800 ERROR("GDB 'set register packet', but no '=' following the register number");
801 return ERROR_SERVER_REMOTE_CLOSED
;
804 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
805 hex_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8) * 2);
806 gdb_target_to_str(target
, separator
+ 1, hex_buf
);
808 /* convert hex-string to binary buffer */
809 bin_buf
= malloc(CEIL(reg_list
[reg_num
]->size
, 8));
810 str_to_buf(hex_buf
, CEIL(reg_list
[reg_num
]->size
, 8) * 2, bin_buf
, reg_list
[reg_num
]->size
, 16);
812 /* get register arch_type, and call set method */
813 arch_type
= register_get_arch_type(reg_list
[reg_num
]->arch_type
);
814 if (arch_type
== NULL
)
816 ERROR("BUG: encountered unregistered arch type");
819 arch_type
->set(reg_list
[reg_num
], bin_buf
);
821 gdb_put_packet(connection
, "OK", 2);
830 int gdb_memory_packet_error(connection_t
*connection
, int retval
)
834 case ERROR_TARGET_NOT_HALTED
:
835 ERROR("gdb tried to read memory but we're not halted, dropping connection");
836 return ERROR_SERVER_REMOTE_CLOSED
;
838 case ERROR_TARGET_DATA_ABORT
:
839 gdb_send_error(connection
, EIO
);
841 case ERROR_TARGET_TRANSLATION_FAULT
:
842 gdb_send_error(connection
, EFAULT
);
844 case ERROR_TARGET_UNALIGNED_ACCESS
:
845 gdb_send_error(connection
, EFAULT
);
848 ERROR("BUG: unexpected error %i", retval
);
855 int gdb_read_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
867 /* skip command character */
870 addr
= strtoul(packet
, &separator
, 16);
872 if (*separator
!= ',')
874 ERROR("incomplete read memory packet received, dropping connection");
875 return ERROR_SERVER_REMOTE_CLOSED
;
878 len
= strtoul(separator
+1, NULL
, 16);
880 buffer
= malloc(len
);
882 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
888 retval
= target
->type
->read_memory(target
, addr
, 4, 1, buffer
);
890 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
894 retval
= target
->type
->read_memory(target
, addr
, 2, 1, buffer
);
896 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
899 if (((addr
% 4) == 0) && ((len
% 4) == 0))
900 retval
= target
->type
->read_memory(target
, addr
, 4, len
/ 4, buffer
);
902 retval
= target
->type
->read_memory(target
, addr
, 1, len
, buffer
);
905 if (retval
== ERROR_OK
)
907 hex_buffer
= malloc(len
* 2 + 1);
909 for (i
=0; i
<len
; i
++)
910 snprintf(hex_buffer
+ 2*i
, 3, "%2.2x", buffer
[i
]);
912 gdb_put_packet(connection
, hex_buffer
, len
* 2);
918 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
927 int gdb_write_memory_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
938 /* skip command character */
941 addr
= strtoul(packet
, &separator
, 16);
943 if (*separator
!= ',')
945 ERROR("incomplete write memory packet received, dropping connection");
946 return ERROR_SERVER_REMOTE_CLOSED
;
949 len
= strtoul(separator
+1, &separator
, 16);
951 if (*(separator
++) != ':')
953 ERROR("incomplete write memory packet received, dropping connection");
954 return ERROR_SERVER_REMOTE_CLOSED
;
957 buffer
= malloc(len
);
959 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
961 for (i
=0; i
<len
; i
++)
964 sscanf(separator
+ 2*i
, "%2x", &tmp
);
971 /* handle sized writes */
974 retval
= target
->type
->write_memory(target
, addr
, 4, 1, buffer
);
976 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
980 retval
= target
->type
->write_memory(target
, addr
, 2, 1, buffer
);
982 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
986 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
988 /* handle bulk writes */
990 retval
= target_write_buffer(target
, addr
, len
, buffer
);
994 if (retval
== ERROR_OK
)
996 gdb_put_packet(connection
, "OK", 2);
1000 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
1009 int gdb_write_memory_binary_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1018 /* skip command character */
1021 addr
= strtoul(packet
, &separator
, 16);
1023 if (*separator
!= ',')
1025 ERROR("incomplete write memory binary packet received, dropping connection");
1026 return ERROR_SERVER_REMOTE_CLOSED
;
1029 len
= strtoul(separator
+1, &separator
, 16);
1031 if (*(separator
++) != ':')
1033 ERROR("incomplete write memory binary packet received, dropping connection");
1034 return ERROR_SERVER_REMOTE_CLOSED
;
1040 buffer
= malloc(len
);
1042 DEBUG("addr: 0x%8.8x, len: 0x%8.8x", addr
, len
);
1044 memcpy( buffer
, separator
, len
);
1049 if ((addr
% 4) == 0)
1050 retval
= target
->type
->write_memory(target
, addr
, 4, 1, buffer
);
1052 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1055 if ((addr
% 2) == 0)
1056 retval
= target
->type
->write_memory(target
, addr
, 2, 1, buffer
);
1058 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1062 retval
= target
->type
->write_memory(target
, addr
, 1, len
, buffer
);
1065 retval
= target_write_buffer(target
, addr
, len
, buffer
);
1072 if (retval
== ERROR_OK
)
1074 gdb_put_packet(connection
, "OK", 2);
1078 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
1085 void gdb_step_continue_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1092 if (packet_size
> 1)
1094 packet
[packet_size
] = 0;
1095 address
= strtoul(packet
+ 1, NULL
, 16);
1102 if (packet
[0] == 'c')
1105 target
->type
->resume(target
, current
, address
, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1107 else if (packet
[0] == 's')
1110 target
->type
->step(target
, current
, address
, 0); /* step at current or address, don't handle breakpoints */
1114 int gdb_bp_wp_packet_error(connection_t
*connection
, int retval
)
1118 case ERROR_TARGET_NOT_HALTED
:
1119 ERROR("gdb tried to set a breakpoint but we're not halted, dropping connection");
1120 return ERROR_SERVER_REMOTE_CLOSED
;
1122 case ERROR_TARGET_RESOURCE_NOT_AVAILABLE
:
1123 gdb_send_error(connection
, EBUSY
);
1126 ERROR("BUG: unexpected error %i", retval
);
1133 int gdb_breakpoint_watchpoint_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1136 enum breakpoint_type bp_type
= BKPT_SOFT
/* dummy init to avoid warning */;
1137 enum watchpoint_rw wp_type
;
1145 type
= strtoul(packet
+ 1, &separator
, 16);
1147 if (type
== 0) /* memory breakpoint */
1148 bp_type
= BKPT_SOFT
;
1149 else if (type
== 1) /* hardware breakpoint */
1150 bp_type
= BKPT_HARD
;
1151 else if (type
== 2) /* write watchpoint */
1152 wp_type
= WPT_WRITE
;
1153 else if (type
== 3) /* read watchpoint */
1155 else if (type
== 4) /* access watchpoint */
1156 wp_type
= WPT_ACCESS
;
1158 if (*separator
!= ',')
1160 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1161 return ERROR_SERVER_REMOTE_CLOSED
;
1164 address
= strtoul(separator
+1, &separator
, 16);
1166 if (*separator
!= ',')
1168 ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1169 return ERROR_SERVER_REMOTE_CLOSED
;
1172 size
= strtoul(separator
+1, &separator
, 16);
1178 if (packet
[0] == 'Z')
1180 if ((retval
= breakpoint_add(target
, address
, size
, bp_type
)) != ERROR_OK
)
1182 if ((retval
= gdb_bp_wp_packet_error(connection
, retval
)) != ERROR_OK
)
1187 gdb_put_packet(connection
, "OK", 2);
1192 breakpoint_remove(target
, address
);
1193 gdb_put_packet(connection
, "OK", 2);
1200 if (packet
[0] == 'Z')
1202 if ((retval
= watchpoint_add(target
, address
, size
, type
-2, 0, 0xffffffffu
)) != ERROR_OK
)
1204 if ((retval
= gdb_bp_wp_packet_error(connection
, retval
)) != ERROR_OK
)
1209 gdb_put_packet(connection
, "OK", 2);
1214 watchpoint_remove(target
, address
);
1215 gdb_put_packet(connection
, "OK", 2);
1226 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1227 void xml_printf(int *retval
, char **xml
, int *pos
, int *size
, const char *fmt
, ...)
1229 if (*retval
!= ERROR_OK
)
1237 if ((*xml
== NULL
) || (!first
))
1239 /* start by 0 to exercise all the code paths.
1240 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1242 *size
= *size
* 2 + 2;
1244 *xml
= realloc(*xml
, *size
);
1249 *retval
=ERROR_SERVER_REMOTE_CLOSED
;
1257 ret
= vsnprintf(*xml
+ *pos
, *size
- *pos
, fmt
, ap
);
1259 if ((ret
> 0) && ((ret
+ 1) < *size
- *pos
))
1264 /* there was just enough or not enough space, allocate more. */
1269 static int decode_xfer_read (char *buf
, char **annex
, int *ofs
, unsigned int *len
)
1273 /* Extract and NUL-terminate the annex. */
1275 while (*buf
&& *buf
!= ':')
1281 /* After the read marker and annex, qXfer looks like a
1282 * traditional 'm' packet. */
1284 *ofs
= strtoul(buf
, &separator
, 16);
1286 if (*separator
!= ',')
1289 *len
= strtoul(separator
+1, NULL
, 16);
1294 int gdb_query_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1296 command_context_t
*cmd_ctx
= connection
->cmd_ctx
;
1298 if (strstr(packet
, "qRcmd,"))
1300 if (packet_size
> 6)
1304 cmd
= malloc((packet_size
- 6)/2 + 1);
1305 for (i
=0; i
< (packet_size
- 6)/2; i
++)
1308 sscanf(packet
+ 6 + 2*i
, "%2x", &tmp
);
1311 cmd
[(packet_size
- 6)/2] = 0x0;
1312 command_run_line(cmd_ctx
, cmd
);
1315 gdb_put_packet(connection
, "OK", 2);
1318 else if (strstr(packet
, "qCRC:"))
1320 if (packet_size
> 5)
1329 /* skip command character */
1332 addr
= strtoul(packet
, &separator
, 16);
1334 if (*separator
!= ',')
1336 ERROR("incomplete read memory packet received, dropping connection");
1337 return ERROR_SERVER_REMOTE_CLOSED
;
1340 len
= strtoul(separator
+ 1, NULL
, 16);
1342 retval
= target_checksum_memory(target
, addr
, len
, &checksum
);
1344 if (retval
== ERROR_OK
)
1346 snprintf(gdb_reply
, 10, "C%8.8x", checksum
);
1347 gdb_put_packet(connection
, gdb_reply
, 9);
1351 if ((retval
= gdb_memory_packet_error(connection
, retval
)) != ERROR_OK
)
1358 else if (strstr(packet
, "qSupported"))
1360 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1361 * disable qXfer:features:read for the moment */
1362 int retval
= ERROR_OK
;
1363 char *buffer
= NULL
;
1366 xml_printf(&retval
, &buffer
, &pos
, &size
,
1367 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-",
1368 (GDB_BUFFER_SIZE
- 1), gdb_use_memory_map
== 1 ? '+' : '-');
1371 gdb_put_packet(connection
, buffer
, strlen(buffer
));
1376 else if (strstr(packet
, "qXfer:memory-map:read::"))
1378 /* We get away with only specifying flash here. Regions that are not
1379 * specified are treated as if we provided no memory map(if not we
1380 * could detect the holes and mark them as RAM).
1381 * Normally we only execute this code once, but no big deal if we
1382 * have to regenerate it a couple of times. */
1388 int retval
= ERROR_OK
;
1394 /* skip command character */
1397 offset
= strtoul(packet
, &separator
, 16);
1398 length
= strtoul(separator
+ 1, &separator
, 16);
1400 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory-map>\n");
1405 p
= get_flash_bank_by_num(i
);
1409 xml_printf(&retval
, &xml
, &pos
, &size
, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1410 "<property name=\"blocksize\">0x%x</property>\n" \
1412 p
->base
, p
->size
, p
->size
/p
->num_sectors
);
1416 xml_printf(&retval
, &xml
, &pos
, &size
, "</memory-map>\n");
1418 if (retval
!= ERROR_OK
)
1420 gdb_send_error(connection
, retval
);
1424 if (offset
+ length
> pos
)
1426 length
= pos
- offset
;
1429 char *t
= malloc(length
+ 1);
1431 memcpy(t
+ 1, xml
+ offset
, length
);
1432 gdb_put_packet(connection
, t
, length
+ 1);
1438 else if (strstr(packet
, "qXfer:features:read:"))
1443 int retval
= ERROR_OK
;
1449 /* skip command character */
1452 if (decode_xfer_read( packet
, &annex
, &offset
, &length
) < 0)
1454 gdb_send_error(connection
, 01);
1458 if (strcmp(annex
, "target.xml") != 0)
1460 gdb_send_error(connection
, 01);
1464 xml_printf(&retval
, &xml
, &pos
, &size
, \
1465 "l<target version=\"1.0\">\n<architecture>arm</architecture>\n</target>\n");
1467 if (retval
!= ERROR_OK
)
1469 gdb_send_error(connection
, retval
);
1473 gdb_put_packet(connection
, xml
, strlen(xml
) + 1);
1479 gdb_put_packet(connection
, "", 0);
1483 int gdb_v_packet(connection_t
*connection
, target_t
*target
, char *packet
, int packet_size
)
1485 gdb_connection_t
*gdb_connection
= connection
->priv
;
1486 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1489 /* if flash programming disabled - send a empty reply */
1491 if (gdb_flash_program
== 0)
1493 gdb_put_packet(connection
, "", 0);
1497 if (strstr(packet
, "vFlashErase:"))
1500 unsigned long length
;
1502 char *parse
= packet
+ 12;
1505 ERROR("incomplete vFlashErase packet received, dropping connection");
1506 return ERROR_SERVER_REMOTE_CLOSED
;
1509 addr
= strtoul(parse
, &parse
, 16);
1511 if (*(parse
++) != ',' || *parse
== '\0')
1513 ERROR("incomplete vFlashErase packet received, dropping connection");
1514 return ERROR_SERVER_REMOTE_CLOSED
;
1517 length
= strtoul(parse
, &parse
, 16);
1521 ERROR("incomplete vFlashErase packet received, dropping connection");
1522 return ERROR_SERVER_REMOTE_CLOSED
;
1525 /* disable gdb output while programming */
1526 gdb_connection
->output_disable
= 1;
1528 /* assume all sectors need erasing - stops any problems
1529 * when flash_write is called multiple times */
1532 /* perform any target specific operations before the erase */
1533 target_call_event_callbacks(gdb_service
->target
, TARGET_EVENT_GDB_PROGRAM
);
1536 if ((result
= flash_erase(gdb_service
->target
, addr
, length
)) != ERROR_OK
)
1538 /* GDB doesn't evaluate the actual error number returned,
1539 * treat a failed erase as an I/O error
1541 gdb_send_error(connection
, EIO
);
1542 ERROR("flash_erase returned %i", result
);
1545 gdb_put_packet(connection
, "OK", 2);
1547 /* reenable gdb output */
1548 gdb_connection
->output_disable
= 0;
1553 if (strstr(packet
, "vFlashWrite:"))
1556 unsigned long length
;
1557 char *parse
= packet
+ 12;
1561 ERROR("incomplete vFlashErase packet received, dropping connection");
1562 return ERROR_SERVER_REMOTE_CLOSED
;
1564 addr
= strtoul(parse
, &parse
, 16);
1565 if (*(parse
++) != ':')
1567 ERROR("incomplete vFlashErase packet received, dropping connection");
1568 return ERROR_SERVER_REMOTE_CLOSED
;
1570 length
= packet_size
- (parse
- packet
);
1572 /* disable gdb output while programming */
1573 gdb_connection
->output_disable
= 1;
1575 /* create a new image if there isn't already one */
1576 if (gdb_connection
->vflash_image
== NULL
)
1578 gdb_connection
->vflash_image
= malloc(sizeof(image_t
));
1579 image_open(gdb_connection
->vflash_image
, "", "build");
1582 /* create new section with content from packet buffer */
1583 image_add_section(gdb_connection
->vflash_image
, addr
, length
, 0x0, (u8
*)parse
);
1585 gdb_put_packet(connection
, "OK", 2);
1587 /* reenable gdb output */
1588 gdb_connection
->output_disable
= 0;
1593 if (!strcmp(packet
, "vFlashDone"))
1598 /* disable gdb output while programming */
1599 gdb_connection
->output_disable
= 1;
1601 /* process the flashing buffer */
1602 if ((result
= flash_write(gdb_service
->target
, gdb_connection
->vflash_image
, &written
, &error_str
, NULL
, 0)) != ERROR_OK
)
1604 if (result
== ERROR_FLASH_DST_OUT_OF_BANK
)
1605 gdb_put_packet(connection
, "E.memtype", 9);
1607 gdb_send_error(connection
, EIO
);
1611 ERROR("flash writing failed: %s", error_str
);
1617 DEBUG("wrote %u bytes from vFlash image to flash", written
);
1618 gdb_put_packet(connection
, "OK", 2);
1621 image_close(gdb_connection
->vflash_image
);
1622 free(gdb_connection
->vflash_image
);
1623 gdb_connection
->vflash_image
= NULL
;
1625 /* reenable gdb output */
1626 gdb_connection
->output_disable
= 0;
1631 gdb_put_packet(connection
, "", 0);
1635 int gdb_detach(connection_t
*connection
, target_t
*target
)
1637 switch( detach_mode
)
1639 case GDB_DETACH_RESUME
:
1640 target
->type
->resume(target
, 1, 0, 1, 0);
1643 case GDB_DETACH_RESET
:
1644 target_process_reset(connection
->cmd_ctx
);
1647 case GDB_DETACH_HALT
:
1648 target
->type
->halt(target
);
1651 case GDB_DETACH_NOTHING
:
1655 gdb_put_packet(connection
, "OK", 2);
1660 int gdb_input(connection_t
*connection
)
1662 gdb_service_t
*gdb_service
= connection
->service
->priv
;
1663 target_t
*target
= gdb_service
->target
;
1664 char packet
[GDB_BUFFER_SIZE
];
1667 gdb_connection_t
*gdb_con
= connection
->priv
;
1668 static int extended_protocol
= 0;
1670 /* drain input buffer */
1673 packet_size
= GDB_BUFFER_SIZE
-1;
1674 if ((retval
= gdb_get_packet(connection
, packet
, &packet_size
)) != ERROR_OK
)
1678 case ERROR_GDB_BUFFER_TOO_SMALL
:
1679 ERROR("BUG: buffer supplied for gdb packet was too small");
1681 case ERROR_SERVER_REMOTE_CLOSED
:
1682 return ERROR_SERVER_REMOTE_CLOSED
;
1684 ERROR("BUG: unexpected error");
1689 /* terminate with zero */
1690 packet
[packet_size
] = 0;
1692 DEBUG("received packet: '%s'", packet
);
1694 if (packet_size
> 0)
1700 /* Hct... -- set thread
1701 * we don't have threads, send empty reply */
1702 gdb_put_packet(connection
, NULL
, 0);
1705 retval
= gdb_query_packet(connection
, target
, packet
, packet_size
);
1708 retval
= gdb_get_registers_packet(connection
, target
, packet
, packet_size
);
1711 retval
= gdb_set_registers_packet(connection
, target
, packet
, packet_size
);
1714 retval
= gdb_get_register_packet(connection
, target
, packet
, packet_size
);
1717 retval
= gdb_set_register_packet(connection
, target
, packet
, packet_size
);
1720 retval
= gdb_read_memory_packet(connection
, target
, packet
, packet_size
);
1723 retval
= gdb_write_memory_packet(connection
, target
, packet
, packet_size
);
1727 retval
= gdb_breakpoint_watchpoint_packet(connection
, target
, packet
, packet_size
);
1730 gdb_last_signal_packet(connection
, target
, packet
, packet_size
);
1734 gdb_step_continue_packet(connection
, target
, packet
, packet_size
);
1737 retval
= gdb_v_packet(connection
, target
, packet
, packet_size
);
1740 retval
= gdb_detach(connection
, target
);
1741 extended_protocol
= 0;
1744 if ((retval
= gdb_write_memory_binary_packet(connection
, target
, packet
, packet_size
)) != ERROR_OK
)
1748 if (extended_protocol
!= 0)
1750 gdb_put_packet(connection
, "OK", 2);
1751 return ERROR_SERVER_REMOTE_CLOSED
;
1753 /* handle extended remote protocol */
1754 extended_protocol
= 1;
1755 gdb_put_packet(connection
, "OK", 2);
1758 /* handle extended restart packet */
1759 target_process_reset(connection
->cmd_ctx
);
1762 /* ignore unkown packets */
1763 DEBUG("ignoring 0x%2.2x packet", packet
[0]);
1764 gdb_put_packet(connection
, NULL
, 0);
1768 /* if a packet handler returned an error, exit input loop */
1769 if (retval
!= ERROR_OK
)
1773 if (gdb_con
->ctrl_c
)
1775 if (target
->state
== TARGET_RUNNING
)
1777 target
->type
->halt(target
);
1778 gdb_con
->ctrl_c
= 0;
1782 } while (gdb_con
->buf_cnt
> 0);
1789 gdb_service_t
*gdb_service
;
1790 target_t
*target
= targets
;
1795 WARNING("no gdb ports allocated as no target has been specified");
1801 WARNING("no gdb port specified, using default port 3333");
1807 char service_name
[8];
1809 snprintf(service_name
, 8, "gdb-%2.2i", i
);
1811 gdb_service
= malloc(sizeof(gdb_service_t
));
1812 gdb_service
->target
= target
;
1814 add_service("gdb", CONNECTION_GDB
, gdb_port
+ i
, 1, gdb_new_connection
, gdb_input
, gdb_connection_closed
, gdb_service
);
1816 DEBUG("gdb service for target %s at port %i", target
->type
->name
, gdb_port
+ i
);
1819 target
= target
->next
;
1825 /* daemon configuration command gdb_port */
1826 int handle_gdb_port_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1831 /* only if the port wasn't overwritten by cmdline */
1833 gdb_port
= strtoul(args
[0], NULL
, 0);
1838 int handle_gdb_detach_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1842 if (strcmp(args
[0], "resume") == 0)
1844 detach_mode
= GDB_DETACH_RESUME
;
1847 else if (strcmp(args
[0], "reset") == 0)
1849 detach_mode
= GDB_DETACH_RESET
;
1852 else if (strcmp(args
[0], "halt") == 0)
1854 detach_mode
= GDB_DETACH_HALT
;
1857 else if (strcmp(args
[0], "nothing") == 0)
1859 detach_mode
= GDB_DETACH_NOTHING
;
1864 WARNING("invalid gdb_detach configuration directive: %s", args
[0]);
1868 int handle_gdb_memory_map_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1872 if (strcmp(args
[0], "enable") == 0)
1874 gdb_use_memory_map
= 1;
1877 else if (strcmp(args
[0], "disable") == 0)
1879 gdb_use_memory_map
= 0;
1884 WARNING("invalid gdb_memory_map configuration directive: %s", args
[0]);
1888 int handle_gdb_flash_program_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1892 if (strcmp(args
[0], "enable") == 0)
1894 gdb_flash_program
= 1;
1897 else if (strcmp(args
[0], "disable") == 0)
1899 gdb_flash_program
= 0;
1904 WARNING("invalid gdb_memory_map configuration directive: %s", args
[0]);
1908 int gdb_register_commands(command_context_t
*command_context
)
1910 register_command(command_context
, NULL
, "gdb_port", handle_gdb_port_command
,
1911 COMMAND_CONFIG
, "");
1912 register_command(command_context
, NULL
, "gdb_detach", handle_gdb_detach_command
,
1913 COMMAND_CONFIG
, "");
1914 register_command(command_context
, NULL
, "gdb_memory_map", handle_gdb_memory_map_command
,
1915 COMMAND_CONFIG
, "");
1916 register_command(command_context
, NULL
, "gdb_flash_program", handle_gdb_flash_program_command
,
1917 COMMAND_CONFIG
, "");
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)