Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
target_gdb_rsp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "config.h"
20 
21 #include <errno.h>
22 #include <ctype.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/select.h>
27 #include <fcntl.h>
28 #include <sys/mman.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #if !defined(UNIX_PATH_MAX)
32 #define UNIX_PATH_MAX (size_t)sizeof(((struct sockaddr_un *) 0)->sun_path)
33 #endif
34 #include <netdb.h>
35 #include <arpa/inet.h>
36 #include <netinet/tcp.h>
37 #include <argp.h>
38 
39 #include "common.h"
40 #include "arch.h"
41 #include "arch_x86.h"
42 #include "arch_x86_64.h"
43 #include "evloop.h"
44 #include "target_api.h"
45 #include "target.h"
46 #include "target_arch_x86.h"
47 #include "target_gdb.h"
48 #include "target_gdb_rsp.h"
49 
50 #include <glib.h>
51 
52 extern int h_errno;
53 
54 /*
55  * Local prototypes.
56  */
57 static inline int __h2ul(char *str,unsigned int len,unsigned long *out);
58 static inline int __hs2ul_lsb(char *str,unsigned int len,unsigned long *out);
59 static inline int __h2ul_be(char *str,unsigned int len,unsigned long *out);
60 static inline int __hs2d(char *str,unsigned int len,char *buf);
61 static inline int __d2hs(char *buf,unsigned int len,char *str);
62 static int gdb_rsp_send_raw(struct target *target,
63  char *data,unsigned int len);
65 gdb_rsp_simple_ack_handler(struct target *target,char *data,unsigned int len,
66  void *handler_data);
68 gdb_rsp_features_handler(struct target *target,char *data,unsigned int len,
69  void *handler_data);
71 gdb_rsp_vcont_check_handler(struct target *target,char *data,unsigned int len,
72  void *handler_data);
74 gdb_rsp_stop_handler(struct target *target,char *data,unsigned int len,
75  void *handler_data);
76 
77 int gdb_rsp_query_stub(struct target *target);
78 
85  struct gdb_state *gstate = (struct gdb_state *)target->state;
86  struct gdb_spec *spec = (struct gdb_spec *)target->spec->backend_spec;
87  struct stat sbuf;
88  struct sockaddr_un sun,sun_client;
89  char *tmpdir;
90  char *cpath;
91  int cpath_len;
92  int len;
93  struct hostent *he;
94  void *dst;
95  int addrtype;
96  int dlen;
97  struct sockaddr_in sin,sinc;
98  struct sockaddr_in6 sin6,sin6c;
100  int sret = 0;
101 
102  if (gstate->fd > -1) {
103  verror("already connected via fd %d!\n",gstate->fd);
104  errno = EALREADY;
105  return -1;
106  }
107 
108  if (spec->hostname || spec->port > -1) {
109  if (!spec->hostname)
110  spec->hostname = strdup("localhost");
111  if (spec->port < 0)
112  spec->port = 1234;
113 
114  he = gethostbyname(spec->hostname);
115  if (!he) {
116  verror("gethostbyname(%s): %s (%d)!",spec->hostname,
117  hstrerror(h_errno),h_errno);
118  goto in_err;
119  }
120 
121  addrtype = he->h_addrtype;
122  if (addrtype == AF_INET) {
123  memcpy(&sin.sin_addr,he->h_addr,he->h_length);
124  dlen = sizeof(sin);
125  sin.sin_port = htons(spec->port);
126  sin.sin_family = addrtype;
127  }
128  else if (addrtype == AF_INET6) {
129  memcpy(&sin6.sin6_addr,he->h_addr,he->h_length);
130  dlen = sizeof(sin6);
131  sin6.sin6_port = htons(spec->port);
132  sin6.sin6_family = addrtype;
133  }
134  else {
135  verror("unknown addrtype %d for hostname %s!\n",
136  addrtype,spec->hostname);
137  goto in_err;
138  }
139 
140  /*
141  if (inet_pton(addrtype,he->h_addr,dst) != 1) {
142  verror("could not convert addr %s to network!\n",he->h_addr);
143  goto in_err;
144  }
145  */
146 
147  if (spec->do_udp)
148  gstate->fd = socket(addrtype,SOCK_DGRAM,0);
149  else
150  gstate->fd = socket(addrtype,SOCK_STREAM,0);
151  if (gstate->fd < 0) {
152  verror("socket(): %s\n",strerror(errno));
153  goto in_err;
154  }
155 
156  if (addrtype == AF_INET) {
157  dst = &sin;
158  dlen = sizeof(sin);
159  if (spec->do_udp) {
160  sinc.sin_family = addrtype;
161  sinc.sin_addr.s_addr = INADDR_ANY;
162  sinc.sin_port = 0;
163  if (bind(gstate->fd,&sinc,sizeof(sinc))) {
164  verror("could not bind udp socket: %s (%d)!\n",
165  strerror(errno),errno);
166  goto in_err;
167  }
168  }
169  }
170  else {
171  dst = &sin6;
172  dlen = sizeof(sin6);
173  if (spec->do_udp) {
174  sin6c.sin6_family = addrtype;
175  sin6c.sin6_addr = in6addr_any;
176  sin6c.sin6_port = 0;
177  if (bind(gstate->fd,&sin6c,sizeof(sin6c))) {
178  verror("could not bind udp socket: %s (%d)!\n",
179  strerror(errno),errno);
180  goto in_err;
181  }
182  }
183  }
184 
185  if (connect(gstate->fd,(struct sockaddr *)dst,dlen) < 0) {
186  verror("connect(%s): %s\n",he->h_addr,strerror(errno));
187  goto in_err;
188  }
189 
190  if (!spec->do_udp) {
191  const int one = 1;
192  setsockopt(gstate->fd,IPPROTO_TCP,TCP_NODELAY,&one,sizeof(&one));
193  }
194 
196  "connected to %s socket %s:%d (fd %d)\n",
197  (spec->do_udp) ? "udp" : "tcp",he->h_name,spec->port,gstate->fd);
198 
199  goto connected;
200 
201  in_err:
202  if (gstate->fd > -1) {
203  close(gstate->fd);
204  gstate->fd = -1;
205  }
206  return -1;
207 
208  }
209  else if (spec->sockfile) {
210  memset(&sun,0,sizeof(sun));
211  sun.sun_family = AF_UNIX;
212  snprintf(sun.sun_path,UNIX_PATH_MAX,"%s",spec->sockfile);
213 
214  /*
215  * Just try TMPDIR or /tmp or .
216  */
217  if ((tmpdir = getenv("TMPDIR"))
218  && stat(tmpdir,&sbuf) == 0 && access(tmpdir,W_OK) == 0) {
219  cpath_len =
220  strlen(tmpdir) + 1 + strlen("target_gdb_sock.") + 11 + 1;
221  cpath = malloc(cpath_len);
222  snprintf(cpath,cpath_len,"%s/target_gdb_sock.%d",tmpdir,getpid());
223  }
224  else if (stat("/tmp",&sbuf) == 0
225  && S_ISDIR(sbuf.st_mode) && access("/tmp",W_OK) == 0) {
226  cpath_len =
227  strlen("/tmp") + 1 + strlen("target_gdb_sock.") + 11 + 1;
228  cpath = malloc(cpath_len);
229  snprintf(cpath,cpath_len,"/tmp/target_gdb_sock.%d",getpid());
230  }
231  else if (stat("/var/tmp",&sbuf) == 0
232  && S_ISDIR(sbuf.st_mode) && access("/var/run",W_OK) == 0) {
233  cpath_len =
234  strlen("/var/run") + 1 + strlen("target_gdb_sock.") + 11 + 1;
235  cpath = malloc(cpath_len);
236  snprintf(cpath,cpath_len,"/var/run/target_gdb_sock.%d",getpid());
237  }
238  else {
239  cpath_len = strlen(".") + 1 + strlen("target_gdb_sock.") + 11 + 1;
240  cpath = malloc(cpath_len);
241  snprintf(cpath,cpath_len,"./target_gdb_sock.%d",getpid());
242  }
243 
244  memset(&sun_client,0,sizeof(sun_client));
245  sun_client.sun_family = AF_UNIX;
246  snprintf(sun_client.sun_path,UNIX_PATH_MAX,"%s",cpath);
247 
248  gstate->sockfile = strdup(cpath);
249 
250  gstate->fd = socket(AF_UNIX,SOCK_STREAM,0);
251  if (gstate->fd < 0) {
252  verror("socket(): %s\n",strerror(errno));
253  goto sun_err;
254  }
255  len = offsetof(struct sockaddr_un,sun_path)
256  + strlen(sun_client.sun_path);
257  if (bind(gstate->fd,&sun_client,len) < 0) {
258  verror("bind(%s): %s\n",sun_client.sun_path,strerror(errno));
259  goto sun_err;
260  }
261  if (fchmod(gstate->fd,S_IRUSR | S_IWUSR) < 0) {
262  verror("chmod(%s): %s\n",sun_client.sun_path,strerror(errno));
263  goto sun_err;
264  }
265 
266  len = offsetof(struct sockaddr_un,sun_path) + strlen(sun.sun_path);
267  if (connect(gstate->fd,&sun,len) < 0) {
268  verror("connect(%s): %s\n",sun.sun_path,strerror(errno));
269  goto sun_err;
270  }
271 
273  "connected to unix socket %s (fd %d)\n",
274  gstate->sockfile,gstate->fd);
275 
276  goto connected;
277 
278  sun_err:
279  if (gstate->fd > -1) {
280  close(gstate->fd);
281  gstate->fd = -1;
282  }
283  if (gstate->sockfile) {
284  unlink(gstate->sockfile);
285  free(gstate->sockfile);
286  gstate->sockfile = NULL;
287  }
288  return -1;
289  }
290  else if (spec->devfile || spec->do_stdio) {
291  verror("unsupported transport!\n");
292  errno = ENOTSUP;
293  return -1;
294  }
295  else {
296  verror("unspecified transport!\n");
297  errno = EINVAL;
298  return -1;
299  }
300 
301  connected:
302 
303  /*
304  * Put the read fd into nonblocking. We don't want reads to stall
305  * us; we'd be ok with synchronous writes; but since for most
306  * transports, read/write will go over the same bidirectional socket,
307  * we'll make them both nonblocking and handle it. Well, actually,
308  * we're going to force synchronous writes... because we don't
309  * really have a mechanism in the target library to queue commands
310  * and callback to caller. Don't have that at all.
311  */
312 
313  /*
314  * Setup the recv buf.
315  */
316  gstate->ibuf = malloc(4096);
317  gstate->ibuf_alen = 4096;
318  gstate->ibuf_len = 0;
319 
320  /*
321  * Send a quick ack to get things started.
322  */
323  if (gdb_rsp_ack(target)) {
324  verror("failed to send an initial ack!\n");
325  goto err_setup;
326  }
327 
328  /*
329  * Tell the stub about our features, and get the stub's features.
330  */
331  if (gdb_rsp_query_stub(target)) {
332  verror("failed to query stub features!\n");
333  goto err_setup;
334  }
335 
336  /*
337  * Set some defaults for subsequent operations...
338  */
339  if (gdb_rsp_send_packet(target,"Hg0",0,gdb_rsp_simple_ack_handler,&sret)) {
340  verror("failed to set Hg0 defaults!\n");
341  return -1;
342  }
343 
344  if (gdb_rsp_recv_until_handled(target,gdb_rsp_simple_ack_handler,&hret)) {
345  verror("failed to recv Hg0 defaults response; aborting!\n");
346  return -1;
347  }
348  else if (hret != GDB_RSP_HANDLER_DONE) {
349  verror("simple ack handler returned not done: %d; aborting!\n",hret);
350  return -1;
351  }
352  else if (sret) {
353  verror("failed to set Hg0 defaults: error %d!\n",sret);
354  return -1;
355  }
356 
357  if (gdb_rsp_send_packet(target,"Hc-1",0,gdb_rsp_simple_ack_handler,&sret)) {
358  verror("failed to set Hc-1 defaults!\n");
359  return -1;
360  }
361 
362  if (gdb_rsp_recv_until_handled(target,gdb_rsp_simple_ack_handler,&hret)) {
363  verror("failed to recv Hc-1 response; aborting!\n");
364  return -1;
365  }
366  else if (hret != GDB_RSP_HANDLER_DONE) {
367  verror("simple ack handler returned not done: %d; aborting!\n",hret);
368  return -1;
369  }
370  else if (sret) {
371  verror("failed to set Hc-1 defaults: error %d!\n",sret);
372  return -1;
373  }
374 
375  vdebug(9,LA_TARGET,LF_GDB,"setup default thread for c/g ops\n");
376 
377  /*
378  * This is sadly necessary to do before installing breakpoints on
379  * the QEMU gdb stub; if you don't read regs first, breakpoints will
380  * claim to work but won't.
381  */
382  gdb_rsp_read_regs(target,NULL);
383 
384  return 0;
385 
386  err_setup:
387  gdb_rsp_close(target,0);
388  return -1;
389 }
390 
391 #define GDB_CHECKCONN(errcode) \
392  do { \
393  if (gstate->fd < 0) { \
394  verror("not connected to gdb server!\n"); \
395  errno = ENOTCONN; \
396  return (errcode); \
397  } \
398  } while(0)
399 
400 int gdb_rsp_close(struct target *target,int stay_paused) {
401  struct gdb_state *gstate = (struct gdb_state *)target->state;
402 
403  GDB_CHECKCONN(0);
404 
405  if (stay_paused && gstate->vcont) {
406  if (gdb_rsp_send_packet(target,"vCont;t",0,NULL,NULL)) {
407  verror("failed to detach, STOPPED, via vCont;t!\n");
408  return -1;
409  }
410  }
411  else if (gdb_rsp_send_packet(target,"D",0,NULL,NULL)) {
412  verror("failed to detach via D !\n");
413  return -1;
414  }
415  else {
416  vdebug(9,LA_TARGET,LF_GDB,"detached via D\n");
417  }
418 
419  /*
420  * Don't bother waiting for an ACK nor a simple ACK packet.
421  */
422 
423  if (gstate->ibuf) {
424  free(gstate->ibuf);
425  gstate->ibuf = NULL;
426  }
427  if (gstate->wfd > -1) {
428  close(gstate->wfd);
429  gstate->wfd = -1;
430  }
431  if (gstate->fd > -1) {
432  close(gstate->fd);
433  gstate->fd = -1;
434  }
435  if (gstate->sockfile) {
436  unlink(gstate->sockfile);
437  free(gstate->sockfile);
438  gstate->sockfile = NULL;
439  }
440 
441  return 0;
442 }
443 
444 static int gdb_rsp_send_raw(struct target *target,
445  char *data,unsigned int len) {
446  struct gdb_state *gstate = (struct gdb_state *)target->state;
447  unsigned int remaining;
448  int rc;
449  int fd;
450 
451  GDB_CHECKCONN(-1);
452 
453  if (gstate->writing) {
454  verror("gdb already sending packet; BUG!\n");
455  errno = EALREADY;
456  return -1;
457  }
458 
459  gstate->writing = 1;
460 
461  if (gstate->wfd > -1)
462  fd = gstate->wfd;
463  else
464  fd = gstate->fd;
465 
466  if (fd < 0) {
467  verror("connection not up!\n");
469  return -1;
470  }
471 
472  remaining = len;
473  while (remaining) {
474  rc = write(fd,data + (len - remaining),remaining);
475 
476  if (rc < 0) {
477  if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
478  continue;
479  else if (errno == EPIPE) {
480  /*
481  * XXX: try to reconnect? how to close gracefully?
482  */
483  verror("connection unexpectedly terminated!\n");
485  goto errout;
486  }
487  else {
488  verror("write to fd %d failed after %d of %d bytes: %s (%d)!\n",
489  fd,(len - remaining),len,strerror(errno),errno);
490  goto errout;
491  }
492  }
493  else
494  remaining -= rc;
495  }
496 
497  vdebug(9,LA_TARGET,LF_GDB,"wrote %d bytes to fd %d\n",len,fd);
498 
499  gstate->writing = 0;
500  return 0;
501 
502  errout:
503  gstate->writing = 0;
504  return -1;
505 }
506 
507 static char GDB_INT_BYTE[1] = { 0x03, };
508 static char GDB_ACK_BYTE[1] = { '+', };
509 static char GDB_NAK_BYTE[1] = { '-', };
510 
511 int gdb_rsp_interrupt(struct target *target) {
512  struct gdb_state *gstate = (struct gdb_state *)target->state;
514  int rc;
515 
516  GDB_CHECKCONN(-1);
517 
518  if (gstate->writing) {
520  "gdb already sending packet; scheduling interrupt!\n");
521  gstate->need_interrupt = 1;
522  return 0;
523  }
524 
525  rc = gdb_rsp_send_raw(target,GDB_INT_BYTE,sizeof(GDB_INT_BYTE));
526  if (rc) {
527  verror("could not send interrupt request!\n");
528  return -1;
529  }
530 
531  /*
532  * If there's a handler, we need to wait for it to handle stuff!
533  *
534  * XXX: hm, maybe we should only do this if the handler is the stop
535  * handler... not sure.
536  */
537  if (gstate->handler == gdb_rsp_stop_handler) {
538  if (gdb_rsp_recv_until_handled(target,gdb_rsp_stop_handler,&hret)) {
539  verror("failed to recv stop response; aborting!\n");
540  return -1;
541  }
542  else if (hret != GDB_RSP_HANDLER_DONE) {
543  verror("stop handler error returned not done: %d; aborting!\n",hret);
544  return -1;
545  }
546  }
547 
548  return 0;
549 }
550 
551 /*
552 int gdb_rsp_interrupt_synch(struct target *target) {
553  struct gdb_state *gstate = (struct gdb_state *)target->state;
554 
555  GDB_CHECKCONN(-1);
556 
557  if (gstate->writing) {
558  vdebug(6,LA_TARGET,LF_GDB,
559  "gdb already sending packet; scheduling interrupt!\n");
560  gstate->need_interrupt = 1;
561  return 0;
562  }
563 
564  while (1) {
565  rc = gdb_rsp_send_raw(target,GDB_INT_BYTE,sizeof(GDB_INT_BYTE));
566  if (
567 }
568 */
569 
570 int gdb_rsp_ack(struct target *target) {
571  struct gdb_state *gstate = (struct gdb_state *)target->state;
572 
573  if (!gstate->need_ack)
574  return 0;
575 
576  GDB_CHECKCONN(-1);
577 
578  if (gstate->writing) {
579  verror("gdb already sending packet; BUG!\n");
580  return -1;
581  }
582 
583  return gdb_rsp_send_raw(target,GDB_ACK_BYTE,sizeof(GDB_ACK_BYTE));
584 }
585 
586 int gdb_rsp_nak(struct target *target) {
587  struct gdb_state *gstate = (struct gdb_state *)target->state;
588 
589  GDB_CHECKCONN(-1);
590 
591  if (gstate->writing) {
592  verror("gdb already sending packet; BUG!\n");
593  return -1;
594  }
595 
596  return gdb_rsp_send_raw(target,GDB_NAK_BYTE,sizeof(GDB_NAK_BYTE));
597 }
598 
599 /*
600  * The binary data representation uses 7d (ASCII ‘}’) as an escape
601  * character. Any escaped byte is transmitted as the escape character
602  * followed by the original character XORed with 0x20. For example, the
603  * byte 0x7d would be transmitted as the two bytes 0x7d 0x5d. The bytes
604  * 0x23 (ASCII ‘#’), 0x24 (ASCII ‘$’), and 0x7d (ASCII ‘}’) must always
605  * be escaped.
606  */
607 static int gdb_rsp_encode(struct target *target,char ptype,
608  char *ibuf,unsigned int ilen,
609  char **obuf,unsigned int *olen) {
610  unsigned long int sum = 0;
611  unsigned int alen = ilen + 32;
612  unsigned int i,j;
613  char *lbuf;
614 
615  lbuf = malloc(alen);
616  j = 0;
617  lbuf[j] = ptype;
618  ++j;
619 
620  for (i = 0; i < ilen; ++i,++j) {
621  if ((j + 1) >= alen) {
622  alen += 32;
623  lbuf = realloc(lbuf,alen);
624  }
625 
626  if (ibuf[i] == 0x7d || ibuf[i] == '#' || ibuf[i] == '$') {
627  lbuf[j] = 0x7d;
628  sum = (sum + lbuf[j]) % 256;
629  ++j;
630  lbuf[j] = lbuf[i] ^ 0x20;
631  sum = (sum + lbuf[j]) % 256;
632  }
633  else {
634  lbuf[j] = ibuf[i];
635  sum = (sum + lbuf[j]) % 256;
636  }
637  }
638 
639  if ((j + 4) >= alen)
640  lbuf = realloc(lbuf,j + 4);
641  else if ((j + 4) < alen)
642  lbuf = realloc(lbuf,j + 4);
643 
644  lbuf[j++] = '#';
645  snprintf(lbuf + j,3,"%02hhx",(uint8_t)sum);
646 
647  *obuf = lbuf;
648  *olen = j + 2;
649 
650  return 0;
651 }
652 
653 int gdb_rsp_send_packet(struct target *target,
654  char *data,unsigned int len,
656  struct gdb_state *gstate = (struct gdb_state *)target->state;
657 
658  GDB_CHECKCONN(-1);
659 
660  if (gstate->writing) {
661  verror("gdb already writing; BUG!\n");
662  return -1;
663  }
664 
665  if (len == 0)
666  len = strlen(data);
667 
668  if (gdb_rsp_encode(target,'$',data,len,
669  &gstate->obuf,&gstate->obuf_len)) {
670  verror("failed to encode data of len %d!\n",len);
671  goto errout;
672  }
673 
674  vdebug(9,LA_TARGET,LF_GDB,"encoded %d bytes into '%c' packet of %d bytes\n",
675  len,'$',gstate->obuf_len);
676 
677  if (gdb_rsp_send_raw(target,gstate->obuf,gstate->obuf_len)) {
678  verror("could not send %d encoded bytes (%d non-encoded)!\n",
679  gstate->obuf_len,len);
680  goto errout;
681  }
682 
683  vdebug(9,LA_TARGET,LF_GDB,"wrote %d encoded bytes (%s)\n",
684  gstate->obuf_len,gstate->obuf);
685 
686  if (!gstate->need_ack) {
687  free(gstate->obuf);
688  gstate->obuf = NULL;
689  gstate->obuf_len = 0;
690  }
691 
692  gstate->handler = handler;
693  gstate->handler_data = handler_data;
695 
696  return 0;
697 
698  errout:
699  gstate->writing = 0;
700  return -1;
701 }
702 
703 int gdb_rsp_send_notification(struct target *target,
704  char *data,unsigned int len) {
705  verror("client should not send notifications!");
706  errno = ENOTSUP;
707  return -1;
708 }
709 
710 /*
711  * Return value of 1 means incomplete packet; 0 means we decoded a
712  * packet (and packet type will be in ptype, and decoded data will be in
713  * *obuf and length in *olen, if ptype is GDB_PACKET or GDB_NOTIFICATION);
714  * -1 means there was a decoding error! We *always* set ibuf_used!
715  */
716 static int gdb_rsp_decode(struct target *target,char *ibuf,unsigned int ilen,
717  gdb_ptype_t *ptype,unsigned int *ilen_used,
718  char **obuf,unsigned int *olen) {
719  unsigned long int sum = 0;
720  unsigned int start;
721  unsigned int end;
722  unsigned long decsum;
723  unsigned int alen;
724  uint8_t rlen;
725  int inesc = 0;
726  unsigned int i,j,k;
727  char prevchar;
728 
729  if (ptype)
730  *ptype = GDB_UNKNOWN;
731 
732  /*
733  * First, skip any non-packet leading junk.
734  */
735  i = 0;
736  while (i < ilen) {
737  switch (*(ibuf+i)) {
738  /* Interrupt -- client should not see it. */
739  case 0x03:
740  if (ptype)
741  *ptype = GDB_INTERRUPT;
742  vdebug(9,LA_TARGET,LF_GDB,"interrupt packet\n");
743  if (ilen_used)
744  *ilen_used = i + 1;
745  return 0;
746  /* Simple ACK/NAK */
747  case '+':
748  if (ptype)
749  *ptype = GDB_ACK;
750  vdebug(9,LA_TARGET,LF_GDB,"ACK packet\n");
751  if (ilen_used)
752  *ilen_used = i + 1;
753  return 0;
754  case '-':
755  if (ptype)
756  *ptype = GDB_NAK;
757  vdebug(9,LA_TARGET,LF_GDB,"NAK packet\n");
758  if (ilen_used)
759  *ilen_used = i + 1;
760  return 0;
761  /* Full packet or notification packet. */
762  case '$':
763  if (ptype)
764  *ptype = GDB_PACKET;
765  vdebug(9,LA_TARGET,LF_GDB,"normal packet\n");
766  break;
767  case '%':
768  if (ptype)
769  *ptype = GDB_NOTIFICATION;
770  vdebug(9,LA_TARGET,LF_GDB,"notification packet\n");
771  break;
772  /* Garbage. */
773  default:
774  vdebug(16,LA_TARGET,LF_GDB,"unrecognized leading char %c (%hhx)\n",
775  *(ibuf+i),*(ibuf+i));
776  ++i;
777  continue;
778  }
779 
780  /* Ok, we found something. */
781  break;
782  }
783 
784  /*
785  * If the data was all garbage, error.
786  */
787  if (i >= ilen) {
788  if (ilen_used)
789  *ilen_used = i + 1;
790  errno = EBADMSG;
791  return -1;
792  }
793 
794  /*
795  * Otherwise, we have a full packet ($) or notification (%); look
796  * for a complete packet and checksum.
797  */
798  start = i;
799  /* Skip the $ or %. */
800  ++i;
801  /* Compute the checksum on the data. */
802  for (; i < ilen && ibuf[i] != '#'; ++i)
803  sum = (sum + ibuf[i]) % 256;
804  if (i >= ilen) {
805  if (ilen_used)
806  /* Let it skip any garbage when it comes back again. */
807  *ilen_used = start;
809  "incomplete packet len %d (%d garbage)\n",ilen,start);
810  return 1;
811  }
812  else if ((i + 2) >= ilen) {
813  if (ilen_used)
814  /* Let it skip any garbage when it comes back again. */
815  *ilen_used = start;
817  "incomplete packet '%s' len %d (%d garbage; missing %d checksum bytes)\n",
818  ibuf + start,ilen,start,(i + 3 - ilen));
819  return 1;
820  }
821  else {
822  if (__h2ul(ibuf + i + 1,2,&decsum)) {
823  verror("error computing checksum!\n");
824  if (ilen_used)
825  *ilen_used = i + 1; /* don't include checksum bytes! */
826  return -1;
827  }
828  else if (decsum != sum) {
829  verror("bad checksum: expected 0x%lx, got 0x%lx (%d data bytes)\n",
830  sum,decsum,i - start - 1);
831  if (ilen_used)
832  *ilen_used = i + 1 + 2; /* do include checksum bytes! */
833  return -1;
834  }
835  else {
837  "good checksum: expected 0x%lx, got 0x%lx (%d data bytes)\n",
838  sum,decsum,i - start - 1);
839  if (ilen_used)
840  *ilen_used = i + 1 + 2;
841  /* Continue decoding... */
842  }
843  }
844 
845  /* Move past $ or % */
846  start += 1;
847  /* End points to # */
848  end = i;
849 
850  /*
851  * Decode the data; alloc *obuf; set *olen.
852  */
853  if (obuf && olen) {
854  if ((end - start) == 0) {
855  *obuf = malloc(1);
856  **obuf = '\0';
857  *olen = 0;
858  vdebug(9,LA_TARGET,LF_GDB,"empty data in packet\n");
859  return 0;
860  }
861 
862  alen = ((end - start) / 4096) * 4096;
863  if ((end - start) != 4096)
864  alen += 4096;
865  *obuf = malloc(alen);
866  *olen = 0;
867 
868  for (i = start,j = 0; i < end; ++i,++j) {
869  /* Make sure *obuf is big enough... */
870  if (j >= alen) {
871  alen += 4096;
872  *obuf = realloc(*obuf,alen);
873  }
874 
875  if (inesc) {
876  (*obuf)[j] = ibuf[i + 1] ^ 0x20;
877  ++i;
878  inesc = 0;
879  }
880  else if (ibuf[i] == 0x7d)
881  inesc = 1;
882  else if (ibuf[i] == 0x2a) {
883  /* Make sure *obuf is big enough for the run sequence */
884  rlen = ibuf[i + 1] - 29;
885 
886  if ((j + rlen) >= alen) {
887  alen += 4096;
888  *obuf = realloc(*obuf,alen);
889  }
890 
891  for (k = 0; k < rlen; ++k)
892  (*obuf)[j + 1 + k] = prevchar;
893 
894  j += rlen;
895  }
896  else {
897  prevchar = (*obuf)[j] = ibuf[i];
898  }
899  }
900 
901  /* Realloc to j + 1 (to leave space for a '\0' */
902  *obuf = realloc(*obuf,j + 1);
903  (*obuf)[j] = '\0';
904  /* But the length does not count the '\0'! */
905  *olen = j;
906  }
907 
908  return 0;
909 }
910 
912 gdb_rsp_default_handler(struct target *target,char *data,unsigned int len,
913  void *handler_data) {
914  vdebug(5,LA_TARGET,LF_GDB,"cannot handle '%s' (len %u)\n",data,len);
915 
917 }
918 
919 int gdb_rsp_recv(struct target *target,int blocking,int only_one,
920  gdb_ptype_t *o_ptype) {
921  struct gdb_state *gstate = (struct gdb_state *)target->state;
922  int rc;
923  gdb_ptype_t ptype = GDB_UNKNOWN;
924  char *obuf = NULL;
925  char *new_ibuf;
926  unsigned int new_alen;
927  unsigned int olen = 0;
928  unsigned int ilen_used = 0;
929  int retval;
930 
931  GDB_CHECKCONN(-1);
932 
933  /*
934  * If we had sent a packet, we might get a response to that packet;
935  * we might get a nak; we might receive a new notification; we might
936  * receive the next part of a notification, or its final OK;
937  */
938 
939  /*
940  * Read everything we can; then see if we have a packet, a
941  * notification, a simple ack (OK, Enn), an ack/nak, or an interrupt
942  * (which we shouldn't get cause we're a client, but whatever). If
943  * we don't have a full packet, return 0 so that the caller tries
944  * again when there is more input; we'll continue reading then...
945  */
946  decode:
947  do {
948  ptype = GDB_UNKNOWN;
949  obuf = NULL;
950  olen = 0;
951  new_ibuf = NULL;
952  new_alen = 0;
953  ilen_used = 0;
954 
955  if (gstate->ibuf && gstate->ibuf_len > 0) {
956  /* Try to decode a full msg! */
957  rc = gdb_rsp_decode(target,gstate->ibuf,gstate->ibuf_len,
958  &ptype,&ilen_used,&obuf,&olen);
959  if (rc == 1) {
961  "incomplete packet; not skipping %u processed bytes\n",
962  ilen_used);
963  }
964  else if (rc == -1) {
965  verror("decoding error; skipping %u processed bytes!\n",
966  ilen_used);
967 
968  /* Send an immediate NAK. */
969  gdb_rsp_nak(target);
970 
971  goto errout;
972  }
973  else {
975  "decoded %u-byte message (%s) of type %d (skip %u);"
976  " processing\n",olen,obuf,ptype,ilen_used);
977  break;
978  }
979  }
980 
981  if (gstate->ibuf_alen == gstate->ibuf_len) {
982  gstate->ibuf = realloc(gstate->ibuf,gstate->ibuf_alen + 4096);
983  gstate->ibuf_alen += 4096;
984  }
985 
986  rc = recv(gstate->fd,gstate->ibuf + gstate->ibuf_len,
987  gstate->ibuf_alen - gstate->ibuf_len,
988  (blocking) ? 0 : MSG_DONTWAIT);
989  if (rc == 0) {
990  vwarn("server disconnected unexpectedly!\n");
991  close(gstate->fd);
992  gstate->fd = -1;
993  return -1;
994  }
995  else if (rc < 0) {
996  if (errno == EAGAIN || errno == EWOULDBLOCK)
997  return 0;
998  else if (errno == EINTR) {
999  return 0;
1000  }
1001  else {
1002  verror("recv: %s (%d)\n",strerror(errno),errno);
1003  /* XXX: should we clear the ibuf buffer? */
1004  return -1;
1005  }
1006  }
1007  else {
1008  gstate->ibuf_len += rc;
1009  }
1010  } while (1);
1011 
1012  if (ptype == GDB_UNKNOWN) {
1013  verror("BUG! unknown message type!\n");
1014  goto errout;
1015  }
1016  else if (ptype == GDB_ACK) {
1018  "ACK for %d bytes sent; clearing\n",gstate->obuf_len);
1019  if (gstate->obuf) {
1020  free(gstate->obuf);
1021  gstate->obuf = NULL;
1022  }
1023  gstate->obuf_len = 0;
1024  }
1025  else if (ptype == GDB_NAK) {
1027  "NAK for %d bytes sent; retrying\n",gstate->obuf_len);
1028 
1029  if (gdb_rsp_send_raw(target,gstate->obuf,gstate->obuf_len)) {
1030  verror("could not send %d encoded bytes that already failed!\n",
1031  gstate->obuf_len);
1032  goto errout;
1033  }
1034 
1036  "retransmitted %d encoded bytes\n",gstate->obuf_len);
1037 
1038  if (!gstate->need_ack) {
1039  free(gstate->obuf);
1040  gstate->obuf = NULL;
1041  gstate->obuf_len = 0;
1042  }
1043  }
1044  else if (ptype == GDB_NOTIFICATION) {
1045  // XXX finish
1046  verror("BUG! cannot handle notifications yet!\n");
1047  goto errout;
1048  }
1049  else if (ptype == GDB_PACKET) {
1050  /* Send an immediate ack */
1051  gdb_rsp_ack(target);
1052 
1053  if (gstate->handler) {
1054  rc = gstate->handler(target,obuf,olen,gstate->handler_data);
1055  if (rc == GDB_RSP_HANDLER_ERR) {
1056  verror("handler failed on packet '%s' (len %u); removing handler"
1057  " and trying default handler!\n",
1058  obuf,olen);
1059  gstate->handler = NULL;
1060  gstate->handler_ret = rc;
1061  goto default_handler;
1062  }
1063  else if (rc == GDB_RSP_HANDLER_DONE) {
1064  vdebug(8,LA_TARGET,LF_GDB,"handler handled '%s' (len %u)\n",
1065  obuf,olen);
1066  gstate->handler = NULL;
1067  gstate->handler_ret = rc;
1068  }
1069  else if (rc == GDB_RSP_HANDLER_MORE) {
1071  "handler handled '%s' (len %u) but expects more packets\n",
1072  obuf,olen);
1073  gstate->handler_ret = rc;
1074  }
1075  else if (rc == GDB_RSP_HANDLER_NOTMINE) {
1077  "handler does not own '%s' (len %u);"
1078  " trying default handler\n",obuf,olen);
1079  gstate->handler_ret = rc;
1080  goto default_handler;
1081  }
1082  else {
1083  vwarn("bad return code %d from handler; trying default"
1084  " handler!\n",rc);
1085  gstate->handler_ret = GDB_RSP_HANDLER_ERR;
1086  goto default_handler;
1087  }
1088  }
1089  else {
1090  default_handler:
1091  rc = gdb_rsp_default_handler(target,obuf,olen,NULL);
1092  if (rc == GDB_RSP_HANDLER_ERR) {
1093  verror("default handler failed on packet '%s' (len %u)!\n",
1094  obuf,olen);
1095  goto errout;
1096  }
1097  else if (rc == GDB_RSP_HANDLER_DONE) {
1099  "default handler handled '%s' (len %u)\n",obuf,olen);
1100  gstate->handler = NULL;
1101  }
1102  else if (rc == GDB_RSP_HANDLER_MORE) {
1104  "default handler handled '%s' (len %u) but expects more"
1105  " packets\n",obuf,olen);
1106  }
1107  else if (rc == GDB_RSP_HANDLER_NOTMINE) {
1109  "default handler does not own '%s' (len %u);"
1110  " skipping this message!\n",obuf,olen);
1111  }
1112  }
1113  }
1114  else {
1115  verror("invalid packet type %d!\n",ptype);
1116  errno = EBADMSG;
1117  goto errout;
1118  }
1119 
1120  /* Return successfully. */
1121  retval = 0;
1122 
1123  out:
1124  if (ilen_used >= gstate->ibuf_len) {
1125  if (gstate->ibuf_alen != 4096) {
1126  gstate->ibuf = realloc(gstate->ibuf,4096);
1127  gstate->ibuf_alen = 4096;
1128 
1129  vdebug(12,LA_TARGET,LF_GDB,
1130  "used all %u bytes; reset input buf to 4096\n",ilen_used);
1131  }
1132  else {
1133  vdebug(12,LA_TARGET,LF_GDB,"used all %u bytes\n",ilen_used);
1134  }
1135 
1136  gstate->ibuf_len = 0;
1137  }
1138  else if (ilen_used > 0) {
1139  new_alen = (gstate->ibuf_len - ilen_used) / 4096;
1140  if ((gstate->ibuf_len - ilen_used) != 4096)
1141  new_alen += 4096;
1142  new_ibuf = malloc(new_alen);
1143  memcpy(new_ibuf,gstate->ibuf + ilen_used,gstate->ibuf_len - ilen_used);
1144  free(gstate->ibuf);
1145  gstate->ibuf = new_ibuf;
1146  gstate->ibuf_alen = new_alen;
1147  gstate->ibuf_len -= ilen_used;
1148 
1149  vdebug(12,LA_TARGET,LF_GDB,
1150  "shrunk input buf to %u by %u bytes; current used %u\n",
1151  new_alen,ilen_used,gstate->ibuf_len);
1152  }
1153 
1154  if (obuf) {
1155  free(obuf);
1156  obuf = NULL;
1157  }
1158 
1159  if (o_ptype)
1160  *o_ptype = ptype;
1161 
1162  /*
1163  * If we processed some input, but there is still more, keep processing!
1164  */
1165  if (!only_one && retval == 0 && gstate->ibuf_len > 0 && ilen_used > 0) {
1166  vdebug(12,LA_TARGET,LF_GDB,"continuing to process %u remaining bytes!\n",
1167  gstate->ibuf_len);
1168  fflush(stderr);
1169  goto decode;
1170  }
1171  else
1172  return retval;
1173 
1174  errout:
1175  retval = -1;
1176  goto out;
1177 }
1178 
1179 int gdb_rsp_recv_until_handled(struct target *target,
1182  struct gdb_state *gstate = (struct gdb_state *)target->state;
1183  int rc;
1184 
1185  while (gstate->handler == handler) {
1186  rc = gdb_rsp_recv(target,1,0,NULL);
1187  if (rc) {
1188  verror("failed to recv response; aborting!\n");
1189  return -1;
1190  }
1191  }
1192 
1193  if (handler_ret)
1194  *handler_ret = gstate->handler_ret;
1195 
1196  return 0;
1197 }
1198 
1199 int gdb_rsp_recv_until_acked(struct target *target) {
1200  int rc;
1201  gdb_ptype_t ptype;
1202 
1203  while (1) {
1204  rc = gdb_rsp_recv(target,1,1,&ptype);
1205  if (rc) {
1206  verror("failed to recv response; aborting!\n");
1207  return -1;
1208  }
1209  else if (ptype != GDB_ACK) {
1210  vdebug(12,LA_TARGET,LF_GDB,"received ptype %d, not ACK!\n",ptype);
1211  }
1212  else {
1213  break;
1214  }
1215  }
1216 
1217  return 0;
1218 }
1219 
1224 static gdb_rsp_handler_ret_t
1225 gdb_rsp_simple_ack_handler(struct target *target,char *data,unsigned int len,
1226  void *handler_data) {
1227  char *next;
1228  int *ret = (int *)handler_data;
1229 
1230  if (len == 0) {
1231  if (ret)
1232  *ret = -1;
1233  vwarnopt(9,LA_TARGET,LF_GDB,"empty reply -- not supported?\n");
1234  return GDB_RSP_HANDLER_DONE;
1235  }
1236 
1237  next = data;
1238  if (strncmp(next,"OK",2) == 0) {
1239  if (ret)
1240  *ret = 0;
1241  return GDB_RSP_HANDLER_DONE;
1242  }
1243  else if (*next == 'E') {
1244  if (ret)
1245  *ret = atoi(next+1);
1246  return GDB_RSP_HANDLER_DONE;
1247  }
1248  else {
1249  vwarnopt(12,LA_TARGET,LF_GDB,"unexpected simple ack '%s'\n",data);
1250  return GDB_RSP_HANDLER_NOTMINE;
1251  }
1252 }
1253 
1254 static gdb_rsp_handler_ret_t
1255 gdb_rsp_features_handler(struct target *target,char *data,unsigned int len,
1256  void *handler_data) {
1257  struct gdb_state *gstate = (struct gdb_state *)target->state;
1258  char *end = data + len;
1259  char *cur;
1260  char *next;
1261  char *value;
1262  unsigned int i;
1263 
1264  if (len == 0) {
1265  vdebug(9,LA_TARGET,LF_GDB,"no features (empty reply)\n");
1266  return GDB_RSP_HANDLER_DONE;
1267  }
1268 
1269  next = data;
1270  while (next < end) {
1271  /* Get rid of any spaces... */
1272  i = 0;
1273  while (next < end) {
1274  if (*next == ' ')
1275  ++next;
1276  else
1277  break;
1278  }
1279  cur = next;
1280  /* Find the next separator */
1281  while (next < end) {
1282  if (*next == ';') {
1283  *next = '\0';
1284  ++next;
1285  break;
1286  }
1287  else
1288  ++next;
1289  }
1290  /* Get rid of any spaces... */
1291  i = 2;
1292  while ((next - i) >= cur && *(next - i) == ' ') {
1293  *(next - i) = '\0';
1294  ++i;
1295  }
1296  /* Parse the token. */
1297  value = cur;
1298  while (*value) {
1299  if (*value == '=') {
1300  *value = '\0';
1301  ++value;
1302  break;
1303  }
1304  else if (*value == '+') {
1305  *value = '\0';
1306  value = "+";
1307  break;
1308  }
1309  else if (*value == '-') {
1310  *value = '\0';
1311  value = "-";
1312  break;
1313  }
1314  else if (*value == '?') {
1315  *value = '\0';
1316  value = "?";
1317  break;
1318  }
1319  else
1320  ++value;
1321  }
1322  /* Record the feature. */
1323  g_hash_table_insert(gstate->stubfeatures,strdup(cur),strdup(value));
1324  vdebug(5,LA_TARGET,LF_GDB,"stub feature '%s' -> '%s'\n",cur,value);
1325  }
1326 
1327  /* Not expecting anything more. */
1328  return GDB_RSP_HANDLER_DONE;
1329 }
1330 
1331 static gdb_rsp_handler_ret_t
1332 gdb_rsp_vcont_check_handler(struct target *target,char *data,unsigned int len,
1333  void *handler_data) {
1334  struct gdb_state *gstate = (struct gdb_state *)target->state;
1335  char *end = data + len;
1336  char *next;
1337 
1338  if (len == 0) {
1339  vdebug(9,LA_TARGET,LF_GDB,"vCont not supported (empty reply)\n");
1340  return GDB_RSP_HANDLER_DONE;
1341  }
1342 
1343  next = data;
1344 
1345  while (next < end && *next == ' ')
1346  ++next;
1347 
1348  if (strncmp(next,"vCont",5) != 0) {
1350  "response '%s' (len %d) not for us!\n",data,len);
1351  return GDB_RSP_HANDLER_NOTMINE;
1352  }
1353  else {
1354  gstate->vcont = 1;
1355  next += 5;
1356  }
1357 
1358  vdebug(5,LA_TARGET,LF_GDB,"checking for vCont actions\n");
1359 
1360  next = data;
1361  while (next < end) {
1362  switch (*next) {
1363  case 'c':
1364  gstate->vcont_c = 1;
1365  vdebug(5,LA_TARGET,LF_GDB,"vCont 'c'\n");
1366  break;
1367  case 'C':
1368  gstate->vcont_C = 1;
1369  vdebug(5,LA_TARGET,LF_GDB,"vCont 'C'\n");
1370  break;
1371  case 's':
1372  gstate->vcont_s = 1;
1373  vdebug(5,LA_TARGET,LF_GDB,"vCont 's'\n");
1374  break;
1375  case 'S':
1376  gstate->vcont_S = 1;
1377  vdebug(5,LA_TARGET,LF_GDB,"vCont 'S'\n");
1378  break;
1379  case 't':
1380  gstate->vcont_t = 1;
1381  vdebug(5,LA_TARGET,LF_GDB,"vCont 't'\n");
1382  break;
1383  case 'r':
1384  gstate->vcont_r = 1;
1385  vdebug(5,LA_TARGET,LF_GDB,"vCont 'r'\n");
1386  break;
1387  default:
1388  break;
1389  }
1390 
1391  ++next;
1392  }
1393 
1394  /* Not expecting anything more. */
1395  return GDB_RSP_HANDLER_DONE;
1396 }
1397 
1398 /*
1399  * From the docs:
1400  *
1401  * Several packets and replies include a thread-id field to identify a
1402  * thread. Normally these are positive numbers with a target-specific
1403  * interpretation, formatted as big-endian hex strings. A thread-id can
1404  * also be a literal ‘-1’ to indicate all threads, or ‘0’ to pick any
1405  * thread.
1406  *
1407  * In addition, the remote protocol supports a multiprocess feature in
1408  * which the thread-id syntax is extended to optionally include both
1409  * process and thread ID fields, as ‘ppid.tid’. The pid (process) and
1410  * tid (thread) components each have the format described above: a
1411  * positive number with target-specific interpretation formatted as a
1412  * big-endian hex string, literal ‘-1’ to indicate all processes or
1413  * threads (respectively), or ‘0’ to indicate an arbitrary process or
1414  * thread. Specifying just a process, as ‘ppid’, is equivalent to
1415  * ‘ppid.-1’. It is an error to specify all processes but a specific
1416  * thread, such as ‘p-1.tid’. Note that the ‘p’ prefix is not used for
1417  * those packets and replies explicitly documented to include a process
1418  * ID, rather than a thread-id.
1419  *
1420  * The multiprocess thread-id syntax extensions are only used if both
1421  * GDB and the stub report support for the ‘multiprocess’ feature using
1422  * ‘qSupported’.
1423  */
1424 
1425 static gdb_rsp_handler_ret_t
1426 gdb_rsp_stop_handler(struct target *target,char *data,unsigned int len,
1427  void *handler_data) {
1428  struct gdb_state *gstate = (struct gdb_state *)target->state;
1429  char *end = data + len;
1430  char *next;
1431  char command;
1432  struct gdb_rsp_stop_status *ss = &gstate->last_stop_status;
1433  char *colon,*semicolon,*dot;
1434  unsigned int xlen;
1435 
1436  next = data;
1437 
1438  while (next < end && *next == ' ') ++next;
1439 
1440  command = *next;
1441  ++next;
1442  switch (command) {
1443  case 'S':
1444  vdebug(9,LA_TARGET,LF_GDB,"stop response 'S'\n");
1445  ss->reason = GDB_RSP_STOP_SIGNAL;
1446  while (next < end && (*next == ' ' || *next == ';')) ++next;
1447  ss->signal = 0;
1448  __h2ul(next,((end - next) > 2) ? 2 : (end - next),&ss->signal);
1449  next += 2;
1450  break;
1451  case 'T':
1452  vdebug(9,LA_TARGET,LF_GDB,"stop response 'T'\n");
1453  ss->reason = GDB_RSP_STOP_SIGNAL;
1454  while (next < end && (*next == ' ' || *next == ';')) ++next;
1455  ss->signal = 0;
1456  xlen = ((end - next) > 2) ? 2 : (end - next);
1457  __h2ul(next,xlen,&ss->signal);
1458  next += xlen;
1459  while (next < end && (*next == ' ' || *next == ';')) ++next;
1460  while (next < end) {
1461  /*
1462  * Parse n1:r1;n2:r2;... string.
1463  */
1464  colon = index(next,':');
1465  if (!colon)
1466  break;
1467  semicolon = index(next,';');
1468  if (!semicolon)
1469  semicolon = end;
1470 
1471  *colon = '\0';
1472  ++colon; /* The current rN value */
1473  *semicolon = '\0';
1474 
1475  if (strncmp(next,"thread",6) == 0) {
1476  dot = index(colon,'.');
1477  if (*colon == 'p' && dot) {
1478  ++colon;
1479  *dot = '\0';
1480  ss->has_pid = 1;
1481  ss->has_tid = 1;
1482  if (strcmp(colon,"-1") == 0)
1483  ss->pid = -1;
1484  else if (*colon == '0' && *(colon + 1) == '\0')
1485  ss->pid = 0;
1486  else {
1487  __h2ul_be(colon,(dot - colon),(unsigned long *)&ss->pid);
1488  }
1489  if (strncmp(dot + 1,"-1",2) == 0)
1490  ss->tid = -1;
1491  else if (*(dot + 1) == '0')
1492  ss->tid = 0;
1493  else {
1494  __h2ul_be(colon,(semicolon - (dot + 1)),
1495  (unsigned long *)&ss->tid);
1496  }
1497  }
1498  else {
1499  ss->has_tid = 1;
1500  if (strcmp(colon,"-1") == 0)
1501  ss->tid = -1;
1502  else if (*colon == '0' && *(colon + 1) == '\0')
1503  ss->tid = 0;
1504  else {
1505  __h2ul_be(colon,(dot - colon),(unsigned long *)&ss->tid);
1506  }
1507  }
1508  }
1509  else if (strncmp(next,"core",4) == 0) {
1510  ss->has_core = 1;
1511  ss->core = 0;
1512  __h2ul(colon,semicolon - colon,&ss->core);
1513  }
1514  else if (strncmp(next,"watch",5) == 0) {
1515  ss->reason = GDB_RSP_STOP_WATCH;
1516  ss->addr = 0;
1517  __h2ul(colon,semicolon - colon,&ss->addr);
1518  }
1519  else if (strncmp(next,"rwatch",6) == 0) {
1520  ss->reason = GDB_RSP_STOP_RWATCH;
1521  ss->addr = 0;
1522  __h2ul(colon,semicolon - colon,&ss->addr);
1523  }
1524  else if (strncmp(next,"awatch",6) == 0) {
1525  ss->reason = GDB_RSP_STOP_AWATCH;
1526  ss->addr = 0;
1527  __h2ul(colon,semicolon - colon,&ss->addr);
1528  }
1529  else if (strncmp(next,"library",7) == 0) {
1530  ss->reason = GDB_RSP_STOP_LIBRARY;
1531  }
1532  else if (strncmp(next,"replaylog",9) == 0) {
1533  ss->reason = GDB_RSP_STOP_LIBRARY;
1534  }
1535  else if (isdigit(*next) || (*next >= 'a' && *next <= 'f')
1536  || (*next >= 'f' && *next <= 'F')) {
1538  "skipping register value %s:%s\n",next,colon);
1539  }
1540  else {
1542  "skipping unknown reason:value %s:%s\n",next,colon);
1543  }
1544 
1545  ++semicolon; /* next value for "next" */
1546  next = semicolon;
1547  }
1548  break;
1549  case 'W':
1550  vdebug(9,LA_TARGET,LF_GDB,"stop response 'W'\n");
1551  ss->reason = GDB_RSP_STOP_EXITED;
1552  while (next < end && (*next == ' ' || *next == ';')) ++next;
1553  ss->signal = 0;
1554  xlen = ((end - next) > 2) ? 2 : (end - next);
1555  __h2ul(next,xlen,&ss->signal);
1556  next += xlen;
1557  while (next < end && (*next == ' ' || *next == ';')) ++next;
1558  if (strncmp(next,"process:",8) == 0) {
1559  next += 8;
1560  ss->has_pid = 1;
1561  ss->pid = 0;
1562  __h2ul_be(next,end - next,(unsigned long *)&ss->pid);
1563  }
1564  break;
1565  case 'X':
1566  vdebug(9,LA_TARGET,LF_GDB,"stop response 'X'\n");
1567  ss->reason = GDB_RSP_STOP_TERMINATED;
1568  while (next < end && (*next == ' ' || *next == ';')) ++next;
1569  ss->signal = 0;
1570  xlen = ((end - next) > 2) ? 2 : (end - next);
1571  __h2ul(next,xlen,&ss->signal);
1572  next += xlen;
1573  while (next < end && (*next == ' ' || *next == ';')) ++next;
1574  if (strncmp(next,"process:",8) == 0) {
1575  next += 8;
1576  ss->has_pid = 1;
1577  ss->pid = 0;
1578  __h2ul_be(next,end - next,(unsigned long *)&ss->pid);
1579  }
1580  break;
1581  case 'O':
1582  vdebug(9,LA_TARGET,LF_GDB,"stop response 'O'\n");
1583  vwarn("unsupported console output stop \"status\"\n");
1584  break;
1585  case 'F':
1586  vdebug(9,LA_TARGET,LF_GDB,"stop response 'F'\n");
1587  vwarn("unsupported file i/o extension; ignoring\n");
1588  break;
1589  default:
1591  "response '%s' (len %d) not for us!\n",data,len);
1592  return GDB_RSP_HANDLER_NOTMINE;
1593  }
1594 
1595  switch (ss->reason) {
1596  case GDB_RSP_STOP_UNKNOWN:
1597  case GDB_RSP_STOP_NONE:
1599  break;
1600  case GDB_RSP_STOP_SIGNAL:
1601  case GDB_RSP_STOP_WATCH:
1602  case GDB_RSP_STOP_RWATCH:
1603  case GDB_RSP_STOP_AWATCH:
1604  case GDB_RSP_STOP_LIBRARY:
1607  break;
1608  case GDB_RSP_STOP_EXITED:
1610  break;
1613  break;
1614  default:
1616  break;
1617  }
1618 
1619  gstate->rsp_status_valid = 1;
1620 
1621  /* Not expecting anything more. */
1622  return GDB_RSP_HANDLER_DONE;
1623 }
1624 
1625 /*
1626  * QEMU's stub is a bit funny!
1627  *
1628  * XXX: we only do the GP regs, IP, flags, and selectors. Who cares
1629  * about mmx and FP regs... not system programmers!
1630  */
1631 static unsigned int qemu_reg_count_64 = 24;
1632 static unsigned int qemu_offset_to_reg_64[24] = {
1633  0,3,2,1,4,5,6,7,8,9,10,11,12,13,14,15,16,51,52,53,50,54,55,
1634 };
1635 static int qemu_arch_reg_to_reg_64[ARCH_X86_64_REG_COUNT] = {
1636  0,3,2,1,4,5,6,7,8,9,10,11,12,13,14,15,16,
1637  -1,-1,-1,-1,-1,-1,-1,-1,
1638  -1,-1,-1,-1,-1,-1,-1,-1,
1639  -1,-1,-1,-1,-1,-1,-1,-1,
1640  -1,-1,-1,-1,-1,-1,-1,-1,
1641  -1,17,18,19,20,21,22,
1642  -1,-1,
1643  -1,-1,-1,-1,
1644  -1,-1,-1,-1,-1,-1,
1645  -1,-1,
1646  -1,-1,-1,-1,-1,-1,-1,-1,-1,
1647  -1,
1648  -1,-1,-1,-1,-1,-1,-1,-1,
1649  -1,
1650 };
1651 static unsigned int qemu_reg_sizes_64[24] = {
1652  8,8,8,8,8,8,8,8,8,8,8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4,
1653 };
1654 static unsigned int qemu_reg_count_32 = 16;
1655 static unsigned int qemu_offset_to_reg_32[16] = {
1656  0,3,2,1,4,5,6,7,8,9, 41,42,43,40,44,45,
1657 };
1658 static int qemu_arch_reg_to_reg_32[ARCH_X86_REG_COUNT] = {
1659  0,3,2,1,4,5,6,7,8,9,
1660  -1,
1661  -1,-1,-1,-1,-1,-1,-1,-1,
1662  -1,-1,
1663  -1,-1,-1,-1,-1,-1,-1,-1,
1664  -1,-1,-1,-1,-1,-1,-1,-1,
1665  -1,-1,-1,
1666  41,42,43,40,44,45,
1667  -1,-1,-1,-1,-1,
1668  -1,-1,-1,-1,-1,-1,-1,-1,
1669  -1,
1670 };
1671 static unsigned int qemu_reg_sizes_32[16] = {
1672  4,4,4,4,4,4,4,4,4,4, 4, 4, 4, 4, 4, 4,
1673 };
1674 
1675 static gdb_rsp_handler_ret_t
1676 gdb_rsp_regs_handler(struct target *target,char *data,unsigned int len,
1677  void *handler_data) {
1678  struct gdb_spec *gspec = (struct gdb_spec *)target->spec->backend_spec;
1679  char *end = data + len;
1680  char *next;
1681  struct regcache *regcache = (struct regcache *)handler_data;
1682  unsigned int i;
1683  REGVAL regval = 0;
1684  unsigned int *offset_to_reg = NULL;
1685  unsigned int *reg_sizes = NULL;
1686  unsigned int reg_count = 0;
1687 
1688  if (gspec->is_qemu) {
1689  if (target->arch->type == ARCH_X86_64) {
1690  offset_to_reg = qemu_offset_to_reg_64;
1691  reg_sizes = qemu_reg_sizes_64;
1692  reg_count = qemu_reg_count_64;
1693  }
1694  else {
1695  offset_to_reg = qemu_offset_to_reg_32;
1696  reg_sizes = qemu_reg_sizes_32;
1697  reg_count = qemu_reg_count_32;
1698  }
1699  }
1700 
1701  if (regcache) {
1702  next = data;
1703  i = 0;
1704  while (next < end) {
1705  regval = 0;
1706 
1707  /*
1708  * If we have a special register mapping hack, use it;
1709  * otherwise just go in order by target wordsize. This
1710  * default will almost certainly always be wrong, but we
1711  * have to do something...
1712  */
1713  if (reg_count > 0) {
1714  if (i >= reg_count)
1715  break;
1716  __hs2ul_lsb(next,reg_sizes[i] * 2,&regval);
1717  regcache_init_reg(regcache,offset_to_reg[i],regval);
1718  next += reg_sizes[i] * 2;
1719  }
1720  else {
1721  __hs2ul_lsb(next,sizeof(regval)*2,&regval);
1722  regcache_init_reg(regcache,i,regval);
1723  next += sizeof(regval) * 2;
1724  }
1725 
1726  ++i;
1727  }
1728  }
1729 
1730  /* Not expecting anything more. */
1731  return GDB_RSP_HANDLER_DONE;
1732 }
1733 
1734 static gdb_rsp_handler_ret_t
1735 gdb_rsp_read_mem_handler(struct target *target,char *data,unsigned int len,
1736  void *handler_data) {
1737  struct gdb_rsp_read_mem_data *d = \
1738  (struct gdb_rsp_read_mem_data *)handler_data;
1739  unsigned int dlen = len;
1740 
1741  if (*data == 'E') {
1742  if (d)
1743  d->error = atoi(data+1);
1744  return GDB_RSP_HANDLER_ERR;
1745  }
1746 
1747  if (!d->buf) {
1748  d->length = len / 2 + (len % 2);
1749  d->buf = malloc(d->length + 1);
1750  d->buf[d->length] = '\0';
1751  }
1752  else if (d->length < (len / 2 + (len % 2))) {
1754  "not enough space in supplied buffer for decoded response;"
1755  " filling what we can!\n");
1756  dlen = d->length * 2;
1757  /*
1758  errno = ENOMEM;
1759  return GDB_RSP_HANDLER_ERR;
1760  */
1761  }
1762 
1763  __hs2d(data,dlen,d->buf);
1764 
1765  /* Not expecting anything more. */
1766  return GDB_RSP_HANDLER_DONE;
1767 }
1768 
1773 /*
1774  * RSP commands from the client to the server are textual strings,
1775  * optionally followed by arguments. Each command is sent in its own
1776  * packet. The packets fall into four groups:
1777  *
1778  * Packets requiring no acknowledgment. These commands are: f, i, I, k,
1779  * R, t and vFlashDone.
1780  *
1781  * Packets requiring a simple acknowledgment packet. The
1782  * acknowledgment is either OK, Enn (where nn is an error number) or
1783  * for some commands an empty packet (meaning "unsupported"). These
1784  * commands are: !, A, D, G, H, M, P, Qxxxx, T, vFlashErase,
1785  * vFlashWrite, X, z and Z.
1786  *
1787  * Packets that return result data or an error code.. These commands
1788  * are: ?, c, C, g, m, p, qxxxx, s, S and most vxxxx.
1789  *
1790  * Deprecated packets which should no longer be used. These commands
1791  * are b, B, d and r.
1792  */
1793 
1794 target_status_t gdb_rsp_load_status(struct target *target) {
1795  struct gdb_state *gstate = (struct gdb_state *)target->state;
1796  gdb_rsp_handler_ret_t hret;
1797 
1798  if (gstate->rsp_status_valid)
1799  return target->status;
1800 
1801  if (gdb_rsp_send_packet(target,"?",0,gdb_rsp_stop_handler,NULL)) {
1802  verror("failed to check status via ? !\n");
1803  return -1;
1804  }
1805  else {
1806  vdebug(9,LA_TARGET,LF_GDB,"queried status via ?\n");
1807  }
1808 
1809  if (gdb_rsp_recv_until_handled(target,gdb_rsp_stop_handler,&hret)) {
1810  verror("failed to recv stop response; aborting!\n");
1811  return -1;
1812  }
1813  else if (hret != GDB_RSP_HANDLER_DONE) {
1814  verror("stop handler error returned not done: %d; aborting!\n",hret);
1815  return -1;
1816  }
1817 
1818  return target->status;
1819 }
1820 
1821 int gdb_rsp_pause(struct target *target) {
1822  /*
1823  * XXX: interrupts are not ACK'd, but they seem to be the only way
1824  * to pause a target. Probably we should query with ? to get a stop
1825  * reply status...
1826  */
1827  if (gdb_rsp_interrupt(target)) {
1828  return -1;
1829  }
1830 
1831  return 0;
1832 }
1833 
1834 int gdb_rsp_resume(struct target *target) {
1835  struct gdb_state *gstate = (struct gdb_state *)target->state;
1836  //gdb_rsp_handler_ret_t hret;
1837  // struct gdb_rsp_stop_status ss;
1838 
1839  //memset(&ss,0,sizeof(ss));
1840 
1841  if (gstate->vcont && gstate->vcont_c) {
1842  if (gdb_rsp_send_packet(target,"vCont;c",0,gdb_rsp_stop_handler,
1843  &gstate->last_stop_status)) {
1844  verror("failed to resume via vCont;c!\n");
1845  return -1;
1846  }
1847  else {
1848  vdebug(9,LA_TARGET,LF_GDB,"resumed via vCont;c\n");
1849  }
1850  }
1851  else {
1852  if (gdb_rsp_send_packet(target,"c",0,gdb_rsp_stop_handler,
1853  &gstate->last_stop_status)) {
1854  verror("failed to resume via c!\n");
1855  return -1;
1856  }
1857  else {
1858  vdebug(9,LA_TARGET,LF_GDB,"resumed via c\n");
1859  }
1860  }
1861 
1862  /*
1863  * XXX: shoot, we can't wait for the vCont response to be handled!
1864  * We only want to wait for an ACK.
1865  */
1866  if (gdb_rsp_recv_until_acked(target)) {
1867  verror("failed to recv ACK; aborting!\n");
1868  return -1;
1869  }
1870 
1871  /*
1872  if (gdb_rsp_recv_until_handled(target,gdb_rsp_stop_handler,&hret)) {
1873  verror("failed to recv stop response; aborting!\n");
1874  return -1;
1875  }
1876  else if (hret != GDB_RSP_HANDLER_DONE) {
1877  verror("stop handler error returned not done: %d; aborting!\n",hret);
1878  return -1;
1879  }
1880 
1881  gstate->last_stop_status = ss;
1882  */
1883 
1884  return 0;
1885 }
1886 
1887 int gdb_rsp_step(struct target *target) {
1888  struct gdb_state *gstate = (struct gdb_state *)target->state;
1889 
1890  if (gstate->vcont && gstate->vcont_s) {
1891  if (gdb_rsp_send_packet(target,"vCont;s",0,gdb_rsp_stop_handler,
1892  &gstate->last_stop_status)) {
1893  verror("failed to resume via vCont;s!\n");
1894  return -1;
1895  }
1896  else {
1897  vdebug(9,LA_TARGET,LF_GDB,"resumed via vCont;s\n");
1898  }
1899  }
1900  else {
1901  if (gdb_rsp_send_packet(target,"s",0,gdb_rsp_stop_handler,
1902  &gstate->last_stop_status)) {
1903  verror("failed to resume via s!\n");
1904  return -1;
1905  }
1906  else {
1907  vdebug(9,LA_TARGET,LF_GDB,"resumed via s\n");
1908  }
1909  }
1910 
1911  if (gdb_rsp_recv_until_acked(target)) {
1912  verror("failed to recv ACK; aborting!\n");
1913  return -1;
1914  }
1915 
1916  return 0;
1917 }
1918 
1919 int gdb_rsp_query_stub(struct target *target) {
1921 
1922  if (gdb_rsp_send_packet(target,
1923  "qSupported:multiprocess+;qRelocInsn-;xmlRegisters-",
1924  0,gdb_rsp_features_handler,NULL)) {
1925  verror("could not send features via qSupported!\n");
1926  return -1;
1927  }
1928 
1929  if (gdb_rsp_recv_until_handled(target,gdb_rsp_features_handler,&hret)) {
1930  verror("failed to recv qSupported response; aborting!\n");
1931  return -1;
1932  }
1933  else if (hret != GDB_RSP_HANDLER_DONE) {
1934  verror("features handler error returned not done: %d; aborting!\n",hret);
1935  return -1;
1936  }
1937 
1938  if (gdb_rsp_send_packet(target,"vCont?",0,gdb_rsp_vcont_check_handler,NULL)) {
1939  verror("could not check vCont features via vCont?!\n");
1940  return -1;
1941  }
1942 
1943  if (gdb_rsp_recv_until_handled(target,gdb_rsp_vcont_check_handler,&hret)) {
1944  verror("failed to recv vCont? response; aborting!\n");
1945  return -1;
1946  }
1947  else if (hret != GDB_RSP_HANDLER_DONE) {
1948  verror("vCont? handler error returned not done: %d; aborting!\n",hret);
1949  return -1;
1950  }
1951 
1952  return 0;
1953 }
1954 
1955 int gdb_rsp_read_regs(struct target *target,struct regcache *regcache) {
1957 
1958  if (gdb_rsp_send_packet(target,"g",0,gdb_rsp_regs_handler,regcache)) {
1959  verror("could not read regs!\n");
1960  return -1;
1961  }
1962 
1963  if (gdb_rsp_recv_until_handled(target,gdb_rsp_regs_handler,&hret)) {
1964  verror("failed to recv read regs response; aborting!\n");
1965  return -1;
1966  }
1967  else if (hret != GDB_RSP_HANDLER_DONE) {
1968  verror("regs handler error returned not done: %d; aborting!\n",hret);
1969  return -1;
1970  }
1971 
1972  return 0;
1973 }
1974 
1975 int gdb_rsp_write_regs(struct target *target,struct regcache *regcache) {
1976  struct gdb_spec *gspec = (struct gdb_spec *)target->spec->backend_spec;
1978  REGVAL regval = 0;
1979  char *buf;
1980  unsigned int len;
1981  int sret = 0;
1982  int rc;
1983  char *next;
1984  unsigned int i;
1985  unsigned int *offset_to_reg = NULL;
1986  unsigned int *reg_sizes = NULL;
1987  unsigned int reg_count = 0;
1988 
1989  if (gspec->is_qemu) {
1990  if (target->arch->type == ARCH_X86_64) {
1991  offset_to_reg = qemu_offset_to_reg_64;
1992  reg_sizes = qemu_reg_sizes_64;
1993  reg_count = qemu_reg_count_64;
1994  }
1995  else {
1996  offset_to_reg = qemu_offset_to_reg_32;
1997  reg_sizes = qemu_reg_sizes_32;
1998  reg_count = qemu_reg_count_32;
1999  }
2000  }
2001  else
2002  reg_count = regcache->arch->regcount;
2003 
2004  /*
2005  * This should be enough, but we have realloc() support in case
2006  * there are big registers. Of course, we don't support those yet :).
2007  */
2008  len = reg_count * target->arch->wordsize * 2 + 2;
2009  buf = malloc(len);
2010 
2011  buf[0] = 'G';
2012  next = buf + 1;
2013  for (i = 0; i < reg_count; ++i) {
2014  regval = 0;
2015 
2016  /*
2017  * If we have a special register mapping hack, use it;
2018  * otherwise just go in order by target wordsize. This
2019  * default will almost certainly always be wrong, but we
2020  * have to do something...
2021  */
2022  if (offset_to_reg) {
2023  if (((next + reg_sizes[i] * 2) - buf) >= (len - 1)) {
2024  len += 128;
2025  buf = realloc(buf,len);
2026  }
2027  regcache_read_reg(regcache,offset_to_reg[i],&regval);
2028  __d2hs((char *)&regval,reg_sizes[i],next);
2029  next += reg_sizes[i] * 2;
2030  }
2031  else {
2032  if (((next + target->arch->wordsize * 2) - buf) >= (len - 1)) {
2033  len += 128;
2034  buf = realloc(buf,len);
2035  }
2036  regcache_read_reg(regcache,i,&regval);
2037  __d2hs((char *)&regval,target->arch->wordsize,next);
2038  next += target->arch->wordsize * 2;
2039  }
2040  }
2041 
2042  *next = '\0';
2043 
2044  rc = gdb_rsp_send_packet(target,buf,0,gdb_rsp_simple_ack_handler,&sret);
2045  if (rc) {
2046  verror("could not write registers!\n");
2047  goto errout;
2048  }
2049 
2050  rc = gdb_rsp_recv_until_handled(target,gdb_rsp_simple_ack_handler,&hret);
2051  if (rc) {
2052  verror("failed to recv response!\n");
2053  goto errout;
2054  }
2055  else if (hret != GDB_RSP_HANDLER_DONE) {
2056  verror("simple ack handler returned not done: %d\n",hret);
2057  goto errout;
2058  }
2059  else if (sret) {
2060  verror("failed: error %d\n",sret);
2061  goto errout;
2062  }
2063 
2064  vdebug(8,LA_TARGET,LF_GDB,"wrote all GP regs\n");
2065  return 0;
2066 
2067  errout:
2068  vwarnopt(8,LA_TARGET,LF_GDB,"failed to write GP regs\n");
2069  return -1;
2070 }
2071 
2072 /*
2073  * XXX: since we don't necessarily save all the registers from the 'g'
2074  * read GP regs command, we cannot just use 'G' to write them back. So
2075  * we write each dirty one back invididually... :).
2076  *
2077  * Ok, but the QEMU stub doesn't accept 'p' or 'P' unless the client and
2078  * stub both support the GDB target xml description stuff... which we
2079  * don't, and QEMU by itself doesn't seem to either.
2080  */
2081 int gdb_rsp_write_reg_one_by_one(struct target *target,struct regcache *regcache) {
2082  struct gdb_spec *gspec = (struct gdb_spec *)target->spec->backend_spec;
2084  int *areg_to_qreg = NULL;
2085  REGVAL regval = 0;
2086  /* 129 can support regval sizes up to 64 bytes for __d2hs below. */
2087  char rsval[129];
2088  char buf[160];
2089  int sret = 0;
2090  int retval = 0;
2091  int count = 0;
2092  unsigned int *reg_sizes = NULL;
2093  int i,qreg;
2094  unsigned int qsize;
2095  int rc;
2096 
2097  if (gspec->is_qemu) {
2098  if (target->arch->type == ARCH_X86_64) {
2099  areg_to_qreg = qemu_arch_reg_to_reg_64;
2100  reg_sizes = qemu_reg_sizes_64;
2101  }
2102  else {
2103  areg_to_qreg = qemu_arch_reg_to_reg_32;
2104  reg_sizes = qemu_reg_sizes_32;
2105  }
2106  }
2107 
2108  for (i = 0; i < target->arch->regcount; ++i) {
2109  if (regcache_read_reg_ifdirty(regcache,i,&regval))
2110  continue;
2111 
2112  if (areg_to_qreg) {
2113  qreg = areg_to_qreg[i];
2114  if (qreg < 0)
2115  continue;
2116  qsize = reg_sizes[qreg];
2117  }
2118  else {
2119  /*
2120  * XXX: this is almost certain to be wrong! We need the
2121  * arch-specific register number mapping and size.
2122  */
2123  qreg = i;
2124  qsize = target->arch->wordsize;
2125  }
2126 
2127  __d2hs((char *)&regval,qsize,rsval);
2128  rsval[qsize * 2] = '\0';
2129  rc = snprintf(buf,sizeof(buf),"P%x=%s",qreg,rsval);
2130 
2131  rc = gdb_rsp_send_packet(target,buf,0,gdb_rsp_simple_ack_handler,&sret);
2132  if (rc) {
2133  verror("could not write areg %d qreg %d!\n",i,qreg);
2134  --retval;
2135  continue;
2136  }
2137 
2138  rc = gdb_rsp_recv_until_handled(target,gdb_rsp_simple_ack_handler,&hret);
2139  if (rc) {
2140  verror("failed to recv response (areg %d qreg %d)!\n",i,qreg);
2141  --retval;
2142  }
2143  else if (hret != GDB_RSP_HANDLER_DONE) {
2144  verror("simple ack handler returned not done: %d (areg %d qreg %d)!\n",
2145  hret,i,qreg);
2146  --retval;
2147  }
2148  else if (sret) {
2149  verror("failed: error %d! (areg %d qreg %d)\n",sret,i,qreg);
2150  --retval;
2151  }
2152  else
2153  ++count;
2154  }
2155 
2156  vdebug(8,LA_TARGET,LF_GDB,"wrote %d dirty regs\n",count);
2157  if (retval) {
2158  vwarnopt(8,LA_TARGET,LF_GDB,"failed to write %d dirty regs\n",-retval);
2159  }
2160 
2161  return retval;
2162 }
2163 
2164 int gdb_rsp_read_mem(struct target *target,ADDR addr,
2165  unsigned long length,unsigned char *buf) {
2167  char cmd[128];
2168  struct gdb_rsp_read_mem_data d;
2169 
2170  memset(&d,0,sizeof(d));
2171  d.buf = (char *)buf;
2172  d.length = length;
2173 
2174  snprintf(cmd,sizeof(cmd),"m%"PRIxADDR",%lx",addr,length);
2175 
2176  if (gdb_rsp_send_packet(target,cmd,0,gdb_rsp_read_mem_handler,&d)) {
2177  verror("could not send read mem request '%s'!\n",cmd);
2178  return -1;
2179  }
2180 
2181  if (gdb_rsp_recv_until_handled(target,gdb_rsp_read_mem_handler,&hret)) {
2182  verror("failed to recv read mem response; aborting!\n");
2183  return -1;
2184  }
2185  else if (hret != GDB_RSP_HANDLER_DONE) {
2186  verror("read mem handler returned not done: %d; aborting!\n",hret);
2187  return -1;
2188  }
2189  else if (d.error) {
2190  verror("failed to read mem: error %d!\n",d.error);
2191  return -1;
2192  }
2193 
2194  return 0;
2195 }
2196 
2197 int gdb_rsp_write_mem(struct target *target,ADDR addr,
2198  unsigned long length,unsigned char *buf) {
2200  char *cmd;
2201  unsigned long len;
2202  int sret = 0;
2203 
2204  len = 16 + 1 + 16 + length * 2 + 1;
2205  cmd = malloc(len);
2206  cmd[len] = '\0';
2207 
2208  sret = snprintf(cmd,len,"M%"PRIxADDR",%lu:",addr,length);
2209  __d2hs((char *)buf,length,cmd + len);
2210 
2211  if (gdb_rsp_send_packet(target,cmd,0,gdb_rsp_simple_ack_handler,&sret)) {
2212  verror("could not send request '%s'!\n",cmd);
2213  return -1;
2214  }
2215 
2216  if (gdb_rsp_recv_until_handled(target,gdb_rsp_simple_ack_handler,&hret)) {
2217  verror("failed to recv response; aborting!\n");
2218  return -1;
2219  }
2220  else if (hret != GDB_RSP_HANDLER_DONE) {
2221  verror("simple ack handler returned not done: %d; aborting!\n",hret);
2222  return -1;
2223  }
2224  else if (sret) {
2225  verror("failed: error %d!\n",sret);
2226  return -1;
2227  }
2228 
2229  return 0;
2230 }
2231 
2232 int gdb_rsp_insert_break(struct target *target,ADDR addr,
2233  gdb_rsp_break_t bt,int kind) {
2235  char buf[128];
2236  int sret = 0;
2237 
2238  snprintf(buf,sizeof(buf),"Z%d,%"PRIxADDR",%d",bt,addr,kind);
2239 
2240  if (gdb_rsp_send_packet(target,buf,0,gdb_rsp_simple_ack_handler,&sret)) {
2241  verror("could not send breakpoint insert request '%s'!\n",buf);
2242  return -1;
2243  }
2244 
2245  if (gdb_rsp_recv_until_handled(target,gdb_rsp_simple_ack_handler,&hret)) {
2246  verror("failed to recv breakpoint insert response; aborting!\n");
2247  return -1;
2248  }
2249  else if (hret != GDB_RSP_HANDLER_DONE) {
2250  verror("simple ack handler returned not done: %d; aborting!\n",hret);
2251  return -1;
2252  }
2253  else if (sret) {
2254  verror("failed to insert breakpoint: error %d!\n",sret);
2255  return -1;
2256  }
2257 
2258  return 0;
2259 }
2260 
2261 int gdb_rsp_remove_break(struct target *target,ADDR addr,
2262  gdb_rsp_break_t bt,int kind) {
2264  char buf[128];
2265  int sret = 0;
2266 
2267  snprintf(buf,sizeof(buf),"z%d,%"PRIxADDR",%d",bt,addr,kind);
2268 
2269  if (gdb_rsp_send_packet(target,buf,0,gdb_rsp_simple_ack_handler,&sret)) {
2270  verror("could not send breakpoint remove request '%s'!\n",buf);
2271  return -1;
2272  }
2273 
2274  if (gdb_rsp_recv_until_handled(target,gdb_rsp_simple_ack_handler,&hret)) {
2275  verror("failed to recv breakpoint remove response; aborting!\n");
2276  return -1;
2277  }
2278  else if (hret != GDB_RSP_HANDLER_DONE) {
2279  verror("simple ack handler returned not done: %d; aborting!\n",hret);
2280  return -1;
2281  }
2282  else if (sret) {
2283  verror("failed to remove breakpoint: error %d!\n",sret);
2284  return -1;
2285  }
2286 
2287  return 0;
2288 }
2289 
2294 static inline int __h2ul(char *str,unsigned int len,unsigned long *out) {
2295  unsigned long factor = 0;
2296  unsigned int x;
2297  unsigned long sum = 0;
2298 
2299  if (len > sizeof(*out) * 2) {
2300  errno = ENOMEM;
2301  return -1;
2302  }
2303 
2304  while (len) {
2305  --len;
2306  x = *(str + len);
2307  if (x >= '0' && x <= '9')
2308  x -= '0';
2309  else if (x >= 'A' && x <= 'F')
2310  x = (x - 'A') + 10;
2311  else if (x >= 'a' && x <= 'f')
2312  x = (x - 'a') + 10;
2313  else {
2314  vwarnopt(15,LA_TARGET,LF_GDB,"bad hex char 0x%hhx/%c!\n",x,(char)x);
2315  errno = EINVAL;
2316  return -1;
2317  }
2318 
2319  if (factor == 0) {
2320  sum = x;
2321  factor = 16;
2322  }
2323  else {
2324  sum += x * factor;
2325  factor *= 16;
2326  }
2327  }
2328 
2329  if (out)
2330  *out = sum;
2331 
2332  return 0;
2333 }
2334 
2335 static inline int __hs2ul_lsb(char *str,unsigned int len,unsigned long *out) {
2336  unsigned long factor = 0;
2337  unsigned int x,y;
2338  unsigned long sum = 0;
2339  unsigned int i;
2340 
2341  if (len > sizeof(*out) * 2) {
2342  errno = ENOMEM;
2343  return -1;
2344  }
2345 
2346  i = 0;
2347  while (i < len) {
2348  x = *(str + i);
2349  y = *(str + i + 1);
2350 
2351  if (x >= '0' && x <= '9') x -= '0';
2352  else if (x >= 'A' && x <= 'F') x = (x - 'A') + 10;
2353  else if (x >= 'a' && x <= 'f') x = (x - 'a') + 10;
2354  else {
2355  vwarnopt(15,LA_TARGET,LF_GDB,"bad hex char 0x%hhx!\n",x);
2356  errno = EINVAL;
2357  return -1;
2358  }
2359 
2360  if (y >= '0' && y <= '9') y -= '0';
2361  else if (y >= 'A' && y <= 'F') y = (y - 'A') + 10;
2362  else if (y >= 'a' && y <= 'f') y = (y - 'a') + 10;
2363  else {
2364  vwarnopt(15,LA_TARGET,LF_GDB,"bad hex char 0x%hhx!\n",y);
2365  errno = EINVAL;
2366  return -1;
2367  }
2368 
2369  if (factor == 0) {
2370  sum = y;
2371  factor = 16;
2372  }
2373  else {
2374  sum += y * factor;
2375  factor *= 16;
2376  }
2377 
2378  sum += x * factor;
2379  factor *= 16;
2380 
2381  i += 2;
2382  }
2383 
2384  if (out)
2385  *out = sum;
2386 
2387  return 0;
2388 }
2389 
2390 static inline int __h2ul_be(char *str,unsigned int len,unsigned long *out) {
2391  unsigned long factor = 0;
2392  unsigned int x;
2393  unsigned long sum = 0;
2394  unsigned int i;
2395 
2396  if (len > sizeof(*out) * 2) {
2397  errno = ENOMEM;
2398  return -1;
2399  }
2400 
2401  i = 0;
2402  while (i < len) {
2403  x = *(str + i);
2404  if (x >= '0' && x <= '9')
2405  x -= '0';
2406  else if (x >= 'A' && x <= 'F')
2407  x = (x - 'A') + 10;
2408  else if (x >= 'a' && x <= 'f')
2409  x = (x - 'a') + 10;
2410  else {
2411  vwarnopt(15,LA_TARGET,LF_GDB,"bad hex char 0x%hhx!\n",x);
2412  errno = EINVAL;
2413  return -1;
2414  }
2415 
2416  if (factor == 0) {
2417  sum = x;
2418  factor = 16;
2419  }
2420  else {
2421  sum += x * factor;
2422  factor *= 16;
2423  }
2424 
2425  ++i;
2426  }
2427 
2428  if (out)
2429  *out = sum;
2430 
2431  return 0;
2432 }
2433 
2434 static inline int __hs2d(char *str,unsigned int len,char *buf) {
2435  unsigned int x,y;
2436  unsigned int i;
2437 
2438  i = 0;
2439  while (i < len) {
2440  x = *(str + i);
2441  y = *(str + i + 1);
2442 
2443  if (x >= '0' && x <= '9') x -= '0';
2444  else if (x >= 'A' && x <= 'F') x = (x - 'A') + 10;
2445  else if (x >= 'a' && x <= 'f') x = (x - 'a') + 10;
2446  else {
2447  vwarnopt(15,LA_TARGET,LF_GDB,"bad hex char 0x%hhx!\n",x);
2448  errno = EINVAL;
2449  return -1;
2450  }
2451 
2452  if (y >= '0' && y <= '9') y -= '0';
2453  else if (y >= 'A' && y <= 'F') y = (y - 'A') + 10;
2454  else if (y >= 'a' && y <= 'f') y = (y - 'a') + 10;
2455  else {
2456  vwarnopt(15,LA_TARGET,LF_GDB,"bad hex char 0x%hhx!\n",y);
2457  errno = EINVAL;
2458  return -1;
2459  }
2460 
2461  buf[i/2] = y + 16*x;
2462 
2463  i += 2;
2464  }
2465 
2466  return 0;
2467 }
2468 
2469 static inline int __d2hs(char *buf,unsigned int len,char *str) {
2470  unsigned int i;
2471  uint8_t hi,lo;
2472 
2473  i = 0;
2474  while (i < len) {
2475  lo = buf[i] & 0xf;
2476  hi = (buf[i] >> 4) & 0xf;
2477 
2478  if (lo <= 9) str[i*2+1] = '0' + lo;
2479  else str[i*2+1] = 'a' + (lo - 10);
2480 
2481  if (hi <= 9) str[i*2] = '0' + hi;
2482  else str[i*2] = 'a' + (hi - 10);
2483 
2484  ++i;
2485  }
2486 
2487  return 0;
2488 }
arch_type_t type
Definition: arch.h:117
unsigned int obuf_len
Definition: target_gdb.h:107
struct arch * arch
Definition: regcache.h:36
int gdb_rsp_recv_until_handled(struct target *target, gdb_rsp_handler_t handler, gdb_rsp_handler_ret_t *handler_ret)
gdb_rsp_handler_t handler
Definition: target_gdb.h:112
unsigned int writing
Definition: target_gdb.h:84
#define vwarnopt(level, area, flags, format,...)
Definition: log.h:37
char * ibuf
Definition: target_gdb.h:102
void * state
Definition: target_api.h:2526
unsigned int vcont_S
Definition: target_gdb.h:84
void * backend_spec
Definition: target_api.h:2290
int gdb_rsp_send_notification(struct target *target, char *data, unsigned int len)
char * sockfile
Definition: target_gdb.h:82
Definition: log.h:175
target_status_t
Definition: target_api.h:197
int gdb_rsp_step(struct target *target)
unsigned int vcont
Definition: target_gdb.h:84
void * handler_data
Definition: target_gdb.h:113
int gdb_rsp_remove_break(struct target *target, ADDR addr, gdb_rsp_break_t bt, int kind)
int gdb_rsp_recv(struct target *target, int blocking, int only_one, gdb_ptype_t *o_ptype)
int gdb_rsp_nak(struct target *target)
int gdb_rsp_send_packet(struct target *target, char *data, unsigned int len, gdb_rsp_handler_t handler, void *handler_data)
static uint64_t unsigned int i
unsigned int need_interrupt
Definition: target_gdb.h:84
int regcache_init_reg(struct regcache *regcache, REG reg, REGVAL regval)
Definition: regcache.c:153
int gdb_rsp_write_mem(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
int regcount
Definition: arch.h:128
#define verror(format,...)
Definition: log.h:30
unsigned int vcont_r
Definition: target_gdb.h:84
unsigned int ibuf_alen
Definition: target_gdb.h:103
int gdb_rsp_close(struct target *target, int stay_paused)
int gdb_rsp_resume(struct target *target)
char * obuf
Definition: target_gdb.h:106
#define vwarn(format,...)
Definition: log.h:33
void free(void *ptr)
Definition: debugserver.c:207
unsigned int vcont_s
Definition: target_gdb.h:84
target_status_t gdb_rsp_load_status(struct target *target)
int regcache_read_reg_ifdirty(struct regcache *regcache, REG reg, REGVAL *regval)
Definition: regcache.c:299
int gdb_rsp_connect(struct target *target)
gdb_rsp_break_t
unsigned int vcont_C
Definition: target_gdb.h:84
unsigned int is_qemu
Definition: target_gdb.h:39
gdb_rsp_handler_ret_t
gdb_rsp_handler_ret_t(* gdb_rsp_handler_t)(struct target *target, char *data, unsigned int len, void *handler_data)
int len
Definition: dumptarget.c:52
gdb_rsp_handler_ret_t handler_ret
Definition: target_gdb.h:114
int gdb_rsp_write_reg_one_by_one(struct target *target, struct regcache *regcache)
#define UNIX_PATH_MAX
int gdb_rsp_query_stub(struct target *target)
gdb_ptype_t
unsigned int vcont_t
Definition: target_gdb.h:84
#define vdebug(devel, areas, flags, format,...)
Definition: log.h:302
int gdb_rsp_read_regs(struct target *target, struct regcache *regcache)
void * realloc(void *ptr, size_t size)
Definition: debugserver.c:221
int regcache_read_reg(struct regcache *regcache, REG reg, REGVAL *regval)
Definition: regcache.c:271
int gdb_rsp_write_regs(struct target *target, struct regcache *regcache)
struct arch * arch
Definition: target_api.h:2603
unsigned int wordsize
Definition: arch.h:121
unsigned int rsp_status_valid
Definition: target_gdb.h:84
int gdb_rsp_pause(struct target *target)
#define ARCH_X86_64_REG_COUNT
Definition: arch_x86_64.h:33
int unlink(const char *pathname)
Definition: qemuhacks.c:134
#define ARCH_X86_REG_COUNT
Definition: arch_x86.h:34
Definition: log.h:70
int gdb_rsp_read_mem(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
uint32_t REGVAL
Definition: common.h:66
unsigned int need_ack
Definition: target_gdb.h:84
struct gdb_rsp_stop_status last_stop_status
Definition: target_gdb.h:116
int gdb_rsp_ack(struct target *target)
int h_errno
uint32_t ADDR
Definition: common.h:64
target_status_t status
Definition: target_api.h:2503
unsigned int ibuf_len
Definition: target_gdb.h:104
#define PRIxADDR
Definition: common.h:67
struct target_spec * spec
Definition: target_api.h:2605
int gdb_rsp_recv_until_acked(struct target *target)
void * malloc(size_t size)
Definition: debugserver.c:214
void target_set_status(struct target *target, target_status_t status)
Definition: target.c:4035
GHashTable * stubfeatures
Definition: target_gdb.h:100
#define GDB_CHECKCONN(errcode)
unsigned int vcont_c
Definition: target_gdb.h:84
int gdb_rsp_insert_break(struct target *target, ADDR addr, gdb_rsp_break_t bt, int kind)
int gdb_rsp_interrupt(struct target *target)