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