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