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 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 int gdb_use_memory_map = 1;
132 /* enabled by default*/
133 static int gdb_flash_program = 1;
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 */
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, 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, char *packet)
355 {
357  return;
358 
361 
362  /* Avoid dumping non-printable characters to the terminal */
363  const unsigned 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, 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  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, 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_ERROR("Target %s not examined yet, refuse gdb connection %d!",
1073  }
1075 
1076  if (target->state != TARGET_HALTED)
1077  LOG_WARNING("GDB connection %d on target %s 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_DEBUG("{%d} GDB Close, Target: %s, state: %s, gdb_actual_connections=%d",
1110 
1111  /* see if an image built with vFlash commands is left */
1116  }
1117 
1118  /* if this connection registered a debug-message receiver delete it */
1120 
1121  free(connection->priv);
1122  connection->priv = NULL;
1123 
1125 
1127 
1129 
1130  return ERROR_OK;
1131 }
1132 
1133 static void gdb_send_error(struct connection *connection, uint8_t the_error)
1134 {
1135  char err[4];
1136  snprintf(err, 4, "E%2.2X", the_error);
1137  gdb_put_packet(connection, err, 3);
1138 }
1139 
1141  char const *packet, int packet_size)
1142 {
1144  struct gdb_connection *gdb_con = connection->priv;
1145  char sig_reply[4];
1146  int signal_var;
1147 
1148  if (!gdb_con->attached) {
1149  /* if we are here we have received a kill packet
1150  * reply W stop reply otherwise gdb gets very unhappy */
1151  gdb_put_packet(connection, "W00", 3);
1152  return ERROR_OK;
1153  }
1154 
1155  signal_var = gdb_last_signal(target);
1156 
1157  snprintf(sig_reply, 4, "S%2.2x", signal_var);
1158  gdb_put_packet(connection, sig_reply, 3);
1159 
1160  return ERROR_OK;
1161 }
1162 
1163 static inline int gdb_reg_pos(struct target *target, int pos, int len)
1164 {
1166  return pos;
1167  else
1168  return len - 1 - pos;
1169 }
1170 
1171 /* Convert register to string of bytes. NB! The # of bits in the
1172  * register might be non-divisible by 8(a byte), in which
1173  * case an entire byte is shown.
1174  *
1175  * NB! the format on the wire is the target endianness
1176  *
1177  * The format of reg->value is little endian
1178  *
1179  */
1180 static void gdb_str_to_target(struct target *target,
1181  char *tstr, struct reg *reg)
1182 {
1183  int i;
1184 
1185  uint8_t *buf;
1186  int buf_len;
1187  buf = reg->value;
1188  buf_len = DIV_ROUND_UP(reg->size, 8);
1189 
1190  for (i = 0; i < buf_len; i++) {
1191  int j = gdb_reg_pos(target, i, buf_len);
1192  tstr += sprintf(tstr, "%02x", buf[j]);
1193  }
1194 }
1195 
1196 /* copy over in register buffer */
1197 static void gdb_target_to_reg(struct target *target,
1198  char const *tstr, int str_len, uint8_t *bin)
1199 {
1200  if (str_len % 2) {
1201  LOG_ERROR("BUG: gdb value with uneven number of characters encountered");
1202  exit(-1);
1203  }
1204 
1205  int i;
1206  for (i = 0; i < str_len; i += 2) {
1207  unsigned t;
1208  if (sscanf(tstr + i, "%02x", &t) != 1) {
1209  LOG_ERROR("BUG: unable to convert register value");
1210  exit(-1);
1211  }
1212 
1213  int j = gdb_reg_pos(target, i/2, str_len/2);
1214  bin[j] = t;
1215  }
1216 }
1217 
1218 /* get register value if needed and fill the buffer accordingly */
1219 static int gdb_get_reg_value_as_str(struct target *target, char *tstr, struct reg *reg)
1220 {
1221  int retval = ERROR_OK;
1222 
1223  if (!reg->valid)
1224  retval = reg->type->get(reg);
1225 
1226  const unsigned int len = DIV_ROUND_UP(reg->size, 8) * 2;
1227  switch (retval) {
1228  case ERROR_OK:
1229  gdb_str_to_target(target, tstr, reg);
1230  return ERROR_OK;
1232  memset(tstr, 'x', len);
1233  tstr[len] = '\0';
1234  return ERROR_OK;
1235  }
1236  return ERROR_FAIL;
1237 }
1238 
1240  char const *packet, int packet_size)
1241 {
1243  struct reg **reg_list;
1244  int reg_list_size;
1245  int retval;
1246  int reg_packet_size = 0;
1247  char *reg_packet;
1248  char *reg_packet_p;
1249  int i;
1250 
1251 #ifdef _DEBUG_GDB_IO_
1252  LOG_DEBUG("-");
1253 #endif
1254 
1256  return ERROR_OK;
1257 
1258  retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1260  if (retval != ERROR_OK)
1261  return gdb_error(connection, retval);
1262 
1263  for (i = 0; i < reg_list_size; i++) {
1264  if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
1265  continue;
1266  reg_packet_size += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1267  }
1268 
1269  assert(reg_packet_size > 0);
1270 
1271  reg_packet = malloc(reg_packet_size + 1); /* plus one for string termination null */
1272  if (!reg_packet)
1273  return ERROR_FAIL;
1274 
1275  reg_packet_p = reg_packet;
1276 
1277  for (i = 0; i < reg_list_size; i++) {
1278  if (!reg_list[i] || reg_list[i]->exist == false || reg_list[i]->hidden)
1279  continue;
1280  if (gdb_get_reg_value_as_str(target, reg_packet_p, reg_list[i]) != ERROR_OK) {
1281  free(reg_packet);
1282  free(reg_list);
1283  return gdb_error(connection, retval);
1284  }
1285  reg_packet_p += DIV_ROUND_UP(reg_list[i]->size, 8) * 2;
1286  }
1287 
1288 #ifdef _DEBUG_GDB_IO_
1289  {
1290  char *reg_packet_p_debug;
1291  reg_packet_p_debug = strndup(reg_packet, reg_packet_size);
1292  LOG_DEBUG("reg_packet: %s", reg_packet_p_debug);
1293  free(reg_packet_p_debug);
1294  }
1295 #endif
1296 
1297  gdb_put_packet(connection, reg_packet, reg_packet_size);
1298  free(reg_packet);
1299 
1300  free(reg_list);
1301 
1302  return ERROR_OK;
1303 }
1304 
1306  char const *packet, int packet_size)
1307 {
1309  int i;
1310  struct reg **reg_list;
1311  int reg_list_size;
1312  int retval;
1313  char const *packet_p;
1314 
1315 #ifdef _DEBUG_GDB_IO_
1316  LOG_DEBUG("-");
1317 #endif
1318 
1319  /* skip command character */
1320  packet++;
1321  packet_size--;
1322 
1323  if (packet_size % 2) {
1324  LOG_WARNING("GDB set_registers packet with uneven characters received, dropping connection");
1326  }
1327 
1328  retval = target_get_gdb_reg_list(target, &reg_list, &reg_list_size,
1330  if (retval != ERROR_OK)
1331  return gdb_error(connection, retval);
1332 
1333  packet_p = packet;
1334  for (i = 0; i < reg_list_size; i++) {
1335  uint8_t *bin_buf;
1336  if (!reg_list[i] || !reg_list[i]->exist || reg_list[i]->hidden)
1337  continue;
1338  int chars = (DIV_ROUND_UP(reg_list[i]->size, 8) * 2);
1339 
1340  if (packet_p + chars > packet + packet_size)
1341  LOG_ERROR("BUG: register packet is too small for registers");
1342 
1343  bin_buf = malloc(DIV_ROUND_UP(reg_list[i]->size, 8));
1344  gdb_target_to_reg(target, packet_p, chars, bin_buf);
1345 
1346  retval = reg_list[i]->type->set(reg_list[i], bin_buf);
1347  if (retval != ERROR_OK && gdb_report_register_access_error) {
1348  LOG_DEBUG("Couldn't set register %s.", reg_list[i]->name);
1349  free(reg_list);
1350  free(bin_buf);
1351  return gdb_error(connection, retval);
1352  }
1353 
1354  /* advance packet pointer */
1355  packet_p += chars;
1356 
1357  free(bin_buf);
1358  }
1359 
1360  /* free struct reg *reg_list[] array allocated by get_gdb_reg_list */
1361  free(reg_list);
1362 
1363  gdb_put_packet(connection, "OK", 2);
1364 
1365  return ERROR_OK;
1366 }
1367 
1369  char const *packet, int packet_size)
1370 {
1372  char *reg_packet;
1373  int reg_num = strtoul(packet + 1, NULL, 16);
1374  struct reg **reg_list;
1375  int reg_list_size;
1376  int retval;
1377 
1378 #ifdef _DEBUG_GDB_IO_
1379  LOG_DEBUG("-");
1380 #endif
1381 
1382  if ((target->rtos) && (rtos_get_gdb_reg(connection, reg_num) == ERROR_OK))
1383  return ERROR_OK;
1384 
1385  retval = target_get_gdb_reg_list_noread(target, &reg_list, &reg_list_size,
1386  REG_CLASS_ALL);
1387  if (retval != ERROR_OK)
1388  return gdb_error(connection, retval);
1389 
1390  if ((reg_list_size <= reg_num) || !reg_list[reg_num] ||
1391  !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) {
1392  LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num);
1394  }
1395 
1396  reg_packet = calloc(DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2 + 1, 1); /* plus one for string termination null */
1397 
1398  if (gdb_get_reg_value_as_str(target, reg_packet, reg_list[reg_num]) != ERROR_OK) {
1399  free(reg_packet);
1400  free(reg_list);
1401  return gdb_error(connection, retval);
1402  }
1403 
1404  gdb_put_packet(connection, reg_packet, DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2);
1405 
1406  free(reg_list);
1407  free(reg_packet);
1408 
1409  return ERROR_OK;
1410 }
1411 
1413  char const *packet, int packet_size)
1414 {
1416  char *separator;
1417  int reg_num = strtoul(packet + 1, &separator, 16);
1418  struct reg **reg_list;
1419  int reg_list_size;
1420  int retval;
1421 
1422 #ifdef _DEBUG_GDB_IO_
1423  LOG_DEBUG("-");
1424 #endif
1425 
1426  if (*separator != '=') {
1427  LOG_ERROR("GDB 'set register packet', but no '=' following the register number");
1429  }
1430  size_t chars = strlen(separator + 1);
1431  uint8_t *bin_buf = malloc(chars / 2);
1432  gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1433 
1434  if ((target->rtos) &&
1435  (rtos_set_reg(connection, reg_num, bin_buf) == ERROR_OK)) {
1436  free(bin_buf);
1437  gdb_put_packet(connection, "OK", 2);
1438  return ERROR_OK;
1439  }
1440 
1441  retval = target_get_gdb_reg_list_noread(target, &reg_list, &reg_list_size,
1442  REG_CLASS_ALL);
1443  if (retval != ERROR_OK) {
1444  free(bin_buf);
1445  return gdb_error(connection, retval);
1446  }
1447 
1448  if ((reg_list_size <= reg_num) || !reg_list[reg_num] ||
1449  !reg_list[reg_num]->exist || reg_list[reg_num]->hidden) {
1450  LOG_ERROR("gdb requested a non-existing register (reg_num=%d)", reg_num);
1451  free(bin_buf);
1452  free(reg_list);
1454  }
1455 
1456  if (chars != (DIV_ROUND_UP(reg_list[reg_num]->size, 8) * 2)) {
1457  LOG_ERROR("gdb sent %zu bits for a %" PRIu32 "-bit register (%s)",
1458  chars * 4, reg_list[reg_num]->size, reg_list[reg_num]->name);
1459  free(bin_buf);
1460  free(reg_list);
1462  }
1463 
1464  gdb_target_to_reg(target, separator + 1, chars, bin_buf);
1465 
1466  retval = reg_list[reg_num]->type->set(reg_list[reg_num], bin_buf);
1467  if (retval != ERROR_OK && gdb_report_register_access_error) {
1468  LOG_DEBUG("Couldn't set register %s.", reg_list[reg_num]->name);
1469  free(bin_buf);
1470  free(reg_list);
1471  return gdb_error(connection, retval);
1472  }
1473 
1474  gdb_put_packet(connection, "OK", 2);
1475 
1476  free(bin_buf);
1477  free(reg_list);
1478 
1479  return ERROR_OK;
1480 }
1481 
1482 /* No attempt is made to translate the "retval" to
1483  * GDB speak. This has to be done at the calling
1484  * site as no mapping really exists.
1485  */
1486 static int gdb_error(struct connection *connection, int retval)
1487 {
1488  LOG_DEBUG("Reporting %i to GDB as generic error", retval);
1489  gdb_send_error(connection, EFAULT);
1490  return ERROR_OK;
1491 }
1492 
1494  char const *packet, int packet_size)
1495 {
1497  char *separator;
1498  uint64_t addr = 0;
1499  uint32_t len = 0;
1500 
1501  uint8_t *buffer;
1502  char *hex_buffer;
1503 
1504  int retval = ERROR_OK;
1505 
1506  /* skip command character */
1507  packet++;
1508 
1509  addr = strtoull(packet, &separator, 16);
1510 
1511  if (*separator != ',') {
1512  LOG_ERROR("incomplete read memory packet received, dropping connection");
1514  }
1515 
1516  len = strtoul(separator + 1, NULL, 16);
1517 
1518  if (!len) {
1519  LOG_WARNING("invalid read memory packet received (len == 0)");
1520  gdb_put_packet(connection, "", 0);
1521  return ERROR_OK;
1522  }
1523 
1524  buffer = malloc(len);
1525 
1526  LOG_DEBUG("addr: 0x%16.16" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1527 
1528  retval = ERROR_NOT_IMPLEMENTED;
1529  if (target->rtos)
1530  retval = rtos_read_buffer(target, addr, len, buffer);
1531  if (retval == ERROR_NOT_IMPLEMENTED)
1532  retval = target_read_buffer(target, addr, len, buffer);
1533 
1534  if ((retval != ERROR_OK) && !gdb_report_data_abort) {
1535  /* TODO : Here we have to lie and send back all zero's lest stack traces won't work.
1536  * At some point this might be fixed in GDB, in which case this code can be removed.
1537  *
1538  * OpenOCD developers are acutely aware of this problem, but there is nothing
1539  * gained by involving the user in this problem that hopefully will get resolved
1540  * eventually
1541  *
1542  * http://sourceware.org/cgi-bin/gnatsweb.pl? \
1543  * cmd = view%20audit-trail&database = gdb&pr = 2395
1544  *
1545  * For now, the default is to fix up things to make current GDB versions work.
1546  * This can be overwritten using the gdb_report_data_abort <'enable'|'disable'> command.
1547  */
1548  memset(buffer, 0, len);
1549  retval = ERROR_OK;
1550  }
1551 
1552  if (retval == ERROR_OK) {
1553  hex_buffer = malloc(len * 2 + 1);
1554 
1555  size_t pkt_len = hexify(hex_buffer, buffer, len, len * 2 + 1);
1556 
1557  gdb_put_packet(connection, hex_buffer, pkt_len);
1558 
1559  free(hex_buffer);
1560  } else
1561  retval = gdb_error(connection, retval);
1562 
1563  free(buffer);
1564 
1565  return retval;
1566 }
1567 
1569  char const *packet, int packet_size)
1570 {
1572  char *separator;
1573  uint64_t addr = 0;
1574  uint32_t len = 0;
1575 
1576  uint8_t *buffer;
1577  int retval;
1578 
1579  /* skip command character */
1580  packet++;
1581 
1582  addr = strtoull(packet, &separator, 16);
1583 
1584  if (*separator != ',') {
1585  LOG_ERROR("incomplete write memory packet received, dropping connection");
1587  }
1588 
1589  len = strtoul(separator + 1, &separator, 16);
1590 
1591  if (*(separator++) != ':') {
1592  LOG_ERROR("incomplete write memory packet received, dropping connection");
1594  }
1595 
1596  buffer = malloc(len);
1597 
1598  LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1599 
1600  if (unhexify(buffer, separator, len) != len)
1601  LOG_ERROR("unable to decode memory packet");
1602 
1603  retval = ERROR_NOT_IMPLEMENTED;
1604  if (target->rtos)
1605  retval = rtos_write_buffer(target, addr, len, buffer);
1606  if (retval == ERROR_NOT_IMPLEMENTED)
1607  retval = target_write_buffer(target, addr, len, buffer);
1608 
1609  if (retval == ERROR_OK)
1610  gdb_put_packet(connection, "OK", 2);
1611  else
1612  retval = gdb_error(connection, retval);
1613 
1614  free(buffer);
1615 
1616  return retval;
1617 }
1618 
1620  char const *packet, int packet_size)
1621 {
1623  char *separator;
1624  uint64_t addr = 0;
1625  uint32_t len = 0;
1626 
1627  int retval = ERROR_OK;
1628  /* Packets larger than fast_limit bytes will be acknowledged instantly on
1629  * the assumption that we're in a download and it's important to go as fast
1630  * as possible. */
1631  uint32_t fast_limit = 8;
1632 
1633  /* skip command character */
1634  packet++;
1635 
1636  addr = strtoull(packet, &separator, 16);
1637 
1638  if (*separator != ',') {
1639  LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1641  }
1642 
1643  len = strtoul(separator + 1, &separator, 16);
1644 
1645  if (*(separator++) != ':') {
1646  LOG_ERROR("incomplete write memory binary packet received, dropping connection");
1648  }
1649 
1651 
1653  retval = ERROR_FAIL;
1654 
1655  if (retval == ERROR_OK) {
1656  if (len >= fast_limit) {
1657  /* By replying the packet *immediately* GDB will send us a new packet
1658  * while we write the last one to the target.
1659  * We only do this for larger writes, so that users who do something like:
1660  * p *((int*)0xdeadbeef)=8675309
1661  * will get immediate feedback that that write failed.
1662  */
1663  gdb_put_packet(connection, "OK", 2);
1664  }
1665  } else {
1666  retval = gdb_error(connection, retval);
1667  /* now that we have reported the memory write error, we can clear the condition */
1669  if (retval != ERROR_OK)
1670  return retval;
1671  }
1672 
1673  if (len) {
1674  LOG_DEBUG("addr: 0x%" PRIx64 ", len: 0x%8.8" PRIx32 "", addr, len);
1675 
1676  retval = ERROR_NOT_IMPLEMENTED;
1677  if (target->rtos)
1678  retval = rtos_write_buffer(target, addr, len, (uint8_t *)separator);
1679  if (retval == ERROR_NOT_IMPLEMENTED)
1680  retval = target_write_buffer(target, addr, len, (uint8_t *)separator);
1681 
1682  if (retval != ERROR_OK)
1684  }
1685 
1686  if (len < fast_limit) {
1687  if (retval != ERROR_OK) {
1688  gdb_error(connection, retval);
1690  } else {
1691  gdb_put_packet(connection, "OK", 2);
1692  }
1693  }
1694 
1695  return ERROR_OK;
1696 }
1697 
1699  char const *packet, int packet_size)
1700 {
1702  int current = 0;
1703  uint64_t address = 0x0;
1704  int retval = ERROR_OK;
1705 
1706  LOG_DEBUG("-");
1707 
1708  if (packet_size > 1)
1709  address = strtoull(packet + 1, NULL, 16);
1710  else
1711  current = 1;
1712 
1713  gdb_running_type = packet[0];
1714  if (packet[0] == 'c') {
1715  LOG_DEBUG("continue");
1716  /* resume at current address, don't handle breakpoints, not debugging */
1717  retval = target_resume(target, current, address, 0, 0);
1718  } else if (packet[0] == 's') {
1719  LOG_DEBUG("step");
1720  /* step at current or address, don't handle breakpoints */
1721  retval = target_step(target, current, address, 0);
1722  }
1723  return retval;
1724 }
1725 
1727  char const *packet, int packet_size)
1728 {
1730  int type;
1731  enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
1732  enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
1733  uint64_t address;
1734  uint32_t size;
1735  char *separator;
1736  int retval;
1737 
1738  LOG_DEBUG("[%s]", target_name(target));
1739 
1740  type = strtoul(packet + 1, &separator, 16);
1741 
1742  if (type == 0) /* memory breakpoint */
1743  bp_type = BKPT_SOFT;
1744  else if (type == 1) /* hardware breakpoint */
1745  bp_type = BKPT_HARD;
1746  else if (type == 2) /* write watchpoint */
1747  wp_type = WPT_WRITE;
1748  else if (type == 3) /* read watchpoint */
1749  wp_type = WPT_READ;
1750  else if (type == 4) /* access watchpoint */
1751  wp_type = WPT_ACCESS;
1752  else {
1753  LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
1755  }
1756 
1757  if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT) || (bp_type == BKPT_HARD)))
1758  bp_type = gdb_breakpoint_override_type;
1759 
1760  if (*separator != ',') {
1761  LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1763  }
1764 
1765  address = strtoull(separator + 1, &separator, 16);
1766 
1767  if (*separator != ',') {
1768  LOG_ERROR("incomplete breakpoint/watchpoint packet received, dropping connection");
1770  }
1771 
1772  size = strtoul(separator + 1, &separator, 16);
1773 
1774  switch (type) {
1775  case 0:
1776  case 1:
1777  if (packet[0] == 'Z') {
1778  retval = breakpoint_add(target, address, size, bp_type);
1779  if (retval == ERROR_NOT_IMPLEMENTED) {
1780  /* Send empty reply to report that breakpoints of this type are not supported */
1781  gdb_put_packet(connection, "", 0);
1782  } else if (retval != ERROR_OK) {
1783  retval = gdb_error(connection, retval);
1784  if (retval != ERROR_OK)
1785  return retval;
1786  } else
1787  gdb_put_packet(connection, "OK", 2);
1788  } else {
1789  breakpoint_remove(target, address);
1790  gdb_put_packet(connection, "OK", 2);
1791  }
1792  break;
1793  case 2:
1794  case 3:
1795  case 4:
1796  {
1797  if (packet[0] == 'Z') {
1798  retval = watchpoint_add(target, address, size, wp_type, 0, WATCHPOINT_IGNORE_DATA_VALUE_MASK);
1799  if (retval == ERROR_NOT_IMPLEMENTED) {
1800  /* Send empty reply to report that watchpoints of this type are not supported */
1801  gdb_put_packet(connection, "", 0);
1802  } else if (retval != ERROR_OK) {
1803  retval = gdb_error(connection, retval);
1804  if (retval != ERROR_OK)
1805  return retval;
1806  } else
1807  gdb_put_packet(connection, "OK", 2);
1808  } else {
1809  watchpoint_remove(target, address);
1810  gdb_put_packet(connection, "OK", 2);
1811  }
1812  break;
1813  }
1814  default:
1815  break;
1816  }
1817 
1818  return ERROR_OK;
1819 }
1820 
1821 /* print out a string and allocate more space as needed,
1822  * mainly used for XML at this point
1823  */
1824 static __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6))) void xml_printf(int *retval,
1825  char **xml, int *pos, int *size, const char *fmt, ...)
1826 {
1827  if (*retval != ERROR_OK)
1828  return;
1829  int first = 1;
1830 
1831  for (;; ) {
1832  if ((!*xml) || (!first)) {
1833  /* start by 0 to exercise all the code paths.
1834  * Need minimum 2 bytes to fit 1 char and 0 terminator. */
1835 
1836  *size = *size * 2 + 2;
1837  char *t = *xml;
1838  *xml = realloc(*xml, *size);
1839  if (!*xml) {
1840  free(t);
1841  *retval = ERROR_SERVER_REMOTE_CLOSED;
1842  return;
1843  }
1844  }
1845 
1846  va_list ap;
1847  int ret;
1848  va_start(ap, fmt);
1849  ret = vsnprintf(*xml + *pos, *size - *pos, fmt, ap);
1850  va_end(ap);
1851  if ((ret > 0) && ((ret + 1) < *size - *pos)) {
1852  *pos += ret;
1853  return;
1854  }
1855  /* there was just enough or not enough space, allocate more. */
1856  first = 0;
1857  }
1858 }
1859 
1860 static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
1861 {
1862  /* Locate the annex. */
1863  const char *annex_end = strchr(buf, ':');
1864  if (!annex_end)
1865  return ERROR_FAIL;
1866 
1867  /* After the read marker and annex, qXfer looks like a
1868  * traditional 'm' packet. */
1869  char *separator;
1870  *ofs = strtoul(annex_end + 1, &separator, 16);
1871 
1872  if (*separator != ',')
1873  return ERROR_FAIL;
1874 
1875  *len = strtoul(separator + 1, NULL, 16);
1876 
1877  /* Extract the annex if needed */
1878  if (annex) {
1879  *annex = strndup(buf, annex_end - buf);
1880  if (!*annex)
1881  return ERROR_FAIL;
1882  }
1883 
1884  return ERROR_OK;
1885 }
1886 
1887 static int compare_bank(const void *a, const void *b)
1888 {
1889  struct flash_bank *b1, *b2;
1890  b1 = *((struct flash_bank **)a);
1891  b2 = *((struct flash_bank **)b);
1892 
1893  if (b1->base == b2->base)
1894  return 0;
1895  else if (b1->base > b2->base)
1896  return 1;
1897  else
1898  return -1;
1899 }
1900 
1902  char const *packet, int packet_size)
1903 {
1904  /* We get away with only specifying flash here. Regions that are not
1905  * specified are treated as if we provided no memory map(if not we
1906  * could detect the holes and mark them as RAM).
1907  * Normally we only execute this code once, but no big deal if we
1908  * have to regenerate it a couple of times.
1909  */
1910 
1912  struct flash_bank *p;
1913  char *xml = NULL;
1914  int size = 0;
1915  int pos = 0;
1916  int retval = ERROR_OK;
1917  struct flash_bank **banks;
1918  int offset;
1919  int length;
1920  char *separator;
1921  target_addr_t ram_start = 0;
1922  unsigned int target_flash_banks = 0;
1923 
1924  /* skip command character */
1925  packet += 23;
1926 
1927  offset = strtoul(packet, &separator, 16);
1928  length = strtoul(separator + 1, &separator, 16);
1929 
1930  xml_printf(&retval, &xml, &pos, &size, "<memory-map>\n");
1931 
1932  /* Sort banks in ascending order. We need to report non-flash
1933  * memory as ram (or rather read/write) by default for GDB, since
1934  * it has no concept of non-cacheable read/write memory (i/o etc).
1935  */
1936  banks = malloc(sizeof(struct flash_bank *)*flash_get_bank_count());
1937 
1938  for (unsigned int i = 0; i < flash_get_bank_count(); i++) {
1940  if (p->target != target)
1941  continue;
1942  retval = get_flash_bank_by_num(i, &p);
1943  if (retval != ERROR_OK) {
1944  free(banks);
1945  gdb_error(connection, retval);
1946  return retval;
1947  }
1948  banks[target_flash_banks++] = p;
1949  }
1950 
1951  qsort(banks, target_flash_banks, sizeof(struct flash_bank *),
1952  compare_bank);
1953 
1954  for (unsigned int i = 0; i < target_flash_banks; i++) {
1955  unsigned sector_size = 0;
1956  unsigned group_len = 0;
1957 
1958  p = banks[i];
1959 
1960  if (ram_start < p->base)
1961  xml_printf(&retval, &xml, &pos, &size,
1962  "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
1963  "length=\"" TARGET_ADDR_FMT "\"/>\n",
1964  ram_start, p->base - ram_start);
1965 
1966  /* Report adjacent groups of same-size sectors. So for
1967  * example top boot CFI flash will list an initial region
1968  * with several large sectors (maybe 128KB) and several
1969  * smaller ones at the end (maybe 32KB). STR7 will have
1970  * regions with 8KB, 32KB, and 64KB sectors; etc.
1971  */
1972  for (unsigned int j = 0; j < p->num_sectors; j++) {
1973 
1974  /* Maybe start a new group of sectors. */
1975  if (sector_size == 0) {
1976  if (p->sectors[j].offset + p->sectors[j].size > p->size) {
1977  LOG_WARNING("The flash sector at offset 0x%08" PRIx32
1978  " overflows the end of %s bank.",
1979  p->sectors[j].offset, p->name);
1980  LOG_WARNING("The rest of bank will not show in gdb memory map.");
1981  break;
1982  }
1984  start = p->base + p->sectors[j].offset;
1985  xml_printf(&retval, &xml, &pos, &size,
1986  "<memory type=\"flash\" "
1987  "start=\"" TARGET_ADDR_FMT "\" ",
1988  start);
1989  sector_size = p->sectors[j].size;
1990  group_len = sector_size;
1991  } else {
1992  group_len += sector_size; /* equal to p->sectors[j].size */
1993  }
1994 
1995  /* Does this finish a group of sectors?
1996  * If not, continue an already-started group.
1997  */
1998  if (j < p->num_sectors - 1
1999  && p->sectors[j + 1].size == sector_size
2000  && p->sectors[j + 1].offset == p->sectors[j].offset + sector_size
2001  && p->sectors[j + 1].offset + p->sectors[j + 1].size <= p->size)
2002  continue;
2003 
2004  xml_printf(&retval, &xml, &pos, &size,
2005  "length=\"0x%x\">\n"
2006  "<property name=\"blocksize\">"
2007  "0x%x</property>\n"
2008  "</memory>\n",
2009  group_len,
2010  sector_size);
2011  sector_size = 0;
2012  }
2013 
2014  ram_start = p->base + p->size;
2015  }
2016 
2017  if (ram_start != 0)
2018  xml_printf(&retval, &xml, &pos, &size,
2019  "<memory type=\"ram\" start=\"" TARGET_ADDR_FMT "\" "
2020  "length=\"" TARGET_ADDR_FMT "\"/>\n",
2021  ram_start, target_address_max(target) - ram_start + 1);
2022  /* ELSE a flash chip could be at the very end of the address space, in
2023  * which case ram_start will be precisely 0 */
2024 
2025  free(banks);
2026 
2027  xml_printf(&retval, &xml, &pos, &size, "</memory-map>\n");
2028 
2029  if (retval != ERROR_OK) {
2030  free(xml);
2031  gdb_error(connection, retval);
2032  return retval;
2033  }
2034 
2035  if (offset + length > pos)
2036  length = pos - offset;
2037 
2038  char *t = malloc(length + 1);
2039  t[0] = 'l';
2040  memcpy(t + 1, xml + offset, length);
2041  gdb_put_packet(connection, t, length + 1);
2042 
2043  free(t);
2044  free(xml);
2045  return ERROR_OK;
2046 }
2047 
2048 static const char *gdb_get_reg_type_name(enum reg_type type)
2049 {
2050  switch (type) {
2051  case REG_TYPE_BOOL:
2052  return "bool";
2053  case REG_TYPE_INT:
2054  return "int";
2055  case REG_TYPE_INT8:
2056  return "int8";
2057  case REG_TYPE_INT16:
2058  return "int16";
2059  case REG_TYPE_INT32:
2060  return "int32";
2061  case REG_TYPE_INT64:
2062  return "int64";
2063  case REG_TYPE_INT128:
2064  return "int128";
2065  case REG_TYPE_UINT:
2066  return "uint";
2067  case REG_TYPE_UINT8:
2068  return "uint8";
2069  case REG_TYPE_UINT16:
2070  return "uint16";
2071  case REG_TYPE_UINT32:
2072  return "uint32";
2073  case REG_TYPE_UINT64:
2074  return "uint64";
2075  case REG_TYPE_UINT128:
2076  return "uint128";
2077  case REG_TYPE_CODE_PTR:
2078  return "code_ptr";
2079  case REG_TYPE_DATA_PTR:
2080  return "data_ptr";
2081  case REG_TYPE_FLOAT:
2082  return "float";
2083  case REG_TYPE_IEEE_SINGLE:
2084  return "ieee_single";
2085  case REG_TYPE_IEEE_DOUBLE:
2086  return "ieee_double";
2087  case REG_TYPE_ARCH_DEFINED:
2088  return "int"; /* return arbitrary string to avoid compile warning. */
2089  }
2090 
2091  return "int"; /* "int" as default value */
2092 }
2093 
2094 static int lookup_add_arch_defined_types(char const **arch_defined_types_list[], const char *type_id,
2095  int *num_arch_defined_types)
2096 {
2097  int tbl_sz = *num_arch_defined_types;
2098 
2099  if (type_id && (strcmp(type_id, ""))) {
2100  for (int j = 0; j < (tbl_sz + 1); j++) {
2101  if (!((*arch_defined_types_list)[j])) {
2102  (*arch_defined_types_list)[tbl_sz++] = type_id;
2103  *arch_defined_types_list = realloc(*arch_defined_types_list,
2104  sizeof(char *) * (tbl_sz + 1));
2105  (*arch_defined_types_list)[tbl_sz] = NULL;
2106  *num_arch_defined_types = tbl_sz;
2107  return 1;
2108  } else {
2109  if (!strcmp((*arch_defined_types_list)[j], type_id))
2110  return 0;
2111  }
2112  }
2113  }
2114 
2115  return -1;
2116 }
2117 
2119  char **tdesc, int *pos, int *size, struct reg_data_type *type,
2120  char const **arch_defined_types_list[], int *num_arch_defined_types)
2121 {
2122  int retval = ERROR_OK;
2123 
2124  if (type->type_class == REG_TYPE_CLASS_VECTOR) {
2125  struct reg_data_type *data_type = type->reg_type_vector->type;
2127  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2128  num_arch_defined_types))
2130  arch_defined_types_list,
2131  num_arch_defined_types);
2132  }
2133  /* <vector id="id" type="type" count="count"/> */
2134  xml_printf(&retval, tdesc, pos, size,
2135  "<vector id=\"%s\" type=\"%s\" count=\"%" PRIu32 "\"/>\n",
2136  type->id, type->reg_type_vector->type->id,
2137  type->reg_type_vector->count);
2138 
2139  } else if (type->type_class == REG_TYPE_CLASS_UNION) {
2140  struct reg_data_type_union_field *field;
2141  field = type->reg_type_union->fields;
2142  while (field) {
2143  struct reg_data_type *data_type = field->type;
2145  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2146  num_arch_defined_types))
2148  arch_defined_types_list,
2149  num_arch_defined_types);
2150  }
2151 
2152  field = field->next;
2153  }
2154  /* <union id="id">
2155  * <field name="name" type="type"/> ...
2156  * </union> */
2157  xml_printf(&retval, tdesc, pos, size,
2158  "<union id=\"%s\">\n",
2159  type->id);
2160 
2161  field = type->reg_type_union->fields;
2162  while (field) {
2163  xml_printf(&retval, tdesc, pos, size,
2164  "<field name=\"%s\" type=\"%s\"/>\n",
2165  field->name, field->type->id);
2166 
2167  field = field->next;
2168  }
2169 
2170  xml_printf(&retval, tdesc, pos, size,
2171  "</union>\n");
2172 
2173  } else if (type->type_class == REG_TYPE_CLASS_STRUCT) {
2174  struct reg_data_type_struct_field *field;
2175  field = type->reg_type_struct->fields;
2176 
2177  if (field->use_bitfields) {
2178  /* <struct id="id" size="size">
2179  * <field name="name" start="start" end="end"/> ...
2180  * </struct> */
2181  xml_printf(&retval, tdesc, pos, size,
2182  "<struct id=\"%s\" size=\"%" PRIu32 "\">\n",
2183  type->id, type->reg_type_struct->size);
2184  while (field) {
2185  xml_printf(&retval, tdesc, pos, size,
2186  "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2187  field->name, field->bitfield->start, field->bitfield->end,
2189 
2190  field = field->next;
2191  }
2192  } else {
2193  while (field) {
2194  struct reg_data_type *data_type = field->type;
2196  if (lookup_add_arch_defined_types(arch_defined_types_list, data_type->id,
2197  num_arch_defined_types))
2199  arch_defined_types_list,
2200  num_arch_defined_types);
2201  }
2202  }
2203 
2204  /* <struct id="id">
2205  * <field name="name" type="type"/> ...
2206  * </struct> */
2207  xml_printf(&retval, tdesc, pos, size,
2208  "<struct id=\"%s\">\n",
2209  type->id);
2210  while (field) {
2211  xml_printf(&retval, tdesc, pos, size,
2212  "<field name=\"%s\" type=\"%s\"/>\n",
2213  field->name, field->type->id);
2214 
2215  field = field->next;
2216  }
2217  }
2218 
2219  xml_printf(&retval, tdesc, pos, size,
2220  "</struct>\n");
2221 
2222  } else if (type->type_class == REG_TYPE_CLASS_FLAGS) {
2223  /* <flags id="id" size="size">
2224  * <field name="name" start="start" end="end"/> ...
2225  * </flags> */
2226  xml_printf(&retval, tdesc, pos, size,
2227  "<flags id=\"%s\" size=\"%" PRIu32 "\">\n",
2228  type->id, type->reg_type_flags->size);
2229 
2230  struct reg_data_type_flags_field *field;
2231  field = type->reg_type_flags->fields;
2232  while (field) {
2233  xml_printf(&retval, tdesc, pos, size,
2234  "<field name=\"%s\" start=\"%" PRIu32 "\" end=\"%" PRIu32 "\" type=\"%s\" />\n",
2235  field->name, field->bitfield->start, field->bitfield->end,
2237 
2238  field = field->next;
2239  }
2240 
2241  xml_printf(&retval, tdesc, pos, size,
2242  "</flags>\n");
2243 
2244  }
2245 
2246  return ERROR_OK;
2247 }
2248 
2249 /* Get a list of available target registers features. feature_list must
2250  * be freed by caller.
2251  */
2252 static int get_reg_features_list(struct target *target, char const **feature_list[], int *feature_list_size,
2253  struct reg **reg_list, int reg_list_size)
2254 {
2255  int tbl_sz = 0;
2256 
2257  /* Start with only one element */
2258  *feature_list = calloc(1, sizeof(char *));
2259 
2260  for (int i = 0; i < reg_list_size; i++) {
2261  if (reg_list[i]->exist == false || reg_list[i]->hidden)
2262  continue;
2263 
2264  if (reg_list[i]->feature
2265  && reg_list[i]->feature->name
2266  && (strcmp(reg_list[i]->feature->name, ""))) {
2267  /* We found a feature, check if the feature is already in the
2268  * table. If not, allocate a new entry for the table and
2269  * put the new feature in it.
2270  */
2271  for (int j = 0; j < (tbl_sz + 1); j++) {
2272  if (!((*feature_list)[j])) {
2273  (*feature_list)[tbl_sz++] = reg_list[i]->feature->name;
2274  *feature_list = realloc(*feature_list, sizeof(char *) * (tbl_sz + 1));
2275  (*feature_list)[tbl_sz] = NULL;
2276  break;
2277  } else {
2278  if (!strcmp((*feature_list)[j], reg_list[i]->feature->name))
2279  break;
2280  }
2281  }
2282  }
2283  }
2284 
2285  if (feature_list_size)
2286  *feature_list_size = tbl_sz;
2287 
2288  return ERROR_OK;
2289 }
2290 
2291 /* Create a register list that's the union of all the registers of the SMP
2292  * group this target is in. If the target is not part of an SMP group, this
2293  * returns the same as target_get_gdb_reg_list_noread().
2294  */
2295 static int smp_reg_list_noread(struct target *target,
2296  struct reg **combined_list[], int *combined_list_size,
2297  enum target_register_class reg_class)
2298 {
2299  if (!target->smp)
2300  return target_get_gdb_reg_list_noread(target, combined_list,
2301  combined_list_size, REG_CLASS_ALL);
2302 
2303  unsigned int combined_allocated = 256;
2304  struct reg **local_list = malloc(combined_allocated * sizeof(struct reg *));
2305  if (!local_list) {
2306  LOG_ERROR("malloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2307  return ERROR_FAIL;
2308  }
2309  unsigned int local_list_size = 0;
2310 
2311  struct target_list *head;
2313  if (!target_was_examined(head->target))
2314  continue;
2315 
2316  struct reg **reg_list = NULL;
2317  int reg_list_size;
2318  int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2319  &reg_list_size, reg_class);
2320  if (result != ERROR_OK) {
2321  free(local_list);
2322  return result;
2323  }
2324  for (int i = 0; i < reg_list_size; i++) {
2325  bool found = false;
2326  struct reg *a = reg_list[i];
2327  if (a->exist) {
2328  /* Nested loop makes this O(n^2), but this entire function with
2329  * 5 RISC-V targets takes just 2ms on my computer. Fast enough
2330  * for me. */
2331  for (unsigned int j = 0; j < local_list_size; j++) {
2332  struct reg *b = local_list[j];
2333  if (!strcmp(a->name, b->name)) {
2334  found = true;
2335  if (a->size != b->size) {
2336  LOG_ERROR("SMP register %s is %d bits on one "
2337  "target, but %d bits on another target.",
2338  a->name, a->size, b->size);
2339  free(reg_list);
2340  free(local_list);
2341  return ERROR_FAIL;
2342  }
2343  break;
2344  }
2345  }
2346  if (!found) {
2347  LOG_DEBUG("[%s] %s not found in combined list", target_name(target), a->name);
2348  if (local_list_size >= combined_allocated) {
2349  combined_allocated *= 2;
2350  local_list = realloc(local_list, combined_allocated * sizeof(struct reg *));
2351  if (!local_list) {
2352  LOG_ERROR("realloc(%zu) failed", combined_allocated * sizeof(struct reg *));
2353  free(reg_list);
2354  return ERROR_FAIL;
2355  }
2356  }
2357  local_list[local_list_size] = a;
2358  local_list_size++;
2359  }
2360  }
2361  }
2362  free(reg_list);
2363  }
2364 
2365  if (local_list_size == 0) {
2366  LOG_ERROR("Unable to get register list");
2367  free(local_list);
2368  return ERROR_FAIL;
2369  }
2370 
2371  /* Now warn the user about any registers that weren't found in every target. */
2373  if (!target_was_examined(head->target))
2374  continue;
2375 
2376  struct reg **reg_list = NULL;
2377  int reg_list_size;
2378  int result = target_get_gdb_reg_list_noread(head->target, &reg_list,
2379  &reg_list_size, reg_class);
2380  if (result != ERROR_OK) {
2381  free(local_list);
2382  return result;
2383  }
2384  for (unsigned int i = 0; i < local_list_size; i++) {
2385  bool found = false;
2386  struct reg *a = local_list[i];
2387  for (int j = 0; j < reg_list_size; j++) {
2388  struct reg *b = reg_list[j];
2389  if (b->exist && !strcmp(a->name, b->name)) {
2390  found = true;
2391  break;
2392  }
2393  }
2394  if (!found) {
2395  LOG_WARNING("Register %s does not exist in %s, which is part of an SMP group where "
2396  "this register does exist.",
2397  a->name, target_name(head->target));
2398  }
2399  }
2400  free(reg_list);
2401  }
2402 
2403  *combined_list = local_list;
2404  *combined_list_size = local_list_size;
2405  return ERROR_OK;
2406 }
2407 
2408 static int gdb_generate_target_description(struct target *target, char **tdesc_out)
2409 {
2410  int retval = ERROR_OK;
2411  struct reg **reg_list = NULL;
2412  int reg_list_size;
2413  char const *architecture;
2414  char const **features = NULL;
2415  int feature_list_size = 0;
2416  char *tdesc = NULL;
2417  int pos = 0;
2418  int size = 0;
2419 
2420 
2421  retval = smp_reg_list_noread(target, &reg_list, &reg_list_size,
2422  REG_CLASS_ALL);
2423 
2424  if (retval != ERROR_OK) {
2425  LOG_ERROR("get register list failed");
2426  retval = ERROR_FAIL;
2427  goto error;
2428  }
2429 
2430  if (reg_list_size <= 0) {
2431  LOG_ERROR("get register list failed");
2432  retval = ERROR_FAIL;
2433  goto error;
2434  }
2435 
2436  /* Get a list of available target registers features */
2437  retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2438  if (retval != ERROR_OK) {
2439  LOG_ERROR("Can't get the registers feature list");
2440  retval = ERROR_FAIL;
2441  goto error;
2442  }
2443 
2444  /* If we found some features associated with registers, create sections */
2445  int current_feature = 0;
2446 
2447  xml_printf(&retval, &tdesc, &pos, &size,
2448  "<?xml version=\"1.0\"?>\n"
2449  "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">\n"
2450  "<target version=\"1.0\">\n");
2451 
2452  /* generate architecture element if supported by target */
2453  architecture = target_get_gdb_arch(target);
2454  if (architecture)
2455  xml_printf(&retval, &tdesc, &pos, &size,
2456  "<architecture>%s</architecture>\n", architecture);
2457 
2458  /* generate target description according to register list */
2459  if (features) {
2460  while (features[current_feature]) {
2461  char const **arch_defined_types = NULL;
2462  int num_arch_defined_types = 0;
2463 
2464  arch_defined_types = calloc(1, sizeof(char *));
2465  xml_printf(&retval, &tdesc, &pos, &size,
2466  "<feature name=\"%s\">\n",
2467  features[current_feature]);
2468 
2469  int i;
2470  for (i = 0; i < reg_list_size; i++) {
2471 
2472  if (reg_list[i]->exist == false || reg_list[i]->hidden)
2473  continue;
2474 
2475  if (strcmp(reg_list[i]->feature->name, features[current_feature]))
2476  continue;
2477 
2478  const char *type_str;
2479  if (reg_list[i]->reg_data_type) {
2480  if (reg_list[i]->reg_data_type->type == REG_TYPE_ARCH_DEFINED) {
2481  /* generate <type... first, if there are architecture-defined types. */
2482  if (lookup_add_arch_defined_types(&arch_defined_types,
2483  reg_list[i]->reg_data_type->id,
2484  &num_arch_defined_types))
2486  reg_list[i]->reg_data_type,
2487  &arch_defined_types,
2488  &num_arch_defined_types);
2489 
2490  type_str = reg_list[i]->reg_data_type->id;
2491  } else {
2492  /* predefined type */
2493  type_str = gdb_get_reg_type_name(
2494  reg_list[i]->reg_data_type->type);
2495  }
2496  } else {
2497  /* Default type is "int" */
2498  type_str = "int";
2499  }
2500 
2501  xml_printf(&retval, &tdesc, &pos, &size,
2502  "<reg name=\"%s\"", reg_list[i]->name);
2503  xml_printf(&retval, &tdesc, &pos, &size,
2504  " bitsize=\"%" PRIu32 "\"", reg_list[i]->size);
2505  xml_printf(&retval, &tdesc, &pos, &size,
2506  " regnum=\"%" PRIu32 "\"", reg_list[i]->number);
2507  if (reg_list[i]->caller_save)
2508  xml_printf(&retval, &tdesc, &pos, &size,
2509  " save-restore=\"yes\"");
2510  else
2511  xml_printf(&retval, &tdesc, &pos, &size,
2512  " save-restore=\"no\"");
2513 
2514  xml_printf(&retval, &tdesc, &pos, &size,
2515  " type=\"%s\"", type_str);
2516 
2517  if (reg_list[i]->group)
2518  xml_printf(&retval, &tdesc, &pos, &size,
2519  " group=\"%s\"", reg_list[i]->group);
2520 
2521  xml_printf(&retval, &tdesc, &pos, &size,
2522  "/>\n");
2523  }
2524 
2525  xml_printf(&retval, &tdesc, &pos, &size,
2526  "</feature>\n");
2527 
2528  current_feature++;
2529  free(arch_defined_types);
2530  }
2531  }
2532 
2533  xml_printf(&retval, &tdesc, &pos, &size,
2534  "</target>\n");
2535 
2536 error:
2537  free(features);
2538  free(reg_list);
2539 
2540  if (retval == ERROR_OK)
2541  *tdesc_out = tdesc;
2542  else
2543  free(tdesc);
2544 
2545  return retval;
2546 }
2547 
2548 static int gdb_get_target_description_chunk(struct target *target, struct target_desc_format *target_desc,
2549  char **chunk, int32_t offset, uint32_t length)
2550 {
2551  if (!target_desc) {
2552  LOG_ERROR("Unable to Generate Target Description");
2553  return ERROR_FAIL;
2554  }
2555 
2556  char *tdesc = target_desc->tdesc;
2557  uint32_t tdesc_length = target_desc->tdesc_length;
2558 
2559  if (!tdesc) {
2560  int retval = gdb_generate_target_description(target, &tdesc);
2561  if (retval != ERROR_OK) {
2562  LOG_ERROR("Unable to Generate Target Description");
2563  return ERROR_FAIL;
2564  }
2565 
2566  tdesc_length = strlen(tdesc);
2567  }
2568 
2569  char transfer_type;
2570 
2571  if (length < (tdesc_length - offset))
2572  transfer_type = 'm';
2573  else
2574  transfer_type = 'l';
2575 
2576  *chunk = malloc(length + 2);
2577  if (!*chunk) {
2578  LOG_ERROR("Unable to allocate memory");
2579  return ERROR_FAIL;
2580  }
2581 
2582  (*chunk)[0] = transfer_type;
2583  if (transfer_type == 'm') {
2584  strncpy((*chunk) + 1, tdesc + offset, length);
2585  (*chunk)[1 + length] = '\0';
2586  } else {
2587  strncpy((*chunk) + 1, tdesc + offset, tdesc_length - offset);
2588  (*chunk)[1 + (tdesc_length - offset)] = '\0';
2589 
2590  /* After gdb-server sends out last chunk, invalidate tdesc. */
2591  free(tdesc);
2592  tdesc = NULL;
2593  tdesc_length = 0;
2594  }
2595 
2596  target_desc->tdesc = tdesc;
2597  target_desc->tdesc_length = tdesc_length;
2598 
2599  return ERROR_OK;
2600 }
2601 
2602 static int gdb_target_description_supported(struct target *target, int *supported)
2603 {
2604  int retval = ERROR_OK;
2605  struct reg **reg_list = NULL;
2606  int reg_list_size = 0;
2607  char const **features = NULL;
2608  int feature_list_size = 0;
2609 
2610  char const *architecture = target_get_gdb_arch(target);
2611 
2612  retval = target_get_gdb_reg_list_noread(target, &reg_list,
2613  &reg_list_size, REG_CLASS_ALL);
2614  if (retval != ERROR_OK) {
2615  LOG_ERROR("get register list failed");
2616  goto error;
2617  }
2618 
2619  if (reg_list_size <= 0) {
2620  LOG_ERROR("get register list failed");
2621  retval = ERROR_FAIL;
2622  goto error;
2623  }
2624 
2625  /* Get a list of available target registers features */
2626  retval = get_reg_features_list(target, &features, &feature_list_size, reg_list, reg_list_size);
2627  if (retval != ERROR_OK) {
2628  LOG_ERROR("Can't get the registers feature list");
2629  goto error;
2630  }
2631 
2632  if (supported) {
2633  if (architecture || feature_list_size)
2634  *supported = 1;
2635  else
2636  *supported = 0;
2637  }
2638 
2639 error:
2640  free(features);
2641 
2642  free(reg_list);
2643 
2644  return retval;
2645 }
2646 
2647 static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
2648 {
2649  struct rtos *rtos = target->rtos;
2650  int retval = ERROR_OK;
2651  char *thread_list = NULL;
2652  int pos = 0;
2653  int size = 0;
2654 
2655  xml_printf(&retval, &thread_list, &pos, &size,
2656  "<?xml version=\"1.0\"?>\n"
2657  "<threads>\n");
2658 
2659  if (rtos) {
2660  for (int i = 0; i < rtos->thread_count; i++) {
2662 
2663  if (!thread_detail->exists)
2664  continue;
2665 
2667  xml_printf(&retval, &thread_list, &pos, &size,
2668  "<thread id=\"%" PRIx64 "\" name=\"%s\">",
2671  else
2672  xml_printf(&retval, &thread_list, &pos, &size,
2673  "<thread id=\"%" PRIx64 "\">", thread_detail->threadid);
2674 
2676  xml_printf(&retval, &thread_list, &pos, &size,
2677  "Name: %s", thread_detail->thread_name_str);
2678 
2681  xml_printf(&retval, &thread_list, &pos, &size,
2682  ", ");
2683  xml_printf(&retval, &thread_list, &pos, &size,
2684  "%s", thread_detail->extra_info_str);
2685  }
2686 
2687  xml_printf(&retval, &thread_list, &pos, &size,
2688  "</thread>\n");
2689  }
2690  }
2691 
2692  xml_printf(&retval, &thread_list, &pos, &size,
2693  "</threads>\n");
2694 
2695  if (retval == ERROR_OK)
2696  *thread_list_out = thread_list;
2697  else
2698  free(thread_list);
2699 
2700  return retval;
2701 }
2702 
2703 static int gdb_get_thread_list_chunk(struct target *target, char **thread_list,
2704  char **chunk, int32_t offset, uint32_t length)
2705 {
2706  if (!*thread_list) {
2707  int retval = gdb_generate_thread_list(target, thread_list);
2708  if (retval != ERROR_OK) {
2709  LOG_ERROR("Unable to Generate Thread List");
2710  return ERROR_FAIL;
2711  }
2712  }
2713 
2714  size_t thread_list_length = strlen(*thread_list);
2715  char transfer_type;
2716 
2717  length = MIN(length, thread_list_length - offset);
2718  if (length < (thread_list_length - offset))
2719  transfer_type = 'm';
2720  else
2721  transfer_type = 'l';
2722 
2723  *chunk = malloc(length + 2 + 3);
2724  /* Allocating extra 3 bytes prevents false positive valgrind report
2725  * of strlen(chunk) word access:
2726  * Invalid read of size 4
2727  * Address 0x4479934 is 44 bytes inside a block of size 45 alloc'd */
2728  if (!*chunk) {
2729  LOG_ERROR("Unable to allocate memory");
2730  return ERROR_FAIL;
2731  }
2732 
2733  (*chunk)[0] = transfer_type;
2734  strncpy((*chunk) + 1, (*thread_list) + offset, length);
2735  (*chunk)[1 + length] = '\0';
2736 
2737  /* After gdb-server sends out last chunk, invalidate thread list. */
2738  if (transfer_type == 'l') {
2739  free(*thread_list);
2740  *thread_list = NULL;
2741  }
2742 
2743  return ERROR_OK;
2744 }
2745 
2747  char const *packet, int packet_size)
2748 {
2749  struct command_context *cmd_ctx = connection->cmd_ctx;
2752 
2753  if (strncmp(packet, "qRcmd,", 6) == 0) {
2754  if (packet_size > 6) {
2755  Jim_Interp *interp = cmd_ctx->interp;
2756  char *cmd;
2757  cmd = malloc((packet_size - 6) / 2 + 1);
2758  size_t len = unhexify((uint8_t *)cmd, packet + 6, (packet_size - 6) / 2);
2759  cmd[len] = 0;
2760 
2761  /* We want to print all debug output to GDB connection */
2764  /* some commands need to know the GDB connection, make note of current
2765  * GDB connection. */
2767 
2768  struct target *saved_target_override = cmd_ctx->current_target_override;
2769  cmd_ctx->current_target_override = NULL;
2770 
2771  struct command_context *old_context = Jim_GetAssocData(interp, "context");
2772  Jim_DeleteAssocData(interp, "context");
2773  int retval = Jim_SetAssocData(interp, "context", NULL, cmd_ctx);
2774  if (retval == JIM_OK) {
2775  retval = Jim_EvalObj(interp, Jim_NewStringObj(interp, cmd, -1));
2776  Jim_DeleteAssocData(interp, "context");
2777  }
2778  int inner_retval = Jim_SetAssocData(interp, "context", NULL, old_context);
2779  if (retval == JIM_OK)
2780  retval = inner_retval;
2781 
2782  cmd_ctx->current_target_override = saved_target_override;
2783 
2787  free(cmd);
2788  if (retval == JIM_RETURN)
2789  retval = interp->returnCode;
2790  int lenmsg;
2791  const char *cretmsg = Jim_GetString(Jim_GetResult(interp), &lenmsg);
2792  char *retmsg;
2793  if (lenmsg && cretmsg[lenmsg - 1] != '\n') {
2794  retmsg = alloc_printf("%s\n", cretmsg);
2795  lenmsg++;
2796  } else {
2797  retmsg = strdup(cretmsg);
2798  }
2799  if (!retmsg)
2801 
2802  if (retval == JIM_OK) {
2803  if (lenmsg) {
2804  char *hex_buffer = malloc(lenmsg * 2 + 1);
2805  if (!hex_buffer) {
2806  free(retmsg);
2808  }
2809 
2810  size_t pkt_len = hexify(hex_buffer, (const uint8_t *)retmsg, lenmsg,
2811  lenmsg * 2 + 1);
2812  gdb_put_packet(connection, hex_buffer, pkt_len);
2813  free(hex_buffer);
2814  } else {
2815  gdb_put_packet(connection, "OK", 2);
2816  }
2817  } else {
2818  if (lenmsg)
2819  gdb_output_con(connection, retmsg);
2820  gdb_send_error(connection, retval);
2821  }
2822  free(retmsg);
2823  return ERROR_OK;
2824  }
2825  gdb_put_packet(connection, "OK", 2);
2826  return ERROR_OK;
2827  } else if (strncmp(packet, "qCRC:", 5) == 0) {
2828  if (packet_size > 5) {
2829  int retval;
2830  char gdb_reply[10];
2831  char *separator;
2832  uint32_t checksum;
2833  target_addr_t addr = 0;
2834  uint32_t len = 0;
2835 
2836  /* skip command character */
2837  packet += 5;
2838 
2839  addr = strtoull(packet, &separator, 16);
2840 
2841  if (*separator != ',') {
2842  LOG_ERROR("incomplete read memory packet received, dropping connection");
2844  }
2845 
2846  len = strtoul(separator + 1, NULL, 16);
2847 
2848  retval = target_checksum_memory(target, addr, len, &checksum);
2849 
2850  if (retval == ERROR_OK) {
2851  snprintf(gdb_reply, 10, "C%8.8" PRIx32 "", checksum);
2852  gdb_put_packet(connection, gdb_reply, 9);
2853  } else {
2854  retval = gdb_error(connection, retval);
2855  if (retval != ERROR_OK)
2856  return retval;
2857  }
2858 
2859  return ERROR_OK;
2860  }
2861  } else if (strncmp(packet, "qSupported", 10) == 0) {
2862  /* we currently support packet size and qXfer:memory-map:read (if enabled)
2863  * qXfer:features:read is supported for some targets */
2864  int retval = ERROR_OK;
2865  char *buffer = NULL;
2866  int pos = 0;
2867  int size = 0;
2868  int gdb_target_desc_supported = 0;
2869 
2870  /* we need to test that the target supports target descriptions */
2871  retval = gdb_target_description_supported(target, &gdb_target_desc_supported);
2872  if (retval != ERROR_OK) {
2873  LOG_INFO("Failed detecting Target Description Support, disabling");
2874  gdb_target_desc_supported = 0;
2875  }
2876 
2877  /* support may be disabled globally */
2878  if (gdb_use_target_description == 0) {
2879  if (gdb_target_desc_supported)
2880  LOG_WARNING("Target Descriptions Supported, but disabled");
2881  gdb_target_desc_supported = 0;
2882  }
2883 
2884  xml_printf(&retval,
2885  &buffer,
2886  &pos,
2887  &size,
2888  "PacketSize=%x;qXfer:memory-map:read%c;qXfer:features:read%c;qXfer:threads:read+;QStartNoAckMode+;vContSupported+",
2890  ((gdb_use_memory_map == 1) && (flash_get_bank_count() > 0)) ? '+' : '-',
2891  (gdb_target_desc_supported == 1) ? '+' : '-');
2892 
2893  if (retval != ERROR_OK) {
2895  return ERROR_OK;
2896  }
2897 
2899  free(buffer);
2900 
2901  return ERROR_OK;
2902  } else if ((strncmp(packet, "qXfer:memory-map:read::", 23) == 0)
2903  && (flash_get_bank_count() > 0))
2904  return gdb_memory_map(connection, packet, packet_size);
2905  else if (strncmp(packet, "qXfer:features:read:", 20) == 0) {
2906  char *xml = NULL;
2907  int retval = ERROR_OK;
2908 
2909  int offset;
2910  unsigned int length;
2911 
2912  /* skip command character */
2913  packet += 20;
2914 
2915  if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2917  return ERROR_OK;
2918  }
2919 
2920  /* Target should prepare correct target description for annex.
2921  * The first character of returned xml is 'm' or 'l'. 'm' for
2922  * there are *more* chunks to transfer. 'l' for it is the *last*
2923  * chunk of target description.
2924  */
2926  &xml, offset, length);
2927  if (retval != ERROR_OK) {
2928  gdb_error(connection, retval);
2929  return retval;
2930  }
2931 
2932  gdb_put_packet(connection, xml, strlen(xml));
2933 
2934  free(xml);
2935  return ERROR_OK;
2936  } else if (strncmp(packet, "qXfer:threads:read:", 19) == 0) {
2937  char *xml = NULL;
2938  int retval = ERROR_OK;
2939 
2940  int offset;
2941  unsigned int length;
2942 
2943  /* skip command character */
2944  packet += 19;
2945 
2946  if (decode_xfer_read(packet, NULL, &offset, &length) < 0) {
2948  return ERROR_OK;
2949  }
2950 
2951  /* Target should prepare correct thread list for annex.
2952  * The first character of returned xml is 'm' or 'l'. 'm' for
2953  * there are *more* chunks to transfer. 'l' for it is the *last*
2954  * chunk of target description.
2955  */
2957  &xml, offset, length);
2958  if (retval != ERROR_OK) {
2959  gdb_error(connection, retval);
2960  return retval;
2961  }
2962 
2963  gdb_put_packet(connection, xml, strlen(xml));
2964 
2965  free(xml);
2966  return ERROR_OK;
2967  } else if (strncmp(packet, "QStartNoAckMode", 15) == 0) {
2969  gdb_put_packet(connection, "OK", 2);
2970  return ERROR_OK;
2971  } else if (target->type->gdb_query_custom) {
2972  char *buffer = NULL;
2973  int ret = target->type->gdb_query_custom(target, packet, &buffer);
2975  return ret;
2976  }
2977 
2978  gdb_put_packet(connection, "", 0);
2979  return ERROR_OK;
2980 }
2981 
2982 static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet,
2983  __attribute__((unused)) int packet_size)
2984 {
2987  const char *parse = packet;
2988  int retval;
2989 
2990  /* query for vCont supported */
2991  if (parse[0] == '?') {
2992  if (target->type->step) {
2993  /* gdb doesn't accept c without C and s without S */
2994  gdb_put_packet(connection, "vCont;c;C;s;S", 13);
2995  return true;
2996  }
2997  return false;
2998  }
2999 
3000  if (parse[0] == ';') {
3001  ++parse;
3002  }
3003 
3004  /* simple case, a continue packet */
3005  if (parse[0] == 'c') {
3006  gdb_running_type = 'c';
3007  LOG_DEBUG("target %s continue", target_name(target));
3009  retval = target_resume(target, 1, 0, 0, 0);
3010  if (retval == ERROR_TARGET_NOT_HALTED)
3011  LOG_INFO("target %s was not halted when resume was requested", target_name(target));
3012 
3013  /* poll target in an attempt to make its internal state consistent */
3014  if (retval != ERROR_OK) {
3015  retval = target_poll(target);
3016  if (retval != ERROR_OK)
3017  LOG_DEBUG("error polling target %s after failed resume", target_name(target));
3018  }
3019 
3020  /*
3021  * We don't report errors to gdb here, move frontend_state to
3022  * TARGET_RUNNING to stay in sync with gdb's expectation of the
3023  * target state
3024  */
3027 
3028  return true;
3029  }
3030 
3031  /* single-step or step-over-breakpoint */
3032  if (parse[0] == 's') {
3033  gdb_running_type = 's';
3034  bool fake_step = false;
3035 
3036  struct target *ct = target;
3037  int current_pc = 1;
3038  int64_t thread_id;
3039  parse++;
3040  if (parse[0] == ':') {
3041  char *endp;
3042  parse++;
3043  thread_id = strtoll(parse, &endp, 16);
3044  if (endp) {
3045  parse = endp;
3046  }
3047  } else {
3048  thread_id = 0;
3049  }
3050 
3051  if (target->rtos) {
3052  /* FIXME: why is this necessary? rtos state should be up-to-date here already! */
3054 
3055  target->rtos->gdb_target_for_threadid(connection, thread_id, &ct);
3056 
3057  /*
3058  * check if the thread to be stepped is the current rtos thread
3059  * if not, we must fake the step
3060  */
3061  if (target->rtos->current_thread != thread_id)
3062  fake_step = true;
3063  }
3064 
3065  if (parse[0] == ';') {
3066  ++parse;
3067 
3068  if (parse[0] == 'c') {
3069  parse += 1;
3070 
3071  /* check if thread-id follows */
3072  if (parse[0] == ':') {
3073  int64_t tid;
3074  parse += 1;
3075 
3076  tid = strtoll(parse, NULL, 16);
3077  if (tid == thread_id) {
3078  /*
3079  * Special case: only step a single thread (core),
3080  * keep the other threads halted. Currently, only
3081  * aarch64 target understands it. Other target types don't
3082  * care (nobody checks the actual value of 'current')
3083  * and it doesn't really matter. This deserves
3084  * a symbolic constant and a formal interface documentation
3085  * at a later time.
3086  */
3087  LOG_DEBUG("request to step current core only");
3088  /* uncomment after checking that indeed other targets are safe */
3089  /*current_pc = 2;*/
3090  }
3091  }
3092  }
3093  }
3094 
3095  LOG_DEBUG("target %s single-step thread %"PRIx64, target_name(ct), thread_id);
3098 
3099  /*
3100  * work around an annoying gdb behaviour: when the current thread
3101  * is changed in gdb, it assumes that the target can follow and also
3102  * make the thread current. This is an assumption that cannot hold
3103  * for a real target running a multi-threading OS. We just fake
3104  * the step to not trigger an internal error in gdb. See
3105  * https://sourceware.org/bugzilla/show_bug.cgi?id=22925 for details
3106  */
3107  if (fake_step) {
3108  int sig_reply_len;
3109  char sig_reply[128];
3110 
3111  LOG_DEBUG("fake step thread %"PRIx64, thread_id);
3112 
3113  sig_reply_len = snprintf(sig_reply, sizeof(sig_reply),
3114  "T05thread:%016"PRIx64";", thread_id);
3115 
3116  gdb_put_packet(connection, sig_reply, sig_reply_len);
3118 
3119  return true;
3120  }
3121 
3122  /* support for gdb_sync command */
3123  if (gdb_connection->sync) {
3124  gdb_connection->sync = false;
3125  if (ct->state == TARGET_HALTED) {
3126  LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3127  "from the target.");
3130  } else
3132  return true;
3133  }
3134 
3135  retval = target_step(ct, current_pc, 0, 0);
3136  if (retval == ERROR_TARGET_NOT_HALTED)
3137  LOG_INFO("target %s was not halted when step was requested", target_name(ct));
3138 
3139  /* if step was successful send a reply back to gdb */
3140  if (retval == ERROR_OK) {
3141  retval = target_poll(ct);
3142  if (retval != ERROR_OK)
3143  LOG_DEBUG("error polling target %s after successful step", target_name(ct));
3144  /* send back signal information */
3146  /* stop forwarding log packets! */
3148  } else
3150  return true;
3151  }
3152  LOG_ERROR("Unknown vCont packet");
3153  return false;
3154 }
3155 
3156 static char *next_hex_encoded_field(const char **str, char sep)
3157 {
3158  size_t hexlen;
3159  const char *hex = *str;
3160  if (hex[0] == '\0')
3161  return NULL;
3162 
3163  const char *end = strchr(hex, sep);
3164  if (!end)
3165  hexlen = strlen(hex);
3166  else
3167  hexlen = end - hex;
3168  *str = hex + hexlen + 1;
3169 
3170  if (hexlen % 2 != 0) {
3171  /* Malformed hex data */
3172  return NULL;
3173  }
3174 
3175  size_t count = hexlen / 2;
3176  char *decoded = malloc(count + 1);
3177  if (!decoded)
3178  return NULL;
3179 
3180  size_t converted = unhexify((void *)decoded, hex, count);
3181  if (converted != count) {
3182  free(decoded);
3183  return NULL;
3184  }
3185 
3186  decoded[count] = '\0';
3187  return decoded;
3188 }
3189 
3190 /* handle extended restart packet */
3191 static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
3192 {
3193  struct gdb_connection *gdb_con = connection->priv;
3195 
3198  command_run_linef(connection->cmd_ctx, "ocd_gdb_restart %s",
3199  target_name(target));
3200  /* set connection as attached after reset */
3201  gdb_con->attached = true;
3202  /* info rtos parts */
3203  gdb_thread_packet(connection, packet, packet_size);
3204 }
3205 
3206 static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
3207 {
3209  const char *parse = packet;
3210 
3211  /* Skip "vRun" */
3212  parse += 4;
3213 
3214  if (parse[0] != ';')
3215  return false;
3216  parse++;
3217 
3218  /* Skip first field "filename"; don't know what to do with it. */
3219  free(next_hex_encoded_field(&parse, ';'));
3220 
3221  char *cmdline = next_hex_encoded_field(&parse, ';');
3222  while (cmdline) {
3223  char *arg = next_hex_encoded_field(&parse, ';');
3224  if (!arg)
3225  break;
3226  char *new_cmdline = alloc_printf("%s %s", cmdline, arg);
3227  free(cmdline);
3228  free(arg);
3229  cmdline = new_cmdline;
3230  }
3231 
3232  if (cmdline) {
3233  if (target->semihosting) {
3234  LOG_INFO("GDB set inferior command line to '%s'", cmdline);
3235  free(target->semihosting->cmdline);
3236  target->semihosting->cmdline = cmdline;
3237  } else {
3238  LOG_INFO("GDB set inferior command line to '%s' but semihosting is unavailable", cmdline);
3239  free(cmdline);
3240  }
3241  }
3242 
3243  gdb_restart_inferior(connection, packet, packet_size);
3244  gdb_put_packet(connection, "S00", 3);
3245  return true;
3246 }
3247 
3249  char const *packet, int packet_size)
3250 {
3252  int result;
3253 
3255 
3256  if (strncmp(packet, "vCont", 5) == 0) {
3257  bool handled;
3258 
3259  packet += 5;
3260  packet_size -= 5;
3261 
3262  handled = gdb_handle_vcont_packet(connection, packet, packet_size);
3263  if (!handled)
3264  gdb_put_packet(connection, "", 0);
3265 
3266  return ERROR_OK;
3267  }
3268 
3269  if (strncmp(packet, "vRun", 4) == 0) {
3270  bool handled;
3271 
3272  handled = gdb_handle_vrun_packet(connection, packet, packet_size);
3273  if (!handled)
3274  gdb_put_packet(connection, "", 0);
3275 
3276  return ERROR_OK;
3277  }
3278 
3279  /* if flash programming disabled - send a empty reply */
3280 
3281  if (gdb_flash_program == 0) {
3282  gdb_put_packet(connection, "", 0);
3283  return ERROR_OK;
3284  }
3285 
3286  if (strncmp(packet, "vFlashErase:", 12) == 0) {
3288  unsigned long length;
3289 
3290  char const *parse = packet + 12;
3291  if (*parse == '\0') {
3292  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3294  }
3295 
3296  addr = strtoull(parse, (char **)&parse, 16);
3297 
3298  if (*(parse++) != ',' || *parse == '\0') {
3299  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3301  }
3302 
3303  length = strtoul(parse, (char **)&parse, 16);
3304 
3305  if (*parse != '\0') {
3306  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3308  }
3309 
3310  /* assume all sectors need erasing - stops any problems
3311  * when flash_write is called multiple times */
3312  flash_set_dirty();
3313 
3314  /* perform any target specific operations before the erase */
3317 
3318  /* vFlashErase:addr,length messages require region start and
3319  * end to be "block" aligned ... if padding is ever needed,
3320  * GDB will have become dangerously confused.
3321  */
3322  result = flash_erase_address_range(target, false, addr,
3323  length);
3324 
3325  /* perform any target specific operations after the erase */
3328 
3329  /* perform erase */
3330  if (result != ERROR_OK) {
3331  /* GDB doesn't evaluate the actual error number returned,
3332  * treat a failed erase as an I/O error
3333  */
3334  gdb_send_error(connection, EIO);
3335  LOG_ERROR("flash_erase returned %i", result);
3336  } else
3337  gdb_put_packet(connection, "OK", 2);
3338 
3339  return ERROR_OK;
3340  }
3341 
3342  if (strncmp(packet, "vFlashWrite:", 12) == 0) {
3343  int retval;
3345  unsigned long length;
3346  char const *parse = packet + 12;
3347 
3348  if (*parse == '\0') {
3349  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3351  }
3352 
3353  addr = strtoull(parse, (char **)&parse, 16);
3354  if (*(parse++) != ':') {
3355  LOG_ERROR("incomplete vFlashErase packet received, dropping connection");
3357  }
3358  length = packet_size - (parse - packet);
3359 
3360  /* create a new image if there isn't already one */
3361  if (!gdb_connection->vflash_image) {
3362  gdb_connection->vflash_image = malloc(sizeof(struct image));
3363  image_open(gdb_connection->vflash_image, "", "build");
3364  }
3365 
3366  /* create new section with content from packet buffer */
3368  addr, length, 0x0, (uint8_t const *)parse);
3369  if (retval != ERROR_OK)
3370  return retval;
3371 
3372  gdb_put_packet(connection, "OK", 2);
3373 
3374  return ERROR_OK;
3375  }
3376 
3377  if (strncmp(packet, "vFlashDone", 10) == 0) {
3378  uint32_t written;
3379 
3380  /* GDB command 'flash-erase' does not send a vFlashWrite,
3381  * so nothing to write here. */
3382  if (!gdb_connection->vflash_image) {
3383  gdb_put_packet(connection, "OK", 2);
3384  return ERROR_OK;
3385  }
3386 
3387  /* process the flashing buffer. No need to erase as GDB
3388  * always issues a vFlashErase first. */
3392  &written, false);
3395  if (result != ERROR_OK) {
3396  if (result == ERROR_FLASH_DST_OUT_OF_BANK)
3397  gdb_put_packet(connection, "E.memtype", 9);
3398  else
3399  gdb_send_error(connection, EIO);
3400  } else {
3401  LOG_DEBUG("wrote %u bytes from vFlash image to flash", (unsigned)written);
3402  gdb_put_packet(connection, "OK", 2);
3403  }
3404 
3408 
3409  return ERROR_OK;
3410  }
3411 
3412  gdb_put_packet(connection, "", 0);
3413  return ERROR_OK;
3414 }
3415 
3416 static int gdb_detach(struct connection *connection)
3417 {
3418  /*
3419  * Only reply "OK" to GDB
3420  * it will close the connection and this will trigger a call to
3421  * gdb_connection_closed() that will in turn trigger the event
3422  * TARGET_EVENT_GDB_DETACH
3423  */
3424  return gdb_put_packet(connection, "OK", 2);
3425 }
3426 
3427 /* The format of 'F' response packet is
3428  * Fretcode,errno,Ctrl-C flag;call-specific attachment
3429  */
3431  char const *packet, int packet_size)
3432 {
3434  char *separator;
3435  char *parsing_point;
3436  int fileio_retcode = strtoul(packet + 1, &separator, 16);
3437  int fileio_errno = 0;
3438  bool fileio_ctrl_c = false;
3439  int retval;
3440 
3441  LOG_DEBUG("-");
3442 
3443  if (*separator == ',') {
3444  parsing_point = separator + 1;
3445  fileio_errno = strtoul(parsing_point, &separator, 16);
3446  if (*separator == ',') {
3447  if (*(separator + 1) == 'C') {
3448  /* TODO: process ctrl-c */
3449  fileio_ctrl_c = true;
3450  }
3451  }
3452  }
3453 
3454  LOG_DEBUG("File-I/O response, retcode: 0x%x, errno: 0x%x, ctrl-c: %s",
3455  fileio_retcode, fileio_errno, fileio_ctrl_c ? "true" : "false");
3456 
3457  retval = target_gdb_fileio_end(target, fileio_retcode, fileio_errno, fileio_ctrl_c);
3458  if (retval != ERROR_OK)
3459  return ERROR_FAIL;
3460 
3461  /* After File-I/O ends, keep continue or step */
3462  if (gdb_running_type == 'c')
3463  retval = target_resume(target, 1, 0x0, 0, 0);
3464  else if (gdb_running_type == 's')
3465  retval = target_step(target, 1, 0x0, 0);
3466  else
3467  retval = ERROR_FAIL;
3468 
3469  if (retval != ERROR_OK)
3470  return ERROR_FAIL;
3471 
3472  return ERROR_OK;
3473 }
3474 
3475 static void gdb_log_callback(void *priv, const char *file, unsigned line,
3476  const char *function, const char *string)
3477 {
3478  struct connection *connection = priv;
3479  struct gdb_connection *gdb_con = connection->priv;
3480 
3481  if (gdb_con->output_flag != GDB_OUTPUT_ALL)
3482  /* No out allowed */
3483  return;
3484 
3485  if (gdb_con->busy) {
3486  /* do not reply this using the O packet */
3487  return;
3488  }
3489 
3490  gdb_output_con(connection, string);
3491 }
3492 
3494 {
3495  char sig_reply[4];
3496  snprintf(sig_reply, 4, "T%2.2x", 2);
3497  gdb_put_packet(connection, sig_reply, 3);
3498 }
3499 
3501 {
3502  /* Do not allocate this on the stack */
3503  static char gdb_packet_buffer[GDB_BUFFER_SIZE + 1]; /* Extra byte for null-termination */
3504 
3505  struct target *target;
3506  char const *packet = gdb_packet_buffer;
3507  int packet_size;
3508  int retval;
3509  struct gdb_connection *gdb_con = connection->priv;
3510  static bool warn_use_ext;
3511 
3513 
3514  /* drain input buffer. If one of the packets fail, then an error
3515  * packet is replied, if applicable.
3516  *
3517  * This loop will terminate and the error code is returned.
3518  *
3519  * The calling fn will check if this error is something that
3520  * can be recovered from, or if the connection must be closed.
3521  *
3522  * If the error is recoverable, this fn is called again to
3523  * drain the rest of the buffer.
3524  */
3525  do {
3526  packet_size = GDB_BUFFER_SIZE;
3527  retval = gdb_get_packet(connection, gdb_packet_buffer, &packet_size);
3528  if (retval != ERROR_OK)
3529  return retval;
3530 
3531  /* terminate with zero */
3532  gdb_packet_buffer[packet_size] = '\0';
3533 
3534  if (packet_size > 0) {
3535 
3536  gdb_log_incoming_packet(connection, gdb_packet_buffer);
3537 
3538  retval = ERROR_OK;
3539  switch (packet[0]) {
3540  case 'T': /* Is thread alive? */
3541  gdb_thread_packet(connection, packet, packet_size);
3542  break;
3543  case 'H': /* Set current thread ( 'c' for step and continue,
3544  * 'g' for all other operations ) */
3545  gdb_thread_packet(connection, packet, packet_size);
3546  break;
3547  case 'q':
3548  case 'Q':
3549  retval = gdb_thread_packet(connection, packet, packet_size);
3550  if (retval == GDB_THREAD_PACKET_NOT_CONSUMED)
3551  retval = gdb_query_packet(connection, packet, packet_size);
3552  break;
3553  case 'g':
3554  retval = gdb_get_registers_packet(connection, packet, packet_size);
3555  break;
3556  case 'G':
3557  retval = gdb_set_registers_packet(connection, packet, packet_size);
3558  break;
3559  case 'p':
3560  retval = gdb_get_register_packet(connection, packet, packet_size);
3561  break;
3562  case 'P':
3563  retval = gdb_set_register_packet(connection, packet, packet_size);
3564  break;
3565  case 'm':
3566  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3567  retval = gdb_read_memory_packet(connection, packet, packet_size);
3568  gdb_con->output_flag = GDB_OUTPUT_NO;
3569  break;
3570  case 'M':
3571  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3572  retval = gdb_write_memory_packet(connection, packet, packet_size);
3573  gdb_con->output_flag = GDB_OUTPUT_NO;
3574  break;
3575  case 'z':
3576  case 'Z':
3577  retval = gdb_breakpoint_watchpoint_packet(connection, packet, packet_size);
3578  break;
3579  case '?':
3580  gdb_last_signal_packet(connection, packet, packet_size);
3581  /* '?' is sent after the eventual '!' */
3582  if (!warn_use_ext && !gdb_con->extended_protocol) {
3583  warn_use_ext = true;
3584  LOG_WARNING("Prefer GDB command \"target extended-remote :%s\" instead of \"target remote :%s\"",
3586  }
3587  break;
3588  case 'c':
3589  case 's':
3590  {
3591  gdb_thread_packet(connection, packet, packet_size);
3592  gdb_con->output_flag = GDB_OUTPUT_ALL;
3593 
3594  if (gdb_con->mem_write_error) {
3595  LOG_ERROR("Memory write failure!");
3596 
3597  /* now that we have reported the memory write error,
3598  * we can clear the condition */
3599  gdb_con->mem_write_error = false;
3600  }
3601 
3602  bool nostep = false;
3603  bool already_running = false;
3604  if (target->state == TARGET_RUNNING) {
3605  LOG_WARNING("WARNING! The target is already running. "
3606  "All changes GDB did to registers will be discarded! "
3607  "Waiting for target to halt.");
3608  already_running = true;
3609  } else if (target->state != TARGET_HALTED) {
3610  LOG_WARNING("The target is not in the halted nor running stated, "
3611  "stepi/continue ignored.");
3612  nostep = true;
3613  } else if ((packet[0] == 's') && gdb_con->sync) {
3614  /* Hmm..... when you issue a continue in GDB, then a "stepi" is
3615  * sent by GDB first to OpenOCD, thus defeating the check to
3616  * make only the single stepping have the sync feature...
3617  */
3618  nostep = true;
3619  LOG_DEBUG("stepi ignored. GDB will now fetch the register state "
3620  "from the target.");
3621  }
3622  gdb_con->sync = false;
3623 
3624  if (!already_running && nostep) {
3625  /* Either the target isn't in the halted state, then we can't
3626  * step/continue. This might be early setup, etc.
3627  *
3628  * Or we want to allow GDB to pick up a fresh set of
3629  * register values without modifying the target state.
3630  *
3631  */
3633 
3634  /* stop forwarding log packets! */
3635  gdb_con->output_flag = GDB_OUTPUT_NO;
3636  } else {
3637  /* We're running/stepping, in which case we can
3638  * forward log output until the target is halted
3639  */
3640  gdb_con->frontend_state = TARGET_RUNNING;
3642 
3643  if (!already_running) {
3644  /* Here we don't want packet processing to stop even if this fails,
3645  * so we use a local variable instead of retval. */
3646  retval = gdb_step_continue_packet(connection, packet, packet_size);
3647  if (retval != ERROR_OK) {
3648  /* we'll never receive a halted
3649  * condition... issue a false one..
3650  */
3652  }
3653  }
3654  }
3655  }
3656  break;
3657  case 'v':
3658  retval = gdb_v_packet(connection, packet, packet_size);
3659  break;
3660  case 'D':
3661  retval = gdb_detach(connection);
3662  break;
3663  case 'X':
3664  gdb_con->output_flag = GDB_OUTPUT_NOTIF;
3665  retval = gdb_write_memory_binary_packet(connection, packet, packet_size);
3666  gdb_con->output_flag = GDB_OUTPUT_NO;
3667  break;
3668  case 'k':
3669  if (gdb_con->extended_protocol) {
3670  gdb_con->attached = false;
3671  break;
3672  }
3673  gdb_put_packet(connection, "OK", 2);
3675  case '!':
3676  /* handle extended remote protocol */
3677  gdb_con->extended_protocol = true;
3678  gdb_put_packet(connection, "OK", 2);
3679  break;
3680  case 'R':
3681  /* handle extended restart packet */
3682  gdb_restart_inferior(connection, packet, packet_size);
3683  break;
3684 
3685  case 'j':
3686  /* DEPRECATED */
3687  /* packet supported only by smp target i.e cortex_a.c*/
3688  /* handle smp packet replying coreid played to gbd */
3689  gdb_read_smp_packet(connection, packet, packet_size);
3690  break;
3691 
3692  case 'J':
3693  /* DEPRECATED */
3694  /* packet supported only by smp target i.e cortex_a.c */
3695  /* handle smp packet setting coreid to be played at next
3696  * resume to gdb */
3697  gdb_write_smp_packet(connection, packet, packet_size);
3698  break;
3699 
3700  case 'F':
3701  /* File-I/O extension */
3702  /* After gdb uses host-side syscall to complete target file
3703  * I/O, gdb sends host-side syscall return value to target
3704  * by 'F' packet.
3705  * The format of 'F' response packet is
3706  * Fretcode,errno,Ctrl-C flag;call-specific attachment
3707  */
3708  gdb_con->frontend_state = TARGET_RUNNING;
3709  gdb_con->output_flag = GDB_OUTPUT_ALL;
3710  gdb_fileio_response_packet(connection, packet, packet_size);
3711  break;
3712 
3713  default:
3714  /* ignore unknown packets */
3715  LOG_DEBUG("ignoring 0x%2.2x packet", packet[0]);
3716  gdb_put_packet(connection, "", 0);
3717  break;
3718  }
3719 
3720  /* if a packet handler returned an error, exit input loop */
3721  if (retval != ERROR_OK)
3722  return retval;
3723  }
3724 
3725  if (gdb_con->ctrl_c) {
3726  if (target->state == TARGET_RUNNING) {
3727  struct target *t = target;
3728  if (target->rtos)
3730  retval = target_halt(t);
3731  if (retval == ERROR_OK)
3732  retval = target_poll(t);
3733  if (retval != ERROR_OK)
3735  gdb_con->ctrl_c = false;
3736  } else {
3737  LOG_INFO("The target is not running when halt was requested, stopping GDB.");
3739  }
3740  }
3741 
3742  } while (gdb_con->buf_cnt > 0);
3743 
3744  return ERROR_OK;
3745 }
3746 
3747 static int gdb_input(struct connection *connection)
3748 {
3749  int retval = gdb_input_inner(connection);
3750  struct gdb_connection *gdb_con = connection->priv;
3751  if (retval == ERROR_SERVER_REMOTE_CLOSED)
3752  return retval;
3753 
3754  /* logging does not propagate the error, yet can set the gdb_con->closed flag */
3755  if (gdb_con->closed)
3757 
3758  /* we'll recover from any other errors(e.g. temporary timeouts, etc.) */
3759  return ERROR_OK;
3760 }
3761 
3762 /*
3763  * Send custom notification packet as keep-alive during memory read/write.
3764  *
3765  * From gdb 7.0 (released 2009-10-06) an unknown notification received during
3766  * memory read/write would be silently dropped.
3767  * Before gdb 7.0 any character, with exclusion of "+-$", would be considered
3768  * as junk and ignored.
3769  * In both cases the reception will reset the timeout counter in gdb, thus
3770  * working as a keep-alive.
3771  * Check putpkt_binary() and getpkt_sane() in gdb commit
3772  * 74531fed1f2d662debc2c209b8b3faddceb55960
3773  *
3774  * Enable remote debug in gdb with 'set debug remote 1' to either dump the junk
3775  * characters in gdb pre-7.0 and the notification from gdb 7.0.
3776  */
3778 {
3779  static unsigned char count;
3780  unsigned char checksum = 0;
3781  char buf[22];
3782 
3783  int len = sprintf(buf, "%%oocd_keepalive:%2.2x", count++);
3784  for (int i = 1; i < len; i++)
3785  checksum += buf[i];
3786  len += sprintf(buf + len, "#%2.2x", checksum);
3787 
3788 #ifdef _DEBUG_GDB_IO_
3789  LOG_DEBUG("sending packet '%s'", buf);
3790 #endif
3791 
3792  gdb_write(connection, buf, len);
3793 }
3794 
3796 {
3797  struct gdb_connection *gdb_con = connection->priv;
3798 
3799  switch (gdb_con->output_flag) {
3800  case GDB_OUTPUT_NO:
3801  /* no need for keep-alive */
3802  break;
3803  case GDB_OUTPUT_NOTIF:
3804  /* send asynchronous notification */
3806  break;
3807  case GDB_OUTPUT_ALL:
3808  /* send an empty O packet */
3810  break;
3811  default:
3812  break;
3813  }
3814 }
3815 
3816 static const struct service_driver gdb_service_driver = {
3817  .name = "gdb",
3818  .new_connection_during_keep_alive_handler = NULL,
3819  .new_connection_handler = gdb_new_connection,
3820  .input_handler = gdb_input,
3821  .connection_closed_handler = gdb_connection_closed,
3822  .keep_client_alive_handler = gdb_keep_client_alive,
3823 };
3824 
3825 static int gdb_target_start(struct target *target, const char *port)
3826 {
3827  struct gdb_service *gdb_service;
3828  int ret;
3829  gdb_service = malloc(sizeof(struct gdb_service));
3830 
3831  if (!gdb_service)
3832  return -ENOMEM;
3833 
3834  LOG_INFO("starting gdb server for %s on %s", target_name(target), port);
3835 
3837  gdb_service->core[0] = -1;
3838  gdb_service->core[1] = -1;
3840 
3842  /* initialize all targets gdb service with the same pointer */
3843  {
3844  struct target_list *head;
3846  struct target *curr = head->target;
3847  if (curr != target)
3848  curr->gdb_service = gdb_service;
3849  }
3850  }
3851  return ret;
3852 }
3853 
3854 static int gdb_target_add_one(struct target *target)
3855 {
3856  /* one gdb instance per smp list */
3857  if ((target->smp) && (target->gdb_service))
3858  return ERROR_OK;
3859 
3860  /* skip targets that cannot handle a gdb connections (e.g. mem_ap) */
3862  LOG_DEBUG("skip gdb server for target %s", target_name(target));
3863  return ERROR_OK;
3864  }
3865 
3866  if (target->gdb_port_override) {
3867  if (strcmp(target->gdb_port_override, "disabled") == 0) {
3868  LOG_INFO("gdb port disabled");
3869  return ERROR_OK;
3870  }
3872  }
3873 
3874  if (strcmp(gdb_port, "disabled") == 0) {
3875  LOG_INFO("gdb port disabled");
3876  return ERROR_OK;
3877  }
3878 
3879  int retval = gdb_target_start(target, gdb_port_next);
3880  if (retval == ERROR_OK) {
3881  /* save the port number so can be queried with
3882  * $target_name cget -gdb-port
3883  */
3885 
3886  long portnumber;
3887  /* If we can parse the port number
3888  * then we increment the port number for the next target.
3889  */
3890  char *end;
3891  portnumber = strtol(gdb_port_next, &end, 0);
3892  if (!*end) {
3893  if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
3894  free(gdb_port_next);
3895  if (portnumber) {
3896  gdb_port_next = alloc_printf("%ld", portnumber+1);
3897  } else {
3898  /* Don't increment if gdb_port is 0, since we're just
3899  * trying to allocate an unused port. */
3900  gdb_port_next = strdup("0");
3901  }
3902  }
3903  }
3904  }
3905  return retval;
3906 }
3907 
3909 {
3910  if (!target) {
3911  LOG_WARNING("gdb services need one or more targets defined");
3912  return ERROR_OK;
3913  }
3914 
3915  while (target) {
3916  int retval = gdb_target_add_one(target);
3917  if (retval != ERROR_OK)
3918  return retval;
3919 
3920  target = target->next;
3921  }
3922 
3923  return ERROR_OK;
3924 }
3925 
3926 COMMAND_HANDLER(handle_gdb_sync_command)
3927 {
3928  if (CMD_ARGC != 0)
3930 
3931  if (!current_gdb_connection) {
3933  "gdb_sync command can only be run from within gdb using \"monitor gdb_sync\"");
3934  return ERROR_FAIL;
3935  }
3936 
3937  current_gdb_connection->sync = true;
3938 
3939  return ERROR_OK;
3940 }
3941 
3942 /* daemon configuration command gdb_port */
3943 COMMAND_HANDLER(handle_gdb_port_command)
3944 {
3945  int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
3946  if (retval == ERROR_OK) {
3947  free(gdb_port_next);
3948  gdb_port_next = strdup(gdb_port);
3949  }
3950  return retval;
3951 }
3952 
3953 COMMAND_HANDLER(handle_gdb_memory_map_command)
3954 {
3955  if (CMD_ARGC != 1)
3957 
3959  return ERROR_OK;
3960 }
3961 
3962 COMMAND_HANDLER(handle_gdb_flash_program_command)
3963 {
3964  if (CMD_ARGC != 1)
3966 
3968  return ERROR_OK;
3969 }
3970 
3971 COMMAND_HANDLER(handle_gdb_report_data_abort_command)
3972 {
3973  if (CMD_ARGC != 1)
3975 
3977  return ERROR_OK;
3978 }
3979 
3980 COMMAND_HANDLER(handle_gdb_report_register_access_error)
3981 {
3982  if (CMD_ARGC != 1)
3984 
3986  return ERROR_OK;
3987 }
3988 
3989 /* gdb_breakpoint_override */
3990 COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
3991 {
3992  if (CMD_ARGC == 0) {
3993  /* nothing */
3994  } else if (CMD_ARGC == 1) {
3996  if (strcmp(CMD_ARGV[0], "hard") == 0)
3998  else if (strcmp(CMD_ARGV[0], "soft") == 0)
4000  else if (strcmp(CMD_ARGV[0], "disable") == 0)
4002  } else
4005  LOG_USER("force %s breakpoints",
4006  (gdb_breakpoint_override_type == BKPT_HARD) ? "hard" : "soft");
4007  else
4008  LOG_USER("breakpoint type is not overridden");
4009 
4010  return ERROR_OK;
4011 }
4012 
4013 COMMAND_HANDLER(handle_gdb_target_description_command)
4014 {
4015  if (CMD_ARGC != 1)
4017 
4019  return ERROR_OK;
4020 }
4021 
4022 COMMAND_HANDLER(handle_gdb_save_tdesc_command)
4023 {
4024  char *tdesc;
4025  uint32_t tdesc_length;
4027 
4028  int retval = gdb_generate_target_description(target, &tdesc);
4029  if (retval != ERROR_OK) {
4030  LOG_ERROR("Unable to Generate Target Description");
4031  return ERROR_FAIL;
4032  }
4033 
4034  tdesc_length = strlen(tdesc);
4035 
4036  struct fileio *fileio;
4037  size_t size_written;
4038 
4039  char *tdesc_filename = alloc_printf("%s.xml", target_type_name(target));
4040  if (!tdesc_filename) {
4041  retval = ERROR_FAIL;
4042  goto out;
4043  }
4044 
4045  retval = fileio_open(&fileio, tdesc_filename, FILEIO_WRITE, FILEIO_TEXT);
4046 
4047  if (retval != ERROR_OK) {
4048  LOG_ERROR("Can't open %s for writing", tdesc_filename);
4049  goto out;
4050  }
4051 
4052  retval = fileio_write(fileio, tdesc_length, tdesc, &size_written);
4053 
4055 
4056  if (retval != ERROR_OK)
4057  LOG_ERROR("Error while writing the tdesc file");
4058 
4059 out:
4060  free(tdesc_filename);
4061  free(tdesc);
4062 
4063  return retval;
4064 }
4065 
4066 static const struct command_registration gdb_command_handlers[] = {
4067  {
4068  .name = "gdb_sync",
4069  .handler = handle_gdb_sync_command,
4070  .mode = COMMAND_ANY,
4071  .help = "next stepi will return immediately allowing "
4072  "GDB to fetch register state without affecting "
4073  "target state",
4074  .usage = ""
4075  },
4076  {
4077  .name = "gdb_port",
4078  .handler = handle_gdb_port_command,
4079  .mode = COMMAND_CONFIG,
4080  .help = "Normally gdb listens to a TCP/IP port. Each subsequent GDB "
4081  "server listens for the next port number after the "
4082  "base port number specified. "
4083  "No arguments reports GDB port. \"pipe\" means listen to stdin "
4084  "output to stdout, an integer is base port number, \"disabled\" disables "
4085  "port. Any other string is are interpreted as named pipe to listen to. "
4086  "Output pipe is the same name as input pipe, but with 'o' appended.",
4087  .usage = "[port_num]",
4088  },
4089  {
4090  .name = "gdb_memory_map",
4091  .handler = handle_gdb_memory_map_command,
4092  .mode = COMMAND_CONFIG,
4093  .help = "enable or disable memory map",
4094  .usage = "('enable'|'disable')"
4095  },
4096  {
4097  .name = "gdb_flash_program",
4098  .handler = handle_gdb_flash_program_command,
4099  .mode = COMMAND_CONFIG,
4100  .help = "enable or disable flash program",
4101  .usage = "('enable'|'disable')"
4102  },
4103  {
4104  .name = "gdb_report_data_abort",
4105  .handler = handle_gdb_report_data_abort_command,
4106  .mode = COMMAND_CONFIG,
4107  .help = "enable or disable reporting data aborts",
4108  .usage = "('enable'|'disable')"
4109  },
4110  {
4111  .name = "gdb_report_register_access_error",
4112  .handler = handle_gdb_report_register_access_error,
4113  .mode = COMMAND_CONFIG,
4114  .help = "enable or disable reporting register access errors",
4115  .usage = "('enable'|'disable')"
4116  },
4117  {
4118  .name = "gdb_breakpoint_override",
4119  .handler = handle_gdb_breakpoint_override_command,
4120  .mode = COMMAND_ANY,
4121  .help = "Display or specify type of breakpoint "
4122  "to be used by gdb 'break' commands.",
4123  .usage = "('hard'|'soft'|'disable')"
4124  },
4125  {
4126  .name = "gdb_target_description",
4127  .handler = handle_gdb_target_description_command,
4128  .mode = COMMAND_CONFIG,
4129  .help = "enable or disable target description",
4130  .usage = "('enable'|'disable')"
4131  },
4132  {
4133  .name = "gdb_save_tdesc",
4134  .handler = handle_gdb_save_tdesc_command,
4135  .mode = COMMAND_EXEC,
4136  .help = "Save the target description file",
4137  .usage = "",
4138  },
4140 };
4141 
4143 {
4144  gdb_port = strdup("3333");
4145  gdb_port_next = strdup("3333");
4146  return register_commands(cmd_ctx, NULL, gdb_command_handlers);
4147 }
4148 
4150 {
4151  free(gdb_port);
4152  free(gdb_port_next);
4153 }
4154 
4156 {
4157  return gdb_actual_connections;
4158 }
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:1432
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:392
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:354
int watchpoint_add(struct target *target, target_addr_t address, uint32_t 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 watchpoint_clear_target(struct target *target)
Definition: breakpoints.c:644
int breakpoint_clear_target(struct target *target)
Definition: breakpoints.c:468
int breakpoint_add(struct target *target, target_addr_t address, uint32_t length, enum breakpoint_type type)
Definition: breakpoints.c:208
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:524
#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:1493
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:3825
static int gdb_target_description_supported(struct target *target, int *supported)
Definition: gdb_server.c:2602
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:3777
static int gdb_v_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:3248
static char * gdb_port
Definition: gdb_server.c:116
static const struct service_driver gdb_service_driver
Definition: gdb_server.c:3816
static int gdb_input_inner(struct connection *connection)
Definition: gdb_server.c:3500
static int gdb_put_packet_inner(struct connection *connection, char *buffer, int len)
Definition: gdb_server.c:404
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:1239
static int gdb_target_add_one(struct target *target)
Definition: gdb_server.c:3854
static void gdb_sig_halted(struct connection *connection)
Definition: gdb_server.c:3493
static bool gdb_handle_vrun_packet(struct connection *connection, const char *packet, int packet_size)
Definition: gdb_server.c:3206
static void gdb_log_incoming_packet(struct connection *connection, char *packet)
Definition: gdb_server.c:354
static int gdb_reg_pos(struct target *target, int pos, int len)
Definition: gdb_server.c:1163
static int gdb_generate_thread_list(struct target *target, char **thread_list_out)
Definition: gdb_server.c:2647
static struct gdb_connection * current_gdb_connection
Definition: gdb_server.c:110
COMMAND_HANDLER(handle_gdb_sync_command)
Definition: gdb_server.c:3926
static int gdb_detach(struct connection *connection)
Definition: gdb_server.c:3416
static void gdb_log_outgoing_packet(struct connection *connection, char *packet_buf, unsigned int packet_len, unsigned char checksum)
Definition: gdb_server.c:387
static int compare_bank(const void *a, const void *b)
Definition: gdb_server.c:1887
int gdb_register_commands(struct command_context *cmd_ctx)
Definition: gdb_server.c:4142
static void gdb_keep_client_alive(struct connection *connection)
Definition: gdb_server.c:3795
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:1219
static int gdb_query_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:2746
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:2703
static char * next_hex_encoded_field(const char **str, char sep)
Definition: gdb_server.c:3156
static void gdb_restart_inferior(struct connection *connection, const char *packet, int packet_size)
Definition: gdb_server.c:3191
int gdb_target_add_all(struct target *target)
Definition: gdb_server.c:3908
static int gdb_set_registers_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1305
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:2118
static int decode_xfer_read(char const *buf, char **annex, int *ofs, unsigned int *len)
Definition: gdb_server.c:1860
static int gdb_fileio_response_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:3430
static int gdb_memory_map(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1901
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:1486
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:4066
static int gdb_input(struct connection *connection)
Definition: gdb_server.c:3747
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:2252
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:2548
static int gdb_get_register_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1368
static int gdb_flash_program
Definition: gdb_server.c:133
int gdb_get_actual_connections(void)
Definition: gdb_server.c:4155
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:1568
static __attribute__((format(PRINTF_ATTRIBUTE_FORMAT, 5, 6)))
Definition: gdb_server.c:1824
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:4149
static void gdb_log_callback(void *priv, const char *file, unsigned line, const char *function, const char *string)
Definition: gdb_server.c:3475
static bool gdb_handle_vcont_packet(struct connection *connection, const char *packet, __attribute__((unused)) int packet_size)
Definition: gdb_server.c:2982
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(struct connection *connection, void *data, int len)
Definition: gdb_server.c:338
static int gdb_write_memory_binary_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1619
static int gdb_use_memory_map
Definition: gdb_server.c:131
static int gdb_breakpoint_override
Definition: gdb_server.c:112
static int gdb_use_target_description
Definition: gdb_server.c:147
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:2295
static int gdb_report_data_abort
Definition: gdb_server.c:139
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:2408
static void gdb_str_to_target(struct target *target, char *tstr, struct reg *reg)
Definition: gdb_server.c:1180
static int gdb_last_signal(struct target *target)
Definition: gdb_server.c:152
static void gdb_send_error(struct connection *connection, uint8_t the_error)
Definition: gdb_server.c:1133
static int gdb_get_packet_inner(struct connection *connection, char *buffer, int *len)
Definition: gdb_server.c:653
static int gdb_last_signal_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1140
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:2048
static int gdb_output(struct command_context *context, const char *line)
Definition: gdb_server.c:765
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:2094
static int gdb_step_continue_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1698
static int gdb_breakpoint_watchpoint_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1726
int gdb_put_packet(struct connection *connection, char *buffer, int len)
Definition: gdb_server.c:525
static int gdb_set_register_packet(struct connection *connection, char const *packet, int packet_size)
Definition: gdb_server.c:1412
static void gdb_target_to_reg(struct target *target, char const *tstr, int str_len, uint8_t *bin)
Definition: gdb_server.c:1197
#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 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
char * find_nonprint_char(char *buf, unsigned buf_len)
Find the first non-printable character in the char buffer, return a pointer to it.
Definition: log.c:508
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
char * alloc_printf(const char *format,...)
Definition: log.c:364
#define LOG_USER(expr ...)
Definition: log.h:135
#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_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
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
int smp
Definition: target.h:187
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
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:6779
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