target: simplify register get/set ops
[openocd/ztw.git] / src / server / gdb_server.c
blob9581ea63aa35e45abba3ca245c52850971374ccd
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "breakpoints.h"
31 #include "target_request.h"
32 #include "register.h"
33 #include "server.h"
34 #include "flash.h"
35 #include "gdb_server.h"
36 #include "image.h"
37 #include "jtag.h"
40 #if 0
41 #define _DEBUG_GDB_IO_
42 #endif
44 static struct gdb_connection *current_gdb_connection;
46 static int gdb_breakpoint_override;
47 static enum breakpoint_type gdb_breakpoint_override_type;
49 extern int gdb_error(struct connection *connection, int retval);
50 static unsigned short gdb_port = 3333;
51 static const char *DIGITS = "0123456789abcdef";
53 static void gdb_log_callback(void *priv, const char *file, unsigned line,
54 const char *function, const char *string);
56 /* number of gdb connections, mainly to supress gdb related debugging spam
57 * in helper/log.c when no gdb connections are actually active */
58 int gdb_actual_connections;
60 /* set if we are sending a memory map to gdb
61 * via qXfer:memory-map:read packet */
62 /* enabled by default*/
63 int gdb_use_memory_map = 1;
64 /* enabled by default*/
65 int gdb_flash_program = 1;
67 /* if set, data aborts cause an error to be reported in memory read packets
68 * see the code in gdb_read_memory_packet() for further explanations */
69 int gdb_report_data_abort = 0;
71 int gdb_last_signal(struct target *target)
73 switch (target->debug_reason)
75 case DBG_REASON_DBGRQ:
76 return 0x2; /* SIGINT */
77 case DBG_REASON_BREAKPOINT:
78 case DBG_REASON_WATCHPOINT:
79 case DBG_REASON_WPTANDBKPT:
80 return 0x05; /* SIGTRAP */
81 case DBG_REASON_SINGLESTEP:
82 return 0x05; /* SIGTRAP */
83 case DBG_REASON_NOTHALTED:
84 return 0x0; /* no signal... shouldn't happen */
85 default:
86 LOG_USER("undefined debug reason %d - target needs reset", target->debug_reason);
87 return 0x0;
91 int check_pending(struct connection *connection, int timeout_s, int *got_data)
93 /* a non-blocking socket will block if there is 0 bytes available on the socket,
94 * but return with as many bytes as are available immediately
96 struct timeval tv;
97 fd_set read_fds;
98 struct gdb_connection *gdb_con = connection->priv;
99 int t;
100 if (got_data == NULL)
101 got_data=&t;
102 *got_data = 0;
104 if (gdb_con->buf_cnt > 0)
106 *got_data = 1;
107 return ERROR_OK;
110 FD_ZERO(&read_fds);
111 FD_SET(connection->fd, &read_fds);
113 tv.tv_sec = timeout_s;
114 tv.tv_usec = 0;
115 if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0)
117 /* This can typically be because a "monitor" command took too long
118 * before printing any progress messages
120 if (timeout_s > 0)
122 return ERROR_GDB_TIMEOUT;
123 } else
125 return ERROR_OK;
128 *got_data = FD_ISSET(connection->fd, &read_fds) != 0;
129 return ERROR_OK;
132 int gdb_get_char(struct connection *connection, int* next_char)
134 struct gdb_connection *gdb_con = connection->priv;
135 int retval = ERROR_OK;
137 #ifdef _DEBUG_GDB_IO_
138 char *debug_buffer;
139 #endif
141 if (gdb_con->buf_cnt-- > 0)
143 *next_char = *(gdb_con->buf_p++);
144 if (gdb_con->buf_cnt > 0)
145 connection->input_pending = 1;
146 else
147 connection->input_pending = 0;
149 #ifdef _DEBUG_GDB_IO_
150 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
151 #endif
153 return ERROR_OK;
156 for (;;)
158 if (connection->service->type == CONNECTION_PIPE)
160 gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
162 else
164 retval = check_pending(connection, 1, NULL);
165 if (retval != ERROR_OK)
166 return retval;
167 gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
170 if (gdb_con->buf_cnt > 0)
172 break;
174 if (gdb_con->buf_cnt == 0)
176 gdb_con->closed = 1;
177 return ERROR_SERVER_REMOTE_CLOSED;
180 #ifdef _WIN32
181 errno = WSAGetLastError();
183 switch (errno)
185 case WSAEWOULDBLOCK:
186 usleep(1000);
187 break;
188 case WSAECONNABORTED:
189 gdb_con->closed = 1;
190 return ERROR_SERVER_REMOTE_CLOSED;
191 case WSAECONNRESET:
192 gdb_con->closed = 1;
193 return ERROR_SERVER_REMOTE_CLOSED;
194 default:
195 LOG_ERROR("read: %d", errno);
196 exit(-1);
198 #else
199 switch (errno)
201 case EAGAIN:
202 usleep(1000);
203 break;
204 case ECONNABORTED:
205 gdb_con->closed = 1;
206 return ERROR_SERVER_REMOTE_CLOSED;
207 case ECONNRESET:
208 gdb_con->closed = 1;
209 return ERROR_SERVER_REMOTE_CLOSED;
210 default:
211 LOG_ERROR("read: %s", strerror(errno));
212 gdb_con->closed = 1;
213 return ERROR_SERVER_REMOTE_CLOSED;
215 #endif
218 #ifdef _DEBUG_GDB_IO_
219 debug_buffer = malloc(gdb_con->buf_cnt + 1);
220 memcpy(debug_buffer, gdb_con->buffer, gdb_con->buf_cnt);
221 debug_buffer[gdb_con->buf_cnt] = 0;
222 LOG_DEBUG("received '%s'", debug_buffer);
223 free(debug_buffer);
224 #endif
226 gdb_con->buf_p = gdb_con->buffer;
227 gdb_con->buf_cnt--;
228 *next_char = *(gdb_con->buf_p++);
229 if (gdb_con->buf_cnt > 0)
230 connection->input_pending = 1;
231 else
232 connection->input_pending = 0;
233 #ifdef _DEBUG_GDB_IO_
234 LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
235 #endif
237 return retval;
240 int gdb_putback_char(struct connection *connection, int last_char)
242 struct gdb_connection *gdb_con = connection->priv;
244 if (gdb_con->buf_p > gdb_con->buffer)
246 *(--gdb_con->buf_p) = last_char;
247 gdb_con->buf_cnt++;
249 else
251 LOG_ERROR("BUG: couldn't put character back");
254 return ERROR_OK;
257 /* The only way we can detect that the socket is closed is the first time
258 * we write to it, we will fail. Subsequent write operations will
259 * succeed. Shudder! */
260 int gdb_write(struct connection *connection, void *data, int len)
262 struct gdb_connection *gdb_con = connection->priv;
263 if (gdb_con->closed)
264 return ERROR_SERVER_REMOTE_CLOSED;
266 if (connection->service->type == CONNECTION_PIPE)
268 /* write to stdout */
269 if (write(STDOUT_FILENO, data, len) == len)
271 return ERROR_OK;
274 else
276 if (write_socket(connection->fd, data, len) == len)
278 return ERROR_OK;
281 gdb_con->closed = 1;
282 return ERROR_SERVER_REMOTE_CLOSED;
285 int gdb_put_packet_inner(struct connection *connection, char *buffer, int len)
287 int i;
288 unsigned char my_checksum = 0;
289 #ifdef _DEBUG_GDB_IO_
290 char *debug_buffer;
291 #endif
292 int reply;
293 int retval;
294 struct gdb_connection *gdb_con = connection->priv;
296 for (i = 0; i < len; i++)
297 my_checksum += buffer[i];
299 #ifdef _DEBUG_GDB_IO_
301 * At this point we should have nothing in the input queue from GDB,
302 * however sometimes '-' is sent even though we've already received
303 * an ACK (+) for everything we've sent off.
305 int gotdata;
306 for (;;)
308 if ((retval = check_pending(connection, 0, &gotdata)) != ERROR_OK)
309 return retval;
310 if (!gotdata)
311 break;
312 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
313 return retval;
314 if (reply == '$') {
315 /* fix a problem with some IAR tools */
316 gdb_putback_char(connection, reply);
317 LOG_DEBUG("Unexpected start of new packet");
318 break;
321 LOG_WARNING("Discard unexpected char %c", reply);
323 #endif
325 while (1)
327 #ifdef _DEBUG_GDB_IO_
328 debug_buffer = malloc(len + 1);
329 memcpy(debug_buffer, buffer, len);
330 debug_buffer[len] = 0;
331 LOG_DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum);
332 free(debug_buffer);
333 #endif
335 char local_buffer[1024];
336 local_buffer[0] = '$';
337 if ((size_t)len + 4 <= sizeof(local_buffer))
339 /* performance gain on smaller packets by only a single call to gdb_write() */
340 memcpy(local_buffer + 1, buffer, len++);
341 local_buffer[len++] = '#';
342 local_buffer[len++] = DIGITS[(my_checksum >> 4) & 0xf];
343 local_buffer[len++] = DIGITS[my_checksum & 0xf];
344 if ((retval = gdb_write(connection, local_buffer, len)) != ERROR_OK)
346 return retval;
349 else
351 /* larger packets are transmitted directly from caller supplied buffer
352 by several calls to gdb_write() to avoid dynamic allocation */
353 local_buffer[1] = '#';
354 local_buffer[2] = DIGITS[(my_checksum >> 4) & 0xf];
355 local_buffer[3] = DIGITS[my_checksum & 0xf];
356 if ((retval = gdb_write(connection, local_buffer, 1)) != ERROR_OK)
358 return retval;
360 if ((retval = gdb_write(connection, buffer, len)) != ERROR_OK)
362 return retval;
364 if ((retval = gdb_write(connection, local_buffer + 1, 3)) != ERROR_OK)
366 return retval;
370 if (gdb_con->noack_mode)
371 break;
373 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
374 return retval;
376 if (reply == '+')
377 break;
378 else if (reply == '-')
380 /* Stop sending output packets for now */
381 log_remove_callback(gdb_log_callback, connection);
382 LOG_WARNING("negative reply, retrying");
384 else if (reply == 0x3)
386 gdb_con->ctrl_c = 1;
387 if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK)
388 return retval;
389 if (reply == '+')
390 break;
391 else if (reply == '-')
393 /* Stop sending output packets for now */
394 log_remove_callback(gdb_log_callback, connection);
395 LOG_WARNING("negative reply, retrying");
397 else if (reply == '$') {
398 LOG_ERROR("GDB missing ack(1) - assumed good");
399 gdb_putback_char(connection, reply);
400 return ERROR_OK;
401 } else {
403 LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
404 gdb_con->closed = 1;
405 return ERROR_SERVER_REMOTE_CLOSED;
408 else if (reply == '$') {
409 LOG_ERROR("GDB missing ack(2) - assumed good");
410 gdb_putback_char(connection, reply);
411 return ERROR_OK;
413 else
415 LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection", reply);
416 gdb_con->closed = 1;
417 return ERROR_SERVER_REMOTE_CLOSED;
420 if (gdb_con->closed)
421 return ERROR_SERVER_REMOTE_CLOSED;
423 return ERROR_OK;
426 int gdb_put_packet(struct connection *connection, char *buffer, int len)
428 struct gdb_connection *gdb_con = connection->priv;
429 gdb_con->busy = 1;
430 int retval = gdb_put_packet_inner(connection, buffer, len);
431 gdb_con->busy = 0;
433 /* we sent some data, reset timer for keep alive messages */
434 kept_alive();
436 return retval;
439 static __inline__ int fetch_packet(struct connection *connection, int *checksum_ok, int noack, int *len, char *buffer)
441 unsigned char my_checksum = 0;
442 char checksum[3];
443 int character;
444 int retval;
446 struct gdb_connection *gdb_con = connection->priv;
447 my_checksum = 0;
448 int count = 0;
449 count = 0;
450 for (;;)
452 /* The common case is that we have an entire packet with no escape chars.
453 * We need to leave at least 2 bytes in the buffer to have
454 * gdb_get_char() update various bits and bobs correctly.
456 if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt + count) < *len))
458 /* The compiler will struggle a bit with constant propagation and
459 * aliasing, so we help it by showing that these values do not
460 * change inside the loop
462 int i;
463 char *buf = gdb_con->buf_p;
464 int run = gdb_con->buf_cnt - 2;
465 i = 0;
466 int done = 0;
467 while (i < run)
469 character = *buf++;
470 i++;
471 if (character == '#')
473 /* Danger! character can be '#' when esc is
474 * used so we need an explicit boolean for done here.
476 done = 1;
477 break;
480 if (character == '}')
482 /* data transmitted in binary mode (X packet)
483 * uses 0x7d as escape character */
484 my_checksum += character & 0xff;
485 character = *buf++;
486 i++;
487 my_checksum += character & 0xff;
488 buffer[count++] = (character ^ 0x20) & 0xff;
490 else
492 my_checksum += character & 0xff;
493 buffer[count++] = character & 0xff;
496 gdb_con->buf_p += i;
497 gdb_con->buf_cnt -= i;
498 if (done)
499 break;
501 if (count > *len)
503 LOG_ERROR("packet buffer too small");
504 return ERROR_GDB_BUFFER_TOO_SMALL;
507 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
508 return retval;
510 if (character == '#')
511 break;
513 if (character == '}')
515 /* data transmitted in binary mode (X packet)
516 * uses 0x7d as escape character */
517 my_checksum += character & 0xff;
518 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
519 return retval;
520 my_checksum += character & 0xff;
521 buffer[count++] = (character ^ 0x20) & 0xff;
523 else
525 my_checksum += character & 0xff;
526 buffer[count++] = character & 0xff;
530 *len = count;
532 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
533 return retval;
534 checksum[0] = character;
535 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
536 return retval;
537 checksum[1] = character;
538 checksum[2] = 0;
540 if (!noack)
542 *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
545 return ERROR_OK;
548 int gdb_get_packet_inner(struct connection *connection, char *buffer, int *len)
550 int character;
551 int retval;
552 struct gdb_connection *gdb_con = connection->priv;
554 while (1)
558 if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
559 return retval;
561 #ifdef _DEBUG_GDB_IO_
562 LOG_DEBUG("character: '%c'", character);
563 #endif
565 switch (character)
567 case '$':
568 break;
569 case '+':
570 /* gdb sends a dummy ack '+' at every remote connect - see remote_start_remote (remote.c)
571 * incase anyone tries to debug why they receive this warning every time */
572 LOG_WARNING("acknowledgment received, but no packet pending");
573 break;
574 case '-':
575 LOG_WARNING("negative acknowledgment, but no packet pending");
576 break;
577 case 0x3:
578 gdb_con->ctrl_c = 1;
579 *len = 0;
580 return ERROR_OK;
581 default:
582 LOG_WARNING("ignoring character 0x%x", character);
583 break;
585 } while (character != '$');
589 int checksum_ok = 0;
590 /* explicit code expansion here to get faster inlined code in -O3 by not
591 * calculating checksum
593 if (gdb_con->noack_mode)
595 if ((retval = fetch_packet(connection, &checksum_ok, 1, len, buffer)) != ERROR_OK)
596 return retval;
597 } else
599 if ((retval = fetch_packet(connection, &checksum_ok, 0, len, buffer)) != ERROR_OK)
600 return retval;
603 if (gdb_con->noack_mode)
605 /* checksum is not checked in noack mode */
606 break;
608 if (checksum_ok)
610 if ((retval = gdb_write(connection, "+", 1)) != ERROR_OK)
612 return retval;
614 break;
617 if (gdb_con->closed)
618 return ERROR_SERVER_REMOTE_CLOSED;
620 return ERROR_OK;
623 int gdb_get_packet(struct connection *connection, char *buffer, int *len)
625 struct gdb_connection *gdb_con = connection->priv;
626 gdb_con->busy = 1;
627 int retval = gdb_get_packet_inner(connection, buffer, len);
628 gdb_con->busy = 0;
629 return retval;
632 int gdb_output_con(struct connection *connection, const char* line)
634 char *hex_buffer;
635 int i, bin_size;
637 bin_size = strlen(line);
639 hex_buffer = malloc(bin_size*2 + 2);
640 if (hex_buffer == NULL)
641 return ERROR_GDB_BUFFER_TOO_SMALL;
643 hex_buffer[0] = 'O';
644 for (i = 0; i < bin_size; i++)
645 snprintf(hex_buffer + 1 + i*2, 3, "%2.2x", line[i]);
646 hex_buffer[bin_size*2 + 1] = 0;
648 int retval = gdb_put_packet(connection, hex_buffer, bin_size*2 + 1);
650 free(hex_buffer);
651 return retval;
654 int gdb_output(struct command_context *context, const char* line)
656 /* this will be dumped to the log and also sent as an O packet if possible */
657 LOG_USER_N("%s", line);
658 return ERROR_OK;
662 static void gdb_frontend_halted(struct target *target, struct connection *connection)
664 struct gdb_connection *gdb_connection = connection->priv;
666 /* In the GDB protocol when we are stepping or continuing execution,
667 * we have a lingering reply. Upon receiving a halted event
668 * when we have that lingering packet, we reply to the original
669 * step or continue packet.
671 * Executing monitor commands can bring the target in and
672 * out of the running state so we'll see lots of TARGET_EVENT_XXX
673 * that are to be ignored.
675 if (gdb_connection->frontend_state == TARGET_RUNNING)
677 char sig_reply[4];
678 int signal;
680 /* stop forwarding log packets! */
681 log_remove_callback(gdb_log_callback, connection);
683 if (gdb_connection->ctrl_c)
685 signal = 0x2;
686 gdb_connection->ctrl_c = 0;
688 else
690 signal = gdb_last_signal(target);
693 snprintf(sig_reply, 4, "T%2.2x", signal);
694 gdb_put_packet(connection, sig_reply, 3);
695 gdb_connection->frontend_state = TARGET_HALTED;
699 int gdb_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
701 int retval;
702 struct connection *connection = priv;
704 target_handle_event(target, event);
705 switch (event)
707 case TARGET_EVENT_GDB_HALT:
708 gdb_frontend_halted(target, connection);
709 break;
710 case TARGET_EVENT_HALTED:
711 target_call_event_callbacks(target, TARGET_EVENT_GDB_END);
712 break;
713 case TARGET_EVENT_GDB_FLASH_ERASE_START:
714 target_handle_event(target, TARGET_EVENT_OLD_gdb_program_config);
715 if ((retval = jtag_execute_queue()) != ERROR_OK)
717 return retval;
719 break;
720 default:
721 break;
724 return ERROR_OK;
727 int gdb_new_connection(struct connection *connection)
729 struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
730 struct gdb_service *gdb_service = connection->service->priv;
731 int retval;
732 int initial_ack;
734 connection->priv = gdb_connection;
736 /* initialize gdb connection information */
737 gdb_connection->buf_p = gdb_connection->buffer;
738 gdb_connection->buf_cnt = 0;
739 gdb_connection->ctrl_c = 0;
740 gdb_connection->frontend_state = TARGET_HALTED;
741 gdb_connection->vflash_image = NULL;
742 gdb_connection->closed = 0;
743 gdb_connection->busy = 0;
744 gdb_connection->noack_mode = 0;
745 gdb_connection->sync = true;
747 /* send ACK to GDB for debug request */
748 gdb_write(connection, "+", 1);
750 /* output goes through gdb connection */
751 command_set_output_handler(connection->cmd_ctx, gdb_output, connection);
753 /* we must remove all breakpoints registered to the target as a previous
754 * GDB session could leave dangling breakpoints if e.g. communication
755 * timed out.
757 breakpoint_clear_target(gdb_service->target);
758 watchpoint_clear_target(gdb_service->target);
760 /* register callback to be informed about target events */
761 target_register_event_callback(gdb_target_callback_event_handler, connection);
763 /* remove the initial ACK from the incoming buffer */
764 if ((retval = gdb_get_char(connection, &initial_ack)) != ERROR_OK)
765 return retval;
767 /* FIX!!!??? would we actually ever receive a + here???
768 * Not observed.
770 if (initial_ack != '+')
771 gdb_putback_char(connection, initial_ack);
772 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_ATTACH);
774 gdb_actual_connections++;
775 LOG_DEBUG("New GDB Connection: %d, Target %s, state: %s",
776 gdb_actual_connections,
777 gdb_service->target->cmd_name,
778 target_state_name(gdb_service->target));
780 return ERROR_OK;
783 int gdb_connection_closed(struct connection *connection)
785 struct gdb_service *gdb_service = connection->service->priv;
786 struct gdb_connection *gdb_connection = connection->priv;
788 /* we're done forwarding messages. Tear down callback before
789 * cleaning up connection.
791 log_remove_callback(gdb_log_callback, connection);
793 gdb_actual_connections--;
794 LOG_DEBUG("GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
795 gdb_service->target->cmd_name,
796 target_state_name(gdb_service->target),
797 gdb_actual_connections);
799 /* see if an image built with vFlash commands is left */
800 if (gdb_connection->vflash_image)
802 image_close(gdb_connection->vflash_image);
803 free(gdb_connection->vflash_image);
804 gdb_connection->vflash_image = NULL;
807 /* if this connection registered a debug-message receiver delete it */
808 delete_debug_msg_receiver(connection->cmd_ctx, gdb_service->target);
810 if (connection->priv)
812 free(connection->priv);
813 connection->priv = NULL;
815 else
817 LOG_ERROR("BUG: connection->priv == NULL");
821 target_unregister_event_callback(gdb_target_callback_event_handler, connection);
823 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_END);
825 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
827 return ERROR_OK;
830 void gdb_send_error(struct connection *connection, uint8_t the_error)
832 char err[4];
833 snprintf(err, 4, "E%2.2X", the_error);
834 gdb_put_packet(connection, err, 3);
837 int gdb_last_signal_packet(struct connection *connection, struct target *target, char* packet, int packet_size)
839 char sig_reply[4];
840 int signal;
842 signal = gdb_last_signal(target);
844 snprintf(sig_reply, 4, "S%2.2x", signal);
845 gdb_put_packet(connection, sig_reply, 3);
847 return ERROR_OK;
850 static int gdb_reg_pos(struct target *target, int pos, int len)
852 if (target->endianness == TARGET_LITTLE_ENDIAN)
853 return pos;
854 else
855 return len - 1 - pos;
858 /* Convert register to string of bytes. NB! The # of bits in the
859 * register might be non-divisible by 8(a byte), in which
860 * case an entire byte is shown.
862 * NB! the format on the wire is the target endianess
864 * The format of reg->value is little endian
867 void gdb_str_to_target(struct target *target, char *tstr, struct reg *reg)
869 int i;
871 uint8_t *buf;
872 int buf_len;
873 buf = reg->value;
874 buf_len = DIV_ROUND_UP(reg->size, 8);
876 for (i = 0; i < buf_len; i++)
878 int j = gdb_reg_pos(target, i, buf_len);
879 tstr[i*2] = DIGITS[(buf[j]>>4) & 0xf];
880 tstr[i*2 + 1] = DIGITS[buf[j]&0xf];
884 static int hextoint(char c)
886 if (c>='0'&&c<='9')
888 return c-'0';
890 c = toupper(c);
891 if (c>='A'&&c<='F')
893 return c-'A'+10;
895 LOG_ERROR("BUG: invalid register value %08x", c);
896 return 0;
899 /* copy over in register buffer */
900 void gdb_target_to_reg(struct target *target, char *tstr, int str_len, uint8_t *bin)
902 if (str_len % 2)
904 LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
905 exit(-1);
908 int i;
909 for (i = 0; i < str_len; i += 2)
911 uint8_t t = hextoint(tstr[i]) << 4;
912 t |= hextoint(tstr[i + 1]);
914 int j = gdb_reg_pos(target, i/2, str_len/2);
915 bin[j] = t;
919 int gdb_get_registers_packet(struct connection *connection, struct target *target, char* packet, int packet_size)
921 struct reg **reg_list;
922 int reg_list_size;
923 int retval;
924 int reg_packet_size = 0;
925 char *reg_packet;
926 char *reg_packet_p;
927 int i;
929 #ifdef _DEBUG_GDB_IO_
930 LOG_DEBUG("-");
931 #endif
933 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
935 return gdb_error(connection, retval);
938 for (i = 0; i < reg_list_size; i++)
940 reg_packet_size += reg_list[i]->size;
943 reg_packet = malloc(DIV_ROUND_UP(reg_packet_size, 8) * 2);
944 reg_packet_p = reg_packet;
946 for (i = 0; i < reg_list_size; i++)
948 gdb_str_to_target(target, reg_packet_p, reg_list[i]);
949 reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
952 #ifdef _DEBUG_GDB_IO_
954 char *reg_packet_p;
955 reg_packet_p = strndup(reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
956 LOG_DEBUG("reg_packet: %s", reg_packet_p);
957 free(reg_packet_p);
959 #endif
961 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_packet_size, 8) * 2);
962 free(reg_packet);
964 free(reg_list);
966 return ERROR_OK;
969 int gdb_set_registers_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
971 int i;
972 struct reg **reg_list;
973 int reg_list_size;
974 int retval;
975 char *packet_p;
977 #ifdef _DEBUG_GDB_IO_
978 LOG_DEBUG("-");
979 #endif
981 /* skip command character */
982 packet++;
983 packet_size--;
985 if (packet_size % 2)
987 LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
988 return ERROR_SERVER_REMOTE_CLOSED;
991 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
993 return gdb_error(connection, retval);
996 packet_p = packet;
997 for (i = 0; i < reg_list_size; i++)
999 uint8_t *bin_buf;
1000 int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1002 if (packet_p + chars > packet + packet_size)
1004 LOG_ERROR("BUG: register packet is too small for registers");
1007 bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1008 gdb_target_to_reg(target, packet_p, chars, bin_buf);
1010 reg_list[i]->type->set(reg_list[i], bin_buf);
1012 /* advance packet pointer */
1013 packet_p += chars;
1016 free(bin_buf);
1019 /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1020 free(reg_list);
1022 gdb_put_packet(connection, "OK", 2);
1024 return ERROR_OK;
1027 int gdb_get_register_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1029 char *reg_packet;
1030 int reg_num = strtoul(packet + 1, NULL, 16);
1031 struct reg **reg_list;
1032 int reg_list_size;
1033 int retval;
1035 #ifdef _DEBUG_GDB_IO_
1036 LOG_DEBUG("-");
1037 #endif
1039 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1041 return gdb_error(connection, retval);
1044 if (reg_list_size <= reg_num)
1046 LOG_ERROR("gdb requested a non-existing register");
1047 exit(-1);
1050 reg_packet = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1052 gdb_str_to_target(target, reg_packet, reg_list[reg_num]);
1054 gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1056 free(reg_list);
1057 free(reg_packet);
1059 return ERROR_OK;
1062 int gdb_set_register_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1064 char *separator;
1065 uint8_t *bin_buf;
1066 int reg_num = strtoul(packet + 1, &separator, 16);
1067 struct reg **reg_list;
1068 int reg_list_size;
1069 int retval;
1071 LOG_DEBUG("-");
1073 if ((retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size)) != ERROR_OK)
1075 return gdb_error(connection, retval);
1078 if (reg_list_size < reg_num)
1080 LOG_ERROR("gdb requested a non-existing register");
1081 return ERROR_SERVER_REMOTE_CLOSED;
1084 if (*separator != '=')
1086 LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1087 return ERROR_SERVER_REMOTE_CLOSED;
1090 /* convert from GDB-string (target-endian) to hex-string (big-endian) */
1091 bin_buf = malloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8));
1092 int chars = (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1094 /* fix!!! add some sanity checks on packet size here */
1096 gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1098 reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1100 gdb_put_packet(connection, "OK", 2);
1102 free(bin_buf);
1103 free(reg_list);
1105 return ERROR_OK;
1108 int gdb_error(struct connection *connection, int retval)
1110 switch (retval)
1112 case ERROR_TARGET_DATA_ABORT:
1113 gdb_send_error(connection, EIO);
1114 break;
1115 case ERROR_TARGET_TRANSLATION_FAULT:
1116 gdb_send_error(connection, EFAULT);
1117 break;
1118 case ERROR_TARGET_UNALIGNED_ACCESS:
1119 gdb_send_error(connection, EFAULT);
1120 break;
1121 case ERROR_TARGET_NOT_HALTED:
1122 gdb_send_error(connection, EFAULT);
1123 break;
1124 default:
1125 /* This could be that the target reset itself. */
1126 LOG_ERROR("unexpected error %i", retval);
1127 gdb_send_error(connection, EFAULT);
1128 break;
1131 return ERROR_OK;
1134 /* We don't have to worry about the default 2 second timeout for GDB packets,
1135 * because GDB breaks up large memory reads into smaller reads.
1137 * 8191 bytes by the looks of it. Why 8191 bytes instead of 8192?????
1139 int gdb_read_memory_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1141 char *separator;
1142 uint32_t addr = 0;
1143 uint32_t len = 0;
1145 uint8_t *buffer;
1146 char *hex_buffer;
1148 int retval = ERROR_OK;
1150 /* skip command character */
1151 packet++;
1153 addr = strtoul(packet, &separator, 16);
1155 if (*separator != ',')
1157 LOG_ERROR("incomplete read memory packet received, dropping connection");
1158 return ERROR_SERVER_REMOTE_CLOSED;
1161 len = strtoul(separator + 1, NULL, 16);
1163 buffer = malloc(len);
1165 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1167 retval = target_read_buffer(target, addr, len, buffer);
1169 if ((retval != ERROR_OK)&&!gdb_report_data_abort)
1171 /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1172 * At some point this might be fixed in GDB, in which case this code can be removed.
1174 * OpenOCD developers are acutely aware of this problem, but there is nothing
1175 * gained by involving the user in this problem that hopefully will get resolved
1176 * eventually
1178 * http://sourceware.org/cgi-bin/gnatsweb.pl?cmd = view%20audit-trail&database = gdb&pr = 2395
1180 * For now, the default is to fix up things to make current GDB versions work.
1181 * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1183 memset(buffer, 0, len);
1184 retval = ERROR_OK;
1187 if (retval == ERROR_OK)
1189 hex_buffer = malloc(len * 2 + 1);
1191 uint32_t i;
1192 for (i = 0; i < len; i++)
1194 uint8_t t = buffer[i];
1195 hex_buffer[2 * i] = DIGITS[(t >> 4) & 0xf];
1196 hex_buffer[2 * i + 1] = DIGITS[t & 0xf];
1199 gdb_put_packet(connection, hex_buffer, len * 2);
1201 free(hex_buffer);
1203 else
1205 retval = gdb_error(connection, retval);
1208 free(buffer);
1210 return retval;
1213 int gdb_write_memory_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1215 char *separator;
1216 uint32_t addr = 0;
1217 uint32_t len = 0;
1219 uint8_t *buffer;
1221 uint32_t i;
1222 int retval;
1224 /* skip command character */
1225 packet++;
1227 addr = strtoul(packet, &separator, 16);
1229 if (*separator != ',')
1231 LOG_ERROR("incomplete write memory packet received, dropping connection");
1232 return ERROR_SERVER_REMOTE_CLOSED;
1235 len = strtoul(separator + 1, &separator, 16);
1237 if (*(separator++) != ':')
1239 LOG_ERROR("incomplete write memory packet received, dropping connection");
1240 return ERROR_SERVER_REMOTE_CLOSED;
1243 buffer = malloc(len);
1245 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1247 for (i = 0; i < len; i++)
1249 uint32_t tmp;
1250 sscanf(separator + 2*i, "%2" SCNx32 , &tmp);
1251 buffer[i] = tmp;
1254 retval = target_write_buffer(target, addr, len, buffer);
1256 if (retval == ERROR_OK)
1258 gdb_put_packet(connection, "OK", 2);
1260 else
1262 retval = gdb_error(connection, retval);
1265 free(buffer);
1267 return retval;
1270 int gdb_write_memory_binary_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1272 char *separator;
1273 uint32_t addr = 0;
1274 uint32_t len = 0;
1276 int retval;
1278 /* skip command character */
1279 packet++;
1281 addr = strtoul(packet, &separator, 16);
1283 if (*separator != ',')
1285 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1286 return ERROR_SERVER_REMOTE_CLOSED;
1289 len = strtoul(separator + 1, &separator, 16);
1291 if (*(separator++) != ':')
1293 LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1294 return ERROR_SERVER_REMOTE_CLOSED;
1297 retval = ERROR_OK;
1298 if (len)
1300 LOG_DEBUG("addr: 0x%8.8" PRIx32 ", len: 0x%8.8" PRIx32 "", addr, len);
1302 retval = target_write_buffer(target, addr, len, (uint8_t*)separator);
1305 if (retval == ERROR_OK)
1307 gdb_put_packet(connection, "OK", 2);
1309 else
1311 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1312 return retval;
1315 return ERROR_OK;
1318 int gdb_step_continue_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1320 int current = 0;
1321 uint32_t address = 0x0;
1322 int retval = ERROR_OK;
1324 LOG_DEBUG("-");
1326 if (packet_size > 1)
1328 packet[packet_size] = 0;
1329 address = strtoul(packet + 1, NULL, 16);
1331 else
1333 current = 1;
1336 if (packet[0] == 'c')
1338 LOG_DEBUG("continue");
1339 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
1340 retval = target_resume(target, current, address, 0, 0); /* resume at current address, don't handle breakpoints, not debugging */
1342 else if (packet[0] == 's')
1344 LOG_DEBUG("step");
1345 /* step at current or address, don't handle breakpoints */
1346 retval = target_step(target, current, address, 0);
1348 return retval;
1351 int gdb_breakpoint_watchpoint_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1353 int type;
1354 enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1355 enum watchpoint_rw wp_type;
1356 uint32_t address;
1357 uint32_t size;
1358 char *separator;
1359 int retval;
1361 LOG_DEBUG("-");
1363 type = strtoul(packet + 1, &separator, 16);
1365 if (type == 0) /* memory breakpoint */
1366 bp_type = BKPT_SOFT;
1367 else if (type == 1) /* hardware breakpoint */
1368 bp_type = BKPT_HARD;
1369 else if (type == 2) /* write watchpoint */
1370 wp_type = WPT_WRITE;
1371 else if (type == 3) /* read watchpoint */
1372 wp_type = WPT_READ;
1373 else if (type == 4) /* access watchpoint */
1374 wp_type = WPT_ACCESS;
1376 if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT)||(bp_type == BKPT_HARD)))
1378 bp_type = gdb_breakpoint_override_type;
1381 if (*separator != ',')
1383 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1384 return ERROR_SERVER_REMOTE_CLOSED;
1387 address = strtoul(separator + 1, &separator, 16);
1389 if (*separator != ',')
1391 LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1392 return ERROR_SERVER_REMOTE_CLOSED;
1395 size = strtoul(separator + 1, &separator, 16);
1397 switch (type)
1399 case 0:
1400 case 1:
1401 if (packet[0] == 'Z')
1403 if ((retval = breakpoint_add(target, address, size, bp_type)) != ERROR_OK)
1405 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1406 return retval;
1408 else
1410 gdb_put_packet(connection, "OK", 2);
1413 else
1415 breakpoint_remove(target, address);
1416 gdb_put_packet(connection, "OK", 2);
1418 break;
1419 case 2:
1420 case 3:
1421 case 4:
1423 if (packet[0] == 'Z')
1425 if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
1427 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1428 return retval;
1430 else
1432 gdb_put_packet(connection, "OK", 2);
1435 else
1437 watchpoint_remove(target, address);
1438 gdb_put_packet(connection, "OK", 2);
1440 break;
1442 default:
1443 break;
1446 return ERROR_OK;
1449 /* print out a string and allocate more space as needed, mainly used for XML at this point */
1450 void xml_printf(int *retval, char **xml, int *pos, int *size, const char *fmt, ...)
1452 if (*retval != ERROR_OK)
1454 return;
1456 int first = 1;
1458 for (;;)
1460 if ((*xml == NULL) || (!first))
1462 /* start by 0 to exercise all the code paths.
1463 * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1465 *size = *size * 2 + 2;
1466 char *t = *xml;
1467 *xml = realloc(*xml, *size);
1468 if (*xml == NULL)
1470 if (t)
1471 free(t);
1472 *retval = ERROR_SERVER_REMOTE_CLOSED;
1473 return;
1477 va_list ap;
1478 int ret;
1479 va_start(ap, fmt);
1480 ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1481 va_end(ap);
1482 if ((ret > 0) && ((ret + 1) < *size - *pos))
1484 *pos += ret;
1485 return;
1487 /* there was just enough or not enough space, allocate more. */
1488 first = 0;
1492 static int decode_xfer_read(char *buf, char **annex, int *ofs, unsigned int *len)
1494 char *separator;
1496 /* Extract and NUL-terminate the annex. */
1497 *annex = buf;
1498 while (*buf && *buf != ':')
1499 buf++;
1500 if (*buf == '\0')
1501 return -1;
1502 *buf++ = 0;
1504 /* After the read marker and annex, qXfer looks like a
1505 * traditional 'm' packet. */
1507 *ofs = strtoul(buf, &separator, 16);
1509 if (*separator != ',')
1510 return -1;
1512 *len = strtoul(separator + 1, NULL, 16);
1514 return 0;
1517 int gdb_calc_blocksize(struct flash_bank *bank)
1519 uint32_t i;
1520 uint32_t block_size = 0xffffffff;
1522 /* loop through all sectors and return smallest sector size */
1524 for (i = 0; i < (uint32_t)bank->num_sectors; i++)
1526 if (bank->sectors[i].size < block_size)
1527 block_size = bank->sectors[i].size;
1530 return block_size;
1533 static int compare_bank (const void * a, const void * b)
1535 struct flash_bank *b1, *b2;
1536 b1=*((struct flash_bank **)a);
1537 b2=*((struct flash_bank **)b);
1539 if (b1->base == b2->base)
1541 return 0;
1542 } else if (b1->base > b2->base)
1544 return 1;
1545 } else
1547 return -1;
1551 int gdb_query_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1553 struct command_context *cmd_ctx = connection->cmd_ctx;
1554 struct gdb_connection *gdb_connection = connection->priv;
1556 if (strstr(packet, "qRcmd,"))
1558 if (packet_size > 6)
1560 char *cmd;
1561 int i;
1562 cmd = malloc((packet_size - 6)/2 + 1);
1563 for (i = 0; i < (packet_size - 6)/2; i++)
1565 uint32_t tmp;
1566 sscanf(packet + 6 + 2*i, "%2" SCNx32 , &tmp);
1567 cmd[i] = tmp;
1569 cmd[(packet_size - 6)/2] = 0x0;
1571 /* We want to print all debug output to GDB connection */
1572 log_add_callback(gdb_log_callback, connection);
1573 target_call_timer_callbacks_now();
1574 /* some commands need to know the GDB connection, make note of current
1575 * GDB connection. */
1576 current_gdb_connection = gdb_connection;
1577 command_run_line(cmd_ctx, cmd);
1578 current_gdb_connection = NULL;
1579 target_call_timer_callbacks_now();
1580 log_remove_callback(gdb_log_callback, connection);
1581 free(cmd);
1583 gdb_put_packet(connection, "OK", 2);
1584 return ERROR_OK;
1586 else if (strstr(packet, "qCRC:"))
1588 if (packet_size > 5)
1590 int retval;
1591 char gdb_reply[10];
1592 char *separator;
1593 uint32_t checksum;
1594 uint32_t addr = 0;
1595 uint32_t len = 0;
1597 /* skip command character */
1598 packet += 5;
1600 addr = strtoul(packet, &separator, 16);
1602 if (*separator != ',')
1604 LOG_ERROR("incomplete read memory packet received, dropping connection");
1605 return ERROR_SERVER_REMOTE_CLOSED;
1608 len = strtoul(separator + 1, NULL, 16);
1610 retval = target_checksum_memory(target, addr, len, &checksum);
1612 if (retval == ERROR_OK)
1614 snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
1615 gdb_put_packet(connection, gdb_reply, 9);
1617 else
1619 if ((retval = gdb_error(connection, retval)) != ERROR_OK)
1620 return retval;
1623 return ERROR_OK;
1626 else if (strstr(packet, "qSupported"))
1628 /* we currently support packet size and qXfer:memory-map:read (if enabled)
1629 * disable qXfer:features:read for the moment */
1630 int retval = ERROR_OK;
1631 char *buffer = NULL;
1632 int pos = 0;
1633 int size = 0;
1635 xml_printf(&retval, &buffer, &pos, &size,
1636 "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read-;QStartNoAckMode+",
1637 (GDB_BUFFER_SIZE - 1), ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-');
1639 if (retval != ERROR_OK)
1641 gdb_send_error(connection, 01);
1642 return ERROR_OK;
1645 gdb_put_packet(connection, buffer, strlen(buffer));
1646 free(buffer);
1648 return ERROR_OK;
1650 else if (strstr(packet, "qXfer:memory-map:read::") && (flash_get_bank_count() > 0))
1652 /* We get away with only specifying flash here. Regions that are not
1653 * specified are treated as if we provided no memory map(if not we
1654 * could detect the holes and mark them as RAM).
1655 * Normally we only execute this code once, but no big deal if we
1656 * have to regenerate it a couple of times. */
1658 struct flash_bank *p;
1659 char *xml = NULL;
1660 int size = 0;
1661 int pos = 0;
1662 int retval = ERROR_OK;
1664 int offset;
1665 int length;
1666 char *separator;
1667 int blocksize;
1669 /* skip command character */
1670 packet += 23;
1672 offset = strtoul(packet, &separator, 16);
1673 length = strtoul(separator + 1, &separator, 16);
1675 xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1678 sort banks in ascending order, we need to make non-flash memory be ram(or rather
1679 read/write) by default for GDB.
1680 GDB does not have a concept of non-cacheable read/write memory.
1682 struct flash_bank **banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1683 int i;
1685 for (i = 0; i < flash_get_bank_count(); i++)
1687 p = get_flash_bank_by_num(i);
1688 if (p == NULL)
1690 free(banks);
1691 retval = ERROR_FAIL;
1692 gdb_send_error(connection, retval);
1693 return retval;
1695 banks[i]=p;
1698 qsort(banks, flash_get_bank_count(), sizeof(struct flash_bank *), compare_bank);
1700 uint32_t ram_start = 0;
1701 for (i = 0; i < flash_get_bank_count(); i++)
1703 p = banks[i];
1705 if (ram_start < p->base)
1707 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1708 ram_start, p->base-ram_start);
1711 /* if device has uneven sector sizes, eg. str7, lpc
1712 * we pass the smallest sector size to gdb memory map */
1713 blocksize = gdb_calc_blocksize(p);
1715 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"flash\" start=\"0x%x\" length=\"0x%x\">\n" \
1716 "<property name=\"blocksize\">0x%x</property>\n" \
1717 "</memory>\n", \
1718 p->base, p->size, blocksize);
1719 ram_start = p->base + p->size;
1721 if (ram_start != 0)
1723 xml_printf(&retval, &xml, &pos, &size, "<memory type=\"ram\" start=\"0x%x\" length=\"0x%x\"/>\n",
1724 ram_start, 0-ram_start);
1725 } else
1727 /* a flash chip could be at the very end of the 32 bit address space, in which case
1728 ram_start will be precisely 0 */
1731 free(banks);
1732 banks = NULL;
1734 xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
1736 if (retval != ERROR_OK)
1738 gdb_send_error(connection, retval);
1739 return retval;
1742 if (offset + length > pos)
1744 length = pos - offset;
1747 char *t = malloc(length + 1);
1748 t[0] = 'l';
1749 memcpy(t + 1, xml + offset, length);
1750 gdb_put_packet(connection, t, length + 1);
1752 free(t);
1753 free(xml);
1754 return ERROR_OK;
1756 else if (strstr(packet, "qXfer:features:read:"))
1758 char *xml = NULL;
1759 int size = 0;
1760 int pos = 0;
1761 int retval = ERROR_OK;
1763 int offset;
1764 unsigned int length;
1765 char *annex;
1767 /* skip command character */
1768 packet += 20;
1770 if (decode_xfer_read(packet, &annex, &offset, &length) < 0)
1772 gdb_send_error(connection, 01);
1773 return ERROR_OK;
1776 if (strcmp(annex, "target.xml") != 0)
1778 gdb_send_error(connection, 01);
1779 return ERROR_OK;
1782 xml_printf(&retval, &xml, &pos, &size, \
1783 "l < target version=\"1.0\">\n < architecture > arm</architecture>\n</target>\n");
1785 if (retval != ERROR_OK)
1787 gdb_send_error(connection, retval);
1788 return retval;
1791 gdb_put_packet(connection, xml, strlen(xml));
1793 free(xml);
1794 return ERROR_OK;
1796 else if (strstr(packet, "QStartNoAckMode"))
1798 gdb_connection->noack_mode = 1;
1799 gdb_put_packet(connection, "OK", 2);
1800 return ERROR_OK;
1803 gdb_put_packet(connection, "", 0);
1804 return ERROR_OK;
1807 int gdb_v_packet(struct connection *connection, struct target *target, char *packet, int packet_size)
1809 struct gdb_connection *gdb_connection = connection->priv;
1810 struct gdb_service *gdb_service = connection->service->priv;
1811 int result;
1813 /* if flash programming disabled - send a empty reply */
1815 if (gdb_flash_program == 0)
1817 gdb_put_packet(connection, "", 0);
1818 return ERROR_OK;
1821 if (strstr(packet, "vFlashErase:"))
1823 unsigned long addr;
1824 unsigned long length;
1826 char *parse = packet + 12;
1827 if (*parse == '\0')
1829 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1830 return ERROR_SERVER_REMOTE_CLOSED;
1833 addr = strtoul(parse, &parse, 16);
1835 if (*(parse++) != ',' || *parse == '\0')
1837 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1838 return ERROR_SERVER_REMOTE_CLOSED;
1841 length = strtoul(parse, &parse, 16);
1843 if (*parse != '\0')
1845 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1846 return ERROR_SERVER_REMOTE_CLOSED;
1849 /* assume all sectors need erasing - stops any problems
1850 * when flash_write is called multiple times */
1851 flash_set_dirty();
1853 /* perform any target specific operations before the erase */
1854 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
1855 result = flash_erase_address_range(gdb_service->target, addr, length);
1856 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
1858 /* perform erase */
1859 if (result != ERROR_OK)
1861 /* GDB doesn't evaluate the actual error number returned,
1862 * treat a failed erase as an I/O error
1864 gdb_send_error(connection, EIO);
1865 LOG_ERROR("flash_erase returned %i", result);
1867 else
1868 gdb_put_packet(connection, "OK", 2);
1870 return ERROR_OK;
1873 if (strstr(packet, "vFlashWrite:"))
1875 int retval;
1876 unsigned long addr;
1877 unsigned long length;
1878 char *parse = packet + 12;
1880 if (*parse == '\0')
1882 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1883 return ERROR_SERVER_REMOTE_CLOSED;
1885 addr = strtoul(parse, &parse, 16);
1886 if (*(parse++) != ':')
1888 LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
1889 return ERROR_SERVER_REMOTE_CLOSED;
1891 length = packet_size - (parse - packet);
1893 /* create a new image if there isn't already one */
1894 if (gdb_connection->vflash_image == NULL)
1896 gdb_connection->vflash_image = malloc(sizeof(struct image));
1897 image_open(gdb_connection->vflash_image, "", "build");
1900 /* create new section with content from packet buffer */
1901 if ((retval = image_add_section(gdb_connection->vflash_image, addr, length, 0x0, (uint8_t*)parse)) != ERROR_OK)
1903 return retval;
1906 gdb_put_packet(connection, "OK", 2);
1908 return ERROR_OK;
1911 if (!strcmp(packet, "vFlashDone"))
1913 uint32_t written;
1915 /* process the flashing buffer. No need to erase as GDB
1916 * always issues a vFlashErase first. */
1917 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_START);
1918 result = flash_write(gdb_service->target, gdb_connection->vflash_image, &written, 0);
1919 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_WRITE_END);
1920 if (result != ERROR_OK)
1922 if (result == ERROR_FLASH_DST_OUT_OF_BANK)
1923 gdb_put_packet(connection, "E.memtype", 9);
1924 else
1925 gdb_send_error(connection, EIO);
1927 else
1929 LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
1930 gdb_put_packet(connection, "OK", 2);
1933 image_close(gdb_connection->vflash_image);
1934 free(gdb_connection->vflash_image);
1935 gdb_connection->vflash_image = NULL;
1937 return ERROR_OK;
1940 gdb_put_packet(connection, "", 0);
1941 return ERROR_OK;
1944 int gdb_detach(struct connection *connection, struct target *target)
1946 struct gdb_service *gdb_service = connection->service->priv;
1948 target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_DETACH);
1950 return gdb_put_packet(connection, "OK", 2);
1953 static void gdb_log_callback(void *priv, const char *file, unsigned line,
1954 const char *function, const char *string)
1956 struct connection *connection = priv;
1957 struct gdb_connection *gdb_con = connection->priv;
1959 if (gdb_con->busy)
1961 /* do not reply this using the O packet */
1962 return;
1965 gdb_output_con(connection, string);
1968 /* Do not allocate this on the stack */
1969 char gdb_packet_buffer[GDB_BUFFER_SIZE];
1971 static void gdb_sig_halted(struct connection *connection)
1973 char sig_reply[4];
1974 snprintf(sig_reply, 4, "T%2.2x", 2);
1975 gdb_put_packet(connection, sig_reply, 3);
1979 int gdb_input_inner(struct connection *connection)
1981 struct gdb_service *gdb_service = connection->service->priv;
1982 struct target *target = gdb_service->target;
1983 char *packet = gdb_packet_buffer;
1984 int packet_size;
1985 int retval;
1986 struct gdb_connection *gdb_con = connection->priv;
1987 static int extended_protocol = 0;
1989 /* drain input buffer */
1992 packet_size = GDB_BUFFER_SIZE-1;
1993 if ((retval = gdb_get_packet(connection, packet, &packet_size)) != ERROR_OK)
1995 return retval;
1998 /* terminate with zero */
1999 packet[packet_size] = 0;
2001 if (LOG_LEVEL_IS(LOG_LVL_DEBUG)) {
2002 if (packet[0] == 'X') {
2003 // binary packets spew junk into the debug log stream
2004 char buf[ 50 ];
2005 int x;
2006 for (x = 0 ; (x < 49) && (packet[x] != ':') ; x++) {
2007 buf[x] = packet[x];
2009 buf[x] = 0;
2010 LOG_DEBUG("received packet: '%s:<binary-data>'", buf);
2011 } else {
2012 LOG_DEBUG("received packet: '%s'", packet);
2016 if (packet_size > 0)
2018 retval = ERROR_OK;
2019 switch (packet[0])
2021 case 'H':
2022 /* Hct... -- set thread
2023 * we don't have threads, send empty reply */
2024 gdb_put_packet(connection, NULL, 0);
2025 break;
2026 case 'q':
2027 case 'Q':
2028 retval = gdb_query_packet(connection, target, packet, packet_size);
2029 break;
2030 case 'g':
2031 retval = gdb_get_registers_packet(connection, target, packet, packet_size);
2032 break;
2033 case 'G':
2034 retval = gdb_set_registers_packet(connection, target, packet, packet_size);
2035 break;
2036 case 'p':
2037 retval = gdb_get_register_packet(connection, target, packet, packet_size);
2038 break;
2039 case 'P':
2040 retval = gdb_set_register_packet(connection, target, packet, packet_size);
2041 break;
2042 case 'm':
2043 retval = gdb_read_memory_packet(connection, target, packet, packet_size);
2044 break;
2045 case 'M':
2046 retval = gdb_write_memory_packet(connection, target, packet, packet_size);
2047 break;
2048 case 'z':
2049 case 'Z':
2050 retval = gdb_breakpoint_watchpoint_packet(connection, target, packet, packet_size);
2051 break;
2052 case '?':
2053 gdb_last_signal_packet(connection, target, packet, packet_size);
2054 break;
2055 case 'c':
2056 case 's':
2058 int retval = ERROR_OK;
2060 struct gdb_connection *gdb_con = connection->priv;
2061 log_add_callback(gdb_log_callback, connection);
2063 bool nostep = false;
2064 if (target->state == TARGET_RUNNING)
2066 LOG_WARNING("The target is already running. Halt target before stepi/continue.");
2067 retval = target_halt(target);
2068 if (retval == ERROR_OK)
2069 retval = target_wait_state(target, TARGET_HALTED, 100);
2070 } else if (target->state != TARGET_HALTED)
2072 LOG_WARNING("The target is not in the halted nor running stated, stepi/continue ignored.");
2073 nostep = true;
2074 } else if ((packet[0] == 's') && gdb_con->sync)
2076 /* Hmm..... when you issue a continue in GDB, then a "stepi" is
2077 * sent by GDB first to OpenOCD, thus defeating the check to
2078 * make only the single stepping have the sync feature...
2080 nostep = true;
2081 LOG_WARNING("stepi ignored. GDB will now fetch the register state from the target.");
2083 gdb_con->sync = false;
2085 if ((retval!=ERROR_OK) || nostep)
2087 /* Either the target isn't in the halted state, then we can't
2088 * step/continue. This might be early setup, etc.
2090 * Or we want to allow GDB to pick up a fresh set of
2091 * register values without modifying the target state.
2094 gdb_sig_halted(connection);
2096 /* stop forwarding log packets! */
2097 log_remove_callback(gdb_log_callback, connection);
2098 } else
2100 /* We're running/stepping, in which case we can
2101 * forward log output until the target is halted
2103 gdb_con->frontend_state = TARGET_RUNNING;
2104 target_call_event_callbacks(target, TARGET_EVENT_GDB_START);
2105 int retval = gdb_step_continue_packet(connection, target, packet, packet_size);
2106 if (retval != ERROR_OK)
2108 /* we'll never receive a halted condition... issue a false one.. */
2109 gdb_frontend_halted(target, connection);
2113 break;
2114 case 'v':
2115 retval = gdb_v_packet(connection, target, packet, packet_size);
2116 break;
2117 case 'D':
2118 retval = gdb_detach(connection, target);
2119 extended_protocol = 0;
2120 break;
2121 case 'X':
2122 if ((retval = gdb_write_memory_binary_packet(connection, target, packet, packet_size)) != ERROR_OK)
2123 return retval;
2124 break;
2125 case 'k':
2126 if (extended_protocol != 0)
2127 break;
2128 gdb_put_packet(connection, "OK", 2);
2129 return ERROR_SERVER_REMOTE_CLOSED;
2130 case '!':
2131 /* handle extended remote protocol */
2132 extended_protocol = 1;
2133 gdb_put_packet(connection, "OK", 2);
2134 break;
2135 case 'R':
2136 /* handle extended restart packet */
2137 breakpoint_clear_target(gdb_service->target);
2138 watchpoint_clear_target(gdb_service->target);
2139 command_run_linef(connection->cmd_ctx,
2140 "ocd_gdb_restart %s",
2141 target->cmd_name);
2142 break;
2143 default:
2144 /* ignore unkown packets */
2145 LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
2146 gdb_put_packet(connection, NULL, 0);
2147 break;
2150 /* if a packet handler returned an error, exit input loop */
2151 if (retval != ERROR_OK)
2152 return retval;
2155 if (gdb_con->ctrl_c)
2157 if (target->state == TARGET_RUNNING)
2159 retval = target_halt(target);
2160 if (retval != ERROR_OK)
2162 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2164 gdb_con->ctrl_c = 0;
2165 } else
2167 LOG_INFO("The target is not running when halt was requested, stopping GDB.");
2168 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2172 } while (gdb_con->buf_cnt > 0);
2174 return ERROR_OK;
2177 int gdb_input(struct connection *connection)
2179 int retval = gdb_input_inner(connection);
2180 struct gdb_connection *gdb_con = connection->priv;
2181 if (retval == ERROR_SERVER_REMOTE_CLOSED)
2182 return retval;
2184 /* logging does not propagate the error, yet can set the gdb_con->closed flag */
2185 if (gdb_con->closed)
2186 return ERROR_SERVER_REMOTE_CLOSED;
2188 /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
2189 return ERROR_OK;
2192 int gdb_init(void)
2194 struct gdb_service *gdb_service;
2195 struct target *target = all_targets;
2197 if (!target)
2199 LOG_WARNING("no gdb ports allocated as no target has been specified");
2200 return ERROR_OK;
2203 if (gdb_port == 0 && server_use_pipes == 0)
2205 LOG_INFO("gdb port disabled");
2206 return ERROR_OK;
2209 if (server_use_pipes)
2211 /* only a single gdb connection when using a pipe */
2213 gdb_service = malloc(sizeof(struct gdb_service));
2214 gdb_service->target = target;
2216 add_service("gdb", CONNECTION_PIPE, 0, 1, gdb_new_connection, gdb_input, gdb_connection_closed, gdb_service);
2218 LOG_DEBUG("gdb service for target %s using pipes",
2219 target_get_name(target));
2221 else
2223 unsigned short port = gdb_port;
2225 while (target)
2227 gdb_service = malloc(sizeof(struct gdb_service));
2228 gdb_service->target = target;
2230 add_service("gdb", CONNECTION_TCP,
2231 port, 1,
2232 gdb_new_connection, gdb_input,
2233 gdb_connection_closed, gdb_service);
2235 LOG_DEBUG("gdb service for target %s at TCP port %i",
2236 target_get_name(target),
2237 port);
2238 target = target->next;
2239 port++;
2243 return ERROR_OK;
2246 COMMAND_HANDLER(handle_gdb_sync_command)
2248 if (argc != 0)
2250 return ERROR_COMMAND_SYNTAX_ERROR;
2253 if (current_gdb_connection == NULL)
2255 command_print(cmd_ctx,
2256 "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
2257 return ERROR_FAIL;
2260 current_gdb_connection->sync = true;
2262 return ERROR_OK;
2265 /* daemon configuration command gdb_port */
2266 COMMAND_HANDLER(handle_gdb_port_command)
2268 return CALL_COMMAND_HANDLER(server_port_command, &gdb_port);
2271 COMMAND_HANDLER(handle_gdb_memory_map_command)
2273 if (argc == 1)
2275 if (strcmp(args[0], "enable") == 0)
2277 gdb_use_memory_map = 1;
2278 return ERROR_OK;
2280 else if (strcmp(args[0], "disable") == 0)
2282 gdb_use_memory_map = 0;
2283 return ERROR_OK;
2285 else
2286 LOG_WARNING("invalid gdb_memory_map configuration directive %s", args[0]);
2289 return ERROR_COMMAND_SYNTAX_ERROR;
2292 COMMAND_HANDLER(handle_gdb_flash_program_command)
2294 if (argc == 1)
2296 if (strcmp(args[0], "enable") == 0)
2298 gdb_flash_program = 1;
2299 return ERROR_OK;
2301 else if (strcmp(args[0], "disable") == 0)
2303 gdb_flash_program = 0;
2304 return ERROR_OK;
2306 else
2307 LOG_WARNING("invalid gdb_flash_program configuration directive: %s", args[0]);
2310 return ERROR_COMMAND_SYNTAX_ERROR;
2313 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
2315 if (argc == 1)
2317 if (strcmp(args[0], "enable") == 0)
2319 gdb_report_data_abort = 1;
2320 return ERROR_OK;
2322 else if (strcmp(args[0], "disable") == 0)
2324 gdb_report_data_abort = 0;
2325 return ERROR_OK;
2327 else
2328 LOG_WARNING("invalid gdb_report_data_abort configuration directive: %s", args[0]);
2331 return ERROR_COMMAND_SYNTAX_ERROR;
2334 /* gdb_breakpoint_override */
2335 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
2337 if (argc == 0)
2340 } else if (argc == 1)
2342 gdb_breakpoint_override = 1;
2343 if (strcmp(args[0], "hard") == 0)
2345 gdb_breakpoint_override_type = BKPT_HARD;
2346 } else if (strcmp(args[0], "soft") == 0)
2348 gdb_breakpoint_override_type = BKPT_SOFT;
2349 } else if (strcmp(args[0], "disable") == 0)
2351 gdb_breakpoint_override = 0;
2353 } else
2355 return ERROR_COMMAND_SYNTAX_ERROR;
2357 if (gdb_breakpoint_override)
2359 LOG_USER("force %s breakpoints", (gdb_breakpoint_override_type == BKPT_HARD)?"hard":"soft");
2360 } else
2362 LOG_USER("breakpoint type is not overriden");
2365 return ERROR_OK;
2368 int gdb_register_commands(struct command_context *command_context)
2370 register_command(command_context, NULL, "gdb_sync",
2371 handle_gdb_sync_command, COMMAND_ANY,
2372 "next stepi will return immediately allowing GDB to "
2373 "fetch register state without affecting target state");
2374 register_command(command_context, NULL, "gdb_port",
2375 handle_gdb_port_command, COMMAND_ANY,
2376 "daemon configuration command gdb_port");
2377 register_command(command_context, NULL, "gdb_memory_map",
2378 handle_gdb_memory_map_command, COMMAND_CONFIG,
2379 "enable or disable memory map");
2380 register_command(command_context, NULL, "gdb_flash_program",
2381 handle_gdb_flash_program_command, COMMAND_CONFIG,
2382 "enable or disable flash program");
2383 register_command(command_context, NULL, "gdb_report_data_abort",
2384 handle_gdb_report_data_abort_command, COMMAND_CONFIG,
2385 "enable or disable reporting data aborts");
2386 register_command(command_context, NULL, "gdb_breakpoint_override",
2387 handle_gdb_breakpoint_override_command, COMMAND_EXEC,
2388 "hard/soft/disable - force type of breakpoint "
2389 "used by gdb 'break' commands.");
2390 return ERROR_OK;