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