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