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