OpenOCD
gdb_server.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007-2010 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008 by Spencer Oliver *
11  * spen@spen-soft.co.uk *
12  * *
13  * Copyright (C) 2011 by Broadcom Corporation *
14  * Evan Hunter - ehunter@broadcom.com *
15  * *
16  * Copyright (C) ST-Ericsson SA 2011 *
17  * michel.jaouen@stericsson.com : smp minimum support *
18  * *
19  * Copyright (C) 2013 Andes Technology *
20  * Hsiangkai Wang <hkwang@andestech.com> *
21  * *
22  * Copyright (C) 2013 Franck Jullien *
23  * elec4fun@gmail.com *
24  ***************************************************************************/
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include <helper/tcl-common.h>
31 #include <target/breakpoints.h>
32 #include <target/target_request.h>
33 #include <target/register.h>
34 #include <target/target.h>
35 #include <target/target_type.h>
37 #include "server.h"
38 #include <flash/nor/core.h>
39 #include "gdb_server.h"
40 #include <target/image.h>
41 #include <jtag/jtag.h>
42 #include "rtos/rtos.h"
43 #include "target/smp.h"
44 
54 #define CTRL(c) ((c) - '@')
55 
57  /* GDB doesn't accept 'O' packets */
59  /* GDB doesn't accept 'O' packets but accepts notifications */
61  /* GDB accepts 'O' packets */
63 };
64 
66  char *tdesc;
67  uint32_t tdesc_length;
68 };
69 
70 /* private connection data for GDB */
72  char buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for null-termination */
73  char *buf_p;
74  int buf_cnt;
75  bool ctrl_c;
78  bool closed;
79  /* set to prevent re-entrance from log messages during gdb_get_packet()
80  * and gdb_put_packet(). */
81  bool busy;
83  /* set flag to true if you want the next stepi to return immediately.
84  * allowing GDB to pick up a fresh set of register values from the target
85  * without modifying the target state. */
86  bool sync;
87  /* We delay reporting memory write errors until next step/continue or memory
88  * write. This improves performance of gdb load significantly as the GDB packet
89  * can be replied immediately and a new GDB packet will be ready without delay
90  * (ca. 10% or so...). */
92  /* with extended-remote it seems we need to better emulate attach/detach.
93  * what this means is we reply with a W stop reply after a kill packet,
94  * normally we reply with a S reply via gdb_last_signal_packet.
95  * as a side note this behaviour only effects gdb > 6.8 */
96  bool attached;
97  /* set when extended protocol is used */
99  /* temporarily used for target description support */
101  /* temporarily used for thread list support */
102  char *thread_list;
103  /* flag to mask the output from gdb_log_callback() */
105  /* Unique index for this GDB connection. */
106  unsigned int unique_index;
107 };
108 
109 #if 0
110 #define _DEBUG_GDB_IO_
111 #endif
112 
114 
117 
118 static int gdb_error(struct connection *connection, int retval);
119 static char *gdb_port;
120 static char *gdb_port_next;
121 
122 static void gdb_log_callback(void *priv, const char *file, unsigned int line,
123  const char *function, const char *string);
124 
125 static void gdb_sig_halted(struct connection *connection);
126 
127 /* number of gdb connections, mainly to suppress gdb related debugging spam
128  * in helper/log.c when no gdb connections are actually active */
130 
131 /* set if we are sending a memory map to gdb
132  * via qXfer:memory-map:read packet */
133 /* enabled by default*/
134 static bool gdb_use_memory_map = true;
135 /* enabled by default*/
136 static bool gdb_flash_program = true;
137 
138 /* if set, data aborts cause an error to be reported in memory read packets
139  * see the code in gdb_read_memory_packet() for further explanations.
140  * Disabled by default.
141  */
143 /* If set, errors when accessing registers are reported to gdb. Disabled by
144  * default. */
146 
147 /* set if we are sending target descriptions to gdb
148  * via qXfer:features:read packet */
149 /* enabled by default */
150 static bool gdb_use_target_description = true;
151 
152 /* current processing free-run type, used by file-I/O */
153 static char gdb_running_type;
154 
155 /* Find an available target in the SMP group that gdb is connected to. For
156  * commands that affect an entire SMP group (like memory access and run control)
157  * this will give better results than returning the unavailable target and having
158  * the command fail. If gdb was aware that targets can be unavailable we
159  * wouldn't need this logic.
160  */
162 {
164  struct target *target = gdb_service->target;
165  if (target->state == TARGET_UNAVAILABLE && target->smp) {
166  struct target_list *tlist;
168  struct target *t = tlist->target;
169  if (t->state != TARGET_UNAVAILABLE)
170  return t;
171  }
172  /* If we can't find an available target, just return the
173  * original. */
174  }
175  return target;
176 }
177 
178 static int gdb_last_signal(struct target *target)
179 {
180  LOG_TARGET_DEBUG(target, "Debug reason is: %s",
182 
183  switch (target->debug_reason) {
184  case DBG_REASON_DBGRQ:
185  return 0x2; /* SIGINT */
189  return 0x05; /* SIGTRAP */
191  return 0x05; /* SIGTRAP */
193  return 0x05;
195  return 0x0; /* no signal... shouldn't happen */
196  default:
197  LOG_USER("undefined debug reason %d (%s) - target needs reset",
200  return 0x0;
201  }
202 }
203 
205  int timeout_s, int *got_data)
206 {
207  /* a non-blocking socket will block if there is 0 bytes available on the socket,
208  * but return with as many bytes as are available immediately
209  */
210  struct timeval tv;
211  fd_set read_fds;
212  struct gdb_connection *gdb_con = connection->priv;
213  int t;
214  if (!got_data)
215  got_data = &t;
216  *got_data = 0;
217 
218  if (gdb_con->buf_cnt > 0) {
219  *got_data = 1;
220  return ERROR_OK;
221  }
222 
223  FD_ZERO(&read_fds);
224  OCD_FD_SET(connection->fd, &read_fds);
225 
226  tv.tv_sec = timeout_s;
227  tv.tv_usec = 0;
228  if (socket_select(connection->fd + 1, &read_fds, NULL, NULL, &tv) == 0) {
229  /* This can typically be because a "monitor" command took too long
230  * before printing any progress messages
231  */
232  if (timeout_s > 0)
233  return ERROR_GDB_TIMEOUT;
234  else
235  return ERROR_OK;
236  }
237  *got_data = OCD_FD_ISSET(connection->fd, &read_fds) != 0;
238  return ERROR_OK;
239 }
240 
241 static int gdb_get_char_inner(struct connection *connection, int *next_char)
242 {
243  struct gdb_connection *gdb_con = connection->priv;
244  int retval = ERROR_OK;
245 
246 #ifdef _DEBUG_GDB_IO_
247  char *debug_buffer;
248 #endif
249  for (;; ) {
251  gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE);
252  else {
253  retval = check_pending(connection, 1, NULL);
254  if (retval != ERROR_OK)
255  return retval;
256  gdb_con->buf_cnt = read_socket(connection->fd,
257  gdb_con->buffer,
259  }
260 
261  if (gdb_con->buf_cnt > 0)
262  break;
263  if (gdb_con->buf_cnt == 0) {
264  LOG_DEBUG("GDB connection closed by the remote client");
265  gdb_con->closed = true;
267  }
268 
269 #ifdef _WIN32
270  bool retry = (WSAGetLastError() == WSAEWOULDBLOCK);
271 #else
272  bool retry = (errno == EAGAIN);
273 #endif
274 
275  if (retry) {
276  // Try again after a delay
277  usleep(1000);
278  } else {
279  // Print error and close the socket
280  log_socket_error("GDB");
281  gdb_con->closed = true;
283  }
284  }
285 
286 #ifdef _DEBUG_GDB_IO_
287  debug_buffer = strndup(gdb_con->buffer, gdb_con->buf_cnt);
288  LOG_DEBUG("received '%s'", debug_buffer);
289  free(debug_buffer);
290 #endif
291 
292  gdb_con->buf_p = gdb_con->buffer;
293  gdb_con->buf_cnt--;
294  *next_char = *(gdb_con->buf_p++);
295  if (gdb_con->buf_cnt > 0)
296  connection->input_pending = true;
297  else
298  connection->input_pending = false;
299 #ifdef _DEBUG_GDB_IO_
300  LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
301 #endif
302 
303  return retval;
304 }
305 
312 static inline int gdb_get_char_fast(struct connection *connection,
313  int *next_char, char **buf_p, int *buf_cnt)
314 {
315  int retval = ERROR_OK;
316 
317  if ((*buf_cnt)-- > 0) {
318  *next_char = **buf_p;
319  (*buf_p)++;
320  if (*buf_cnt > 0)
321  connection->input_pending = true;
322  else
323  connection->input_pending = false;
324 
325 #ifdef _DEBUG_GDB_IO_
326  LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
327 #endif
328 
329  return ERROR_OK;
330  }
331 
332  struct gdb_connection *gdb_con = connection->priv;
333  gdb_con->buf_p = *buf_p;
334  gdb_con->buf_cnt = *buf_cnt;
335  retval = gdb_get_char_inner(connection, next_char);
336  *buf_p = gdb_con->buf_p;
337  *buf_cnt = gdb_con->buf_cnt;
338 
339  return retval;
340 }
341 
342 static int gdb_get_char(struct connection *connection, int *next_char)
343 {
344  struct gdb_connection *gdb_con = connection->priv;
345  return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
346 }
347 
348 static int gdb_putback_char(struct connection *connection, int last_char)
349 {
350  struct gdb_connection *gdb_con = connection->priv;
351 
352  if (gdb_con->buf_p > gdb_con->buffer) {
353  *(--gdb_con->buf_p) = last_char;
354  gdb_con->buf_cnt++;
355  } else
356  LOG_ERROR("BUG: couldn't put character back");
357 
358  return ERROR_OK;
359 }
360 
361 /* The only way we can detect that the socket is closed is the first time
362  * we write to it, we will fail. Subsequent write operations will
363  * succeed. Shudder! */
364 static int gdb_write(struct connection *connection, const void *data, int len)
365 {
366  struct gdb_connection *gdb_con = connection->priv;
367  if (gdb_con->closed) {
368  LOG_DEBUG("GDB socket marked as closed, cannot write to it.");
370  }
371 
372  if (connection_write(connection, data, len) == len)
373  return ERROR_OK;
374 
375  LOG_WARNING("Error writing to GDB socket. Dropping the connection.");
376  gdb_con->closed = true;
378 }
379 
380 static void gdb_log_incoming_packet(struct connection *connection, const char *packet)
381 {
383  return;
384 
387 
388  /* Avoid dumping non-printable characters to the terminal */
389  const unsigned int packet_len = strlen(packet);
390  const char *nonprint = find_nonprint_char(packet, packet_len);
391  if (nonprint) {
392  /* Does packet at least have a prefix that is printable?
393  * Look within the first 50 chars of the packet. */
394  const char *colon = memchr(packet, ':', MIN(50, packet_len));
395  const bool packet_has_prefix = (colon);
396  const bool packet_prefix_printable = (packet_has_prefix && nonprint > colon);
397 
398  if (packet_prefix_printable) {
399  const unsigned int prefix_len = colon - packet + 1; /* + 1 to include the ':' */
400  const unsigned int payload_len = packet_len - prefix_len;
401  LOG_TARGET_DEBUG(target, "{%d} received packet: %.*s<binary-data-%u-bytes>",
402  gdb_connection->unique_index, prefix_len, packet, payload_len);
403  } else {
404  LOG_TARGET_DEBUG(target, "{%d} received packet: <binary-data-%u-bytes>",
405  gdb_connection->unique_index, packet_len);
406  }
407  } else {
408  /* All chars printable, dump the packet as is */
409  LOG_TARGET_DEBUG(target, "{%d} received packet: %s", gdb_connection->unique_index, packet);
410  }
411 }
412 
413 static void gdb_log_outgoing_packet(struct connection *connection, const char *packet_buf,
414  unsigned int packet_len, unsigned char checksum)
415 {
417  return;
418 
421 
422  if (find_nonprint_char(packet_buf, packet_len))
423  LOG_TARGET_DEBUG(target, "{%d} sending packet: $<binary-data-%u-bytes>#%2.2x",
424  gdb_connection->unique_index, packet_len, checksum);
425  else
426  LOG_TARGET_DEBUG(target, "{%d} sending packet: $%.*s#%2.2x",
427  gdb_connection->unique_index, packet_len, packet_buf, checksum);
428 }
429 
430 static void gdb_log_outgoing_async_notif(struct connection *connection, const char *buf,
431  unsigned int len)
432 {
434  return;
435 
438 
439  LOG_TARGET_DEBUG(target, "{%d} sending packet: %.*s",
440  gdb_connection->unique_index, len, buf);
441 }
442 
444  const char *buffer, int len)
445 {
446  int i;
447  unsigned char my_checksum = 0;
448  int reply;
449  int retval;
450  struct gdb_connection *gdb_con = connection->priv;
451 
452  for (i = 0; i < len; i++)
453  my_checksum += buffer[i];
454 
455 #ifdef _DEBUG_GDB_IO_
456  /*
457  * At this point we should have nothing in the input queue from GDB,
458  * however sometimes '-' is sent even though we've already received
459  * an ACK (+) for everything we've sent off.
460  */
461  int gotdata;
462  for (;; ) {
463  retval = check_pending(connection, 0, &gotdata);
464  if (retval != ERROR_OK)
465  return retval;
466  if (!gotdata)
467  break;
468  retval = gdb_get_char(connection, &reply);
469  if (retval != ERROR_OK)
470  return retval;
471  if (reply == '$') {
472  /* fix a problem with some IAR tools */
474  LOG_DEBUG("Unexpected start of new packet");
475  break;
476  } else if (reply == CTRL('C')) {
477  /* do not discard Ctrl-C */
479  break;
480  }
481 
482  LOG_WARNING("Discard unexpected char %c", reply);
483  }
484 #endif
485 
486  while (1) {
487  gdb_log_outgoing_packet(connection, buffer, len, my_checksum);
488 
489  char local_buffer[1024];
490  local_buffer[0] = '$';
491  if ((size_t)len + 5 <= sizeof(local_buffer)) {
492  /* performance gain on smaller packets by only a single call to gdb_write() */
493  memcpy(local_buffer + 1, buffer, len++);
494  len += snprintf(local_buffer + len, sizeof(local_buffer) - len, "#%02x", my_checksum);
495  retval = gdb_write(connection, local_buffer, len);
496  if (retval != ERROR_OK)
497  return retval;
498  } else {
499  /* larger packets are transmitted directly from caller supplied buffer
500  * by several calls to gdb_write() to avoid dynamic allocation */
501  snprintf(local_buffer + 1, sizeof(local_buffer) - 1, "#%02x", my_checksum);
502  retval = gdb_write(connection, local_buffer, 1);
503  if (retval != ERROR_OK)
504  return retval;
505  retval = gdb_write(connection, buffer, len);
506  if (retval != ERROR_OK)
507  return retval;
508  retval = gdb_write(connection, local_buffer + 1, 3);
509  if (retval != ERROR_OK)
510  return retval;
511  }
512 
513  if (gdb_con->noack_mode)
514  break;
515 
516  retval = gdb_get_char(connection, &reply);
517  if (retval != ERROR_OK)
518  return retval;
519 
520  if (reply == '+') {
522  break;
523  } else if (reply == '-') {
524  /* Stop sending output packets for now */
525  gdb_con->output_flag = GDB_OUTPUT_NO;
527  LOG_WARNING("negative reply, retrying");
528  } else if (reply == CTRL('C')) {
529  gdb_con->ctrl_c = true;
530  gdb_log_incoming_packet(connection, "<Ctrl-C>");
531  retval = gdb_get_char(connection, &reply);
532  if (retval != ERROR_OK)
533  return retval;
534  if (reply == '+') {
536  break;
537  } else if (reply == '-') {
538  /* Stop sending output packets for now */
539  gdb_con->output_flag = GDB_OUTPUT_NO;
541  LOG_WARNING("negative reply, retrying");
542  } else if (reply == '$') {
543  LOG_ERROR("GDB missing ack(1) - assumed good");
545  return ERROR_OK;
546  } else {
547  LOG_ERROR("unknown character(1) 0x%2.2x in reply, dropping connection", reply);
548  gdb_con->closed = true;
550  }
551  } else if (reply == '$') {
552  LOG_ERROR("GDB missing ack(2) - assumed good");
554  return ERROR_OK;
555  } else {
556  LOG_ERROR("unknown character(2) 0x%2.2x in reply, dropping connection",
557  reply);
558  gdb_con->closed = true;
560  }
561  }
562  if (gdb_con->closed)
564 
565  return ERROR_OK;
566 }
567 
568 int gdb_put_packet(struct connection *connection, const char *buffer, int len)
569 {
570  struct gdb_connection *gdb_con = connection->priv;
571  gdb_con->busy = true;
572  int retval = gdb_put_packet_inner(connection, buffer, len);
573  gdb_con->busy = false;
574 
575  /* we sent some data, reset timer for keep alive messages */
576  kept_alive();
577 
578  return retval;
579 }
580 
581 static inline int fetch_packet(struct connection *connection,
582  int *checksum_ok, int noack, int *len, char *buffer)
583 {
584  unsigned char my_checksum = 0;
585  char checksum[3];
586  int character;
587  int retval = ERROR_OK;
588 
589  struct gdb_connection *gdb_con = connection->priv;
590  my_checksum = 0;
591  int count = 0;
592  count = 0;
593 
594  /* move this over into local variables to use registers and give the
595  * more freedom to optimize */
596  char *buf_p = gdb_con->buf_p;
597  int buf_cnt = gdb_con->buf_cnt;
598 
599  for (;; ) {
600  /* The common case is that we have an entire packet with no escape chars.
601  * We need to leave at least 2 bytes in the buffer to have
602  * gdb_get_char() update various bits and bobs correctly.
603  */
604  if ((buf_cnt > 2) && ((buf_cnt + count) < *len)) {
605  /* The compiler will struggle a bit with constant propagation and
606  * aliasing, so we help it by showing that these values do not
607  * change inside the loop
608  */
609  int i;
610  char *buf = buf_p;
611  int run = buf_cnt - 2;
612  i = 0;
613  int done = 0;
614  while (i < run) {
615  character = *buf++;
616  i++;
617  if (character == '#') {
618  /* Danger! character can be '#' when esc is
619  * used so we need an explicit boolean for done here. */
620  done = 1;
621  break;
622  }
623 
624  if (character == '}') {
625  /* data transmitted in binary mode (X packet)
626  * uses 0x7d as escape character */
627  my_checksum += character & 0xff;
628  character = *buf++;
629  i++;
630  my_checksum += character & 0xff;
631  buffer[count++] = (character ^ 0x20) & 0xff;
632  } else {
633  my_checksum += character & 0xff;
634  buffer[count++] = character & 0xff;
635  }
636  }
637  buf_p += i;
638  buf_cnt -= i;
639  if (done)
640  break;
641  }
642  if (count > *len) {
643  LOG_ERROR("packet buffer too small");
645  break;
646  }
647 
648  retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
649  if (retval != ERROR_OK)
650  break;
651 
652  if (character == '#')
653  break;
654 
655  if (character == '}') {
656  /* data transmitted in binary mode (X packet)
657  * uses 0x7d as escape character */
658  my_checksum += character & 0xff;
659 
660  retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
661  if (retval != ERROR_OK)
662  break;
663 
664  my_checksum += character & 0xff;
665  buffer[count++] = (character ^ 0x20) & 0xff;
666  } else {
667  my_checksum += character & 0xff;
668  buffer[count++] = character & 0xff;
669  }
670  }
671 
672  gdb_con->buf_p = buf_p;
673  gdb_con->buf_cnt = buf_cnt;
674 
675  if (retval != ERROR_OK)
676  return retval;
677 
678  *len = count;
679 
680  retval = gdb_get_char(connection, &character);
681  if (retval != ERROR_OK)
682  return retval;
683  checksum[0] = character;
684  retval = gdb_get_char(connection, &character);
685  if (retval != ERROR_OK)
686  return retval;
687  checksum[1] = character;
688  checksum[2] = 0;
689 
690  if (!noack)
691  *checksum_ok = (my_checksum == strtoul(checksum, NULL, 16));
692 
693  return ERROR_OK;
694 }
695 
697  char *buffer, int *len)
698 {
699  int character;
700  int retval;
701  struct gdb_connection *gdb_con = connection->priv;
702 
703  while (1) {
704  do {
705  retval = gdb_get_char(connection, &character);
706  if (retval != ERROR_OK)
707  return retval;
708 
709 #ifdef _DEBUG_GDB_IO_
710  LOG_DEBUG("character: '%c'", character);
711 #endif
712 
713  switch (character) {
714  case '$':
715  break;
716  case '+':
718  /* According to the GDB documentation
719  * (https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html):
720  * "gdb sends a final `+` acknowledgment of the stub's `OK`
721  * response, which can be safely ignored by the stub."
722  * However OpenOCD server already is in noack mode at this
723  * point and instead of ignoring this it was emitting a
724  * warning. This code makes server ignore the first ACK
725  * that will be received after going into noack mode,
726  * warning only about subsequent ACK's. */
727  if (gdb_con->noack_mode > 1) {
728  LOG_WARNING("acknowledgment received, but no packet pending");
729  } else if (gdb_con->noack_mode) {
730  LOG_DEBUG("Received first acknowledgment after entering noack mode. Ignoring it.");
731  gdb_con->noack_mode = 2;
732  }
733  break;
734  case '-':
736  LOG_WARNING("negative acknowledgment, but no packet pending");
737  break;
738  case CTRL('C'):
739  gdb_log_incoming_packet(connection, "<Ctrl-C>");
740  gdb_con->ctrl_c = true;
741  *len = 0;
742  return ERROR_OK;
743  default:
744  LOG_WARNING("ignoring character 0x%x", character);
745  break;
746  }
747  } while (character != '$');
748 
749  int checksum_ok = 0;
750  /* explicit code expansion here to get faster inlined code in -O3 by not
751  * calculating checksum */
752  if (gdb_con->noack_mode) {
753  retval = fetch_packet(connection, &checksum_ok, 1, len, buffer);
754  if (retval != ERROR_OK)
755  return retval;
756  } else {
757  retval = fetch_packet(connection, &checksum_ok, 0, len, buffer);
758  if (retval != ERROR_OK)
759  return retval;
760  }
761 
762  if (gdb_con->noack_mode) {
763  /* checksum is not checked in noack mode */
764  break;
765  }
766  if (checksum_ok) {
767  retval = gdb_write(connection, "+", 1);
768  if (retval != ERROR_OK)
769  return retval;
770  break;
771  }
772  }
773  if (gdb_con->closed)
775 
776  return ERROR_OK;
777 }
778 
779 static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
780 {
781  struct gdb_connection *gdb_con = connection->priv;
782  gdb_con->busy = true;
783  int retval = gdb_get_packet_inner(connection, buffer, len);
784  gdb_con->busy = false;
785  return retval;
786 }
787 
788 static int gdb_output_con(struct connection *connection, const char *line)
789 {
790  char *hex_buffer;
791  int bin_size;
792 
793  bin_size = strlen(line);
794 
795  hex_buffer = malloc(bin_size * 2 + 2);
796  if (!hex_buffer)
798 
799  hex_buffer[0] = 'O';
800  size_t pkt_len = hexify(hex_buffer + 1, (const uint8_t *)line, bin_size,
801  bin_size * 2 + 1);
802  int retval = gdb_put_packet(connection, hex_buffer, pkt_len + 1);
803 
804  free(hex_buffer);
805  return retval;
806 }
807 
808 static int gdb_output(struct command_context *context, const char *line)
809 {
810  /* this will be dumped to the log and also sent as an O packet if possible */
811  LOG_USER_N("%s", line);
812  return ERROR_OK;
813 }
814 
815 static void gdb_signal_reply(struct target *target, struct connection *connection)
816 {
818  char sig_reply[65];
819  char stop_reason[32];
820  char current_thread[25];
821  int sig_reply_len;
822  int signal_var;
823 
825 
827  sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "W00");
828  } else {
829  struct target *ct;
830  struct rtos *rtos;
831 
833  if (rtos) {
836  } else {
837  ct = target;
838  }
839 
840  if (gdb_connection->ctrl_c) {
841  LOG_TARGET_DEBUG(target, "Responding with signal 2 (SIGINT) to debugger due to Ctrl-C");
842  signal_var = 0x2;
843  } else
844  signal_var = gdb_last_signal(ct);
845 
846  stop_reason[0] = '\0';
847  if (ct->debug_reason == DBG_REASON_WATCHPOINT) {
848  enum watchpoint_rw hit_wp_type;
849  target_addr_t hit_wp_address;
850 
851  if (watchpoint_hit(ct, &hit_wp_type, &hit_wp_address) == ERROR_OK) {
852 
853  switch (hit_wp_type) {
854  case WPT_WRITE:
855  snprintf(stop_reason, sizeof(stop_reason),
856  "watch:%08" TARGET_PRIxADDR ";", hit_wp_address);
857  break;
858  case WPT_READ:
859  snprintf(stop_reason, sizeof(stop_reason),
860  "rwatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
861  break;
862  case WPT_ACCESS:
863  snprintf(stop_reason, sizeof(stop_reason),
864  "awatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
865  break;
866  default:
867  break;
868  }
869  }
870  }
871 
872  current_thread[0] = '\0';
873  if (rtos)
874  snprintf(current_thread, sizeof(current_thread), "thread:%" PRIx64 ";",
876 
877  sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "T%2.2x%s%s",
878  signal_var, stop_reason, current_thread);
879 
880  gdb_connection->ctrl_c = false;
881  }
882 
883  gdb_put_packet(connection, sig_reply, sig_reply_len);
885 }
886 
887 static void gdb_fileio_reply(struct target *target, struct connection *connection)
888 {
890  char fileio_command[256];
891  int command_len;
892  bool program_exited = false;
893 
894  if (strcmp(target->fileio_info->identifier, "open") == 0)
895  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
897  target->fileio_info->param_2 + 1, /* len + trailing zero */
900  else if (strcmp(target->fileio_info->identifier, "close") == 0)
901  sprintf(fileio_command, "F%s,%" PRIx64, target->fileio_info->identifier,
903  else if (strcmp(target->fileio_info->identifier, "read") == 0)
904  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
908  else if (strcmp(target->fileio_info->identifier, "write") == 0)
909  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
913  else if (strcmp(target->fileio_info->identifier, "lseek") == 0)
914  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
918  else if (strcmp(target->fileio_info->identifier, "rename") == 0)
919  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
921  target->fileio_info->param_2 + 1, /* len + trailing zero */
923  target->fileio_info->param_4 + 1); /* len + trailing zero */
924  else if (strcmp(target->fileio_info->identifier, "unlink") == 0)
925  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
927  target->fileio_info->param_2 + 1); /* len + trailing zero */
928  else if (strcmp(target->fileio_info->identifier, "stat") == 0)
929  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
933  else if (strcmp(target->fileio_info->identifier, "fstat") == 0)
934  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
937  else if (strcmp(target->fileio_info->identifier, "gettimeofday") == 0)
938  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
941  else if (strcmp(target->fileio_info->identifier, "isatty") == 0)
942  sprintf(fileio_command, "F%s,%" PRIx64, target->fileio_info->identifier,
944  else if (strcmp(target->fileio_info->identifier, "system") == 0)
945  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
947  target->fileio_info->param_2 + 1); /* len + trailing zero */
948  else if (strcmp(target->fileio_info->identifier, "exit") == 0) {
949  /* If target hits exit syscall, report to GDB the program is terminated.
950  * In addition, let target run its own exit syscall handler. */
951  program_exited = true;
952  sprintf(fileio_command, "W%02" PRIx64, target->fileio_info->param_1);
953  } else {
954  LOG_DEBUG("Unknown syscall: %s", target->fileio_info->identifier);
955 
956  /* encounter unknown syscall, continue */
958  target_resume(target, true, 0x0, false, false);
959  return;
960  }
961 
962  command_len = strlen(fileio_command);
963  gdb_put_packet(connection, fileio_command, command_len);
964 
965  if (program_exited) {
966  /* Use target_resume() to let target run its own exit syscall handler. */
968  target_resume(target, true, 0x0, false, false);
969  } else {
972  }
973 }
974 
976 {
978 
979  /* In the GDB protocol when we are stepping or continuing execution,
980  * we have a lingering reply. Upon receiving a halted event
981  * when we have that lingering packet, we reply to the original
982  * step or continue packet.
983  *
984  * Executing monitor commands can bring the target in and
985  * out of the running state so we'll see lots of TARGET_EVENT_XXX
986  * that are to be ignored.
987  */
989  /* stop forwarding log packets! */
991 
992  /* check fileio first */
995  else
997  }
998 }
999 
1001  enum target_event event, void *priv)
1002 {
1003  struct connection *connection = priv;
1005 
1006  if (gdb_target != target)
1007  return ERROR_OK;
1008 
1009  switch (event) {
1010  case TARGET_EVENT_GDB_HALT:
1012  break;
1013  case TARGET_EVENT_HALTED:
1015  break;
1016  default:
1017  break;
1018  }
1019 
1020  return ERROR_OK;
1021 }
1022 
1024 {
1025  struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
1026  struct target *target;
1027  int retval;
1028  int initial_ack;
1029  static unsigned int next_unique_id = 1;
1030 
1034 
1035  /* initialize gdb connection information */
1037  gdb_connection->buf_cnt = 0;
1038  gdb_connection->ctrl_c = false;
1041  gdb_connection->closed = false;
1042  gdb_connection->busy = false;
1044  gdb_connection->sync = false;
1046  gdb_connection->attached = true;
1052  gdb_connection->unique_index = next_unique_id++;
1053 
1054  /* output goes through gdb connection */
1056 
1057  /* we must remove all breakpoints registered to the target as a previous
1058  * GDB session could leave dangling breakpoints if e.g. communication
1059  * timed out.
1060  */
1063 
1064  /* Since version 3.95 (gdb-19990504), with the exclusion of 6.5~6.8, GDB
1065  * sends an ACK at connection with the following comment in its source code:
1066  * "Ack any packet which the remote side has already sent."
1067  * LLDB does the same since the first gdb-remote implementation.
1068  * Remove the initial ACK from the incoming buffer.
1069  */
1070  retval = gdb_get_char(connection, &initial_ack);
1071  if (retval != ERROR_OK)
1072  return retval;
1073 
1074  if (initial_ack != '+')
1075  gdb_putback_char(connection, initial_ack);
1076 
1078 
1079  if (target->rtos) {
1080  /* clean previous rtos session if supported*/
1081  if (target->rtos->type->clean)
1082  target->rtos->type->clean(target);
1083 
1084  /* update threads */
1086  }
1087 
1088  if (gdb_use_memory_map) {
1089  /* Connect must fail if the memory map can't be set up correctly.
1090  *
1091  * This will cause an auto_probe to be invoked, which is either
1092  * a no-op or it will fail when the target isn't ready(e.g. not halted).
1093  */
1094  for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
1095  struct flash_bank *p;
1097  if (p->target != target)
1098  continue;
1099  retval = get_flash_bank_by_num(i, &p);
1100  if (retval != ERROR_OK) {
1101  LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target "
1102  "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1103  return retval;
1104  }
1105  }
1106  }
1107 
1109  __FILE__, __LINE__, __func__,
1110  "New GDB Connection: %d, Target %s, state: %s",
1114 
1115  if (!target_was_examined(target)) {
1116  LOG_TARGET_ERROR(target, "Target not examined yet, refuse gdb connection %d!",
1119  }
1121 
1122  if (target->state != TARGET_HALTED)
1123  LOG_TARGET_WARNING(target, "GDB connection %d not halted",
1125 
1126  /* DANGER! If we fail subsequently, we must remove this handler,
1127  * otherwise we occasionally see crashes as the timer can invoke the
1128  * callback fn.
1129  *
1130  * register callback to be informed about target events */
1132 
1134 
1135  return ERROR_OK;
1136 }
1137 
1139 {
1140  struct target *target;
1142 
1144 
1145  /* we're done forwarding messages. Tear down callback before
1146  * cleaning up connection.
1147  */
1149 
1151  LOG_TARGET_DEBUG(target, "{%d} GDB Close, state: %s, gdb_actual_connections=%d",
1155 
1156  /* see if an image built with vFlash commands is left */
1161  }
1162 
1163  /* if this connection registered a debug-message receiver delete it */
1165 
1166  free(connection->priv);
1167  connection->priv = NULL;
1168 
1170 
1172 
1174 
1175  return ERROR_OK;
1176 }
1177 
1178 static void gdb_send_error(struct connection *connection, uint8_t the_error)
1179 {
1180  char err[4];
1181  snprintf(err, 4, "E%2.2X", the_error);
1182  gdb_put_packet(connection, err, 3);
1183 }
1184 
1186  char const *packet, int packet_size)
1187 {
1189  struct gdb_connection *gdb_con = connection->priv;
1190  char sig_reply[4];
1191  int signal_var;
1192 
1193  if (!gdb_con->attached) {
1194  /* if we are here we have received a kill packet
1195  * reply W stop reply otherwise gdb gets very unhappy */
1196  gdb_put_packet(connection, "W00", 3);
1197  return ERROR_OK;
1198  }
1199 
1200  signal_var = gdb_last_signal(target);
1201 
1202  snprintf(sig_reply, 4, "S%2.2x", signal_var);
1203  gdb_put_packet(connection, sig_reply, 3);
1204 
1205  return ERROR_OK;
1206 }
1207 
1208 static inline int gdb_reg_pos(struct target *target, int pos, int len)
1209 {
1211  return pos;
1212  else
1213  return len - 1 - pos;
1214 }
1215 
1216 /* Convert register to string of bytes. NB! The # of bits in the
1217  * register might be non-divisible by 8(a byte), in which
1218  * case an entire byte is shown.
1219  *
1220  * NB! the format on the wire is the target endianness
1221  *
1222  * The format of reg->value is little endian
1223  *
1224  */
1225 static void gdb_str_to_target(struct target *target,
1226  char *tstr, struct reg *reg)
1227 {
1228  int i;
1229 
1230  uint8_t *buf;
1231  int buf_len;
1232  buf = reg->value;
1233  buf_len = DIV_ROUND_UP(reg->size, 8);
1234 
1235  for (i = 0; i < buf_len; i++) {
1236  int j = gdb_reg_pos(target, i, buf_len);
1237  tstr += sprintf(tstr, "%02x", buf[j]);
1238  }
1239 }
1240 
1241 /* copy over in register buffer */
1242 static void gdb_target_to_reg(struct target *target,
1243  char const *tstr, int str_len, uint8_t *bin)
1244 {
1245  if (str_len % 2) {
1246  LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1247  exit(-1);
1248  }
1249 
1250  int i;
1251  for (i = 0; i < str_len; i += 2) {
1252  unsigned int t;
1253  if (sscanf(tstr + i, "%02x", &t) != 1) {
1254  LOG_ERROR("BUG: unable to convert register value");
1255  exit(-1);
1256  }
1257 
1258  int j = gdb_reg_pos(target, i/2, str_len/2);
1259  bin[j] = t;
1260  }
1261 }
1262 
1263 /* get register value if needed and fill the buffer accordingly */
1264 static int gdb_get_reg_value_as_str(struct target *target, char *tstr, struct reg *reg)
1265 {
1266  int retval = ERROR_OK;
1267 
1268  if (!reg->valid)
1269  retval = reg->type->get(reg);
1270 
1271  const unsigned int len = DIV_ROUND_UP(reg->size, 8) * 2;
1272  switch (retval) {
1273  case ERROR_OK:
1274  gdb_str_to_target(target, tstr, reg);
1275  return ERROR_OK;
1277  memset(tstr, 'x', len);
1278  tstr[len] = '\0';
1279  return ERROR_OK;
1280  }
1281  memset(tstr, '0', len);
1282  tstr[len] = '\0';
1283  return ERROR_FAIL;
1284 }
1285 
1287  char const *packet, int packet_size)
1288 {
1290  struct reg **reg_list;
1291  int reg_list_size;
1292  int retval;
1293  int reg_packet_size = 0;
1294  char *reg_packet;
1295  char *reg_packet_p;
1296  int i;
1297 
1298 #ifdef _DEBUG_GDB_IO_
1299  LOG_DEBUG("-");
1300 #endif
1301 
1303  return ERROR_OK;
1304 
1305  retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1307  if (retval != ERROR_OK)
1308  return gdb_error(connection, retval);
1309 
1310  for (i = 0; i < reg_list_size; i++) {
1311  if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden)
1312  continue;
1313  reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1314  }
1315 
1316  assert(reg_packet_size > 0);
1317 
1318  reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
1319  if (!reg_packet)
1320  return ERROR_FAIL;
1321 
1322  reg_packet_p = reg_packet;
1323 
1324  for (i = 0; i < reg_list_size; i++) {
1325  if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden)
1326  continue;
1327  retval = gdb_get_reg_value_as_str(target, reg_packet_p, reg_list[i]);
1328  if (retval != ERROR_OK && gdb_report_register_access_error) {
1329  LOG_DEBUG("Couldn't get register %s.", reg_list[i]->name);
1330  free(reg_packet);
1331  free(reg_list);
1332  return gdb_error(connection, retval);
1333  }
1334  reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1335  }
1336 
1337 #ifdef _DEBUG_GDB_IO_
1338  {
1339  char *reg_packet_p_debug;
1340  reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1341  LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1342  free(reg_packet_p_debug);
1343  }
1344 #endif
1345 
1346  gdb_put_packet(connection, reg_packet, reg_packet_size);
1347  free(reg_packet);
1348 
1349  free(reg_list);
1350 
1351  return ERROR_OK;
1352 }
1353 
1355  char const *packet, int packet_size)
1356 {
1358  int i;
1359  struct reg **reg_list;
1360  int reg_list_size;
1361  int retval;
1362  char const *packet_p;
1363 
1364 #ifdef _DEBUG_GDB_IO_
1365  LOG_DEBUG("-");
1366 #endif
1367 
1368  /* skip command character */
1369  packet++;
1370  packet_size--;
1371 
1372  if (packet_size % 2) {
1373  LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1375  }
1376 
1377  retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1379  if (retval != ERROR_OK)
1380  return gdb_error(connection, retval);
1381 
1382  packet_p = packet;
1383  for (i = 0; i < reg_list_size; i++) {
1384  uint8_t *bin_buf;
1385  if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden)
1386  continue;
1387  int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1388 
1389  if (packet_p + chars > packet + packet_size)
1390  LOG_ERROR("BUG: register packet is too small for registers");
1391 
1392  bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1393  gdb_target_to_reg(target, packet_p, chars, bin_buf);
1394 
1395  retval = reg_list[i]->type->set(reg_list[i], bin_buf);
1396  if (retval != ERROR_OK && gdb_report_register_access_error) {
1397  LOG_DEBUG("Couldn't set register %s.", reg_list[i]->name);
1398  free(reg_list);
1399  free(bin_buf);
1400  return gdb_error(connection, retval);
1401  }
1402 
1403  /* advance packet pointer */
1404  packet_p += chars;
1405 
1406  free(bin_buf);
1407  }
1408 
1409  /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1410  free(reg_list);
1411 
1412  gdb_put_packet(connection, "OK", 2);
1413 
1414  return ERROR_OK;
1415 }
1416 
1418  char const *packet, int packet_size)
1419 {
1421  char *reg_packet;
1422  int reg_num = strtoul(packet + 1, NULL, 16);
1423  struct reg **reg_list;
1424  int reg_list_size;
1425  int retval;
1426 
1427 #ifdef _DEBUG_GDB_IO_
1428  LOG_DEBUG("-");
1429 #endif
1430 
1431  if (target->rtos) {
1432  retval = rtos_get_gdb_reg(connection, reg_num);
1433  if (retval == ERROR_OK)
1434  return ERROR_OK;
1435  if (retval != ERROR_NOT_IMPLEMENTED)
1436  return gdb_error(connection, retval);
1437  }
1438 
1439  retval = target_get_gdb_reg_list_noread(target, &reg_list, &reg_list_size,
1440  REG_CLASS_ALL);
1441  if (retval != ERROR_OK)
1442  return gdb_error(connection, retval);
1443 
1444  if ((reg_list_size <= reg_num) || !reg_list[reg_num] ||
1445  !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) {
1446  LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num);
1448  }
1449 
1450  reg_packet = calloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1, 1); /* plus one for string termination null */
1451 
1452  retval = gdb_get_reg_value_as_str(target, reg_packet, reg_list[reg_num]);
1453  if (retval != ERROR_OK && gdb_report_register_access_error) {
1454  LOG_DEBUG("Couldn't get register %s.", reg_list[reg_num]->name);
1455  free(reg_packet);
1456  free(reg_list);
1457  return gdb_error(connection, retval);
1458  }
1459 
1460  gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1461 
1462  free(reg_list);
1463  free(reg_packet);
1464 
1465  return ERROR_OK;
1466 }
1467 
1469  char const *packet, int packet_size)
1470 {
1472  char *separator;
1473  int reg_num = strtoul(packet + 1, &separator, 16);
1474  struct reg **reg_list;
1475  int reg_list_size;
1476  int retval;
1477 
1478 #ifdef _DEBUG_GDB_IO_
1479  LOG_DEBUG("-");
1480 #endif
1481 
1482  if (*separator != '=') {
1483  LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1485  }
1486  size_t chars = strlen(separator + 1);
1487  uint8_t *bin_buf = malloc(chars / 2);
1488  gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1489 
1490  if ((target->rtos) &&
1491  (rtos_set_reg(connection, reg_num, bin_buf) == ERROR_OK)) {
1492  free(bin_buf);
1493  gdb_put_packet(connection, "OK", 2);
1494  return ERROR_OK;
1495  }
1496 
1497  retval = target_get_gdb_reg_list_noread(target, &reg_list, &reg_list_size,
1498  REG_CLASS_ALL);
1499  if (retval != ERROR_OK) {
1500  free(bin_buf);
1501  return gdb_error(connection, retval);
1502  }
1503 
1504  if ((reg_list_size <= reg_num) || !reg_list[reg_num] ||
1505  !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) {
1506  LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num);
1507  free(bin_buf);
1508  free(reg_list);
1510  }
1511 
1512  if (chars != (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2)) {
1513  LOG_ERROR("gdb sent %zu bits for a %" PRIu32 "-bit register (%s)",
1514  chars * 4, reg_list[reg_num]->size, reg_list[reg_num]->name);
1515  free(bin_buf);
1516  free(reg_list);
1518  }
1519 
1520  gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1521 
1522  retval = reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1523  if (retval != ERROR_OK && gdb_report_register_access_error) {
1524  LOG_DEBUG("Couldn't set register %s.", reg_list[reg_num]->name);
1525  free(bin_buf);
1526  free(reg_list);
1527  return gdb_error(connection, retval);
1528  }
1529 
1530  gdb_put_packet(connection, "OK", 2);
1531 
1532  free(bin_buf);
1533  free(reg_list);
1534 
1535  return ERROR_OK;
1536 }
1537 
1538 /* No attempt is made to translate the "retval" to
1539  * GDB speak. This has to be done at the calling
1540  * site as no mapping really exists.
1541  */
1542 static int gdb_error(struct connection *connection, int retval)
1543 {
1544  LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1545  gdb_send_error(connection, EFAULT);
1546  return ERROR_OK;
1547 }
1548 
1550  char const *packet, int packet_size)
1551 {
1553  char *separator;
1554  uint64_t addr = 0;
1555  uint32_t len = 0;
1556 
1557  uint8_t *buffer;
1558  char *hex_buffer;
1559 
1560  int retval = ERROR_OK;
1561 
1562  /* skip command character */
1563  packet++;
1564 
1565  addr = strtoull(packet, &separator, 16);
1566 
1567  if (*separator != ',') {
1568  LOG_ERROR("incomplete read memory packet received, dropping connection");
1570  }
1571 
1572  len = strtoul(separator + 1, NULL, 16);
1573 
1574  if (!len) {
1575  LOG_WARNING("invalid read memory packet received (len == 0)");
1576  gdb_put_packet(connection, "", 0);
1577  return ERROR_OK;
1578  }
1579 
1580  buffer = malloc(len);
1581 
1582  LOG_DEBUG("addr: 0x%16.16" PRIx64 ", len: 0x%8.8" PRIx32, addr, len);
1583 
1584  retval = ERROR_NOT_IMPLEMENTED;
1585  if (target->rtos)
1586  retval = rtos_read_buffer(target, addr, len, buffer);
1587  if (retval == ERROR_NOT_IMPLEMENTED)
1588  retval = target_read_buffer(target, addr, len, buffer);
1589 
1590  if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1591  /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1592  * At some point this might be fixed in GDB, in which case this code can be removed.
1593  *
1594  * OpenOCD developers are acutely aware of this problem, but there is nothing
1595  * gained by involving the user in this problem that hopefully will get resolved
1596  * eventually
1597  *
1598  * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1599  * cmd = view%20audit-trail&database = gdb&pr = 2395
1600  *
1601  * For now, the default is to fix up things to make current GDB versions work.
1602  * This can be overwritten using the "gdb report_data_abort <'enable'|'disable'>" command.
1603  */
1604  memset(buffer, 0, len);
1605  retval = ERROR_OK;
1606  }
1607 
1608  if (retval == ERROR_OK) {
1609  hex_buffer = malloc(len * 2 + 1);
1610 
1611  size_t pkt_len = hexify(hex_buffer, buffer, len, len * 2 + 1);
1612 
1613  gdb_put_packet(connection, hex_buffer, pkt_len);
1614 
1615  free(hex_buffer);
1616  } else
1617  retval = gdb_error(connection, retval);
1618 
1619  free(buffer);
1620 
1621  return retval;
1622 }
1623 
1625  char const *packet, int packet_size)
1626 {
1628  char *separator;
1629  uint64_t addr = 0;
1630  uint32_t len = 0;
1631 
1632  uint8_t *buffer;
1633  int retval;
1634 
1635  /* skip command character */
1636  packet++;
1637 
1638  addr = strtoull(packet, &separator, 16);
1639 
1640  if (*separator != ',') {
1641  LOG_ERROR("incomplete write memory packet received, dropping connection");
1643  }
1644 
1645  len = strtoul(separator + 1, &separator, 16);
1646 
1647  if (*(separator++) != ':') {
1648  LOG_ERROR("incomplete write memory packet received, dropping connection");
1650  }
1651 
1652  buffer = malloc(len);
1653 
1654  LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32, addr, len);
1655 
1656  if (unhexify(buffer, separator, len) != len)
1657  LOG_ERROR("unable to decode memory packet");
1658 
1659  retval = ERROR_NOT_IMPLEMENTED;
1660  if (target->rtos)
1661  retval = rtos_write_buffer(target, addr, len, buffer);
1662  if (retval == ERROR_NOT_IMPLEMENTED)
1663  retval = target_write_buffer(target, addr, len, buffer);
1664 
1665  if (retval == ERROR_OK)
1666  gdb_put_packet(connection, "OK", 2);
1667  else
1668  retval = gdb_error(connection, retval);
1669 
1670  free(buffer);
1671 
1672  return retval;
1673 }
1674 
1676  char const *packet, int packet_size)
1677 {
1679  char *separator;
1680  uint64_t addr = 0;
1681  uint32_t len = 0;
1682 
1683  int retval = ERROR_OK;
1684  /* Packets larger than fast_limit bytes will be acknowledged instantly on
1685  * the assumption that we're in a download and it's important to go as fast
1686  * as possible. */
1687  uint32_t fast_limit = 8;
1688 
1689  /* skip command character */
1690  packet++;
1691 
1692  addr = strtoull(packet, &separator, 16);
1693 
1694  if (*separator != ',') {
1695  LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1697  }
1698 
1699  len = strtoul(separator + 1, &separator, 16);
1700 
1701  if (*(separator++) != ':') {
1702  LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1704  }
1705 
1707 
1709  retval = ERROR_FAIL;
1710 
1711  if (retval == ERROR_OK) {
1712  if (len >= fast_limit) {
1713  /* By replying the packet *immediately* GDB will send us a new packet
1714  * while we write the last one to the target.
1715  * We only do this for larger writes, so that users who do something like:
1716  * p *((int*)0xdeadbeef)=8675309
1717  * will get immediate feedback that that write failed.
1718  */
1719  gdb_put_packet(connection, "OK", 2);
1720  }
1721  } else {
1722  retval = gdb_error(connection, retval);
1723  /* now that we have reported the memory write error, we can clear the condition */
1725  if (retval != ERROR_OK)
1726  return retval;
1727  }
1728 
1729  if (len) {
1730  LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32, addr, len);
1731 
1732  retval = ERROR_NOT_IMPLEMENTED;
1733  if (target->rtos)
1734  retval = rtos_write_buffer(target, addr, len, (uint8_t *)separator);
1735  if (retval == ERROR_NOT_IMPLEMENTED)
1736  retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1737 
1738  if (retval != ERROR_OK)
1740  }
1741 
1742  if (len < fast_limit) {
1743  if (retval != ERROR_OK) {
1744  gdb_error(connection, retval);
1746  } else {
1747  gdb_put_packet(connection, "OK", 2);
1748  }
1749  }
1750 
1751  return ERROR_OK;
1752 }
1753 
1755  char const *packet, int packet_size)
1756 {
1758  bool current = false;
1759  uint64_t address = 0x0;
1760  int retval = ERROR_OK;
1761 
1762  LOG_DEBUG("-");
1763 
1764  if (packet_size > 1)
1765  address = strtoull(packet + 1, NULL, 16);
1766  else
1767  current = true;
1768 
1769  gdb_running_type = packet[0];
1770  if (packet[0] == 'c') {
1771  LOG_DEBUG("continue");
1772  /* resume at current address, don't handle breakpoints, not debugging */
1773  retval = target_resume(target, current, address, false, false);
1774  } else if (packet[0] == 's') {
1775  LOG_DEBUG("step");
1776  /* step at current or address, don't handle breakpoints */
1777  retval = target_step(target, current, address, false);
1778  }
1779  return retval;
1780 }
1781 
1783  char const *packet, int packet_size)
1784 {
1786  int type;
1787  enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1788  enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1789  uint64_t address;
1790  uint32_t size;
1791  char *separator;
1792  int retval;
1793 
1794  LOG_DEBUG("[%s]", target_name(target));
1795 
1796  type = strtoul(packet + 1, &separator, 16);
1797 
1798  if (type == 0) /* memory breakpoint */
1799  bp_type = BKPT_SOFT;
1800  else if (type == 1) /* hardware breakpoint */
1801  bp_type = BKPT_HARD;
1802  else if (type == 2) /* write watchpoint */
1803  wp_type = WPT_WRITE;
1804  else if (type == 3) /* read watchpoint */
1805  wp_type = WPT_READ;
1806  else if (type == 4) /* access watchpoint */
1807  wp_type = WPT_ACCESS;
1808  else {
1809  LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1811  }
1812 
1813  if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1814  bp_type = gdb_breakpoint_override_type;
1815 
1816  if (*separator != ',') {
1817  LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1819  }
1820 
1821  address = strtoull(separator + 1, &separator, 16);
1822 
1823  if (*separator != ',') {
1824  LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1826  }
1827 
1828  size = strtoul(separator + 1, &separator, 16);
1829 
1830  switch (type) {
1831  case 0:
1832  case 1:
1833  if (packet[0] == 'Z') {
1834  struct target *bp_target = target;
1835  if (target->rtos && bp_type == BKPT_SOFT) {
1836  bp_target = rtos_swbp_target(target, address, size, bp_type);
1837  if (!bp_target) {
1838  retval = ERROR_FAIL;
1839  break;
1840  }
1841  }
1842  retval = breakpoint_add(bp_target, address, size, bp_type);
1843  } else {
1844  assert(packet[0] == 'z');
1845  retval = breakpoint_remove(target, address);
1846  }
1847  break;
1848  case 2:
1849  case 3:
1850  case 4:
1851  {
1852  if (packet[0] == 'Z') {
1854  } else {
1855  assert(packet[0] == 'z');
1856  retval = watchpoint_remove(target, address);
1857  }
1858  break;
1859  }
1860  default:
1861  {
1862  retval = ERROR_NOT_IMPLEMENTED;
1863  break;
1864  }
1865  }
1866 
1867  if (retval == ERROR_NOT_IMPLEMENTED) {
1868  /* Send empty reply to report that watchpoints of this type are not supported */
1869  return gdb_put_packet(connection, "", 0);
1870  }
1871  if (retval != ERROR_OK)
1872  return gdb_error(connection, retval);
1873  return gdb_put_packet(connection, "OK", 2);
1874 }
1875 
1876 /* print out a string and allocate more space as needed,
1877  * mainly used for XML at this point
1878  */
1879 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6))) void xml_printf(int *retval,
1880  char **xml, int *pos, int *size, const char *fmt, ...)
1881 {
1882  if (*retval != ERROR_OK)
1883  return;
1884  int first = 1;
1885 
1886  for (;; ) {
1887  if ((!*xml) || (!first)) {
1888  /* start by 0 to exercise all the code paths.
1889  * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1890 
1891  *size = *size * 2 + 2;
1892  char *t = *xml;
1893  *xml = realloc(*xml, *size);
1894  if (!*xml) {
1895  free(t);
1896  *retval = ERROR_SERVER_REMOTE_CLOSED;
1897  return;
1898  }
1899  }
1900 
1901  va_list ap;
1902  int ret;
1903  va_start(ap, fmt);
1904  ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1905  va_end(ap);
1906  if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1907  *pos += ret;
1908  return;
1909  }
1910  /* there was just enough or not enough space, allocate more. */
1911  first = 0;
1912  }
1913 }
1914 
1915 static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
1916 {
1917  /* Locate the annex. */
1918  const char *annex_end = strchr(buf, ':');
1919  if (!annex_end)
1920  return ERROR_FAIL;
1921 
1922  /* After the read marker and annex, qXfer looks like a
1923  * traditional 'm' packet. */
1924  char *separator;
1925  *ofs = strtoul(annex_end + 1, &separator, 16);
1926 
1927  if (*separator != ',')
1928  return ERROR_FAIL;
1929 
1930  *len = strtoul(separator + 1, NULL, 16);
1931 
1932  /* Extract the annex if needed */
1933  if (annex) {
1934  *annex = strndup(buf, annex_end - buf);
1935  if (!*annex)
1936  return ERROR_FAIL;
1937  }
1938 
1939  return ERROR_OK;
1940 }
1941 
1942 static int compare_bank(const void *a, const void *b)
1943 {
1944  struct flash_bank *b1, *b2;
1945  b1 = *((struct flash_bank **)a);
1946  b2 = *((struct flash_bank **)b);
1947 
1948  if (b1->base == b2->base)
1949  return 0;
1950  else if (b1->base > b2->base)
1951  return 1;
1952  else
1953  return -1;
1954 }
1955 
1957  char const *packet, int packet_size)
1958 {
1959  /* We get away with only specifying flash here. Regions that are not
1960  * specified are treated as if we provided no memory map(if not we
1961  * could detect the holes and mark them as RAM).
1962  * Normally we only execute this code once, but no big deal if we
1963  * have to regenerate it a couple of times.
1964  */
1965 
1967  struct flash_bank *p;
1968  char *xml = NULL;
1969  int size = 0;
1970  int pos = 0;
1971  int retval = ERROR_OK;
1972  struct flash_bank **banks;
1973  int offset;
1974  int length;
1975  char *separator;
1976  target_addr_t ram_start = 0;
1977  unsigned int target_flash_banks = 0;
1978 
1979  /* skip command character */
1980  packet += 23;
1981 
1982  offset = strtoul(packet, &separator, 16);
1983  length = strtoul(separator + 1, &separator, 16);
1984 
1985  xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1986 
1987  /* Sort banks in ascending order. We need to report non-flash
1988  * memory as ram (or rather read/write) by default for GDB, since
1989  * it has no concept of non-cacheable read/write memory (i/o etc).
1990  */
1991  banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1992 
1993  for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
1995  if (p->target != target)
1996  continue;
1997  retval = get_flash_bank_by_num(i, &p);
1998  if (retval != ERROR_OK) {
1999  free(banks);
2000  gdb_error(connection, retval);
2001  return retval;
2002  }
2003  banks[target_flash_banks++] = p;
2004  }
2005 
2006  qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
2007  compare_bank);
2008 
2009  for (unsigned int i = 0; i < target_flash_banks; i++) {
2010  unsigned int sector_size = 0;
2011  unsigned int group_len = 0;
2012 
2013  p = banks[i];
2014 
2015  if (ram_start < p->base)
2016  xml_printf(&retval, &xml, &pos, &size,
2017  "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
2018  "length=\"" TARGET_ADDR_FMT "\"/>\n",
2019  ram_start, p->base - ram_start);
2020 
2021  if (p->read_only) {
2022  xml_printf(&retval, &xml, &pos, &size,
2023  "<memory type=\"rom\" start=\"" TARGET_ADDR_FMT "\" "
2024  "length=\"0x%x\"/>\n",
2025  p->base, p->size);
2026  } else {
2027  if (p->num_sectors == 0) {
2028  xml_printf(&retval, &xml, &pos, &size,
2029  "<memory type=\"flash\" "
2030  "start=\"" TARGET_ADDR_FMT "\" "
2031  "length=\"0x%x\">"
2032  "<property name=\"blocksize\">0x%x</property>\n"
2033  "</memory>\n", p->base, p->size, p->size);
2034  }
2035 
2036  /* Report adjacent groups of same-size sectors. So for
2037  * example top boot CFI flash will list an initial region
2038  * with several large sectors (maybe 128KB) and several
2039  * smaller ones at the end (maybe 32KB). STR7 will have
2040  * regions with 8KB, 32KB, and 64KB sectors; etc.
2041  */
2042  for (unsigned int j = 0; j < p->num_sectors; j++) {
2043  // Maybe start a new group of sectors
2044  if (sector_size == 0) {
2045  if (p->sectors[j].offset + p->sectors[j].size > p->size) {
2046  LOG_WARNING("The flash sector at offset 0x%08" PRIx32
2047  " overflows the end of %s bank.",
2048  p->sectors[j].offset, p->name);
2049  LOG_WARNING("The rest of bank will not show in gdb memory map.");
2050  break;
2051  }
2053  start = p->base + p->sectors[j].offset;
2054  xml_printf(&retval, &xml, &pos, &size,
2055  "<memory type=\"flash\" "
2056  "start=\"" TARGET_ADDR_FMT "\" ",
2057  start);
2058  sector_size = p->sectors[j].size;
2059  group_len = sector_size;
2060  } else {
2061  group_len += sector_size; /* equal to p->sectors[j].size */
2062  }
2063 
2064  /* Does this finish a group of sectors?
2065  * If not, continue an already-started group.
2066  */
2067  if (j < p->num_sectors - 1
2068  && p->sectors[j + 1].size == sector_size
2069  && p->sectors[j + 1].offset == p->sectors[j].offset + sector_size
2070  && p->sectors[j + 1].offset + p->sectors[j + 1].size <= p->size)
2071  continue;
2072 
2073  xml_printf(&retval, &xml, &pos, &size,
2074  "length=\"0x%x\">\n"
2075  "<property name=\"blocksize\">"
2076  "0x%x</property>\n"
2077  "</memory>\n",
2078  group_len,
2079  sector_size);
2080  sector_size = 0;
2081  }
2082  }
2083 
2084  ram_start = p->base + p->size;
2085  }
2086 
2087  if (ram_start != 0)
2088  xml_printf(&retval, &xml, &pos, &size,
2089  "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
2090  "length=\"" TARGET_ADDR_FMT "\"/>\n",
2091  ram_start, target_address_max(target) - ram_start + 1);
2092  /* ELSE a flash chip could be at the very end of the address space, in
2093  * which case ram_start will be precisely 0 */
2094 
2095  free(banks);
2096 
2097  xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
2098 
2099  if (retval != ERROR_OK) {
2100  free(xml);
2101  gdb_error(connection, retval);
2102  return retval;
2103  }
2104 
2105  if (offset + length > pos)
2106  length = pos - offset;
2107 
2108  char *t = malloc(length + 1);
2109  t[0] = 'l';
2110  memcpy(t + 1, xml + offset, length);
2111  gdb_put_packet(connection, t, length + 1);
2112 
2113  free(t);
2114  free(xml);
2115  return ERROR_OK;
2116 }
2117 
2118 static const char *gdb_get_reg_type_name(enum reg_type type)
2119 {
2120  switch (type) {
2121  case REG_TYPE_BOOL:
2122  return "bool";
2123  case REG_TYPE_INT:
2124  return "int";
2125  case REG_TYPE_INT8:
2126  return "int8";
2127  case REG_TYPE_INT16:
2128  return "int16";
2129  case REG_TYPE_INT32:
2130  return "int32";
2131  case REG_TYPE_INT64:
2132  return "int64";
2133  case REG_TYPE_INT128:
2134  return "int128";
2135  case REG_TYPE_UINT:
2136  return "uint";
2137  case REG_TYPE_UINT8:
2138  return "uint8";
2139  case REG_TYPE_UINT16:
2140  return "uint16";
2141  case REG_TYPE_UINT32:
2142  return "uint32";
2143  case REG_TYPE_UINT64:
2144  return "uint64";
2145  case REG_TYPE_UINT128:
2146  return "uint128";
2147  case REG_TYPE_CODE_PTR:
2148  return "code_ptr";
2149  case REG_TYPE_DATA_PTR:
2150  return "data_ptr";
2151  case REG_TYPE_FLOAT:
2152  return "float";
2153  case REG_TYPE_IEEE_SINGLE:
2154  return "ieee_single";
2155  case REG_TYPE_IEEE_DOUBLE:
2156  return "ieee_double";
2157  case REG_TYPE_ARCH_DEFINED:
2158  return "int"; /* return arbitrary string to avoid compile warning. */
2159  }
2160 
2161  return "int"; /* "int" as default value */
2162 }
2163 
2164 static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id,
2165  int *num_arch_defined_types)
2166 {
2167  int tbl_sz = *num_arch_defined_types;
2168 
2169  if (type_id && (strcmp(type_id, ""))) {
2170  for (int j = 0; j < (tbl_sz + 1); j++) {
2171  if (!((*arch_defined_types_list)[j])) {
2172  (*arch_defined_types_list)[tbl_sz++] = type_id;
2173  *arch_defined_types_list = realloc(*arch_defined_types_list,
2174  sizeof(char *) * (tbl_sz + 1));
2175  (*arch_defined_types_list)[tbl_sz] = NULL;
2176  *num_arch_defined_types = tbl_sz;
2177  return 1;
2178  } else {
2179  if (!strcmp((*arch_defined_types_list)[j], type_id))
2180  return 0;
2181  }
2182  }
2183  }
2184 
2185  return -1;
2186 }
2187 
2189  char **tdesc, int *pos, int *size, struct reg_data_type *type,
2190  char const **arch_defined_types_list[], int *num_arch_defined_types)
2191 {
2192  int retval = ERROR_OK;
2193 
2194  if (type->type_class == REG_TYPE_CLASS_VECTOR) {
2195  struct reg_data_type *data_type = type->reg_type_vector->type;
2197  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2198  num_arch_defined_types))
2200  arch_defined_types_list,
2201  num_arch_defined_types);
2202  }
2203  /* <vector id="id" type="type" count="count"/> */
2204  xml_printf(&retval, tdesc, pos, size,
2205  "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32 "\"/>\n",
2206  type->id, type->reg_type_vector->type->id,
2207  type->reg_type_vector->count);
2208 
2209  } else if (type->type_class == REG_TYPE_CLASS_UNION) {
2210  struct reg_data_type_union_field *field;
2211  field = type->reg_type_union->fields;
2212  while (field) {
2213  struct reg_data_type *data_type = field->type;
2215  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2216  num_arch_defined_types))
2218  arch_defined_types_list,
2219  num_arch_defined_types);
2220  }
2221 
2222  field = field->next;
2223  }
2224  /* <union id="id">
2225  * <field name="name" type="type"/> ...
2226  * </union> */
2227  xml_printf(&retval, tdesc, pos, size,
2228  "<union id=\"%s\">\n",
2229  type->id);
2230 
2231  field = type->reg_type_union->fields;
2232  while (field) {
2233  xml_printf(&retval, tdesc, pos, size,
2234  "<field name=\"%s\" type=\"%s\"/>\n",
2235  field->name, field->type->id);
2236 
2237  field = field->next;
2238  }
2239 
2240  xml_printf(&retval, tdesc, pos, size,
2241  "</union>\n");
2242 
2243  } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
2244  struct reg_data_type_struct_field *field;
2245  field = type->reg_type_struct->fields;
2246 
2247  if (field->use_bitfields) {
2248  /* <struct id="id" size="size">
2249  * <field name="name" start="start" end="end"/> ...
2250  * </struct> */
2251  xml_printf(&retval, tdesc, pos, size,
2252  "<struct id=\"%s\" size=\"%" PRIu32 "\">\n",
2253  type->id, type->reg_type_struct->size);
2254  while (field) {
2255  xml_printf(&retval, tdesc, pos, size,
2256  "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2257  field->name, field->bitfield->start, field->bitfield->end,
2259 
2260  field = field->next;
2261  }
2262  } else {
2263  while (field) {
2264  struct reg_data_type *data_type = field->type;
2266  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2267  num_arch_defined_types))
2269  arch_defined_types_list,
2270  num_arch_defined_types);
2271  }
2272  }
2273 
2274  /* <struct id="id">
2275  * <field name="name" type="type"/> ...
2276  * </struct> */
2277  xml_printf(&retval, tdesc, pos, size,
2278  "<struct id=\"%s\">\n",
2279  type->id);
2280  while (field) {
2281  xml_printf(&retval, tdesc, pos, size,
2282  "<field name=\"%s\" type=\"%s\"/>\n",
2283  field->name, field->type->id);
2284 
2285  field = field->next;
2286  }
2287  }
2288 
2289  xml_printf(&retval, tdesc, pos, size,
2290  "</struct>\n");
2291 
2292  } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
2293  /* <flags id="id" size="size">
2294  * <field name="name" start="start" end="end"/> ...
2295  * </flags> */
2296  xml_printf(&retval, tdesc, pos, size,
2297  "<flags id=\"%s\" size=\"%" PRIu32 "\">\n",
2298  type->id, type->reg_type_flags->size);
2299 
2300  struct reg_data_type_flags_field *field;
2301  field = type->reg_type_flags->fields;
2302  while (field) {
2303  xml_printf(&retval, tdesc, pos, size,
2304  "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2305  field->name, field->bitfield->start, field->bitfield->end,
2307 
2308  field = field->next;
2309  }
2310 
2311  xml_printf(&retval, tdesc, pos, size,
2312  "</flags>\n");
2313 
2314  }
2315 
2316  return ERROR_OK;
2317 }
2318 
2319 /* Get a list of available target registers features. feature_list must
2320  * be freed by caller.
2321  */
2322 static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
2323  struct reg **reg_list, int reg_list_size)
2324 {
2325  int tbl_sz = 0;
2326 
2327  /* Start with only one element */
2328  *feature_list = calloc(1, sizeof(char *));
2329 
2330  for (int i = 0; i < reg_list_size; i++) {
2331  if (!reg_list[i]->exist || reg_list[i]->hidden)
2332  continue;
2333 
2334  if (reg_list[i]->feature
2335  && reg_list[i]->feature->name
2336  && (strcmp(reg_list[i]->feature->name, ""))) {
2337  /* We found a feature, check if the feature is already in the
2338  * table. If not, allocate a new entry for the table and
2339  * put the new feature in it.
2340  */
2341  for (int j = 0; j < (tbl_sz + 1); j++) {
2342  if (!((*feature_list)[j])) {
2343  (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
2344  *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
2345  (*feature_list)[tbl_sz] = NULL;
2346  break;
2347  } else {
2348  if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
2349  break;
2350  }
2351  }
2352  }
2353  }
2354 
2355  if (feature_list_size)
2356  *feature_list_size = tbl_sz;
2357 
2358  return ERROR_OK;
2359 }
2360 
2361 /* Create a register list that's the union of all the registers of the SMP
2362  * group this target is in. If the target is not part of an SMP group, this
2363  * returns the same as target_get_gdb_reg_list_noread().
2364  */
2365 static int smp_reg_list_noread(struct target *target,
2366  struct reg **combined_list[], int *combined_list_size,
2367  enum target_register_class reg_class)
2368 {
2369  if (!target->smp)
2370  return target_get_gdb_reg_list_noread(target, combined_list,
2371  combined_list_size, REG_CLASS_ALL);
2372 
2373  unsigned int combined_allocated = 256;
2374  struct reg **local_list = malloc(combined_allocated * sizeof(struct reg *));
2375  if (!local_list) {
2376  LOG_ERROR("malloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2377  return ERROR_FAIL;
2378  }
2379  unsigned int local_list_size = 0;
2380 
2381  struct target_list *head;
2383  if (!target_was_examined(head->target))
2384  continue;
2385 
2386  struct reg **reg_list = NULL;
2387  int reg_list_size;
2388  int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2389  &reg_list_size, reg_class);
2390  if (result != ERROR_OK) {
2391  free(local_list);
2392  return result;
2393  }
2394  for (int i = 0; i < reg_list_size; i++) {
2395  bool found = false;
2396  struct reg *a = reg_list[i];
2397  if (a->exist) {
2398  /* Nested loop makes this O(n^2), but this entire function with
2399  * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2400  * for me. */
2401  for (unsigned int j = 0; j < local_list_size; j++) {
2402  struct reg *b = local_list[j];
2403  if (!strcmp(a->name, b->name)) {
2404  found = true;
2405  if (a->size != b->size) {
2406  LOG_ERROR("SMP register %s is %d bits on one "
2407  "target, but %d bits on another target.",
2408  a->name, a->size, b->size);
2409  free(reg_list);
2410  free(local_list);
2411  return ERROR_FAIL;
2412  }
2413  break;
2414  }
2415  }
2416  if (!found) {
2417  LOG_TARGET_DEBUG(target, "%s not found in combined list", a->name);
2418  if (local_list_size >= combined_allocated) {
2419  combined_allocated *= 2;
2420  local_list = realloc(local_list, combined_allocated * sizeof(struct reg *));
2421  if (!local_list) {
2422  LOG_ERROR("realloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2423  free(reg_list);
2424  return ERROR_FAIL;
2425  }
2426  }
2427  local_list[local_list_size] = a;
2428  local_list_size++;
2429  }
2430  }
2431  }
2432  free(reg_list);
2433  }
2434 
2435  if (local_list_size == 0) {
2436  LOG_ERROR("Unable to get register list");
2437  free(local_list);
2438  return ERROR_FAIL;
2439  }
2440 
2441  /* Now warn the user about any registers that weren't found in every target. */
2443  if (!target_was_examined(head->target))
2444  continue;
2445 
2446  struct reg **reg_list = NULL;
2447  int reg_list_size;
2448  int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2449  &reg_list_size, reg_class);
2450  if (result != ERROR_OK) {
2451  free(local_list);
2452  return result;
2453  }
2454  for (unsigned int i = 0; i < local_list_size; i++) {
2455  bool found = false;
2456  struct reg *a = local_list[i];
2457  for (int j = 0; j < reg_list_size; j++) {
2458  struct reg *b = reg_list[j];
2459  if (b->exist && !strcmp(a->name, b->name)) {
2460  found = true;
2461  break;
2462  }
2463  }
2464  if (!found) {
2465  LOG_TARGET_WARNING(head->target, "Register %s does not exist, which is part of an SMP group where "
2466  "this register does exist.", a->name);
2467  }
2468  }
2469  free(reg_list);
2470  }
2471 
2472  *combined_list = local_list;
2473  *combined_list_size = local_list_size;
2474  return ERROR_OK;
2475 }
2476 
2477 static int gdb_generate_target_description(struct target *target, char **tdesc_out)
2478 {
2479  int retval = ERROR_OK;
2480  struct reg **reg_list = NULL;
2481  int reg_list_size;
2482  char const *architecture;
2483  char const **features = NULL;
2484  int feature_list_size = 0;
2485  char *tdesc = NULL;
2486  int pos = 0;
2487  int size = 0;
2488 
2489 
2490  retval = smp_reg_list_noread(target, &reg_list, &reg_list_size,
2491  REG_CLASS_ALL);
2492 
2493  if (retval != ERROR_OK) {
2494  LOG_ERROR("get register list failed");
2495  retval = ERROR_FAIL;
2496  goto error;
2497  }
2498 
2499  if (reg_list_size <= 0) {
2500  LOG_ERROR("get register list failed");
2501  retval = ERROR_FAIL;
2502  goto error;
2503  }
2504 
2505  /* Get a list of available target registers features */
2506  retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2507  if (retval != ERROR_OK) {
2508  LOG_ERROR("Can't get the registers feature list");
2509  retval = ERROR_FAIL;
2510  goto error;
2511  }
2512 
2513  /* If we found some features associated with registers, create sections */
2514  int current_feature = 0;
2515 
2516  xml_printf(&retval, &tdesc, &pos, &size,
2517  "<?xml version=\"1.0\"?>\n"
2518  "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2519  "<target version=\"1.0\">\n");
2520 
2521  /* generate architecture element if supported by target */
2522  architecture = target_get_gdb_arch(target);
2523  if (architecture)
2524  xml_printf(&retval, &tdesc, &pos, &size,
2525  "<architecture>%s</architecture>\n", architecture);
2526 
2527  /* generate target description according to register list */
2528  if (features) {
2529  while (features[current_feature]) {
2530  char const **arch_defined_types = NULL;
2531  int num_arch_defined_types = 0;
2532 
2533  arch_defined_types = calloc(1, sizeof(char *));
2534  xml_printf(&retval, &tdesc, &pos, &size,
2535  "<feature name=\"%s\">\n",
2536  features[current_feature]);
2537 
2538  int i;
2539  for (i = 0; i < reg_list_size; i++) {
2540 
2541  if (!reg_list[i]->exist || reg_list[i]->hidden)
2542  continue;
2543 
2544  if (strcmp(reg_list[i]->feature->name, features[current_feature]))
2545  continue;
2546 
2547  const char *type_str;
2548  if (reg_list[i]->reg_data_type) {
2549  if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
2550  /* generate <type... first, if there are architecture-defined types. */
2551  if (lookup_add_arch_defined_types(&arch_defined_types,
2552  reg_list[i]->reg_data_type->id,
2553  &num_arch_defined_types))
2555  reg_list[i]->reg_data_type,
2556  &arch_defined_types,
2557  &num_arch_defined_types);
2558 
2559  type_str = reg_list[i]->reg_data_type->id;
2560  } else {
2561  /* predefined type */
2562  type_str = gdb_get_reg_type_name(
2563  reg_list[i]->reg_data_type->type);
2564  }
2565  } else {
2566  /* Default type is "int" */
2567  type_str = "int";
2568  }
2569 
2570  xml_printf(&retval, &tdesc, &pos, &size,
2571  "<reg name=\"%s\"", reg_list[i]->name);
2572  xml_printf(&retval, &tdesc, &pos, &size,
2573  " bitsize=\"%" PRIu32 "\"", reg_list[i]->size);
2574  xml_printf(&retval, &tdesc, &pos, &size,
2575  " regnum=\"%" PRIu32 "\"", reg_list[i]->number);
2576  if (reg_list[i]->caller_save)
2577  xml_printf(&retval, &tdesc, &pos, &size,
2578  " save-restore=\"yes\"");
2579  else
2580  xml_printf(&retval, &tdesc, &pos, &size,
2581  " save-restore=\"no\"");
2582 
2583  xml_printf(&retval, &tdesc, &pos, &size,
2584  " type=\"%s\"", type_str);
2585 
2586  if (reg_list[i]->group)
2587  xml_printf(&retval, &tdesc, &pos, &size,
2588  " group=\"%s\"", reg_list[i]->group);
2589 
2590  xml_printf(&retval, &tdesc, &pos, &size,
2591  "/>\n");
2592  }
2593 
2594  xml_printf(&retval, &tdesc, &pos, &size,
2595  "</feature>\n");
2596 
2597  current_feature++;
2598  free(arch_defined_types);
2599  }
2600  }
2601 
2602  xml_printf(&retval, &tdesc, &pos, &size,
2603  "</target>\n");
2604 
2605 error:
2606  free(features);
2607  free(reg_list);
2608 
2609  if (retval == ERROR_OK)
2610  *tdesc_out = tdesc;
2611  else
2612  free(tdesc);
2613 
2614  return retval;
2615 }
2616 
2617 static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
2618  char **chunk, int32_t offset, uint32_t length)
2619 {
2620  if (!target_desc) {
2621  LOG_ERROR("Unable to Generate Target Description");
2622  return ERROR_FAIL;
2623  }
2624 
2625  char *tdesc = target_desc->tdesc;
2626  uint32_t tdesc_length = target_desc->tdesc_length;
2627 
2628  if (!tdesc) {
2629  int retval = gdb_generate_target_description(target, &tdesc);
2630  if (retval != ERROR_OK) {
2631  LOG_ERROR("Unable to Generate Target Description");
2632  return ERROR_FAIL;
2633  }
2634 
2635  tdesc_length = strlen(tdesc);
2636  }
2637 
2638  char transfer_type;
2639 
2640  if (length < (tdesc_length - offset))
2641  transfer_type = 'm';
2642  else
2643  transfer_type = 'l';
2644 
2645  *chunk = malloc(length + 2);
2646  if (!*chunk) {
2647  LOG_ERROR("Unable to allocate memory");
2648  return ERROR_FAIL;
2649  }
2650 
2651  (*chunk)[0] = transfer_type;
2652  if (transfer_type == 'm') {
2653  strncpy((*chunk) + 1, tdesc + offset, length);
2654  (*chunk)[1 + length] = '\0';
2655  } else {
2656  strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
2657  (*chunk)[1 + (tdesc_length - offset)] = '\0';
2658 
2659  /* After gdb-server sends out last chunk, invalidate tdesc. */
2660  free(tdesc);
2661  tdesc = NULL;
2662  tdesc_length = 0;
2663  }
2664 
2665  target_desc->tdesc = tdesc;
2666  target_desc->tdesc_length = tdesc_length;
2667 
2668  return ERROR_OK;
2669 }
2670 
2671 static int gdb_target_description_supported(struct target *target, bool *supported)
2672 {
2673  int retval = ERROR_OK;
2674  struct reg **reg_list = NULL;
2675  int reg_list_size = 0;
2676  char const **features = NULL;
2677  int feature_list_size = 0;
2678 
2679  char const *architecture = target_get_gdb_arch(target);
2680 
2681  retval = target_get_gdb_reg_list_noread(target, &reg_list,
2682  &reg_list_size, REG_CLASS_ALL);
2683  if (retval != ERROR_OK) {
2684  LOG_ERROR("get register list failed");
2685  goto error;
2686  }
2687 
2688  if (reg_list_size <= 0) {
2689  LOG_ERROR("get register list failed");
2690  retval = ERROR_FAIL;
2691  goto error;
2692  }
2693 
2694  /* Get a list of available target registers features */
2695  retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2696  if (retval != ERROR_OK) {
2697  LOG_ERROR("Can't get the registers feature list");
2698  goto error;
2699  }
2700 
2701  if (supported) {
2702  if (architecture || feature_list_size)
2703  *supported = true;
2704  else
2705  *supported = false;
2706  }
2707 
2708 error:
2709  free(features);
2710 
2711  free(reg_list);
2712 
2713  return retval;
2714 }
2715 
2716 static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
2717 {
2718  struct rtos *rtos = target->rtos;
2719  int retval = ERROR_OK;
2720  char *thread_list = NULL;
2721  int pos = 0;
2722  int size = 0;
2723 
2724  xml_printf(&retval, &thread_list, &pos, &size,
2725  "<?xml version=\"1.0\"?>\n"
2726  "<threads>\n");
2727 
2728  if (rtos) {
2729  for (int i = 0; i < rtos->thread_count; i++) {
2731 
2732  if (!thread_detail->exists)
2733  continue;
2734 
2736  xml_printf(&retval, &thread_list, &pos, &size,
2737  "<thread id=\"%" PRIx64 "\" name=\"%s\">",
2740  else
2741  xml_printf(&retval, &thread_list, &pos, &size,
2742  "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
2743 
2745  xml_printf(&retval, &thread_list, &pos, &size,
2746  "Name: %s", thread_detail->thread_name_str);
2747 
2750  xml_printf(&retval, &thread_list, &pos, &size,
2751  ", ");
2752  xml_printf(&retval, &thread_list, &pos, &size,
2753  "%s", thread_detail->extra_info_str);
2754  }
2755 
2756  xml_printf(&retval, &thread_list, &pos, &size,
2757  "</thread>\n");
2758  }
2759  }
2760 
2761  xml_printf(&retval, &thread_list, &pos, &size,
2762  "</threads>\n");
2763 
2764  if (retval == ERROR_OK)
2765  *thread_list_out = thread_list;
2766  else
2767  free(thread_list);
2768 
2769  return retval;
2770 }
2771 
2772 static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
2773  char **chunk, int32_t offset, uint32_t length)
2774 {
2775  if (!*thread_list) {
2776  int retval = gdb_generate_thread_list(target, thread_list);
2777  if (retval != ERROR_OK) {
2778  LOG_ERROR("Unable to Generate Thread List");
2779  return ERROR_FAIL;
2780  }
2781  }
2782 
2783  size_t thread_list_length = strlen(*thread_list);
2784  char transfer_type;
2785 
2786  length = MIN(length, thread_list_length - offset);
2787  if (length < (thread_list_length - offset))
2788  transfer_type = 'm';
2789  else
2790  transfer_type = 'l';
2791 
2792  *chunk = malloc(length + 2 + 3);
2793  /* Allocating extra 3 bytes prevents false positive valgrind report
2794  * of strlen(chunk) word access:
2795  * Invalid read of size 4
2796  * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2797  if (!*chunk) {
2798  LOG_ERROR("Unable to allocate memory");
2799  return ERROR_FAIL;
2800  }
2801 
2802  (*chunk)[0] = transfer_type;
2803  strncpy((*chunk) + 1, (*thread_list) + offset, length);
2804  (*chunk)[1 + length] = '\0';
2805 
2806  /* After gdb-server sends out last chunk, invalidate thread list. */
2807  if (transfer_type == 'l') {
2808  free(*thread_list);
2809  *thread_list = NULL;
2810  }
2811 
2812  return ERROR_OK;
2813 }
2814 
2816  char const *packet, int packet_size)
2817 {
2818  struct command_context *cmd_ctx = connection->cmd_ctx;
2821 
2822  if (strncmp(packet, "qRcmd,", 6) == 0) {
2823  if (packet_size > 6) {
2824  Jim_Interp *interp = cmd_ctx->interp;
2825  char *cmd;
2826  cmd = malloc((packet_size - 6) / 2 + 1);
2827  size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
2828  cmd[len] = 0;
2829 
2830  /* We want to print all debug output to GDB connection */
2833  /* some commands need to know the GDB connection, make note of current
2834  * GDB connection. */
2836 
2837  struct target *saved_target_override = cmd_ctx->current_target_override;
2838  cmd_ctx->current_target_override = NULL;
2839 
2840  struct command_context *old_context = Jim_GetAssocData(interp, "context");
2841  Jim_DeleteAssocData(interp, "context");
2842  int retval = Jim_SetAssocData(interp, "context", NULL, cmd_ctx);
2843  if (retval == JIM_OK) {
2844  retval = Jim_EvalObj(interp, Jim_NewStringObj(interp, cmd, -1));
2845  Jim_DeleteAssocData(interp, "context");
2846  }
2847  int inner_retval = Jim_SetAssocData(interp, "context", NULL, old_context);
2848  if (retval == JIM_OK)
2849  retval = inner_retval;
2850 
2851  cmd_ctx->current_target_override = saved_target_override;
2852 
2855  free(cmd);
2856  if (retval == JIM_RETURN)
2857  retval = interp->returnCode;
2858  int lenmsg;
2859  const char *cretmsg = Jim_GetString(Jim_GetResult(interp), &lenmsg);
2860  char *retmsg;
2861  if (lenmsg && cretmsg[lenmsg - 1] != '\n') {
2862  retmsg = alloc_printf("%s\n", cretmsg);
2863  lenmsg++;
2864  } else {
2865  retmsg = strdup(cretmsg);
2866  }
2867 
2869 
2870  if (!retmsg)
2872 
2873  if (retval == JIM_OK) {
2874  if (lenmsg) {
2875  char *hex_buffer = malloc(lenmsg * 2 + 1);
2876  if (!hex_buffer) {
2877  free(retmsg);
2879  }
2880 
2881  size_t pkt_len = hexify(hex_buffer, (const uint8_t *)retmsg, lenmsg,
2882  lenmsg * 2 + 1);
2883  gdb_put_packet(connection, hex_buffer, pkt_len);
2884  free(hex_buffer);
2885  } else {
2886  gdb_put_packet(connection, "OK", 2);
2887  }
2888  } else {
2889  if (lenmsg)
2890  gdb_output_con(connection, retmsg);
2891  gdb_send_error(connection, retval);
2892  }
2893  free(retmsg);
2894  return ERROR_OK;
2895  }
2896  gdb_put_packet(connection, "OK", 2);
2897  return ERROR_OK;
2898  } else if (strncmp(packet, "qCRC:", 5) == 0) {
2899  if (packet_size > 5) {
2900  int retval;
2901  char gdb_reply[10];
2902  char *separator;
2903  uint32_t checksum;
2904  target_addr_t addr = 0;
2905  uint32_t len = 0;
2906 
2907  /* skip command character */
2908  packet += 5;
2909 
2910  addr = strtoull(packet, &separator, 16);
2911 
2912  if (*separator != ',') {
2913  LOG_ERROR("incomplete read memory packet received, dropping connection");
2915  }
2916 
2917  len = strtoul(separator + 1, NULL, 16);
2918 
2920  retval = target_checksum_memory(target, addr, len, &checksum);
2922 
2923  if (retval == ERROR_OK) {
2924  snprintf(gdb_reply, 10, "C%8.8" PRIx32, checksum);
2925  gdb_put_packet(connection, gdb_reply, 9);
2926  } else {
2927  retval = gdb_error(connection, retval);
2928  if (retval != ERROR_OK)
2929  return retval;
2930  }
2931 
2932  return ERROR_OK;
2933  }
2934  } else if (strncmp(packet, "qSupported", 10) == 0) {
2935  /* we currently support packet size and qXfer:memory-map:read (if enabled)
2936  * qXfer:features:read is supported for some targets */
2937  int retval = ERROR_OK;
2938  char *buffer = NULL;
2939  int pos = 0;
2940  int size = 0;
2941  bool gdb_target_desc_supported = false;
2942 
2943  /* we need to test that the target supports target descriptions */
2944  retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
2945  if (retval != ERROR_OK) {
2946  LOG_INFO("Failed detecting Target Description Support, disabling");
2947  gdb_target_desc_supported = false;
2948  }
2949 
2950  /* support may be disabled globally */
2952  if (gdb_target_desc_supported)
2953  LOG_WARNING("Target Descriptions Supported, but disabled");
2954  gdb_target_desc_supported = false;
2955  }
2956 
2957  xml_printf(&retval,
2958  &buffer,
2959  &pos,
2960  &size,
2961  "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2963  (gdb_use_memory_map && (flash_get_bank_count() > 0)) ? '+' : '-',
2964  gdb_target_desc_supported ? '+' : '-');
2965 
2966  if (retval != ERROR_OK) {
2968  return ERROR_OK;
2969  }
2970 
2972  free(buffer);
2973 
2974  return ERROR_OK;
2975  } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
2976  && (flash_get_bank_count() > 0))
2977  return gdb_memory_map(connection, packet, packet_size);
2978  else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
2979  char *xml = NULL;
2980  int retval = ERROR_OK;
2981 
2982  int offset;
2983  unsigned int length;
2984 
2985  /* skip command character */
2986  packet += 20;
2987 
2988  if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2990  return ERROR_OK;
2991  }
2992 
2993  /* Target should prepare correct target description for annex.
2994  * The first character of returned xml is 'm' or 'l'. 'm' for
2995  * there are *more* chunks to transfer. 'l' for it is the *last*
2996  * chunk of target description.
2997  */
2999  &xml, offset, length);
3000  if (retval != ERROR_OK) {
3001  gdb_error(connection, retval);
3002  return retval;
3003  }
3004 
3005  gdb_put_packet(connection, xml, strlen(xml));
3006 
3007  free(xml);
3008  return ERROR_OK;
3009  } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
3010  char *xml = NULL;
3011  int retval = ERROR_OK;
3012 
3013  int offset;
3014  unsigned int length;
3015 
3016  /* skip command character */
3017  packet += 19;
3018 
3019  if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
3021  return ERROR_OK;
3022  }
3023 
3024  /* Target should prepare correct thread list for annex.
3025  * The first character of returned xml is 'm' or 'l'. 'm' for
3026  * there are *more* chunks to transfer. 'l' for it is the *last*
3027  * chunk of target description.
3028  */
3030  &xml, offset, length);
3031  if (retval != ERROR_OK) {
3032  gdb_error(connection, retval);
3033  return retval;
3034  }
3035 
3036  gdb_put_packet(connection, xml, strlen(xml));
3037 
3038  free(xml);
3039  return ERROR_OK;
3040  } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
3042  gdb_put_packet(connection, "OK", 2);
3043  return ERROR_OK;
3044  } else if (target->type->gdb_query_custom) {
3045  char *buffer = NULL;
3046  int ret = target->type->gdb_query_custom(target, packet, &buffer);
3048  return ret;
3049  }
3050 
3051  gdb_put_packet(connection, "", 0);
3052  return ERROR_OK;
3053 }
3054 
3055 static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet,
3056  __attribute__((unused)) int packet_size)
3057 {
3060  const char *parse = packet;
3061  int retval;
3062 
3063  /* query for vCont supported */
3064  if (parse[0] == '?') {
3065  if (target->type->step) {
3066  /* gdb doesn't accept c without C and s without S */
3067  gdb_put_packet(connection, "vCont;c;C;s;S", 13);
3068  return true;
3069  }
3070  return false;
3071  }
3072 
3073  if (parse[0] == ';') {
3074  ++parse;
3075  }
3076 
3077  /* simple case, a continue packet */
3078  if (parse[0] == 'c') {
3079  gdb_running_type = 'c';
3080  LOG_TARGET_DEBUG(target, "target continue");
3082  retval = target_resume(target, true, 0, false, false);
3083  if (retval == ERROR_TARGET_NOT_HALTED)
3084  LOG_TARGET_INFO(target, "target was not halted when resume was requested");
3085 
3086  /* poll target in an attempt to make its internal state consistent */
3087  if (retval != ERROR_OK) {
3088  retval = target_poll(target);
3089  if (retval != ERROR_OK)
3090  LOG_TARGET_DEBUG(target, "error polling target after failed resume");
3091  }
3092 
3093  /*
3094  * We don't report errors to gdb here, move frontend_state to
3095  * TARGET_RUNNING to stay in sync with gdb's expectation of the
3096  * target state
3097  */
3100 
3101  return true;
3102  }
3103 
3104  /* single-step or step-over-breakpoint */
3105  if (parse[0] == 's') {
3106  gdb_running_type = 's';
3107  bool fake_step = false;
3108 
3109  struct target *ct = target;
3110  bool current_pc = true;
3111  int64_t thread_id;
3112  parse++;
3113  if (parse[0] == ':') {
3114  char *endp;
3115  parse++;
3116  thread_id = strtoll(parse, &endp, 16);
3117  if (endp) {
3118  parse = endp;
3119  }
3120  } else {
3121  thread_id = 0;
3122  }
3123 
3124  if (target->rtos) {
3125  /* Sometimes this results in picking a different thread than
3126  * gdb just requested to step. Then we fake it, and now there's
3127  * a different thread selected than gdb expects, so register
3128  * accesses go to the wrong one!
3129  * E.g.:
3130  * Hg1$
3131  * P8=72101ce197869329$ # write r8 on thread 1
3132  * g$
3133  * vCont?$
3134  * vCont;s:1;c$ # rtos_update_threads changes to other thread
3135  * g$
3136  * qXfer:threads:read::0,fff$
3137  * P8=cc060607eb89ca7f$ # write r8 on other thread
3138  * g$
3139  */
3140  /* rtos_update_threads(target); */
3141 
3142  target->rtos->gdb_target_for_threadid(connection, thread_id, &ct);
3143 
3144  /*
3145  * check if the thread to be stepped is the current rtos thread
3146  * if not, we must fake the step
3147  */
3148  fake_step = rtos_needs_fake_step(target, thread_id);
3149  }
3150 
3151  if (parse[0] == ';') {
3152  ++parse;
3153 
3154  if (parse[0] == 'c') {
3155  parse += 1;
3156 
3157  /* check if thread-id follows */
3158  if (parse[0] == ':') {
3159  int64_t tid;
3160  parse += 1;
3161 
3162  tid = strtoll(parse, NULL, 16);
3163  if (tid == thread_id) {
3164  /*
3165  * Special case: only step a single thread (core),
3166  * keep the other threads halted. Currently, only
3167  * aarch64 target understands it. Other target types don't
3168  * care (nobody checks the actual value of 'current')
3169  * and it doesn't really matter. This deserves
3170  * a symbolic constant and a formal interface documentation
3171  * at a later time.
3172  */
3173  LOG_DEBUG("request to step current core only");
3174  /* uncomment after checking that indeed other targets are safe */
3175  /*current_pc = 2;*/
3176  }
3177  }
3178  }
3179  }
3180 
3181  LOG_TARGET_DEBUG(ct, "single-step thread %" PRIx64, thread_id);
3184 
3185  /*
3186  * work around an annoying gdb behaviour: when the current thread
3187  * is changed in gdb, it assumes that the target can follow and also
3188  * make the thread current. This is an assumption that cannot hold
3189  * for a real target running a multi-threading OS. We just fake
3190  * the step to not trigger an internal error in gdb. See
3191  * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3192  */
3193  if (fake_step) {
3194  int sig_reply_len;
3195  char sig_reply[128];
3196 
3197  LOG_DEBUG("fake step thread %"PRIx64, thread_id);
3198 
3199  sig_reply_len = snprintf(sig_reply, sizeof(sig_reply),
3200  "T05thread:%016"PRIx64";", thread_id);
3201 
3202  gdb_put_packet(connection, sig_reply, sig_reply_len);
3204 
3205  return true;
3206  }
3207 
3208  /* support for gdb_sync command */
3209  if (gdb_connection->sync) {
3210  gdb_connection->sync = false;
3211  if (ct->state == TARGET_HALTED) {
3212  LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3213  "from the target.");
3216  } else
3218  return true;
3219  }
3220 
3221  retval = target_step(ct, current_pc, 0, false);
3222  if (retval == ERROR_TARGET_NOT_HALTED)
3223  LOG_TARGET_INFO(ct, "target was not halted when step was requested");
3224 
3225  /* if step was successful send a reply back to gdb */
3226  if (retval == ERROR_OK) {
3227  retval = target_poll(ct);
3228  if (retval != ERROR_OK)
3229  LOG_TARGET_DEBUG(ct, "error polling target after successful step");
3230  /* send back signal information */
3232  /* stop forwarding log packets! */
3234  } else
3236  return true;
3237  }
3238  LOG_ERROR("Unknown vCont packet");
3239  return false;
3240 }
3241 
3242 static char *next_hex_encoded_field(const char **str, char sep)
3243 {
3244  size_t hexlen;
3245  const char *hex = *str;
3246  if (hex[0] == '\0')
3247  return NULL;
3248 
3249  const char *end = strchr(hex, sep);
3250  if (!end)
3251  hexlen = strlen(hex);
3252  else
3253  hexlen = end - hex;
3254  *str = hex + hexlen + 1;
3255 
3256  if (hexlen % 2 != 0) {
3257  /* Malformed hex data */
3258  return NULL;
3259  }
3260 
3261  size_t count = hexlen / 2;
3262  char *decoded = malloc(count + 1);
3263  if (!decoded)
3264  return NULL;
3265 
3266  size_t converted = unhexify((void *)decoded, hex, count);
3267  if (converted != count) {
3268  free(decoded);
3269  return NULL;
3270  }
3271 
3272  decoded[count] = '\0';
3273  return decoded;
3274 }
3275 
3276 /* handle extended restart packet */
3277 static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
3278 {
3279  struct gdb_connection *gdb_con = connection->priv;
3281 
3284  command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
3285  target_name(target));
3286  /* set connection as attached after reset */
3287  gdb_con->attached = true;
3288  /* info rtos parts */
3289  gdb_thread_packet(connection, packet, packet_size);
3290 }
3291 
3292 static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
3293 {
3295  const char *parse = packet;
3296 
3297  /* Skip "vRun" */
3298  parse += 4;
3299 
3300  if (parse[0] != ';')
3301  return false;
3302  parse++;
3303 
3304  /* Skip first field "filename"; don't know what to do with it. */
3305  free(next_hex_encoded_field(&parse, ';'));
3306 
3307  char *cmdline = next_hex_encoded_field(&parse, ';');
3308  while (cmdline) {
3309  char *arg = next_hex_encoded_field(&parse, ';');
3310  if (!arg)
3311  break;
3312  char *new_cmdline = alloc_printf("%s %s", cmdline, arg);
3313  free(cmdline);
3314  free(arg);
3315  cmdline = new_cmdline;
3316  }
3317 
3318  if (cmdline) {
3319  if (target->semihosting) {
3320  LOG_INFO("GDB set inferior command line to '%s'", cmdline);
3321  free(target->semihosting->cmdline);
3322  target->semihosting->cmdline = cmdline;
3323  } else {
3324  LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline);
3325  free(cmdline);
3326  }
3327  }
3328 
3329  gdb_restart_inferior(connection, packet, packet_size);
3330  gdb_put_packet(connection, "S00", 3);
3331  return true;
3332 }
3333 
3335  char const *packet, int packet_size)
3336 {
3338  int result;
3339 
3341 
3342  if (strncmp(packet, "vCont", 5) == 0) {
3343  bool handled;
3344 
3345  packet += 5;
3346  packet_size -= 5;
3347 
3348  handled = gdb_handle_vcont_packet(connection, packet, packet_size);
3349  if (!handled)
3350  gdb_put_packet(connection, "", 0);
3351 
3352  return ERROR_OK;
3353  }
3354 
3355  if (strncmp(packet, "vRun", 4) == 0) {
3356  bool handled;
3357 
3358  handled = gdb_handle_vrun_packet(connection, packet, packet_size);
3359  if (!handled)
3360  gdb_put_packet(connection, "", 0);
3361 
3362  return ERROR_OK;
3363  }
3364 
3365  /* if flash programming disabled - send a empty reply */
3366 
3367  if (!gdb_flash_program) {
3368  gdb_put_packet(connection, "", 0);
3369  return ERROR_OK;
3370  }
3371 
3372  if (strncmp(packet, "vFlashErase:", 12) == 0) {
3374  unsigned long length;
3375 
3376  char const *parse = packet + 12;
3377  if (*parse == '\0') {
3378  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3380  }
3381 
3382  addr = strtoull(parse, (char **)&parse, 16);
3383 
3384  if (*(parse++) != ',' || *parse == '\0') {
3385  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3387  }
3388 
3389  length = strtoul(parse, (char **)&parse, 16);
3390 
3391  if (*parse != '\0') {
3392  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3394  }
3395 
3396  /* assume all sectors need erasing - stops any problems
3397  * when flash_write is called multiple times */
3398  flash_set_dirty();
3399 
3400  /* perform any target specific operations before the erase */
3403 
3404  /* vFlashErase:addr,length messages require region start and
3405  * end to be "block" aligned ... if padding is ever needed,
3406  * GDB will have become dangerously confused.
3407  */
3408  result = flash_erase_address_range(target, false, addr,
3409  length);
3410 
3411  /* perform any target specific operations after the erase */
3414 
3415  /* perform erase */
3416  if (result != ERROR_OK) {
3417  /* GDB doesn't evaluate the actual error number returned,
3418  * treat a failed erase as an I/O error
3419  */
3420  gdb_send_error(connection, EIO);
3421  LOG_ERROR("flash_erase returned %i", result);
3422  } else
3423  gdb_put_packet(connection, "OK", 2);
3424 
3425  return ERROR_OK;
3426  }
3427 
3428  if (strncmp(packet, "vFlashWrite:", 12) == 0) {
3429  int retval;
3431  unsigned long length;
3432  char const *parse = packet + 12;
3433 
3434  if (*parse == '\0') {
3435  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3437  }
3438 
3439  addr = strtoull(parse, (char **)&parse, 16);
3440  if (*(parse++) != ':') {
3441  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3443  }
3444  length = packet_size - (parse - packet);
3445 
3446  /* create a new image if there isn't already one */
3447  if (!gdb_connection->vflash_image) {
3448  gdb_connection->vflash_image = malloc(sizeof(struct image));
3449  image_open(gdb_connection->vflash_image, "", "build");
3450  }
3451 
3452  /* create new section with content from packet buffer */
3454  addr, length, 0x0, (uint8_t const *)parse);
3455  if (retval != ERROR_OK)
3456  return retval;
3457 
3458  gdb_put_packet(connection, "OK", 2);
3459 
3460  return ERROR_OK;
3461  }
3462 
3463  if (strncmp(packet, "vFlashDone", 10) == 0) {
3464  uint32_t written;
3465 
3466  /* GDB command 'flash-erase' does not send a vFlashWrite,
3467  * so nothing to write here. */
3468  if (!gdb_connection->vflash_image) {
3469  gdb_put_packet(connection, "OK", 2);
3470  return ERROR_OK;
3471  }
3472 
3473  /* process the flashing buffer. No need to erase as GDB
3474  * always issues a vFlashErase first. */
3478  &written, false);
3481  if (result != ERROR_OK) {
3482  if (result == ERROR_FLASH_DST_OUT_OF_BANK)
3483  gdb_put_packet(connection, "E.memtype", 9);
3484  else
3485  gdb_send_error(connection, EIO);
3486  } else {
3487  LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
3488  gdb_put_packet(connection, "OK", 2);
3489  }
3490 
3494 
3495  return ERROR_OK;
3496  }
3497 
3498  gdb_put_packet(connection, "", 0);
3499  return ERROR_OK;
3500 }
3501 
3502 static int gdb_detach(struct connection *connection)
3503 {
3504  /*
3505  * Only reply "OK" to GDB
3506  * it will close the connection and this will trigger a call to
3507  * gdb_connection_closed() that will in turn trigger the event
3508  * TARGET_EVENT_GDB_DETACH
3509  */
3510  return gdb_put_packet(connection, "OK", 2);
3511 }
3512 
3513 /* The format of 'F' response packet is
3514  * Fretcode,errno,Ctrl-C flag;call-specific attachment
3515  */
3517  char const *packet, int packet_size)
3518 {
3520  char *separator;
3521  char *parsing_point;
3522  int fileio_retcode = strtoul(packet + 1, &separator, 16);
3523  int fileio_errno = 0;
3524  bool fileio_ctrl_c = false;
3525  int retval;
3526 
3527  LOG_DEBUG("-");
3528 
3529  if (*separator == ',') {
3530  parsing_point = separator + 1;
3531  fileio_errno = strtoul(parsing_point, &separator, 16);
3532  if (*separator == ',') {
3533  if (*(separator + 1) == 'C') {
3534  /* TODO: process ctrl-c */
3535  fileio_ctrl_c = true;
3536  }
3537  }
3538  }
3539 
3540  LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3541  fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false");
3542 
3543  retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c);
3544  if (retval != ERROR_OK)
3545  return ERROR_FAIL;
3546 
3547  /* After File-I/O ends, keep continue or step */
3548  if (gdb_running_type == 'c')
3549  retval = target_resume(target, true, 0x0, false, false);
3550  else if (gdb_running_type == 's')
3551  retval = target_step(target, true, 0x0, false);
3552  else
3553  retval = ERROR_FAIL;
3554 
3555  if (retval != ERROR_OK)
3556  return ERROR_FAIL;
3557 
3558  return ERROR_OK;
3559 }
3560 
3561 static void gdb_log_callback(void *priv, const char *file, unsigned int line,
3562  const char *function, const char *string)
3563 {
3564  struct connection *connection = priv;
3565  struct gdb_connection *gdb_con = connection->priv;
3566 
3567  if (gdb_con->output_flag != GDB_OUTPUT_ALL)
3568  /* No out allowed */
3569  return;
3570 
3571  if (gdb_con->busy) {
3572  /* do not reply this using the O packet */
3573  return;
3574  }
3575 
3576  gdb_output_con(connection, string);
3577 }
3578 
3580 {
3581  char sig_reply[4];
3582  snprintf(sig_reply, 4, "T%2.2x", 2);
3583  gdb_put_packet(connection, sig_reply, 3);
3584 }
3585 
3587 {
3588  /* Do not allocate this on the stack */
3589  static char gdb_packet_buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for null-termination */
3590 
3591  struct target *target;
3592  char const *packet = gdb_packet_buffer;
3593  int packet_size;
3594  int retval;
3595  struct gdb_connection *gdb_con = connection->priv;
3596  static bool warn_use_ext;
3597 
3599 
3600  /* drain input buffer. If one of the packets fail, then an error
3601  * packet is replied, if applicable.
3602  *
3603  * This loop will terminate and the error code is returned.
3604  *
3605  * The calling fn will check if this error is something that
3606  * can be recovered from, or if the connection must be closed.
3607  *
3608  * If the error is recoverable, this fn is called again to
3609  * drain the rest of the buffer.
3610  */
3611  do {
3612  packet_size = GDB_BUFFER_SIZE;
3613  retval = gdb_get_packet(connection, gdb_packet_buffer, &packet_size);
3614  if (retval != ERROR_OK)
3615  return retval;
3616 
3617  /* terminate with zero */
3618  gdb_packet_buffer[packet_size] = '\0';
3619 
3620  if (packet_size > 0) {
3621 
3622  gdb_log_incoming_packet(connection, gdb_packet_buffer);
3623 
3624  retval = ERROR_OK;
3625  switch (packet[0]) {
3626  case 'T': /* Is thread alive? */
3627  gdb_thread_packet(connection, packet, packet_size);
3628  break;
3629  case 'H': /* Set current thread ( 'c' for step and continue,
3630  * 'g' for all other operations ) */
3631  gdb_thread_packet(connection, packet, packet_size);
3632  break;
3633  case 'q':
3634  case 'Q':
3635  retval = gdb_thread_packet(connection, packet, packet_size);
3636  if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
3637  retval = gdb_query_packet(connection, packet, packet_size);
3638  break;
3639  case 'g':
3640  retval = gdb_get_registers_packet(connection, packet, packet_size);
3641  break;
3642  case 'G':
3643  retval = gdb_set_registers_packet(connection, packet, packet_size);
3644  break;
3645  case 'p':
3646  retval = gdb_get_register_packet(connection, packet, packet_size);
3647  break;
3648  case 'P':
3649  retval = gdb_set_register_packet(connection, packet, packet_size);
3650  break;
3651  case 'm':
3652  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3653  retval = gdb_read_memory_packet(connection, packet, packet_size);
3654  gdb_con->output_flag = GDB_OUTPUT_NO;
3655  break;
3656  case 'M':
3657  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3658  retval = gdb_write_memory_packet(connection, packet, packet_size);
3659  gdb_con->output_flag = GDB_OUTPUT_NO;
3660  break;
3661  case 'z':
3662  case 'Z':
3663  retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
3664  break;
3665  case '?':
3666  gdb_last_signal_packet(connection, packet, packet_size);
3667  /* '?' is sent after the eventual '!' */
3668  if (!warn_use_ext && !gdb_con->extended_protocol) {
3669  warn_use_ext = true;
3670  LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3672  }
3673  break;
3674  case 'c':
3675  case 's':
3676  {
3677  gdb_thread_packet(connection, packet, packet_size);
3678  gdb_con->output_flag = GDB_OUTPUT_ALL;
3679 
3680  if (gdb_con->mem_write_error) {
3681  LOG_ERROR("Memory write failure!");
3682 
3683  /* now that we have reported the memory write error,
3684  * we can clear the condition */
3685  gdb_con->mem_write_error = false;
3686  }
3687 
3688  bool nostep = false;
3689  bool already_running = false;
3690  if (target->state == TARGET_RUNNING) {
3691  LOG_WARNING("WARNING! The target is already running. "
3692  "All changes GDB did to registers will be discarded! "
3693  "Waiting for target to halt.");
3694  already_running = true;
3695  } else if (target->state != TARGET_HALTED) {
3696  LOG_WARNING("The target is not in the halted nor running stated, "
3697  "stepi/continue ignored.");
3698  nostep = true;
3699  } else if ((packet[0] == 's') && gdb_con->sync) {
3700  /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3701  * sent by GDB first to OpenOCD, thus defeating the check to
3702  * make only the single stepping have the sync feature...
3703  */
3704  nostep = true;
3705  LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3706  "from the target.");
3707  }
3708  gdb_con->sync = false;
3709 
3710  if (!already_running && nostep) {
3711  /* Either the target isn't in the halted state, then we can't
3712  * step/continue. This might be early setup, etc.
3713  *
3714  * Or we want to allow GDB to pick up a fresh set of
3715  * register values without modifying the target state.
3716  *
3717  */
3719 
3720  /* stop forwarding log packets! */
3721  gdb_con->output_flag = GDB_OUTPUT_NO;
3722  } else {
3723  /* We're running/stepping, in which case we can
3724  * forward log output until the target is halted
3725  */
3726  gdb_con->frontend_state = TARGET_RUNNING;
3728 
3729  if (!already_running) {
3730  /* Here we don't want packet processing to stop even if this fails,
3731  * so we use a local variable instead of retval. */
3732  retval = gdb_step_continue_packet(connection, packet, packet_size);
3733  if (retval != ERROR_OK) {
3734  /* we'll never receive a halted
3735  * condition... issue a false one..
3736  */
3738  }
3739  }
3740  }
3741  }
3742  break;
3743  case 'v':
3744  retval = gdb_v_packet(connection, packet, packet_size);
3745  break;
3746  case 'D':
3747  retval = gdb_detach(connection);
3748  break;
3749  case 'X':
3750  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3751  retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
3752  gdb_con->output_flag = GDB_OUTPUT_NO;
3753  break;
3754  case 'k':
3755  if (gdb_con->extended_protocol) {
3756  gdb_con->attached = false;
3757  break;
3758  }
3759  gdb_put_packet(connection, "OK", 2);
3761  case '!':
3762  /* handle extended remote protocol */
3763  gdb_con->extended_protocol = true;
3764  gdb_put_packet(connection, "OK", 2);
3765  break;
3766  case 'R':
3767  /* handle extended restart packet */
3768  gdb_restart_inferior(connection, packet, packet_size);
3769  break;
3770 
3771  case 'j':
3772  if (strncmp(packet, "jc", 2) == 0) {
3773  /* DEPRECATED */
3774  /* packet supported only by smp target i.e cortex_a.c*/
3775  /* handle smp packet replying coreid played to gbd */
3776  gdb_read_smp_packet(connection, packet, packet_size);
3777  } else {
3778  /* ignore unknown packets */
3779  LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3780  retval = gdb_put_packet(connection, "", 0);
3781  }
3782  break;
3783 
3784  case 'J':
3785  if (strncmp(packet, "jc", 2) == 0) {
3786  /* DEPRECATED */
3787  /* packet supported only by smp target i.e cortex_a.c */
3788  /* handle smp packet setting coreid to be played at next
3789  * resume to gdb */
3790  gdb_read_smp_packet(connection, packet, packet_size);
3791  } else {
3792  /* ignore unknown packets */
3793  LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3794  retval = gdb_put_packet(connection, "", 0);
3795  }
3796  break;
3797 
3798  case 'F':
3799  /* File-I/O extension */
3800  /* After gdb uses host-side syscall to complete target file
3801  * I/O, gdb sends host-side syscall return value to target
3802  * by 'F' packet.
3803  * The format of 'F' response packet is
3804  * Fretcode,errno,Ctrl-C flag;call-specific attachment
3805  */
3806  gdb_con->frontend_state = TARGET_RUNNING;
3807  gdb_con->output_flag = GDB_OUTPUT_ALL;
3808  gdb_fileio_response_packet(connection, packet, packet_size);
3809  break;
3810 
3811  default:
3812  /* ignore unknown packets */
3813  LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3814  gdb_put_packet(connection, "", 0);
3815  break;
3816  }
3817 
3818  /* if a packet handler returned an error, exit input loop */
3819  if (retval != ERROR_OK)
3820  return retval;
3821  }
3822 
3823  if (gdb_con->ctrl_c) {
3824  struct target *available_target = get_available_target_from_connection(connection);
3825  if (available_target->state == TARGET_RUNNING) {
3826  struct target *t = available_target;
3827  if (available_target->rtos)
3829  retval = target_halt(t);
3830  if (retval == ERROR_OK)
3831  retval = target_poll(t);
3832  if (retval != ERROR_OK)
3834  gdb_con->ctrl_c = false;
3835  } else {
3836  LOG_TARGET_INFO(target, "Not running when halt was requested, stopping GDB. (state=%d)",
3837  target->state);
3839  }
3840  }
3841 
3842  } while (gdb_con->buf_cnt > 0);
3843 
3844  return ERROR_OK;
3845 }
3846 
3847 static int gdb_input(struct connection *connection)
3848 {
3849  int retval = gdb_input_inner(connection);
3850  struct gdb_connection *gdb_con = connection->priv;
3851  if (retval == ERROR_SERVER_REMOTE_CLOSED)
3852  return retval;
3853 
3854  /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3855  if (gdb_con->closed)
3857 
3858  /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3859  return ERROR_OK;
3860 }
3861 
3862 /*
3863  * Send custom notification packet as keep-alive during memory read/write.
3864  *
3865  * From gdb 7.0 (released 2009-10-06) an unknown notification received during
3866  * memory read/write would be silently dropped.
3867  * Before gdb 7.0 any character, with exclusion of "+-$", would be considered
3868  * as junk and ignored.
3869  * In both cases the reception will reset the timeout counter in gdb, thus
3870  * working as a keep-alive.
3871  * Check putpkt_binary() and getpkt_sane() in gdb commit
3872  * 74531fed1f2d662debc2c209b8b3faddceb55960
3873  *
3874  * Enable remote debug in gdb with 'set debug remote 1' to either dump the junk
3875  * characters in gdb pre-7.0 and the notification from gdb 7.0.
3876  */
3878 {
3879  static unsigned char count;
3880  unsigned char checksum = 0;
3881  char buf[22];
3882 
3883  int len = sprintf(buf, "%%oocd_keepalive:%2.2x", count++);
3884  for (int i = 1; i < len; i++)
3885  checksum += buf[i];
3886  len += sprintf(buf + len, "#%2.2x", checksum);
3887 
3888 #ifdef _DEBUG_GDB_IO_
3889  LOG_DEBUG("sending packet '%s'", buf);
3890 #endif
3891 
3893  gdb_write(connection, buf, len);
3894 }
3895 
3897 {
3898  struct gdb_connection *gdb_con = connection->priv;
3899 
3900  switch (gdb_con->output_flag) {
3901  case GDB_OUTPUT_NO:
3902  /* no need for keep-alive */
3903  break;
3904  case GDB_OUTPUT_NOTIF:
3905  /* send asynchronous notification */
3907  break;
3908  case GDB_OUTPUT_ALL:
3909  /* send an empty O packet */
3911  break;
3912  default:
3913  break;
3914  }
3915 }
3916 
3917 static COMMAND_HELPER(gdb_service_info, const struct service *service)
3918 {
3919  struct gdb_service *gdb_service = service->priv;
3920 
3921  char *cmd_name = tcl_escape_alloc(CMD_CTX->interp, gdb_service->target->cmd_name);
3922  if (!cmd_name) {
3923  LOG_ERROR("Unable to escape Tcl string");
3924  return ERROR_FAIL;
3925  }
3926  command_print(cmd, " target %s", cmd_name);
3927  free(cmd_name);
3928  return ERROR_OK;
3929 }
3930 
3931 static const struct service_driver gdb_service_driver = {
3932  .name = "gdb",
3933  .new_connection_during_keep_alive_handler = NULL,
3934  .new_connection_handler = gdb_new_connection,
3935  .input_handler = gdb_input,
3936  .connection_closed_handler = gdb_connection_closed,
3937  .keep_client_alive_handler = gdb_keep_client_alive,
3938  .service_info_handler = gdb_service_info,
3939 };
3940 
3941 static int gdb_target_start(struct target *target, const char *port)
3942 {
3943  struct gdb_service *gdb_service = malloc(sizeof(struct gdb_service));
3944  if (!gdb_service) {
3945  LOG_ERROR("Out of memory");
3946  return ERROR_FAIL;
3947  }
3948 
3949  LOG_TARGET_INFO(target, "starting gdb server on %s", port);
3950 
3952  gdb_service->core[0] = -1;
3953  gdb_service->core[1] = -1;
3955 
3956  int retval = add_service(&gdb_service_driver, port,
3958  if (retval != ERROR_OK) {
3959  free(gdb_service);
3960  return retval;
3961  }
3962 
3963  /* initialize all targets gdb service with the same pointer */
3964  struct target_list *head;
3966  struct target *curr = head->target;
3967  if (curr != target)
3968  curr->gdb_service = gdb_service;
3969  }
3970 
3971  return ERROR_OK;
3972 }
3973 
3974 static int gdb_target_add_one(struct target *target)
3975 {
3976  /* one gdb instance per smp list */
3977  if ((target->smp) && (target->gdb_service))
3978  return ERROR_OK;
3979 
3980  /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3982  LOG_TARGET_DEBUG(target, "skip gdb server");
3983  return ERROR_OK;
3984  }
3985 
3986  if (target->gdb_port_override) {
3987  if (strcmp(target->gdb_port_override, "disabled") == 0) {
3988  LOG_TARGET_INFO(target, "gdb port disabled");
3989  return ERROR_OK;
3990  }
3992  }
3993 
3994  if (strcmp(gdb_port_next, "disabled") == 0) {
3995  LOG_TARGET_INFO(target, "gdb port disabled");
3996  return ERROR_OK;
3997  }
3998 
3999  int retval = gdb_target_start(target, gdb_port_next);
4000  if (retval == ERROR_OK) {
4001  /* save the port number so can be queried with
4002  * $target_name cget -gdb-port
4003  */
4005 
4006  long portnumber;
4007  /* If we can parse the port number
4008  * then we increment the port number for the next target.
4009  */
4010  char *end;
4011  portnumber = strtol(gdb_port_next, &end, 0);
4012  if (!*end) {
4013  if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
4014  free(gdb_port_next);
4015  if (portnumber) {
4016  gdb_port_next = alloc_printf("%ld", portnumber+1);
4017  } else {
4018  /* Don't increment if gdb_port is 0, since we're just
4019  * trying to allocate an unused port. */
4020  gdb_port_next = strdup("0");
4021  }
4022  }
4023  } else if (strcmp(gdb_port_next, "pipe") == 0) {
4024  free(gdb_port_next);
4025  gdb_port_next = strdup("disabled");
4026  }
4027  }
4028  return retval;
4029 }
4030 
4032 {
4033  if (!target) {
4034  LOG_WARNING("gdb services need one or more targets defined");
4035  return ERROR_OK;
4036  }
4037 
4038  while (target) {
4039  int retval = gdb_target_add_one(target);
4040  if (retval != ERROR_OK)
4041  return retval;
4042 
4043  target = target->next;
4044  }
4045 
4046  return ERROR_OK;
4047 }
4048 
4049 COMMAND_HANDLER(handle_gdb_sync_command)
4050 {
4051  if (CMD_ARGC != 0)
4053 
4054  if (!current_gdb_connection) {
4056  "gdb sync command can only be run from within gdb using \"monitor gdb sync\"");
4057  return ERROR_FAIL;
4058  }
4059 
4060  current_gdb_connection->sync = true;
4061 
4062  return ERROR_OK;
4063 }
4064 
4065 COMMAND_HANDLER(handle_gdb_port_command)
4066 {
4067  int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
4068  if (retval == ERROR_OK) {
4069  free(gdb_port_next);
4070  gdb_port_next = strdup(gdb_port);
4071  }
4072  return retval;
4073 }
4074 
4075 COMMAND_HANDLER(handle_gdb_memory_map_command)
4076 {
4077  if (CMD_ARGC != 1)
4079 
4081  return ERROR_OK;
4082 }
4083 
4084 COMMAND_HANDLER(handle_gdb_flash_program_command)
4085 {
4086  if (CMD_ARGC != 1)
4088 
4090  return ERROR_OK;
4091 }
4092 
4093 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
4094 {
4095  if (CMD_ARGC != 1)
4097 
4099  return ERROR_OK;
4100 }
4101 
4102 COMMAND_HANDLER(handle_gdb_report_register_access_error)
4103 {
4104  if (CMD_ARGC != 1)
4106 
4108  return ERROR_OK;
4109 }
4110 
4111 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
4112 {
4113  if (CMD_ARGC == 0) {
4114  /* nothing */
4115  } else if (CMD_ARGC == 1) {
4117  if (strcmp(CMD_ARGV[0], "hard") == 0)
4119  else if (strcmp(CMD_ARGV[0], "soft") == 0)
4121  else if (strcmp(CMD_ARGV[0], "disable") == 0)
4123  } else
4126  LOG_USER("force %s breakpoints",
4127  (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
4128  else
4129  LOG_USER("breakpoint type is not overridden");
4130 
4131  return ERROR_OK;
4132 }
4133 
4134 COMMAND_HANDLER(handle_gdb_target_description_command)
4135 {
4136  if (CMD_ARGC != 1)
4138 
4140  return ERROR_OK;
4141 }
4142 
4143 COMMAND_HANDLER(handle_gdb_save_tdesc_command)
4144 {
4145  char *tdesc;
4146  uint32_t tdesc_length;
4148 
4149  int retval = gdb_generate_target_description(target, &tdesc);
4150  if (retval != ERROR_OK) {
4151  LOG_ERROR("Unable to Generate Target Description");
4152  return ERROR_FAIL;
4153  }
4154 
4155  tdesc_length = strlen(tdesc);
4156 
4157  struct fileio *fileio;
4158  size_t size_written;
4159 
4160  char *tdesc_filename = alloc_printf("%s.xml", target_type_name(target));
4161  if (!tdesc_filename) {
4162  retval = ERROR_FAIL;
4163  goto out;
4164  }
4165 
4166  retval = fileio_open(&fileio, tdesc_filename, FILEIO_WRITE, FILEIO_TEXT);
4167 
4168  if (retval != ERROR_OK) {
4169  LOG_ERROR("Can't open %s for writing", tdesc_filename);
4170  goto out;
4171  }
4172 
4173  retval = fileio_write(fileio, tdesc_length, tdesc, &size_written);
4174 
4176 
4177  if (retval != ERROR_OK)
4178  LOG_ERROR("Error while writing the tdesc file");
4179 
4180 out:
4181  free(tdesc_filename);
4182  free(tdesc);
4183 
4184  return retval;
4185 }
4186 
4187 static const struct command_registration gdb_subcommand_handlers[] = {
4188  {
4189  .name = "sync",
4190  .handler = handle_gdb_sync_command,
4191  .mode = COMMAND_ANY,
4192  .help = "next stepi will return immediately allowing "
4193  "GDB to fetch register state without affecting "
4194  "target state",
4195  .usage = ""
4196  },
4197  {
4198  .name = "port",
4199  .handler = handle_gdb_port_command,
4200  .mode = COMMAND_CONFIG,
4201  .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4202  "server listens for the next port number after the "
4203  "base port number specified. "
4204  "No arguments reports GDB port. \"pipe\" means listen to stdin "
4205  "output to stdout, an integer is base port number, \"disabled\" disables "
4206  "port. Any other string is are interpreted as named pipe to listen to. "
4207  "Output pipe is the same name as input pipe, but with 'o' appended.",
4208  .usage = "[port_num]",
4209  },
4210  {
4211  .name = "memory_map",
4212  .handler = handle_gdb_memory_map_command,
4213  .mode = COMMAND_CONFIG,
4214  .help = "enable or disable memory map",
4215  .usage = "('enable'|'disable')"
4216  },
4217  {
4218  .name = "flash_program",
4219  .handler = handle_gdb_flash_program_command,
4220  .mode = COMMAND_CONFIG,
4221  .help = "enable or disable flash program",
4222  .usage = "('enable'|'disable')"
4223  },
4224  {
4225  .name = "report_data_abort",
4226  .handler = handle_gdb_report_data_abort_command,
4227  .mode = COMMAND_CONFIG,
4228  .help = "enable or disable reporting data aborts",
4229  .usage = "('enable'|'disable')"
4230  },
4231  {
4232  .name = "report_register_access_error",
4233  .handler = handle_gdb_report_register_access_error,
4234  .mode = COMMAND_CONFIG,
4235  .help = "enable or disable reporting register access errors",
4236  .usage = "('enable'|'disable')"
4237  },
4238  {
4239  .name = "breakpoint_override",
4240  .handler = handle_gdb_breakpoint_override_command,
4241  .mode = COMMAND_ANY,
4242  .help = "Display or specify type of breakpoint "
4243  "to be used by gdb 'break' commands.",
4244  .usage = "('hard'|'soft'|'disable')"
4245  },
4246  {
4247  .name = "target_description",
4248  .handler = handle_gdb_target_description_command,
4249  .mode = COMMAND_CONFIG,
4250  .help = "enable or disable target description",
4251  .usage = "('enable'|'disable')"
4252  },
4253  {
4254  .name = "save_tdesc",
4255  .handler = handle_gdb_save_tdesc_command,
4256  .mode = COMMAND_EXEC,
4257  .help = "Save the target description file",
4258  .usage = "",
4259  },
4261 };
4262 
4263 static const struct command_registration gdb_command_handlers[] = {
4264  {
4265  .name = "gdb",
4266  .mode = COMMAND_ANY,
4267  .help = "GDB commands",
4268  .chain = gdb_subcommand_handlers,
4269  .usage = "",
4270  },
4272 };
4273 
4275 {
4276  gdb_port = strdup("3333");
4277  gdb_port_next = strdup("3333");
4278  return register_commands(cmd_ctx, NULL, gdb_command_handlers);
4279 }
4280 
4282 {
4283  free(gdb_port);
4284  free(gdb_port_next);
4285 }
4286 
4288 {
4289  return gdb_actual_connections;
4290 }
const char * group
Definition: armv4_5.c:366
const char * name
Definition: armv4_5.c:75
const char * feature
Definition: armv4_5.c:367
struct reg_data_type * data_type
Definition: armv7m.c:105
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t length)
Convert binary data into a string of hexadecimal pairs.
Definition: binarybuffer.c:380
size_t unhexify(uint8_t *bin, const char *hex, size_t count)
Convert a string of hexadecimal pairs into its binary representation.
Definition: binarybuffer.c:342
int watchpoint_add(struct target *target, target_addr_t address, unsigned int length, enum watchpoint_rw rw, uint64_t value, uint64_t mask)
Definition: breakpoints.c:551
int breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:346
int watchpoint_hit(struct target *target, enum watchpoint_rw *rw, target_addr_t *address)
Definition: breakpoints.c:627
int watchpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:588
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:216
int watchpoint_remove_all(struct target *target)
Definition: breakpoints.c:467
int breakpoint_remove_all(struct target *target)
Definition: breakpoints.c:462
breakpoint_type
Definition: breakpoints.h:17
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
#define WATCHPOINT_IGNORE_DATA_VALUE_MASK
Definition: breakpoints.h:39
watchpoint_rw
Definition: breakpoints.h:22
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
int command_run_linef(struct command_context *context, const char *format,...)
Definition: command.c:553
void command_set_output_handler(struct command_context *context, command_output_handler_t output_handler, void *priv)
Definition: command.c:568
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define PRINTF_ATTRIBUTE_FORMAT
Definition: command.h:27
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
int parse_long(const char *str, long *ul)
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:536
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint32_t sector_size
Sector size.
Definition: dw-spi-helper.h:1
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
enum esirisc_reg_num number
Definition: esirisc.c:87
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
uint8_t length
Definition: esp_usb_jtag.c:1
#define ERROR_FLASH_DST_OUT_OF_BANK
Definition: flash/common.h:31
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
unsigned int flash_get_bank_count(void)
int flash_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length)
Erases length bytes in the target flash, starting at addr.
int flash_write(struct target *target, struct image *image, uint32_t *written, bool erase)
Writes image into the target flash.
int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
Returns the flash bank like get_flash_bank_by_name(), without probing.
static int gdb_read_memory_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1549
static void gdb_fileio_reply(struct target *target, struct connection *connection)
Definition: gdb_server.c:887
static void gdb_signal_reply(struct target *target, struct connection *connection)
Definition: gdb_server.c:815
struct target * get_available_target_from_connection(struct connection *connection)
Definition: gdb_server.c:161
static int gdb_get_char_inner(struct connection *connection, int *next_char)
Definition: gdb_server.c:241
static int gdb_target_start(struct target *target, const char *port)
Definition: gdb_server.c:3941
static int gdb_output_con(struct connection *connection, const char *line)
Definition: gdb_server.c:788
static void gdb_async_notif(struct connection *connection)
Definition: gdb_server.c:3877
static int gdb_v_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:3334
static void gdb_log_incoming_packet(struct connection *connection, const char *packet)
Definition: gdb_server.c:380
static char * gdb_port
Definition: gdb_server.c:119
static const struct service_driver gdb_service_driver
Definition: gdb_server.c:3931
int gdb_put_packet(struct connection *connection, const char *buffer, int len)
Definition: gdb_server.c:568
static int gdb_input_inner(struct connection *connection)
Definition: gdb_server.c:3586
gdb_output_flag
Definition: gdb_server.c:56
@ GDB_OUTPUT_NO
Definition: gdb_server.c:58
@ GDB_OUTPUT_NOTIF
Definition: gdb_server.c:60
@ GDB_OUTPUT_ALL
Definition: gdb_server.c:62
static int gdb_get_registers_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1286
static int gdb_target_add_one(struct target *target)
Definition: gdb_server.c:3974
static void gdb_sig_halted(struct connection *connection)
Definition: gdb_server.c:3579
static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
Definition: gdb_server.c:3292
static int gdb_reg_pos(struct target *target, int pos, int len)
Definition: gdb_server.c:1208
static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
Definition: gdb_server.c:2716
static struct gdb_connection * current_gdb_connection
Definition: gdb_server.c:113
COMMAND_HANDLER(handle_gdb_sync_command)
Definition: gdb_server.c:4049
static int gdb_detach(struct connection *connection)
Definition: gdb_server.c:3502
static int compare_bank(const void *a, const void *b)
Definition: gdb_server.c:1942
#define CTRL(c)
Definition: gdb_server.c:54
int gdb_register_commands(struct command_context *cmd_ctx)
Definition: gdb_server.c:4274
static void gdb_keep_client_alive(struct connection *connection)
Definition: gdb_server.c:3896
static int gdb_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
Definition: gdb_server.c:1000
static char gdb_running_type
Definition: gdb_server.c:153
static int gdb_get_reg_value_as_str(struct target *target, char *tstr, struct reg *reg)
Definition: gdb_server.c:1264
static int gdb_query_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:2815
static int gdb_get_thread_list_chunk(struct target *target, char **thread_list, char **chunk, int32_t offset, uint32_t length)
Definition: gdb_server.c:2772
static char * next_hex_encoded_field(const char **str, char sep)
Definition: gdb_server.c:3242
static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
Definition: gdb_server.c:3277
static COMMAND_HELPER(gdb_service_info, const struct service *service)
Definition: gdb_server.c:3917
int gdb_target_add_all(struct target *target)
Definition: gdb_server.c:4031
static int gdb_set_registers_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1354
static int gdb_generate_reg_type_description(struct target *target, char **tdesc, int *pos, int *size, struct reg_data_type *type, char const **arch_defined_types_list[], int *num_arch_defined_types)
Definition: gdb_server.c:2188
static void gdb_log_outgoing_packet(struct connection *connection, const char *packet_buf, unsigned int packet_len, unsigned char checksum)
Definition: gdb_server.c:413
static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
Definition: gdb_server.c:1915
static int gdb_fileio_response_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:3516
static int gdb_memory_map(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1956
static int gdb_get_packet(struct connection *connection, char *buffer, int *len)
Definition: gdb_server.c:779
static int check_pending(struct connection *connection, int timeout_s, int *got_data)
Definition: gdb_server.c:204
static int gdb_error(struct connection *connection, int retval)
Definition: gdb_server.c:1542
static int gdb_put_packet_inner(struct connection *connection, const char *buffer, int len)
Definition: gdb_server.c:443
static int gdb_actual_connections
Definition: gdb_server.c:129
static void gdb_frontend_halted(struct target *target, struct connection *connection)
Definition: gdb_server.c:975
static int gdb_connection_closed(struct connection *connection)
Definition: gdb_server.c:1138
static const struct command_registration gdb_command_handlers[]
Definition: gdb_server.c:4263
static int gdb_input(struct connection *connection)
Definition: gdb_server.c:3847
static int gdb_new_connection(struct connection *connection)
Definition: gdb_server.c:1023
static int gdb_get_char_fast(struct connection *connection, int *next_char, char **buf_p, int *buf_cnt)
The cool thing about this fn is that it allows buf_p and buf_cnt to be held in registers in the inner...
Definition: gdb_server.c:312
static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size, struct reg **reg_list, int reg_list_size)
Definition: gdb_server.c:2322
static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc, char **chunk, int32_t offset, uint32_t length)
Definition: gdb_server.c:2617
static int gdb_get_register_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1417
int gdb_get_actual_connections(void)
Definition: gdb_server.c:4287
static char * gdb_port_next
Definition: gdb_server.c:120
static int gdb_write_memory_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1624
static void gdb_log_outgoing_async_notif(struct connection *connection, const char *buf, unsigned int len)
Definition: gdb_server.c:430
static __attribute__((format(PRINTF_ATTRIBUTE_FORMAT, 5, 6)))
Definition: gdb_server.c:1879
static int gdb_get_char(struct connection *connection, int *next_char)
Definition: gdb_server.c:342
void gdb_service_free(void)
Definition: gdb_server.c:4281
static bool gdb_use_target_description
Definition: gdb_server.c:150
static int gdb_write(struct connection *connection, const void *data, int len)
Definition: gdb_server.c:364
static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet, __attribute__((unused)) int packet_size)
Definition: gdb_server.c:3055
static int gdb_putback_char(struct connection *connection, int last_char)
Definition: gdb_server.c:348
static enum breakpoint_type gdb_breakpoint_override_type
Definition: gdb_server.c:116
static int gdb_write_memory_binary_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1675
static int gdb_breakpoint_override
Definition: gdb_server.c:115
static int smp_reg_list_noread(struct target *target, struct reg **combined_list[], int *combined_list_size, enum target_register_class reg_class)
Definition: gdb_server.c:2365
static int gdb_report_data_abort
Definition: gdb_server.c:142
static int gdb_target_description_supported(struct target *target, bool *supported)
Definition: gdb_server.c:2671
static int gdb_report_register_access_error
Definition: gdb_server.c:145
static int gdb_generate_target_description(struct target *target, char **tdesc_out)
Definition: gdb_server.c:2477
static void gdb_str_to_target(struct target *target, char *tstr, struct reg *reg)
Definition: gdb_server.c:1225
static int gdb_last_signal(struct target *target)
Definition: gdb_server.c:178
static bool gdb_flash_program
Definition: gdb_server.c:136
static void gdb_send_error(struct connection *connection, uint8_t the_error)
Definition: gdb_server.c:1178
static int gdb_get_packet_inner(struct connection *connection, char *buffer, int *len)
Definition: gdb_server.c:696
static bool gdb_use_memory_map
Definition: gdb_server.c:134
static int gdb_last_signal_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1185
static int fetch_packet(struct connection *connection, int *checksum_ok, int noack, int *len, char *buffer)
Definition: gdb_server.c:581
static const char * gdb_get_reg_type_name(enum reg_type type)
Definition: gdb_server.c:2118
static int gdb_output(struct command_context *context, const char *line)
Definition: gdb_server.c:808
static void gdb_log_callback(void *priv, const char *file, unsigned int line, const char *function, const char *string)
Definition: gdb_server.c:3561
static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id, int *num_arch_defined_types)
Definition: gdb_server.c:2164
static int gdb_step_continue_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1754
static int gdb_breakpoint_watchpoint_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1782
static int gdb_set_register_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1468
static const struct command_registration gdb_subcommand_handlers[]
Definition: gdb_server.c:4187
static void gdb_target_to_reg(struct target *target, char const *tstr, int str_len, uint8_t *bin)
Definition: gdb_server.c:1242
#define ERROR_GDB_BUFFER_TOO_SMALL
Definition: gdb_server.h:41
#define ERROR_GDB_TIMEOUT
Definition: gdb_server.h:42
#define GDB_BUFFER_SIZE
Definition: gdb_server.h:25
static struct target * get_target_from_connection(struct connection *connection)
Definition: gdb_server.h:35
int fileio_write(struct fileio *fileio, size_t size, const void *buffer, size_t *size_written)
int fileio_close(struct fileio *fileio)
int fileio_open(struct fileio **fileio, const char *url, enum fileio_access access_type, enum fileio_type type)
@ FILEIO_WRITE
Definition: helper/fileio.h:29
@ FILEIO_TEXT
Definition: helper/fileio.h:22
void image_close(struct image *image)
Definition: image.c:1210
int image_add_section(struct image *image, target_addr_t base, uint32_t size, uint64_t flags, uint8_t const *data)
Definition: image.c:1173
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:956
The JTAG interface can be implemented with a software or hardware fifo.
int log_remove_callback(log_callback_fn fn, void *priv)
Definition: log.c:341
void log_printf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format,...)
Definition: log.c:201
int log_add_callback(log_callback_fn fn, void *priv)
Definition: log.c:316
static int64_t start
Definition: log.c:38
void log_socket_error(const char *socket_desc)
Definition: log.c:506
void kept_alive(void)
Definition: log.c:465
const char * find_nonprint_char(const char *buf, unsigned int buf_len)
Find the first non-printable character in the char buffer, return a pointer to it.
Definition: log.c:530
char * alloc_printf(const char *format,...)
Definition: log.c:386
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:167
#define LOG_USER(expr ...)
Definition: log.h:150
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:173
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:164
#define LOG_USER_N(expr ...)
Definition: log.h:153
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_LEVEL_IS(FOO)
Definition: log.h:112
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_INFO
Definition: log.h:54
@ LOG_LVL_DEBUG
Definition: log.h:55
Upper level NOR flash interfaces.
void flash_set_dirty(void)
Forces targets to re-examine their erase/protection state.
reg_type
Definition: register.h:19
@ REG_TYPE_INT
Definition: register.h:21
@ REG_TYPE_UINT16
Definition: register.h:29
@ REG_TYPE_BOOL
Definition: register.h:20
@ REG_TYPE_IEEE_DOUBLE
Definition: register.h:37
@ REG_TYPE_INT64
Definition: register.h:25
@ REG_TYPE_INT16
Definition: register.h:23
@ REG_TYPE_UINT32
Definition: register.h:30
@ REG_TYPE_CODE_PTR
Definition: register.h:33
@ REG_TYPE_DATA_PTR
Definition: register.h:34
@ REG_TYPE_INT32
Definition: register.h:24
@ REG_TYPE_INT128
Definition: register.h:26
@ REG_TYPE_UINT128
Definition: register.h:32
@ REG_TYPE_UINT
Definition: register.h:27
@ REG_TYPE_FLOAT
Definition: register.h:35
@ REG_TYPE_UINT64
Definition: register.h:31
@ REG_TYPE_INT8
Definition: register.h:22
@ REG_TYPE_ARCH_DEFINED
Definition: register.h:38
@ REG_TYPE_IEEE_SINGLE
Definition: register.h:36
@ REG_TYPE_UINT8
Definition: register.h:28
@ REG_TYPE_CLASS_VECTOR
Definition: register.h:93
@ REG_TYPE_CLASS_FLAGS
Definition: register.h:96
@ REG_TYPE_CLASS_UNION
Definition: register.h:94
@ REG_TYPE_CLASS_STRUCT
Definition: register.h:95
char * strndup(const char *s, size_t n)
Definition: replacements.c:71
static int socket_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
Definition: replacements.h:214
#define MIN(a, b)
Definition: replacements.h:22
static int read_socket(int handle, void *buffer, unsigned int count)
Definition: replacements.h:174
#define OCD_FD_SET(fd, set)
Definition: replacements.h:159
#define OCD_FD_ISSET(fd, set)
Definition: replacements.h:161
struct target * rtos_swbp_target(struct target *target, target_addr_t address, uint32_t length, enum breakpoint_type type)
Definition: rtos.c:781
int gdb_thread_packet(struct connection *connection, char const *packet, int packet_size)
Definition: rtos.c:150
int rtos_set_reg(struct connection *connection, int reg_num, uint8_t *reg_value)
Definition: rtos.c:641
int rtos_get_gdb_reg_list(struct connection *connection)
Return a list of general registers.
Definition: rtos.c:608
int rtos_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: rtos.c:766
int rtos_update_threads(struct target *target)
Definition: rtos.c:732
int rtos_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: rtos.c:758
bool rtos_needs_fake_step(struct target *target, int64_t thread_id)
Definition: rtos.c:774
struct rtos * rtos_from_target(struct target *target)
Get the RTOS from the target itself, or from one of the targets in the same SMP node,...
Definition: rtos.c:719
int rtos_get_gdb_reg(struct connection *connection, int reg_num)
Look through all registers to find this register.
Definition: rtos.c:551
#define GDB_THREAD_PACKET_NOT_CONSUMED
Definition: rtos.h:129
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct target * target
Definition: rtt/rtt.c:26
int connection_write(struct connection *connection, const void *data, int len)
Definition: server.c:772
int add_service(const struct service_driver *driver, const char *port, int max_connections, void *priv)
Definition: server.c:211
#define ERROR_SERVER_REMOTE_CLOSED
Definition: server.h:132
@ CONNECTION_TCP
Definition: server.h:29
int gdb_read_smp_packet(struct connection *connection, char const *packet, int packet_size)
Definition: smp.c:48
#define foreach_smp_target(pos, head)
Definition: smp.h:15
Jim_Interp * interp
Definition: command.h:53
struct target * current_target_override
Definition: command.h:57
struct target * current_target
Definition: command.h:55
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
struct command_context * cmd_ctx
Definition: server.h:40
void * priv
Definition: server.h:43
int fd
Definition: server.h:37
struct service * service
Definition: server.h:41
bool input_pending
Definition: server.h:42
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
Definition: nor/core.h:118
target_addr_t base
The base address of this bank.
Definition: nor/core.h:84
uint32_t size
The size of this chip bank, in bytes.
Definition: nor/core.h:85
unsigned int num_sectors
The number of sectors on this chip.
Definition: nor/core.h:116
bool read_only
a ROM region - mainly to list in gdb memory map
Definition: nor/core.h:87
struct target * target
Target to which this bank belongs.
Definition: nor/core.h:78
char * name
Definition: nor/core.h:76
uint32_t offset
Bus offset from start of the flash chip (in bytes).
Definition: nor/core.h:30
uint32_t size
Number of bytes in this flash sector.
Definition: nor/core.h:32
enum gdb_output_flag output_flag
Definition: gdb_server.c:104
enum target_state frontend_state
Definition: gdb_server.c:76
char * thread_list
Definition: gdb_server.c:102
unsigned int unique_index
Definition: gdb_server.c:106
struct target_desc_format target_desc
Definition: gdb_server.c:100
struct image * vflash_image
Definition: gdb_server.c:77
char * buf_p
Definition: gdb_server.c:73
bool mem_write_error
Definition: gdb_server.c:91
char buffer[GDB_BUFFER_SIZE+1]
Definition: gdb_server.c:72
bool extended_protocol
Definition: gdb_server.c:98
uint64_t param_1
Definition: target.h:232
uint64_t param_4
Definition: target.h:235
uint64_t param_3
Definition: target.h:234
char * identifier
Definition: target.h:231
uint64_t param_2
Definition: target.h:233
int32_t core[2]
Definition: target.h:103
struct target * target
Definition: target.h:98
Definition: image.h:48
int(* get)(struct reg *reg)
Definition: register.h:152
int(* set)(struct reg *reg, uint8_t *buf)
Definition: register.h:153
enum reg_type type
Definition: register.h:63
struct reg_data_type_flags_field * next
Definition: register.h:84
struct reg_data_type_bitfield * bitfield
Definition: register.h:83
struct reg_data_type * type
Definition: register.h:71
struct reg_data_type_bitfield * bitfield
Definition: register.h:70
struct reg_data_type_struct_field * next
Definition: register.h:73
struct reg_data_type * type
Definition: register.h:52
struct reg_data_type_union_field * next
Definition: register.h:53
enum reg_type type
Definition: register.h:100
const char * id
Definition: register.h:101
const char * name
Definition: register.h:42
Definition: register.h:111
bool caller_save
Definition: register.h:119
bool valid
Definition: register.h:126
bool exist
Definition: register.h:128
uint32_t size
Definition: register.h:132
uint8_t * value
Definition: register.h:122
struct reg_feature * feature
Definition: register.h:117
struct reg_data_type * reg_data_type
Definition: register.h:135
bool hidden
Definition: register.h:130
const struct reg_arch_type * type
Definition: register.h:141
const char * name
Definition: register.h:113
int(* clean)(struct target *target)
Definition: rtos.h:73
Definition: rtos.h:36
const struct rtos_type * type
Definition: rtos.h:37
int thread_count
Definition: rtos.h:47
struct thread_detail * thread_details
Definition: rtos.h:46
int(* gdb_target_for_threadid)(struct connection *connection, int64_t thread_id, struct target **p_target)
Definition: rtos.h:49
threadid_t current_thread
Definition: rtos.h:45
int64_t current_threadid
Definition: rtos.h:43
char * cmdline
The semihosting command line to be passed to the target.
const char * name
the name of the server
Definition: server.h:49
Definition: server.h:70
void * priv
Definition: server.h:91
char * port
The 'port', which can be an integer for TCP, or 'disabled', 'pipe' (stdin/out) or a FIFO path.
Definition: server.h:77
enum connection_type type
Definition: server.h:72
uint32_t tdesc_length
Definition: gdb_server.c:67
struct target * target
Definition: target.h:227
int(* step)(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Definition: target_type.h:48
int(* gdb_query_custom)(struct target *target, const char *packet, char **response_p)
Definition: target_type.h:299
Definition: target.h:119
struct semihosting * semihosting
Definition: target.h:222
struct gdb_service * gdb_service
Definition: target.h:212
enum target_debug_reason debug_reason
Definition: target.h:164
enum target_state state
Definition: target.h:167
char * gdb_port_override
Definition: target.h:217
enum target_endianness endianness
Definition: target.h:165
struct list_head * smp_targets
Definition: target.h:201
struct rtos * rtos
Definition: target.h:193
struct gdb_fileio_info * fileio_info
Definition: target.h:215
unsigned int smp
Definition: target.h:200
struct target_type * type
Definition: target.h:120
int gdb_max_connections
Definition: target.h:219
char * cmd_name
Definition: target.h:121
struct target * next
Definition: target.h:176
char * extra_info_str
Definition: rtos.h:33
char * thread_name_str
Definition: rtos.h:32
bool exists
Definition: rtos.h:31
threadid_t threadid
Definition: rtos.h:30
long tv_sec
Definition: replacements.h:46
long tv_usec
Definition: replacements.h:47
int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
Obtain file-I/O information from target for GDB to do syscall.
Definition: target.c:1456
struct target * all_targets
Definition: target.c:117
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1816
int target_unregister_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1726
int target_register_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1623
int target_halt(struct target *target)
Definition: target.c:518
int target_get_gdb_reg_list_noread(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Obtain the registers for GDB, but don't read register values from the target.
Definition: target.c:1415
bool target_supports_gdb_connection(const struct target *target)
Check if target allows GDB connections.
Definition: target.c:1426
int target_call_timer_callbacks_now(void)
Invoke this to ensure that e.g.
Definition: target.c:1936
int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
Definition: target.c:2530
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2405
target_addr_t target_address_max(struct target *target)
Return the highest accessible address for this target.
Definition: target.c:1474
int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
Pass GDB file-I/O response to target after finishing host syscall.
Definition: target.c:1465
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2470
int target_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size, enum target_register_class reg_class)
Obtain the registers for GDB.
Definition: target.c:1392
const char * target_debug_reason_str(enum target_debug_reason reason)
Definition: target.c:6880
const char * target_state_name(const struct target *t)
Return the name of this targets current state.
Definition: target.c:271
int target_poll(struct target *target)
Definition: target.c:488
int target_resume(struct target *target, bool current, target_addr_t address, bool handle_breakpoints, bool debug_execution)
Make the target (re)start executing using its saved execution context (possibly with some modificatio...
Definition: target.c:567
const char * target_get_gdb_arch(const struct target *target)
Obtain the architecture for GDB.
Definition: target.c:1385
int target_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Step the target.
Definition: target.c:1435
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:469
const char * target_type_name(const struct target *target)
Get the target type name.
Definition: target.c:750
@ DBG_REASON_WPTANDBKPT
Definition: target.h:75
@ DBG_REASON_EXIT
Definition: target.h:78
@ DBG_REASON_NOTHALTED
Definition: target.h:77
@ DBG_REASON_DBGRQ
Definition: target.h:72
@ DBG_REASON_SINGLESTEP
Definition: target.h:76
@ DBG_REASON_WATCHPOINT
Definition: target.h:74
@ DBG_REASON_EXC_CATCH
Definition: target.h:79
@ DBG_REASON_BREAKPOINT
Definition: target.h:73
target_register_class
Definition: target.h:113
@ REG_CLASS_GENERAL
Definition: target.h:115
@ REG_CLASS_ALL
Definition: target.h:114
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:817
static bool target_was_examined(const struct target *target)
Definition: target.h:443
target_event
Definition: target.h:253
@ TARGET_EVENT_GDB_FLASH_WRITE_END
Definition: target.h:297
@ TARGET_EVENT_HALTED
Definition: target.h:265
@ TARGET_EVENT_GDB_START
Definition: target.h:272
@ TARGET_EVENT_GDB_END
Definition: target.h:273
@ TARGET_EVENT_GDB_FLASH_ERASE_START
Definition: target.h:294
@ TARGET_EVENT_GDB_FLASH_WRITE_START
Definition: target.h:296
@ TARGET_EVENT_GDB_ATTACH
Definition: target.h:291
@ TARGET_EVENT_GDB_FLASH_ERASE_END
Definition: target.h:295
@ TARGET_EVENT_GDB_DETACH
Definition: target.h:292
@ TARGET_EVENT_GDB_HALT
Definition: target.h:264
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:246
target_state
Definition: target.h:55
@ TARGET_UNAVAILABLE
Definition: target.h:61
@ TARGET_HALTED
Definition: target.h:58
@ TARGET_RUNNING
Definition: target.h:57
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:824
@ TARGET_LITTLE_ENDIAN
Definition: target.h:85
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:821
int delete_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
char * tcl_escape_alloc(Jim_Interp *interp, const char *s)
Convert a C string to a string that can be used for Tcl list.
Definition: tcl-libjim.c:25
#define TARGET_ADDR_FMT
Definition: types.h:286
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
uint64_t target_addr_t
Definition: types.h:279
#define TARGET_PRIxADDR
Definition: types.h:284
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22