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