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 static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
2025 {
2026  /* Locate the annex. */
2027  const char *annex_end = strchr(buf, ':');
2028  if (!annex_end)
2029  return ERROR_FAIL;
2030 
2031  /* After the read marker and annex, qXfer looks like a
2032  * traditional 'm' packet. */
2033  char *separator;
2034  *ofs = strtoul(annex_end + 1, &separator, 16);
2035 
2036  if (*separator != ',')
2037  return ERROR_FAIL;
2038 
2039  *len = strtoul(separator + 1, NULL, 16);
2040 
2041  /* Extract the annex if needed */
2042  if (annex) {
2043  *annex = strndup(buf, annex_end - buf);
2044  if (!*annex)
2045  return ERROR_FAIL;
2046  }
2047 
2048  return ERROR_OK;
2049 }
2050 
2051 static int compare_bank(const void *a, const void *b)
2052 {
2053  struct flash_bank *b1, *b2;
2054  b1 = *((struct flash_bank **)a);
2055  b2 = *((struct flash_bank **)b);
2056 
2057  if (b1->base == b2->base)
2058  return 0;
2059  else if (b1->base > b2->base)
2060  return 1;
2061  else
2062  return -1;
2063 }
2064 
2066  char const *packet, int packet_size)
2067 {
2068  /* We get away with only specifying flash here. Regions that are not
2069  * specified are treated as if we provided no memory map(if not we
2070  * could detect the holes and mark them as RAM).
2071  * Normally we only execute this code once, but no big deal if we
2072  * have to regenerate it a couple of times.
2073  */
2074 
2076  struct flash_bank *p;
2077  char *xml = NULL;
2078  int size = 0;
2079  int pos = 0;
2080  int retval = ERROR_OK;
2081  struct flash_bank **banks;
2082  int offset;
2083  int length;
2084  char *separator;
2085  target_addr_t ram_start = 0;
2086  unsigned int target_flash_banks = 0;
2087 
2088  /* skip command character */
2089  packet += 23;
2090 
2091  offset = strtoul(packet, &separator, 16);
2092  length = strtoul(separator + 1, &separator, 16);
2093 
2094  xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
2095 
2096  /* Sort banks in ascending order. We need to report non-flash
2097  * memory as ram (or rather read/write) by default for GDB, since
2098  * it has no concept of non-cacheable read/write memory (i/o etc).
2099  */
2100  banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
2101 
2102  for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
2104  if (p->target != target)
2105  continue;
2106  retval = get_flash_bank_by_num(i, &p);
2107  if (retval != ERROR_OK) {
2108  free(banks);
2109  gdb_error(connection, retval);
2110  return retval;
2111  }
2112  banks[target_flash_banks++] = p;
2113  }
2114 
2115  qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
2116  compare_bank);
2117 
2118  for (unsigned int i = 0; i < target_flash_banks; i++) {
2119  unsigned int sector_size = 0;
2120  unsigned int group_len = 0;
2121 
2122  p = banks[i];
2123 
2124  if (ram_start < p->base)
2125  xml_printf(&retval, &xml, &pos, &size,
2126  "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
2127  "length=\"" TARGET_ADDR_FMT "\"/>\n",
2128  ram_start, p->base - ram_start);
2129 
2130  if (p->read_only) {
2131  xml_printf(&retval, &xml, &pos, &size,
2132  "<memory type=\"rom\" start=\"" TARGET_ADDR_FMT "\" "
2133  "length=\"0x%x\"/>\n",
2134  p->base, p->size);
2135  } else {
2136  if (p->num_sectors == 0) {
2137  xml_printf(&retval, &xml, &pos, &size,
2138  "<memory type=\"flash\" "
2139  "start=\"" TARGET_ADDR_FMT "\" "
2140  "length=\"0x%x\">"
2141  "<property name=\"blocksize\">0x%x</property>\n"
2142  "</memory>\n", p->base, p->size, p->size);
2143  }
2144 
2145  /* Report adjacent groups of same-size sectors. So for
2146  * example top boot CFI flash will list an initial region
2147  * with several large sectors (maybe 128KB) and several
2148  * smaller ones at the end (maybe 32KB). STR7 will have
2149  * regions with 8KB, 32KB, and 64KB sectors; etc.
2150  */
2151  for (unsigned int j = 0; j < p->num_sectors; j++) {
2152  // Maybe start a new group of sectors
2153  if (sector_size == 0) {
2154  if (p->sectors[j].offset + p->sectors[j].size > p->size) {
2155  LOG_WARNING("The flash sector at offset 0x%08" PRIx32
2156  " overflows the end of %s bank.",
2157  p->sectors[j].offset, p->name);
2158  LOG_WARNING("The rest of bank will not show in gdb memory map.");
2159  break;
2160  }
2162  start = p->base + p->sectors[j].offset;
2163  xml_printf(&retval, &xml, &pos, &size,
2164  "<memory type=\"flash\" "
2165  "start=\"" TARGET_ADDR_FMT "\" ",
2166  start);
2167  sector_size = p->sectors[j].size;
2168  group_len = sector_size;
2169  } else {
2170  group_len += sector_size; /* equal to p->sectors[j].size */
2171  }
2172 
2173  /* Does this finish a group of sectors?
2174  * If not, continue an already-started group.
2175  */
2176  if (j < p->num_sectors - 1
2177  && p->sectors[j + 1].size == sector_size
2178  && p->sectors[j + 1].offset == p->sectors[j].offset + sector_size
2179  && p->sectors[j + 1].offset + p->sectors[j + 1].size <= p->size)
2180  continue;
2181 
2182  xml_printf(&retval, &xml, &pos, &size,
2183  "length=\"0x%x\">\n"
2184  "<property name=\"blocksize\">"
2185  "0x%x</property>\n"
2186  "</memory>\n",
2187  group_len,
2188  sector_size);
2189  sector_size = 0;
2190  }
2191  }
2192 
2193  ram_start = p->base + p->size;
2194  }
2195 
2196  if (ram_start != 0)
2197  xml_printf(&retval, &xml, &pos, &size,
2198  "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
2199  "length=\"" TARGET_ADDR_FMT "\"/>\n",
2200  ram_start, target_address_max(target) - ram_start + 1);
2201  /* ELSE a flash chip could be at the very end of the address space, in
2202  * which case ram_start will be precisely 0 */
2203 
2204  free(banks);
2205 
2206  xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
2207 
2208  if (retval != ERROR_OK) {
2209  free(xml);
2210  gdb_error(connection, retval);
2211  return retval;
2212  }
2213 
2214  if (offset + length > pos)
2215  length = pos - offset;
2216 
2217  char *t = malloc(length + 1);
2218  t[0] = 'l';
2219  memcpy(t + 1, xml + offset, length);
2220  gdb_put_packet(connection, t, length + 1);
2221 
2222  free(t);
2223  free(xml);
2224  return ERROR_OK;
2225 }
2226 
2227 static const char *gdb_get_reg_type_name(enum reg_type type)
2228 {
2229  switch (type) {
2230  case REG_TYPE_BOOL:
2231  return "bool";
2232  case REG_TYPE_INT:
2233  return "int";
2234  case REG_TYPE_INT8:
2235  return "int8";
2236  case REG_TYPE_INT16:
2237  return "int16";
2238  case REG_TYPE_INT32:
2239  return "int32";
2240  case REG_TYPE_INT64:
2241  return "int64";
2242  case REG_TYPE_INT128:
2243  return "int128";
2244  case REG_TYPE_UINT:
2245  return "uint";
2246  case REG_TYPE_UINT8:
2247  return "uint8";
2248  case REG_TYPE_UINT16:
2249  return "uint16";
2250  case REG_TYPE_UINT32:
2251  return "uint32";
2252  case REG_TYPE_UINT64:
2253  return "uint64";
2254  case REG_TYPE_UINT128:
2255  return "uint128";
2256  case REG_TYPE_CODE_PTR:
2257  return "code_ptr";
2258  case REG_TYPE_DATA_PTR:
2259  return "data_ptr";
2260  case REG_TYPE_FLOAT:
2261  return "float";
2262  case REG_TYPE_IEEE_SINGLE:
2263  return "ieee_single";
2264  case REG_TYPE_IEEE_DOUBLE:
2265  return "ieee_double";
2266  case REG_TYPE_ARCH_DEFINED:
2267  return "int"; /* return arbitrary string to avoid compile warning. */
2268  }
2269 
2270  return "int"; /* "int" as default value */
2271 }
2272 
2273 static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id,
2274  int *num_arch_defined_types)
2275 {
2276  int tbl_sz = *num_arch_defined_types;
2277 
2278  if (type_id && (strcmp(type_id, ""))) {
2279  for (int j = 0; j < (tbl_sz + 1); j++) {
2280  if (!((*arch_defined_types_list)[j])) {
2281  (*arch_defined_types_list)[tbl_sz++] = type_id;
2282  *arch_defined_types_list = realloc(*arch_defined_types_list,
2283  sizeof(char *) * (tbl_sz + 1));
2284  (*arch_defined_types_list)[tbl_sz] = NULL;
2285  *num_arch_defined_types = tbl_sz;
2286  return 1;
2287  } else {
2288  if (!strcmp((*arch_defined_types_list)[j], type_id))
2289  return 0;
2290  }
2291  }
2292  }
2293 
2294  return -1;
2295 }
2296 
2298  char **tdesc, int *pos, int *size, struct reg_data_type *type,
2299  char const **arch_defined_types_list[], int *num_arch_defined_types)
2300 {
2301  int retval = ERROR_OK;
2302 
2303  if (type->type_class == REG_TYPE_CLASS_VECTOR) {
2304  struct reg_data_type *data_type = type->reg_type_vector->type;
2306  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2307  num_arch_defined_types))
2309  arch_defined_types_list,
2310  num_arch_defined_types);
2311  }
2312  /* <vector id="id" type="type" count="count"/> */
2313  xml_printf(&retval, tdesc, pos, size,
2314  "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32 "\"/>\n",
2315  type->id, type->reg_type_vector->type->id,
2316  type->reg_type_vector->count);
2317 
2318  } else if (type->type_class == REG_TYPE_CLASS_UNION) {
2319  struct reg_data_type_union_field *field;
2320  field = type->reg_type_union->fields;
2321  while (field) {
2322  struct reg_data_type *data_type = field->type;
2324  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2325  num_arch_defined_types))
2327  arch_defined_types_list,
2328  num_arch_defined_types);
2329  }
2330 
2331  field = field->next;
2332  }
2333  /* <union id="id">
2334  * <field name="name" type="type"/> ...
2335  * </union> */
2336  xml_printf(&retval, tdesc, pos, size,
2337  "<union id=\"%s\">\n",
2338  type->id);
2339 
2340  field = type->reg_type_union->fields;
2341  while (field) {
2342  xml_printf(&retval, tdesc, pos, size,
2343  "<field name=\"%s\" type=\"%s\"/>\n",
2344  field->name, field->type->id);
2345 
2346  field = field->next;
2347  }
2348 
2349  xml_printf(&retval, tdesc, pos, size,
2350  "</union>\n");
2351 
2352  } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
2353  struct reg_data_type_struct_field *field;
2354  field = type->reg_type_struct->fields;
2355 
2356  if (field->use_bitfields) {
2357  /* <struct id="id" size="size">
2358  * <field name="name" start="start" end="end"/> ...
2359  * </struct> */
2360  xml_printf(&retval, tdesc, pos, size,
2361  "<struct id=\"%s\" size=\"%" PRIu32 "\">\n",
2362  type->id, type->reg_type_struct->size);
2363  while (field) {
2364  xml_printf(&retval, tdesc, pos, size,
2365  "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2366  field->name, field->bitfield->start, field->bitfield->end,
2368 
2369  field = field->next;
2370  }
2371  } else {
2372  while (field) {
2373  struct reg_data_type *data_type = field->type;
2375  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2376  num_arch_defined_types))
2378  arch_defined_types_list,
2379  num_arch_defined_types);
2380  }
2381  }
2382 
2383  /* <struct id="id">
2384  * <field name="name" type="type"/> ...
2385  * </struct> */
2386  xml_printf(&retval, tdesc, pos, size,
2387  "<struct id=\"%s\">\n",
2388  type->id);
2389  while (field) {
2390  xml_printf(&retval, tdesc, pos, size,
2391  "<field name=\"%s\" type=\"%s\"/>\n",
2392  field->name, field->type->id);
2393 
2394  field = field->next;
2395  }
2396  }
2397 
2398  xml_printf(&retval, tdesc, pos, size,
2399  "</struct>\n");
2400 
2401  } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
2402  /* <flags id="id" size="size">
2403  * <field name="name" start="start" end="end"/> ...
2404  * </flags> */
2405  xml_printf(&retval, tdesc, pos, size,
2406  "<flags id=\"%s\" size=\"%" PRIu32 "\">\n",
2407  type->id, type->reg_type_flags->size);
2408 
2409  struct reg_data_type_flags_field *field;
2410  field = type->reg_type_flags->fields;
2411  while (field) {
2412  xml_printf(&retval, tdesc, pos, size,
2413  "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2414  field->name, field->bitfield->start, field->bitfield->end,
2416 
2417  field = field->next;
2418  }
2419 
2420  xml_printf(&retval, tdesc, pos, size,
2421  "</flags>\n");
2422 
2423  }
2424 
2425  return ERROR_OK;
2426 }
2427 
2428 /* Get a list of available target registers features. feature_list must
2429  * be freed by caller.
2430  */
2431 static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
2432  struct reg **reg_list, int reg_list_size)
2433 {
2434  int tbl_sz = 0;
2435 
2436  /* Start with only one element */
2437  *feature_list = calloc(1, sizeof(char *));
2438 
2439  for (int i = 0; i < reg_list_size; i++) {
2440  if (!reg_list[i]->exist || reg_list[i]->hidden)
2441  continue;
2442 
2443  if (reg_list[i]->feature
2444  && reg_list[i]->feature->name
2445  && (strcmp(reg_list[i]->feature->name, ""))) {
2446  /* We found a feature, check if the feature is already in the
2447  * table. If not, allocate a new entry for the table and
2448  * put the new feature in it.
2449  */
2450  for (int j = 0; j < (tbl_sz + 1); j++) {
2451  if (!((*feature_list)[j])) {
2452  (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
2453  *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
2454  (*feature_list)[tbl_sz] = NULL;
2455  break;
2456  } else {
2457  if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
2458  break;
2459  }
2460  }
2461  }
2462  }
2463 
2464  if (feature_list_size)
2465  *feature_list_size = tbl_sz;
2466 
2467  return ERROR_OK;
2468 }
2469 
2470 /* Create a register list that's the union of all the registers of the SMP
2471  * group this target is in. If the target is not part of an SMP group, this
2472  * returns the same as target_get_gdb_reg_list_noread().
2473  */
2474 static int smp_reg_list_noread(struct target *target,
2475  struct reg **combined_list[], int *combined_list_size,
2476  enum target_register_class reg_class)
2477 {
2478  if (!target->smp)
2479  return target_get_gdb_reg_list_noread(target, combined_list,
2480  combined_list_size, REG_CLASS_ALL);
2481 
2482  unsigned int combined_allocated = 256;
2483  struct reg **local_list = malloc(combined_allocated * sizeof(struct reg *));
2484  if (!local_list) {
2485  LOG_ERROR("malloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2486  return ERROR_FAIL;
2487  }
2488  unsigned int local_list_size = 0;
2489 
2490  struct target_list *head;
2492  if (!target_was_examined(head->target))
2493  continue;
2494 
2495  struct reg **reg_list = NULL;
2496  int reg_list_size;
2497  int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2498  &reg_list_size, reg_class);
2499  if (result != ERROR_OK) {
2500  free(local_list);
2501  return result;
2502  }
2503  for (int i = 0; i < reg_list_size; i++) {
2504  bool found = false;
2505  struct reg *a = reg_list[i];
2506  if (a->exist) {
2507  /* Nested loop makes this O(n^2), but this entire function with
2508  * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2509  * for me. */
2510  for (unsigned int j = 0; j < local_list_size; j++) {
2511  struct reg *b = local_list[j];
2512  if (!strcmp(a->name, b->name)) {
2513  found = true;
2514  if (a->size != b->size) {
2515  LOG_ERROR("SMP register %s is %d bits on one "
2516  "target, but %d bits on another target.",
2517  a->name, a->size, b->size);
2518  free(reg_list);
2519  free(local_list);
2520  return ERROR_FAIL;
2521  }
2522  break;
2523  }
2524  }
2525  if (!found) {
2526  LOG_TARGET_DEBUG(target, "%s not found in combined list", a->name);
2527  if (local_list_size >= combined_allocated) {
2528  combined_allocated *= 2;
2529  local_list = realloc(local_list, combined_allocated * sizeof(struct reg *));
2530  if (!local_list) {
2531  LOG_ERROR("realloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2532  free(reg_list);
2533  return ERROR_FAIL;
2534  }
2535  }
2536  local_list[local_list_size] = a;
2537  local_list_size++;
2538  }
2539  }
2540  }
2541  free(reg_list);
2542  }
2543 
2544  if (local_list_size == 0) {
2545  LOG_ERROR("Unable to get register list");
2546  free(local_list);
2547  return ERROR_FAIL;
2548  }
2549 
2550  /* Now warn the user about any registers that weren't found in every target. */
2552  if (!target_was_examined(head->target))
2553  continue;
2554 
2555  struct reg **reg_list = NULL;
2556  int reg_list_size;
2557  int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2558  &reg_list_size, reg_class);
2559  if (result != ERROR_OK) {
2560  free(local_list);
2561  return result;
2562  }
2563  for (unsigned int i = 0; i < local_list_size; i++) {
2564  bool found = false;
2565  struct reg *a = local_list[i];
2566  for (int j = 0; j < reg_list_size; j++) {
2567  struct reg *b = reg_list[j];
2568  if (b->exist && !strcmp(a->name, b->name)) {
2569  found = true;
2570  break;
2571  }
2572  }
2573  if (!found) {
2574  LOG_TARGET_WARNING(head->target, "Register %s does not exist, which is part of an SMP group where "
2575  "this register does exist.", a->name);
2576  }
2577  }
2578  free(reg_list);
2579  }
2580 
2581  *combined_list = local_list;
2582  *combined_list_size = local_list_size;
2583  return ERROR_OK;
2584 }
2585 
2586 static int gdb_generate_target_description(struct target *target, char **tdesc_out)
2587 {
2588  int retval = ERROR_OK;
2589  struct reg **reg_list = NULL;
2590  int reg_list_size;
2591  char const *architecture;
2592  char const **features = NULL;
2593  int feature_list_size = 0;
2594  char *tdesc = NULL;
2595  int pos = 0;
2596  int size = 0;
2597 
2598 
2599  retval = smp_reg_list_noread(target, &reg_list, &reg_list_size,
2600  REG_CLASS_ALL);
2601 
2602  if (retval != ERROR_OK) {
2603  LOG_ERROR("get register list failed");
2604  retval = ERROR_FAIL;
2605  goto error;
2606  }
2607 
2608  if (reg_list_size <= 0) {
2609  LOG_ERROR("get register list failed");
2610  retval = ERROR_FAIL;
2611  goto error;
2612  }
2613 
2614  /* Get a list of available target registers features */
2615  retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2616  if (retval != ERROR_OK) {
2617  LOG_ERROR("Can't get the registers feature list");
2618  retval = ERROR_FAIL;
2619  goto error;
2620  }
2621 
2622  /* If we found some features associated with registers, create sections */
2623  int current_feature = 0;
2624 
2625  xml_printf(&retval, &tdesc, &pos, &size,
2626  "<?xml version=\"1.0\"?>\n"
2627  "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2628  "<target version=\"1.0\">\n");
2629 
2630  /* generate architecture element if supported by target */
2631  architecture = target_get_gdb_arch(target);
2632  if (architecture)
2633  xml_printf(&retval, &tdesc, &pos, &size,
2634  "<architecture>%s</architecture>\n", architecture);
2635 
2636  /* generate target description according to register list */
2637  if (features) {
2638  while (features[current_feature]) {
2639  char const **arch_defined_types = NULL;
2640  int num_arch_defined_types = 0;
2641 
2642  arch_defined_types = calloc(1, sizeof(char *));
2643  xml_printf(&retval, &tdesc, &pos, &size,
2644  "<feature name=\"%s\">\n",
2645  features[current_feature]);
2646 
2647  int i;
2648  for (i = 0; i < reg_list_size; i++) {
2649 
2650  if (!reg_list[i]->exist || reg_list[i]->hidden)
2651  continue;
2652 
2653  if (strcmp(reg_list[i]->feature->name, features[current_feature]))
2654  continue;
2655 
2656  const char *type_str;
2657  if (reg_list[i]->reg_data_type) {
2658  if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
2659  /* generate <type... first, if there are architecture-defined types. */
2660  if (lookup_add_arch_defined_types(&arch_defined_types,
2661  reg_list[i]->reg_data_type->id,
2662  &num_arch_defined_types))
2664  reg_list[i]->reg_data_type,
2665  &arch_defined_types,
2666  &num_arch_defined_types);
2667 
2668  type_str = reg_list[i]->reg_data_type->id;
2669  } else {
2670  /* predefined type */
2671  type_str = gdb_get_reg_type_name(
2672  reg_list[i]->reg_data_type->type);
2673  }
2674  } else {
2675  /* Default type is "int" */
2676  type_str = "int";
2677  }
2678 
2679  xml_printf(&retval, &tdesc, &pos, &size,
2680  "<reg name=\"%s\"", reg_list[i]->name);
2681  xml_printf(&retval, &tdesc, &pos, &size,
2682  " bitsize=\"%" PRIu32 "\"", reg_list[i]->size);
2683  xml_printf(&retval, &tdesc, &pos, &size,
2684  " regnum=\"%" PRIu32 "\"", reg_list[i]->number);
2685  if (reg_list[i]->caller_save)
2686  xml_printf(&retval, &tdesc, &pos, &size,
2687  " save-restore=\"yes\"");
2688  else
2689  xml_printf(&retval, &tdesc, &pos, &size,
2690  " save-restore=\"no\"");
2691 
2692  xml_printf(&retval, &tdesc, &pos, &size,
2693  " type=\"%s\"", type_str);
2694 
2695  if (reg_list[i]->group)
2696  xml_printf(&retval, &tdesc, &pos, &size,
2697  " group=\"%s\"", reg_list[i]->group);
2698 
2699  xml_printf(&retval, &tdesc, &pos, &size,
2700  "/>\n");
2701  }
2702 
2703  xml_printf(&retval, &tdesc, &pos, &size,
2704  "</feature>\n");
2705 
2706  current_feature++;
2707  free(arch_defined_types);
2708  }
2709  }
2710 
2711  xml_printf(&retval, &tdesc, &pos, &size,
2712  "</target>\n");
2713 
2714 error:
2715  free(features);
2716  free(reg_list);
2717 
2718  if (retval == ERROR_OK)
2719  *tdesc_out = tdesc;
2720  else
2721  free(tdesc);
2722 
2723  return retval;
2724 }
2725 
2726 static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
2727  char **chunk, int32_t offset, uint32_t length)
2728 {
2729  if (!target_desc) {
2730  LOG_ERROR("Unable to Generate Target Description");
2731  return ERROR_FAIL;
2732  }
2733 
2734  char *tdesc = target_desc->tdesc;
2735  uint32_t tdesc_length = target_desc->tdesc_length;
2736 
2737  if (!tdesc) {
2738  int retval = gdb_generate_target_description(target, &tdesc);
2739  if (retval != ERROR_OK) {
2740  LOG_ERROR("Unable to Generate Target Description");
2741  return ERROR_FAIL;
2742  }
2743 
2744  tdesc_length = strlen(tdesc);
2745  }
2746 
2747  char transfer_type;
2748 
2749  if (length < (tdesc_length - offset))
2750  transfer_type = 'm';
2751  else
2752  transfer_type = 'l';
2753 
2754  *chunk = malloc(length + 2);
2755  if (!*chunk) {
2756  LOG_ERROR("Unable to allocate memory");
2757  return ERROR_FAIL;
2758  }
2759 
2760  (*chunk)[0] = transfer_type;
2761  if (transfer_type == 'm') {
2762  strncpy((*chunk) + 1, tdesc + offset, length);
2763  (*chunk)[1 + length] = '\0';
2764  } else {
2765  strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
2766  (*chunk)[1 + (tdesc_length - offset)] = '\0';
2767 
2768  /* After gdb-server sends out last chunk, invalidate tdesc. */
2769  free(tdesc);
2770  tdesc = NULL;
2771  tdesc_length = 0;
2772  }
2773 
2774  target_desc->tdesc = tdesc;
2775  target_desc->tdesc_length = tdesc_length;
2776 
2777  return ERROR_OK;
2778 }
2779 
2780 static int gdb_target_description_supported(struct target *target, bool *supported)
2781 {
2782  int retval = ERROR_OK;
2783  struct reg **reg_list = NULL;
2784  int reg_list_size = 0;
2785  char const **features = NULL;
2786  int feature_list_size = 0;
2787 
2788  char const *architecture = target_get_gdb_arch(target);
2789 
2790  retval = target_get_gdb_reg_list_noread(target, &reg_list,
2791  &reg_list_size, REG_CLASS_ALL);
2792  if (retval != ERROR_OK) {
2793  LOG_ERROR("get register list failed");
2794  goto error;
2795  }
2796 
2797  if (reg_list_size <= 0) {
2798  LOG_ERROR("get register list failed");
2799  retval = ERROR_FAIL;
2800  goto error;
2801  }
2802 
2803  /* Get a list of available target registers features */
2804  retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2805  if (retval != ERROR_OK) {
2806  LOG_ERROR("Can't get the registers feature list");
2807  goto error;
2808  }
2809 
2810  if (supported) {
2811  if (architecture || feature_list_size)
2812  *supported = true;
2813  else
2814  *supported = false;
2815  }
2816 
2817 error:
2818  free(features);
2819 
2820  free(reg_list);
2821 
2822  return retval;
2823 }
2824 
2825 static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
2826 {
2827  struct rtos *rtos = target->rtos;
2828  int retval = ERROR_OK;
2829  char *thread_list = NULL;
2830  int pos = 0;
2831  int size = 0;
2832 
2833  xml_printf(&retval, &thread_list, &pos, &size,
2834  "<?xml version=\"1.0\"?>\n"
2835  "<threads>\n");
2836 
2837  if (rtos) {
2838  for (int i = 0; i < rtos->thread_count; i++) {
2840 
2841  if (!thread_detail->exists)
2842  continue;
2843 
2845  xml_printf(&retval, &thread_list, &pos, &size,
2846  "<thread id=\"%" PRIx64 "\" name=\"%s\">",
2849  else
2850  xml_printf(&retval, &thread_list, &pos, &size,
2851  "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
2852 
2854  xml_printf(&retval, &thread_list, &pos, &size,
2855  "Name: %s", thread_detail->thread_name_str);
2856 
2859  xml_printf(&retval, &thread_list, &pos, &size,
2860  ", ");
2861  xml_printf(&retval, &thread_list, &pos, &size,
2862  "%s", thread_detail->extra_info_str);
2863  }
2864 
2865  xml_printf(&retval, &thread_list, &pos, &size,
2866  "</thread>\n");
2867  }
2868  }
2869 
2870  xml_printf(&retval, &thread_list, &pos, &size,
2871  "</threads>\n");
2872 
2873  if (retval == ERROR_OK)
2874  *thread_list_out = thread_list;
2875  else
2876  free(thread_list);
2877 
2878  return retval;
2879 }
2880 
2881 static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
2882  char **chunk, int32_t offset, uint32_t length)
2883 {
2884  if (!*thread_list) {
2885  int retval = gdb_generate_thread_list(target, thread_list);
2886  if (retval != ERROR_OK) {
2887  LOG_ERROR("Unable to Generate Thread List");
2888  return ERROR_FAIL;
2889  }
2890  }
2891 
2892  size_t thread_list_length = strlen(*thread_list);
2893  char transfer_type;
2894 
2895  length = MIN(length, thread_list_length - offset);
2896  if (length < (thread_list_length - offset))
2897  transfer_type = 'm';
2898  else
2899  transfer_type = 'l';
2900 
2901  *chunk = malloc(length + 2 + 3);
2902  /* Allocating extra 3 bytes prevents false positive valgrind report
2903  * of strlen(chunk) word access:
2904  * Invalid read of size 4
2905  * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2906  if (!*chunk) {
2907  LOG_ERROR("Unable to allocate memory");
2908  return ERROR_FAIL;
2909  }
2910 
2911  (*chunk)[0] = transfer_type;
2912  strncpy((*chunk) + 1, (*thread_list) + offset, length);
2913  (*chunk)[1 + length] = '\0';
2914 
2915  /* After gdb-server sends out last chunk, invalidate thread list. */
2916  if (transfer_type == 'l') {
2917  free(*thread_list);
2918  *thread_list = NULL;
2919  }
2920 
2921  return ERROR_OK;
2922 }
2923 
2925  char const *packet, int packet_size)
2926 {
2927  struct command_context *cmd_ctx = connection->cmd_ctx;
2930 
2931  if (strncmp(packet, "qRcmd,", 6) == 0) {
2932  if (packet_size > 6) {
2933  Jim_Interp *interp = cmd_ctx->interp;
2934  char *cmd;
2935  cmd = malloc((packet_size - 6) / 2 + 1);
2936  size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
2937  cmd[len] = 0;
2938 
2939  /* We want to print all debug output to GDB connection */
2942  /* some commands need to know the GDB connection, make note of current
2943  * GDB connection. */
2945 
2946  struct target *saved_target_override = cmd_ctx->current_target_override;
2947  cmd_ctx->current_target_override = NULL;
2948 
2949  struct command_context *old_context = Jim_GetAssocData(interp, "context");
2950  Jim_DeleteAssocData(interp, "context");
2951  int retval = Jim_SetAssocData(interp, "context", NULL, cmd_ctx);
2952  if (retval == JIM_OK) {
2953  retval = Jim_EvalObj(interp, Jim_NewStringObj(interp, cmd, -1));
2954  Jim_DeleteAssocData(interp, "context");
2955  }
2956  int inner_retval = Jim_SetAssocData(interp, "context", NULL, old_context);
2957  if (retval == JIM_OK)
2958  retval = inner_retval;
2959 
2960  cmd_ctx->current_target_override = saved_target_override;
2961 
2964  free(cmd);
2965  if (retval == JIM_RETURN)
2966  retval = interp->returnCode;
2967  int lenmsg;
2968  const char *cretmsg = Jim_GetString(Jim_GetResult(interp), &lenmsg);
2969  char *retmsg;
2970  if (lenmsg && cretmsg[lenmsg - 1] != '\n') {
2971  retmsg = alloc_printf("%s\n", cretmsg);
2972  lenmsg++;
2973  } else {
2974  retmsg = strdup(cretmsg);
2975  }
2976 
2978 
2979  if (!retmsg)
2981 
2982  if (retval == JIM_OK) {
2983  if (lenmsg) {
2984  char *hex_buffer = malloc(lenmsg * 2 + 1);
2985  if (!hex_buffer) {
2986  free(retmsg);
2988  }
2989 
2990  size_t pkt_len = hexify(hex_buffer, (const uint8_t *)retmsg, lenmsg,
2991  lenmsg * 2 + 1);
2992  gdb_put_packet(connection, hex_buffer, pkt_len);
2993  free(hex_buffer);
2994  } else {
2995  gdb_put_packet(connection, "OK", 2);
2996  }
2997  } else {
2998  if (lenmsg)
2999  gdb_output_con(connection, retmsg);
3000  gdb_send_error(connection, retval);
3001  }
3002  free(retmsg);
3003  return ERROR_OK;
3004  }
3005  gdb_put_packet(connection, "OK", 2);
3006  return ERROR_OK;
3007  } else if (strncmp(packet, "qCRC:", 5) == 0) {
3008  if (packet_size > 5) {
3009  int retval;
3010  char gdb_reply[10];
3011  char *separator;
3012  uint32_t checksum;
3013  target_addr_t addr = 0;
3014  uint32_t len = 0;
3015 
3016  /* skip command character */
3017  packet += 5;
3018 
3019  addr = strtoull(packet, &separator, 16);
3020 
3021  if (*separator != ',') {
3022  LOG_ERROR("incomplete read memory packet received, dropping connection");
3024  }
3025 
3026  len = strtoul(separator + 1, NULL, 16);
3027 
3029  retval = target_checksum_memory(target, addr, len, &checksum);
3031 
3032  if (retval == ERROR_OK) {
3033  snprintf(gdb_reply, 10, "C%8.8" PRIx32, checksum);
3034  gdb_put_packet(connection, gdb_reply, 9);
3035  } else {
3036  retval = gdb_error(connection, retval);
3037  if (retval != ERROR_OK)
3038  return retval;
3039  }
3040 
3041  return ERROR_OK;
3042  }
3043  } else if (strncmp(packet, "qSupported", 10) == 0) {
3044  /* we currently support packet size and qXfer:memory-map:read (if enabled)
3045  * qXfer:features:read is supported for some targets */
3046  int retval = ERROR_OK;
3047  char *buffer = NULL;
3048  int pos = 0;
3049  int size = 0;
3050  bool gdb_target_desc_supported = false;
3051 
3052  /* we need to test that the target supports target descriptions */
3053  retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
3054  if (retval != ERROR_OK) {
3055  LOG_INFO("Failed detecting Target Description Support, disabling");
3056  gdb_target_desc_supported = false;
3057  }
3058 
3059  /* support may be disabled globally */
3061  if (gdb_target_desc_supported)
3062  LOG_WARNING("Target Descriptions Supported, but disabled");
3063  gdb_target_desc_supported = false;
3064  }
3065 
3066  xml_printf(&retval,
3067  &buffer,
3068  &pos,
3069  &size,
3070  "PacketSize=%x;"
3071  "QStartNoAckMode+;"
3072  "binary-upload+;"
3073  "qXfer:features:read%c;"
3074  "qXfer:memory-map:read%c;"
3075  "qXfer:threads:read+;"
3076  "vContSupported+",
3078  gdb_target_desc_supported ? '+' : '-',
3079  (gdb_use_memory_map && (flash_get_bank_count() > 0)) ? '+' : '-');
3080 
3081  if (retval != ERROR_OK) {
3083  return ERROR_OK;
3084  }
3085 
3087  free(buffer);
3088 
3089  return ERROR_OK;
3090  } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
3091  && (flash_get_bank_count() > 0))
3092  return gdb_memory_map(connection, packet, packet_size);
3093  else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
3094  char *xml = NULL;
3095  int retval = ERROR_OK;
3096 
3097  int offset;
3098  unsigned int length;
3099 
3100  /* skip command character */
3101  packet += 20;
3102 
3103  if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
3105  return ERROR_OK;
3106  }
3107 
3108  /* Target should prepare correct target description for annex.
3109  * The first character of returned xml is 'm' or 'l'. 'm' for
3110  * there are *more* chunks to transfer. 'l' for it is the *last*
3111  * chunk of target description.
3112  */
3114  &xml, offset, length);
3115  if (retval != ERROR_OK) {
3116  gdb_error(connection, retval);
3117  return retval;
3118  }
3119 
3120  gdb_put_packet(connection, xml, strlen(xml));
3121 
3122  free(xml);
3123  return ERROR_OK;
3124  } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
3125  char *xml = NULL;
3126  int retval = ERROR_OK;
3127 
3128  int offset;
3129  unsigned int length;
3130 
3131  /* skip command character */
3132  packet += 19;
3133 
3134  if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
3136  return ERROR_OK;
3137  }
3138 
3139  /* Target should prepare correct thread list for annex.
3140  * The first character of returned xml is 'm' or 'l'. 'm' for
3141  * there are *more* chunks to transfer. 'l' for it is the *last*
3142  * chunk of target description.
3143  */
3145  &xml, offset, length);
3146  if (retval != ERROR_OK) {
3147  gdb_error(connection, retval);
3148  return retval;
3149  }
3150 
3151  gdb_put_packet(connection, xml, strlen(xml));
3152 
3153  free(xml);
3154  return ERROR_OK;
3155  } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
3157  gdb_put_packet(connection, "OK", 2);
3158  return ERROR_OK;
3159  } else if (target->type->gdb_query_custom) {
3160  char *buffer = NULL;
3161  int ret = target->type->gdb_query_custom(target, packet, &buffer);
3163  return ret;
3164  }
3165 
3166  gdb_put_packet(connection, "", 0);
3167  return ERROR_OK;
3168 }
3169 
3170 static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet,
3171  __attribute__((unused)) int packet_size)
3172 {
3175  const char *parse = packet;
3176  int retval;
3177 
3178  /* query for vCont supported */
3179  if (parse[0] == '?') {
3180  if (target->type->step) {
3181  /* gdb doesn't accept c without C and s without S */
3182  gdb_put_packet(connection, "vCont;c;C;s;S", 13);
3183  return true;
3184  }
3185  return false;
3186  }
3187 
3188  if (parse[0] == ';') {
3189  ++parse;
3190  }
3191 
3192  /* simple case, a continue packet */
3193  if (parse[0] == 'c') {
3194  gdb_running_type = 'c';
3195  LOG_TARGET_DEBUG(target, "target continue");
3197  retval = target_resume(target, true, 0, false, false);
3198  if (retval == ERROR_TARGET_NOT_HALTED)
3199  LOG_TARGET_INFO(target, "target was not halted when resume was requested");
3200 
3201  /* poll target in an attempt to make its internal state consistent */
3202  if (retval != ERROR_OK) {
3203  retval = target_poll(target);
3204  if (retval != ERROR_OK)
3205  LOG_TARGET_DEBUG(target, "error polling target after failed resume");
3206  }
3207 
3208  /*
3209  * We don't report errors to gdb here, move frontend_state to
3210  * TARGET_RUNNING to stay in sync with gdb's expectation of the
3211  * target state
3212  */
3215 
3216  return true;
3217  }
3218 
3219  /* single-step or step-over-breakpoint */
3220  if (parse[0] == 's') {
3221  gdb_running_type = 's';
3222  bool fake_step = false;
3223 
3224  struct target *ct = target;
3225  bool current_pc = true;
3226  int64_t thread_id;
3227  parse++;
3228  if (parse[0] == ':') {
3229  char *endp;
3230  parse++;
3231  thread_id = strtoll(parse, &endp, 16);
3232  if (endp) {
3233  parse = endp;
3234  }
3235  } else {
3236  thread_id = 0;
3237  }
3238 
3239  if (target->rtos) {
3240  /* Sometimes this results in picking a different thread than
3241  * gdb just requested to step. Then we fake it, and now there's
3242  * a different thread selected than gdb expects, so register
3243  * accesses go to the wrong one!
3244  * E.g.:
3245  * Hg1$
3246  * P8=72101ce197869329$ # write r8 on thread 1
3247  * g$
3248  * vCont?$
3249  * vCont;s:1;c$ # rtos_update_threads changes to other thread
3250  * g$
3251  * qXfer:threads:read::0,fff$
3252  * P8=cc060607eb89ca7f$ # write r8 on other thread
3253  * g$
3254  */
3255  /* rtos_update_threads(target); */
3256 
3257  target->rtos->gdb_target_for_threadid(connection, thread_id, &ct);
3258 
3259  /*
3260  * check if the thread to be stepped is the current rtos thread
3261  * if not, we must fake the step
3262  */
3263  fake_step = rtos_needs_fake_step(target, thread_id);
3264  }
3265 
3266  if (parse[0] == ';') {
3267  ++parse;
3268 
3269  if (parse[0] == 'c') {
3270  parse += 1;
3271 
3272  /* check if thread-id follows */
3273  if (parse[0] == ':') {
3274  int64_t tid;
3275  parse += 1;
3276 
3277  tid = strtoll(parse, NULL, 16);
3278  if (tid == thread_id) {
3279  /*
3280  * Special case: only step a single thread (core),
3281  * keep the other threads halted. Currently, only
3282  * aarch64 target understands it. Other target types don't
3283  * care (nobody checks the actual value of 'current')
3284  * and it doesn't really matter. This deserves
3285  * a symbolic constant and a formal interface documentation
3286  * at a later time.
3287  */
3288  LOG_DEBUG("request to step current core only");
3289  /* uncomment after checking that indeed other targets are safe */
3290  /*current_pc = 2;*/
3291  }
3292  }
3293  }
3294  }
3295 
3296  LOG_TARGET_DEBUG(ct, "single-step thread %" PRIx64, thread_id);
3299 
3300  /*
3301  * work around an annoying gdb behaviour: when the current thread
3302  * is changed in gdb, it assumes that the target can follow and also
3303  * make the thread current. This is an assumption that cannot hold
3304  * for a real target running a multi-threading OS. We just fake
3305  * the step to not trigger an internal error in gdb. See
3306  * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3307  */
3308  if (fake_step) {
3309  int sig_reply_len;
3310  char sig_reply[128];
3311 
3312  LOG_DEBUG("fake step thread %"PRIx64, thread_id);
3313 
3314  sig_reply_len = snprintf(sig_reply, sizeof(sig_reply),
3315  "T05thread:%016"PRIx64";", thread_id);
3316 
3317  gdb_put_packet(connection, sig_reply, sig_reply_len);
3319 
3320  return true;
3321  }
3322 
3323  /* support for gdb_sync command */
3324  if (gdb_connection->sync) {
3325  gdb_connection->sync = false;
3326  if (ct->state == TARGET_HALTED) {
3327  LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3328  "from the target.");
3331  } else
3333  return true;
3334  }
3335 
3336  retval = target_step(ct, current_pc, 0, false);
3337  if (retval == ERROR_TARGET_NOT_HALTED)
3338  LOG_TARGET_INFO(ct, "target was not halted when step was requested");
3339 
3340  /* if step was successful send a reply back to gdb */
3341  if (retval == ERROR_OK) {
3342  retval = target_poll(ct);
3343  if (retval != ERROR_OK)
3344  LOG_TARGET_DEBUG(ct, "error polling target after successful step");
3345  /* send back signal information */
3347  /* stop forwarding log packets! */
3349  } else
3351  return true;
3352  }
3353  LOG_ERROR("Unknown vCont packet");
3354  return false;
3355 }
3356 
3357 static char *next_hex_encoded_field(const char **str, char sep)
3358 {
3359  size_t hexlen;
3360  const char *hex = *str;
3361  if (hex[0] == '\0')
3362  return NULL;
3363 
3364  const char *end = strchr(hex, sep);
3365  if (!end)
3366  hexlen = strlen(hex);
3367  else
3368  hexlen = end - hex;
3369  *str = hex + hexlen + 1;
3370 
3371  if (hexlen % 2 != 0) {
3372  /* Malformed hex data */
3373  return NULL;
3374  }
3375 
3376  size_t count = hexlen / 2;
3377  char *decoded = malloc(count + 1);
3378  if (!decoded)
3379  return NULL;
3380 
3381  size_t converted = unhexify((void *)decoded, hex, count);
3382  if (converted != count) {
3383  free(decoded);
3384  return NULL;
3385  }
3386 
3387  decoded[count] = '\0';
3388  return decoded;
3389 }
3390 
3391 /* handle extended restart packet */
3392 static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
3393 {
3394  struct gdb_connection *gdb_con = connection->priv;
3396 
3399  command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
3400  target_name(target));
3401  /* set connection as attached after reset */
3402  gdb_con->attached = true;
3403  /* info rtos parts */
3404  gdb_thread_packet(connection, packet, packet_size);
3405 }
3406 
3407 static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
3408 {
3410  const char *parse = packet;
3411 
3412  /* Skip "vRun" */
3413  parse += 4;
3414 
3415  if (parse[0] != ';')
3416  return false;
3417  parse++;
3418 
3419  /* Skip first field "filename"; don't know what to do with it. */
3420  free(next_hex_encoded_field(&parse, ';'));
3421 
3422  char *cmdline = next_hex_encoded_field(&parse, ';');
3423  while (cmdline) {
3424  char *arg = next_hex_encoded_field(&parse, ';');
3425  if (!arg)
3426  break;
3427  char *new_cmdline = alloc_printf("%s %s", cmdline, arg);
3428  free(cmdline);
3429  free(arg);
3430  cmdline = new_cmdline;
3431  }
3432 
3433  if (cmdline) {
3434  if (target->semihosting) {
3435  LOG_INFO("GDB set inferior command line to '%s'", cmdline);
3436  free(target->semihosting->cmdline);
3437  target->semihosting->cmdline = cmdline;
3438  } else {
3439  LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline);
3440  free(cmdline);
3441  }
3442  }
3443 
3444  gdb_restart_inferior(connection, packet, packet_size);
3445  gdb_put_packet(connection, "S00", 3);
3446  return true;
3447 }
3448 
3450  char const *packet, int packet_size)
3451 {
3453  int result;
3454 
3456 
3457  if (strncmp(packet, "vCont", 5) == 0) {
3458  bool handled;
3459 
3460  packet += 5;
3461  packet_size -= 5;
3462 
3463  handled = gdb_handle_vcont_packet(connection, packet, packet_size);
3464  if (!handled)
3465  gdb_put_packet(connection, "", 0);
3466 
3467  return ERROR_OK;
3468  }
3469 
3470  if (strncmp(packet, "vRun", 4) == 0) {
3471  bool handled;
3472 
3473  handled = gdb_handle_vrun_packet(connection, packet, packet_size);
3474  if (!handled)
3475  gdb_put_packet(connection, "", 0);
3476 
3477  return ERROR_OK;
3478  }
3479 
3480  /* if flash programming disabled - send a empty reply */
3481 
3482  if (!gdb_flash_program) {
3483  gdb_put_packet(connection, "", 0);
3484  return ERROR_OK;
3485  }
3486 
3487  if (strncmp(packet, "vFlashErase:", 12) == 0) {
3489  unsigned long length;
3490 
3491  char const *parse = packet + 12;
3492  if (*parse == '\0') {
3493  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3495  }
3496 
3497  addr = strtoull(parse, (char **)&parse, 16);
3498 
3499  if (*(parse++) != ',' || *parse == '\0') {
3500  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3502  }
3503 
3504  length = strtoul(parse, (char **)&parse, 16);
3505 
3506  if (*parse != '\0') {
3507  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3509  }
3510 
3511  /* assume all sectors need erasing - stops any problems
3512  * when flash_write is called multiple times */
3513  flash_set_dirty();
3514 
3515  /* perform any target specific operations before the erase */
3518 
3519  /* vFlashErase:addr,length messages require region start and
3520  * end to be "block" aligned ... if padding is ever needed,
3521  * GDB will have become dangerously confused.
3522  */
3523  result = flash_erase_address_range(target, false, addr,
3524  length);
3525 
3526  /* perform any target specific operations after the erase */
3529 
3530  /* perform erase */
3531  if (result != ERROR_OK) {
3532  /* GDB doesn't evaluate the actual error number returned,
3533  * treat a failed erase as an I/O error
3534  */
3535  gdb_send_error(connection, EIO);
3536  LOG_ERROR("flash_erase returned %i", result);
3537  } else
3538  gdb_put_packet(connection, "OK", 2);
3539 
3540  return ERROR_OK;
3541  }
3542 
3543  if (strncmp(packet, "vFlashWrite:", 12) == 0) {
3544  int retval;
3546  unsigned long length;
3547  char const *parse = packet + 12;
3548 
3549  if (*parse == '\0') {
3550  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3552  }
3553 
3554  addr = strtoull(parse, (char **)&parse, 16);
3555  if (*(parse++) != ':') {
3556  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3558  }
3559  length = packet_size - (parse - packet);
3560 
3561  /* create a new image if there isn't already one */
3562  if (!gdb_connection->vflash_image) {
3563  gdb_connection->vflash_image = malloc(sizeof(struct image));
3564  image_open(gdb_connection->vflash_image, "", "build");
3565  }
3566 
3567  /* create new section with content from packet buffer */
3569  addr, length, 0x0, (uint8_t const *)parse);
3570  if (retval != ERROR_OK)
3571  return retval;
3572 
3573  gdb_put_packet(connection, "OK", 2);
3574 
3575  return ERROR_OK;
3576  }
3577 
3578  if (strncmp(packet, "vFlashDone", 10) == 0) {
3579  uint32_t written;
3580 
3581  /* GDB command 'flash-erase' does not send a vFlashWrite,
3582  * so nothing to write here. */
3583  if (!gdb_connection->vflash_image) {
3584  gdb_put_packet(connection, "OK", 2);
3585  return ERROR_OK;
3586  }
3587 
3588  /* process the flashing buffer. No need to erase as GDB
3589  * always issues a vFlashErase first. */
3593  &written, false);
3596  if (result != ERROR_OK) {
3597  if (result == ERROR_FLASH_DST_OUT_OF_BANK)
3598  gdb_put_packet(connection, "E.memtype", 9);
3599  else
3600  gdb_send_error(connection, EIO);
3601  } else {
3602  LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
3603  gdb_put_packet(connection, "OK", 2);
3604  }
3605 
3609 
3610  return ERROR_OK;
3611  }
3612 
3613  gdb_put_packet(connection, "", 0);
3614  return ERROR_OK;
3615 }
3616 
3617 static int gdb_detach(struct connection *connection)
3618 {
3619  /*
3620  * Only reply "OK" to GDB
3621  * it will close the connection and this will trigger a call to
3622  * gdb_connection_closed() that will in turn trigger the event
3623  * TARGET_EVENT_GDB_DETACH
3624  */
3625  return gdb_put_packet(connection, "OK", 2);
3626 }
3627 
3628 /* The format of 'F' response packet is
3629  * Fretcode,errno,Ctrl-C flag;call-specific attachment
3630  */
3632  char const *packet, int packet_size)
3633 {
3635  char *separator;
3636  char *parsing_point;
3637  int fileio_retcode = strtoul(packet + 1, &separator, 16);
3638  int fileio_errno = 0;
3639  bool fileio_ctrl_c = false;
3640  int retval;
3641 
3642  LOG_DEBUG("-");
3643 
3644  if (*separator == ',') {
3645  parsing_point = separator + 1;
3646  fileio_errno = strtoul(parsing_point, &separator, 16);
3647  if (*separator == ',') {
3648  if (*(separator + 1) == 'C') {
3649  /* TODO: process ctrl-c */
3650  fileio_ctrl_c = true;
3651  }
3652  }
3653  }
3654 
3655  LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3656  fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false");
3657 
3658  retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c);
3659  if (retval != ERROR_OK)
3660  return ERROR_FAIL;
3661 
3662  /* After File-I/O ends, keep continue or step */
3663  if (gdb_running_type == 'c')
3664  retval = target_resume(target, true, 0x0, false, false);
3665  else if (gdb_running_type == 's')
3666  retval = target_step(target, true, 0x0, false);
3667  else
3668  retval = ERROR_FAIL;
3669 
3670  if (retval != ERROR_OK)
3671  return ERROR_FAIL;
3672 
3673  return ERROR_OK;
3674 }
3675 
3676 static void gdb_log_callback(void *priv, const char *file, unsigned int line,
3677  const char *function, const char *string)
3678 {
3679  struct connection *connection = priv;
3680  struct gdb_connection *gdb_con = connection->priv;
3681 
3682  if (gdb_con->output_flag != GDB_OUTPUT_ALL)
3683  /* No out allowed */
3684  return;
3685 
3686  if (gdb_con->busy) {
3687  /* do not reply this using the O packet */
3688  return;
3689  }
3690 
3691  gdb_output_con(connection, string);
3692 }
3693 
3695 {
3696  char sig_reply[4];
3697  snprintf(sig_reply, 4, "T%2.2x", 2);
3698  gdb_put_packet(connection, sig_reply, 3);
3699 }
3700 
3702 {
3703  /* Do not allocate this on the stack */
3704  static char gdb_packet_buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for null-termination */
3705 
3706  struct target *target;
3707  char const *packet = gdb_packet_buffer;
3708  int packet_size;
3709  int retval;
3710  struct gdb_connection *gdb_con = connection->priv;
3711  static bool warn_use_ext;
3712 
3714 
3715  /* drain input buffer. If one of the packets fail, then an error
3716  * packet is replied, if applicable.
3717  *
3718  * This loop will terminate and the error code is returned.
3719  *
3720  * The calling fn will check if this error is something that
3721  * can be recovered from, or if the connection must be closed.
3722  *
3723  * If the error is recoverable, this fn is called again to
3724  * drain the rest of the buffer.
3725  */
3726  do {
3727  packet_size = GDB_BUFFER_SIZE;
3728  retval = gdb_get_packet(connection, gdb_packet_buffer, &packet_size);
3729  if (retval != ERROR_OK)
3730  return retval;
3731 
3732  /* terminate with zero */
3733  gdb_packet_buffer[packet_size] = '\0';
3734 
3735  if (packet_size > 0) {
3736 
3737  gdb_log_incoming_packet(connection, gdb_packet_buffer);
3738 
3739  retval = ERROR_OK;
3740  switch (packet[0]) {
3741  case 'T': /* Is thread alive? */
3742  gdb_thread_packet(connection, packet, packet_size);
3743  break;
3744  case 'H': /* Set current thread ( 'c' for step and continue,
3745  * 'g' for all other operations ) */
3746  gdb_thread_packet(connection, packet, packet_size);
3747  break;
3748  case 'q':
3749  case 'Q':
3750  retval = gdb_thread_packet(connection, packet, packet_size);
3751  if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
3752  retval = gdb_query_packet(connection, packet, packet_size);
3753  break;
3754  case 'g':
3755  retval = gdb_get_registers_packet(connection, packet, packet_size);
3756  break;
3757  case 'G':
3758  retval = gdb_set_registers_packet(connection, packet, packet_size);
3759  break;
3760  case 'p':
3761  retval = gdb_get_register_packet(connection, packet, packet_size);
3762  break;
3763  case 'P':
3764  retval = gdb_set_register_packet(connection, packet, packet_size);
3765  break;
3766  case 'm':
3767  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3768  retval = gdb_read_memory_packet(connection, packet, packet_size);
3769  gdb_con->output_flag = GDB_OUTPUT_NO;
3770  break;
3771  case 'M':
3772  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3773  retval = gdb_write_memory_packet(connection, packet, packet_size);
3774  gdb_con->output_flag = GDB_OUTPUT_NO;
3775  break;
3776  case 'z':
3777  case 'Z':
3778  retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
3779  break;
3780  case '?':
3781  gdb_last_signal_packet(connection, packet, packet_size);
3782  /* '?' is sent after the eventual '!' */
3783  if (!warn_use_ext && !gdb_con->extended_protocol) {
3784  warn_use_ext = true;
3785  LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3787  }
3788  break;
3789  case 'c':
3790  case 's':
3791  {
3792  gdb_thread_packet(connection, packet, packet_size);
3793  gdb_con->output_flag = GDB_OUTPUT_ALL;
3794 
3795  if (gdb_con->mem_write_error) {
3796  LOG_ERROR("Memory write failure!");
3797 
3798  /* now that we have reported the memory write error,
3799  * we can clear the condition */
3800  gdb_con->mem_write_error = false;
3801  }
3802 
3803  bool nostep = false;
3804  bool already_running = false;
3805  if (target->state == TARGET_RUNNING) {
3806  LOG_WARNING("WARNING! The target is already running. "
3807  "All changes GDB did to registers will be discarded! "
3808  "Waiting for target to halt.");
3809  already_running = true;
3810  } else if (target->state != TARGET_HALTED) {
3811  LOG_WARNING("The target is not in the halted nor running stated, "
3812  "stepi/continue ignored.");
3813  nostep = true;
3814  } else if ((packet[0] == 's') && gdb_con->sync) {
3815  /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3816  * sent by GDB first to OpenOCD, thus defeating the check to
3817  * make only the single stepping have the sync feature...
3818  */
3819  nostep = true;
3820  LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3821  "from the target.");
3822  }
3823  gdb_con->sync = false;
3824 
3825  if (!already_running && nostep) {
3826  /* Either the target isn't in the halted state, then we can't
3827  * step/continue. This might be early setup, etc.
3828  *
3829  * Or we want to allow GDB to pick up a fresh set of
3830  * register values without modifying the target state.
3831  *
3832  */
3834 
3835  /* stop forwarding log packets! */
3836  gdb_con->output_flag = GDB_OUTPUT_NO;
3837  } else {
3838  /* We're running/stepping, in which case we can
3839  * forward log output until the target is halted
3840  */
3841  gdb_con->frontend_state = TARGET_RUNNING;
3843 
3844  if (!already_running) {
3845  /* Here we don't want packet processing to stop even if this fails,
3846  * so we use a local variable instead of retval. */
3847  retval = gdb_step_continue_packet(connection, packet, packet_size);
3848  if (retval != ERROR_OK) {
3849  /* we'll never receive a halted
3850  * condition... issue a false one..
3851  */
3853  }
3854  }
3855  }
3856  }
3857  break;
3858  case 'v':
3859  retval = gdb_v_packet(connection, packet, packet_size);
3860  break;
3861  case 'D':
3862  retval = gdb_detach(connection);
3863  break;
3864  case 'X':
3865  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3866  retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
3867  gdb_con->output_flag = GDB_OUTPUT_NO;
3868  break;
3869  case 'x':
3870  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3871  retval = gdb_read_memory_packet_binary(connection, packet, packet_size);
3872  gdb_con->output_flag = GDB_OUTPUT_NO;
3873  break;
3874  case 'k':
3875  if (gdb_con->extended_protocol) {
3876  gdb_con->attached = false;
3877  break;
3878  }
3879  gdb_put_packet(connection, "OK", 2);
3881  case '!':
3882  /* handle extended remote protocol */
3883  gdb_con->extended_protocol = true;
3884  gdb_put_packet(connection, "OK", 2);
3885  break;
3886  case 'R':
3887  /* handle extended restart packet */
3888  gdb_restart_inferior(connection, packet, packet_size);
3889  break;
3890 
3891  case 'j':
3892  if (strncmp(packet, "jc", 2) == 0) {
3893  /* DEPRECATED */
3894  /* packet supported only by smp target i.e cortex_a.c*/
3895  /* handle smp packet replying coreid played to gbd */
3896  gdb_read_smp_packet(connection, packet, packet_size);
3897  } else {
3898  /* ignore unknown packets */
3899  LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3900  retval = gdb_put_packet(connection, "", 0);
3901  }
3902  break;
3903 
3904  case 'J':
3905  if (strncmp(packet, "jc", 2) == 0) {
3906  /* DEPRECATED */
3907  /* packet supported only by smp target i.e cortex_a.c */
3908  /* handle smp packet setting coreid to be played at next
3909  * resume to gdb */
3910  gdb_read_smp_packet(connection, packet, packet_size);
3911  } else {
3912  /* ignore unknown packets */
3913  LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3914  retval = gdb_put_packet(connection, "", 0);
3915  }
3916  break;
3917 
3918  case 'F':
3919  /* File-I/O extension */
3920  /* After gdb uses host-side syscall to complete target file
3921  * I/O, gdb sends host-side syscall return value to target
3922  * by 'F' packet.
3923  * The format of 'F' response packet is
3924  * Fretcode,errno,Ctrl-C flag;call-specific attachment
3925  */
3926  gdb_con->frontend_state = TARGET_RUNNING;
3927  gdb_con->output_flag = GDB_OUTPUT_ALL;
3928  gdb_fileio_response_packet(connection, packet, packet_size);
3929  break;
3930 
3931  default:
3932  /* ignore unknown packets */
3933  LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3934  gdb_put_packet(connection, "", 0);
3935  break;
3936  }
3937 
3938  /* if a packet handler returned an error, exit input loop */
3939  if (retval != ERROR_OK)
3940  return retval;
3941  }
3942 
3943  if (gdb_con->ctrl_c) {
3944  struct target *available_target = get_available_target_from_connection(connection);
3945  if (available_target->state == TARGET_RUNNING) {
3946  struct target *t = available_target;
3947  if (available_target->rtos)
3949  retval = target_halt(t);
3950  if (retval == ERROR_OK)
3951  retval = target_poll(t);
3952  if (retval != ERROR_OK)
3954  gdb_con->ctrl_c = false;
3955  } else {
3956  LOG_TARGET_INFO(target, "Not running when halt was requested, stopping GDB. (state=%d)",
3957  target->state);
3959  }
3960  }
3961 
3962  } while (gdb_con->buf_cnt > 0);
3963 
3964  return ERROR_OK;
3965 }
3966 
3967 static int gdb_input(struct connection *connection)
3968 {
3969  int retval = gdb_input_inner(connection);
3970  struct gdb_connection *gdb_con = connection->priv;
3971  if (retval == ERROR_SERVER_REMOTE_CLOSED)
3972  return retval;
3973 
3974  /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3975  if (gdb_con->closed)
3977 
3978  /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3979  return ERROR_OK;
3980 }
3981 
3982 /*
3983  * Send custom notification packet as keep-alive during memory read/write.
3984  *
3985  * From gdb 7.0 (released 2009-10-06) an unknown notification received during
3986  * memory read/write would be silently dropped.
3987  * Before gdb 7.0 any character, with exclusion of "+-$", would be considered
3988  * as junk and ignored.
3989  * In both cases the reception will reset the timeout counter in gdb, thus
3990  * working as a keep-alive.
3991  * Check putpkt_binary() and getpkt_sane() in gdb commit
3992  * 74531fed1f2d662debc2c209b8b3faddceb55960
3993  *
3994  * Enable remote debug in gdb with 'set debug remote 1' to either dump the junk
3995  * characters in gdb pre-7.0 and the notification from gdb 7.0.
3996  */
3998 {
3999  static unsigned char count;
4000  unsigned char checksum = 0;
4001  char buf[22];
4002 
4003  int len = sprintf(buf, "%%oocd_keepalive:%2.2x", count++);
4004  for (int i = 1; i < len; i++)
4005  checksum += buf[i];
4006  len += sprintf(buf + len, "#%2.2x", checksum);
4007 
4008 #ifdef _DEBUG_GDB_IO_
4009  LOG_DEBUG("sending packet '%s'", buf);
4010 #endif
4011 
4013  gdb_write(connection, buf, len);
4014 }
4015 
4017 {
4018  struct gdb_connection *gdb_con = connection->priv;
4019 
4020  switch (gdb_con->output_flag) {
4021  case GDB_OUTPUT_NO:
4022  /* no need for keep-alive */
4023  break;
4024  case GDB_OUTPUT_NOTIF:
4025  /* send asynchronous notification */
4027  break;
4028  case GDB_OUTPUT_ALL:
4029  /* send an empty O packet */
4031  break;
4032  default:
4033  break;
4034  }
4035 }
4036 
4037 static COMMAND_HELPER(gdb_service_info, const struct service *service)
4038 {
4039  struct gdb_service *gdb_service = service->priv;
4040 
4041  char *cmd_name = tcl_escape_alloc(CMD_CTX->interp, gdb_service->target->cmd_name);
4042  if (!cmd_name) {
4043  LOG_ERROR("Unable to escape Tcl string");
4044  return ERROR_FAIL;
4045  }
4046  command_print(cmd, " target %s", cmd_name);
4047  free(cmd_name);
4048  return ERROR_OK;
4049 }
4050 
4051 static const struct service_driver gdb_service_driver = {
4052  .name = "gdb",
4053  .new_connection_during_keep_alive_handler = NULL,
4054  .new_connection_handler = gdb_new_connection,
4055  .input_handler = gdb_input,
4056  .connection_closed_handler = gdb_connection_closed,
4057  .keep_client_alive_handler = gdb_keep_client_alive,
4058  .service_info_handler = gdb_service_info,
4059 };
4060 
4061 static int gdb_target_start(struct target *target, const char *port)
4062 {
4063  struct gdb_service *gdb_service = malloc(sizeof(struct gdb_service));
4064  if (!gdb_service) {
4065  LOG_ERROR("Out of memory");
4066  return ERROR_FAIL;
4067  }
4068 
4069  LOG_TARGET_INFO(target, "starting gdb server on %s", port);
4070 
4072  gdb_service->core[0] = -1;
4073  gdb_service->core[1] = -1;
4075 
4076  int retval = add_service(&gdb_service_driver, port,
4078  if (retval != ERROR_OK) {
4079  free(gdb_service);
4080  return retval;
4081  }
4082 
4083  /* initialize all targets gdb service with the same pointer */
4084  struct target_list *head;
4086  struct target *curr = head->target;
4087  if (curr != target)
4088  curr->gdb_service = gdb_service;
4089  }
4090 
4091  return ERROR_OK;
4092 }
4093 
4094 static int gdb_target_add_one(struct target *target)
4095 {
4096  /* one gdb instance per smp list */
4097  if ((target->smp) && (target->gdb_service))
4098  return ERROR_OK;
4099 
4100  /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
4102  LOG_TARGET_DEBUG(target, "skip gdb server");
4103  return ERROR_OK;
4104  }
4105 
4106  if (target->gdb_port_override) {
4107  if (strcmp(target->gdb_port_override, "disabled") == 0) {
4108  LOG_TARGET_INFO(target, "gdb port disabled");
4109  return ERROR_OK;
4110  }
4112  }
4113 
4114  if (strcmp(gdb_port_next, "disabled") == 0) {
4115  LOG_TARGET_INFO(target, "gdb port disabled");
4116  return ERROR_OK;
4117  }
4118 
4119  int retval = gdb_target_start(target, gdb_port_next);
4120  if (retval == ERROR_OK) {
4121  /* save the port number so can be queried with
4122  * $target_name cget -gdb-port
4123  */
4125 
4126  long portnumber;
4127  /* If we can parse the port number
4128  * then we increment the port number for the next target.
4129  */
4130  char *end;
4131  portnumber = strtol(gdb_port_next, &end, 0);
4132  if (!*end) {
4133  if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
4134  free(gdb_port_next);
4135  if (portnumber) {
4136  gdb_port_next = alloc_printf("%ld", portnumber+1);
4137  } else {
4138  /* Don't increment if gdb_port is 0, since we're just
4139  * trying to allocate an unused port. */
4140  gdb_port_next = strdup("0");
4141  }
4142  }
4143  } else if (strcmp(gdb_port_next, "pipe") == 0) {
4144  free(gdb_port_next);
4145  gdb_port_next = strdup("disabled");
4146  }
4147  }
4148  return retval;
4149 }
4150 
4152 {
4153  if (!target) {
4154  LOG_WARNING("gdb services need one or more targets defined");
4155  return ERROR_OK;
4156  }
4157 
4158  while (target) {
4159  int retval = gdb_target_add_one(target);
4160  if (retval != ERROR_OK)
4161  return retval;
4162 
4163  target = target->next;
4164  }
4165 
4166  return ERROR_OK;
4167 }
4168 
4169 COMMAND_HANDLER(handle_gdb_sync_command)
4170 {
4171  if (CMD_ARGC != 0)
4173 
4174  if (!current_gdb_connection) {
4176  "gdb sync command can only be run from within gdb using \"monitor gdb sync\"");
4177  return ERROR_FAIL;
4178  }
4179 
4180  current_gdb_connection->sync = true;
4181 
4182  return ERROR_OK;
4183 }
4184 
4185 COMMAND_HANDLER(handle_gdb_port_command)
4186 {
4187  int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
4188  if (retval == ERROR_OK) {
4189  free(gdb_port_next);
4190  gdb_port_next = strdup(gdb_port);
4191  }
4192  return retval;
4193 }
4194 
4195 COMMAND_HANDLER(handle_gdb_memory_map_command)
4196 {
4197  if (CMD_ARGC != 1)
4199 
4201  return ERROR_OK;
4202 }
4203 
4204 COMMAND_HANDLER(handle_gdb_flash_program_command)
4205 {
4206  if (CMD_ARGC != 1)
4208 
4210  return ERROR_OK;
4211 }
4212 
4213 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
4214 {
4215  if (CMD_ARGC != 1)
4217 
4219  return ERROR_OK;
4220 }
4221 
4222 COMMAND_HANDLER(handle_gdb_report_register_access_error)
4223 {
4224  if (CMD_ARGC != 1)
4226 
4228  return ERROR_OK;
4229 }
4230 
4231 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
4232 {
4233  if (CMD_ARGC == 0) {
4234  /* nothing */
4235  } else if (CMD_ARGC == 1) {
4237  if (strcmp(CMD_ARGV[0], "hard") == 0)
4239  else if (strcmp(CMD_ARGV[0], "soft") == 0)
4241  else if (strcmp(CMD_ARGV[0], "disable") == 0)
4243  } else
4246  LOG_USER("force %s breakpoints",
4247  (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
4248  else
4249  LOG_USER("breakpoint type is not overridden");
4250 
4251  return ERROR_OK;
4252 }
4253 
4254 COMMAND_HANDLER(handle_gdb_target_description_command)
4255 {
4256  if (CMD_ARGC != 1)
4258 
4260  return ERROR_OK;
4261 }
4262 
4263 COMMAND_HANDLER(handle_gdb_save_tdesc_command)
4264 {
4265  char *tdesc;
4266  uint32_t tdesc_length;
4268 
4269  int retval = gdb_generate_target_description(target, &tdesc);
4270  if (retval != ERROR_OK) {
4271  LOG_ERROR("Unable to Generate Target Description");
4272  return ERROR_FAIL;
4273  }
4274 
4275  tdesc_length = strlen(tdesc);
4276 
4277  struct fileio *fileio;
4278  size_t size_written;
4279 
4280  char *tdesc_filename = alloc_printf("%s.xml", target_type_name(target));
4281  if (!tdesc_filename) {
4282  retval = ERROR_FAIL;
4283  goto out;
4284  }
4285 
4286  retval = fileio_open(&fileio, tdesc_filename, FILEIO_WRITE, FILEIO_TEXT);
4287 
4288  if (retval != ERROR_OK) {
4289  LOG_ERROR("Can't open %s for writing", tdesc_filename);
4290  goto out;
4291  }
4292 
4293  retval = fileio_write(fileio, tdesc_length, tdesc, &size_written);
4294 
4296 
4297  if (retval != ERROR_OK)
4298  LOG_ERROR("Error while writing the tdesc file");
4299 
4300 out:
4301  free(tdesc_filename);
4302  free(tdesc);
4303 
4304  return retval;
4305 }
4306 
4307 static const struct command_registration gdb_subcommand_handlers[] = {
4308  {
4309  .name = "sync",
4310  .handler = handle_gdb_sync_command,
4311  .mode = COMMAND_ANY,
4312  .help = "next stepi will return immediately allowing "
4313  "GDB to fetch register state without affecting "
4314  "target state",
4315  .usage = ""
4316  },
4317  {
4318  .name = "port",
4319  .handler = handle_gdb_port_command,
4320  .mode = COMMAND_CONFIG,
4321  .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4322  "server listens for the next port number after the "
4323  "base port number specified. "
4324  "No arguments reports GDB port. \"pipe\" means listen to stdin "
4325  "output to stdout, an integer is base port number, \"disabled\" disables "
4326  "port. Any other string is are interpreted as named pipe to listen to. "
4327  "Output pipe is the same name as input pipe, but with 'o' appended.",
4328  .usage = "[port_num]",
4329  },
4330  {
4331  .name = "memory_map",
4332  .handler = handle_gdb_memory_map_command,
4333  .mode = COMMAND_CONFIG,
4334  .help = "enable or disable memory map",
4335  .usage = "('enable'|'disable')"
4336  },
4337  {
4338  .name = "flash_program",
4339  .handler = handle_gdb_flash_program_command,
4340  .mode = COMMAND_CONFIG,
4341  .help = "enable or disable flash program",
4342  .usage = "('enable'|'disable')"
4343  },
4344  {
4345  .name = "report_data_abort",
4346  .handler = handle_gdb_report_data_abort_command,
4347  .mode = COMMAND_CONFIG,
4348  .help = "enable or disable reporting data aborts",
4349  .usage = "('enable'|'disable')"
4350  },
4351  {
4352  .name = "report_register_access_error",
4353  .handler = handle_gdb_report_register_access_error,
4354  .mode = COMMAND_CONFIG,
4355  .help = "enable or disable reporting register access errors",
4356  .usage = "('enable'|'disable')"
4357  },
4358  {
4359  .name = "breakpoint_override",
4360  .handler = handle_gdb_breakpoint_override_command,
4361  .mode = COMMAND_ANY,
4362  .help = "Display or specify type of breakpoint "
4363  "to be used by gdb 'break' commands.",
4364  .usage = "('hard'|'soft'|'disable')"
4365  },
4366  {
4367  .name = "target_description",
4368  .handler = handle_gdb_target_description_command,
4369  .mode = COMMAND_CONFIG,
4370  .help = "enable or disable target description",
4371  .usage = "('enable'|'disable')"
4372  },
4373  {
4374  .name = "save_tdesc",
4375  .handler = handle_gdb_save_tdesc_command,
4376  .mode = COMMAND_EXEC,
4377  .help = "Save the target description file",
4378  .usage = "",
4379  },
4381 };
4382 
4383 static const struct command_registration gdb_command_handlers[] = {
4384  {
4385  .name = "gdb",
4386  .mode = COMMAND_ANY,
4387  .help = "GDB commands",
4388  .chain = gdb_subcommand_handlers,
4389  .usage = "",
4390  },
4392 };
4393 
4395 {
4396  gdb_port = strdup("3333");
4397  gdb_port_next = strdup("3333");
4398  return register_commands(cmd_ctx, NULL, gdb_command_handlers);
4399 }
4400 
4402 {
4403  free(gdb_port);
4404  free(gdb_port_next);
4405 }
4406 
4408 {
4409  return gdb_actual_connections;
4410 }
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:551
int breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:346
int watchpoint_hit(struct target *target, enum watchpoint_rw *rw, target_addr_t *address)
Definition: breakpoints.c:627
int watchpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:588
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:216
int watchpoint_remove_all(struct target *target)
Definition: breakpoints.c:467
int breakpoint_remove_all(struct target *target)
Definition: breakpoints.c:462
breakpoint_type
Definition: breakpoints.h:17
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
#define WATCHPOINT_IGNORE_DATA_VALUE_MASK
Definition: breakpoints.h:39
watchpoint_rw
Definition: breakpoints.h:22
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
int command_run_linef(struct command_context *context, const char *format,...)
Definition: command.c:553
void command_set_output_handler(struct command_context *context, command_output_handler_t output_handler, void *priv)
Definition: command.c:568
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define PRINTF_ATTRIBUTE_FORMAT
Definition: command.h:27
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
int parse_long(const char *str, long *ul)
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:536
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint32_t sector_size
Sector size.
Definition: dw-spi-helper.h:1
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
enum esirisc_reg_num number
Definition: esirisc.c:87
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
uint8_t length
Definition: esp_usb_jtag.c:1
#define ERROR_FLASH_DST_OUT_OF_BANK
Definition: flash/common.h:31
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
unsigned int flash_get_bank_count(void)
int flash_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length)
Erases length bytes in the target flash, starting at addr.
int flash_write(struct target *target, struct image *image, uint32_t *written, bool erase)
Writes image into the target flash.
int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
Returns the flash bank like get_flash_bank_by_name(), without probing.
static int gdb_read_memory_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c: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:4061
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:3997
static int gdb_v_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:3449
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:4051
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:3701
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:4094
static void gdb_sig_halted(struct connection *connection)
Definition: gdb_server.c:3694
static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
Definition: gdb_server.c:3407
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:2825
static struct gdb_connection * current_gdb_connection
Definition: gdb_server.c:113
COMMAND_HANDLER(handle_gdb_sync_command)
Definition: gdb_server.c:4169
static int gdb_detach(struct connection *connection)
Definition: gdb_server.c:3617
static int compare_bank(const void *a, const void *b)
Definition: gdb_server.c:2051
#define CTRL(c)
Definition: gdb_server.c:54
int gdb_register_commands(struct command_context *cmd_ctx)
Definition: gdb_server.c:4394
static void gdb_keep_client_alive(struct connection *connection)
Definition: gdb_server.c:4016
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:2924
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:2881
static char * next_hex_encoded_field(const char **str, char sep)
Definition: gdb_server.c:3357
static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
Definition: gdb_server.c:3392
static COMMAND_HELPER(gdb_service_info, const struct service *service)
Definition: gdb_server.c:4037
int gdb_target_add_all(struct target *target)
Definition: gdb_server.c:4151
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:2297
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:2024
static int gdb_fileio_response_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:3631
static int gdb_memory_map(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:2065
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:4383
static int gdb_input(struct connection *connection)
Definition: gdb_server.c:3967
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:2431
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:2726
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:4407
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:4401
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:3170
static int gdb_putback_char(struct connection *connection, int last_char)
Definition: gdb_server.c:348
static enum breakpoint_type gdb_breakpoint_override_type
Definition: gdb_server.c:116
static int gdb_write_memory_binary_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c: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:2474
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:2780
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:2586
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:2227
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:3676
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:2273
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:4307
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:6941
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