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.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 <libgen.h>
30 #include <endian.h>
31 #include <gelf.h>
32 #include <elf.h>
33 #include <libelf.h>
34 #include <argp.h>
35 
36 #include "common.h"
37 #include "arch.h"
38 #include "arch_x86.h"
39 #include "arch_x86_64.h"
40 #include "evloop.h"
41 #include "binfile.h"
42 #include "dwdebug.h"
43 #include "dwdebug_priv.h"
44 #include "target_api.h"
45 #include "target.h"
46 #include "target_arch_x86.h"
47 #include "target_os.h"
48 #include "probe_api.h"
49 
50 #include "target_gdb.h"
51 #include "target_gdb_rsp.h"
52 
53 #include <glib.h>
54 
57 
58 /*
59  * Prototypes.
60  */
61 struct target *gdb_instantiate(struct target_spec *spec,struct evloop *evloop);
62 static struct target *gdb_attach(struct target_spec *spec,
63  struct evloop *evloop);
64 
65 static int gdb_snprintf(struct target *target,char *buf,int bufsiz);
66 static int gdb_init(struct target *target);
67 static int gdb_attach_internal(struct target *target);
68 static int gdb_detach(struct target *target,int stay_paused);
69 static int gdb_fini(struct target *target);
70 static int gdb_kill(struct target *target,int sig);
71 static int gdb_loadspaces(struct target *target);
72 static int gdb_loadregions(struct target *target,struct addrspace *space);
73 static int gdb_loaddebugfiles(struct target *target,struct addrspace *space,
74  struct memregion *region);
75 static int gdb_postloadinit(struct target *target);
76 static int gdb_postopened(struct target *target);
77 static int gdb_set_active_probing(struct target *target,
78  active_probe_flags_t flags);
79 
80 static target_status_t gdb_handle_exception(struct target *target,
82  int *again,void *priv);
83 
84 static struct target *
85 gdb_instantiate_overlay(struct target *target,
86  struct target_thread *tthread,
87  struct target_spec *spec,
88  struct target_thread **ntthread);
89 static struct target_thread *
90 gdb_lookup_overlay_thread_by_id(struct target *target,int id);
91 static struct target_thread *
92 gdb_lookup_overlay_thread_by_name(struct target *target,char *name);
93 static int gdb_attach_overlay_thread(struct target *base,struct target *overlay,
94  tid_t newtid);
95 static int gdb_detach_overlay_thread(struct target *base,struct target *overlay,
96  tid_t tid);
97 
98 static target_status_t gdb_status(struct target *target);
99 static int gdb_pause(struct target *target,int nowait);
100 static int __gdb_resume(struct target *target,int detaching);
101 static int gdb_resume(struct target *target);
102 static target_status_t gdb_monitor(struct target *target);
103 static target_status_t gdb_poll(struct target *target,struct timeval *tv,
104  target_poll_outcome_t *outcome,int *pstatus);
105 static int gdb_attach_evloop(struct target *target,struct evloop *evloop);
106 static int gdb_detach_evloop(struct target *target);
107 static unsigned char *gdb_read(struct target *target,ADDR addr,
108  unsigned long length,unsigned char *buf);
109 static unsigned long gdb_write(struct target *target,ADDR addr,
110  unsigned long length,unsigned char *buf);
111 static int gdb_addr_v2p(struct target *target,tid_t tid,
112  ADDR vaddr,ADDR *paddr);
113 static unsigned char *gdb_read_phys(struct target *target,ADDR paddr,
114  unsigned long length,unsigned char *buf);
115 static unsigned long gdb_write_phys(struct target *target,ADDR paddr,
116  unsigned long length,unsigned char *buf);
117 
118 static tid_t gdb_gettid(struct target *target);
119 static void gdb_free_thread_state(struct target *target,void *state);
120 static struct array_list *gdb_list_available_tids(struct target *target);
121 static struct target_thread *gdb_load_thread(struct target *target,tid_t tid,
122  int force);
123 static struct target_thread *gdb_load_current_thread(struct target *target,
124  int force);
125 static int gdb_load_all_threads(struct target *target,int force);
126 static int gdb_load_available_threads(struct target *target,int force);
127 /*
128  * XXX: figure out how to do this later... for some targets, pausing a
129  * single thread might be meaningful; others you don't have thread control.
130  */
131 //static int gdb_pause_thread(struct target *target,tid_t tid,int nowait);
132 static int gdb_flush_thread(struct target *target,tid_t tid);
133 static int gdb_flush_current_thread(struct target *target);
134 static int gdb_flush_all_threads(struct target *target);
135 static int gdb_invalidate_thread(struct target *target,
136  struct target_thread *tthread);
137 static int gdb_thread_snprintf(struct target *target,
138  struct target_thread *tthread,
139  char *buf,int bufsiz,
140  int detail,char *sep,char *key_val_sep);
141 
142 static struct target_memmod *gdb_insert_sw_breakpoint(struct target *target,
143  tid_t tid,ADDR addr);
144 static int gdb_remove_sw_breakpoint(struct target *target,tid_t tid,
145  struct target_memmod *mmod);
146 static int gdb_remove_sw_breakpoint(struct target *target,tid_t tid,
147  struct target_memmod *mmod);
148 static int gdb_enable_sw_breakpoint(struct target *target,tid_t tid,
149  struct target_memmod *mmod);
150 static int gdb_disable_sw_breakpoint(struct target *target,tid_t tid,
151  struct target_memmod *mmod);
152 static int gdb_change_sw_breakpoint(struct target *target,tid_t tid,
153  struct target_memmod *mmod,
154  unsigned char *code,unsigned long code_len);
155 static int gdb_unchange_sw_breakpoint(struct target *target,tid_t tid,
156  struct target_memmod *mmod);
157 static int gdb_set_hw_breakpoint(struct target *target,tid_t tid,
158  REG num,ADDR addr);
159 static int gdb_set_hw_watchpoint(struct target *target,tid_t tid,
160  REG num,ADDR addr,
161  probepoint_whence_t whence,
162  probepoint_watchsize_t watchsize);
163 static int gdb_unset_hw_breakpoint(struct target *target,tid_t tid,REG num);
164 static int gdb_unset_hw_watchpoint(struct target *target,tid_t tid,REG num);
165 static int gdb_disable_hw_breakpoints(struct target *target,tid_t tid);
166 static int gdb_enable_hw_breakpoints(struct target *target,tid_t tid);
167 static int gdb_disable_hw_breakpoint(struct target *target,tid_t tid,REG dreg);
168 static int gdb_enable_hw_breakpoint(struct target *target,tid_t tid,REG dreg);
169 
170 static int gdb_singlestep(struct target *target,tid_t tid,int isbp,
171  struct target *overlay);
172 static int gdb_singlestep_end(struct target *target,tid_t tid,
173  struct target *overlay);
174 
176 
177 /* Internal prototypes. */
178 static struct target_thread *__gdb_load_cached_thread(struct target *target,
179  tid_t tid);
180 static struct target_thread *__gdb_load_current_thread(struct target *target,
181  int force,
182  int globalonly);
183 static int __gdb_pgd(struct target *target,tid_t tid,uint64_t *pgd);
184 
185 /*
186  * Globals.
187  */
188 
189 
190 /*
191  * Set up the target interface for this library.
192  */
193 struct target_ops gdb_ops = {
194  .snprintf = gdb_snprintf,
195 
196  .init = gdb_init,
197  .fini = gdb_fini,
198  .attach = gdb_attach_internal,
199  .detach = gdb_detach,
200 /*
201  .kill = gdb_kill,
202 */
203 
204  .loadspaces = gdb_loadspaces,
205  .loadregions = gdb_loadregions,
206  .loaddebugfiles = gdb_loaddebugfiles,
207  .postloadinit = gdb_postloadinit,
208  .postopened = gdb_postopened,
209  .set_active_probing = gdb_set_active_probing,
210 
211  .handle_exception = gdb_handle_exception,
212  .handle_break = probepoint_bp_handler,
213  .handle_step = probepoint_ss_handler,
214 /*
215  .handle_interrupted_step = NULL,
216 */
217 
218  .instantiate_overlay = gdb_instantiate_overlay,
219  .lookup_overlay_thread_by_id = gdb_lookup_overlay_thread_by_id,
220  .lookup_overlay_thread_by_name = gdb_lookup_overlay_thread_by_name,
221  .attach_overlay_thread = gdb_attach_overlay_thread,
222  .detach_overlay_thread = gdb_detach_overlay_thread,
223 
224  .status = gdb_status,
225  .pause = gdb_pause,
226  .resume = gdb_resume,
227  .monitor = gdb_monitor,
228  .poll = gdb_poll,
229 
230  .read = gdb_read,
231  .write = gdb_write,
232  .addr_v2p = gdb_addr_v2p,
233  .read_phys = gdb_read_phys,
234  .write_phys = gdb_write_phys,
235 
236  .gettid = gdb_gettid,
237  .free_thread_state = gdb_free_thread_state,
238  .list_available_tids = gdb_list_available_tids,
239  .load_available_threads = gdb_load_available_threads,
240  .load_thread = gdb_load_thread,
241  .load_current_thread = gdb_load_current_thread,
242  .load_all_threads = gdb_load_all_threads,
243  .pause_thread = NULL,
244  .flush_thread = gdb_flush_thread,
245  .flush_current_thread = gdb_flush_current_thread,
246  .flush_all_threads = gdb_flush_all_threads,
247  .invalidate_thread = gdb_invalidate_thread,
248  .thread_snprintf = gdb_thread_snprintf,
249 
250  .attach_evloop = gdb_attach_evloop,
251  .detach_evloop = gdb_detach_evloop,
252 
253  .readreg = target_regcache_readreg,
254  .writereg = target_regcache_writereg,
255  .copy_registers = target_regcache_copy_registers,
256  .readreg_tidctxt = target_regcache_readreg_tidctxt,
257  .writereg_tidctxt = target_regcache_writereg_tidctxt,
258 
259  .insert_sw_breakpoint = gdb_insert_sw_breakpoint,
260  .remove_sw_breakpoint = gdb_remove_sw_breakpoint,
261  .change_sw_breakpoint = gdb_change_sw_breakpoint,
262  .unchange_sw_breakpoint = gdb_unchange_sw_breakpoint,
263  .enable_sw_breakpoint = gdb_enable_sw_breakpoint,
264  .disable_sw_breakpoint = gdb_disable_sw_breakpoint,
265 
266  .set_hw_breakpoint = gdb_set_hw_breakpoint,
267  .set_hw_watchpoint = gdb_set_hw_watchpoint,
268  .unset_hw_breakpoint = gdb_unset_hw_breakpoint,
269  .unset_hw_watchpoint = gdb_unset_hw_watchpoint,
270  .disable_hw_breakpoints = gdb_disable_hw_breakpoints,
271  .enable_hw_breakpoints = gdb_enable_hw_breakpoints,
272  .disable_hw_breakpoint = gdb_disable_hw_breakpoint,
273  .enable_hw_breakpoint = gdb_enable_hw_breakpoint,
274 
275  .singlestep = gdb_singlestep,
276  .singlestep_end = gdb_singlestep_end,
277 /*
278  .instr_can_switch_context = gdb_instr_can_switch_context,
279 */
280 };
281 
282 #define GDB_ARGP_HOST 0x650001
283 #define GDB_ARGP_PORT 0x650002
284 #define GDB_ARGP_UDP 0x650003
285 #define GDB_ARGP_SOCKFILE 0x650004
286 #define GDB_ARGP_IS_KVM 0x650005
287 #define GDB_ARGP_IS_QEMU 0x650006
288 #define GDB_ARGP_QEMU_QMP_HOST 0x650007
289 #define GDB_ARGP_QEMU_MEM_PATH 0x650008
290 #define GDB_ARGP_QEMU_QMP_PORT 0x650009
291 #define GDB_ARGP_CLEAR_MEM_CACHES 0x65000a
292 #define GDB_ARGP_MEMCACHE_MMAP_SIZE 0x65000b
293 #define GDB_ARGP_LIBVIRT_DOMAIN 0x65000c
294 
295 struct argp_option gdb_argp_opts[] = {
296  /* These options set a flag. */
297  { "gdb-host",GDB_ARGP_HOST,"HOSTNAME",0,
298  "The hostname the GDB stub is listening on (default localhost).",-4 },
299  { "gdb-port",GDB_ARGP_PORT,"PORT",0,
300  "The port the GDB stub is listening on (default 1234).",-4 },
301  { "gdb-udp",GDB_ARGP_UDP,NULL,0,
302  "Use UDP instead of TCP (default TCP).",-4 },
303  { "gdb-sockfile",GDB_ARGP_SOCKFILE,"FILENAME",0,
304  "The UNIX domain socket filename the GDB stub is listening on.",-4 },
305  { "main-filename",'M',"FILE",0,
306  "Set main binary's filepath for target.",-4 },
307  { "clear-mem-caches-each-exception",GDB_ARGP_CLEAR_MEM_CACHES,NULL,0,
308  "Clear mem caches on each debug exception.",-4 },
309  { "qemu",GDB_ARGP_IS_QEMU,NULL,0,
310  "Enable QEMU GDB stub support",-4 },
311  { "qemu-qmp-host",GDB_ARGP_QEMU_QMP_HOST,"HOSTNAME",0,
312  "Attach to QEMU QMP on the given host (default localhost).",-4 },
313  { "qemu-qmp-port",GDB_ARGP_QEMU_QMP_PORT,"PORT",0,
314  "Attach to QEMU QMP on the given port (default 1235).",-4 },
315  { "qemu-mem-path",GDB_ARGP_QEMU_MEM_PATH,"PATHNAME",0,
316  "Read/write QEMU's physical memory via this filename (see QEMU's -mem-path option; also preload libnunlink.so and set NUNLINK_PREFIX accordingly).",-4 },
317 #ifdef ENABLE_LIBVIRT
318  { "qemu-libvirt-domain",GDB_ARGP_LIBVIRT_DOMAIN,"DOMAIN",0,
319  "Access QEMU QMP over libvirt proxy.",-4 },
320 #endif
321  { "kvm",GDB_ARGP_IS_KVM,NULL,0,
322  "Enable QEMU GDB KVM stub support.",-4 },
323  { "memcache-mmap-size",GDB_ARGP_MEMCACHE_MMAP_SIZE,"BYTES",0,
324  "Max size (bytes) of the mmap cache (default 128MB).",-4 },
325  { 0,0,0,0,0,0 }
326 };
327 
328 int gdb_spec_to_argv(struct target_spec *spec,int *argc,char ***argv) {
329  struct gdb_spec *xspec =
330  (struct gdb_spec *)spec->backend_spec;
331  char **av = NULL;
332  int ac = 0;
333  int j;
334 
335  if (!xspec) {
336  if (argv)
337  *argv = NULL;
338  if (argc)
339  *argc = 0;
340  return 0;
341  }
342 
343  if (xspec->hostname)
344  ac += 2;
345  if (xspec->port)
346  ac += 2;
347  if (xspec->do_udp)
348  ac += 1;
349  if (xspec->sockfile)
350  ac += 2;
351 
352  if (xspec->main_filename)
353  ac += 2;
355  ac += 1;
356  if (xspec->is_qemu)
357  ac += 1;
358  if (xspec->qemu_qmp_hostname)
359  ac += 2;
360  if (xspec->qemu_qmp_port > 0)
361  ac += 2;
362  if (xspec->qemu_mem_path)
363  ac += 2;
364 #ifdef ENABLE_LIBVIRT
365  if (xspec->qemu_libvirt_domain)
366  ac += 2;
367 #endif
368  if (xspec->is_kvm)
369  ac += 1;
370  if (xspec->memcache_mmap_size)
371  ac += 2;
372 
373  av = calloc(ac + 1,sizeof(char *));
374  j = 0;
375  if (xspec->hostname) {
376  av[j++] = strdup("--gdb-host");
377  av[j++] = strdup(xspec->hostname);
378  }
379  if (xspec->port > -1) {
380  av[j++] = strdup("--gdb-port");
381  av[j] = malloc(16);
382  snprintf(av[j],16,"%d",xspec->port);
383  ++j;
384  }
385  if (xspec->do_udp) {
386  av[j++] = strdup("--gdb-udp");
387  }
388  if (xspec->sockfile) {
389  av[j++] = strdup("--gdb-sockfile");
390  av[j++] = strdup(xspec->sockfile);
391  }
392  if (xspec->main_filename) {
393  av[j++] = strdup("-M");
394  av[j++] = strdup(xspec->main_filename);
395  }
396  if (xspec->clear_mem_caches_each_exception) {
397  av[j++] = strdup("--clear-mem-caches-each-exception");
398  }
399  if (xspec->is_qemu)
400  av[j++] = strdup("--qemu");
401  if (xspec->qemu_qmp_hostname) {
402  av[j++] = strdup("--qemu-qmp-host");
403  av[j++] = strdup(xspec->qemu_qmp_hostname);
404  }
405  if (xspec->qemu_qmp_port > 0) {
406  av[j++] = strdup("--qemu-qmp-port");
407  av[j] = malloc(16);
408  snprintf(av[j],16,"%d",xspec->qemu_qmp_port);
409  j++;
410  }
411  if (xspec->qemu_mem_path) {
412  av[j++] = strdup("--qemu-mem-path");
413  av[j++] = strdup(xspec->qemu_mem_path);
414  }
415 #ifdef ENABLE_LIBVIRT
416  if (xspec->qemu_libvirt_domain) {
417  av[j++] = strdup("--qemu-libvirt-domain");
418  av[j++] = strdup(xspec->qemu_libvirt_domain);
419  }
420 #endif
421  if (xspec->is_kvm)
422  av[j++] = strdup("--kvm");
423  if (xspec->memcache_mmap_size) {
424  av[j++] = strdup("--memcache-mmap-size");
425  av[j] = malloc(32);
426  snprintf(av[j],32,"%lu",xspec->memcache_mmap_size);
427  j++;
428  }
429  av[j++] = NULL;
430 
431  if (argv)
432  *argv = av;
433  if (argc)
434  *argc = ac;
435 
436  return 0;
437 }
438 
439 error_t gdb_argp_parse_opt(int key,char *arg,struct argp_state *state) {
440  struct target_argp_parser_state *tstate = \
441  (struct target_argp_parser_state *)state->input;
442  struct target_spec *spec;
443  struct gdb_spec *xspec;
444  struct argp_option *opti;
445  int ourkey;
446 
447  if (key == ARGP_KEY_INIT)
448  return 0;
449  else if (!state->input)
450  return ARGP_ERR_UNKNOWN;
451 
452  if (tstate)
453  spec = tstate->spec;
454 
455  /*
456  * Check to see if this is really one of our keys. If it is, we
457  * need to see if some other backend has already started parsing
458  * args; if it has, we throw an error. Otherwise, we assume we are
459  * using this backend, and process the arg.
460  */
461  ourkey = 0;
462  for (opti = &gdb_argp_opts[0]; opti->key != 0; ++opti) {
463  if (key == opti->key) {
464  ourkey = 1;
465  break;
466  }
467  }
468 
469  if (ourkey) {
472  xspec = gdb_build_spec();
473  spec->backend_spec = xspec;
474  }
475  else if (spec->target_type != TARGET_TYPE_GDB) {
476  verror("cannot mix arguments for GDB target (%c) with non-GDB"
477  " target!\n",key);
478  return EINVAL;
479  }
480 
481  /* Only "claim" these args if this is our key. */
484  xspec = calloc(1,sizeof(*xspec));
485  spec->backend_spec = xspec;
486  }
487  else if (spec->target_type != TARGET_TYPE_GDB) {
488  verror("cannot mix arguments for GDB target with non-GDB target!\n");
489  return EINVAL;
490  }
491  }
492 
494  xspec = (struct gdb_spec *)spec->backend_spec;
495  else
496  xspec = NULL;
497 
498  switch (key) {
499  case ARGP_KEY_ARG:
500  case ARGP_KEY_ARGS:
501  /* Only handle these if you need arguments. */
502  return ARGP_ERR_UNKNOWN;
503  case ARGP_KEY_INIT:
504  case ARGP_KEY_END:
505  case ARGP_KEY_NO_ARGS:
506  /* Nothing to do unless you malloc something in _INIT. */
507  return 0;
508  case ARGP_KEY_SUCCESS:
509  case ARGP_KEY_ERROR:
510  case ARGP_KEY_FINI:
511  /* Check spec for sanity if necessary. */
512  return 0;
513 
514  case GDB_ARGP_HOST:
515  xspec->hostname = strdup(arg);
516  break;
517  case GDB_ARGP_PORT:
518  xspec->port = atoi(arg);
519  break;
520  case GDB_ARGP_UDP:
521  xspec->do_udp = 1;
522  break;
523  case GDB_ARGP_SOCKFILE:
524  xspec->sockfile = strdup(arg);
525  break;
526  case 'M':
527  xspec->main_filename = strdup(arg);
528  break;
530  xspec->clear_mem_caches_each_exception = 1;
531  break;
532  case GDB_ARGP_IS_QEMU:
533  xspec->is_qemu = 1;
534  break;
536  xspec->qemu_qmp_hostname = strdup(arg);
537  break;
539  xspec->qemu_qmp_port = atoi(arg);
540  break;
542  xspec->qemu_mem_path = strdup(arg);
543  break;
544 #ifdef ENABLE_LIBVIRT
546  xspec->qemu_libvirt_domain = strdup(arg);
547  break;
548 #endif
549  case GDB_ARGP_IS_KVM:
550  xspec->is_kvm = 1;
551  break;
553  xspec->memcache_mmap_size = atoi(arg);
554  break;
555  default:
556  return ARGP_ERR_UNKNOWN;
557  }
558 
559  return 0;
560 }
561 
562 struct argp gdb_argp = {
563  gdb_argp_opts,gdb_argp_parse_opt,NULL,NULL,NULL,NULL,NULL
564 };
565 char *gdb_argp_header = "GDB Backend Options";
566 
571 struct gdb_spec *gdb_build_spec(void) {
572  struct gdb_spec *xspec;
573 
574  xspec = calloc(1,sizeof(*xspec));
575  xspec->port = -1;
576 
577  return xspec;
578 }
579 
580 void gdb_free_spec(struct gdb_spec *xspec) {
581  if (xspec->devfile)
582  free(xspec->devfile);
583  if (xspec->sockfile)
584  free(xspec->sockfile);
585  if(xspec->hostname)
586  free(xspec->hostname);
587 
588  free(xspec);
589 }
590 
592  struct evloop *evloop) {
593  return gdb_attach(spec,evloop);
594 }
595 
600 /*
601  * Attaches to the GDB server.
602  */
603 static struct target *gdb_attach(struct target_spec *spec,
604  struct evloop *evloop) {
605  struct gdb_spec *xspec = (struct gdb_spec *)spec->backend_spec;
606  struct target *target = NULL;
607  struct gdb_state *xstate = NULL;
608 
609  vdebug(5,LA_TARGET,LF_GDB,"attaching to GDB server\n");
610 
611  if (!(target = target_create("gdb",spec)))
612  return NULL;
613 
614  if (!(xstate = (struct gdb_state *)malloc(sizeof(*xstate)))) {
615  free(target);
616  return NULL;
617  }
618  memset(xstate,0,sizeof(*xstate));
619  /* Assume RSP needs acks unless we hear otherwise. */
620  xstate->need_ack = 1;
621 
622  xstate->fd = xstate->wfd = -1;
623 
624  target->state = xstate;
625 
626  xstate->evloop_fd = -1;
627 
628  if (xspec->main_filename) {
629  g_hash_table_insert(target->config,strdup("MAIN_FILENAME"),
630  strdup(xspec->main_filename));
631 
633  "using main filename %s\n",xspec->main_filename);
634  }
635 
636  /*
637  * Try to infer the personality.
638  */
639  if (!target->personality_ops
640  && xspec->main_filename
641  && (strstr(xspec->main_filename,"inux")
642  || strstr(xspec->main_filename,"inuz"))) {
643  if (target_personality_attach(target,"os_linux_generic",NULL) == 0) {
645  "autoinitialized the os_linux_generic personality!\n");
646  }
647  else {
648  verror("failed to autoinitialize the os_linux_generic personality!\n");
649  goto errout;
650  }
651  }
652  else {
653  vwarn("cannot initialize a personality!\n");
654  }
655 
656  target->live = 1;
657  target->writeable = 1;
658  target->mmapable = 0; /* XXX: change this once we get mmap API
659  worked out. */
660 
661  /*
662  * Now load up our {xa|vmi}_instance as much as we can now; we'll
663  * try to do more when we load the debuginfo file for the kernel.
664  */
665  xstate->hops = NULL;
666  if (!xstate->hops && xspec->is_qemu)
667  xstate->hops = &gdb_helper_ops_qemu;
668  else if (!xstate->hops)
669  xstate->hops = &gdb_helper_ops_builtin;
670 
671  if (xstate->hops->init) {
672  if (xstate->hops->init(target)) {
673  verror("failed to init hops!\n");
674  goto errout;
675  }
676  }
677 
678  /* Our threads can have two contexts -- kernel and user spaces. */
680 
681  if (evloop && xstate->evloop_fd < 0) {
682  /*
683  * Just save it off; we can't use it until in gdb_attach_internal.
684  */
685  target->evloop = evloop;
686  }
687 
688  vdebug(5,LA_TARGET,LF_GDB,"opened GDB server\n");
689 
690  return target;
691 
692  errout:
693  if (xstate->ostype) {
694  free(xstate->ostype);
695  xstate->ostype = NULL;
696  }
697  if (xstate) {
698  free(xstate);
699  if (target)
700  target->state = NULL;
701  }
702  if (target)
704 
705  return NULL;
706 }
707 
708 static int gdb_snprintf(struct target *target,char *buf,int bufsiz) {
709  struct gdb_spec *xspec = \
710  (struct gdb_spec *)target->spec->backend_spec;
711 
712  if (xspec->do_udp)
713  return snprintf(buf,bufsiz,"gdbstub@udp(%s::%d)",
714  xspec->hostname,xspec->port);
715  else if (xspec->do_unix)
716  return snprintf(buf,bufsiz,"gdbstub@unix(%s)",
717  xspec->sockfile);
718  else if (xspec->hostname || xspec->port > -1)
719  return snprintf(buf,bufsiz,"gdbstub@tcp(%s::%d)",
720  xspec->hostname,xspec->port);
721  else
722  return snprintf(buf,bufsiz,"gdbstub@unknown");
723 }
724 
725 static int gdb_init(struct target *target) {
726  struct gdb_state *xstate = (struct gdb_state *)target->state;
727  struct gdb_thread_state *tstate;
728 
729  vdebug(5,LA_TARGET,LF_GDB,"target %s\n",target->name);
730 
731  if (target->spec->bpmode == THREAD_BPMODE_STRICT) {
732  vwarn("auto-enabling SEMI_STRICT bpmode on GDB target %s\n",target->name);
734  }
735 
736  // XXX
737  target->nodisablehwbponss = 1;
738  target->threadctl = 0;
739  /*
740  * GDB manages EIP for us, of course.
741  */
742  target->no_adjust_bp_ip = 1;
743 
744  xstate->stubfeatures =
745  g_hash_table_new_full(g_str_hash,g_str_equal,free,free);
746 
747  /* Create the default thread. */
748  tstate = (struct gdb_thread_state *)calloc(1,sizeof(*tstate));
749 
750  target->global_thread = target_create_thread(target,TID_GLOBAL,tstate,NULL);
751  /* Default thread is always running. */
753 
754  /* Create our default context now; update its region later. */
755  target->global_tlctxt =
757 
758  return 0;
759 }
760 
761 static int gdb_attach_internal(struct target *target) {
762  struct gdb_state *xstate = (struct gdb_state *)target->state;
763 
764  vdebug(5,LA_TARGET,LF_GDB,"attaching to GDB stub\n");
765 
766  /*
767  * Attach to the GDB stub!
768  */
769  if (gdb_rsp_connect(target))
770  return -1;
771 
772  if (gdb_pause(target,0)) {
773  vwarn("could not pause target before attaching; continuing anyway!\n");
774  }
775 
776  /*
777  * Make sure hops is setup to read from memory.
778  */
779  if (xstate->hops && xstate->hops->attach) {
780  if (xstate->hops->attach(target)) {
781  verror("could not attach hops!\n");
782  return -1;
783  }
784  }
785 
786  if (target->evloop && xstate->evloop_fd < 0) {
787  gdb_attach_evloop(target,target->evloop);
788  }
789 
790  return 0;
791 }
792 
793 static int gdb_detach(struct target *target,int stay_paused) {
794  struct gdb_state *xstate = (struct gdb_state *)(target->state);
795 
797  "preparing to detach from GDB stub (target %s)\n",target->name);
798 
799  if (!target->opened)
800  return 0;
801 
802  if (gdb_status(target) == TSTATUS_PAUSED
803  && (g_hash_table_size(target->threads) || target->global_thread)) {
804  /* Flush back registers if they're dirty, but if we don't have
805  * any threads (i.e. because we're closing/detaching), don't
806  * flush all, which would load the global thread!
807  */
808  target_flush_all_threads(target);
809  }
810 
811  if (target->evloop && xstate->evloop_fd > -1)
812  gdb_detach_evloop(target);
813 
814  if (xstate->hops->fini) {
815  if (xstate->hops->fini(target)) {
816  verror("failed to fini hops; continuing anyway (target %s)!\n",
817  target->name);
818  return 0;
819  }
820  }
821 
822  /*
823  * Hopefully we don't need this...
824  */
825  /*
826  if (gdb_status(target) == TSTATUS_PAUSED) {
827  __gdb_resume(target,1);
828  }
829  */
830 
831  vdebug(4,LA_TARGET,LF_GDB,"detaching from stub (target %s)\n",target->name);
832 
833  if (gdb_rsp_close(target,stay_paused))
834  verror("failed to detach from GDB stub (target %s)!\n",target->name);
835  else
836  vdebug(3,LA_TARGET,LF_GDB,"detach succeeded (target %s)\n",target->name);
837 
838  return 0;
839 }
840 
841 static int gdb_fini(struct target *target) {
842  struct gdb_state *xstate = (struct gdb_state *)(target->state);
843 
844  vdebug(5,LA_TARGET,LF_GDB,"target %s\n",target->name);
845 
846  if (xstate) {
847  if (xstate->machine) {
848  regcache_destroy(xstate->machine);
849  xstate->machine = NULL;
850  }
851  if (xstate->sockfile) {
852  unlink(xstate->sockfile);
853  free(xstate->sockfile);
854  }
855  if (xstate->stubfeatures)
856  g_hash_table_destroy(xstate->stubfeatures);
857  if (xstate->ibuf)
858  free(xstate->ibuf);
859 
860  free(xstate);
861  }
862 
863  return 0;
864 }
865 
866 /*
867  * One is enough for any GDB target, I think.
868  */
869 static int gdb_loadspaces(struct target *target) {
870  vdebug(5,LA_TARGET,LF_GDB,"%s\n",target->name);
871 
872  if (target->personality == TARGET_PERSONALITY_OS)
873  addrspace_create(target,"kernel",1);
874  else
875  addrspace_create(target,"main",1);
876 
877  return 0;
878 }
879 
880 /*
881  * XXX: actually use GDB RSP's region info...
882  */
883 static int gdb_loadregions(struct target *target,struct addrspace *space) {
884  struct memregion *region;
885  struct memrange *range;
886  char *main_filename;
887 
888  vdebug(5,LA_TARGET,LF_GDB,"%s\n",target->name);
889 
890  main_filename =
891  (char *)g_hash_table_lookup(target->config,"MAIN_FILENAME");
892  if (!main_filename)
893  return 0;
894 
895  region = memregion_create(space,REGION_TYPE_MAIN,main_filename);
896  if (!region)
897  return -1;
898  range = memrange_create(region,0,ADDRMAX,0,
900  if (!range)
901  return -1;
902 
903  target->global_tlctxt->region = region;
904 
905  return 0;
906 }
907 
908 static int gdb_loaddebugfiles(struct target *target,struct addrspace *space,
909  struct memregion *region) {
910  int retval = -1;
911  struct debugfile *debugfile;
912  int bfn = 0;
913  int bfpn = 0;
914 
915  vdebug(5,LA_TARGET,LF_GDB,"%s\n",target->name);
916 
917  /*
918  * Open up the actual ELF binary and look for three sections to inform
919  * our search. First, if there is a nonzero .debug_info section,
920  * load that. Second, if there is a .note.gnu.build-id section,
921  * read the build id and decompose it into a two-byte dir/file.debug
922  * string that we look for in our search path (i.e., we look for
923  * $PATH/.build-id/b1/b2..bX.debug). Otherwise, if there is a
924  * .gnu_debuglink section, we read that section and try to find a
925  * matching debug file.
926  */
927  if (!region->name || strlen(region->name) == 0)
928  return -1;
929 
930  debugfile = debugfile_from_file(region->name,
931  target->spec->debugfile_root_prefix,
932  target->spec->debugfile_load_opts_list);
933  if (!debugfile)
934  goto out;
935 
936  if (target_associate_debugfile(target,region,debugfile)) {
937  goto out;
938  }
939 
940  /*
941  * Try to figure out which binfile has the info we need. On
942  * different distros, they're stripped different ways.
943  */
944  if (debugfile->binfile_pointing) {
945  binfile_get_root_scope_sizes(debugfile->binfile,&bfn,NULL,NULL,NULL);
947  NULL,NULL,NULL);
948  if (bfpn > bfn) {
949  RHOLD(debugfile->binfile_pointing,region);
950  region->binfile = debugfile->binfile_pointing;
951  }
952  }
953 
954  if (!region->binfile) {
955  RHOLD(debugfile->binfile,region);
956  region->binfile = debugfile->binfile;
957  }
958 
959  if (!target->arch) {
960  target->arch = debugfile->binfile->arch;
961  }
962 
963  /*
964  * Propagate some binfile info...
965  */
966  region->base_phys_addr = region->binfile->base_phys_addr;
967  region->base_virt_addr = region->binfile->base_virt_addr;
968 
969  retval = 0;
970 
971  out:
972  return retval;
973 }
974 
975 static int gdb_postloadinit(struct target *target) {
976  struct gdb_state *gstate = (struct gdb_state *)target->state;
977  int rc;
978 
979  gstate->machine = regcache_create(target->arch);
980 
981  /*
982  * We might not know this until now! Which register is the fbreg is
983  * dependent on host cpu type, not target cpu type.
984  */
985  if (target->arch->type == ARCH_X86_64) {
986  target->fbregno = REG_X86_64_RBP;
987  target->spregno = REG_X86_64_RSP;
988  target->ipregno = REG_X86_64_RIP;
989  }
990  else {
991  target->fbregno = REG_X86_EBP;
992  target->spregno = REG_X86_ESP;
993  target->ipregno = REG_X86_EIP;
994  }
995 
996  SAFE_PERSONALITY_OP_WARN(init,rc,0,target);
997  SAFE_PERSONALITY_OP_WARN(postloadinit,rc,0,target);
998 
999  char *start = (char *)g_hash_table_lookup(target->config,
1000  "OS_KERNEL_START_ADDR");
1001  if (start)
1002  gstate->kernel_start_addr = strtoul(start,NULL,0);
1003 
1004  return 0;
1005 
1006  return 0;
1007 }
1008 
1009 static int gdb_postopened(struct target *target) {
1010  int rc;
1011  SAFE_PERSONALITY_OP_WARN(postopened,rc,0,target);
1012  return rc;
1013 }
1014 
1015 static int gdb_set_active_probing(struct target *target,
1016  active_probe_flags_t flags) {
1017  int rc;
1018  SAFE_PERSONALITY_OP_WARN(set_active_probing,rc,0,target,flags);
1019  return rc;
1020 }
1021 
1022 static struct target *
1023 gdb_instantiate_overlay(struct target *target,
1024  struct target_thread *tthread,
1025  struct target_spec *spec,
1026  struct target_thread **ntthread) {
1027  struct target *overlay;
1028  REGVAL thip;
1029  tid_t ltid;
1030  struct target_thread *leader;
1031 
1032  if (spec->target_type != TARGET_TYPE_OS_PROCESS) {
1033  errno = EINVAL;
1034  return NULL;
1035  }
1036 
1037  errno = 0;
1038  thip = target_read_reg(target,tthread->tid,target->ipregno);
1039  if (errno) {
1040  verror("could not read IP for tid %"PRIiTID"!!\n",tthread->tid);
1041  return NULL;
1042  }
1043  if (target_os_thread_is_user(target,tthread->tid) != 1) {
1044  errno = EINVAL;
1045  verror("tid %"PRIiTID" IP 0x%"PRIxADDR" is not a user thread!\n",
1046  tthread->tid,thip);
1047  return NULL;
1048  }
1049 
1050  /*
1051  * Flip to the group leader if it is not this thread itself.
1052  */
1053  ltid = target_os_thread_get_leader(target,tthread->tid);
1054  leader = target_lookup_thread(target,ltid);
1055  if (!leader) {
1056  verror("could not load group_leader for thread %d; BUG?!\n",tthread->tid);
1057  return NULL;
1058  }
1059  else if (leader != tthread) {
1061  "using group_leader %d instead of user-supplied overlay thread %d\n",
1062  leader->tid,tthread->tid);
1063  *ntthread = leader;
1064  }
1065 
1066  /*
1067  * All we want to do here is create the overlay target.
1068  */
1069  overlay = target_create("os_process",spec);
1070 
1071  return overlay;
1072 }
1073 
1074 static struct target_thread *
1075 gdb_lookup_overlay_thread_by_id(struct target *target,int id) {
1076  struct target_thread *retval;
1077 
1078  retval = gdb_load_thread(target,id,0);
1079  if (!retval) {
1080  if (!errno)
1081  errno = ESRCH;
1082  return NULL;
1083  }
1084 
1085  if (target_os_thread_is_user(target,retval->tid) == 1) {
1087  "found overlay thread %d\n",id);
1088  return retval;
1089  }
1090  else {
1091  verror("tid %d matched %d, but is a kernel thread!\n",retval->tid,id);
1092  errno = EINVAL;
1093  return NULL;
1094  }
1095 }
1096 
1097 static struct target_thread *
1098 gdb_lookup_overlay_thread_by_name(struct target *target,char *name) {
1099  struct target_thread *retval = NULL;
1100  struct target_thread *tthread;
1101  int slen;
1102  int rc;
1103  GHashTableIter iter;
1104 
1105  if ((rc = gdb_load_available_threads(target,0)))
1106  vwarn("could not load %d threads; continuing anyway!\n",-rc);
1107 
1108  g_hash_table_iter_init(&iter,target->threads);
1109  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
1110  if (tthread == target->global_thread)
1111  continue;
1112 
1113  if (!tthread->name) {
1114  vwarn("tid %d does not have a name; continuing!\n",
1115  tthread->tid);
1116  continue;
1117  }
1118 
1119  slen = strlen(tthread->name);
1121  "checking task with name '%*s' against '%s'\n",
1122  slen,tthread->name,name);
1123  if (strncmp(name,tthread->name,slen) == 0) {
1124  retval = tthread;
1125  break;
1126  }
1127  }
1128 
1129  if (retval) {
1130  if (target_os_thread_is_user(target,retval->tid) != 1) {
1131  verror("tid %d matched '%s', but is a kernel thread!\n",
1132  retval->tid,name);
1133  errno = EINVAL;
1134  return NULL;
1135  }
1136  else {
1138  "found overlay thread %"PRIiTID"\n",retval->tid);
1139  return tthread;
1140  }
1141  }
1142  else {
1143  errno = ESRCH;
1144  return NULL;
1145  }
1146 }
1147 
1148 static int gdb_attach_overlay_thread(struct target *base,struct target *overlay,
1149  tid_t newtid) {
1150  tid_t cltid,nltid;
1151 
1152  nltid = target_os_thread_get_leader(base,newtid);
1153  cltid = target_os_thread_get_leader(base,overlay->base_thread->tid);
1154 
1155  if (nltid == -1 || cltid == -1)
1156  return -1;
1157 
1158  if (nltid == cltid)
1159  return 0;
1160 
1161  errno = EINVAL;
1162  return 1;
1163 }
1164 
1165 static int gdb_detach_overlay_thread(struct target *base,struct target *overlay,
1166  tid_t tid) {
1167  return 0;
1168 }
1169 
1170 static int __gdb_in_userspace(struct target *target,int cpl,REGVAL ipval) {
1171  struct gdb_state *xstate = (struct gdb_state *)target->state;
1172 
1173  /*
1174  * This is a real pain. We have to use kernel_start_addr because on
1175  * at least some Xen hypervisors, %cs is zeroed out, so we cannot
1176  * extract the CPL. From my reading of the x86 and amd64 manuals,
1177  * it should not be zeroed out -- the segment selector registers are
1178  * only used for privilege levels in long mode.
1179  */
1180  if (xstate->kernel_start_addr && xstate->kernel_start_addr < ADDRMAX) {
1181  if (ipval < xstate->kernel_start_addr)
1182  return 1;
1183  else
1184  return 0;
1185  }
1186  else {
1187  if (cpl == 3)
1188  return 1;
1189  else
1190  return 0;
1191  }
1192 }
1193 
1194 static int __gdb_get_cpl_thread(struct target *target,
1195  struct target_thread *tthread) {
1196  REG csr = -1;
1197  REGVAL cs;
1198 
1199  if (target->arch->type == ARCH_X86)
1200  csr = REG_X86_CS;
1201  else if (target->arch->type == ARCH_X86_64)
1202  csr = REG_X86_64_CS;
1203 
1204  /* Load the CPL. */
1205  errno = 0;
1206  cs = 0x3 & target_read_reg(target,tthread->tid,csr);
1207  if (errno) {
1208  verror("could not read CS register to find CPL!\n");
1209  return -1;
1210  }
1211 
1212  return (int)cs;
1213 }
1214 
1215 static int __gdb_get_cpl(struct target *target,tid_t tid) {
1216  struct target_thread *tthread;
1217 
1218  if (!(tthread = __gdb_load_cached_thread(target,tid))) {
1219  if (!errno)
1220  errno = EINVAL;
1221  verror("could not load cached thread %"PRIiTID"\n",tid);
1222  return 0;
1223  }
1224 
1225  return __gdb_get_cpl_thread(target,tthread);
1226 }
1227 
1228 static target_status_t gdb_handle_exception(struct target *target,
1230  int *again,void *priv) {
1231  struct gdb_state *gstate = (struct gdb_state *)target->state;
1232  struct gdb_rsp_stop_status *ss =
1233  (struct gdb_rsp_stop_status *)&gstate->last_stop_status;
1234  target_status_t tstatus;
1235  REGVAL ipval;
1236  int cpl;
1237  tid_t tid;
1238  struct target_thread *tthread;
1239  struct probepoint *pp;
1240  int rc;
1241 
1242  tstatus = gdb_status(target);
1243  if (tstatus == TSTATUS_ERROR) {
1244  vwarn("failed to load status for GDB stub (target %s);"
1245  " returning to user!\n",target->name);
1246  goto out_err;
1247  }
1248  else if (tstatus == TSTATUS_UNKNOWN) {
1249  vwarn("unknown status for GDB stub (target %s);"
1250  " returning to user!\n",target->name);
1251  goto out_err;
1252  }
1253  else if (tstatus == TSTATUS_RUNNING) {
1254  vwarn("%s is running; ignoring\n",target->name);
1255  if (again)
1256  *again = 0;
1257  return tstatus;
1258  }
1259  else if (tstatus == TSTATUS_PAUSED) {
1260  vdebug(3,LA_TARGET,LF_GDB,"new debug event\n");
1261 
1262  target->monitorhandling = 1;
1263 
1264  /* Force current thread to be reloaded. */
1265  target->current_thread = NULL;
1266 
1267  /*
1268  * Load the global thread (machine state) very first... we have
1269  * to be able to read some register state!
1270  */
1271  if (!__gdb_load_current_thread(target,0,1)) {
1272  verror("could not load global thread!\n");
1273  goto out_err;
1274  }
1275 
1276  /*
1277  * Grab EIP and CPL first so we can see if we're in user or
1278  * kernel space and print better messages.
1279  */
1280  errno = 0;
1281  cpl = __gdb_get_cpl(target,TID_GLOBAL);
1282  if (errno) {
1283  verror("could not read CPL while checking debug event: %s\n",
1284  strerror(errno));
1285  goto out_err;
1286  }
1287  ipval = target_read_reg(target,TID_GLOBAL,target->ipregno);
1288  if (errno) {
1289  verror("could not read EIP while checking debug event: %s\n",
1290  strerror(errno));
1291  goto out_err;
1292  }
1293 
1294  /*
1295  * Give the personality a chance to update its state.
1296  */
1297  SAFE_PERSONALITY_OP_WARN_NORET(handle_exception,rc,0,target,flags);
1298 
1299  /*
1300  * Give the hops a chance to update.
1301  */
1302  if (gstate->hops && gstate->hops->handle_exception_ours) {
1303  gstate->hops->handle_exception_ours(target);
1304  }
1305 
1306  /*
1307  * Reload the current thread. We don't force it because we
1308  * flush all threads before continuing the loop via again:,
1309  * or in target_resume/target_singlestep.
1310  */
1311  gdb_load_current_thread(target,0);
1312 
1313  tthread = target->current_thread;
1314 
1315  if (!tthread) {
1316  verror("could not load current thread!\n");
1317  goto out_err_again;
1318  }
1319 
1320  tid = target->current_thread->tid;
1321 
1322  if (__gdb_in_userspace(target,cpl,ipval)) {
1324  "user-mode debug event at EIP 0x%"PRIxADDR" in tid %"PRIiTID
1325  "; will try to handle it!\n",ipval,tid);
1326  }
1327 
1329  "thread %d at IP 0x%"PRIxADDR"\n",tid,ipval);
1330  }
1331  else {
1332  verror("bad GDB status %d; aborting!\n",tstatus);
1333  goto out_err;
1334  }
1335 
1336  /*
1337  * Handle the exception! We loaded everything we needed above...
1338  */
1339  switch (ss->reason) {
1340  case GDB_RSP_STOP_UNKNOWN:
1341  case GDB_RSP_STOP_NONE:
1342  return TSTATUS_ERROR;
1343  case GDB_RSP_STOP_SIGNAL:
1344  if (ss->signal == SIGTRAP) {
1345  /*
1346  * Must be a single step -- wait -- how do we know? I guess
1347  * for now, it'll be whether or not the event happened at a
1348  * breakpoint, and if the IP is the breakpoint. Anything
1349  * else we'll have to assume is a single step. Then if we
1350  * can't find a thread that is stepping, maybe it's an
1351  * exception somewhere else.
1352  */
1353  vdebug(9,LA_TARGET,LF_GDB,"checking for breakpoint hit\n");
1354 
1355  pp = (struct probepoint *) \
1356  g_hash_table_lookup(tthread->hard_probepoints,(gpointer)ipval);
1357  if (pp) {
1359  "found hw break on 0x%"PRIxADDR"\n",ipval);
1360  }
1361  else {
1362  pp = (struct probepoint *) \
1363  g_hash_table_lookup(target->soft_probepoints,
1364  (gpointer)ipval);
1365  if (pp) {
1367  "found sw break on 0x%"PRIxADDR"\n",ipval);
1368  }
1369  else {
1371  "did not find hw/sw break; must be step!\n");
1372  }
1373  }
1374 
1375  /*
1376  * Handle the breakpoint, if there was one:
1377  */
1378  if (pp) {
1379  target->ops->handle_break(target,tthread,pp,0);
1380  goto out_bp_again;
1381  }
1382 
1383  /*
1384  * Ok, hunt down the single step -- if we can!
1385  */
1386  if (target->sstep_thread == tthread) {
1387  vdebug(5,LA_TARGET,LF_GDB,"assuming single step in tid %d\n",
1388  tthread->tid);
1389 
1390  pp = tthread->tpc->probepoint;
1391 
1392  target->ops->handle_step(target,tthread,pp);
1393 
1394  goto out_ss_again;
1395  }
1396 
1397  /*
1398  * Finally, if there's not a single step,
1399  */
1400  vwarn("could not find hardware bp and not sstep'ing;"
1401  " letting user handle fault at 0x%"PRIxADDR"!\n",ipval);
1402  goto out_paused;
1403  }
1404  else {
1405  vwarn("target %s signaled unexpectedly with %lu; ignoring!\n",
1406  target->name,ss->signal);
1407  }
1408  break;
1409  case GDB_RSP_STOP_WATCH:
1410  case GDB_RSP_STOP_RWATCH:
1411  case GDB_RSP_STOP_AWATCH:
1412  vdebug(5,LA_TARGET,LF_GDB,"watchpoint 0x%"PRIxADDR"\n",ss->addr);
1413  break;
1414  case GDB_RSP_STOP_LIBRARY:
1415  vdebug(5,LA_TARGET,LF_GDB,"library notification not supported!\n");
1416  break;
1418  vdebug(5,LA_TARGET,LF_GDB,"replaylog notification not supported!\n");
1419  break;
1420  case GDB_RSP_STOP_EXITED:
1421  vdebug(5,LA_TARGET,LF_GDB,"exited notification not supported!\n");
1422  break;
1424  vdebug(5,LA_TARGET,LF_GDB,"termination notification not supported!\n");
1425  break;
1426  default:
1427  verror("bad GDB stop status %d; aborting!\n",ss->reason);
1428  break;
1429  }
1430 
1431  out_err:
1432  target->monitorhandling = 0;
1433  if (again)
1434  *again = 0;
1435  return TSTATUS_ERROR;
1436 
1437  out_err_again:
1438  target->monitorhandling = 0;
1439  if (again)
1440  *again = -1;
1441  return TSTATUS_ERROR;
1442 
1443  out_paused:
1444  target->monitorhandling = 0;
1445  if (again)
1446  *again = 0;
1447  return TSTATUS_PAUSED;
1448 
1449  out_bp_again:
1450  target->monitorhandling = 0;
1451  if (again)
1452  *again = 1;
1453  return TSTATUS_PAUSED;
1454 
1455  out_ss_again:
1456  target->monitorhandling = 0;
1457  if (again)
1458  *again = 2;
1459  return TSTATUS_PAUSED;
1460 }
1461 
1462 static target_status_t gdb_status(struct target *target) {
1463  struct gdb_state *gstate = (struct gdb_state *)target->state;
1465 
1466  if (gstate->rsp_status_valid) {
1468  "current GDB stub (%s) status is valid\n",target->name);
1469  return target->status;
1470  }
1471 
1472  retval = gdb_rsp_load_status(target);
1473  if (retval == TSTATUS_ERROR) {
1474  verror("could not load status for target %s\n",target->name);
1475  return retval;
1476  }
1477 
1478  gstate->rsp_status_valid = 1;
1479 
1480  vdebug(9,LA_TARGET,LF_GDB,"target %s status %d\n",target->name,retval);
1481 
1482  return retval;
1483 }
1484 
1485 static int gdb_pause(struct target *target,int nowait) {
1486  struct gdb_state *gstate = (struct gdb_state *)target->state;
1487  struct timeval check_tv = { 0,0};
1488  target_poll_outcome_t outcome;
1489  int pstatus;
1490  target_status_t status;
1491 
1492  vdebug(5,LA_TARGET,LF_GDB,"target %s\n",target->name);
1493 
1494  status = gdb_status(target);
1495  if (status == TSTATUS_ERROR)
1496  vwarn("failed to load status for GDB stub (target %s);"
1497  " trying to pause anyway!\n",target->name);
1498  else if (status == TSTATUS_UNKNOWN)
1499  vwarn("unknown status for GDB stub (target %s);"
1500  " trying to pause anyway!\n",target->name);
1501 
1502  if (gdb_rsp_pause(target)) {
1503  verror("could not pause target %s!\n",target->name);
1504  return -1;
1505  }
1506 
1507  /*
1508  * Give the hops a chance to handle pause.
1509  */
1510  if (gstate->hops && gstate->hops->handle_pause) {
1511  gstate->hops->handle_pause(target);
1512  }
1513 
1515 
1516  gstate->rsp_status_valid = 0;
1517  status = gdb_rsp_load_status(target);
1518  if (status == TSTATUS_UNKNOWN || status == TSTATUS_ERROR)
1519  vwarn("could not reload GDB stub status target %s after pause!\n",
1520  target->name);
1521  else
1522  gstate->rsp_status_valid = 1;
1523 
1524  /*
1525  * NB: very important.
1526  *
1527  * Since we allow pauses to be commanded asynchronously
1528  * w.r.t. target vm execution state, we have to check if there is
1529  * something to handle once we successfully pause it, and handle it
1530  * if so. Otherwise if a target_pause() and debug exception happen
1531  * at the "same" time relative to the user, we might leave a debug
1532  * event unhandled, and this could whack the target.
1533  *
1534  * We pass in a 0,0 timeval so that the select() in gdb_poll
1535  * truly polls.
1536  *
1537  * Also note that we don't care what the outcome is.
1538  */
1539  gdb_poll(target,&check_tv,&outcome,&pstatus);
1540 
1541  return 0;
1542 }
1543 
1544 static int __gdb_resume(struct target *target,int detaching) {
1545  struct gdb_state *gstate = (struct gdb_state *)target->state;
1546  int rc;
1547  target_status_t status;
1548 
1549  vdebug(5,LA_TARGET,LF_GDB,"target %s\n",target->name);
1550 
1551  status = gdb_status(target);
1552  if (status == TSTATUS_ERROR)
1553  vwarn("failed to load status for GDB stub (target %s);"
1554  " trying to resume anyway!\n",target->name);
1555  else if (status == TSTATUS_UNKNOWN)
1556  vwarn("unknown status for GDB stub (target %s);"
1557  " trying to resume anyway!\n",target->name);
1558 
1559  if (target_status(target) != TSTATUS_PAUSED) {
1560  vwarn("not paused; not invalidating and resuming; BUG?\n");
1561  return -1;
1562  }
1563 
1564  /*
1565  REGVAL ipval = target_read_reg(target,TID_GLOBAL,target->ipregno);
1566  target_write_reg(target,TID_GLOBAL,target->ipregno,ipval);
1567  */
1568 
1569  /*
1570  * Only call this if we have threads still, or we are not detaching;
1571  * if we're detaching and the target_api has already deleted our
1572  * threads, flush_all_threads will end up loading at least the
1573  * global thread... which is counterproductive.
1574  */
1575  if (!detaching
1576  || g_hash_table_size(target->threads) || target->global_thread) {
1577  /* Flush back registers if they're dirty! */
1578  target_flush_all_threads(target);
1579 
1580  /* Invalidate our cached copies of threads. */
1582  }
1583 
1584  /*
1585  * Flush machine state via gdb_rsp_write_regs .
1586  */
1587  if ((rc = regcache_isdirty(gstate->machine))) {
1589  "machine dirty; writing %d regs!\n",rc);
1590 
1591  rc = gdb_rsp_write_regs(target,gstate->machine);
1592  if (rc) {
1593  verror("could not write CPU regs!\n");
1595  return rc;
1596  }
1597  }
1598  else {
1600  "machine state not dirty; not writing regs!\n");
1601  }
1602 
1603  regcache_mark_flushed(gstate->machine);
1604  regcache_invalidate(gstate->machine);
1605  gstate->machine_valid = 0;
1606 
1607  if (gstate->stepping)
1608  rc = gdb_rsp_step(target);
1609  else
1610  rc = gdb_rsp_resume(target);
1611  if (rc) {
1612  if (gstate->fd < 0) {
1613  verror("cannot resume; disconnected!\n");
1615  return rc;
1616  }
1617  }
1618 
1620 
1621  return rc;
1622 }
1623 
1624 static int gdb_resume(struct target *target) {
1625  return __gdb_resume(target,0);
1626 }
1627 
1628 static target_status_t gdb_monitor(struct target *target) {
1629  struct gdb_state *gstate = (struct gdb_state *)target->state;
1630  int ret;
1631  struct timeval tv;
1632  fd_set inset;
1633  int again;
1634  target_status_t retval;
1635 
1636  while (1) {
1637  if (gstate->fd < 0) {
1638  verror("server disconnected!\n");
1639  return TSTATUS_UNKNOWN;
1640  }
1641 
1642  tv.tv_sec = 0;
1643  tv.tv_usec = 50;
1644  FD_ZERO(&inset);
1645  FD_SET(gstate->fd,&inset);
1646 
1647  /* wait for input from the stub */
1648  ret = select(gstate->fd + 1,&inset,NULL,NULL,&tv);
1649  if (ret == -1) // timeout
1650  continue;
1651 
1652  if (!FD_ISSET(gstate->fd,&inset))
1653  continue; // nothing from stub
1654 
1655  /* we've got something from the stub; let's see what it is! */
1656  ret = gdb_rsp_recv(target,0,0,NULL);
1657 
1658  again = 0;
1659  retval = gdb_handle_exception(target,0,&again,NULL);
1660  if (retval == TSTATUS_ERROR && again == 0) {
1661  target->needmonitorinterrupt = 0;
1662  return retval;
1663  }
1664  else if (target->needmonitorinterrupt) {
1665  target->needmonitorinterrupt = 0;
1666  return TSTATUS_INTERRUPTED;
1667  }
1668 
1669  __gdb_resume(target,0);
1670 
1671  /*
1672  else if (retval == TSTATUS_PAUSED && again == 0)
1673  return retval;
1674  */
1675  /*
1676  if (gdb_load_dominfo(target)) {
1677  vwarn("could not load dominfo for dom %d, trying to unpause anyway!\n",
1678  xstate->id);
1679  __gdb_resume(target,0);
1680  }
1681  else if (xstate->dominfo.paused) {
1682  __gdb_resume(target,0);
1683  }
1684  */
1685  }
1686 
1687  return TSTATUS_ERROR; /* Never hit, just compiler foo */
1688 }
1689 
1690 static target_status_t gdb_poll(struct target *target,struct timeval *tv,
1691  target_poll_outcome_t *outcome,int *pstatus) {
1692  struct gdb_state *xstate = (struct gdb_state *)target->state;
1693  int ret, fd;
1694  struct timeval itv;
1695  fd_set inset;
1696  int again;
1697  target_status_t retval;
1698 
1699  fd = xstate->fd;
1700 
1701  if (!tv) {
1702  itv.tv_sec = 0;
1703  itv.tv_usec = 0;
1704  tv = &itv;
1705  }
1706  FD_ZERO(&inset);
1707  FD_SET(fd,&inset);
1708 
1709  ret = select(fd+1,&inset,NULL,NULL,tv);
1710  if (ret == 0) {
1711  if (outcome)
1712  *outcome = POLL_NOTHING;
1713  return TSTATUS_RUNNING;
1714  }
1715 
1716  if (!FD_ISSET(fd, &inset)) {
1717  if (outcome)
1718  *outcome = POLL_NOTHING;
1719  return TSTATUS_RUNNING;
1720  }
1721 
1722  again = 0;
1723  retval = gdb_handle_exception(target,0,&again,NULL);
1724  if (pstatus)
1725  *pstatus = again;
1726 
1727  return retval;
1728 }
1729 
1730 int gdb_evloop_handler(int readfd,int fdtype,void *state) {
1731  struct target *target = (struct target *)state;
1732  int again;
1733  int retval;
1734 
1735  again = 0;
1736  retval = gdb_handle_exception(target,0,&again,NULL);
1737  if (retval == TSTATUS_ERROR && again == 0)
1738  return EVLOOP_HRET_ERROR;
1739  /*
1740  * XXX: this is the "abort to user handler" case -- but in this
1741  * case, we have no user, basically. Fix this.
1742  */
1743  //else if (retval == TSTATUS_PAUSED && again == 0)
1744  // return EVLOOP_HRET_SUCCESS;
1745 
1746  __gdb_resume(target,0);
1747 
1748  return EVLOOP_HRET_SUCCESS;
1749 }
1750 
1751 int gdb_attach_evloop(struct target *target,struct evloop *evloop) {
1752  struct gdb_state *xstate = (struct gdb_state *)target->state;
1753 
1754  if (!target->evloop) {
1755  verror("no evloop attached!\n");
1756  return -1;
1757  }
1758 
1759  xstate->evloop_fd = xstate->fd;
1760 
1762  gdb_evloop_handler,target);
1763 
1765  "added evloop readfd %d event channel\n",xstate->evloop_fd);
1766 
1767  return 0;
1768 }
1769 
1770 static int gdb_detach_evloop(struct target *target) {
1771  struct gdb_state *gstate = (struct gdb_state *)target->state;
1772 
1773  if (gstate->evloop_fd < 0)
1774  return 0;
1775 
1777 
1778  gstate->evloop_fd = -1;
1779 
1780  return 0;
1781 }
1782 
1783 tid_t gdb_gettid(struct target *target) {
1784  struct target_thread *tthread;
1785 
1786  if (target->current_thread && OBJVALID(target->current_thread))
1787  return target->current_thread->tid;
1788 
1789  tthread = gdb_load_current_thread(target,0);
1790  if (!tthread) {
1791  verror("could not load current thread to get TID!\n");
1792  return 0;
1793  }
1794 
1795  return tthread->tid;
1796 }
1797 
1798 void gdb_free_thread_state(struct target *target,void *state) {
1799  if (state)
1800  free(state);
1801 }
1802 
1803 static struct target_thread *__gdb_load_cached_thread(struct target *target,
1804  tid_t tid) {
1805  struct target_thread *tthread;
1806 
1807  tthread = target_lookup_thread(target,tid);
1808  if (!tthread)
1809  return NULL;
1810 
1811  if (!OBJVALID(tthread))
1812  return gdb_load_thread(target,tid,0);
1813 
1814  return tthread;
1815 }
1816 
1817 static struct target_thread *__gdb_load_current_thread(struct target *target,
1818  int force,
1819  int globalonly) {
1820  struct gdb_state *gstate = (struct gdb_state *)target->state;
1821  int rc;
1822  struct target_thread *tthread = NULL;
1823  struct gdb_thread_state *tstate = NULL;
1824  //struct gdb_thread_state *gtstate;
1825  REG rip,rcs;
1826  REGVAL ipval,cs;
1827  //uint64_t pgd = 0;
1828  int cpl;
1829 
1830  /*
1831  * If the global thread has been loaded, and that's all the caller
1832  * wants, and they don't want to force a reload, give them that.
1833  */
1834  if (globalonly && !force
1835  && target->global_thread && OBJVALID(target->global_thread))
1836  return target->global_thread;
1837  /*
1838  * Otherwise, if the current thread is valid, and we're not forcing
1839  * a reload, give them the current thread.
1840  */
1841  else if (!globalonly && !force
1842  && target->current_thread && OBJVALID(target->current_thread))
1843  return target->current_thread;
1844 
1845  if (target_status(target) != TSTATUS_PAUSED) {
1846  verror("target not paused; cannot load current thread!\n");
1847  errno = EBUSY;
1848  return NULL;
1849  }
1850 
1851  /*
1852  * The first thing to do is load the machine state into the global
1853  * thread, and set it as valid -- EVEN THOUGH we have not loaded
1854  * thread_info for it! We must do this so that a whole bunch of
1855  * register reads can work via the API.
1856  */
1857  if (!gstate->machine_valid) {
1858  rc = gdb_rsp_read_regs(target,gstate->machine);
1859  if (rc) {
1860  verror("could not read CPU regs!\n");
1861  return NULL;
1862  }
1863 
1864  gstate->machine_valid = 1;
1865 
1866  /*
1867  * Let the helper ops help!
1868  */
1869  if (gstate->hops && gstate->hops->load_machine) {
1870  rc = gstate->hops->load_machine(target,gstate->machine);
1871  if (rc) {
1872  vwarn("helper ops load_machine failed!\n");
1873  }
1874  }
1875  }
1876 
1877  //gtstate = (struct gdb_thread_state *)target->global_thread->state;
1878 
1879  /*
1880  * Load EIP for info, and CPL for user-mode check. This should work
1881  * out whether we're an OS or a process; in the former case, threads
1882  * will have a context, either kernel or user; in the latter, if the
1883  * stub sets %cs appropriately, CPL will be 3 == user; else, if it
1884  * does not, CPL will be 0 -- but the point is it will be
1885  * consistent. I could also always force the CPL to 3 if we're not
1886  * attached to an OS, but I'd prefer not to do that, so that the
1887  * right thing still happens whether or not there's a personality
1888  * attached.
1889  *
1890  * NB: note that these two calls do *not* go through the target
1891  * API. They cannot, because the global thread has not been loaded
1892  * yet. And we can't finish loading the global thread yet, even
1893  * though we have the machine state, because we don't know which
1894  * thread context's regcache to put the machine state into (kernel
1895  * or userspace).
1896  */
1897  errno = 0;
1898  if (target->arch->type == ARCH_X86_64) {
1899  rip = REG_X86_64_RIP;
1900  rcs = REG_X86_64_CS;
1901  }
1902  else {
1903  rip = REG_X86_EIP;
1904  rcs = REG_X86_CS;
1905  }
1906 
1907  rc = regcache_read_reg(gstate->machine,rip,&ipval);
1908  if (rc) {
1909  verror("could not read IP from machine state!\n");
1910  goto errout;
1911  }
1912  rc = regcache_read_reg(gstate->machine,rcs,&cs);
1913  if (rc) {
1914  verror("could not read IP from machine state!\n");
1915  goto errout;
1916  }
1917 
1918  cpl = 0x3 & cs;
1919 
1920  /* Keep loading the global thread... */
1921  if (!OBJVALID(target->global_thread)) {
1922  if (__gdb_in_userspace(target,cpl,ipval))
1924  else
1926 
1927  /*
1928  * Push the registers into the regcache!
1929  */
1931  target->global_thread->tidctxt,
1932  gstate->machine);
1933 
1934  /*
1935  * Very important. If thread is in userspace, we need to get
1936  * Xen's special kernel_sp register and set it as SP for the
1937  * kernel context so that personalities can load kernel threads
1938  * on i386 because they need kernel_sp to find the stack. On
1939  * x86_64 this is not necessary.
1940  */
1941  if (target->global_thread->tidctxt == THREAD_CTXT_USER
1942  && target->personality == TARGET_PERSONALITY_OS) {
1943  vwarn("not supported yet!!!\n");
1944  /*
1945  target_regcache_init_reg_tidctxt(target,target->global_thread,
1946  THREAD_CTXT_KERNEL,target->spregno,
1947  gtstate->context.kernel_sp);
1948  */
1949  }
1950 
1951  /*
1952  * NB: we must set the thread as valid now, because the next few
1953  * function calls are going to try to use the target API to read
1954  * registers from the global thread. So even through we're
1955  * technically still loading it, mark it as valid now... it'll
1956  * be fully valid shortly!
1957  */
1958  OBJSVALID(target->global_thread);
1960  }
1961 
1962  /*
1963  * Load CR3 for debug purposes.
1964  *
1965  * Can't do this from GDB with only gp regs!
1966  */
1967  /*
1968  __gdb_vm_pgd(target,TID_GLOBAL,&pgd);
1969 
1970  vdebug(9,LA_TARGET,LF_GDB,
1971  "loading current thread (ip = 0x%"PRIxADDR",pgd = 0x%"PRIxADDR","
1972  "cpl = %d,tidctxt = %d)\n",ipval,pgd,cpl,
1973  target->global_thread->tidctxt);
1974  */
1975 
1976  /*
1977  * If only loading the global thread, stop here.
1978  */
1979  if (globalonly)
1980  return target->global_thread;
1981 
1982  /*
1983  * Ask the personality to detect our current thread.
1984  */
1985  SAFE_PERSONALITY_OP(load_current_thread,tthread,NULL,target,force);
1986 
1987  /*
1988  * Set the current thread (might be a real thread, or the global
1989  * thread). If the personality detects a current thread, use it;
1990  * otherwise we have to just use the global thread!
1991  */
1992  if (tthread) {
1993  target->current_thread = tthread;
1994 
1995  /*
1996  * We want to set the current thread's context to whatever the
1997  * global thread was detected to be in. Enforce our will, no
1998  * matter what the personality does!
1999  */
2000  if (tthread->tidctxt != target->global_thread->tidctxt) {
2001  vwarn("personality set current thread context to %d; global thread"
2002  " context is %d; forcing current to global!\n",
2003  tthread->tidctxt,target->global_thread->tidctxt);
2004  tthread->tidctxt = target->global_thread->tidctxt;
2005  }
2006 
2007  /*
2008  * Now, copy in the machine state. Be careful -- if we have not
2009  * allocated tthread->state yet, allocate it now!
2010  */
2011  tstate = (struct gdb_thread_state *)tthread->state;
2012  if (!tstate)
2013  tthread->state = tstate = \
2014  (struct gdb_thread_state *)calloc(1,sizeof(*tstate));
2015 
2016  /* Also update the regcache for the current thread. */
2018  target->global_thread->tidctxt,
2019  tthread,tthread->tidctxt);
2020  }
2021  else
2022  target->current_thread = target->global_thread;
2023 
2025 
2027  "loaded current thread %d\n",target->current_thread->tid);
2028 
2029  /* Mark its state as valid in our cache. */
2030  OBJSVALID(tthread);
2031 
2032  return tthread;
2033 
2034  errout:
2035  /* XXX: should we really set this here? */
2036  target->current_thread = target->global_thread;
2037 
2038  vwarn("error loading current thread; trying to use default thread\n");
2039  errno = 0;
2040 
2041  return target->global_thread;
2042 }
2043 
2044 static struct target_thread *gdb_load_current_thread(struct target *target,
2045  int force) {
2046  return __gdb_load_current_thread(target,force,0);
2047 }
2048 
2049 static struct target_thread *gdb_load_thread(struct target *target,
2050  tid_t tid,int force) {
2051  struct target_thread *tthread = NULL;
2052 
2053  /*
2054  * If we are asking for the global thread (TID_GLOBAL), do that
2055  * right away.
2056  */
2057  if (tid == TID_GLOBAL) {
2058  /*
2059  * We have to *not* call _load_current_thread if the global
2060  * thread is valid. This is part of a hack (chicken and egg)
2061  * problem where to "fully" load the global thread, we must have
2062  * its registers. Our register read functions try to load the
2063  * current thread if it's not loaded. So... see
2064  * _load_current_thread for more...
2065  */
2066  if (OBJVALID(target->global_thread))
2067  return target->global_thread;
2068  else {
2069  gdb_load_current_thread(target,force);
2070  return target->global_thread;
2071  }
2072  }
2073 
2074  /*
2075  * If we haven't loaded current_thread yet, we really should load it
2076  * because otherwise we don't know if current_thread->tid == @tid.
2077  * If it does, we don't want to do the below stuff, which only
2078  * applies to non-running threads.
2079  */
2080  if (!gdb_load_current_thread(target,force)) {
2081  vwarn("could not load current thread to compare with"
2082  " tid %"PRIiTID"!\n",tid);
2083  }
2084 
2085  /*
2086  * If the thread tid we are asking for is the current thread and is
2087  * valid, or if the thread is in our cache and is valid.
2088  */
2089  else if (target->current_thread
2090  && OBJVALID(target->current_thread)
2091  && target->current_thread->tid == tid) {
2092  return gdb_load_current_thread(target,force);
2093  }
2094  /*
2095  * Otherwise, try to lookup thread @tid.
2096  */
2097  else if ((tthread = target_lookup_thread(target,tid))) {
2098  if (OBJVALID(tthread) && !force) {
2100  "did not need to load thread; copy is valid\n");
2101  return tthread;
2102  }
2103  }
2104 
2105  /*
2106  * Note:
2107  *
2108  * At this point, we can be sure that we are loading a thread that
2109  * is not running; thus, its CPU state is on the kernel stack.
2110  *
2111  * This means we must ask the personality to do it, because only the
2112  * personality can interpret the kernel stack.
2113  */
2114  SAFE_PERSONALITY_OP_WARN_NORET(load_thread,tthread,NULL,target,tid,force);
2115 
2116  return tthread;
2117 }
2118 
2119 static struct array_list *gdb_list_available_tids(struct target *target) {
2120  struct array_list *retval;
2121  SAFE_PERSONALITY_OP(list_available_tids,retval,NULL,target);
2122  return retval;
2123 }
2124 
2125 static int gdb_load_all_threads(struct target *target,int force) {
2126  struct array_list *cthreads;
2127  int rc = 0;
2128  int i;
2129  struct target_thread *tthread;
2130 
2131  cthreads = target_list_threads(target);
2132 
2133  for (i = 0; i < array_list_len(cthreads); ++i) {
2134  tthread = (struct target_thread *)array_list_item(cthreads,i);
2135 
2137  "tid %"PRIiTID" (%p)\n",tthread->tid,tthread);
2138 
2139  if (!gdb_load_thread(target,tthread->tid,force)) {
2140  if (target_lookup_thread(target,tthread->tid)) {
2141  verror("could not load thread %"PRIiTID"\n",tthread->tid);
2142  --rc;
2143  continue;
2144  }
2145  /*
2146  * If it's no longer in the cache, we evicted it because it
2147  * no longer exists... so this is not an error.
2148  */
2149  }
2150  }
2151 
2152  return rc;
2153 
2154 }
2155 
2156 static int gdb_load_available_threads(struct target *target,int force) {
2157  int rc;
2158 
2159  /*
2160  * Load the current thread first to load the global thread. The
2161  * current thread will get loaded again in the loop below if @force
2162  * is set...
2163  */
2164  if (!__gdb_load_current_thread(target,force,1)) {
2165  verror("could not load current thread!\n");
2166  return -1;
2167  }
2168 
2169  SAFE_PERSONALITY_OP(load_available_threads,rc,0,target,force);
2170  return rc;
2171 }
2172 
2173 /*
2174  * This only flushes CPU state, from the global thread, to the machine.
2175  *
2176  * NB: we don't write the machine until we unpause the target!
2177  */
2178 static int gdb_flush_global_thread(struct target *target,
2179  struct target_thread *current_thread) {
2180  struct target_thread *tthread;
2181  struct gdb_state *xstate = (struct gdb_state *)(target->state);
2182  tid_t tid;
2183  REGVAL ipval;
2184 
2185  tthread = target->global_thread;
2186  tid = tthread->tid;
2187 
2188  if (!tthread) {
2189  verror("global thread not loaded; BUG!!!\n");
2190  errno = EINVAL;
2191  return -1;
2192  }
2193 
2194  vdebug(5,LA_TARGET,LF_GDB,"%s tid %"PRIiTID"\n",target->name,tid);
2195 
2196  if (!OBJVALID(tthread) || !OBJDIRTY(tthread)) {
2198  "%s tid %"PRIiTID" not valid (%d) or not dirty (%d)\n",
2199  target->name,tid,OBJVALID(tthread),OBJDIRTY(tthread));
2200  return 0;
2201  }
2202 
2203  ipval = target_read_reg(target,tid,target->ipregno);
2205  "EIP is 0x%"PRIxREGVAL" before flush (%s tid %"PRIiTID")\n",
2206  ipval,target->name,tid);
2207 
2208  /*
2209  * Just copy the dirty registers from current to gstate->machine.
2210  */
2211  if (target_regcache_copy_dirty_to(tthread,tthread->tidctxt,
2212  xstate->machine)) {
2213  verror("failed to copy dirty regs from %s tid %d tidctxt %d to machine!\n",
2214  target->name,tid,target->current_thread->tidctxt);
2215  errno = EINVAL;
2216  return -1;
2217  }
2218 
2219  if (target_regcache_mark_flushed(target,tthread,tthread->tidctxt)) {
2220  vwarn("failed to mark %s tid %d tidctxt %d flushed!\n",
2221  target->name,tid,tthread->tidctxt);
2222  }
2223 
2224  /* Mark cached copy as clean. */
2225  OBJSCLEAN(tthread);
2226 
2227  return 0;
2228 }
2229 
2230 static int gdb_flush_current_thread(struct target *target) {
2231  struct gdb_state *xstate = (struct gdb_state *)(target->state);
2232  struct target_thread *tthread;
2233  tid_t tid;
2234  int rc;
2235 
2236  if (!target->current_thread) {
2237  verror("current thread not loaded!\n");
2238  errno = EINVAL;
2239  return -1;
2240  }
2241 
2242  /* gdb_flush_global_thread must be called to handle this. */
2243  if (target->current_thread == target->global_thread)
2244  return 0;
2245 
2246  tthread = target->current_thread;
2247  tid = tthread->tid;
2248 
2249  vdebug(5,LA_TARGET,LF_GDB,"%s tid %"PRIiTID"\n",target->name,tid);
2250 
2251  if (!OBJVALID(tthread) || !OBJDIRTY(tthread)) {
2253  "%s tid %"PRIiTID" not valid (%d) or not dirty (%d)\n",
2254  target->name,tid,OBJVALID(tthread),OBJDIRTY(tthread));
2255  return 0;
2256  }
2257 
2259  "EIP is 0x%"PRIxREGVAL" before flush (%s tid %"PRIiTID")\n",
2260  target_read_reg(target,tid,target->ipregno),target->name,tid);
2261 
2262  /*
2263  * Just copy the dirty registers from current to gstate->machine.
2264  */
2265  if (target_regcache_copy_dirty_to(tthread,tthread->tidctxt,
2266  xstate->machine)) {
2267  verror("failed to copy dirty regs from %s tid %d tidctxt %d to machine!\n",
2268  target->name,tid,target->current_thread->tidctxt);
2269  errno = EINVAL;
2270  return -1;
2271  }
2272 
2273  if (target_regcache_mark_flushed(target,tthread,tthread->tidctxt)) {
2274  vwarn("failed to mark %s tid %d tidctxt %d flushed!\n",
2275  target->name,tid,tthread->tidctxt);
2276  }
2277 
2278  SAFE_PERSONALITY_OP(flush_current_thread,rc,0,target);
2279  return rc;
2280 }
2281 
2282 static int gdb_flush_thread(struct target *target,tid_t tid) {
2283  struct target_thread *tthread;
2284  int rc;
2285 
2286  vdebug(16,LA_TARGET,LF_GDB,"%s\n",target->name);
2287 
2288  /*
2289  * If we are flushing the global thread (TID_GLOBAL), do it right
2290  * away.
2291  */
2292  if (tid == TID_GLOBAL)
2293  return gdb_flush_current_thread(target);
2294 
2295  /*
2296  * If we haven't loaded current_thread yet, we really should load it
2297  * because otherwise we don't know if current_thread->tid == @tid.
2298  * If it does, we don't want to do the below stuff, which only
2299  * applies to non-running threads -- in this case, we want to flush
2300  * to the hardware directly.
2301  *
2302  * BUT -- we can't load a thread in the flush code; we might be
2303  * iterating over the threads hashtable, so a load might result in
2304  * a thread create which would result in the hashtable being
2305  * modified.
2306  */
2307  if (!target->current_thread) {
2309  "current thread not loaded to compare with"
2310  " tid %"PRIiTID"; exiting, user-mode EIP, or BUG?\n",
2311  tid);
2312  }
2313  else if (!OBJVALID(target->current_thread)) {
2315  "current thread not valid to compare with"
2316  " tid %"PRIiTID"; exiting, user-mode EIP, or BUG?\n",
2317  tid);
2318  }
2319 
2320  /*
2321  * If the thread tid we are asking for is the current thread and is
2322  * valid, or if the thread is in our cache and is valid.
2323  */
2324  if (target->current_thread && target->current_thread->tid == tid) {
2325  return gdb_flush_current_thread(target);
2326  }
2327  /*
2328  * Otherwise, try to lookup thread @tid.
2329  */
2330  tthread = target_lookup_thread(target,tid);
2331 
2332  if (!tthread) {
2333  verror("cannot flush unknown thread %"PRIiTID"; you forgot to load?\n",
2334  tid);
2335  errno = EINVAL;
2336  return -1;
2337  }
2338 
2339  if (tthread == target->current_thread)
2340  return gdb_flush_current_thread(target);
2341 
2342  if (!OBJVALID(tthread) || !OBJDIRTY(tthread)) {
2344  "%s tid %"PRIiTID" not valid (%d) or not dirty (%d)\n",
2345  target->name,tthread->tid,OBJVALID(tthread),OBJDIRTY(tthread));
2346  return 0;
2347  }
2348 
2349  SAFE_PERSONALITY_OP(flush_thread,rc,0,target,tid);
2350  if (rc)
2351  goto errout;
2352 
2353  OBJSCLEAN(tthread);
2354 
2355  return 0;
2356 
2357  errout:
2358  return -1;
2359 }
2360 
2361 static int gdb_flush_all_threads(struct target *target) {
2362  int rc, retval = 0;
2363  GHashTableIter iter;
2364  struct target_thread *tthread;
2365  struct target_thread *current_thread = NULL;
2366 
2367  g_hash_table_iter_init(&iter,target->threads);
2368  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
2369  if (tthread == target->current_thread
2370  || tthread == target->global_thread)
2371  continue;
2372  else
2373  rc = gdb_flush_thread(target,tthread->tid);
2374  if (rc) {
2375  verror("could not flush thread %"PRIiTID"\n",tthread->tid);
2376  ++retval;
2377  }
2378  }
2379 
2380  /*
2381  * If the current thread is not the global thread, we have to try to
2382  * flush it.
2383  */
2384  if (target->current_thread
2385  && target->current_thread != target->global_thread) {
2386  /* Save this off to tell flush_global_thread below that
2387  * it must merge its state with this thread's state.
2388  *
2389  * So if the current thread is not the global thread itself, and
2390  * its state is valid (whether it is dirty or not!!), we must
2391  * merge.
2392  */
2393  if (OBJVALID(target->current_thread))
2394  current_thread = target->current_thread;
2395 
2396  rc = gdb_flush_current_thread(target);
2397  if (rc) {
2398  verror("could not flush current thread %"PRIiTID"\n",
2399  target->current_thread->tid);
2400  ++retval;
2401  }
2402  }
2403 
2404  /*
2405  * Also, we always have to try to flush the "global" thread.
2406  * Remember, the global thread is a fake thread; it never maps to
2407  * anything real; it is just the current CPU registers. If the user
2408  * sets any probes or modifies registers with TID_GLOBAL, they only
2409  * get flushed if we flush the global thread.
2410  *
2411  * OF COURSE, this means that if you mix per-thread probing/register
2412  * modification and global thread modification, your changes to the
2413  * current hardware state will almost certainly stomp on each
2414  * other. OK, this is no longer permitted; get_unused_debug_reg now
2415  * makes sure this cannot happen.
2416  *
2417  * If we were handling a software breakpoint, we would have modified
2418  * cpu context in the current thread; if we were hanlding a hardware
2419  * probe or modifying a hardware probe, we would have written the
2420  * the global thread's cpu state (AND the current thread's CPU state
2421  * too, like EIP, etc). So what we need to is arbitrate between the
2422  * two contexts depending on what we're doing. For instance, if we
2423  * handled a hardware probepoint, we'll always need to flush the
2424  * global thread -- see monitor() and flush_global_thread().
2425  */
2426  rc = gdb_flush_global_thread(target,current_thread);
2427  if (rc) {
2428  verror("could not flush global thread %"PRIiTID"\n",TID_GLOBAL);
2429  ++retval;
2430  }
2431 
2432  return retval;
2433 }
2434 
2435 static int gdb_invalidate_thread(struct target *target,
2436  struct target_thread *tthread) {
2437  int rc;
2438  SAFE_PERSONALITY_OP(invalidate_thread,rc,0,target,tthread);
2439  return rc;
2440 }
2441 
2442 static int gdb_thread_snprintf(struct target *target,
2443  struct target_thread *tthread,
2444  char *buf,int bufsiz,
2445  int detail,char *sep,char *kvsep) {
2446  int rc = 0;
2447  int nrc;
2448 
2449  if (tthread == target->current_thread || tthread == target->global_thread) {
2450  rc = target_regcache_snprintf(target,tthread,tthread->tidctxt,
2451  buf,bufsiz,detail,sep,kvsep,0);
2452  if (rc < 0)
2453  return rc;
2454  }
2455 
2456  SAFE_PERSONALITY_OP(thread_snprintf,nrc,0,target,tthread,
2457  (rc >= bufsiz) ? NULL : buf + rc,
2458  (rc >= bufsiz) ? 0 : bufsiz - rc,
2459  detail,sep,kvsep);
2460  if (nrc < 0) {
2461  verror("could not snprintf personality info for thread %d!\n",
2462  tthread->tid);
2463  return nrc;
2464  }
2465 
2466  return rc + nrc;
2467 }
2468 
2469 static unsigned char *gdb_read(struct target *target,ADDR addr,
2470  unsigned long length,unsigned char *buf) {
2471  struct gdb_state *xstate;
2472  uint64_t pgd = 0;
2473 
2474  xstate = (struct gdb_state *)target->state;
2475 
2476  if (!xstate->hops || !xstate->hops->read_tid) {
2477  errno = EINVAL;
2478  return 0;
2479  }
2480 
2481  if (__gdb_pgd(target,TID_GLOBAL,&pgd)) {
2482  verror("could not read pgd for tid %"PRIiTID"!\n",TID_GLOBAL);
2483  return NULL;
2484  }
2485 
2486  return xstate->hops->read_tid(target,TID_GLOBAL,pgd,addr,length,buf);
2487 }
2488 
2489 static unsigned long gdb_write(struct target *target,ADDR addr,
2490  unsigned long length,unsigned char *buf) {
2491  struct gdb_state *xstate;
2492  uint64_t pgd = 0;
2493 
2494  xstate = (struct gdb_state *)target->state;
2495 
2496  if (!xstate->hops || !xstate->hops->write_tid) {
2497  errno = EINVAL;
2498  return 0;
2499  }
2500 
2501  if (__gdb_pgd(target,TID_GLOBAL,&pgd)) {
2502  verror("could not read pgd for tid %"PRIiTID"!\n",TID_GLOBAL);
2503  return 0;
2504  }
2505 
2506  return xstate->hops->write_tid(target,TID_GLOBAL,pgd,addr,length,buf);
2507 }
2508 
2509 /*
2510  * We have to either load pgd from vcpu context (for a running task), or
2511  * from the task struct (for a swapped out task).
2512  *
2513  * NB: @cr3 will be a physical address, not a kernel virtual address.
2514  * The mm_struct contains a virtual address; but the CR3 register of
2515  * course contains a physical one. And the CR3 content is not quite a
2516  * physical address, sometimes, it seems.
2517  */
2518 static int __gdb_pgd(struct target *target,tid_t tid,uint64_t *pgd) {
2519  struct gdb_spec *gspec;
2520  struct gdb_state *xstate;
2521  struct target_thread *tthread;
2522  //struct gdb_thread_state *xtstate;
2523  REGVAL cr0 = 0,cr3 = 0,cr4 = 0,msr_efer = 0,cpuid_edx = 0;
2524 
2525  gspec = (struct gdb_spec *)target->spec->backend_spec;
2526  xstate = (struct gdb_state *)target->state;
2527 
2528  if (tid == TID_GLOBAL) {
2529  tthread = __gdb_load_current_thread(target,0,1);
2530  if (!tthread) {
2531  verror("could not load global thread!\n");
2532  return -1;
2533  }
2534  /*
2535  xtstate = (struct gdb_thread_state *)tthread->state;
2536 
2537  if (xtstate->context.vm_assist & (1 << VMASST_TYPE_pae_extended_cr3)) {
2538  *pgd = ((uint64_t)xen_cr3_to_pfn(xtstate->context.ctrlreg[3])) \
2539  << XC_PAGE_SHIFT;
2540  }
2541  else {
2542  *pgd = xtstate->context.ctrlreg[3] & ~(__PAGE_SIZE - 1);
2543  }
2544  */
2545 
2546  /*
2547  * XXX NB: Also load the current paging flags! This seems to be
2548  * the right place to do it... realistically, the flags are not
2549  * going to change much except during boot... or in the future
2550  * where there are nested HVMs! I suppose, in the future, we'll
2551  * have to have these set on a per-thread basis...
2552  *
2553  * (Pass cpuid_edx=REGVALMAX for now to make sure the NOPSE*
2554  * bits don't get set -- until we actually bother to find the
2555  * cpuid info.)
2556  */
2557  if (target->arch->type == ARCH_X86_64) {
2561  regcache_read_reg(xstate->machine,REG_X86_64_MSR_EFER,&msr_efer);
2562  }
2563  else {
2564  regcache_read_reg(xstate->machine,REG_X86_CR0,&cr0);
2565  regcache_read_reg(xstate->machine,REG_X86_CR3,&cr3);
2566  regcache_read_reg(xstate->machine,REG_X86_CR4,&cr4);
2567  regcache_read_reg(xstate->machine,REG_X86_MSR_EFER,&msr_efer);
2568  }
2569  cpuid_edx = ADDRMAX;
2570 
2571  *pgd = cr3;
2572 
2573  if (target_arch_x86_v2p_get_flags(target,cr0,cr4,msr_efer,
2574  cpuid_edx,&xstate->v2p_flags)) {
2575  if (target->arch->type == ARCH_X86_64) {
2576  verror("could not determine v2p_flags! pgd walks might fail;"
2577  " assuming 64-bit long mode and paging!\n");
2578  xstate->v2p_flags = ARCH_X86_V2P_LMA;
2579  }
2580  else {
2581  verror("could not determine v2p_flags! pgd walks might fail;"
2582  " assuming 32-bit mode and PAE (and auto-PSE)!\n");
2583  xstate->v2p_flags = ARCH_X86_V2P_PAE;
2584  }
2585  }
2586 
2587  if (vdebug_is_on(8,LA_TARGET,LF_GDB)) {
2588  char buf[256];
2589  buf[0] = '\0';
2591  buf,sizeof(buf));
2592  vdebug(8,LA_TARGET,LF_TARGET,"v2p_flags = %s\n",buf);
2593  }
2594 
2595  /* Also quickly set the V2P_PV flag if this domain is paravirt. */
2596  if (!gspec->is_kvm)
2597  xstate->v2p_flags |= ARCH_X86_V2P_PV;
2598  }
2599  else {
2600  tthread = gdb_load_thread(target,tid,0);
2601  if (!tthread) {
2602  verror("could not load tid %"PRIiTID"!\n",tid);
2603  return -1;
2604  }
2605 
2606  if (target_os_thread_get_pgd_phys(target,tid,pgd)) {
2607  verror("could not get phys pgd for tid %"PRIiTID": %s!\n",
2608  tid,strerror(errno));
2609  return -1;
2610  }
2611  }
2612 
2613  vdebug(12,LA_TARGET,LF_GDB,
2614  "tid %"PRIiTID" pgd (phys) = 0x%"PRIx64"\n",tid,*pgd);
2615 
2616  return 0;
2617 }
2618 
2619 static int gdb_addr_v2p(struct target *target,tid_t tid,
2620  ADDR vaddr,ADDR *paddr) {
2621  struct gdb_state *xstate;
2622  uint64_t pgd = 0;
2623 
2624  xstate = (struct gdb_state *)target->state;
2625 
2626  if (__gdb_pgd(target,tid,&pgd)) {
2627  verror("could not read pgd for tid %"PRIiTID"!\n",tid);
2628  return -1;
2629  }
2630 
2631  if (!xstate->hops || !xstate->hops->addr_v2p) {
2632  errno = EINVAL;
2633  return -1;
2634  }
2635 
2636  return xstate->hops->addr_v2p(target,tid,pgd,vaddr,paddr);
2637 }
2638 
2639 static unsigned char *gdb_read_phys(struct target *target,ADDR paddr,
2640  unsigned long length,unsigned char *buf) {
2641  struct gdb_state *xstate;
2642 
2643  xstate = (struct gdb_state *)target->state;
2644 
2645  if (!xstate->hops || !xstate->hops->read_phys) {
2646  errno = EINVAL;
2647  return NULL;
2648  }
2649 
2650  return xstate->hops->read_phys(target,paddr,length,buf);
2651 }
2652 
2653 static unsigned long gdb_write_phys(struct target *target,ADDR paddr,
2654  unsigned long length,unsigned char *buf) {
2655  struct gdb_state *xstate;
2656 
2657  xstate = (struct gdb_state *)target->state;
2658 
2659  if (!xstate->hops || !xstate->hops->write_phys) {
2660  errno = EINVAL;
2661  return 0;
2662  }
2663 
2664  return xstate->hops->write_phys(target,paddr,length,buf);
2665 }
2666 
2667 static struct target_memmod *gdb_insert_sw_breakpoint(struct target *target,
2668  tid_t tid,ADDR addr) {
2669  struct target_memmod *mmod;
2670 
2671  /*
2672  * Create a fake memmod before gdb changes memory.
2673  */
2674  mmod = target_memmod_create(target,tid,addr,0,MMT_BP,
2675  target->arch->breakpoint_instrs,
2676  target->arch->breakpoint_instrs_len,1);
2677 
2678  if (gdb_rsp_insert_break(target,addr,GDB_RSP_BREAK_SW,1)) {
2679  verror("could not insert breakpoint!\n");
2680  target_memmod_release(target,tid,mmod);
2681  return NULL;
2682  }
2683 
2684  return mmod;
2685 }
2686 
2687 static int gdb_remove_sw_breakpoint(struct target *target,tid_t tid,
2688  struct target_memmod *mmod) {
2689  if (gdb_rsp_remove_break(target,mmod->addr,GDB_RSP_BREAK_SW,1)) {
2690  verror("could not remove breakpoint!\n");
2691  return -1;
2692  }
2693  else
2694  /* Make sure the no-write mmod is removed. */
2695  return _target_remove_sw_breakpoint(target,tid,mmod);
2696 }
2697 
2698 static int gdb_enable_sw_breakpoint(struct target *target,tid_t tid,
2699  struct target_memmod *mmod) {
2700  if (gdb_rsp_insert_break(target,mmod->addr,GDB_RSP_BREAK_SW,1)) {
2701  verror("could not insert breakpoint!\n");
2702  return -1;
2703  }
2704  else
2705  return _target_enable_sw_breakpoint(target,tid,mmod);
2706 }
2707 
2708 static int gdb_disable_sw_breakpoint(struct target *target,tid_t tid,
2709  struct target_memmod *mmod) {
2710  if (gdb_rsp_remove_break(target,mmod->addr,GDB_RSP_BREAK_SW,1)) {
2711  verror("could not remove breakpoint!\n");
2712  return -1;
2713  }
2714  else
2715  return _target_disable_sw_breakpoint(target,tid,mmod);
2716 }
2717 
2718 int gdb_change_sw_breakpoint(struct target *target,tid_t tid,
2719  struct target_memmod *mmod,
2720  unsigned char *code,unsigned long code_len) {
2721  /*
2722  * GDB (at least QEMU's stub) is persnickety. We have to remove the
2723  * breakpoint first, then change the code, then change it back.
2724  */
2725  if (gdb_rsp_remove_break(target,mmod->addr,GDB_RSP_BREAK_SW,1)) {
2726  vwarn("could not remove breakpoint before change;"
2727  " it may quit working!\n");
2728  }
2729 
2730  target_memmod_set_writeable(target,mmod,1);
2731 
2732  return _target_change_sw_breakpoint(target,tid,mmod,code,code_len);
2733 }
2734 
2735 int gdb_unchange_sw_breakpoint(struct target *target,tid_t tid,
2736  struct target_memmod *mmod) {
2737  _target_unchange_sw_breakpoint(target,tid,mmod);
2738 
2739  target_memmod_set_writeable(target,mmod,0);
2740 
2741  if (gdb_rsp_insert_break(target,mmod->addr,GDB_RSP_BREAK_SW,1)) {
2742  verror("could not insert breakpoint!\n");
2743  return -1;
2744  }
2745  else
2746  return 0;
2747 }
2748 
2749 #define CHECKTIDGLOBAL(tid) \
2750  if ((tid) != TID_GLOBAL) { \
2751  verror("only TID_GLOBAL supported, not tid %d!\n",tid); \
2752  errno = EINVAL; \
2753  return -1; \
2754  }
2755 
2756 #define CHECKTIDGLOBALORCURRENT(_T) \
2757  if ((_T) != TID_GLOBAL \
2758  && (!target->current_thread || target->current_thread->tid != (_T))) { \
2759  verror("only TID_GLOBAL/current tid supported, not tid %d!\n",(_T)); \
2760  errno = EINVAL; \
2761  return -1; \
2762  }
2763 
2764 static int gdb_set_hw_breakpoint(struct target *target,tid_t tid,
2765  REG num,ADDR addr) {
2766  CHECKTIDGLOBAL(tid);
2767  if (gdb_rsp_insert_break(target,addr,GDB_RSP_BREAK_HW,1)) {
2768  verror("could not insert breakpoint!\n");
2769  return -1;
2770  }
2771  else
2772  return 0;
2773 }
2774 
2775 static int gdb_set_hw_watchpoint(struct target *target,tid_t tid,
2776  REG num,ADDR addr,
2777  probepoint_whence_t whence,
2778  probepoint_watchsize_t watchsize) {
2780  int ws = target->arch->wordsize;
2781 
2782  CHECKTIDGLOBAL(tid);
2783 
2784  if (whence == PROBEPOINT_WRITE) bt = GDB_RSP_BREAK_AWATCH;
2785  else if (whence == PROBEPOINT_READWRITE) bt = GDB_RSP_BREAK_RWATCH;
2786 
2787  if (watchsize == PROBEPOINT_L0) ws = 0;
2788  else if (watchsize == PROBEPOINT_L2) ws = 2;
2789  else if (watchsize == PROBEPOINT_L4) ws = 4;
2790  else if (watchsize == PROBEPOINT_L8) ws = 8;
2791 
2792  if (gdb_rsp_insert_break(target,addr,bt,ws)) {
2793  verror("could not insert breakpoint!\n");
2794  return -1;
2795  }
2796  else
2797  return 0;
2798 }
2799 
2800 static int gdb_unset_hw_breakpoint(struct target *target,tid_t tid,REG num) {
2801 
2802 }
2803 
2804 static int gdb_unset_hw_watchpoint(struct target *target,tid_t tid,REG num) {
2805 
2806 }
2807 
2808 static int gdb_disable_hw_breakpoints(struct target *target,tid_t tid) {
2809 
2810 }
2811 
2812 static int gdb_enable_hw_breakpoints(struct target *target,tid_t tid) {
2813 
2814 }
2815 
2816 static int gdb_disable_hw_breakpoint(struct target *target,tid_t tid,REG dreg) {
2817 
2818 }
2819 
2820 static int gdb_enable_hw_breakpoint(struct target *target,tid_t tid,REG dreg) {
2821 
2822 }
2823 
2824 
2825 static int gdb_singlestep(struct target *target,tid_t tid,int isbp,
2826  struct target *overlay) {
2827  struct gdb_state *gstate = (struct gdb_state *)target->state;
2828  struct target_thread *tthread;
2829 
2831 
2832  tthread = __gdb_load_cached_thread(target,tid);
2833  gstate->stepping = 1;
2834  target->sstep_thread = tthread;
2835 
2836  return 0;
2837 }
2838 
2839 static int gdb_singlestep_end(struct target *target,tid_t tid,
2840  struct target *overlay) {
2841  struct gdb_state *gstate = (struct gdb_state *)target->state;
2842  struct target_thread *tthread;
2843 
2845 
2846  tthread = __gdb_load_cached_thread(target,tid);
2847  gstate->stepping = 0;
2848 
2849  if (target->sstep_thread == tthread) {
2850  vdebug(5,LA_TARGET,LF_GDB,"clearing sstep flag for tid %d\n",tid);
2851  }
2852  else if (target->sstep_thread) {
2853  vwarn("was told to clear sstep flag for tid %d, but tid %d was stepping;"
2854  " clearing anyway!\n",tid,target->sstep_thread->tid);
2855  }
2856  else {
2857  vwarn("was told to clear sstep flag for tid %d, but no tid was stepping;"
2858  " clearing anyway!\n",tid);
2859  }
2860 
2861  target->sstep_thread = NULL;
2862 
2863  return 0;
2864 }
arch_type_t type
Definition: arch.h:117
#define OBJSCLEAN(obj)
Definition: object.h:116
uint8_t * breakpoint_instrs
Definition: arch.h:149
#define GDB_ARGP_MEMCACHE_MMAP_SIZE
Definition: target_gdb.c:292
int target_flush_all_threads(struct target *target)
Definition: target_api.c:1275
int qemu_qmp_port
Definition: target_gdb.h:55
ADDR base_virt_addr
Definition: binfile.h:286
REGVAL target_regcache_readreg_tidctxt(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg)
Definition: target.c:6566
#define REG_X86_64_RSP
Definition: arch_x86_64.h:43
GHashTable * config
Definition: target_api.h:2582
int target_arch_x86_v2p_flags_snprintf(struct target *target, arch_x86_v2p_flags_t flags, char *buf, unsigned int bufsiz)
char * ibuf
Definition: target_gdb.h:102
void * state
Definition: target_api.h:2488
void regcache_mark_flushed(struct regcache *regcache)
Definition: regcache.c:118
#define SAFE_PERSONALITY_OP_WARN(op, outvar, expoutval, target,...)
Definition: target.h:737
unsigned int do_unix
Definition: target_gdb.h:39
void * backend_spec
Definition: target_api.h:2252
int evloop_unset_fd(struct evloop *evloop, int fd, int fdtype)
Definition: evloop.c:165
#define SAFE_PERSONALITY_OP_WARN_NORET(op, outvar, expoutval, target,...)
Definition: target.h:755
struct debugfile * debugfile_from_file(char *filename, char *root_prefix, struct array_list *debugfile_load_opts_list)
Definition: debug.c:1584
thread_bpmode_t bpmode
Definition: target_api.h:2174
char * sockfile
Definition: target_gdb.h:82
int32_t tid_t
Definition: common.h:36
#define OBJSVALID(obj)
Definition: object.h:101
target_personality_t personality
Definition: target_api.h:2477
Definition: log.h:175
#define REG_X86_64_CR4
Definition: arch_x86_64.h:140
target_status_t
Definition: target_api.h:197
#define REG_X86_64_RIP
Definition: arch_x86_64.h:54
int gdb_rsp_step(struct target *target)
result_t probepoint_ss_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: probe.c:3021
char * hostname
Definition: target_gdb.h:51
uint32_t no_adjust_bp_ip
Definition: target_api.h:2427
struct memregion * region
Definition: target_api.h:2353
int gdb_spec_to_argv(struct target_spec *spec, int *argc, char ***argv)
Definition: target_gdb.c:328
int target_regcache_copy_all(struct target_thread *sthread, thread_ctxt_t stidctxt, struct target_thread *dthread, thread_ctxt_t dtidctxt)
Definition: target.c:6451
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 target_regcache_copy_dirty_to(struct target_thread *sthread, thread_ctxt_t stidctxt, struct regcache *dregcache)
Definition: target.c:6521
GHashTable * soft_probepoints
Definition: target_api.h:2695
#define GDB_ARGP_SOCKFILE
Definition: target_gdb.c:285
#define GDB_ARGP_QEMU_QMP_HOST
Definition: target_gdb.c:288
struct target_personality_ops * personality_ops
Definition: target_api.h:2547
struct target_thread * sstep_thread
Definition: target_api.h:2687
GHashTable * target_regcache_copy_registers(struct target *target, tid_t tid)
Definition: target.c:6543
ADDR kernel_start_addr
Definition: target_gdb.h:123
#define GDB_ARGP_CLEAR_MEM_CACHES
Definition: target_gdb.c:291
unsigned long int memcache_mmap_size
Definition: target_gdb.h:47
unsigned long(* write_tid)(struct target *target, tid_t tid, ADDR pgd, ADDR addr, unsigned long length, unsigned char *buf)
Definition: target_gdb.h:161
int target_os_thread_is_user(struct target *target, tid_t tid)
Definition: target_os.c:55
probepoint_whence_t
Definition: probe_api.h:234
target_debug_bp_handler_t handle_break
Definition: target_api.h:2829
struct target_ops gdb_ops
Definition: target_gdb.c:193
#define SAFE_PERSONALITY_OP(op, outvar, defoutval, target,...)
Definition: target.h:772
struct target_thread * base_thread
Definition: target_api.h:2615
int target_regcache_copy_from(struct target_thread *dthread, thread_ctxt_t dtidctxt, struct regcache *sregcache)
Definition: target.c:6499
struct argp_option gdb_argp_opts[]
Definition: target_gdb.c:295
struct target_memmod * target_memmod_create(struct target *target, tid_t tid, ADDR addr, int is_phys, target_memmod_type_t mmt, unsigned char *code, unsigned int code_len, int nowrite)
Definition: target.c:4726
int gdb_instr_can_switch_context(struct target *target, ADDR addr)
int _target_unchange_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1744
#define REG_X86_EBP
Definition: arch_x86.h:42
struct target_thread * global_thread
Definition: target_api.h:2645
struct target * target_create(char *type, struct target_spec *spec)
Definition: target.c:1849
uint32_t monitorhandling
Definition: target_api.h:2427
#define REG_X86_CR4
Definition: arch_x86.h:103
char * name
Definition: target.h:928
char * qemu_mem_path
Definition: target_gdb.h:61
unsigned long(* write_phys)(struct target *target, ADDR paddr, unsigned long length, unsigned char *buf)
Definition: target_gdb.h:157
int target_regcache_writereg_tidctxt(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg, REGVAL value)
Definition: target.c:6588
#define REG_X86_CR3
Definition: arch_x86.h:102
int target_associate_debugfile(struct target *target, struct memregion *region, struct debugfile *debugfile)
Definition: target.c:1963
#define verror(format,...)
Definition: log.h:30
#define REG_X86_MSR_EFER
Definition: arch_x86.h:114
tid_t target_os_thread_get_leader(struct target *target, tid_t tid)
Definition: target_os.c:62
arch_x86_v2p_flags_t v2p_flags
Definition: target_gdb.h:128
unsigned int stepping
Definition: target_gdb.h:84
char * devfile
Definition: target_gdb.h:49
char * qemu_qmp_hostname
Definition: target_gdb.h:54
int gdb_rsp_close(struct target *target, int stay_paused)
int gdb_rsp_resume(struct target *target)
#define GDB_ARGP_QEMU_QMP_PORT
Definition: target_gdb.c:290
Definition: evloop.h:66
uint32_t mmapable
Definition: target_api.h:2427
#define REG_X86_ESP
Definition: arch_x86.h:41
struct memregion * memregion_create(struct addrspace *space, region_type_t type, char *name)
Definition: memory.c:242
ADDR base_phys_addr
Definition: target.h:966
int target_personality_attach(struct target *target, char *personality, char *personality_lib)
Definition: target.c:5965
#define vwarn(format,...)
Definition: log.h:33
uint32_t threadctl
Definition: target_api.h:2427
struct evloop * evloop
Definition: target_api.h:2598
#define ADDRMAX
Definition: common.h:74
REGVAL target_read_reg(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1083
void regcache_invalidate(struct regcache *regcache)
Definition: regcache.c:127
result_t probepoint_bp_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint, int was_stepping)
Definition: probe.c:2593
int(* addr_v2p)(struct target *target, tid_t tid, ADDR pgd, ADDR vaddr, ADDR *paddr)
Definition: target_gdb.h:153
char * gdb_argp_header
Definition: target_gdb.c:565
int(* load_machine)(struct target *target, struct regcache *regcache)
Definition: target_gdb.h:152
struct arch * arch
Definition: binfile.h:216
int gdb_evloop_handler(int readfd, int fdtype, void *state)
Definition: target_gdb.c:1730
struct memrange * memrange_create(struct memregion *region, ADDR start, ADDR end, OFFSET offset, unsigned int prot_flags)
Definition: memory.c:538
int target_os_thread_get_pgd_phys(struct target *target, tid_t tid, ADDR *pgdp)
Definition: target_os.c:48
int evloop_fd
Definition: target_gdb.h:134
target_status_t gdb_rsp_load_status(struct target *target)
int gdb_rsp_connect(struct target *target)
struct target_thread * current_thread
Definition: target_api.h:2640
#define OBJDIRTY(obj)
Definition: object.h:80
gdb_rsp_break_t
#define GDB_ARGP_UDP
Definition: target_gdb.c:284
#define GDB_ARGP_IS_KVM
Definition: target_gdb.c:286
#define REG_X86_CS
Definition: arch_x86.h:84
void target_memmod_set_writeable(struct target *target, struct target_memmod *mmod, int writeable)
Definition: target.c:4836
char * main_filename
Definition: target_gdb.h:63
probepoint_watchsize_t
Definition: probe_api.h:241
unsigned int clear_mem_caches_each_exception
Definition: target_gdb.h:39
struct target_location_ctxt * global_tlctxt
Definition: target_api.h:2667
Definition: target.h:331
uint32_t live
Definition: target_api.h:2427
#define THREAD_CTXT_USER
Definition: target_os.h:33
void gdb_free_spec(struct gdb_spec *xspec)
Definition: target_gdb.c:580
int target_arch_x86_v2p_get_flags(struct target *target, REGVAL cr0, REGVAL cr4, REGVAL msr_efer, REGVAL cpuid_edx, arch_x86_v2p_flags_t *flags)
unsigned int is_qemu
Definition: target_gdb.h:39
GHashTable * threads
Definition: target_api.h:2632
unsigned int do_udp
Definition: target_gdb.h:39
struct target_spec * spec
Definition: target_api.h:2258
struct gdb_helper_ops gdb_helper_ops_builtin
#define GDB_ARGP_PORT
Definition: target_gdb.c:283
#define REG_X86_64_CS
Definition: arch_x86_64.h:103
#define THREAD_CTXT_KERNEL
Definition: target_os.h:32
#define PROT_EXEC
Definition: common.h:108
#define RHOLD(x, hx)
Definition: common.h:622
#define REG_X86_64_MSR_EFER
Definition: arch_x86_64.h:154
#define PROT_WRITE
Definition: common.h:107
ADDR addr
Definition: target.h:381
int target_regcache_snprintf(struct target *target, struct target_thread *tthread, thread_ctxt_t tctxt, char *buf, int bufsiz, int detail, char *sep, char *kvsep, int flags)
Definition: target.c:6368
#define EVLOOP_FDTYPE_A
Definition: evloop.h:29
target_poll_outcome_t
Definition: target_api.h:394
target_type_t target_type
Definition: target_api.h:2166
#define vdebug(devel, areas, flags, format,...)
Definition: log.h:302
#define REG_X86_EIP
Definition: arch_x86.h:45
int gdb_rsp_read_regs(struct target *target, struct regcache *regcache)
int evloop_set_fd(struct evloop *evloop, int fd, int fdtype, evloop_handler_t handler, void *state)
Definition: evloop.c:48
struct thread_probepoint_context * tpc
Definition: target_api.h:2131
int _target_enable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1703
#define EVLOOP_HRET_ERROR
Definition: evloop.h:35
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)
Definition: log.h:172
#define GDB_ARGP_IS_QEMU
Definition: target_gdb.c:287
#define OBJVALID(obj)
Definition: object.h:76
struct gdb_helper_ops gdb_helper_ops_qemu
struct memregion * region
Definition: target.h:981
struct arch * arch
Definition: target_api.h:2563
unsigned int wordsize
Definition: arch.h:121
unsigned int rsp_status_valid
Definition: target_gdb.h:84
int gdb_rsp_pause(struct target *target)
int(* handle_exception_ours)(struct target *target)
Definition: target_gdb.h:150
int unlink(const char *pathname)
Definition: qemuhacks.c:132
struct array_list * debugfile_load_opts_list
Definition: target_api.h:2199
struct binfile * binfile
Definition: dwdebug.h:808
int target_regcache_mark_flushed(struct target *target, struct target_thread *tthread, thread_ctxt_t tctxt)
Definition: target.c:6415
unsigned int machine_valid
Definition: target_gdb.h:84
error_t gdb_argp_parse_opt(int key, char *arg, struct argp_state *state)
Definition: target_gdb.c:439
void target_thread_set_status(struct target_thread *tthread, thread_status_t status)
Definition: target.c:3999
Definition: log.h:70
void regcache_destroy(struct regcache *regcache)
Definition: regcache.c:42
uint32_t REGVAL
Definition: common.h:66
int(* attach)(struct target *target)
Definition: target_gdb.h:148
unsigned int is_kvm
Definition: target_gdb.h:39
Definition: arch.h:102
#define CHECKTIDGLOBAL(tid)
Definition: target_gdb.c:2749
int target_memmod_release(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target.c:4877
uint32_t needmonitorinterrupt
Definition: target_api.h:2427
#define EVLOOP_HRET_SUCCESS
Definition: evloop.h:36
target_status_t target_status(struct target *target)
Definition: target_api.c:1007
int target_invalidate_all_threads(struct target *target)
Definition: target.c:4426
#define PRIiTID
Definition: common.h:37
struct gdb_rsp_stop_status last_stop_status
Definition: target_gdb.h:116
char * qemu_libvirt_domain
Definition: target_gdb.h:65
struct binfile * binfile_pointing
Definition: dwdebug.h:830
int8_t REG
Definition: common.h:93
uint32_t opened
Definition: target_api.h:2427
struct target_location_ctxt * target_location_ctxt_create(struct target *target, tid_t tid, struct memregion *region)
Definition: target.c:5248
int target_finalize(struct target *target)
Definition: target.c:1925
unsigned int breakpoint_instrs_len
Definition: arch.h:150
GHashTable * hard_probepoints
Definition: target_api.h:2080
uint32_t ADDR
Definition: common.h:64
char * name
Definition: target_api.h:2483
struct target_ops * ops
Definition: target_api.h:2510
Definition: log.h:162
int _target_remove_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1673
target_exception_flags_t
Definition: target_api.h:386
REG spregno
Definition: target_api.h:2469
thread_ctxt_t tidctxt
Definition: target_api.h:2043
int _target_disable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1716
target_status_t status
Definition: target_api.h:2465
struct target * gdb_instantiate(struct target_spec *spec, struct evloop *evloop)
Definition: target_gdb.c:591
#define PROT_READ
Definition: common.h:106
unsigned char *(* read_tid)(struct target *target, tid_t tid, ADDR pgd, ADDR addr, unsigned long target_length, unsigned char *buf)
Definition: target_gdb.h:159
int port
Definition: target_gdb.h:52
REG fbregno
Definition: target_api.h:2468
#define PRIxADDR
Definition: common.h:67
int(* handle_pause)(struct target *target)
Definition: target_gdb.h:151
struct gdb_helper_ops * hops
Definition: target_gdb.h:131
#define EVLOOP_FDTYPE_R
Definition: evloop.h:30
int binfile_get_root_scope_sizes(struct binfile *binfile, int *named, int *duplicated, int *anon, int *numscopes)
Definition: binfile.c:326
REGVAL target_regcache_readreg(struct target *target, tid_t tid, REG reg)
Definition: target.c:6209
#define GDB_ARGP_QEMU_MEM_PATH
Definition: target_gdb.c:289
int(* snprintf)(struct target *target, char *buf, int bufsiz)
Definition: target_api.h:2758
struct target_spec * spec
Definition: target_api.h:2565
struct regcache * machine
Definition: target_gdb.h:118
uint32_t nodisablehwbponss
Definition: target_api.h:2427
#define REG_X86_CR0
Definition: arch_x86.h:99
ADDR base_virt_addr
Definition: target.h:967
active_probe_flags_t
Definition: target_api.h:432
#define REG_X86_64_CR0
Definition: arch_x86_64.h:136
char * debugfile_root_prefix
Definition: target_api.h:2197
struct regcache * regcache_create(struct arch *arch)
Definition: regcache.c:29
unsigned int max_thread_ctxt
Definition: target_api.h:2467
struct target_thread * target_lookup_thread(struct target *target, tid_t tid)
Definition: target.c:3981
void target_set_status(struct target *target, target_status_t status)
Definition: target.c:3993
#define CHECKTIDGLOBALORCURRENT(_T)
Definition: target_gdb.c:2756
struct gdb_spec * gdb_build_spec(void)
Definition: target_gdb.c:571
target_debug_handler_t handle_step
Definition: target_api.h:2830
unsigned char *(* read_phys)(struct target *target, ADDR paddr, unsigned long length, unsigned char *buf)
Definition: target_gdb.h:155
ADDR base_phys_addr
Definition: binfile.h:285
char * sockfile
Definition: target_gdb.h:50
#define REG_X86_64_CR3
Definition: arch_x86_64.h:139
int(* fini)(struct target *target)
Definition: target_gdb.h:163
int vdebug_is_on(int level, log_areas_t areas, log_flags_t flags)
Definition: log.c:335
struct binfile * binfile
Definition: target.h:950
#define REG_X86_64_RBP
Definition: arch_x86_64.h:42
struct target_thread * target_create_thread(struct target *target, tid_t tid, void *tstate, void *tpstate)
Definition: target.c:4021
struct array_list * target_list_threads(struct target *target)
Definition: target_api.c:1168
int _target_change_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod, unsigned char *code, unsigned long code_len)
Definition: target_api.c:1729
int target_regcache_writereg(struct target *target, tid_t tid, REG reg, REGVAL value)
Definition: target.c:6250
REG ipregno
Definition: target_api.h:2470
#define TID_GLOBAL
Definition: target_api.h:145
int regcache_isdirty(struct regcache *regcache)
Definition: regcache.c:236
GHashTable * stubfeatures
Definition: target_gdb.h:100
#define GDB_ARGP_LIBVIRT_DOMAIN
Definition: target_gdb.c:293
#define GDB_ARGP_HOST
Definition: target_gdb.c:282
struct probepoint * probepoint
Definition: probe.h:207
#define PRIxREGVAL
Definition: common.h:72
struct addrspace * addrspace_create(struct target *target, char *name, ADDR tag)
Definition: memory.c:41
uint32_t writeable
Definition: target_api.h:2427
int gdb_rsp_insert_break(struct target *target, ADDR addr, gdb_rsp_break_t bt, int kind)
struct argp gdb_argp
Definition: target_gdb.c:562