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  struct rtos *rtos;
817 
819  if (rtos) {
822  } else {
823  ct = target;
824  }
825 
826  if (gdb_connection->ctrl_c) {
827  LOG_TARGET_DEBUG(target, "Responding with signal 2 (SIGINT) to debugger due to Ctrl-C");
828  signal_var = 0x2;
829  } else
830  signal_var = gdb_last_signal(ct);
831 
832  stop_reason[0] = '\0';
833  if (ct->debug_reason == DBG_REASON_WATCHPOINT) {
834  enum watchpoint_rw hit_wp_type;
835  target_addr_t hit_wp_address;
836 
837  if (watchpoint_hit(ct, &hit_wp_type, &hit_wp_address) == ERROR_OK) {
838 
839  switch (hit_wp_type) {
840  case WPT_WRITE:
841  snprintf(stop_reason, sizeof(stop_reason),
842  "watch:%08" TARGET_PRIxADDR ";", hit_wp_address);
843  break;
844  case WPT_READ:
845  snprintf(stop_reason, sizeof(stop_reason),
846  "rwatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
847  break;
848  case WPT_ACCESS:
849  snprintf(stop_reason, sizeof(stop_reason),
850  "awatch:%08" TARGET_PRIxADDR ";", hit_wp_address);
851  break;
852  default:
853  break;
854  }
855  }
856  }
857 
858  current_thread[0] = '\0';
859  if (rtos)
860  snprintf(current_thread, sizeof(current_thread), "thread:%" PRIx64 ";",
862 
863  sig_reply_len = snprintf(sig_reply, sizeof(sig_reply), "T%2.2x%s%s",
864  signal_var, stop_reason, current_thread);
865 
866  gdb_connection->ctrl_c = false;
867  }
868 
869  gdb_put_packet(connection, sig_reply, sig_reply_len);
871 }
872 
873 static void gdb_fileio_reply(struct target *target, struct connection *connection)
874 {
876  char fileio_command[256];
877  int command_len;
878  bool program_exited = false;
879 
880  if (strcmp(target->fileio_info->identifier, "open") == 0)
881  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
883  target->fileio_info->param_2 + 1, /* len + trailing zero */
886  else if (strcmp(target->fileio_info->identifier, "close") == 0)
887  sprintf(fileio_command, "F%s,%" PRIx64, target->fileio_info->identifier,
889  else if (strcmp(target->fileio_info->identifier, "read") == 0)
890  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
894  else if (strcmp(target->fileio_info->identifier, "write") == 0)
895  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
899  else if (strcmp(target->fileio_info->identifier, "lseek") == 0)
900  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
904  else if (strcmp(target->fileio_info->identifier, "rename") == 0)
905  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
907  target->fileio_info->param_2 + 1, /* len + trailing zero */
909  target->fileio_info->param_4 + 1); /* len + trailing zero */
910  else if (strcmp(target->fileio_info->identifier, "unlink") == 0)
911  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
913  target->fileio_info->param_2 + 1); /* len + trailing zero */
914  else if (strcmp(target->fileio_info->identifier, "stat") == 0)
915  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
919  else if (strcmp(target->fileio_info->identifier, "fstat") == 0)
920  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
923  else if (strcmp(target->fileio_info->identifier, "gettimeofday") == 0)
924  sprintf(fileio_command, "F%s,%" PRIx64 ",%" PRIx64, target->fileio_info->identifier,
927  else if (strcmp(target->fileio_info->identifier, "isatty") == 0)
928  sprintf(fileio_command, "F%s,%" PRIx64, target->fileio_info->identifier,
930  else if (strcmp(target->fileio_info->identifier, "system") == 0)
931  sprintf(fileio_command, "F%s,%" PRIx64 "/%" PRIx64, target->fileio_info->identifier,
933  target->fileio_info->param_2 + 1); /* len + trailing zero */
934  else if (strcmp(target->fileio_info->identifier, "exit") == 0) {
935  /* If target hits exit syscall, report to GDB the program is terminated.
936  * In addition, let target run its own exit syscall handler. */
937  program_exited = true;
938  sprintf(fileio_command, "W%02" PRIx64, target->fileio_info->param_1);
939  } else {
940  LOG_DEBUG("Unknown syscall: %s", target->fileio_info->identifier);
941 
942  /* encounter unknown syscall, continue */
944  target_resume(target, true, 0x0, false, false);
945  return;
946  }
947 
948  command_len = strlen(fileio_command);
949  gdb_put_packet(connection, fileio_command, command_len);
950 
951  if (program_exited) {
952  /* Use target_resume() to let target run its own exit syscall handler. */
954  target_resume(target, true, 0x0, false, false);
955  } else {
958  }
959 }
960 
962 {
964 
965  /* In the GDB protocol when we are stepping or continuing execution,
966  * we have a lingering reply. Upon receiving a halted event
967  * when we have that lingering packet, we reply to the original
968  * step or continue packet.
969  *
970  * Executing monitor commands can bring the target in and
971  * out of the running state so we'll see lots of TARGET_EVENT_XXX
972  * that are to be ignored.
973  */
975  /* stop forwarding log packets! */
977 
978  /* check fileio first */
981  else
983  }
984 }
985 
987  enum target_event event, void *priv)
988 {
989  struct connection *connection = priv;
991 
992  if (gdb_target != target)
993  return ERROR_OK;
994 
995  switch (event) {
998  break;
999  case TARGET_EVENT_HALTED:
1001  break;
1002  default:
1003  break;
1004  }
1005 
1006  return ERROR_OK;
1007 }
1008 
1010 {
1011  struct gdb_connection *gdb_connection = malloc(sizeof(struct gdb_connection));
1012  struct target *target;
1013  int retval;
1014  int initial_ack;
1015  static unsigned int next_unique_id = 1;
1016 
1020 
1021  /* initialize gdb connection information */
1023  gdb_connection->buf_cnt = 0;
1024  gdb_connection->ctrl_c = false;
1027  gdb_connection->closed = false;
1028  gdb_connection->busy = false;
1030  gdb_connection->sync = false;
1032  gdb_connection->attached = true;
1038  gdb_connection->unique_index = next_unique_id++;
1039 
1040  /* output goes through gdb connection */
1042 
1043  /* we must remove all breakpoints registered to the target as a previous
1044  * GDB session could leave dangling breakpoints if e.g. communication
1045  * timed out.
1046  */
1049 
1050  /* Since version 3.95 (gdb-19990504), with the exclusion of 6.5~6.8, GDB
1051  * sends an ACK at connection with the following comment in its source code:
1052  * "Ack any packet which the remote side has already sent."
1053  * LLDB does the same since the first gdb-remote implementation.
1054  * Remove the initial ACK from the incoming buffer.
1055  */
1056  retval = gdb_get_char(connection, &initial_ack);
1057  if (retval != ERROR_OK)
1058  return retval;
1059 
1060  if (initial_ack != '+')
1061  gdb_putback_char(connection, initial_ack);
1062 
1064 
1065  if (target->rtos) {
1066  /* clean previous rtos session if supported*/
1067  if (target->rtos->type->clean)
1068  target->rtos->type->clean(target);
1069 
1070  /* update threads */
1072  }
1073 
1074  if (gdb_use_memory_map) {
1075  /* Connect must fail if the memory map can't be set up correctly.
1076  *
1077  * This will cause an auto_probe to be invoked, which is either
1078  * a no-op or it will fail when the target isn't ready(e.g. not halted).
1079  */
1080  for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
1081  struct flash_bank *p;
1083  if (p->target != target)
1084  continue;
1085  retval = get_flash_bank_by_num(i, &p);
1086  if (retval != ERROR_OK) {
1087  LOG_ERROR("Connect failed. Consider setting up a gdb-attach event for the target "
1088  "to prepare target for GDB connect, or use 'gdb_memory_map disable'.");
1089  return retval;
1090  }
1091  }
1092  }
1093 
1095  __FILE__, __LINE__, __func__,
1096  "New GDB Connection: %d, Target %s, state: %s",
1100 
1101  if (!target_was_examined(target)) {
1102  LOG_TARGET_ERROR(target, "Target not examined yet, refuse gdb connection %d!",
1105  }
1107 
1108  if (target->state != TARGET_HALTED)
1109  LOG_TARGET_WARNING(target, "GDB connection %d not halted",
1111 
1112  /* DANGER! If we fail subsequently, we must remove this handler,
1113  * otherwise we occasionally see crashes as the timer can invoke the
1114  * callback fn.
1115  *
1116  * register callback to be informed about target events */
1118 
1120 
1121  return ERROR_OK;
1122 }
1123 
1125 {
1126  struct target *target;
1128 
1130 
1131  /* we're done forwarding messages. Tear down callback before
1132  * cleaning up connection.
1133  */
1135 
1137  LOG_TARGET_DEBUG(target, "{%d} GDB Close, state: %s, gdb_actual_connections=%d",
1141 
1142  /* see if an image built with vFlash commands is left */
1147  }
1148 
1149  /* if this connection registered a debug-message receiver delete it */
1151 
1152  free(connection->priv);
1153  connection->priv = NULL;
1154 
1156 
1158 
1160 
1161  return ERROR_OK;
1162 }
1163 
1164 static void gdb_send_error(struct connection *connection, uint8_t the_error)
1165 {
1166  char err[4];
1167  snprintf(err, 4, "E%2.2X", the_error);
1168  gdb_put_packet(connection, err, 3);
1169 }
1170 
1172  char const *packet, int packet_size)
1173 {
1175  struct gdb_connection *gdb_con = connection->priv;
1176  char sig_reply[4];
1177  int signal_var;
1178 
1179  if (!gdb_con->attached) {
1180  /* if we are here we have received a kill packet
1181  * reply W stop reply otherwise gdb gets very unhappy */
1182  gdb_put_packet(connection, "W00", 3);
1183  return ERROR_OK;
1184  }
1185 
1186  signal_var = gdb_last_signal(target);
1187 
1188  snprintf(sig_reply, 4, "S%2.2x", signal_var);
1189  gdb_put_packet(connection, sig_reply, 3);
1190 
1191  return ERROR_OK;
1192 }
1193 
1194 static inline int gdb_reg_pos(struct target *target, int pos, int len)
1195 {
1197  return pos;
1198  else
1199  return len - 1 - pos;
1200 }
1201 
1202 /* Convert register to string of bytes. NB! The # of bits in the
1203  * register might be non-divisible by 8(a byte), in which
1204  * case an entire byte is shown.
1205  *
1206  * NB! the format on the wire is the target endianness
1207  *
1208  * The format of reg->value is little endian
1209  *
1210  */
1211 static void gdb_str_to_target(struct target *target,
1212  char *tstr, struct reg *reg)
1213 {
1214  int i;
1215 
1216  uint8_t *buf;
1217  int buf_len;
1218  buf = reg->value;
1219  buf_len = DIV_ROUND_UP(reg->size, 8);
1220 
1221  for (i = 0; i < buf_len; i++) {
1222  int j = gdb_reg_pos(target, i, buf_len);
1223  tstr += sprintf(tstr, "%02x", buf[j]);
1224  }
1225 }
1226 
1227 /* copy over in register buffer */
1228 static void gdb_target_to_reg(struct target *target,
1229  char const *tstr, int str_len, uint8_t *bin)
1230 {
1231  if (str_len % 2) {
1232  LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1233  exit(-1);
1234  }
1235 
1236  int i;
1237  for (i = 0; i < str_len; i += 2) {
1238  unsigned int t;
1239  if (sscanf(tstr + i, "%02x", &t) != 1) {
1240  LOG_ERROR("BUG: unable to convert register value");
1241  exit(-1);
1242  }
1243 
1244  int j = gdb_reg_pos(target, i/2, str_len/2);
1245  bin[j] = t;
1246  }
1247 }
1248 
1249 /* get register value if needed and fill the buffer accordingly */
1250 static int gdb_get_reg_value_as_str(struct target *target, char *tstr, struct reg *reg)
1251 {
1252  int retval = ERROR_OK;
1253 
1254  if (!reg->valid)
1255  retval = reg->type->get(reg);
1256 
1257  const unsigned int len = DIV_ROUND_UP(reg->size, 8) * 2;
1258  switch (retval) {
1259  case ERROR_OK:
1260  gdb_str_to_target(target, tstr, reg);
1261  return ERROR_OK;
1263  memset(tstr, 'x', len);
1264  tstr[len] = '\0';
1265  return ERROR_OK;
1266  }
1267  memset(tstr, '0', len);
1268  tstr[len] = '\0';
1269  return ERROR_FAIL;
1270 }
1271 
1273  char const *packet, int packet_size)
1274 {
1276  struct reg **reg_list;
1277  int reg_list_size;
1278  int retval;
1279  int reg_packet_size = 0;
1280  char *reg_packet;
1281  char *reg_packet_p;
1282  int i;
1283 
1284 #ifdef _DEBUG_GDB_IO_
1285  LOG_DEBUG("-");
1286 #endif
1287 
1289  return ERROR_OK;
1290 
1291  retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1293  if (retval != ERROR_OK)
1294  return gdb_error(connection, retval);
1295 
1296  for (i = 0; i < reg_list_size; i++) {
1297  if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden)
1298  continue;
1299  reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1300  }
1301 
1302  assert(reg_packet_size > 0);
1303 
1304  reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
1305  if (!reg_packet)
1306  return ERROR_FAIL;
1307 
1308  reg_packet_p = reg_packet;
1309 
1310  for (i = 0; i < reg_list_size; i++) {
1311  if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden)
1312  continue;
1313  retval = gdb_get_reg_value_as_str(target, reg_packet_p, reg_list[i]);
1314  if (retval != ERROR_OK && gdb_report_register_access_error) {
1315  LOG_DEBUG("Couldn't get register %s.", reg_list[i]->name);
1316  free(reg_packet);
1317  free(reg_list);
1318  return gdb_error(connection, retval);
1319  }
1320  reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1321  }
1322 
1323 #ifdef _DEBUG_GDB_IO_
1324  {
1325  char *reg_packet_p_debug;
1326  reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1327  LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1328  free(reg_packet_p_debug);
1329  }
1330 #endif
1331 
1332  gdb_put_packet(connection, reg_packet, reg_packet_size);
1333  free(reg_packet);
1334 
1335  free(reg_list);
1336 
1337  return ERROR_OK;
1338 }
1339 
1341  char const *packet, int packet_size)
1342 {
1344  int i;
1345  struct reg **reg_list;
1346  int reg_list_size;
1347  int retval;
1348  char const *packet_p;
1349 
1350 #ifdef _DEBUG_GDB_IO_
1351  LOG_DEBUG("-");
1352 #endif
1353 
1354  /* skip command character */
1355  packet++;
1356  packet_size--;
1357 
1358  if (packet_size % 2) {
1359  LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1361  }
1362 
1363  retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1365  if (retval != ERROR_OK)
1366  return gdb_error(connection, retval);
1367 
1368  packet_p = packet;
1369  for (i = 0; i < reg_list_size; i++) {
1370  uint8_t *bin_buf;
1371  if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden)
1372  continue;
1373  int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1374 
1375  if (packet_p + chars > packet + packet_size)
1376  LOG_ERROR("BUG: register packet is too small for registers");
1377 
1378  bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1379  gdb_target_to_reg(target, packet_p, chars, bin_buf);
1380 
1381  retval = reg_list[i]->type->set(reg_list[i], bin_buf);
1382  if (retval != ERROR_OK && gdb_report_register_access_error) {
1383  LOG_DEBUG("Couldn't set register %s.", reg_list[i]->name);
1384  free(reg_list);
1385  free(bin_buf);
1386  return gdb_error(connection, retval);
1387  }
1388 
1389  /* advance packet pointer */
1390  packet_p += chars;
1391 
1392  free(bin_buf);
1393  }
1394 
1395  /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1396  free(reg_list);
1397 
1398  gdb_put_packet(connection, "OK", 2);
1399 
1400  return ERROR_OK;
1401 }
1402 
1404  char const *packet, int packet_size)
1405 {
1407  char *reg_packet;
1408  int reg_num = strtoul(packet + 1, NULL, 16);
1409  struct reg **reg_list;
1410  int reg_list_size;
1411  int retval;
1412 
1413 #ifdef _DEBUG_GDB_IO_
1414  LOG_DEBUG("-");
1415 #endif
1416 
1417  if (target->rtos) {
1418  retval = rtos_get_gdb_reg(connection, reg_num);
1419  if (retval == ERROR_OK)
1420  return ERROR_OK;
1421  if (retval != ERROR_NOT_IMPLEMENTED)
1422  return gdb_error(connection, retval);
1423  }
1424 
1425  retval = target_get_gdb_reg_list_noread(target, &reg_list, &reg_list_size,
1426  REG_CLASS_ALL);
1427  if (retval != ERROR_OK)
1428  return gdb_error(connection, retval);
1429 
1430  if ((reg_list_size <= reg_num) || !reg_list[reg_num] ||
1431  !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) {
1432  LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num);
1434  }
1435 
1436  reg_packet = calloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1, 1); /* plus one for string termination null */
1437 
1438  retval = gdb_get_reg_value_as_str(target, reg_packet, reg_list[reg_num]);
1439  if (retval != ERROR_OK && gdb_report_register_access_error) {
1440  LOG_DEBUG("Couldn't get register %s.", reg_list[reg_num]->name);
1441  free(reg_packet);
1442  free(reg_list);
1443  return gdb_error(connection, retval);
1444  }
1445 
1446  gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1447 
1448  free(reg_list);
1449  free(reg_packet);
1450 
1451  return ERROR_OK;
1452 }
1453 
1455  char const *packet, int packet_size)
1456 {
1458  char *separator;
1459  int reg_num = strtoul(packet + 1, &separator, 16);
1460  struct reg **reg_list;
1461  int reg_list_size;
1462  int retval;
1463 
1464 #ifdef _DEBUG_GDB_IO_
1465  LOG_DEBUG("-");
1466 #endif
1467 
1468  if (*separator != '=') {
1469  LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1471  }
1472  size_t chars = strlen(separator + 1);
1473  uint8_t *bin_buf = malloc(chars / 2);
1474  gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1475 
1476  if ((target->rtos) &&
1477  (rtos_set_reg(connection, reg_num, bin_buf) == ERROR_OK)) {
1478  free(bin_buf);
1479  gdb_put_packet(connection, "OK", 2);
1480  return ERROR_OK;
1481  }
1482 
1483  retval = target_get_gdb_reg_list_noread(target, &reg_list, &reg_list_size,
1484  REG_CLASS_ALL);
1485  if (retval != ERROR_OK) {
1486  free(bin_buf);
1487  return gdb_error(connection, retval);
1488  }
1489 
1490  if ((reg_list_size <= reg_num) || !reg_list[reg_num] ||
1491  !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) {
1492  LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num);
1493  free(bin_buf);
1494  free(reg_list);
1496  }
1497 
1498  if (chars != (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2)) {
1499  LOG_ERROR("gdb sent %zu bits for a %" PRIu32 "-bit register (%s)",
1500  chars * 4, reg_list[reg_num]->size, reg_list[reg_num]->name);
1501  free(bin_buf);
1502  free(reg_list);
1504  }
1505 
1506  gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1507 
1508  retval = reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1509  if (retval != ERROR_OK && gdb_report_register_access_error) {
1510  LOG_DEBUG("Couldn't set register %s.", reg_list[reg_num]->name);
1511  free(bin_buf);
1512  free(reg_list);
1513  return gdb_error(connection, retval);
1514  }
1515 
1516  gdb_put_packet(connection, "OK", 2);
1517 
1518  free(bin_buf);
1519  free(reg_list);
1520 
1521  return ERROR_OK;
1522 }
1523 
1524 /* No attempt is made to translate the "retval" to
1525  * GDB speak. This has to be done at the calling
1526  * site as no mapping really exists.
1527  */
1528 static int gdb_error(struct connection *connection, int retval)
1529 {
1530  LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1531  gdb_send_error(connection, EFAULT);
1532  return ERROR_OK;
1533 }
1534 
1536  char const *packet, int packet_size)
1537 {
1539  char *separator;
1540  uint64_t addr = 0;
1541  uint32_t len = 0;
1542 
1543  uint8_t *buffer;
1544  char *hex_buffer;
1545 
1546  int retval = ERROR_OK;
1547 
1548  /* skip command character */
1549  packet++;
1550 
1551  addr = strtoull(packet, &separator, 16);
1552 
1553  if (*separator != ',') {
1554  LOG_ERROR("incomplete read memory packet received, dropping connection");
1556  }
1557 
1558  len = strtoul(separator + 1, NULL, 16);
1559 
1560  if (!len) {
1561  LOG_WARNING("invalid read memory packet received (len == 0)");
1562  gdb_put_packet(connection, "", 0);
1563  return ERROR_OK;
1564  }
1565 
1566  buffer = malloc(len);
1567 
1568  LOG_DEBUG("addr: 0x%16.16" PRIx64 ", len: 0x%8.8" PRIx32, addr, len);
1569 
1570  retval = ERROR_NOT_IMPLEMENTED;
1571  if (target->rtos)
1572  retval = rtos_read_buffer(target, addr, len, buffer);
1573  if (retval == ERROR_NOT_IMPLEMENTED)
1574  retval = target_read_buffer(target, addr, len, buffer);
1575 
1576  if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1577  /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1578  * At some point this might be fixed in GDB, in which case this code can be removed.
1579  *
1580  * OpenOCD developers are acutely aware of this problem, but there is nothing
1581  * gained by involving the user in this problem that hopefully will get resolved
1582  * eventually
1583  *
1584  * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1585  * cmd = view%20audit-trail&database = gdb&pr = 2395
1586  *
1587  * For now, the default is to fix up things to make current GDB versions work.
1588  * This can be overwritten using the "gdb report_data_abort <'enable'|'disable'>" command.
1589  */
1590  memset(buffer, 0, len);
1591  retval = ERROR_OK;
1592  }
1593 
1594  if (retval == ERROR_OK) {
1595  hex_buffer = malloc(len * 2 + 1);
1596 
1597  size_t pkt_len = hexify(hex_buffer, buffer, len, len * 2 + 1);
1598 
1599  gdb_put_packet(connection, hex_buffer, pkt_len);
1600 
1601  free(hex_buffer);
1602  } else
1603  retval = gdb_error(connection, retval);
1604 
1605  free(buffer);
1606 
1607  return retval;
1608 }
1609 
1611  char const *packet, int packet_size)
1612 {
1614  char *separator;
1615  uint64_t addr = 0;
1616  uint32_t len = 0;
1617 
1618  uint8_t *buffer;
1619  int retval;
1620 
1621  /* skip command character */
1622  packet++;
1623 
1624  addr = strtoull(packet, &separator, 16);
1625 
1626  if (*separator != ',') {
1627  LOG_ERROR("incomplete write memory packet received, dropping connection");
1629  }
1630 
1631  len = strtoul(separator + 1, &separator, 16);
1632 
1633  if (*(separator++) != ':') {
1634  LOG_ERROR("incomplete write memory packet received, dropping connection");
1636  }
1637 
1638  buffer = malloc(len);
1639 
1640  LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32, addr, len);
1641 
1642  if (unhexify(buffer, separator, len) != len)
1643  LOG_ERROR("unable to decode memory packet");
1644 
1645  retval = ERROR_NOT_IMPLEMENTED;
1646  if (target->rtos)
1647  retval = rtos_write_buffer(target, addr, len, buffer);
1648  if (retval == ERROR_NOT_IMPLEMENTED)
1649  retval = target_write_buffer(target, addr, len, buffer);
1650 
1651  if (retval == ERROR_OK)
1652  gdb_put_packet(connection, "OK", 2);
1653  else
1654  retval = gdb_error(connection, retval);
1655 
1656  free(buffer);
1657 
1658  return retval;
1659 }
1660 
1662  char const *packet, int packet_size)
1663 {
1665  char *separator;
1666  uint64_t addr = 0;
1667  uint32_t len = 0;
1668 
1669  int retval = ERROR_OK;
1670  /* Packets larger than fast_limit bytes will be acknowledged instantly on
1671  * the assumption that we're in a download and it's important to go as fast
1672  * as possible. */
1673  uint32_t fast_limit = 8;
1674 
1675  /* skip command character */
1676  packet++;
1677 
1678  addr = strtoull(packet, &separator, 16);
1679 
1680  if (*separator != ',') {
1681  LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1683  }
1684 
1685  len = strtoul(separator + 1, &separator, 16);
1686 
1687  if (*(separator++) != ':') {
1688  LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1690  }
1691 
1693 
1695  retval = ERROR_FAIL;
1696 
1697  if (retval == ERROR_OK) {
1698  if (len >= fast_limit) {
1699  /* By replying the packet *immediately* GDB will send us a new packet
1700  * while we write the last one to the target.
1701  * We only do this for larger writes, so that users who do something like:
1702  * p *((int*)0xdeadbeef)=8675309
1703  * will get immediate feedback that that write failed.
1704  */
1705  gdb_put_packet(connection, "OK", 2);
1706  }
1707  } else {
1708  retval = gdb_error(connection, retval);
1709  /* now that we have reported the memory write error, we can clear the condition */
1711  if (retval != ERROR_OK)
1712  return retval;
1713  }
1714 
1715  if (len) {
1716  LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32, addr, len);
1717 
1718  retval = ERROR_NOT_IMPLEMENTED;
1719  if (target->rtos)
1720  retval = rtos_write_buffer(target, addr, len, (uint8_t *)separator);
1721  if (retval == ERROR_NOT_IMPLEMENTED)
1722  retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1723 
1724  if (retval != ERROR_OK)
1726  }
1727 
1728  if (len < fast_limit) {
1729  if (retval != ERROR_OK) {
1730  gdb_error(connection, retval);
1732  } else {
1733  gdb_put_packet(connection, "OK", 2);
1734  }
1735  }
1736 
1737  return ERROR_OK;
1738 }
1739 
1741  char const *packet, int packet_size)
1742 {
1744  bool current = false;
1745  uint64_t address = 0x0;
1746  int retval = ERROR_OK;
1747 
1748  LOG_DEBUG("-");
1749 
1750  if (packet_size > 1)
1751  address = strtoull(packet + 1, NULL, 16);
1752  else
1753  current = true;
1754 
1755  gdb_running_type = packet[0];
1756  if (packet[0] == 'c') {
1757  LOG_DEBUG("continue");
1758  /* resume at current address, don't handle breakpoints, not debugging */
1759  retval = target_resume(target, current, address, false, false);
1760  } else if (packet[0] == 's') {
1761  LOG_DEBUG("step");
1762  /* step at current or address, don't handle breakpoints */
1763  retval = target_step(target, current, address, false);
1764  }
1765  return retval;
1766 }
1767 
1769  char const *packet, int packet_size)
1770 {
1772  int type;
1773  enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1774  enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1775  uint64_t address;
1776  uint32_t size;
1777  char *separator;
1778  int retval;
1779 
1780  LOG_DEBUG("[%s]", target_name(target));
1781 
1782  type = strtoul(packet + 1, &separator, 16);
1783 
1784  if (type == 0) /* memory breakpoint */
1785  bp_type = BKPT_SOFT;
1786  else if (type == 1) /* hardware breakpoint */
1787  bp_type = BKPT_HARD;
1788  else if (type == 2) /* write watchpoint */
1789  wp_type = WPT_WRITE;
1790  else if (type == 3) /* read watchpoint */
1791  wp_type = WPT_READ;
1792  else if (type == 4) /* access watchpoint */
1793  wp_type = WPT_ACCESS;
1794  else {
1795  LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1797  }
1798 
1799  if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1800  bp_type = gdb_breakpoint_override_type;
1801 
1802  if (*separator != ',') {
1803  LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1805  }
1806 
1807  address = strtoull(separator + 1, &separator, 16);
1808 
1809  if (*separator != ',') {
1810  LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1812  }
1813 
1814  size = strtoul(separator + 1, &separator, 16);
1815 
1816  switch (type) {
1817  case 0:
1818  case 1:
1819  if (packet[0] == 'Z') {
1820  struct target *bp_target = target;
1821  if (target->rtos && bp_type == BKPT_SOFT) {
1822  bp_target = rtos_swbp_target(target, address, size, bp_type);
1823  if (!bp_target) {
1824  retval = ERROR_FAIL;
1825  break;
1826  }
1827  }
1828  retval = breakpoint_add(bp_target, address, size, bp_type);
1829  } else {
1830  assert(packet[0] == 'z');
1831  retval = breakpoint_remove(target, address);
1832  }
1833  break;
1834  case 2:
1835  case 3:
1836  case 4:
1837  {
1838  if (packet[0] == 'Z') {
1840  } else {
1841  assert(packet[0] == 'z');
1842  retval = watchpoint_remove(target, address);
1843  }
1844  break;
1845  }
1846  default:
1847  {
1848  retval = ERROR_NOT_IMPLEMENTED;
1849  break;
1850  }
1851  }
1852 
1853  if (retval == ERROR_NOT_IMPLEMENTED) {
1854  /* Send empty reply to report that watchpoints of this type are not supported */
1855  return gdb_put_packet(connection, "", 0);
1856  }
1857  if (retval != ERROR_OK)
1858  return gdb_error(connection, retval);
1859  return gdb_put_packet(connection, "OK", 2);
1860 }
1861 
1862 /* print out a string and allocate more space as needed,
1863  * mainly used for XML at this point
1864  */
1865 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6))) void xml_printf(int *retval,
1866  char **xml, int *pos, int *size, const char *fmt, ...)
1867 {
1868  if (*retval != ERROR_OK)
1869  return;
1870  int first = 1;
1871 
1872  for (;; ) {
1873  if ((!*xml) || (!first)) {
1874  /* start by 0 to exercise all the code paths.
1875  * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1876 
1877  *size = *size * 2 + 2;
1878  char *t = *xml;
1879  *xml = realloc(*xml, *size);
1880  if (!*xml) {
1881  free(t);
1882  *retval = ERROR_SERVER_REMOTE_CLOSED;
1883  return;
1884  }
1885  }
1886 
1887  va_list ap;
1888  int ret;
1889  va_start(ap, fmt);
1890  ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1891  va_end(ap);
1892  if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1893  *pos += ret;
1894  return;
1895  }
1896  /* there was just enough or not enough space, allocate more. */
1897  first = 0;
1898  }
1899 }
1900 
1901 static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
1902 {
1903  /* Locate the annex. */
1904  const char *annex_end = strchr(buf, ':');
1905  if (!annex_end)
1906  return ERROR_FAIL;
1907 
1908  /* After the read marker and annex, qXfer looks like a
1909  * traditional 'm' packet. */
1910  char *separator;
1911  *ofs = strtoul(annex_end + 1, &separator, 16);
1912 
1913  if (*separator != ',')
1914  return ERROR_FAIL;
1915 
1916  *len = strtoul(separator + 1, NULL, 16);
1917 
1918  /* Extract the annex if needed */
1919  if (annex) {
1920  *annex = strndup(buf, annex_end - buf);
1921  if (!*annex)
1922  return ERROR_FAIL;
1923  }
1924 
1925  return ERROR_OK;
1926 }
1927 
1928 static int compare_bank(const void *a, const void *b)
1929 {
1930  struct flash_bank *b1, *b2;
1931  b1 = *((struct flash_bank **)a);
1932  b2 = *((struct flash_bank **)b);
1933 
1934  if (b1->base == b2->base)
1935  return 0;
1936  else if (b1->base > b2->base)
1937  return 1;
1938  else
1939  return -1;
1940 }
1941 
1943  char const *packet, int packet_size)
1944 {
1945  /* We get away with only specifying flash here. Regions that are not
1946  * specified are treated as if we provided no memory map(if not we
1947  * could detect the holes and mark them as RAM).
1948  * Normally we only execute this code once, but no big deal if we
1949  * have to regenerate it a couple of times.
1950  */
1951 
1953  struct flash_bank *p;
1954  char *xml = NULL;
1955  int size = 0;
1956  int pos = 0;
1957  int retval = ERROR_OK;
1958  struct flash_bank **banks;
1959  int offset;
1960  int length;
1961  char *separator;
1962  target_addr_t ram_start = 0;
1963  unsigned int target_flash_banks = 0;
1964 
1965  /* skip command character */
1966  packet += 23;
1967 
1968  offset = strtoul(packet, &separator, 16);
1969  length = strtoul(separator + 1, &separator, 16);
1970 
1971  xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1972 
1973  /* Sort banks in ascending order. We need to report non-flash
1974  * memory as ram (or rather read/write) by default for GDB, since
1975  * it has no concept of non-cacheable read/write memory (i/o etc).
1976  */
1977  banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1978 
1979  for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
1981  if (p->target != target)
1982  continue;
1983  retval = get_flash_bank_by_num(i, &p);
1984  if (retval != ERROR_OK) {
1985  free(banks);
1986  gdb_error(connection, retval);
1987  return retval;
1988  }
1989  banks[target_flash_banks++] = p;
1990  }
1991 
1992  qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1993  compare_bank);
1994 
1995  for (unsigned int i = 0; i < target_flash_banks; i++) {
1996  unsigned int sector_size = 0;
1997  unsigned int group_len = 0;
1998 
1999  p = banks[i];
2000 
2001  if (ram_start < p->base)
2002  xml_printf(&retval, &xml, &pos, &size,
2003  "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
2004  "length=\"" TARGET_ADDR_FMT "\"/>\n",
2005  ram_start, p->base - ram_start);
2006 
2007  if (p->read_only) {
2008  xml_printf(&retval, &xml, &pos, &size,
2009  "<memory type=\"rom\" start=\"" TARGET_ADDR_FMT "\" "
2010  "length=\"0x%x\"/>\n",
2011  p->base, p->size);
2012  } else {
2013  if (p->num_sectors == 0) {
2014  xml_printf(&retval, &xml, &pos, &size,
2015  "<memory type=\"flash\" "
2016  "start=\"" TARGET_ADDR_FMT "\" "
2017  "length=\"0x%x\">"
2018  "<property name=\"blocksize\">0x%x</property>\n"
2019  "</memory>\n", p->base, p->size, p->size);
2020  }
2021 
2022  /* Report adjacent groups of same-size sectors. So for
2023  * example top boot CFI flash will list an initial region
2024  * with several large sectors (maybe 128KB) and several
2025  * smaller ones at the end (maybe 32KB). STR7 will have
2026  * regions with 8KB, 32KB, and 64KB sectors; etc.
2027  */
2028  for (unsigned int j = 0; j < p->num_sectors; j++) {
2029  // Maybe start a new group of sectors
2030  if (sector_size == 0) {
2031  if (p->sectors[j].offset + p->sectors[j].size > p->size) {
2032  LOG_WARNING("The flash sector at offset 0x%08" PRIx32
2033  " overflows the end of %s bank.",
2034  p->sectors[j].offset, p->name);
2035  LOG_WARNING("The rest of bank will not show in gdb memory map.");
2036  break;
2037  }
2039  start = p->base + p->sectors[j].offset;
2040  xml_printf(&retval, &xml, &pos, &size,
2041  "<memory type=\"flash\" "
2042  "start=\"" TARGET_ADDR_FMT "\" ",
2043  start);
2044  sector_size = p->sectors[j].size;
2045  group_len = sector_size;
2046  } else {
2047  group_len += sector_size; /* equal to p->sectors[j].size */
2048  }
2049 
2050  /* Does this finish a group of sectors?
2051  * If not, continue an already-started group.
2052  */
2053  if (j < p->num_sectors - 1
2054  && p->sectors[j + 1].size == sector_size
2055  && p->sectors[j + 1].offset == p->sectors[j].offset + sector_size
2056  && p->sectors[j + 1].offset + p->sectors[j + 1].size <= p->size)
2057  continue;
2058 
2059  xml_printf(&retval, &xml, &pos, &size,
2060  "length=\"0x%x\">\n"
2061  "<property name=\"blocksize\">"
2062  "0x%x</property>\n"
2063  "</memory>\n",
2064  group_len,
2065  sector_size);
2066  sector_size = 0;
2067  }
2068  }
2069 
2070  ram_start = p->base + p->size;
2071  }
2072 
2073  if (ram_start != 0)
2074  xml_printf(&retval, &xml, &pos, &size,
2075  "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
2076  "length=\"" TARGET_ADDR_FMT "\"/>\n",
2077  ram_start, target_address_max(target) - ram_start + 1);
2078  /* ELSE a flash chip could be at the very end of the address space, in
2079  * which case ram_start will be precisely 0 */
2080 
2081  free(banks);
2082 
2083  xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
2084 
2085  if (retval != ERROR_OK) {
2086  free(xml);
2087  gdb_error(connection, retval);
2088  return retval;
2089  }
2090 
2091  if (offset + length > pos)
2092  length = pos - offset;
2093 
2094  char *t = malloc(length + 1);
2095  t[0] = 'l';
2096  memcpy(t + 1, xml + offset, length);
2097  gdb_put_packet(connection, t, length + 1);
2098 
2099  free(t);
2100  free(xml);
2101  return ERROR_OK;
2102 }
2103 
2104 static const char *gdb_get_reg_type_name(enum reg_type type)
2105 {
2106  switch (type) {
2107  case REG_TYPE_BOOL:
2108  return "bool";
2109  case REG_TYPE_INT:
2110  return "int";
2111  case REG_TYPE_INT8:
2112  return "int8";
2113  case REG_TYPE_INT16:
2114  return "int16";
2115  case REG_TYPE_INT32:
2116  return "int32";
2117  case REG_TYPE_INT64:
2118  return "int64";
2119  case REG_TYPE_INT128:
2120  return "int128";
2121  case REG_TYPE_UINT:
2122  return "uint";
2123  case REG_TYPE_UINT8:
2124  return "uint8";
2125  case REG_TYPE_UINT16:
2126  return "uint16";
2127  case REG_TYPE_UINT32:
2128  return "uint32";
2129  case REG_TYPE_UINT64:
2130  return "uint64";
2131  case REG_TYPE_UINT128:
2132  return "uint128";
2133  case REG_TYPE_CODE_PTR:
2134  return "code_ptr";
2135  case REG_TYPE_DATA_PTR:
2136  return "data_ptr";
2137  case REG_TYPE_FLOAT:
2138  return "float";
2139  case REG_TYPE_IEEE_SINGLE:
2140  return "ieee_single";
2141  case REG_TYPE_IEEE_DOUBLE:
2142  return "ieee_double";
2143  case REG_TYPE_ARCH_DEFINED:
2144  return "int"; /* return arbitrary string to avoid compile warning. */
2145  }
2146 
2147  return "int"; /* "int" as default value */
2148 }
2149 
2150 static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id,
2151  int *num_arch_defined_types)
2152 {
2153  int tbl_sz = *num_arch_defined_types;
2154 
2155  if (type_id && (strcmp(type_id, ""))) {
2156  for (int j = 0; j < (tbl_sz + 1); j++) {
2157  if (!((*arch_defined_types_list)[j])) {
2158  (*arch_defined_types_list)[tbl_sz++] = type_id;
2159  *arch_defined_types_list = realloc(*arch_defined_types_list,
2160  sizeof(char *) * (tbl_sz + 1));
2161  (*arch_defined_types_list)[tbl_sz] = NULL;
2162  *num_arch_defined_types = tbl_sz;
2163  return 1;
2164  } else {
2165  if (!strcmp((*arch_defined_types_list)[j], type_id))
2166  return 0;
2167  }
2168  }
2169  }
2170 
2171  return -1;
2172 }
2173 
2175  char **tdesc, int *pos, int *size, struct reg_data_type *type,
2176  char const **arch_defined_types_list[], int *num_arch_defined_types)
2177 {
2178  int retval = ERROR_OK;
2179 
2180  if (type->type_class == REG_TYPE_CLASS_VECTOR) {
2181  struct reg_data_type *data_type = type->reg_type_vector->type;
2183  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2184  num_arch_defined_types))
2186  arch_defined_types_list,
2187  num_arch_defined_types);
2188  }
2189  /* <vector id="id" type="type" count="count"/> */
2190  xml_printf(&retval, tdesc, pos, size,
2191  "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32 "\"/>\n",
2192  type->id, type->reg_type_vector->type->id,
2193  type->reg_type_vector->count);
2194 
2195  } else if (type->type_class == REG_TYPE_CLASS_UNION) {
2196  struct reg_data_type_union_field *field;
2197  field = type->reg_type_union->fields;
2198  while (field) {
2199  struct reg_data_type *data_type = field->type;
2201  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2202  num_arch_defined_types))
2204  arch_defined_types_list,
2205  num_arch_defined_types);
2206  }
2207 
2208  field = field->next;
2209  }
2210  /* <union id="id">
2211  * <field name="name" type="type"/> ...
2212  * </union> */
2213  xml_printf(&retval, tdesc, pos, size,
2214  "<union id=\"%s\">\n",
2215  type->id);
2216 
2217  field = type->reg_type_union->fields;
2218  while (field) {
2219  xml_printf(&retval, tdesc, pos, size,
2220  "<field name=\"%s\" type=\"%s\"/>\n",
2221  field->name, field->type->id);
2222 
2223  field = field->next;
2224  }
2225 
2226  xml_printf(&retval, tdesc, pos, size,
2227  "</union>\n");
2228 
2229  } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
2230  struct reg_data_type_struct_field *field;
2231  field = type->reg_type_struct->fields;
2232 
2233  if (field->use_bitfields) {
2234  /* <struct id="id" size="size">
2235  * <field name="name" start="start" end="end"/> ...
2236  * </struct> */
2237  xml_printf(&retval, tdesc, pos, size,
2238  "<struct id=\"%s\" size=\"%" PRIu32 "\">\n",
2239  type->id, type->reg_type_struct->size);
2240  while (field) {
2241  xml_printf(&retval, tdesc, pos, size,
2242  "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2243  field->name, field->bitfield->start, field->bitfield->end,
2245 
2246  field = field->next;
2247  }
2248  } else {
2249  while (field) {
2250  struct reg_data_type *data_type = field->type;
2252  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2253  num_arch_defined_types))
2255  arch_defined_types_list,
2256  num_arch_defined_types);
2257  }
2258  }
2259 
2260  /* <struct id="id">
2261  * <field name="name" type="type"/> ...
2262  * </struct> */
2263  xml_printf(&retval, tdesc, pos, size,
2264  "<struct id=\"%s\">\n",
2265  type->id);
2266  while (field) {
2267  xml_printf(&retval, tdesc, pos, size,
2268  "<field name=\"%s\" type=\"%s\"/>\n",
2269  field->name, field->type->id);
2270 
2271  field = field->next;
2272  }
2273  }
2274 
2275  xml_printf(&retval, tdesc, pos, size,
2276  "</struct>\n");
2277 
2278  } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
2279  /* <flags id="id" size="size">
2280  * <field name="name" start="start" end="end"/> ...
2281  * </flags> */
2282  xml_printf(&retval, tdesc, pos, size,
2283  "<flags id=\"%s\" size=\"%" PRIu32 "\">\n",
2284  type->id, type->reg_type_flags->size);
2285 
2286  struct reg_data_type_flags_field *field;
2287  field = type->reg_type_flags->fields;
2288  while (field) {
2289  xml_printf(&retval, tdesc, pos, size,
2290  "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2291  field->name, field->bitfield->start, field->bitfield->end,
2293 
2294  field = field->next;
2295  }
2296 
2297  xml_printf(&retval, tdesc, pos, size,
2298  "</flags>\n");
2299 
2300  }
2301 
2302  return ERROR_OK;
2303 }
2304 
2305 /* Get a list of available target registers features. feature_list must
2306  * be freed by caller.
2307  */
2308 static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
2309  struct reg **reg_list, int reg_list_size)
2310 {
2311  int tbl_sz = 0;
2312 
2313  /* Start with only one element */
2314  *feature_list = calloc(1, sizeof(char *));
2315 
2316  for (int i = 0; i < reg_list_size; i++) {
2317  if (!reg_list[i]->exist || reg_list[i]->hidden)
2318  continue;
2319 
2320  if (reg_list[i]->feature
2321  && reg_list[i]->feature->name
2322  && (strcmp(reg_list[i]->feature->name, ""))) {
2323  /* We found a feature, check if the feature is already in the
2324  * table. If not, allocate a new entry for the table and
2325  * put the new feature in it.
2326  */
2327  for (int j = 0; j < (tbl_sz + 1); j++) {
2328  if (!((*feature_list)[j])) {
2329  (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
2330  *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
2331  (*feature_list)[tbl_sz] = NULL;
2332  break;
2333  } else {
2334  if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
2335  break;
2336  }
2337  }
2338  }
2339  }
2340 
2341  if (feature_list_size)
2342  *feature_list_size = tbl_sz;
2343 
2344  return ERROR_OK;
2345 }
2346 
2347 /* Create a register list that's the union of all the registers of the SMP
2348  * group this target is in. If the target is not part of an SMP group, this
2349  * returns the same as target_get_gdb_reg_list_noread().
2350  */
2351 static int smp_reg_list_noread(struct target *target,
2352  struct reg **combined_list[], int *combined_list_size,
2353  enum target_register_class reg_class)
2354 {
2355  if (!target->smp)
2356  return target_get_gdb_reg_list_noread(target, combined_list,
2357  combined_list_size, REG_CLASS_ALL);
2358 
2359  unsigned int combined_allocated = 256;
2360  struct reg **local_list = malloc(combined_allocated * sizeof(struct reg *));
2361  if (!local_list) {
2362  LOG_ERROR("malloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2363  return ERROR_FAIL;
2364  }
2365  unsigned int local_list_size = 0;
2366 
2367  struct target_list *head;
2369  if (!target_was_examined(head->target))
2370  continue;
2371 
2372  struct reg **reg_list = NULL;
2373  int reg_list_size;
2374  int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2375  &reg_list_size, reg_class);
2376  if (result != ERROR_OK) {
2377  free(local_list);
2378  return result;
2379  }
2380  for (int i = 0; i < reg_list_size; i++) {
2381  bool found = false;
2382  struct reg *a = reg_list[i];
2383  if (a->exist) {
2384  /* Nested loop makes this O(n^2), but this entire function with
2385  * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2386  * for me. */
2387  for (unsigned int j = 0; j < local_list_size; j++) {
2388  struct reg *b = local_list[j];
2389  if (!strcmp(a->name, b->name)) {
2390  found = true;
2391  if (a->size != b->size) {
2392  LOG_ERROR("SMP register %s is %d bits on one "
2393  "target, but %d bits on another target.",
2394  a->name, a->size, b->size);
2395  free(reg_list);
2396  free(local_list);
2397  return ERROR_FAIL;
2398  }
2399  break;
2400  }
2401  }
2402  if (!found) {
2403  LOG_TARGET_DEBUG(target, "%s not found in combined list", a->name);
2404  if (local_list_size >= combined_allocated) {
2405  combined_allocated *= 2;
2406  local_list = realloc(local_list, combined_allocated * sizeof(struct reg *));
2407  if (!local_list) {
2408  LOG_ERROR("realloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2409  free(reg_list);
2410  return ERROR_FAIL;
2411  }
2412  }
2413  local_list[local_list_size] = a;
2414  local_list_size++;
2415  }
2416  }
2417  }
2418  free(reg_list);
2419  }
2420 
2421  if (local_list_size == 0) {
2422  LOG_ERROR("Unable to get register list");
2423  free(local_list);
2424  return ERROR_FAIL;
2425  }
2426 
2427  /* Now warn the user about any registers that weren't found in every target. */
2429  if (!target_was_examined(head->target))
2430  continue;
2431 
2432  struct reg **reg_list = NULL;
2433  int reg_list_size;
2434  int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2435  &reg_list_size, reg_class);
2436  if (result != ERROR_OK) {
2437  free(local_list);
2438  return result;
2439  }
2440  for (unsigned int i = 0; i < local_list_size; i++) {
2441  bool found = false;
2442  struct reg *a = local_list[i];
2443  for (int j = 0; j < reg_list_size; j++) {
2444  struct reg *b = reg_list[j];
2445  if (b->exist && !strcmp(a->name, b->name)) {
2446  found = true;
2447  break;
2448  }
2449  }
2450  if (!found) {
2451  LOG_TARGET_WARNING(head->target, "Register %s does not exist, which is part of an SMP group where "
2452  "this register does exist.", a->name);
2453  }
2454  }
2455  free(reg_list);
2456  }
2457 
2458  *combined_list = local_list;
2459  *combined_list_size = local_list_size;
2460  return ERROR_OK;
2461 }
2462 
2463 static int gdb_generate_target_description(struct target *target, char **tdesc_out)
2464 {
2465  int retval = ERROR_OK;
2466  struct reg **reg_list = NULL;
2467  int reg_list_size;
2468  char const *architecture;
2469  char const **features = NULL;
2470  int feature_list_size = 0;
2471  char *tdesc = NULL;
2472  int pos = 0;
2473  int size = 0;
2474 
2475 
2476  retval = smp_reg_list_noread(target, &reg_list, &reg_list_size,
2477  REG_CLASS_ALL);
2478 
2479  if (retval != ERROR_OK) {
2480  LOG_ERROR("get register list failed");
2481  retval = ERROR_FAIL;
2482  goto error;
2483  }
2484 
2485  if (reg_list_size <= 0) {
2486  LOG_ERROR("get register list failed");
2487  retval = ERROR_FAIL;
2488  goto error;
2489  }
2490 
2491  /* Get a list of available target registers features */
2492  retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2493  if (retval != ERROR_OK) {
2494  LOG_ERROR("Can't get the registers feature list");
2495  retval = ERROR_FAIL;
2496  goto error;
2497  }
2498 
2499  /* If we found some features associated with registers, create sections */
2500  int current_feature = 0;
2501 
2502  xml_printf(&retval, &tdesc, &pos, &size,
2503  "<?xml version=\"1.0\"?>\n"
2504  "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2505  "<target version=\"1.0\">\n");
2506 
2507  /* generate architecture element if supported by target */
2508  architecture = target_get_gdb_arch(target);
2509  if (architecture)
2510  xml_printf(&retval, &tdesc, &pos, &size,
2511  "<architecture>%s</architecture>\n", architecture);
2512 
2513  /* generate target description according to register list */
2514  if (features) {
2515  while (features[current_feature]) {
2516  char const **arch_defined_types = NULL;
2517  int num_arch_defined_types = 0;
2518 
2519  arch_defined_types = calloc(1, sizeof(char *));
2520  xml_printf(&retval, &tdesc, &pos, &size,
2521  "<feature name=\"%s\">\n",
2522  features[current_feature]);
2523 
2524  int i;
2525  for (i = 0; i < reg_list_size; i++) {
2526 
2527  if (!reg_list[i]->exist || reg_list[i]->hidden)
2528  continue;
2529 
2530  if (strcmp(reg_list[i]->feature->name, features[current_feature]))
2531  continue;
2532 
2533  const char *type_str;
2534  if (reg_list[i]->reg_data_type) {
2535  if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
2536  /* generate <type... first, if there are architecture-defined types. */
2537  if (lookup_add_arch_defined_types(&arch_defined_types,
2538  reg_list[i]->reg_data_type->id,
2539  &num_arch_defined_types))
2541  reg_list[i]->reg_data_type,
2542  &arch_defined_types,
2543  &num_arch_defined_types);
2544 
2545  type_str = reg_list[i]->reg_data_type->id;
2546  } else {
2547  /* predefined type */
2548  type_str = gdb_get_reg_type_name(
2549  reg_list[i]->reg_data_type->type);
2550  }
2551  } else {
2552  /* Default type is "int" */
2553  type_str = "int";
2554  }
2555 
2556  xml_printf(&retval, &tdesc, &pos, &size,
2557  "<reg name=\"%s\"", reg_list[i]->name);
2558  xml_printf(&retval, &tdesc, &pos, &size,
2559  " bitsize=\"%" PRIu32 "\"", reg_list[i]->size);
2560  xml_printf(&retval, &tdesc, &pos, &size,
2561  " regnum=\"%" PRIu32 "\"", reg_list[i]->number);
2562  if (reg_list[i]->caller_save)
2563  xml_printf(&retval, &tdesc, &pos, &size,
2564  " save-restore=\"yes\"");
2565  else
2566  xml_printf(&retval, &tdesc, &pos, &size,
2567  " save-restore=\"no\"");
2568 
2569  xml_printf(&retval, &tdesc, &pos, &size,
2570  " type=\"%s\"", type_str);
2571 
2572  if (reg_list[i]->group)
2573  xml_printf(&retval, &tdesc, &pos, &size,
2574  " group=\"%s\"", reg_list[i]->group);
2575 
2576  xml_printf(&retval, &tdesc, &pos, &size,
2577  "/>\n");
2578  }
2579 
2580  xml_printf(&retval, &tdesc, &pos, &size,
2581  "</feature>\n");
2582 
2583  current_feature++;
2584  free(arch_defined_types);
2585  }
2586  }
2587 
2588  xml_printf(&retval, &tdesc, &pos, &size,
2589  "</target>\n");
2590 
2591 error:
2592  free(features);
2593  free(reg_list);
2594 
2595  if (retval == ERROR_OK)
2596  *tdesc_out = tdesc;
2597  else
2598  free(tdesc);
2599 
2600  return retval;
2601 }
2602 
2603 static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
2604  char **chunk, int32_t offset, uint32_t length)
2605 {
2606  if (!target_desc) {
2607  LOG_ERROR("Unable to Generate Target Description");
2608  return ERROR_FAIL;
2609  }
2610 
2611  char *tdesc = target_desc->tdesc;
2612  uint32_t tdesc_length = target_desc->tdesc_length;
2613 
2614  if (!tdesc) {
2615  int retval = gdb_generate_target_description(target, &tdesc);
2616  if (retval != ERROR_OK) {
2617  LOG_ERROR("Unable to Generate Target Description");
2618  return ERROR_FAIL;
2619  }
2620 
2621  tdesc_length = strlen(tdesc);
2622  }
2623 
2624  char transfer_type;
2625 
2626  if (length < (tdesc_length - offset))
2627  transfer_type = 'm';
2628  else
2629  transfer_type = 'l';
2630 
2631  *chunk = malloc(length + 2);
2632  if (!*chunk) {
2633  LOG_ERROR("Unable to allocate memory");
2634  return ERROR_FAIL;
2635  }
2636 
2637  (*chunk)[0] = transfer_type;
2638  if (transfer_type == 'm') {
2639  strncpy((*chunk) + 1, tdesc + offset, length);
2640  (*chunk)[1 + length] = '\0';
2641  } else {
2642  strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
2643  (*chunk)[1 + (tdesc_length - offset)] = '\0';
2644 
2645  /* After gdb-server sends out last chunk, invalidate tdesc. */
2646  free(tdesc);
2647  tdesc = NULL;
2648  tdesc_length = 0;
2649  }
2650 
2651  target_desc->tdesc = tdesc;
2652  target_desc->tdesc_length = tdesc_length;
2653 
2654  return ERROR_OK;
2655 }
2656 
2657 static int gdb_target_description_supported(struct target *target, bool *supported)
2658 {
2659  int retval = ERROR_OK;
2660  struct reg **reg_list = NULL;
2661  int reg_list_size = 0;
2662  char const **features = NULL;
2663  int feature_list_size = 0;
2664 
2665  char const *architecture = target_get_gdb_arch(target);
2666 
2667  retval = target_get_gdb_reg_list_noread(target, &reg_list,
2668  &reg_list_size, REG_CLASS_ALL);
2669  if (retval != ERROR_OK) {
2670  LOG_ERROR("get register list failed");
2671  goto error;
2672  }
2673 
2674  if (reg_list_size <= 0) {
2675  LOG_ERROR("get register list failed");
2676  retval = ERROR_FAIL;
2677  goto error;
2678  }
2679 
2680  /* Get a list of available target registers features */
2681  retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2682  if (retval != ERROR_OK) {
2683  LOG_ERROR("Can't get the registers feature list");
2684  goto error;
2685  }
2686 
2687  if (supported) {
2688  if (architecture || feature_list_size)
2689  *supported = true;
2690  else
2691  *supported = false;
2692  }
2693 
2694 error:
2695  free(features);
2696 
2697  free(reg_list);
2698 
2699  return retval;
2700 }
2701 
2702 static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
2703 {
2704  struct rtos *rtos = target->rtos;
2705  int retval = ERROR_OK;
2706  char *thread_list = NULL;
2707  int pos = 0;
2708  int size = 0;
2709 
2710  xml_printf(&retval, &thread_list, &pos, &size,
2711  "<?xml version=\"1.0\"?>\n"
2712  "<threads>\n");
2713 
2714  if (rtos) {
2715  for (int i = 0; i < rtos->thread_count; i++) {
2717 
2718  if (!thread_detail->exists)
2719  continue;
2720 
2722  xml_printf(&retval, &thread_list, &pos, &size,
2723  "<thread id=\"%" PRIx64 "\" name=\"%s\">",
2726  else
2727  xml_printf(&retval, &thread_list, &pos, &size,
2728  "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
2729 
2731  xml_printf(&retval, &thread_list, &pos, &size,
2732  "Name: %s", thread_detail->thread_name_str);
2733 
2736  xml_printf(&retval, &thread_list, &pos, &size,
2737  ", ");
2738  xml_printf(&retval, &thread_list, &pos, &size,
2739  "%s", thread_detail->extra_info_str);
2740  }
2741 
2742  xml_printf(&retval, &thread_list, &pos, &size,
2743  "</thread>\n");
2744  }
2745  }
2746 
2747  xml_printf(&retval, &thread_list, &pos, &size,
2748  "</threads>\n");
2749 
2750  if (retval == ERROR_OK)
2751  *thread_list_out = thread_list;
2752  else
2753  free(thread_list);
2754 
2755  return retval;
2756 }
2757 
2758 static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
2759  char **chunk, int32_t offset, uint32_t length)
2760 {
2761  if (!*thread_list) {
2762  int retval = gdb_generate_thread_list(target, thread_list);
2763  if (retval != ERROR_OK) {
2764  LOG_ERROR("Unable to Generate Thread List");
2765  return ERROR_FAIL;
2766  }
2767  }
2768 
2769  size_t thread_list_length = strlen(*thread_list);
2770  char transfer_type;
2771 
2772  length = MIN(length, thread_list_length - offset);
2773  if (length < (thread_list_length - offset))
2774  transfer_type = 'm';
2775  else
2776  transfer_type = 'l';
2777 
2778  *chunk = malloc(length + 2 + 3);
2779  /* Allocating extra 3 bytes prevents false positive valgrind report
2780  * of strlen(chunk) word access:
2781  * Invalid read of size 4
2782  * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2783  if (!*chunk) {
2784  LOG_ERROR("Unable to allocate memory");
2785  return ERROR_FAIL;
2786  }
2787 
2788  (*chunk)[0] = transfer_type;
2789  strncpy((*chunk) + 1, (*thread_list) + offset, length);
2790  (*chunk)[1 + length] = '\0';
2791 
2792  /* After gdb-server sends out last chunk, invalidate thread list. */
2793  if (transfer_type == 'l') {
2794  free(*thread_list);
2795  *thread_list = NULL;
2796  }
2797 
2798  return ERROR_OK;
2799 }
2800 
2802  char const *packet, int packet_size)
2803 {
2804  struct command_context *cmd_ctx = connection->cmd_ctx;
2807 
2808  if (strncmp(packet, "qRcmd,", 6) == 0) {
2809  if (packet_size > 6) {
2810  Jim_Interp *interp = cmd_ctx->interp;
2811  char *cmd;
2812  cmd = malloc((packet_size - 6) / 2 + 1);
2813  size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
2814  cmd[len] = 0;
2815 
2816  /* We want to print all debug output to GDB connection */
2819  /* some commands need to know the GDB connection, make note of current
2820  * GDB connection. */
2822 
2823  struct target *saved_target_override = cmd_ctx->current_target_override;
2824  cmd_ctx->current_target_override = NULL;
2825 
2826  struct command_context *old_context = Jim_GetAssocData(interp, "context");
2827  Jim_DeleteAssocData(interp, "context");
2828  int retval = Jim_SetAssocData(interp, "context", NULL, cmd_ctx);
2829  if (retval == JIM_OK) {
2830  retval = Jim_EvalObj(interp, Jim_NewStringObj(interp, cmd, -1));
2831  Jim_DeleteAssocData(interp, "context");
2832  }
2833  int inner_retval = Jim_SetAssocData(interp, "context", NULL, old_context);
2834  if (retval == JIM_OK)
2835  retval = inner_retval;
2836 
2837  cmd_ctx->current_target_override = saved_target_override;
2838 
2842  free(cmd);
2843  if (retval == JIM_RETURN)
2844  retval = interp->returnCode;
2845  int lenmsg;
2846  const char *cretmsg = Jim_GetString(Jim_GetResult(interp), &lenmsg);
2847  char *retmsg;
2848  if (lenmsg && cretmsg[lenmsg - 1] != '\n') {
2849  retmsg = alloc_printf("%s\n", cretmsg);
2850  lenmsg++;
2851  } else {
2852  retmsg = strdup(cretmsg);
2853  }
2854  if (!retmsg)
2856 
2857  if (retval == JIM_OK) {
2858  if (lenmsg) {
2859  char *hex_buffer = malloc(lenmsg * 2 + 1);
2860  if (!hex_buffer) {
2861  free(retmsg);
2863  }
2864 
2865  size_t pkt_len = hexify(hex_buffer, (const uint8_t *)retmsg, lenmsg,
2866  lenmsg * 2 + 1);
2867  gdb_put_packet(connection, hex_buffer, pkt_len);
2868  free(hex_buffer);
2869  } else {
2870  gdb_put_packet(connection, "OK", 2);
2871  }
2872  } else {
2873  if (lenmsg)
2874  gdb_output_con(connection, retmsg);
2875  gdb_send_error(connection, retval);
2876  }
2877  free(retmsg);
2878  return ERROR_OK;
2879  }
2880  gdb_put_packet(connection, "OK", 2);
2881  return ERROR_OK;
2882  } else if (strncmp(packet, "qCRC:", 5) == 0) {
2883  if (packet_size > 5) {
2884  int retval;
2885  char gdb_reply[10];
2886  char *separator;
2887  uint32_t checksum;
2888  target_addr_t addr = 0;
2889  uint32_t len = 0;
2890 
2891  /* skip command character */
2892  packet += 5;
2893 
2894  addr = strtoull(packet, &separator, 16);
2895 
2896  if (*separator != ',') {
2897  LOG_ERROR("incomplete read memory packet received, dropping connection");
2899  }
2900 
2901  len = strtoul(separator + 1, NULL, 16);
2902 
2904  retval = target_checksum_memory(target, addr, len, &checksum);
2906 
2907  if (retval == ERROR_OK) {
2908  snprintf(gdb_reply, 10, "C%8.8" PRIx32, checksum);
2909  gdb_put_packet(connection, gdb_reply, 9);
2910  } else {
2911  retval = gdb_error(connection, retval);
2912  if (retval != ERROR_OK)
2913  return retval;
2914  }
2915 
2916  return ERROR_OK;
2917  }
2918  } else if (strncmp(packet, "qSupported", 10) == 0) {
2919  /* we currently support packet size and qXfer:memory-map:read (if enabled)
2920  * qXfer:features:read is supported for some targets */
2921  int retval = ERROR_OK;
2922  char *buffer = NULL;
2923  int pos = 0;
2924  int size = 0;
2925  bool gdb_target_desc_supported = false;
2926 
2927  /* we need to test that the target supports target descriptions */
2928  retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
2929  if (retval != ERROR_OK) {
2930  LOG_INFO("Failed detecting Target Description Support, disabling");
2931  gdb_target_desc_supported = false;
2932  }
2933 
2934  /* support may be disabled globally */
2936  if (gdb_target_desc_supported)
2937  LOG_WARNING("Target Descriptions Supported, but disabled");
2938  gdb_target_desc_supported = false;
2939  }
2940 
2941  xml_printf(&retval,
2942  &buffer,
2943  &pos,
2944  &size,
2945  "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2947  (gdb_use_memory_map && (flash_get_bank_count() > 0)) ? '+' : '-',
2948  gdb_target_desc_supported ? '+' : '-');
2949 
2950  if (retval != ERROR_OK) {
2952  return ERROR_OK;
2953  }
2954 
2956  free(buffer);
2957 
2958  return ERROR_OK;
2959  } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
2960  && (flash_get_bank_count() > 0))
2961  return gdb_memory_map(connection, packet, packet_size);
2962  else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
2963  char *xml = NULL;
2964  int retval = ERROR_OK;
2965 
2966  int offset;
2967  unsigned int length;
2968 
2969  /* skip command character */
2970  packet += 20;
2971 
2972  if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2974  return ERROR_OK;
2975  }
2976 
2977  /* Target should prepare correct target description for annex.
2978  * The first character of returned xml is 'm' or 'l'. 'm' for
2979  * there are *more* chunks to transfer. 'l' for it is the *last*
2980  * chunk of target description.
2981  */
2983  &xml, offset, length);
2984  if (retval != ERROR_OK) {
2985  gdb_error(connection, retval);
2986  return retval;
2987  }
2988 
2989  gdb_put_packet(connection, xml, strlen(xml));
2990 
2991  free(xml);
2992  return ERROR_OK;
2993  } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
2994  char *xml = NULL;
2995  int retval = ERROR_OK;
2996 
2997  int offset;
2998  unsigned int length;
2999 
3000  /* skip command character */
3001  packet += 19;
3002 
3003  if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
3005  return ERROR_OK;
3006  }
3007 
3008  /* Target should prepare correct thread list for annex.
3009  * The first character of returned xml is 'm' or 'l'. 'm' for
3010  * there are *more* chunks to transfer. 'l' for it is the *last*
3011  * chunk of target description.
3012  */
3014  &xml, offset, length);
3015  if (retval != ERROR_OK) {
3016  gdb_error(connection, retval);
3017  return retval;
3018  }
3019 
3020  gdb_put_packet(connection, xml, strlen(xml));
3021 
3022  free(xml);
3023  return ERROR_OK;
3024  } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
3026  gdb_put_packet(connection, "OK", 2);
3027  return ERROR_OK;
3028  } else if (target->type->gdb_query_custom) {
3029  char *buffer = NULL;
3030  int ret = target->type->gdb_query_custom(target, packet, &buffer);
3032  return ret;
3033  }
3034 
3035  gdb_put_packet(connection, "", 0);
3036  return ERROR_OK;
3037 }
3038 
3039 static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet,
3040  __attribute__((unused)) int packet_size)
3041 {
3044  const char *parse = packet;
3045  int retval;
3046 
3047  /* query for vCont supported */
3048  if (parse[0] == '?') {
3049  if (target->type->step) {
3050  /* gdb doesn't accept c without C and s without S */
3051  gdb_put_packet(connection, "vCont;c;C;s;S", 13);
3052  return true;
3053  }
3054  return false;
3055  }
3056 
3057  if (parse[0] == ';') {
3058  ++parse;
3059  }
3060 
3061  /* simple case, a continue packet */
3062  if (parse[0] == 'c') {
3063  gdb_running_type = 'c';
3064  LOG_TARGET_DEBUG(target, "target continue");
3066  retval = target_resume(target, true, 0, false, false);
3067  if (retval == ERROR_TARGET_NOT_HALTED)
3068  LOG_TARGET_INFO(target, "target was not halted when resume was requested");
3069 
3070  /* poll target in an attempt to make its internal state consistent */
3071  if (retval != ERROR_OK) {
3072  retval = target_poll(target);
3073  if (retval != ERROR_OK)
3074  LOG_TARGET_DEBUG(target, "error polling target after failed resume");
3075  }
3076 
3077  /*
3078  * We don't report errors to gdb here, move frontend_state to
3079  * TARGET_RUNNING to stay in sync with gdb's expectation of the
3080  * target state
3081  */
3084 
3085  return true;
3086  }
3087 
3088  /* single-step or step-over-breakpoint */
3089  if (parse[0] == 's') {
3090  gdb_running_type = 's';
3091  bool fake_step = false;
3092 
3093  struct target *ct = target;
3094  bool current_pc = true;
3095  int64_t thread_id;
3096  parse++;
3097  if (parse[0] == ':') {
3098  char *endp;
3099  parse++;
3100  thread_id = strtoll(parse, &endp, 16);
3101  if (endp) {
3102  parse = endp;
3103  }
3104  } else {
3105  thread_id = 0;
3106  }
3107 
3108  if (target->rtos) {
3109  /* Sometimes this results in picking a different thread than
3110  * gdb just requested to step. Then we fake it, and now there's
3111  * a different thread selected than gdb expects, so register
3112  * accesses go to the wrong one!
3113  * E.g.:
3114  * Hg1$
3115  * P8=72101ce197869329$ # write r8 on thread 1
3116  * g$
3117  * vCont?$
3118  * vCont;s:1;c$ # rtos_update_threads changes to other thread
3119  * g$
3120  * qXfer:threads:read::0,fff$
3121  * P8=cc060607eb89ca7f$ # write r8 on other thread
3122  * g$
3123  */
3124  /* rtos_update_threads(target); */
3125 
3126  target->rtos->gdb_target_for_threadid(connection, thread_id, &ct);
3127 
3128  /*
3129  * check if the thread to be stepped is the current rtos thread
3130  * if not, we must fake the step
3131  */
3132  fake_step = rtos_needs_fake_step(target, thread_id);
3133  }
3134 
3135  if (parse[0] == ';') {
3136  ++parse;
3137 
3138  if (parse[0] == 'c') {
3139  parse += 1;
3140 
3141  /* check if thread-id follows */
3142  if (parse[0] == ':') {
3143  int64_t tid;
3144  parse += 1;
3145 
3146  tid = strtoll(parse, NULL, 16);
3147  if (tid == thread_id) {
3148  /*
3149  * Special case: only step a single thread (core),
3150  * keep the other threads halted. Currently, only
3151  * aarch64 target understands it. Other target types don't
3152  * care (nobody checks the actual value of 'current')
3153  * and it doesn't really matter. This deserves
3154  * a symbolic constant and a formal interface documentation
3155  * at a later time.
3156  */
3157  LOG_DEBUG("request to step current core only");
3158  /* uncomment after checking that indeed other targets are safe */
3159  /*current_pc = 2;*/
3160  }
3161  }
3162  }
3163  }
3164 
3165  LOG_TARGET_DEBUG(ct, "single-step thread %" PRIx64, thread_id);
3168 
3169  /*
3170  * work around an annoying gdb behaviour: when the current thread
3171  * is changed in gdb, it assumes that the target can follow and also
3172  * make the thread current. This is an assumption that cannot hold
3173  * for a real target running a multi-threading OS. We just fake
3174  * the step to not trigger an internal error in gdb. See
3175  * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3176  */
3177  if (fake_step) {
3178  int sig_reply_len;
3179  char sig_reply[128];
3180 
3181  LOG_DEBUG("fake step thread %"PRIx64, thread_id);
3182 
3183  sig_reply_len = snprintf(sig_reply, sizeof(sig_reply),
3184  "T05thread:%016"PRIx64";", thread_id);
3185 
3186  gdb_put_packet(connection, sig_reply, sig_reply_len);
3188 
3189  return true;
3190  }
3191 
3192  /* support for gdb_sync command */
3193  if (gdb_connection->sync) {
3194  gdb_connection->sync = false;
3195  if (ct->state == TARGET_HALTED) {
3196  LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3197  "from the target.");
3200  } else
3202  return true;
3203  }
3204 
3205  retval = target_step(ct, current_pc, 0, false);
3206  if (retval == ERROR_TARGET_NOT_HALTED)
3207  LOG_TARGET_INFO(ct, "target was not halted when step was requested");
3208 
3209  /* if step was successful send a reply back to gdb */
3210  if (retval == ERROR_OK) {
3211  retval = target_poll(ct);
3212  if (retval != ERROR_OK)
3213  LOG_TARGET_DEBUG(ct, "error polling target after successful step");
3214  /* send back signal information */
3216  /* stop forwarding log packets! */
3218  } else
3220  return true;
3221  }
3222  LOG_ERROR("Unknown vCont packet");
3223  return false;
3224 }
3225 
3226 static char *next_hex_encoded_field(const char **str, char sep)
3227 {
3228  size_t hexlen;
3229  const char *hex = *str;
3230  if (hex[0] == '\0')
3231  return NULL;
3232 
3233  const char *end = strchr(hex, sep);
3234  if (!end)
3235  hexlen = strlen(hex);
3236  else
3237  hexlen = end - hex;
3238  *str = hex + hexlen + 1;
3239 
3240  if (hexlen % 2 != 0) {
3241  /* Malformed hex data */
3242  return NULL;
3243  }
3244 
3245  size_t count = hexlen / 2;
3246  char *decoded = malloc(count + 1);
3247  if (!decoded)
3248  return NULL;
3249 
3250  size_t converted = unhexify((void *)decoded, hex, count);
3251  if (converted != count) {
3252  free(decoded);
3253  return NULL;
3254  }
3255 
3256  decoded[count] = '\0';
3257  return decoded;
3258 }
3259 
3260 /* handle extended restart packet */
3261 static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
3262 {
3263  struct gdb_connection *gdb_con = connection->priv;
3265 
3268  command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
3269  target_name(target));
3270  /* set connection as attached after reset */
3271  gdb_con->attached = true;
3272  /* info rtos parts */
3273  gdb_thread_packet(connection, packet, packet_size);
3274 }
3275 
3276 static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
3277 {
3279  const char *parse = packet;
3280 
3281  /* Skip "vRun" */
3282  parse += 4;
3283 
3284  if (parse[0] != ';')
3285  return false;
3286  parse++;
3287 
3288  /* Skip first field "filename"; don't know what to do with it. */
3289  free(next_hex_encoded_field(&parse, ';'));
3290 
3291  char *cmdline = next_hex_encoded_field(&parse, ';');
3292  while (cmdline) {
3293  char *arg = next_hex_encoded_field(&parse, ';');
3294  if (!arg)
3295  break;
3296  char *new_cmdline = alloc_printf("%s %s", cmdline, arg);
3297  free(cmdline);
3298  free(arg);
3299  cmdline = new_cmdline;
3300  }
3301 
3302  if (cmdline) {
3303  if (target->semihosting) {
3304  LOG_INFO("GDB set inferior command line to '%s'", cmdline);
3305  free(target->semihosting->cmdline);
3306  target->semihosting->cmdline = cmdline;
3307  } else {
3308  LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline);
3309  free(cmdline);
3310  }
3311  }
3312 
3313  gdb_restart_inferior(connection, packet, packet_size);
3314  gdb_put_packet(connection, "S00", 3);
3315  return true;
3316 }
3317 
3319  char const *packet, int packet_size)
3320 {
3322  int result;
3323 
3325 
3326  if (strncmp(packet, "vCont", 5) == 0) {
3327  bool handled;
3328 
3329  packet += 5;
3330  packet_size -= 5;
3331 
3332  handled = gdb_handle_vcont_packet(connection, packet, packet_size);
3333  if (!handled)
3334  gdb_put_packet(connection, "", 0);
3335 
3336  return ERROR_OK;
3337  }
3338 
3339  if (strncmp(packet, "vRun", 4) == 0) {
3340  bool handled;
3341 
3342  handled = gdb_handle_vrun_packet(connection, packet, packet_size);
3343  if (!handled)
3344  gdb_put_packet(connection, "", 0);
3345 
3346  return ERROR_OK;
3347  }
3348 
3349  /* if flash programming disabled - send a empty reply */
3350 
3351  if (!gdb_flash_program) {
3352  gdb_put_packet(connection, "", 0);
3353  return ERROR_OK;
3354  }
3355 
3356  if (strncmp(packet, "vFlashErase:", 12) == 0) {
3358  unsigned long length;
3359 
3360  char const *parse = packet + 12;
3361  if (*parse == '\0') {
3362  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3364  }
3365 
3366  addr = strtoull(parse, (char **)&parse, 16);
3367 
3368  if (*(parse++) != ',' || *parse == '\0') {
3369  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3371  }
3372 
3373  length = strtoul(parse, (char **)&parse, 16);
3374 
3375  if (*parse != '\0') {
3376  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3378  }
3379 
3380  /* assume all sectors need erasing - stops any problems
3381  * when flash_write is called multiple times */
3382  flash_set_dirty();
3383 
3384  /* perform any target specific operations before the erase */
3387 
3388  /* vFlashErase:addr,length messages require region start and
3389  * end to be "block" aligned ... if padding is ever needed,
3390  * GDB will have become dangerously confused.
3391  */
3392  result = flash_erase_address_range(target, false, addr,
3393  length);
3394 
3395  /* perform any target specific operations after the erase */
3398 
3399  /* perform erase */
3400  if (result != ERROR_OK) {
3401  /* GDB doesn't evaluate the actual error number returned,
3402  * treat a failed erase as an I/O error
3403  */
3404  gdb_send_error(connection, EIO);
3405  LOG_ERROR("flash_erase returned %i", result);
3406  } else
3407  gdb_put_packet(connection, "OK", 2);
3408 
3409  return ERROR_OK;
3410  }
3411 
3412  if (strncmp(packet, "vFlashWrite:", 12) == 0) {
3413  int retval;
3415  unsigned long length;
3416  char const *parse = packet + 12;
3417 
3418  if (*parse == '\0') {
3419  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3421  }
3422 
3423  addr = strtoull(parse, (char **)&parse, 16);
3424  if (*(parse++) != ':') {
3425  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3427  }
3428  length = packet_size - (parse - packet);
3429 
3430  /* create a new image if there isn't already one */
3431  if (!gdb_connection->vflash_image) {
3432  gdb_connection->vflash_image = malloc(sizeof(struct image));
3433  image_open(gdb_connection->vflash_image, "", "build");
3434  }
3435 
3436  /* create new section with content from packet buffer */
3438  addr, length, 0x0, (uint8_t const *)parse);
3439  if (retval != ERROR_OK)
3440  return retval;
3441 
3442  gdb_put_packet(connection, "OK", 2);
3443 
3444  return ERROR_OK;
3445  }
3446 
3447  if (strncmp(packet, "vFlashDone", 10) == 0) {
3448  uint32_t written;
3449 
3450  /* GDB command 'flash-erase' does not send a vFlashWrite,
3451  * so nothing to write here. */
3452  if (!gdb_connection->vflash_image) {
3453  gdb_put_packet(connection, "OK", 2);
3454  return ERROR_OK;
3455  }
3456 
3457  /* process the flashing buffer. No need to erase as GDB
3458  * always issues a vFlashErase first. */
3462  &written, false);
3465  if (result != ERROR_OK) {
3466  if (result == ERROR_FLASH_DST_OUT_OF_BANK)
3467  gdb_put_packet(connection, "E.memtype", 9);
3468  else
3469  gdb_send_error(connection, EIO);
3470  } else {
3471  LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
3472  gdb_put_packet(connection, "OK", 2);
3473  }
3474 
3478 
3479  return ERROR_OK;
3480  }
3481 
3482  gdb_put_packet(connection, "", 0);
3483  return ERROR_OK;
3484 }
3485 
3486 static int gdb_detach(struct connection *connection)
3487 {
3488  /*
3489  * Only reply "OK" to GDB
3490  * it will close the connection and this will trigger a call to
3491  * gdb_connection_closed() that will in turn trigger the event
3492  * TARGET_EVENT_GDB_DETACH
3493  */
3494  return gdb_put_packet(connection, "OK", 2);
3495 }
3496 
3497 /* The format of 'F' response packet is
3498  * Fretcode,errno,Ctrl-C flag;call-specific attachment
3499  */
3501  char const *packet, int packet_size)
3502 {
3504  char *separator;
3505  char *parsing_point;
3506  int fileio_retcode = strtoul(packet + 1, &separator, 16);
3507  int fileio_errno = 0;
3508  bool fileio_ctrl_c = false;
3509  int retval;
3510 
3511  LOG_DEBUG("-");
3512 
3513  if (*separator == ',') {
3514  parsing_point = separator + 1;
3515  fileio_errno = strtoul(parsing_point, &separator, 16);
3516  if (*separator == ',') {
3517  if (*(separator + 1) == 'C') {
3518  /* TODO: process ctrl-c */
3519  fileio_ctrl_c = true;
3520  }
3521  }
3522  }
3523 
3524  LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3525  fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false");
3526 
3527  retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c);
3528  if (retval != ERROR_OK)
3529  return ERROR_FAIL;
3530 
3531  /* After File-I/O ends, keep continue or step */
3532  if (gdb_running_type == 'c')
3533  retval = target_resume(target, true, 0x0, false, false);
3534  else if (gdb_running_type == 's')
3535  retval = target_step(target, true, 0x0, false);
3536  else
3537  retval = ERROR_FAIL;
3538 
3539  if (retval != ERROR_OK)
3540  return ERROR_FAIL;
3541 
3542  return ERROR_OK;
3543 }
3544 
3545 static void gdb_log_callback(void *priv, const char *file, unsigned int line,
3546  const char *function, const char *string)
3547 {
3548  struct connection *connection = priv;
3549  struct gdb_connection *gdb_con = connection->priv;
3550 
3551  if (gdb_con->output_flag != GDB_OUTPUT_ALL)
3552  /* No out allowed */
3553  return;
3554 
3555  if (gdb_con->busy) {
3556  /* do not reply this using the O packet */
3557  return;
3558  }
3559 
3560  gdb_output_con(connection, string);
3561 }
3562 
3564 {
3565  char sig_reply[4];
3566  snprintf(sig_reply, 4, "T%2.2x", 2);
3567  gdb_put_packet(connection, sig_reply, 3);
3568 }
3569 
3571 {
3572  /* Do not allocate this on the stack */
3573  static char gdb_packet_buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for null-termination */
3574 
3575  struct target *target;
3576  char const *packet = gdb_packet_buffer;
3577  int packet_size;
3578  int retval;
3579  struct gdb_connection *gdb_con = connection->priv;
3580  static bool warn_use_ext;
3581 
3583 
3584  /* drain input buffer. If one of the packets fail, then an error
3585  * packet is replied, if applicable.
3586  *
3587  * This loop will terminate and the error code is returned.
3588  *
3589  * The calling fn will check if this error is something that
3590  * can be recovered from, or if the connection must be closed.
3591  *
3592  * If the error is recoverable, this fn is called again to
3593  * drain the rest of the buffer.
3594  */
3595  do {
3596  packet_size = GDB_BUFFER_SIZE;
3597  retval = gdb_get_packet(connection, gdb_packet_buffer, &packet_size);
3598  if (retval != ERROR_OK)
3599  return retval;
3600 
3601  /* terminate with zero */
3602  gdb_packet_buffer[packet_size] = '\0';
3603 
3604  if (packet_size > 0) {
3605 
3606  gdb_log_incoming_packet(connection, gdb_packet_buffer);
3607 
3608  retval = ERROR_OK;
3609  switch (packet[0]) {
3610  case 'T': /* Is thread alive? */
3611  gdb_thread_packet(connection, packet, packet_size);
3612  break;
3613  case 'H': /* Set current thread ( 'c' for step and continue,
3614  * 'g' for all other operations ) */
3615  gdb_thread_packet(connection, packet, packet_size);
3616  break;
3617  case 'q':
3618  case 'Q':
3619  retval = gdb_thread_packet(connection, packet, packet_size);
3620  if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
3621  retval = gdb_query_packet(connection, packet, packet_size);
3622  break;
3623  case 'g':
3624  retval = gdb_get_registers_packet(connection, packet, packet_size);
3625  break;
3626  case 'G':
3627  retval = gdb_set_registers_packet(connection, packet, packet_size);
3628  break;
3629  case 'p':
3630  retval = gdb_get_register_packet(connection, packet, packet_size);
3631  break;
3632  case 'P':
3633  retval = gdb_set_register_packet(connection, packet, packet_size);
3634  break;
3635  case 'm':
3636  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3637  retval = gdb_read_memory_packet(connection, packet, packet_size);
3638  gdb_con->output_flag = GDB_OUTPUT_NO;
3639  break;
3640  case 'M':
3641  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3642  retval = gdb_write_memory_packet(connection, packet, packet_size);
3643  gdb_con->output_flag = GDB_OUTPUT_NO;
3644  break;
3645  case 'z':
3646  case 'Z':
3647  retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
3648  break;
3649  case '?':
3650  gdb_last_signal_packet(connection, packet, packet_size);
3651  /* '?' is sent after the eventual '!' */
3652  if (!warn_use_ext && !gdb_con->extended_protocol) {
3653  warn_use_ext = true;
3654  LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3656  }
3657  break;
3658  case 'c':
3659  case 's':
3660  {
3661  gdb_thread_packet(connection, packet, packet_size);
3662  gdb_con->output_flag = GDB_OUTPUT_ALL;
3663 
3664  if (gdb_con->mem_write_error) {
3665  LOG_ERROR("Memory write failure!");
3666 
3667  /* now that we have reported the memory write error,
3668  * we can clear the condition */
3669  gdb_con->mem_write_error = false;
3670  }
3671 
3672  bool nostep = false;
3673  bool already_running = false;
3674  if (target->state == TARGET_RUNNING) {
3675  LOG_WARNING("WARNING! The target is already running. "
3676  "All changes GDB did to registers will be discarded! "
3677  "Waiting for target to halt.");
3678  already_running = true;
3679  } else if (target->state != TARGET_HALTED) {
3680  LOG_WARNING("The target is not in the halted nor running stated, "
3681  "stepi/continue ignored.");
3682  nostep = true;
3683  } else if ((packet[0] == 's') && gdb_con->sync) {
3684  /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3685  * sent by GDB first to OpenOCD, thus defeating the check to
3686  * make only the single stepping have the sync feature...
3687  */
3688  nostep = true;
3689  LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3690  "from the target.");
3691  }
3692  gdb_con->sync = false;
3693 
3694  if (!already_running && nostep) {
3695  /* Either the target isn't in the halted state, then we can't
3696  * step/continue. This might be early setup, etc.
3697  *
3698  * Or we want to allow GDB to pick up a fresh set of
3699  * register values without modifying the target state.
3700  *
3701  */
3703 
3704  /* stop forwarding log packets! */
3705  gdb_con->output_flag = GDB_OUTPUT_NO;
3706  } else {
3707  /* We're running/stepping, in which case we can
3708  * forward log output until the target is halted
3709  */
3710  gdb_con->frontend_state = TARGET_RUNNING;
3712 
3713  if (!already_running) {
3714  /* Here we don't want packet processing to stop even if this fails,
3715  * so we use a local variable instead of retval. */
3716  retval = gdb_step_continue_packet(connection, packet, packet_size);
3717  if (retval != ERROR_OK) {
3718  /* we'll never receive a halted
3719  * condition... issue a false one..
3720  */
3722  }
3723  }
3724  }
3725  }
3726  break;
3727  case 'v':
3728  retval = gdb_v_packet(connection, packet, packet_size);
3729  break;
3730  case 'D':
3731  retval = gdb_detach(connection);
3732  break;
3733  case 'X':
3734  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3735  retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
3736  gdb_con->output_flag = GDB_OUTPUT_NO;
3737  break;
3738  case 'k':
3739  if (gdb_con->extended_protocol) {
3740  gdb_con->attached = false;
3741  break;
3742  }
3743  gdb_put_packet(connection, "OK", 2);
3745  case '!':
3746  /* handle extended remote protocol */
3747  gdb_con->extended_protocol = true;
3748  gdb_put_packet(connection, "OK", 2);
3749  break;
3750  case 'R':
3751  /* handle extended restart packet */
3752  gdb_restart_inferior(connection, packet, packet_size);
3753  break;
3754 
3755  case 'j':
3756  /* DEPRECATED */
3757  /* packet supported only by smp target i.e cortex_a.c*/
3758  /* handle smp packet replying coreid played to gbd */
3759  gdb_read_smp_packet(connection, packet, packet_size);
3760  break;
3761 
3762  case 'J':
3763  /* DEPRECATED */
3764  /* packet supported only by smp target i.e cortex_a.c */
3765  /* handle smp packet setting coreid to be played at next
3766  * resume to gdb */
3767  gdb_write_smp_packet(connection, packet, packet_size);
3768  break;
3769 
3770  case 'F':
3771  /* File-I/O extension */
3772  /* After gdb uses host-side syscall to complete target file
3773  * I/O, gdb sends host-side syscall return value to target
3774  * by 'F' packet.
3775  * The format of 'F' response packet is
3776  * Fretcode,errno,Ctrl-C flag;call-specific attachment
3777  */
3778  gdb_con->frontend_state = TARGET_RUNNING;
3779  gdb_con->output_flag = GDB_OUTPUT_ALL;
3780  gdb_fileio_response_packet(connection, packet, packet_size);
3781  break;
3782 
3783  default:
3784  /* ignore unknown packets */
3785  LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3786  gdb_put_packet(connection, "", 0);
3787  break;
3788  }
3789 
3790  /* if a packet handler returned an error, exit input loop */
3791  if (retval != ERROR_OK)
3792  return retval;
3793  }
3794 
3795  if (gdb_con->ctrl_c) {
3796  struct target *available_target = get_available_target_from_connection(connection);
3797  if (available_target->state == TARGET_RUNNING) {
3798  struct target *t = available_target;
3799  if (available_target->rtos)
3801  retval = target_halt(t);
3802  if (retval == ERROR_OK)
3803  retval = target_poll(t);
3804  if (retval != ERROR_OK)
3806  gdb_con->ctrl_c = false;
3807  } else {
3808  LOG_TARGET_INFO(target, "Not running when halt was requested, stopping GDB. (state=%d)",
3809  target->state);
3811  }
3812  }
3813 
3814  } while (gdb_con->buf_cnt > 0);
3815 
3816  return ERROR_OK;
3817 }
3818 
3819 static int gdb_input(struct connection *connection)
3820 {
3821  int retval = gdb_input_inner(connection);
3822  struct gdb_connection *gdb_con = connection->priv;
3823  if (retval == ERROR_SERVER_REMOTE_CLOSED)
3824  return retval;
3825 
3826  /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3827  if (gdb_con->closed)
3829 
3830  /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3831  return ERROR_OK;
3832 }
3833 
3834 /*
3835  * Send custom notification packet as keep-alive during memory read/write.
3836  *
3837  * From gdb 7.0 (released 2009-10-06) an unknown notification received during
3838  * memory read/write would be silently dropped.
3839  * Before gdb 7.0 any character, with exclusion of "+-$", would be considered
3840  * as junk and ignored.
3841  * In both cases the reception will reset the timeout counter in gdb, thus
3842  * working as a keep-alive.
3843  * Check putpkt_binary() and getpkt_sane() in gdb commit
3844  * 74531fed1f2d662debc2c209b8b3faddceb55960
3845  *
3846  * Enable remote debug in gdb with 'set debug remote 1' to either dump the junk
3847  * characters in gdb pre-7.0 and the notification from gdb 7.0.
3848  */
3850 {
3851  static unsigned char count;
3852  unsigned char checksum = 0;
3853  char buf[22];
3854 
3855  int len = sprintf(buf, "%%oocd_keepalive:%2.2x", count++);
3856  for (int i = 1; i < len; i++)
3857  checksum += buf[i];
3858  len += sprintf(buf + len, "#%2.2x", checksum);
3859 
3860 #ifdef _DEBUG_GDB_IO_
3861  LOG_DEBUG("sending packet '%s'", buf);
3862 #endif
3863 
3864  gdb_write(connection, buf, len);
3865 }
3866 
3868 {
3869  struct gdb_connection *gdb_con = connection->priv;
3870 
3871  switch (gdb_con->output_flag) {
3872  case GDB_OUTPUT_NO:
3873  /* no need for keep-alive */
3874  break;
3875  case GDB_OUTPUT_NOTIF:
3876  /* send asynchronous notification */
3878  break;
3879  case GDB_OUTPUT_ALL:
3880  /* send an empty O packet */
3882  break;
3883  default:
3884  break;
3885  }
3886 }
3887 
3888 static const struct service_driver gdb_service_driver = {
3889  .name = "gdb",
3890  .new_connection_during_keep_alive_handler = NULL,
3891  .new_connection_handler = gdb_new_connection,
3892  .input_handler = gdb_input,
3893  .connection_closed_handler = gdb_connection_closed,
3894  .keep_client_alive_handler = gdb_keep_client_alive,
3895 };
3896 
3897 static int gdb_target_start(struct target *target, const char *port)
3898 {
3899  struct gdb_service *gdb_service = malloc(sizeof(struct gdb_service));
3900  if (!gdb_service) {
3901  LOG_ERROR("Out of memory");
3902  return ERROR_FAIL;
3903  }
3904 
3905  LOG_TARGET_INFO(target, "starting gdb server on %s", port);
3906 
3908  gdb_service->core[0] = -1;
3909  gdb_service->core[1] = -1;
3911 
3912  int retval = add_service(&gdb_service_driver, port,
3914  if (retval != ERROR_OK) {
3915  free(gdb_service);
3916  return retval;
3917  }
3918 
3919  /* initialize all targets gdb service with the same pointer */
3920  struct target_list *head;
3922  struct target *curr = head->target;
3923  if (curr != target)
3924  curr->gdb_service = gdb_service;
3925  }
3926 
3927  return ERROR_OK;
3928 }
3929 
3930 static int gdb_target_add_one(struct target *target)
3931 {
3932  /* one gdb instance per smp list */
3933  if ((target->smp) && (target->gdb_service))
3934  return ERROR_OK;
3935 
3936  /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3938  LOG_TARGET_DEBUG(target, "skip gdb server");
3939  return ERROR_OK;
3940  }
3941 
3942  if (target->gdb_port_override) {
3943  if (strcmp(target->gdb_port_override, "disabled") == 0) {
3944  LOG_TARGET_INFO(target, "gdb port disabled");
3945  return ERROR_OK;
3946  }
3948  }
3949 
3950  if (strcmp(gdb_port_next, "disabled") == 0) {
3951  LOG_TARGET_INFO(target, "gdb port disabled");
3952  return ERROR_OK;
3953  }
3954 
3955  int retval = gdb_target_start(target, gdb_port_next);
3956  if (retval == ERROR_OK) {
3957  /* save the port number so can be queried with
3958  * $target_name cget -gdb-port
3959  */
3961 
3962  long portnumber;
3963  /* If we can parse the port number
3964  * then we increment the port number for the next target.
3965  */
3966  char *end;
3967  portnumber = strtol(gdb_port_next, &end, 0);
3968  if (!*end) {
3969  if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
3970  free(gdb_port_next);
3971  if (portnumber) {
3972  gdb_port_next = alloc_printf("%ld", portnumber+1);
3973  } else {
3974  /* Don't increment if gdb_port is 0, since we're just
3975  * trying to allocate an unused port. */
3976  gdb_port_next = strdup("0");
3977  }
3978  }
3979  } else if (strcmp(gdb_port_next, "pipe") == 0) {
3980  free(gdb_port_next);
3981  gdb_port_next = strdup("disabled");
3982  }
3983  }
3984  return retval;
3985 }
3986 
3988 {
3989  if (!target) {
3990  LOG_WARNING("gdb services need one or more targets defined");
3991  return ERROR_OK;
3992  }
3993 
3994  while (target) {
3995  int retval = gdb_target_add_one(target);
3996  if (retval != ERROR_OK)
3997  return retval;
3998 
3999  target = target->next;
4000  }
4001 
4002  return ERROR_OK;
4003 }
4004 
4005 COMMAND_HANDLER(handle_gdb_sync_command)
4006 {
4007  if (CMD_ARGC != 0)
4009 
4010  if (!current_gdb_connection) {
4012  "gdb sync command can only be run from within gdb using \"monitor gdb sync\"");
4013  return ERROR_FAIL;
4014  }
4015 
4016  current_gdb_connection->sync = true;
4017 
4018  return ERROR_OK;
4019 }
4020 
4021 COMMAND_HANDLER(handle_gdb_port_command)
4022 {
4023  int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
4024  if (retval == ERROR_OK) {
4025  free(gdb_port_next);
4026  gdb_port_next = strdup(gdb_port);
4027  }
4028  return retval;
4029 }
4030 
4031 COMMAND_HANDLER(handle_gdb_memory_map_command)
4032 {
4033  if (CMD_ARGC != 1)
4035 
4037  return ERROR_OK;
4038 }
4039 
4040 COMMAND_HANDLER(handle_gdb_flash_program_command)
4041 {
4042  if (CMD_ARGC != 1)
4044 
4046  return ERROR_OK;
4047 }
4048 
4049 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
4050 {
4051  if (CMD_ARGC != 1)
4053 
4055  return ERROR_OK;
4056 }
4057 
4058 COMMAND_HANDLER(handle_gdb_report_register_access_error)
4059 {
4060  if (CMD_ARGC != 1)
4062 
4064  return ERROR_OK;
4065 }
4066 
4067 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
4068 {
4069  if (CMD_ARGC == 0) {
4070  /* nothing */
4071  } else if (CMD_ARGC == 1) {
4073  if (strcmp(CMD_ARGV[0], "hard") == 0)
4075  else if (strcmp(CMD_ARGV[0], "soft") == 0)
4077  else if (strcmp(CMD_ARGV[0], "disable") == 0)
4079  } else
4082  LOG_USER("force %s breakpoints",
4083  (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
4084  else
4085  LOG_USER("breakpoint type is not overridden");
4086 
4087  return ERROR_OK;
4088 }
4089 
4090 COMMAND_HANDLER(handle_gdb_target_description_command)
4091 {
4092  if (CMD_ARGC != 1)
4094 
4096  return ERROR_OK;
4097 }
4098 
4099 COMMAND_HANDLER(handle_gdb_save_tdesc_command)
4100 {
4101  char *tdesc;
4102  uint32_t tdesc_length;
4104 
4105  int retval = gdb_generate_target_description(target, &tdesc);
4106  if (retval != ERROR_OK) {
4107  LOG_ERROR("Unable to Generate Target Description");
4108  return ERROR_FAIL;
4109  }
4110 
4111  tdesc_length = strlen(tdesc);
4112 
4113  struct fileio *fileio;
4114  size_t size_written;
4115 
4116  char *tdesc_filename = alloc_printf("%s.xml", target_type_name(target));
4117  if (!tdesc_filename) {
4118  retval = ERROR_FAIL;
4119  goto out;
4120  }
4121 
4122  retval = fileio_open(&fileio, tdesc_filename, FILEIO_WRITE, FILEIO_TEXT);
4123 
4124  if (retval != ERROR_OK) {
4125  LOG_ERROR("Can't open %s for writing", tdesc_filename);
4126  goto out;
4127  }
4128 
4129  retval = fileio_write(fileio, tdesc_length, tdesc, &size_written);
4130 
4132 
4133  if (retval != ERROR_OK)
4134  LOG_ERROR("Error while writing the tdesc file");
4135 
4136 out:
4137  free(tdesc_filename);
4138  free(tdesc);
4139 
4140  return retval;
4141 }
4142 
4143 static const struct command_registration gdb_subcommand_handlers[] = {
4144  {
4145  .name = "sync",
4146  .handler = handle_gdb_sync_command,
4147  .mode = COMMAND_ANY,
4148  .help = "next stepi will return immediately allowing "
4149  "GDB to fetch register state without affecting "
4150  "target state",
4151  .usage = ""
4152  },
4153  {
4154  .name = "port",
4155  .handler = handle_gdb_port_command,
4156  .mode = COMMAND_CONFIG,
4157  .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4158  "server listens for the next port number after the "
4159  "base port number specified. "
4160  "No arguments reports GDB port. \"pipe\" means listen to stdin "
4161  "output to stdout, an integer is base port number, \"disabled\" disables "
4162  "port. Any other string is are interpreted as named pipe to listen to. "
4163  "Output pipe is the same name as input pipe, but with 'o' appended.",
4164  .usage = "[port_num]",
4165  },
4166  {
4167  .name = "memory_map",
4168  .handler = handle_gdb_memory_map_command,
4169  .mode = COMMAND_CONFIG,
4170  .help = "enable or disable memory map",
4171  .usage = "('enable'|'disable')"
4172  },
4173  {
4174  .name = "flash_program",
4175  .handler = handle_gdb_flash_program_command,
4176  .mode = COMMAND_CONFIG,
4177  .help = "enable or disable flash program",
4178  .usage = "('enable'|'disable')"
4179  },
4180  {
4181  .name = "report_data_abort",
4182  .handler = handle_gdb_report_data_abort_command,
4183  .mode = COMMAND_CONFIG,
4184  .help = "enable or disable reporting data aborts",
4185  .usage = "('enable'|'disable')"
4186  },
4187  {
4188  .name = "report_register_access_error",
4189  .handler = handle_gdb_report_register_access_error,
4190  .mode = COMMAND_CONFIG,
4191  .help = "enable or disable reporting register access errors",
4192  .usage = "('enable'|'disable')"
4193  },
4194  {
4195  .name = "breakpoint_override",
4196  .handler = handle_gdb_breakpoint_override_command,
4197  .mode = COMMAND_ANY,
4198  .help = "Display or specify type of breakpoint "
4199  "to be used by gdb 'break' commands.",
4200  .usage = "('hard'|'soft'|'disable')"
4201  },
4202  {
4203  .name = "target_description",
4204  .handler = handle_gdb_target_description_command,
4205  .mode = COMMAND_CONFIG,
4206  .help = "enable or disable target description",
4207  .usage = "('enable'|'disable')"
4208  },
4209  {
4210  .name = "save_tdesc",
4211  .handler = handle_gdb_save_tdesc_command,
4212  .mode = COMMAND_EXEC,
4213  .help = "Save the target description file",
4214  .usage = "",
4215  },
4217 };
4218 
4219 static const struct command_registration gdb_command_handlers[] = {
4220  {
4221  .name = "gdb",
4222  .mode = COMMAND_ANY,
4223  .help = "GDB commands",
4224  .chain = gdb_subcommand_handlers,
4225  .usage = "",
4226  },
4228 };
4229 
4231 {
4232  gdb_port = strdup("3333");
4233  gdb_port_next = strdup("3333");
4234  return register_commands(cmd_ctx, NULL, gdb_command_handlers);
4235 }
4236 
4238 {
4239  free(gdb_port);
4240  free(gdb_port_next);
4241 }
4242 
4244 {
4245  return gdb_actual_connections;
4246 }
const char * group
Definition: armv4_5.c:367
const char * name
Definition: armv4_5.c:76
const char * feature
Definition: armv4_5.c:368
struct reg_data_type * data_type
Definition: armv7m.c:105
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t length)
Convert binary data into a string of hexadecimal pairs.
Definition: binarybuffer.c:380
size_t unhexify(uint8_t *bin, const char *hex, size_t count)
Convert a string of hexadecimal pairs into its binary representation.
Definition: binarybuffer.c:342
int watchpoint_add(struct target *target, target_addr_t address, unsigned int length, enum watchpoint_rw rw, uint64_t value, uint64_t mask)
Definition: breakpoints.c:551
int breakpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:346
int watchpoint_hit(struct target *target, enum watchpoint_rw *rw, target_addr_t *address)
Definition: breakpoints.c:627
int watchpoint_remove(struct target *target, target_addr_t address)
Definition: breakpoints.c:588
int breakpoint_add(struct target *target, target_addr_t address, unsigned int length, enum breakpoint_type type)
Definition: breakpoints.c:216
int watchpoint_remove_all(struct target *target)
Definition: breakpoints.c:467
int breakpoint_remove_all(struct target *target)
Definition: breakpoints.c:462
breakpoint_type
Definition: breakpoints.h:17
@ BKPT_HARD
Definition: breakpoints.h:18
@ BKPT_SOFT
Definition: breakpoints.h:19
#define WATCHPOINT_IGNORE_DATA_VALUE_MASK
Definition: breakpoints.h:39
watchpoint_rw
Definition: breakpoints.h:22
@ WPT_ACCESS
Definition: breakpoints.h:23
@ WPT_READ
Definition: breakpoints.h:23
@ WPT_WRITE
Definition: breakpoints.h:23
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
int command_run_linef(struct command_context *context, const char *format,...)
Definition: command.c:553
void command_set_output_handler(struct command_context *context, command_output_handler_t output_handler, void *priv)
Definition: command.c:568
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define PRINTF_ATTRIBUTE_FORMAT
Definition: command.h:27
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
int parse_long(const char *str, long *ul)
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:536
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint32_t sector_size
Sector size.
Definition: dw-spi-helper.h:1
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
enum esirisc_reg_num number
Definition: esirisc.c:87
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
uint8_t length
Definition: esp_usb_jtag.c:1
#define ERROR_FLASH_DST_OUT_OF_BANK
Definition: flash/common.h:31
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
unsigned int flash_get_bank_count(void)
int flash_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length)
Erases length bytes in the target flash, starting at addr.
int flash_write(struct target *target, struct image *image, uint32_t *written, bool erase)
Writes image into the target flash.
int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
Returns the flash bank like get_flash_bank_by_name(), without probing.
static int gdb_read_memory_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1535
static void gdb_fileio_reply(struct target *target, struct connection *connection)
Definition: gdb_server.c:873
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:3897
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:3849
static int gdb_v_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:3318
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:3888
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:3570
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:1272
static int gdb_target_add_one(struct target *target)
Definition: gdb_server.c:3930
static void gdb_sig_halted(struct connection *connection)
Definition: gdb_server.c:3563
static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
Definition: gdb_server.c:3276
static int gdb_reg_pos(struct target *target, int pos, int len)
Definition: gdb_server.c:1194
static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
Definition: gdb_server.c:2702
static struct gdb_connection * current_gdb_connection
Definition: gdb_server.c:112
COMMAND_HANDLER(handle_gdb_sync_command)
Definition: gdb_server.c:4005
static int gdb_detach(struct connection *connection)
Definition: gdb_server.c:3486
static int compare_bank(const void *a, const void *b)
Definition: gdb_server.c:1928
#define CTRL(c)
Definition: gdb_server.c:53
int gdb_register_commands(struct command_context *cmd_ctx)
Definition: gdb_server.c:4230
static void gdb_keep_client_alive(struct connection *connection)
Definition: gdb_server.c:3867
static int gdb_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
Definition: gdb_server.c:986
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:1250
static int gdb_query_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:2801
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:2758
static char * next_hex_encoded_field(const char **str, char sep)
Definition: gdb_server.c:3226
static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
Definition: gdb_server.c:3261
int gdb_target_add_all(struct target *target)
Definition: gdb_server.c:3987
static int gdb_set_registers_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1340
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:2174
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:1901
static int gdb_fileio_response_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:3500
static int gdb_memory_map(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1942
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:1528
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:961
static int gdb_connection_closed(struct connection *connection)
Definition: gdb_server.c:1124
static const struct command_registration gdb_command_handlers[]
Definition: gdb_server.c:4219
static int gdb_input(struct connection *connection)
Definition: gdb_server.c:3819
static int gdb_new_connection(struct connection *connection)
Definition: gdb_server.c:1009
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:2308
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:2603
static int gdb_get_register_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1403
int gdb_get_actual_connections(void)
Definition: gdb_server.c:4243
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:1610
static __attribute__((format(PRINTF_ATTRIBUTE_FORMAT, 5, 6)))
Definition: gdb_server.c:1865
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:4237
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:3039
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:1661
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:2351
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:2657
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:2463
static void gdb_str_to_target(struct target *target, char *tstr, struct reg *reg)
Definition: gdb_server.c:1211
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:1164
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:1171
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:2104
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:3545
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:2150
static int gdb_step_continue_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1740
static int gdb_breakpoint_watchpoint_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1768
static int gdb_set_register_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1454
static const struct command_registration gdb_subcommand_handlers[]
Definition: gdb_server.c:4143
static void gdb_target_to_reg(struct target *target, char const *tstr, int str_len, uint8_t *bin)
Definition: gdb_server.c:1228
#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:337
void log_printf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format,...)
Definition: log.c:197
int log_add_callback(log_callback_fn fn, void *priv)
Definition: log.c:312
static int64_t start
Definition: log.c:38
void log_socket_error(const char *socket_desc)
Definition: log.c:502
void kept_alive(void)
Definition: log.c:461
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:526
char * alloc_printf(const char *format,...)
Definition: log.c:382
#define LOG_TARGET_INFO(target, fmt_str,...)
Definition: log.h:167
#define LOG_USER(expr ...)
Definition: log.h:150
#define LOG_TARGET_WARNING(target, fmt_str,...)
Definition: log.h:173
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:192
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_TARGET_ERROR(target, fmt_str,...)
Definition: log.h:176
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:164
#define LOG_USER_N(expr ...)
Definition: log.h:153
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_LEVEL_IS(FOO)
Definition: log.h:112
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_INFO
Definition: log.h:54
@ LOG_LVL_DEBUG
Definition: log.h:55
Upper level NOR flash interfaces.
void flash_set_dirty(void)
Forces targets to re-examine their erase/protection state.
reg_type
Definition: register.h:19
@ REG_TYPE_INT
Definition: register.h:21
@ REG_TYPE_UINT16
Definition: register.h:29
@ REG_TYPE_BOOL
Definition: register.h:20
@ REG_TYPE_IEEE_DOUBLE
Definition: register.h:37
@ REG_TYPE_INT64
Definition: register.h:25
@ REG_TYPE_INT16
Definition: register.h:23
@ REG_TYPE_UINT32
Definition: register.h:30
@ REG_TYPE_CODE_PTR
Definition: register.h:33
@ REG_TYPE_DATA_PTR
Definition: register.h:34
@ REG_TYPE_INT32
Definition: register.h:24
@ REG_TYPE_INT128
Definition: register.h:26
@ REG_TYPE_UINT128
Definition: register.h:32
@ REG_TYPE_UINT
Definition: register.h:27
@ REG_TYPE_FLOAT
Definition: register.h:35
@ REG_TYPE_UINT64
Definition: register.h:31
@ REG_TYPE_INT8
Definition: register.h:22
@ REG_TYPE_ARCH_DEFINED
Definition: register.h:38
@ REG_TYPE_IEEE_SINGLE
Definition: register.h:36
@ REG_TYPE_UINT8
Definition: register.h:28
@ REG_TYPE_CLASS_VECTOR
Definition: register.h:93
@ REG_TYPE_CLASS_FLAGS
Definition: register.h:96
@ REG_TYPE_CLASS_UNION
Definition: register.h:94
@ REG_TYPE_CLASS_STRUCT
Definition: register.h:95
char * strndup(const char *s, size_t n)
Definition: replacements.c: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
struct target * rtos_swbp_target(struct target *target, target_addr_t address, uint32_t length, enum breakpoint_type type)
Definition: rtos.c:781
int gdb_thread_packet(struct connection *connection, char const *packet, int packet_size)
Definition: rtos.c:150
int rtos_set_reg(struct connection *connection, int reg_num, uint8_t *reg_value)
Definition: rtos.c:641
int rtos_get_gdb_reg_list(struct connection *connection)
Return a list of general registers.
Definition: rtos.c:608
int rtos_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: rtos.c:766
int rtos_update_threads(struct target *target)
Definition: rtos.c:732
int rtos_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: rtos.c:758
bool rtos_needs_fake_step(struct target *target, int64_t thread_id)
Definition: rtos.c:774
struct rtos * rtos_from_target(struct target *target)
Get the RTOS from the target itself, or from one of the targets in the same SMP node,...
Definition: rtos.c:719
int rtos_get_gdb_reg(struct connection *connection, int reg_num)
Look through all registers to find this register.
Definition: rtos.c:551
#define GDB_THREAD_PACKET_NOT_CONSUMED
Definition: rtos.h:129
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct target * target
Definition: rtt/rtt.c:26
int connection_write(struct connection *connection, const void *data, int len)
Definition: server.c:742
int add_service(const struct service_driver *driver, const char *port, int max_connections, void *priv)
Definition: server.c:206
#define ERROR_SERVER_REMOTE_CLOSED
Definition: server.h:121
@ 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:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
struct command_context * cmd_ctx
Definition: server.h:40
void * priv
Definition: server.h:43
int fd
Definition: server.h:37
struct service * service
Definition: server.h:41
bool input_pending
Definition: server.h:42
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
Definition: nor/core.h:118
target_addr_t base
The base address of this bank.
Definition: nor/core.h:84
uint32_t size
The size of this chip bank, in bytes.
Definition: nor/core.h:85
unsigned int num_sectors
The number of sectors on this chip.
Definition: nor/core.h:116
bool read_only
a ROM region - mainly to list in gdb memory map
Definition: nor/core.h:87
struct target * target
Target to which this bank belongs.
Definition: nor/core.h:78
char * name
Definition: nor/core.h:76
uint32_t offset
Bus offset from start of the flash chip (in bytes).
Definition: nor/core.h:30
uint32_t size
Number of bytes in this flash sector.
Definition: nor/core.h:32
enum gdb_output_flag output_flag
Definition: gdb_server.c: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:73
Definition: rtos.h:36
const struct rtos_type * type
Definition: rtos.h:37
int thread_count
Definition: rtos.h:47
struct thread_detail * thread_details
Definition: rtos.h:46
int(* gdb_target_for_threadid)(struct connection *connection, int64_t thread_id, struct target **p_target)
Definition: rtos.h:49
threadid_t current_thread
Definition: rtos.h:45
int64_t current_threadid
Definition: rtos.h:43
char * cmdline
The semihosting command line to be passed to the target.
const char * name
the name of the server
Definition: server.h:49
void * priv
Definition: server.h:83
char * port
Definition: server.h:71
enum connection_type type
Definition: server.h:70
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:298
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:33
char * thread_name_str
Definition: rtos.h:32
bool exists
Definition: rtos.h:31
threadid_t threadid
Definition: rtos.h:30
long tv_sec
Definition: replacements.h:46
long tv_usec
Definition: replacements.h:47
int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
Obtain file-I/O information from target for GDB to do syscall.
Definition: target.c:1444
struct target * all_targets
Definition: target.c:115
int target_call_event_callbacks(struct target *target, enum target_event event)
Definition: target.c:1782
int target_unregister_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1705
int target_register_event_callback(int(*callback)(struct target *target, enum target_event event, void *priv), void *priv)
Definition: target.c:1610
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:1408
bool target_supports_gdb_connection(const struct target *target)
Check if target allows GDB connections.
Definition: target.c:1419
int target_call_timer_callbacks_now(void)
Invoke this to ensure that e.g.
Definition: target.c:1902
int target_checksum_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t *crc)
Definition: target.c:2484
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2359
target_addr_t target_address_max(struct target *target)
Return the highest accessible address for this target.
Definition: target.c:1462
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:1453
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2424
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:1386
const char * target_debug_reason_str(enum target_debug_reason reason)
Definition: target.c:6732
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:1379
int target_step(struct target *target, bool current, target_addr_t address, bool handle_breakpoints)
Step the target.
Definition: target.c:1428
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:795
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:802
@ TARGET_LITTLE_ENDIAN
Definition: target.h:85
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:799
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