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