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