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