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_api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-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 #include "common.h"
21 #include "glib_wrapper.h"
22 #include "arch.h"
23 
24 #include "target_api.h"
25 #include "target.h"
26 #include "target_os.h"
27 #include "target_process.h"
28 #include "probe_api.h"
29 #include "probe.h"
30 
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <inttypes.h>
37 #include <signal.h>
38 #include <glib.h>
39 
40 #include "target_linux_userproc.h"
41 #ifdef ENABLE_XENSUPPORT
42 #include "target_xen_vm.h"
43 #endif
44 #include "target_os_process.h"
45 #include "target_php.h"
46 #include "target_gdb.h"
47 
52 /*
53  * Generic function that launches or attaches to a target, given @spec.
54  */
56  struct evloop *evloop) {
57  struct target *target = NULL;
58 
59  if (spec->target_type == TARGET_TYPE_PTRACE) {
60  target = linux_userproc_instantiate(spec,evloop);
61  }
62 #ifdef ENABLE_XENSUPPORT
63  else if (spec->target_type == TARGET_TYPE_XEN) {
64  target = xen_vm_instantiate(spec,evloop);
65  }
66 #endif
67  else if (spec->target_type == TARGET_TYPE_OS_PROCESS) {
68  verror("cannot directly instantiate TARGET_TYPE_OS_PROCESS;"
69  " call target_instantiate_overlay instead.\n");
70  errno = EINVAL;
71  return NULL;
72  }
73  else if (spec->target_type == TARGET_TYPE_PHP) {
74  verror("cannot directly instantiate TARGET_TYPE_PHP;"
75  " call target_instantiate_overlay instead.\n");
76  errno = EINVAL;
77  return NULL;
78  }
79  else if (spec->target_type == TARGET_TYPE_GDB) {
80  target = gdb_instantiate(spec,evloop);
81  }
82 
83  if (target) {
84  target->spec = spec;
85  return target;
86  }
87 
88  errno = EINVAL;
89  return NULL;
90 }
91 
92 GList *target_instantiate_and_open(struct target_spec *primary_target_spec,
93  GList *base_target_specs,
94  GList *overlay_target_specs,
95  struct evloop *evloop,
96  GList **error_specs) {
97  struct target *primary_target = NULL,*target = NULL,*base_target;
98  struct target_spec *spec;
99  int i;
100  GList *tmp;
101  GList *lcopy = NULL;
102  GList *retval = NULL;
103 
104  i = 0;
105 
106  /*
107  * Instantiate the primary target first; then the base targets;
108  * then the overlay targets. If an overlay target doesn't have a
109  * base target id, then we assume and try the primary target created
110  * from the primary target spec; otherwise we fail.
111  */
112 
113  if (primary_target_spec) {
114  primary_target = target_instantiate(primary_target_spec,evloop);
115  if (!primary_target) {
116  if (error_specs) {
117  vwarn("could not instantiate primary target; skipping!\n");
118  *error_specs = g_list_append(*error_specs,primary_target_spec);
119  }
120  else {
121  verror("could not instantiate primary target; aborting\n!");
122  goto errout;
123  }
124  }
125  else {
126  if (!target_open(primary_target)) {
127  vdebug(5,LA_TARGET,LF_TARGET,"instantiated primary target\n");
128  retval = g_list_append(retval,primary_target);
129  }
130  else if (error_specs) {
131  vwarn("could not open primary target spec %d\n",i);
132  target_close(primary_target);
133  target_finalize(primary_target);
134  *error_specs = g_list_append(*error_specs,primary_target_spec);
135  }
136  else {
137  verror("could not open primary target spec %d\n",i);
138  target_close(primary_target);
139  target_finalize(primary_target);
140  primary_target = NULL;
141  goto errout;
142  }
143  }
144  }
145 
146  if (base_target_specs) {
147  lcopy = g_list_copy(base_target_specs);
148  v_g_list_foreach(lcopy,tmp,spec) {
149  ++i;
150  target = target_instantiate(spec,evloop);
151 
152  if (target) {
153  target->spec = spec;
154  }
155  else if (error_specs) {
156  vwarn("could not instantiate target spec %d\n",i);
157  *error_specs = g_list_append(*error_specs,spec);
158  }
159  else {
160  verror("could not instantiate target spec %d\n",i);
161  goto errout;
162  }
163 
164  if (!target_open(target)) {
165  retval = g_list_append(retval,target);
166  }
167  else if (error_specs) {
168  vwarn("could not instantiate target spec %d\n",i);
169  target_close(target);
170  target_finalize(target);
171  *error_specs = g_list_append(*error_specs,spec);
172  }
173  else {
174  verror("could not instantiate target spec %d\n",i);
175  target_close(target);
176  target_finalize(target);
177  target = NULL;
178  goto errout;
179  }
180  }
181  g_list_free(lcopy);
182  lcopy = NULL;
183  }
184 
185  if (overlay_target_specs) {
186  lcopy = g_list_copy(overlay_target_specs);
187  v_g_list_foreach(lcopy,tmp,spec) {
188  ++i;
189  if (spec->base_target_id <= 0)
190  base_target = primary_target;
191  else
192  base_target = target_lookup_target_id(spec->base_target_id);
193 
194  if (!base_target) {
195  if (!error_specs) {
196  verror("could not instantiate overlay target spec %d;"
197  " no base target with id %d\n",
198  i,spec->base_target_id);
199  goto errout;
200  }
201  else {
202  vwarn("could not instantiate overlay target spec %d;"
203  " no base target with id %d\n",
204  i,spec->base_target_id);
205  *error_specs = g_list_append(*error_specs,spec);
206  continue;
207  }
208  }
209 
210  if (spec->base_thread_name) {
211  spec->base_thread_id =
213  spec->base_thread_name);
214  if (spec->base_thread_id < 0) {
215  if (!error_specs) {
216  verror("could not instantiate overlay target spec %d;"
217  " no base target thread named %s\n",
218  i,spec->base_thread_name);
219  goto errout;
220  }
221  else {
222  vwarn("could not instantiate overlay target spec %d;"
223  " no base target thread named %s\n",
224  i,spec->base_thread_name);
225  *error_specs = g_list_append(*error_specs,spec);
226  }
227  }
228  }
229 
230  target = target_instantiate_overlay(base_target,
231  spec->base_thread_id,spec);
232  if (target) {
233  target->spec = spec;
234  retval = g_list_append(retval,target);
235  }
236  else if (!error_specs) {
237  verror("could not instantiate overlay target spec %d"
238  " on base thread %d\n",
239  i,spec->base_thread_id);
240  goto errout;
241  }
242  else {
243  vwarn("could not instantiate overlay target spec %d"
244  " on base thread %d\n",
245  i,spec->base_thread_id);
246  *error_specs = g_list_append(*error_specs,spec);
247  }
248 
249  if (!target_open(target)) {
250  retval = g_list_append(retval,target);
251  }
252  else if (error_specs) {
253  vwarn("could not instantiate target spec %d\n",i);
254  target_close(target);
255  target_finalize(target);
256  *error_specs = g_list_append(*error_specs,spec);
257  }
258  else {
259  verror("could not instantiate target spec %d\n",i);
260  target_close(target);
261  target_finalize(target);
262  target = NULL;
263  goto errout;
264  }
265  }
266  g_list_free(lcopy);
267  lcopy = NULL;
268  }
269 
270  return retval;
271 
272  errout:
273  if (lcopy) {
274  g_list_free(lcopy);
275  lcopy = NULL;
276  }
277  if (retval) {
278  retval = g_list_reverse(retval);
279  v_g_list_foreach(retval,tmp,target) {
280  target_finalize(target);
281  }
282  g_list_free(retval);
283  }
284  return NULL;
285 }
286 
287 GList *target_instantiate_and_open_list(GList *target_specs,
288  struct evloop *evloop,
289  GList **error_specs) {
290  struct target *target = NULL;
291  struct target_spec *spec;
292  int i;
293  GList *tmp,*tmp2;
294  GList *lcopy;
295  GList *retval = NULL;
296  int progress,last_progress;
297  struct target *base_target;
298  tid_t base_thread_id;
299 
300  lcopy = g_list_copy(target_specs);
301 
302  /*
303  * Instantiate all the base targets first, removing them as we go;
304  * then instantiate the overlay targets if there is a matching
305  * target, or if any target will accept them.
306  */
307  i = 0;
308  /* Force at least two trips through the loop before we start giving
309  * up on lookups.
310  */
311  progress = 1;
312  while (1) {
313  last_progress = progress;
314  progress = 0;
315  v_g_list_foreach_safe(lcopy,tmp,tmp2,spec) {
316  if (spec->target_type == TARGET_TYPE_PTRACE) {
317  target = linux_userproc_instantiate(spec,evloop);
318  }
319 #ifdef ENABLE_XENSUPPORT
320  else if (spec->target_type == TARGET_TYPE_XEN) {
321  target = xen_vm_instantiate(spec,evloop);
322  }
323 #endif
324  else if (spec->target_type == TARGET_TYPE_GDB) {
325  target = gdb_instantiate(spec,evloop);
326  }
327  else {
328  base_target = target_lookup_target_id(spec->base_target_id);
329 
330  if (!base_target && !last_progress) {
331  if (!error_specs) {
332  verror("could not lookup base target id %d for"
333  " overlay target spec\n",spec->base_target_id);
334  goto errout;
335  }
336  else {
337  vwarn("could not lookup base target id %d for"
338  " target spec; skipping\n",spec->base_target_id);
339  v_g_list_foreach_remove(lcopy,tmp,tmp2);
340  *error_specs = g_list_append(*error_specs,spec);
341  continue;
342  }
343  }
344  else if (!base_target) {
345  /* Try again until there is no progress */
346  continue;
347  }
348 
349  if (!spec->base_thread_name)
350  base_thread_id = spec->base_thread_id;
351  else {
352  base_thread_id =
354  spec->base_thread_name);
355  if (base_thread_id < 0) {
356  if (!error_specs) {
357  verror("could not lookup base target thread name %s"
358  " for overlay target spec\n",
359  spec->base_thread_name);
360  goto errout;
361  }
362  else {
363  vwarn("could not lookup base target thread name %s"
364  " for overlay target spec; skipping\n",
365  spec->base_thread_name);
366  v_g_list_foreach_remove(lcopy,tmp,tmp2);
367  *error_specs = g_list_append(*error_specs,spec);
368  continue;
369  }
370  }
371  }
372 
373  target = target_instantiate_overlay(base_target,base_thread_id,
374  spec);
375  }
376  }
377 
378  if (target) {
379  ++progress;
380  target->spec = spec;
381  retval = g_list_append(retval,target);
382  }
383  else if (error_specs) {
384  vwarn("could not instantiate target spec %d\n",i);
385  *error_specs = g_list_append(*error_specs,spec);
386  }
387  else {
388  vwarn("could not instantiate target spec %d\n",i);
389  goto errout;
390  }
391 
392  v_g_list_foreach_remove(lcopy,tmp,tmp2);
393  }
394 
395  g_list_free(lcopy);
396  return retval;
397 
398  errout:
399  g_list_free(lcopy);
400  if (retval) {
401  retval = g_list_reverse(retval);
402  v_g_list_foreach(retval,tmp,target) {
403  target_finalize(target);
404  }
405  g_list_free(retval);
406  }
407  return NULL;
408 }
409 
411  struct target_spec *tspec;
412 
413  if (type == TARGET_TYPE_NONE) {
414  tspec = calloc(1,sizeof(*tspec));
415  }
416  else if (type == TARGET_TYPE_PTRACE) {
417  tspec = calloc(1,sizeof(*tspec));
419  }
420 #ifdef ENABLE_XENSUPPORT
421  else if (type == TARGET_TYPE_XEN) {
422  tspec = calloc(1,sizeof(*tspec));
423  tspec->backend_spec = xen_vm_build_spec();
424  }
425 #endif
426  else if (type == TARGET_TYPE_OS_PROCESS) {
427  tspec = calloc(1,sizeof(*tspec));
429  }
430  else if (type == TARGET_TYPE_PHP) {
431  tspec = calloc(1,sizeof(*tspec));
432  tspec->backend_spec = php_build_spec();
433  }
434  else if (type == TARGET_TYPE_GDB) {
435  tspec = calloc(1,sizeof(*tspec));
436  tspec->backend_spec = gdb_build_spec();
437  }
438  else {
439  errno = EINVAL;
440  return NULL;
441  }
442 
443  tspec->target_id = -1;
444  tspec->target_type = type;
445  tspec->target_mode = mode;
446  tspec->style = PROBEPOINT_FASTEST;
447  tspec->kill_on_close_sig = SIGKILL;
448  tspec->read_only = 0;
449 
450  return tspec;
451 }
452 
453 void target_free_spec(struct target_spec *spec) {
454  int i;
455 
456  if (spec->backend_spec) {
457  if (spec->target_type == TARGET_TYPE_PTRACE) {
459  }
460 #ifdef ENABLE_XENSUPPORT
461  else if (spec->target_type == TARGET_TYPE_XEN) {
462  xen_vm_free_spec((struct xen_vm_spec *)spec->backend_spec);
463  }
464 #endif
465  else if (spec->target_type == TARGET_TYPE_OS_PROCESS) {
467  }
468  else if (spec->target_type == TARGET_TYPE_PHP) {
469  php_free_spec((struct php_spec *)spec->backend_spec);
470  }
471  else if (spec->target_type == TARGET_TYPE_GDB) {
472  gdb_free_spec((struct gdb_spec *)spec->backend_spec);
473  }
474  }
475 
476  if (spec->debugfile_load_opts_list) {
477  for (i = 0; i < array_list_len(spec->debugfile_load_opts_list); ++i) {
478  struct debugfile_load_opts *dlo_list = (struct debugfile_load_opts *) \
479  array_list_item(spec->debugfile_load_opts_list,i);
480  debugfile_load_opts_free(dlo_list);
481  }
482  array_list_free(spec->debugfile_load_opts_list);
483  spec->debugfile_load_opts_list = NULL;
484  }
485  if (spec->infile) {
486  free(spec->infile);
487  spec->infile = NULL;
488  }
489  if (spec->outfile) {
490  free(spec->outfile);
491  spec->outfile = NULL;
492  }
493  if (spec->errfile) {
494  free(spec->errfile);
495  spec->errfile = NULL;
496  }
497 
498  free(spec);
499 }
500 
502  return target->spec->target_type;
503 }
504 
505 char *target_name(struct target *target) {
506  return target->name;
507 }
508 
509 int target_id(struct target *target) {
510  return target->id;
511 }
512 
513 int target_open(struct target *target) {
514  int rc;
515  struct addrspace *space;
516  struct memregion *region;
517  char buf[128];
518  GList *t1,*t2;
519 
520  if (!target->spec) {
521  verror("cannot open a target without a specification!\n");
522  errno = EINVAL;
523  return -1;
524  }
525 
526  vdebug(5,LA_TARGET,LF_TARGET,"opening target type(%d)\n",target_type(target));
527 
528  /*
529  * Try to load the user-specified personality if one exists, and if
530  * the target did *NOT* load it alrady!
531  */
532  if (target->spec->personality && !target->personality_ops) {
534  "loading user-specified personality '%s' (%s)\n",
535  target->spec->personality,target->spec->personality_lib ? : "");
536  if ((rc = target_personality_attach(target,target->spec->personality,
537  target->spec->personality_lib))) {
538  verror("Failed to initialize user-specified personality (%d)!\n",rc);
539  return -1;
540  }
541  }
542  else if (target->spec->personality_lib) {
543  verror("cannot specify a personality library without a"
544  " personality name!\n");
545  errno = EINVAL;
546  return -1;
547  }
548 
549  vdebug(5,LA_TARGET,LF_TARGET,"target type(%d): init\n",target_type(target));
550  if ((rc = target->ops->init(target))) {
551  return rc;
552  }
553  SAFE_PERSONALITY_OP_WARN(init,rc,0,target);
554 
555  if (target_snprintf(target,buf,sizeof(buf)) < 0)
556  target->name = NULL;
557  else
558  target->name = strdup(buf);
559 
560  if (target->spec->bpmode == THREAD_BPMODE_STRICT && !target->threadctl) {
561  verror("cannot init a target in BPMODE_STRICT that does not have"
562  " threadctl!\n");
563  errno = ENOTSUP;
564  return -1;
565  }
566 
567  SAFE_TARGET_OP(loadspaces,rc,0,target);
568  v_g_list_foreach(target->spaces,t1,space) {
569  SAFE_TARGET_OP(loadregions,rc,0,target,space);
570  }
571 
572  v_g_list_foreach(target->spaces,t1,space) {
573  v_g_list_foreach(space->regions,t2,region) {
574  if (region->type == REGION_TYPE_HEAP
575  || region->type == REGION_TYPE_STACK
576  || region->type == REGION_TYPE_VDSO
577  || region->type == REGION_TYPE_VSYSCALL)
578  continue;
579 
580  SAFE_TARGET_OP_WARN_NORET(loaddebugfiles,rc,0,target,space,region);
581 
582  /*
583  * Once the region has been loaded and associated with a
584  * debuginfo file, we calculate the phys_offset of the
585  * loaded code -- which is the base_phys_addr - base_virt_addr
586  * from the ELF program headers.
587  */
588  if (region->type == REGION_TYPE_MAIN)
589  region->phys_offset = 0;
590  /*
591  * If it got loaded at the base_phys_addr from the binary,
592  * there is no offset.
593  */
594  else if (region->base_load_addr == region->base_phys_addr)
595  region->phys_offset = 0;
596  else
597  region->phys_offset = region->base_load_addr \
598  + (region->base_phys_addr - region->base_virt_addr);
599 
601  "target(%s:%s:0x%"PRIxADDR") finished region(%s:%s,"
602  "base_load_addr=0x%"PRIxADDR",base_phys_addr=0x%"PRIxADDR
603  ",base_virt_addr=0x%"PRIxADDR
604  ",phys_offset=%"PRIiOFFSET" (0x%"PRIxOFFSET"))\n",
605  target->name,space->name,space->tag,
606  region->name,REGION_TYPE(region->type),
607  region->base_load_addr,region->base_phys_addr,
608  region->base_virt_addr,region->phys_offset,
609  region->phys_offset);
610  }
611  }
612 
613  SAFE_TARGET_OP(postloadinit,rc,0,target);
614  SAFE_TARGET_OP(attach,rc,0,target);
615 
616  target->opened = 1;
617 
618  /*
619  * Set up active probing if requested, once we're opened.
620  *
621  * NB: it's better if the backend does everything it can to
622  * pre-setup active probing -- i.e., making sure the necessary
623  * symbols exist, and it will be possible to probe them, in
624  * postloadinit().
625  */
626  SAFE_TARGET_OP(set_active_probing,rc,0,target,target->spec->ap_flags);
627 
628  SAFE_TARGET_OP(postopened,rc,0,target);
629 
630  return 0;
631 }
632 
634  int rc;
635 
636  if (!target->writeable && flags != AFP_NONE) {
637  verror("target not writeable; cannot enable any active probing!\n");
638  errno = EINVAL;
639  return -1;
640  }
641 
642  if (!target->ops->set_active_probing
643  && !target->personality_ops
644  && !target->personality_ops->set_active_probing) {
646  "no active probing support in target(%s)\n",target->name);
647  errno = ENOTSUP;
648  return -1;
649  }
650 
651  SAFE_TARGET_OP(set_active_probing,rc,0,target,flags);
652 
653  return rc;
654 }
655 
657  target_type_t type) {
658  struct array_list *retval;
659  GHashTableIter iter;
660  struct target_thread *tthread;
661 
662  vdebug(8,LA_TARGET,LF_TARGET,"loading available threads\n");
663 
664  if (target_load_available_threads(target,0)) {
665  verror("could not load available threads!\n");
666  return NULL;
667  }
668 
669  retval = array_list_create(g_hash_table_size(target->threads));
670  g_hash_table_iter_init(&iter,target->threads);
671  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
672  if (tthread == target->global_thread)
673  continue;
674 
675  if ((type == TARGET_TYPE_NONE && tthread->supported_overlay_types)
676  || (type & tthread->supported_overlay_types))
677  array_list_append(retval,(void *)(uintptr_t)tthread->tid);
678  }
679  array_list_compact(retval);
680 
681  vdebug(8,LA_TARGET,LF_TARGET,"can overlay available threads\n");
682 
683  return retval;
684 }
685 
687  if (g_hash_table_size(target->overlays) == 0)
688  return NULL;
689 
690  return array_list_create_from_g_hash_table(target->overlays);
691 }
692 
694  struct target_thread *tthread;
695 
696  if (!target->ops->lookup_overlay_thread_by_id) {
697  verror("no overlay support in target(%s)!\n",target->name);
698  errno = ENOTSUP;
699  return -1;
700  }
701 
702  vdebug(16,LA_TARGET,LF_TARGET,"target(%s)\n",target->name);
703 
704  tthread = target->ops->lookup_overlay_thread_by_id(target,id);
705  if (tthread)
706  return tthread->tid;
707 
708  if (!errno)
709  errno = ESRCH;
710  return -1;
711 }
712 
714  struct target_thread *tthread;
715 
716  if (!target->ops->lookup_overlay_thread_by_name) {
717  verror("no overlay support in target(%s)!\n",target->name);
718  errno = ENOTSUP;
719  return -1;
720  }
721 
722  vdebug(16,LA_TARGET,LF_TARGET,"target(%s)\n",target->name);
723 
724  tthread = target->ops->lookup_overlay_thread_by_name(target,name);
725  if (tthread)
726  return tthread->tid;
727 
728  if (!errno)
729  errno = ESRCH;
730  return -1;
731 
732 }
733 
735  tid_t tid) {
736  struct target_spec *retval;
737 
739  "target(%s) tid %"PRIiTID"\n",target->name,tid);
740 
741  if (!target->ops->build_default_overlay_spec) {
742  errno = ENOTSUP;
743  return NULL;
744  }
745 
746  retval = target->ops->build_default_overlay_spec(target,tid);
747  if (retval && target->writeable == 0 && retval->read_only == 0) {
748  verror("base target not writeable; cannot enable writeable overlay!\n");
749  errno = EINVAL;
750  target_free_spec(retval);
751  return NULL;
752  }
753  else
754  return retval;
755 }
756 
758  struct target_spec *spec) {
759  struct target *overlay;
760  struct target_thread *tthread;
761  struct target_thread *ntthread = NULL;
762 
763  if (!target->ops->instantiate_overlay) {
764  verror("no overlay support in target(%s)!\n",target->name);
765  errno = ENOTSUP;
766  return NULL;
767  }
768 
769  if (!target->writeable && !spec->read_only) {
770  verror("base target not writeable; cannot enable writeable overlay!\n");
771  errno = EINVAL;
772  return NULL;
773  }
774 
776  "target(%s) tid %"PRIiTID"\n",target->name,tid);
777 
778  if (g_hash_table_lookup(target->overlays,(gpointer)(uintptr_t)tid)
779  || g_hash_table_lookup(target->overlay_aliases,(gpointer)(uintptr_t)tid)) {
780  verror("target(%s) tid %"PRIiTID" already has overlay!\n",
781  target->name,tid);
782  errno = EALREADY;
783  return NULL;
784  }
785 
786  tthread = target_load_thread(target,tid,0);
787  if (!tthread) {
788  verror("target(%s) tid %d could not be loaded!\n",target->name,tid);
789  errno = ESRCH;
790  return NULL;
791  }
792 
793  overlay = target->ops->instantiate_overlay(target,tthread,spec,&ntthread);
794  if (!overlay) {
795  verror("target(%s) tid %"PRIiTID" failed to create overlay!\n",
796  target->name,tid);
797  return NULL;
798  }
799 
800  if (ntthread)
801  tthread = ntthread;
802 
803  overlay->base = target;
804  RHOLDW(overlay->base,overlay);
805  overlay->base_id = target->id;
806  overlay->base_thread = tthread;
807  RHOLDW(tthread,overlay);
808  overlay->base_tid = tthread->tid;
809 
810  g_hash_table_insert(target->overlays,
811  (gpointer)(uintptr_t)tthread->tid,overlay);
812  RHOLD(overlay,target);
813 
814  if (tid == tthread->tid) {
816  "target(%s) tid %"PRIiTID" new overlay target(%s) (id %d)\n",
817  target->name,tthread->tid,overlay->name,overlay->id);
818  }
819  else {
821  "target(%s) tid %"PRIiTID" new overlay target(%s) (id %d)"
822  " (not using user-supplied thread %d; base target overrode it!)\n",
823  target->name,tthread->tid,overlay->name,overlay->id,tid);
824  }
825 
826  return overlay;
827 }
828 
829 int target_snprintf(struct target *target,char *buf,int bufsiz) {
830  vdebug(16,LA_TARGET,LF_TARGET,"target(%s)\n",target->name);
831  return target->ops->snprintf(target,buf,bufsiz);
832 }
833 
835  if (target->evloop) {
836  verror("an evloop is already associated with target(%s)!\n",
837  target->name);
838  errno = EINVAL;
839  return -1;
840  }
841  vdebug(16,LA_TARGET,LF_TARGET,"target(%s)\n",target->name);
842  target->evloop = evloop;
843  return target->ops->attach_evloop(target,evloop);
844 }
845 
847  int rc;
848 
849  if (!target->evloop) {
850  vwarn("no evloop is associated with target(%s)!\n",
851  target->name);
852  return -1;
853  }
854 
855  vdebug(16,LA_TARGET,LF_TARGET,"target(%s)\n",target->name);
856  rc = target->ops->detach_evloop(target);
857 
858  target->evloop = NULL;
859 
860  return rc;
861 }
862 
864  if (target->evloop && evloop && target->evloop == evloop)
865  return 1;
866  return 0;
867 }
868 
870  if (target->status != TSTATUS_RUNNING) {
871  verror("cannot monitor target(%s) in state %s; ERROR!\n",
872  target->name,TSTATUS(target->status));
873  return TSTATUS_ERROR;
874  }
876  vdebug(8,LA_TARGET,LF_TARGET,"monitoring target(%s)\n",target->name);
877  return target->ops->monitor(target);
878 }
879 
880 int target_monitor_evloop(struct evloop *evloop,struct timeval *timeout,
881  struct target **target,target_status_t *status) {
882  struct evloop_fdinfo *fdinfo;
883  int hrc;
884  int fdtype;
885  int rc = 0;
886  int nfds;
887  struct timeval tv;
888  struct target *t;
889 
891 
892  if (timeout)
893  tv = *timeout;
894 
895  while (evloop_maxsize(evloop) > -1) {
896  if (target)
897  *target = NULL;
898  if (status)
899  *status = TSTATUS_UNKNOWN;
900  fdinfo = NULL;
901  hrc = 0;
902  fdtype = -1;
903 
904  vdebug(9,LA_TARGET,LF_TARGET,"monitoring evloop with up to %d FDs\n",
905  evloop_maxsize(evloop));
906 
907  /* Always reinit the timeout on new pass */
908  if (timeout)
909  *timeout = tv;
910 
911  rc = evloop_handleone(evloop,EVLOOP_RETONINT,timeout,
912  &fdinfo,&fdtype,&hrc);
913 
914  if (rc < 0) {
915  if (errno == EINTR) {
916  /*
917  * Did our default sighandler flag a signal as needing
918  * handling from the user? If so, return; else keep
919  * going.
920  */
922  break;
923  else
924  continue;
925  }
926  else if (errno == EBADF || errno == EINVAL || errno == ENOMEM
927  || errno == ENOENT || errno == EBADSLT || errno == ENOTSUP) {
928  vdebug(9,LA_TARGET,LA_TARGET,"evloop_handlone: '%s'\n",
929  strerror(errno));
930  }
931  else {
933  "evloop_handlone: unexpected error '%s' (%d)\n",
934  strerror(errno),errno);
935  }
936  break;
937  }
938  else if (rc == 0) {
939  if (fdinfo) {
940  /*
941  * Check this target and see if its status is one we
942  * want to punt to the user to notify/handle.
943  */
944  if (fdtype == EVLOOP_FDTYPE_R)
945  t = (struct target *)fdinfo->rhstate;
946  else if (fdtype == EVLOOP_FDTYPE_W)
947  t = (struct target *)fdinfo->whstate;
948  else if (fdtype == EVLOOP_FDTYPE_X)
949  t = (struct target *)fdinfo->xhstate;
950  else
951  t = NULL;
952 
953  if (t) {
954  if (t->status == TSTATUS_RUNNING
955  || t->status == TSTATUS_PAUSED)
956  continue;
957  else
958  break;
959  }
960  else
961  continue;
962  }
963  else if ((nfds = evloop_maxsize(evloop) < 0))
964  /* Nothing left. */
965  break;
966  else if (nfds) {
967  /* Something left; keep going. */
968  continue;
969  }
970  else {
971  verror("evloop_handleone returned 0 but still FDs to handle!\n");
972  errno = EINVAL;
973  rc = -1;
974  break;
975  }
976  }
977  else {
978  verror("evloop_handleone returned unexpected code '%d'; aborting!\n",
979  rc);
980  rc = -1;
981  break;
982  }
983  }
984 
985  if (target) {
986  if (fdtype == EVLOOP_FDTYPE_R)
987  *target = (struct target *)fdinfo->rhstate;
988  else if (fdtype == EVLOOP_FDTYPE_W)
989  *target = (struct target *)fdinfo->whstate;
990  else if (fdtype == EVLOOP_FDTYPE_X)
991  *target = (struct target *)fdinfo->xhstate;
992  else
993  *target = NULL;
994  }
995  if (status && *target)
996  *status = (*target)->status;
997 
998  return rc;
999 }
1000 
1001 target_status_t target_poll(struct target *target,struct timeval *tv,
1002  target_poll_outcome_t *outcome,int *pstatus) {
1003  if (target->status != TSTATUS_RUNNING) {
1004  verror("cannot poll target(%s) in state %s; ERROR!\n",
1005  target->name,TSTATUS(target->status));
1006  return TSTATUS_ERROR;
1007  }
1008  vdebug(8,LA_TARGET,LF_TARGET,"polling target(%s)\n",target->name);
1009  return target->ops->poll(target,tv,outcome,pstatus);
1010 }
1011 
1013  if (target->status != TSTATUS_PAUSED && target->status != TSTATUS_EXITING) {
1014  verror("cannot resume target(%s) in state %s; ERROR!\n",
1015  target->name,TSTATUS(target->status));
1016  return TSTATUS_ERROR;
1017  }
1018  if (target->status == TSTATUS_DONE) {
1020  "not pausing target(%s); already finished\n",target->name);
1021  return -1;
1022  }
1023  vdebug(8,LA_TARGET,LF_TARGET,"resuming target(%s)\n",target->name);
1024  return target->ops->resume(target);
1025 }
1026 
1028  if (target->status == TSTATUS_PAUSED) {
1030  "not pausing target(%s); already paused\n",target->name);
1031  return 0;
1032  }
1033  if (target->status == TSTATUS_DONE) {
1035  "not pausing target(%s); already finished\n",target->name);
1036  return -1;
1037  }
1038  vdebug(8,LA_TARGET,LF_TARGET,"pausing target(%s)\n",target->name);
1039  return target->ops->pause(target,0);
1040 }
1041 
1043  return target->opened;
1044 }
1045 
1048  "calling backend to get target(%s) status\n",target->name);
1049  target->status = target->ops->status(target);
1050  return target->status;
1051 }
1052 
1053 unsigned char *target_read_addr(struct target *target,ADDR addr,
1054  unsigned long length,unsigned char *buf) {
1055  vdebug(16,LA_TARGET,LF_TARGET,"reading target(%s) at 0x%"PRIxADDR" into %p (%d)\n",
1056  target->name,addr,buf,length);
1057  return target->ops->read(target,addr,length,buf);
1058 }
1059 
1060 unsigned long target_write_addr(struct target *target,ADDR addr,
1061  unsigned long length,unsigned char *buf) {
1062  vdebug(16,LA_TARGET,LF_TARGET,"writing target(%s) at 0x%"PRIxADDR" (%d)\n",
1063  target->name,addr,length);
1064  if (!target->writeable) {
1065  verror("target not writeable!\n");
1066  errno = EINVAL;
1067  return 0;
1068  }
1069  return target->ops->write(target,addr,length,buf);
1070 }
1071 
1072 int target_addr_v2p(struct target *target,tid_t tid,ADDR vaddr,ADDR *paddr) {
1073  if (!target->ops->addr_v2p) {
1074  vwarn("target(%s) does not support v2p addr translation!\n",target->name);
1075  errno = ENOTSUP;
1076  return -1;
1077  }
1079  "translating v 0x%"PRIxADDR" in tid %"PRIiTID"\n",vaddr,tid);
1080  return target->ops->addr_v2p(target,tid,vaddr,paddr);
1081 }
1082 
1083 unsigned char *target_read_physaddr(struct target *target,ADDR paddr,
1084  unsigned long length,unsigned char *buf) {
1085  if (!target->ops->read_phys) {
1086  vwarn("target(%s) does not support phys addr reads!\n",target->name);
1087  errno = ENOTSUP;
1088  return NULL;
1089  }
1091  "reading target(%s) at phys 0x%"PRIxADDR" into %p (%d)\n",
1092  target->name,paddr,buf,length);
1093  return target->ops->read_phys(target,paddr,length,buf);
1094 }
1095 
1096 unsigned long target_write_physaddr(struct target *target,ADDR paddr,
1097  unsigned long length,unsigned char *buf) {
1098  if (!target->ops->write_phys) {
1099  vwarn("target(%s) does not support phys addr writes!\n",target->name);
1100  errno = ENOTSUP;
1101  return 0;
1102  }
1104  "writing target(%s) at phys 0x%"PRIxADDR" (%d)\n",
1105  target->name,paddr,length);
1106  if (!target->writeable) {
1107  verror("target not writeable!\n");
1108  errno = EINVAL;
1109  return 0;
1110  }
1111  return target->ops->write_phys(target,paddr,length,buf);
1112 }
1113 
1114 const char *target_regname(struct target *target,REG reg) {
1115  vdebug(16,LA_TARGET,LF_TARGET,"target(%s) reg name %d)\n",
1116  target->name,reg);
1117  return arch_regname(target->arch,reg);
1118 }
1119 
1120 int target_regno(struct target *target,char *name,REG *reg) {
1121  vdebug(16,LA_TARGET,LF_TARGET,"target(%s) target reg %s)\n",
1122  target->name,name);
1123  return arch_regno(target->arch,name,reg);
1124 }
1125 
1126 int target_cregno(struct target *target,common_reg_t creg,REG *reg) {
1127  vdebug(16,LA_TARGET,LF_TARGET,"target(%s) common reg %d)\n",
1128  target->name,creg);
1129  return arch_cregno(target->arch,creg,reg);
1130 }
1131 
1133  vdebug(16,LA_TARGET,LF_TARGET,"reading target(%s:%"PRIiTID") reg %d)\n",
1134  target->name,tid,reg);
1135  if (target->ops->readreg)
1136  return target->ops->readreg(target,tid,reg);
1137  else
1138  return target->ops->readreg_tidctxt(target,tid,THREAD_CTXT_DEFAULT,reg);
1139 }
1140 
1143  "writing target(%s:%"PRIiTID") reg %d 0x%"PRIxREGVAL")\n",
1144  target->name,tid,reg,value);
1145  if (!target->writeable) {
1146  verror("target not writeable!\n");
1147  errno = EINVAL;
1148  return -1;
1149  }
1150  if (target->ops->writereg)
1151  return target->ops->writereg(target,tid,reg,value);
1152  else
1153  return target->ops->writereg_tidctxt(target,tid,THREAD_CTXT_DEFAULT,
1154  reg,value);
1155 }
1156 
1158  REG reg) {
1160  "reading target(%s:%"PRIiTID") reg %d tidctxt %d)\n",
1161  target->name,tid,reg,tidctxt);
1162  return target->ops->readreg_tidctxt(target,tid,tidctxt,reg);
1163 }
1164 
1166  REG reg,REGVAL value) {
1168  "writing target(%s:%"PRIiTID") reg %d tidctxt %d 0x%"PRIxREGVAL")\n",
1169  target->name,tid,reg,tidctxt,value);
1170  if (!target->writeable) {
1171  verror("target not writeable!\n");
1172  errno = EINVAL;
1173  return -1;
1174  }
1175  return target->ops->writereg_tidctxt(target,tid,tidctxt,reg,value);
1176 }
1177 
1179  REG treg;
1180 
1181  if (target_cregno(target,reg,&treg))
1182  return 0;
1183 
1184  return target_read_reg(target,tid,treg);
1185 }
1186 
1188  REGVAL value) {
1189  REG treg;
1190 
1191  if (!target->writeable) {
1192  verror("target not writeable!\n");
1193  errno = EINVAL;
1194  return -1;
1195  }
1196 
1197  if (target_cregno(target,reg,&treg))
1198  return 0;
1199 
1200  return target_write_reg(target,tid,treg,value);
1201 }
1202 
1203 GHashTable *target_copy_registers(struct target *target,tid_t tid) {
1205  "copying target(%s:%"PRIiTID") regs\n",
1206  target->name,tid);
1207  return target->ops->copy_registers(target,tid);
1208 }
1209 
1211  struct array_list *retval;
1212  GHashTableIter iter;
1213  struct target_thread *tthread;
1214  gpointer key;
1215 
1216  if (g_hash_table_size(target->threads) == 0)
1217  return NULL;
1218 
1219  retval = array_list_create(g_hash_table_size(target->threads));
1220 
1221  g_hash_table_iter_init(&iter,target->threads);
1222  while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
1223  if ((tid_t)(uintptr_t)key == TID_GLOBAL
1224  && tthread->tid != TID_GLOBAL)
1225  continue;
1226 
1227  array_list_append(retval,(void *)(ptr_t)tthread->tid);
1228  }
1229 
1230  return retval;
1231 }
1232 
1234  struct array_list *retval;
1235  GHashTableIter iter;
1236  struct target_thread *tthread;
1237  gpointer key;
1238 
1239  retval = array_list_create(g_hash_table_size(target->threads));
1240 
1241  g_hash_table_iter_init(&iter,target->threads);
1242  while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
1243  if ((tid_t)(uintptr_t)key == TID_GLOBAL
1244  && tthread->tid != TID_GLOBAL)
1245  continue;
1246 
1247  array_list_append(retval,tthread);
1248  }
1249 
1250  return retval;
1251 }
1252 
1253 GHashTable *target_hash_threads(struct target *target) {
1254  GHashTable *retval;
1255  GHashTableIter iter;
1256  gpointer key;
1257  struct target_thread *tthread;
1258 
1259  retval = g_hash_table_new_full(g_direct_hash,g_direct_equal,NULL,NULL);
1260 
1261  g_hash_table_iter_init(&iter,target->threads);
1262  while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread))
1263  if ((tid_t)(uintptr_t)key == TID_GLOBAL
1264  && tthread->tid != TID_GLOBAL)
1265  continue;
1266 
1267  g_hash_table_insert(retval,key,tthread);
1268 
1269  return retval;
1270 }
1271 
1273  vdebug(12,LA_TARGET,LF_TARGET,"target(%s)\n",target->name);
1274  return target->ops->list_available_tids(target);
1275 }
1276 
1278  int i;
1279  struct array_list *tids;
1280  GHashTable *retval;
1281  tid_t tid;
1282 
1283  tids = target_list_available_tids(target);
1284  if (!tids) {
1285  verror("could not load available tids!\n");
1286  return NULL;
1287  }
1288 
1289  retval = g_hash_table_new_full(g_direct_hash,g_direct_equal,NULL,NULL);
1290 
1291  for (i = 0; i < array_list_len(tids); ++i) {
1292  tid = (tid_t)(ptr_t)array_list_item(tids,i);
1293  g_hash_table_insert(retval,(gpointer)(ptr_t)tid,(gpointer)(ptr_t)tid);
1294  }
1295  array_list_free(tids);
1296 
1297  return retval;
1298 }
1299 
1301  vdebug(12,LA_TARGET,LF_TARGET,"target(%s)\n",target->name);
1302  return target->ops->load_available_threads(target,force);
1303 }
1304 
1306  int force) {
1307  vdebug(8,LA_TARGET,LF_TARGET,"loading target(%s) current thread\n",target->name);
1308  return target->ops->load_current_thread(target,force);
1309 }
1310 
1312  int force) {
1313  vdebug(8,LA_TARGET,LF_TARGET,"loading target(%s:%"PRIiTID") thread\n",
1314  target->name,tid);
1315  return target->ops->load_thread(target,tid,force);
1316 }
1317 
1318 int target_load_all_threads(struct target *target,int force) {
1319  vdebug(8,LA_TARGET,LF_TARGET,"loading all target(%s) threads\n",target->name);
1320  return target->ops->load_all_threads(target,force);
1321 }
1322 
1323 int target_pause_thread(struct target *target,tid_t tid,int nowait) {
1324  vdebug(12,LA_TARGET,LF_TARGET,"pausing target(%s) thread %"PRIiTID" (nowait=%d)\n",
1325  target->name,tid,nowait);
1326  return target->ops->pause_thread(target,tid,nowait);
1327 }
1328 
1330  vdebug(8,LA_TARGET,LF_TARGET,"flushing target(%s) current thread\n",target->name);
1331  return target->ops->flush_current_thread(target);
1332 }
1333 
1335  vdebug(8,LA_TARGET,LF_TARGET,"flushing target(%s:%"PRIiTID") thread\n",
1336  target->name,tid);
1337  return target->ops->flush_thread(target,tid);
1338 }
1339 
1341  GHashTableIter iter;
1342  struct target *overlay;
1343  int rc;
1344 
1346  "flushing all target(%s) threads\n",target->name);
1347 
1348  /*
1349  * Do it for all the overlays first.
1350  */
1351  g_hash_table_iter_init(&iter,target->overlays);
1352  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&overlay)) {
1354  "flushing all overlay target(%s) threads\n",overlay->name);
1355  rc = target_flush_all_threads(overlay);
1357  "flushing all overlay target(%s) threads (%d)\n",overlay->name,rc);
1358  }
1359 
1360  if (target->ops->flush_all_threads)
1361  return target->ops->flush_all_threads(target);
1362  else {
1363  errno = ENOTSUP;
1364  return -1;
1365  }
1366 }
1367 
1369  int rc = 0;
1370  int i;
1371  struct array_list *cached_tids;
1372  GHashTable *real_tids;
1373  tid_t tid;
1374  struct target_thread *tthread;
1375 
1376  vdebug(8,LA_TARGET,LF_TARGET,"garbage collecting cached threads (%s)\n",target->name);
1377  if (target->ops->gc_threads)
1378  return target->ops->gc_threads(target);
1379 
1380 
1381  cached_tids = target_list_tids(target);
1382  if (!cached_tids) {
1383  verror("could not list cached threads!\n");
1384  return -1;
1385  }
1386 
1387  real_tids = target_hash_available_tids(target);
1388  if (!real_tids) {
1389  verror("could not load currently available threads!\n");
1390  array_list_free(cached_tids);
1391  return -1;
1392  }
1393 
1394  for (i = 0; i < array_list_len(cached_tids); ++i) {
1395  tid = (tid_t)(ptr_t)array_list_item(cached_tids,i);
1396 
1397  if (tid == TID_GLOBAL)
1398  continue;
1399 
1400  if (!g_hash_table_lookup_extended(real_tids,(gpointer)(ptr_t)tid,
1401  NULL,NULL)) {
1402  if (target->current_thread && target->current_thread->tid == tid) {
1403  vwarn("thread %d seems to no longer exist, but is the"
1404  " current thread; not detaching!\n",tid);
1405  continue;
1406  }
1407 
1409  "cached thread %"PRIiTID" no longer exists; detaching!\n",tid);
1410  tthread = target_lookup_thread(target,tid);
1411  target_detach_thread(target,tthread);
1412  ++rc;
1413  }
1414  }
1415  array_list_free(cached_tids);
1416  g_hash_table_destroy(real_tids);
1417 
1418  if (rc)
1419  vdebug(5,LA_TARGET,LF_TARGET,"garbage collected %d cached threads (%s)\n",
1420  rc,target->name);
1421 
1422  return rc;
1423 }
1424 
1426  char *buf,int bufsiz,
1427  int detail,char *sep,char *kvsep) {
1428  struct target_thread *tthread;
1429  int rc;
1430 
1431  if (!buf) {
1432  errno = EINVAL;
1433  return -1;
1434  }
1435 
1436  vdebug(16,LA_TARGET,LF_TARGET,"target(%s:%"PRIiTID") thread\n",
1437  target->name,tid);
1438 
1439  if (!(tthread = target_lookup_thread(target,tid))) {
1440  verror("thread %"PRIiTID" does not exist?\n",tid);
1441  return -1;
1442  }
1443 
1444  if (!sep)
1445  sep = ",";
1446  if (!kvsep)
1447  kvsep = "=";
1448 
1449  if (detail < -1)
1450  return snprintf(buf,bufsiz,"tid%s%"PRIiTID,kvsep,tthread->tid);
1451  else if (detail < 0)
1452  return snprintf(buf,bufsiz,"tid%s%"PRIiTID "%s" "name%s%s" "%s"
1453  "curctxt%s%d" "%s",
1454  kvsep,tid,sep,kvsep,tthread->name,sep,
1455  kvsep,tthread->tidctxt,sep);
1456  else if (!target->ops->thread_snprintf)
1457  return snprintf(buf,bufsiz,
1458  "tid%s%"PRIiTID "%s" "name%s%s" "%s" "curctxt%s%d" "%s"
1459  "ptid%s%"PRIiTID "%s" "tgid%s%"PRIiTID "%s"
1460  "uid%s%d" "%s" "gid%s%d",
1461  kvsep,tthread->tid,sep,kvsep,tthread->name,sep,
1462  kvsep,tthread->tidctxt,sep,
1463  kvsep,tthread->ptid,sep,kvsep,tthread->tgid,sep,
1464  kvsep,tthread->uid,sep,kvsep,tthread->gid);
1465  else {
1466  rc = snprintf(buf,bufsiz,
1467  "tid%s%"PRIiTID "%s" "name%s%s" "%s" "curctxt%s%d" "%s"
1468  "ptid%s%"PRIiTID "%s" "ptid%s%"PRIiTID "%s"
1469  "uid%s%d" "%s" "gid%s%d" "%s",
1470  kvsep,tthread->tid,sep,kvsep,tthread->name,sep,
1471  kvsep,tthread->tidctxt,sep,
1472  kvsep,tthread->ptid,sep,kvsep,tthread->tgid,sep,
1473  kvsep,tthread->uid,sep,kvsep,tthread->gid,sep);
1474  if (rc >= bufsiz)
1475  rc += target->ops->thread_snprintf(target,tthread,NULL,0,
1476  detail,sep,kvsep);
1477  else
1478  rc += target->ops->thread_snprintf(target,tthread,buf + rc,bufsiz - rc,
1479  detail,sep,kvsep);
1480  return rc;
1481  }
1482 }
1483 
1484 void target_dump_thread(struct target *target,tid_t tid,FILE *stream,int detail) {
1485  char buf[1024];
1486  struct target_thread *tthread;
1487 
1488  vdebug(16,LA_TARGET,LF_TARGET,"dumping target(%s:%"PRIiTID") thread\n",
1489  target->name,tid);
1490 
1491  if (!(tthread = target_lookup_thread(target,tid)))
1492  verror("thread %"PRIiTID" does not exist?\n",tid);
1493 
1494  if (target_thread_snprintf(target,tid,buf,sizeof(buf),detail,NULL,NULL) < 0)
1495  fprintf(stream ? stream : stdout,"tid(%"PRIiTID"): <API ERROR>\n",tid);
1496  else
1497  fprintf(stream ? stream : stdout,"tid(%"PRIiTID"): %s\n",tid,buf);
1498 }
1499 
1500 void target_dump_all_threads(struct target *target,FILE *stream,int detail) {
1501  struct target_thread *tthread;
1502  GHashTableIter iter;
1503 
1504  vdebug(16,LA_TARGET,LF_TARGET,"dumping all target(%s) threads\n",target->name);
1505 
1506  g_hash_table_iter_init(&iter,target->threads);
1507  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread))
1508  target_dump_thread(target,tthread->tid,stream,detail);
1509 }
1510 
1512  int rc;
1513  GHashTableIter iter;
1514  struct probepoint *probepoint;
1515  struct target *overlay;
1516  struct target_memmod *mmod;
1517  unsigned int rlen;
1518 
1519  if (!target->opened) {
1520  vdebug(3,LA_TARGET,LF_TARGET,"target(%s) already closed\n",target->name);
1521  return target->status;
1522  }
1523 
1524  vdebug(5,LA_TARGET,LF_TARGET,"closing target(%s)\n",target->name);
1525 
1526  /* Make sure! */
1527  target_pause(target);
1528 
1529  /*
1530  * Do it for all the overlays first.
1531  */
1532  g_hash_table_iter_init(&iter,target->overlays);
1533  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&overlay)) {
1535  "closing overlay target(%s)\n",overlay->name);
1536  rc = target_close(overlay);
1538  "closed overlay target(%s) (%d)\n",overlay->name,rc);
1539  }
1540 
1541  if (target->evloop)
1542  target_detach_evloop(target);
1543 
1544  target_flush_all_threads(target);
1545 
1546  /*
1547  * We have to free the soft probepoints manually, then remove all. We
1548  * can't remove an element during an iteration, but we *can* free
1549  * the data :).
1550  */
1551  vdebug(2,LA_PROBE,LF_PROBEPOINT,"%d soft probepoints to free!\n",
1552  g_hash_table_size(target->soft_probepoints));
1553  g_hash_table_iter_init(&iter,target->soft_probepoints);
1554  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&probepoint)) {
1555  probepoint_free_ext(probepoint);
1556  }
1557  g_hash_table_remove_all(target->soft_probepoints);
1558 
1559  /*
1560  * Free the memmods, if any are left.
1561  */
1562  g_hash_table_iter_init(&iter,target->mmods);
1563  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&mmod)) {
1564  g_hash_table_iter_remove(&iter);
1565 
1566  if (mmod->tmp)
1567  free(mmod->tmp);
1568  /* Breakpoint hack */
1569  if (mmod->mod && mmod->mod != target->arch->breakpoint_instrs)
1570  free(mmod->mod);
1571 
1572  if (mmod->orig) {
1573  rlen = target_write_addr(target,mmod->addr,mmod->orig_len,mmod->orig);
1574  if (rlen != mmod->orig_len) {
1575  verror("could not restore orig memory at 0x%"PRIxADDR";"
1576  " but cannot do anything!\n",mmod->addr);
1577  }
1578  }
1579 
1580  if (mmod->threads) {
1581  array_list_free(mmod->threads);
1582  }
1583  if (mmod->orig)
1584  free(mmod->orig);
1585 
1586  if (target_notify_sw_breakpoint(target,mmod->addr,0)) {
1588  "sw bp removal notification failed; ignoring\n");
1589  }
1590 
1591  free(mmod);
1592  }
1593 
1594  /* XXX: should we deal with memcache? No, let backends do it. */
1595 
1596  vdebug(5,LA_TARGET,LF_TARGET,"detach target(%s) (stay_paused = %d)\n",
1597  target->name,target->spec->stay_paused);
1598  if ((rc = target->ops->detach(target,target->spec->stay_paused))) {
1599  verror("detach target(%s) failed: %s\n",target->name,strerror(errno));
1600  }
1601 
1602  if (target->spec->kill_on_close)
1603  target_kill(target,target->spec->kill_on_close_sig);
1604 
1605  /*
1606  * Don't delete the threads yet; just unset current_thread.
1607  */
1608  target->current_thread = NULL;
1609 
1610  target->opened = 0;
1611 
1612  /*
1613  * Set the target and its core objects to be non-live.
1614  */
1615  OBJSDEAD(target,target);
1616 
1617  return target->status;
1618 }
1619 
1621  obj_flags_t orf,obj_flags_t nandf) {
1622  int retval;
1623  GHashTableIter iter;
1624  gpointer vp;
1625  struct target_thread *tthread;
1626  GList *t1;
1627  struct addrspace *space;
1628 
1629  /*
1630  * Notify all our children -- threads (which have no children),
1631  * spaces -- and call the target_ops propagation method too, if it
1632  * exists -- or call the personality method.
1633  */
1634  g_hash_table_iter_init(&iter,target->threads);
1635  while (g_hash_table_iter_next(&iter,NULL,&vp)) {
1636  tthread = (struct target_thread *)vp;
1637  tthread->obj_flags |= orf;
1638  tthread->obj_flags &= ~nandf;
1639  }
1640 
1641  v_g_list_foreach(target->spaces,t1,space) {
1642  space->obj_flags |= orf;
1643  space->obj_flags &= ~nandf;
1644  addrspace_obj_flags_propagate(space,orf,nandf);
1645  }
1646 
1647  SAFE_TARGET_OP(obj_flags_propagate,retval,0,target,orf,nandf);
1648 
1649  return retval;
1650 }
1651 
1652 int target_kill(struct target *target,int sig) {
1653  vdebug(5,LA_TARGET,LF_TARGET,"killing target(%s) with %d\n",target->name,sig);
1654  return target->ops->kill(target,sig);
1655 }
1656 
1657 void target_hold(struct target *target) {
1658  RHOLD(target,target);
1659 }
1660 
1662  REFCNT trefcnt;
1663  RPUT(target,target,target,trefcnt);
1664 }
1665 
1666 struct probe *target_lookup_probe(struct target *target,int probe_id) {
1667  return (struct probe *)g_hash_table_lookup(target->probes,
1668  (gpointer)(uintptr_t)probe_id);
1669 }
1670 
1671 struct action *target_lookup_action(struct target *target,int action_id) {
1672  return (struct action *)g_hash_table_lookup(target->actions,
1673  (gpointer)(uintptr_t)action_id);
1674 }
1675 
1677  tid_t tid,ADDR addr,
1678  int is_phys,int nowrite) {
1679  struct target_memmod *mmod;
1680  struct target_thread *tthread;
1681 
1682  tthread = target_lookup_thread(target,tid);
1683  if (!tthread) {
1684  verror("tid %"PRIiTID" does not exist!\n",tid);
1685  errno = ESRCH;
1686  return NULL;
1687  }
1688 
1689  mmod = target_memmod_lookup(target,tid,addr,is_phys);
1690 
1691  if (mmod) {
1692  if (mmod->type != MMT_BP) {
1693  verror("mmod already at 0x%"PRIxADDR"; but not breakpoint!\n",addr);
1694  errno = EADDRINUSE;
1695  return NULL;
1696  }
1697  else if (mmod->state != MMS_SUBST) {
1698  verror("mmod already at 0x%"PRIxADDR"; state is not SUBST (%d)!\n",
1699  addr,mmod->state);
1700  errno = EBUSY;
1701  return NULL;
1702  }
1703  else {
1704  /* Add us to the threads list if necessary. */
1705  if (array_list_find(mmod->threads,tthread) < 0)
1706  array_list_append(mmod->threads,tthread);
1707  else
1708  vwarn("tid %"PRIiTID" already on threads list; BUG!\n",tid);
1709  return mmod;
1710  }
1711  }
1712  else {
1713  mmod = target_memmod_create(target,tid,addr,is_phys,MMT_BP,
1714  target->arch->breakpoint_instrs,
1715  target->arch->breakpoint_instrs_len,nowrite);
1716  if (!mmod) {
1717  verror("could not create memmod for tid %"PRIiTID" at 0x%"PRIxADDR"!\n",
1718  tid,addr);
1719  return NULL;
1720  }
1721 
1722  if (target_notify_sw_breakpoint(target,addr,1))
1723  vwarn("sw bp insertion notification failed; ignoring\n");
1724 
1725  return mmod;
1726  }
1727 }
1728 
1730  tid_t tid,ADDR addr) {
1731  if (target->ops->insert_sw_breakpoint)
1732  return target->ops->insert_sw_breakpoint(target,tid,addr);
1733  else
1734  /* Just default to sw breakpoints on virt addrs. */
1735  return _target_insert_sw_breakpoint(target,tid,addr,0,0);
1736 }
1737 
1739  struct target_memmod *mmod) {
1740  int retval;
1741  ADDR addr;
1742 
1743  addr = mmod->addr;
1744  retval = target_memmod_release(target,tid,mmod);
1745  if (retval) {
1746  verror("could not remove memmod at 0x%"PRIxADDR" for tid %"PRIiTID"\n",
1747  addr,tid);
1748  return -1;
1749  }
1750 
1751  /* If this was the last thread, signal. */
1752  if (!target_memmod_lookup(target,tid,addr,mmod->is_phys)) {
1753  if (target_notify_sw_breakpoint(target,addr,0))
1754  vwarn("sw bp removal notification failed; ignoring\n");
1755  }
1756 
1757  return 0;
1758 }
1759 
1761  struct target_memmod *mmod) {
1762  if (target->ops->remove_sw_breakpoint)
1763  return target->ops->remove_sw_breakpoint(target,tid,mmod);
1764  else
1765  return _target_remove_sw_breakpoint(target,tid,mmod);
1766 }
1767 
1769  struct target_memmod *mmod) {
1770  return target_memmod_set(target,tid,mmod);
1771 }
1772 
1774  struct target_memmod *mmod) {
1775  if (target->ops->enable_sw_breakpoint)
1776  return target->ops->enable_sw_breakpoint(target,tid,mmod);
1777 
1778  return _target_enable_sw_breakpoint(target,tid,mmod);
1779 }
1780 
1782  struct target_memmod *mmod) {
1783  return target_memmod_unset(target,tid,mmod);
1784 }
1785 
1787  struct target_memmod *mmod) {
1788  if (target->ops->disable_sw_breakpoint)
1789  return target->ops->disable_sw_breakpoint(target,tid,mmod);
1790  else
1791  return _target_disable_sw_breakpoint(target,tid,mmod);
1792 }
1793 
1795  struct target_memmod *mmod,
1796  unsigned char *code,unsigned long code_len) {
1797  return target_memmod_set_tmp(target,tid,mmod,code,code_len);
1798 }
1799 
1801  struct target_memmod *mmod,
1802  unsigned char *code,unsigned long code_len) {
1803  if (target->ops->change_sw_breakpoint)
1804  return target->ops->change_sw_breakpoint(target,tid,mmod,code,code_len);
1805  else
1806  return _target_change_sw_breakpoint(target,tid,mmod,code,code_len);
1807 }
1808 
1810  struct target_memmod *mmod) {
1811  return target_memmod_set(target,tid,mmod);
1812 }
1813 
1815  struct target_memmod *mmod) {
1816  if (target->ops->unchange_sw_breakpoint)
1817  return target->ops->unchange_sw_breakpoint(target,tid,mmod);
1818  else
1819  return _target_unchange_sw_breakpoint(target,tid,mmod);
1820 }
1821 
1823  REG retval;
1824  if (!target->ops->get_unused_debug_reg) {
1825  errno = ENOTSUP;
1826  return -1;
1827  }
1828  vdebug(5,LA_TARGET,LF_TARGET,"getting unused debug reg for target(%s):%"PRIiTID"\n",
1829  target->name,tid);
1830  retval = target->ops->get_unused_debug_reg(target,tid);
1831  vdebug(5,LA_TARGET,LF_TARGET,"got unused debug reg for target(%s):%"PRIiTID": %"PRIiREG"\n",
1832  target->name,tid,retval);
1833  return retval;
1834 }
1835 
1838  "setting hw breakpoint at 0x%"PRIxADDR" on target(%s:%"PRIiTID") dreg %d\n",
1839  addr,target->name,tid,reg);
1840  return target->ops->set_hw_breakpoint(target,tid,reg,addr);
1841 }
1842 
1844  probepoint_whence_t whence,int watchsize) {
1846  "setting hw watchpoint at 0x%"PRIxADDR" on target(%s:%"PRIiTID") dreg %d (%d)\n",
1847  addr,target->name,tid,reg,watchsize);
1848  return target->ops->set_hw_watchpoint(target,tid,reg,addr,whence,watchsize);
1849 }
1850 
1853  "removing hw breakpoint on target(%s:%"PRIiTID") dreg %d\n",
1854  target->name,tid,reg);
1855  return target->ops->unset_hw_breakpoint(target,tid,reg);
1856 }
1857 
1860  "removing hw watchpoint on target(%s:%"PRIiTID") dreg %d\n",
1861  target->name,tid,reg);
1862  return target->ops->unset_hw_watchpoint(target,tid,reg);
1863 }
1864 
1867  "disable hw breakpoints on target(%s:%"PRIiTID")\n",target->name,tid);
1868  return target->ops->disable_hw_breakpoints(target,tid);
1869 }
1870 
1873  "enable hw breakpoints on target(%s:%"PRIiTID")\n",target->name,tid);
1874  return target->ops->enable_hw_breakpoints(target,tid);
1875 }
1876 
1879  "disable hw breakpoint %"PRIiREG" on target(%s:%"PRIiTID")\n",
1880  dreg,target->name,tid);
1881  return target->ops->disable_hw_breakpoint(target,tid,dreg);
1882 }
1883 
1886  "enable hw breakpoint %"PRIiREG" on target(%s:%"PRIiTID")\n",
1887  dreg,target->name,tid);
1888  return target->ops->enable_hw_breakpoint(target,tid,dreg);
1889 }
1890 
1892  int notification) {
1893  if (target->ops->notify_sw_breakpoint) {
1895  "notify sw breakpoint (%d) on target(%s)\n",
1896  notification,target->name);
1897  return target->ops->notify_sw_breakpoint(target,addr,notification);
1898  }
1899  else
1900  return 0;
1901 }
1902 
1903 int target_singlestep(struct target *target,tid_t tid,int isbp) {
1904  vdebug(5,LA_TARGET,LF_TARGET,"single stepping target(%s:%"PRIiTID") isbp=%d\n",
1905  target->name,tid,isbp);
1906  return target->ops->singlestep(target,tid,isbp,NULL);
1907 }
1908 
1910  if (target->ops->singlestep_end) {
1911  vdebug(5,LA_TARGET,LF_TARGET,"ending single stepping of target(%s:%"PRIiTID")\n",
1912  target->name,tid);
1913  return target->ops->singlestep_end(target,tid,NULL);
1914  }
1915  return 0;
1916 }
1917 
1919  tid_t retval = 0;
1920 
1921  vdebug(9,LA_TARGET,LF_TARGET,"gettid target(%s)\n",target->name);
1922  retval = target->ops->gettid(target);
1923  vdebug(5,LA_TARGET,LF_TARGET,"gettid target(%s) -> 0x%"PRIx64" \n",
1924  target->name,retval);
1925 
1926  return retval;
1927 }
1928 
1929 uint64_t target_get_tsc(struct target *target) {
1930  if (target->ops->get_tsc)
1931  return target->ops->get_tsc(target);
1932  errno = EINVAL;
1933  return UINT64_MAX;
1934 }
1935 
1936 uint64_t target_get_time(struct target *target) {
1937  if (target->ops->get_time)
1938  return target->ops->get_time(target);
1939  errno = EINVAL;
1940  return UINT64_MAX;
1941 }
1942 
1943 uint64_t target_get_counter(struct target *target) {
1944  if (target->ops->get_counter)
1945  return target->ops->get_counter(target);
1946  errno = EINVAL;
1947  return UINT64_MAX;
1948 }
1949 
1950 int target_enable_feature(struct target *target,int feature,void *arg) {
1951  if (target->ops->enable_feature)
1952  return target->ops->enable_feature(target,feature,arg);
1953  errno = EINVAL;
1954  return -1;
1955 }
1956 
1957 int target_disable_feature(struct target *target,int feature) {
1958  if (target->ops->disable_feature)
1959  return target->ops->disable_feature(target,feature);
1960  errno = EINVAL;
1961  return -1;
1962 }
1963 
1965  struct target_thread *tthread;
1966 
1967  tthread = target_lookup_thread(target,tid);
1968  if (!tthread) {
1969  verror("no such thread %"PRIiTID"\n",tid);
1970  errno = EINVAL;
1971  return THREAD_STATUS_UNKNOWN;
1972  }
1973 
1974  return tthread->status;
1975 }
1976 
#define SAFE_TARGET_OP_WARN_NORET(op, outvar, expoutval, target,...)
Definition: target.h:825
struct target * target_instantiate_overlay(struct target *target, tid_t tid, struct target_spec *spec)
Definition: target_api.c:757
int target_thread_snprintf(struct target *target, tid_t tid, char *buf, int bufsiz, int detail, char *sep, char *kvsep)
Definition: target_api.c:1425
void * xhstate
Definition: evloop.h:90
uint8_t * breakpoint_instrs
Definition: arch.h:149
int target_flush_all_threads(struct target *target)
Definition: target_api.c:1340
tid_t target_lookup_overlay_thread_by_name(struct target *target, char *name)
Definition: target_api.c:713
int target_obj_flags_propagate(struct target *target, obj_flags_t orf, obj_flags_t nandf)
Definition: target_api.c:1620
struct array_list * threads
Definition: target.h:385
#define vwarnopt(level, area, flags, format,...)
Definition: log.h:37
int(* singlestep)(struct target *target, tid_t tid, int isbp, struct target *overlay)
Definition: target_api.h:3111
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:1800
int(* flush_current_thread)(struct target *target)
Definition: target_api.h:3003
#define SAFE_TARGET_OP(op, outvar, expoutval, target,...)
Definition: target.h:795
#define PRIiOFFSET
Definition: common.h:70
target_mode_t target_mode
Definition: target_api.h:2210
struct target * base
Definition: target_api.h:2654
#define SAFE_PERSONALITY_OP_WARN(op, outvar, expoutval, target,...)
Definition: target.h:748
#define RHOLDW(x, hx)
Definition: common.h:623
void * backend_spec
Definition: target_api.h:2290
thread_bpmode_t bpmode
Definition: target_api.h:2211
int arch_regno(struct arch *arch, char *name, REG *reg)
Definition: arch.c:58
unsigned char *(* read)(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
Definition: target_api.h:2953
int32_t tid_t
Definition: common.h:36
int(* addr_v2p)(struct target *target, tid_t tid, ADDR vaddr, ADDR *paddr)
Definition: target_api.h:2975
void * rhstate
Definition: evloop.h:86
target_status_t
Definition: target_api.h:197
int(* disable_hw_breakpoints)(struct target *target, tid_t tid)
Definition: target_api.h:3105
void addrspace_obj_flags_propagate(struct addrspace *space, obj_flags_t orf, obj_flags_t nandf)
Definition: memory.c:162
int target_load_available_threads(struct target *target, int force)
Definition: target_api.c:1300
common_reg_t
Definition: arch.h:73
tid_t target_lookup_overlay_thread_by_id(struct target *target, int id)
Definition: target_api.c:693
struct target_spec * tspec
Definition: dumptarget.c:113
int(* flush_all_threads)(struct target *target)
Definition: target_api.h:3004
uint64_t target_get_tsc(struct target *target)
Definition: target_api.c:1929
GHashTable * overlays
Definition: target_api.h:2663
Definition: probe.h:392
int target_set_hw_watchpoint(struct target *target, tid_t tid, REG reg, ADDR addr, probepoint_whence_t whence, int watchsize)
Definition: target_api.c:1843
struct linux_userproc_spec * linux_userproc_build_spec(void)
int(* unchange_sw_breakpoint)(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.h:3096
GHashTable * soft_probepoints
Definition: target_api.h:2735
#define PRIiREG
Definition: common.h:94
static uint64_t unsigned int i
tid_t(* gettid)(struct target *target)
Definition: target_api.h:2991
int target_monitor_evloop(struct evloop *evloop, struct timeval *timeout, struct target **target, target_status_t *status)
Definition: target_api.c:880
struct target_personality_ops * personality_ops
Definition: target_api.h:2585
int target_remove_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1760
int(* set_active_probing)(struct target *target, active_probe_flags_t flags)
Definition: target_api.h:3153
GHashTable * actions
Definition: target_api.h:2776
#define v_g_list_foreach_remove(glhead, glcur, glnext)
Definition: glib_wrapper.h:55
GHashTable * target_copy_registers(struct target *target, tid_t tid)
Definition: target_api.c:1203
struct target_spec *(* build_default_overlay_spec)(struct target *target, tid_t tid)
Definition: target_api.h:2911
struct action * target_lookup_action(struct target *target, int action_id)
Definition: target_api.c:1671
int target_resume(struct target *target)
Definition: target_api.c:1012
#define v_g_list_foreach(glhead, glcur, elm)
Definition: glib_wrapper.h:34
int target_is_evloop_attached(struct target *target, struct evloop *evloop)
Definition: target_api.c:863
int target_disable_feature(struct target *target, int feature)
Definition: target_api.c:1957
probepoint_whence_t
Definition: probe_api.h:234
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
uint8_t kill_on_close
Definition: target_api.h:2213
int _target_remove_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1738
void target_dump_thread(struct target *target, tid_t tid, FILE *stream, int detail)
Definition: target_api.c:1484
struct target_thread * base_thread
Definition: target_api.h:2655
struct os_process_spec * os_process_build_spec(void)
struct php_spec * php_build_spec(void)
Definition: target_php.c:938
void php_free_spec(struct php_spec *spec)
Definition: target_php.c:946
probepoint_style_t style
Definition: target_api.h:2212
int(* unset_hw_breakpoint)(struct target *target, tid_t tid, REG reg)
Definition: target_api.h:3103
int target_pause(struct target *target)
Definition: target_api.c:1027
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 target_pause_thread(struct target *target, tid_t tid, int nowait)
Definition: target_api.c:1323
int target_cregno(struct target *target, common_reg_t creg, REG *reg)
Definition: target_api.c:1126
int evloop_handleone(struct evloop *evloop, evloop_flags_t flags, struct timeval *timeout, struct evloop_fdinfo **handled_fdinfo, int *handled_fdtype, int *handled_hrc)
Definition: evloop.c:434
tid_t target_gettid(struct target *target)
Definition: target_api.c:1918
struct target_thread * global_thread
Definition: target_api.h:2685
int(* disable_sw_breakpoint)(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.h:3091
target_memmod_state_t state
Definition: target.h:388
char * name
Definition: target.h:939
int(* load_available_threads)(struct target *target, int force)
Definition: target_api.h:2999
int(* set_hw_breakpoint)(struct target *target, tid_t tid, REG reg, ADDR addr)
Definition: target_api.h:3099
int target_flush_thread(struct target *target, tid_t tid)
Definition: target_api.c:1334
int target_unchange_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1814
#define ptr_t
Definition: common.h:79
OFFSET phys_offset
Definition: target.h:987
GHashTable * target_hash_threads(struct target *target)
Definition: target_api.c:1253
int target_close(struct target *target)
Definition: target_api.c:1511
uint8_t stay_paused
Definition: target_api.h:2213
target_status_t(* status)(struct target *target)
Definition: target_api.h:2934
int(* writereg)(struct target *target, tid_t tid, REG reg, REGVAL value)
Definition: target_api.h:3064
#define verror(format,...)
Definition: log.h:30
int base_target_id
Definition: target_api.h:2206
unsigned char * target_read_addr(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
Definition: target_api.c:1053
int target_singlestep(struct target *target, tid_t tid, int isbp)
Definition: target_api.c:1903
int target_is_open(struct target *target)
Definition: target_api.c:1042
void linux_userproc_free_spec(struct linux_userproc_spec *lspec)
int target_regno(struct target *target, char *name, REG *reg)
Definition: target_api.c:1120
void probepoint_free_ext(struct probepoint *probepoint)
Definition: probe.c:570
void target_detach_thread(struct target *target, struct target_thread *tthread)
Definition: target.c:4115
int(* writereg_tidctxt)(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg, REGVAL value)
Definition: target_api.h:3069
Definition: evloop.h:66
GHashTable * mmods
Definition: target_api.h:2741
char * base_thread_name
Definition: target_api.h:2208
int(* pause)(struct target *target, int nowait)
Definition: target_api.h:2936
ADDR base_phys_addr
Definition: target.h:977
int target_personality_attach(struct target *target, char *personality, char *personality_lib)
Definition: target.c:6062
GList * target_instantiate_and_open_list(GList *target_specs, struct evloop *evloop, GList **error_specs)
Definition: target_api.c:287
int _target_disable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1781
struct target_thread *(* lookup_overlay_thread_by_id)(struct target *target, int id)
Definition: target_api.h:2917
thread_status_t status
Definition: target_api.h:2084
#define vwarn(format,...)
Definition: log.h:33
unsigned char * target_read_physaddr(struct target *target, ADDR paddr, unsigned long length, unsigned char *buf)
Definition: target_api.c:1083
uint32_t threadctl
Definition: target_api.h:2465
struct evloop * evloop
Definition: target_api.h:2638
int target_snprintf(struct target *target, char *buf, int bufsiz)
Definition: target_api.c:829
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
int(* thread_snprintf)(struct target *target, struct target_thread *tthread, char *buf, int bufsiz, int detail, char *sep, char *key_val_sep)
Definition: target_api.h:3007
uint64_t(* get_tsc)(struct target *target)
Definition: target_api.h:3129
REG target_get_unused_debug_reg(struct target *target, tid_t tid)
Definition: target_api.c:1822
int target_write_reg_ctxt(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg, REGVAL value)
Definition: target_api.c:1165
struct target_thread * target_load_thread(struct target *target, tid_t tid, int force)
Definition: target_api.c:1311
int target_singlestep_end(struct target *target, tid_t tid)
Definition: target_api.c:1909
int target_attach_evloop(struct target *target, struct evloop *evloop)
Definition: target_api.c:834
GHashTable *(* copy_registers)(struct target *target, tid_t tid)
Definition: target_api.h:3065
void target_monitor_clear_global_interrupt(void)
Definition: target.c:235
int target_flush_current_thread(struct target *target)
Definition: target_api.c:1329
thread_status_t target_thread_status(struct target *target, tid_t tid)
Definition: target_api.c:1964
uint64_t target_get_time(struct target *target)
Definition: target_api.c:1936
struct array_list * target_list_available_overlay_tids(struct target *target, target_type_t type)
Definition: target_api.c:656
struct xen_vm_spec * xen_vm_build_spec(void)
struct target_thread * target_load_current_thread(struct target *target, int force)
Definition: target_api.c:1305
struct target_thread * current_thread
Definition: target_api.h:2680
int target_monitor_was_interrupted(siginfo_t *last_siginfo)
Definition: target.c:226
struct probe * target_lookup_probe(struct target *target, int probe_id)
Definition: target_api.c:1666
int target_enable_hw_breakpoint(struct target *target, tid_t tid, REG dreg)
Definition: target_api.c:1884
struct target_memmod * target_memmod_lookup(struct target *target, tid_t tid, ADDR addr, int is_phys)
Definition: target.c:4897
struct array_list * target_list_tids(struct target *target)
Definition: target_api.c:1210
uint64_t(* get_time)(struct target *target)
Definition: target_api.h:3130
tid_t base_tid
Definition: target_api.h:2657
int(* detach)(struct target *target, int stay_paused)
Definition: target_api.h:2821
void target_free_spec(struct target_spec *spec)
Definition: target_api.c:453
target_memmod_type_t type
Definition: target.h:387
struct target_thread *(* load_thread)(struct target *target, tid_t tid, int force)
Definition: target_api.h:2994
int target_set_active_probing(struct target *target, active_probe_flags_t flags)
Definition: target_api.c:633
struct target * linux_userproc_instantiate(struct target_spec *spec, struct evloop *evloop)
#define TSTATUS(n)
Definition: target_api.h:252
Definition: target.h:342
int(* flush_thread)(struct target *target, tid_t tid)
Definition: target_api.h:3002
unsigned long target_write_physaddr(struct target *target, ADDR paddr, unsigned long length, unsigned char *buf)
Definition: target_api.c:1096
struct target_spec * target_build_default_overlay_spec(struct target *target, tid_t tid)
Definition: target_api.c:734
target_mode_t
Definition: target_api.h:187
char * target_name(struct target *target)
Definition: target_api.c:505
void gdb_free_spec(struct gdb_spec *xspec)
Definition: target_gdb.c:583
#define PRIxOFFSET
Definition: common.h:71
const char * target_regname(struct target *target, REG reg)
Definition: target_api.c:1114
int(* set_hw_watchpoint)(struct target *target, tid_t tid, REG reg, ADDR addr, probepoint_whence_t whence, probepoint_watchsize_t watchsize)
Definition: target_api.h:3100
REG(* get_unused_debug_reg)(struct target *target, tid_t tid)
Definition: target_api.h:3098
int _target_enable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1768
GHashTable * threads
Definition: target_api.h:2672
target_status_t(* monitor)(struct target *target)
Definition: target_api.h:2940
obj_flags_t obj_flags
Definition: target_api.h:2087
int target_memmod_unset(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target.c:5102
int(* resume)(struct target *target)
Definition: target_api.h:2938
#define EVLOOP_FDTYPE_X
Definition: evloop.h:32
int(* load_all_threads)(struct target *target, int force)
Definition: target_api.h:2998
ADDR tag
Definition: target.h:897
#define RHOLD(x, hx)
Definition: common.h:622
struct array_list *(* list_available_tids)(struct target *target)
Definition: target_api.h:2993
ADDR addr
Definition: target.h:392
int target_enable_feature(struct target *target, int feature, void *arg)
Definition: target_api.c:1950
int(* disable_hw_breakpoint)(struct target *target, tid_t tid, REG dreg)
Definition: target_api.h:3107
target_poll_outcome_t
Definition: target_api.h:394
Definition: probe.h:308
target_type_t target_type
Definition: target_api.h:2203
GList * spaces
Definition: target_api.h:2643
REGVAL(* readreg_tidctxt)(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg)
Definition: target_api.h:3067
const char * arch_regname(struct arch *arch, REG reg)
Definition: arch.c:51
obj_flags_t obj_flags
Definition: target.h:913
REGVAL target_read_creg(struct target *target, tid_t tid, common_reg_t reg)
Definition: target_api.c:1178
int(* remove_sw_breakpoint)(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.h:3087
#define v_g_list_foreach_safe(glhead, glcur, glnext, elm)
Definition: glib_wrapper.h:46
#define vdebug(devel, areas, flags, format,...)
Definition: log.h:302
int target_notify_sw_breakpoint(struct target *target, ADDR addr, int notification)
Definition: target_api.c:1891
int(* gc_threads)(struct target *target)
Definition: target_api.h:3006
int base_thread_id
Definition: target_api.h:2207
int(* set_active_probing)(struct target *target, active_probe_flags_t flags)
Definition: target_api.h:2853
obj_flags_t
Definition: object.h:43
GHashTable * target_hash_available_tids(struct target *target)
Definition: target_api.c:1277
int target_load_all_threads(struct target *target, int force)
Definition: target_api.c:1318
int arch_cregno(struct arch *arch, common_reg_t creg, REG *reg)
Definition: arch.c:73
int(* attach_evloop)(struct target *target, struct evloop *evloop)
Definition: target_api.h:2949
struct target * xen_vm_instantiate(struct target_spec *spec, struct evloop *evloop)
struct array_list * target_list_available_tids(struct target *target)
Definition: target_api.c:1272
int(* enable_sw_breakpoint)(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.h:3089
int target_detach_evloop(struct target *target)
Definition: target_api.c:846
int evloop_maxsize(struct evloop *evloop)
Definition: evloop.c:191
void * whstate
Definition: evloop.h:88
int(* notify_sw_breakpoint)(struct target *target, ADDR addr, int notification)
Definition: target_api.h:3109
struct arch * arch
Definition: target_api.h:2603
void os_process_free_spec(struct os_process_spec *spec)
int(* unset_hw_watchpoint)(struct target *target, tid_t tid, REG reg)
Definition: target_api.h:3104
void * calloc(size_t nmemb, size_t size)
Definition: debugserver.c:200
unsigned int is_phys
Definition: target.h:389
struct array_list * debugfile_load_opts_list
Definition: target_api.h:2237
unsigned int thread_ctxt_t
Definition: target_api.h:300
int target_disable_hw_breakpoints(struct target *target, tid_t tid)
Definition: target_api.c:1865
struct target * target
Definition: target_api.h:2078
GHashTable * overlay_aliases
Definition: target_api.h:2670
#define REGION_TYPE(n)
Definition: target_api.h:384
Definition: log.h:70
uint32_t REGVAL
Definition: common.h:66
#define THREAD_CTXT_DEFAULT
Definition: target_api.h:301
int target_memmod_release(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target.c:4933
Definition: log.h:71
struct target * target_lookup_target_id(int id)
Definition: target.c:332
target_status_t target_status(struct target *target)
Definition: target_api.c:1046
int target_disable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1786
#define PRIiTID
Definition: common.h:37
void target_hold(struct target *target)
Definition: target_api.c:1657
struct target_spec * target_build_spec(target_type_t type, target_mode_t mode)
Definition: target_api.c:410
int target_write_creg(struct target *target, tid_t tid, common_reg_t reg, REGVAL value)
Definition: target_api.c:1187
int target_kill(struct target *target, int sig)
Definition: target_api.c:1652
int8_t REG
Definition: common.h:93
target_type_t
Definition: target_api.h:163
int target_disable_hw_breakpoint(struct target *target, tid_t tid, REG dreg)
Definition: target_api.c:1877
uint64_t(* get_counter)(struct target *target)
Definition: target_api.h:3131
uint32_t opened
Definition: target_api.h:2465
int target_finalize(struct target *target)
Definition: target.c:1955
unsigned long(* write_phys)(struct target *target, ADDR paddr, unsigned long length, unsigned char *buf)
Definition: target_api.h:2981
unsigned int breakpoint_instrs_len
Definition: arch.h:150
struct target_thread *(* lookup_overlay_thread_by_name)(struct target *target, char *name)
Definition: target_api.h:2919
int(* enable_feature)(struct target *target, int feature, void *arg)
Definition: target_api.h:3133
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
char * infile
Definition: target_api.h:2286
REGVAL target_read_reg_ctxt(struct target *target, tid_t tid, thread_ctxt_t tidctxt, REG reg)
Definition: target_api.c:1157
int target_memmod_set(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target.c:5021
struct target_memmod * target_insert_sw_breakpoint(struct target *target, tid_t tid, ADDR addr)
Definition: target_api.c:1729
target_status_t target_poll(struct target *target, struct timeval *tv, target_poll_outcome_t *outcome, int *pstatus)
Definition: target_api.c:1001
char * name
Definition: target.h:898
thread_ctxt_t tidctxt
Definition: target_api.h:2080
GList * target_instantiate_and_open(struct target_spec *primary_target_spec, GList *base_target_specs, GList *overlay_target_specs, struct evloop *evloop, GList **error_specs)
Definition: target_api.c:92
int target_open(struct target *target)
Definition: target_api.c:513
thread_status_t
Definition: target_api.h:254
void target_release(struct target *target)
Definition: target_api.c:1661
target_status_t status
Definition: target_api.h:2503
#define EVLOOP_FDTYPE_W
Definition: evloop.h:31
int target_unset_hw_breakpoint(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1851
struct target * gdb_instantiate(struct target_spec *spec, struct evloop *evloop)
Definition: target_gdb.c:594
void target_dump_all_threads(struct target *target, FILE *stream, int detail)
Definition: target_api.c:1500
target_status_t target_monitor(struct target *target)
Definition: target_api.c:869
char * personality
Definition: target_api.h:2228
uint32_t REFCNT
Definition: common.h:124
int target_id(struct target *target)
Definition: target_api.c:509
#define PRIxADDR
Definition: common.h:67
#define EVLOOP_FDTYPE_R
Definition: evloop.h:30
int(* snprintf)(struct target *target, char *buf, int bufsiz)
Definition: target_api.h:2798
int kill_on_close_sig
Definition: target_api.h:2242
int(* detach_evloop)(struct target *target)
Definition: target_api.h:2950
struct target_spec * spec
Definition: target_api.h:2605
int target_memmod_set_tmp(struct target *target, tid_t tid, struct target_memmod *mmod, unsigned char *code, unsigned long code_len)
Definition: target.c:5179
int _target_unchange_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1809
struct target * target_instantiate(struct target_spec *spec, struct evloop *evloop)
Definition: target_api.c:55
#define RPUT(x, objtype, hx, rc)
Definition: common.h:624
ADDR base_virt_addr
Definition: target.h:978
REGVAL(* readreg)(struct target *target, tid_t tid, REG reg)
Definition: target_api.h:3063
void debugfile_load_opts_free(struct debugfile_load_opts *opts)
Definition: debug.c:1275
active_probe_flags_t
Definition: target_api.h:432
int target_set_hw_breakpoint(struct target *target, tid_t tid, REG reg, ADDR addr)
Definition: target_api.c:1836
int id
Definition: target_api.h:2514
region_type_t type
Definition: target.h:940
struct target_thread * target_lookup_thread(struct target *target, tid_t tid)
Definition: target.c:4023
int target_gc_threads(struct target *target)
Definition: target_api.c:1368
GHashTable * probes
Definition: target_api.h:2765
int target_enable_hw_breakpoints(struct target *target, tid_t tid)
Definition: target_api.c:1871
ADDR base_load_addr
Definition: target.h:968
int(* disable_feature)(struct target *target, int feature)
Definition: target_api.h:3134
unsigned long(* write)(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
Definition: target_api.h:2956
target_type_t target_type(struct target *target)
Definition: target_api.c:501
unsigned char *(* read_phys)(struct target *target, ADDR paddr, unsigned long length, unsigned char *buf)
Definition: target_api.h:2978
unsigned long target_write_addr(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
Definition: target_api.c:1060
struct gdb_spec * gdb_build_spec(void)
Definition: target_gdb.c:574
char * errfile
Definition: target_api.h:2288
int(* singlestep_end)(struct target *target, tid_t tid, struct target *overlay)
Definition: target_api.h:3113
Definition: log.h:177
uint64_t target_get_counter(struct target *target)
Definition: target_api.c:1943
char * outfile
Definition: target_api.h:2287
void xen_vm_free_spec(struct xen_vm_spec *xspec)
int target_addr_v2p(struct target *target, tid_t tid, ADDR vaddr, ADDR *paddr)
Definition: target_api.c:1072
int(* change_sw_breakpoint)(struct target *target, tid_t tid, struct target_memmod *mmod, unsigned char *code, unsigned long code_len)
Definition: target_api.h:3093
int(* init)(struct target *target)
Definition: target_api.h:2810
struct target_thread *(* load_current_thread)(struct target *target, int force)
Definition: target_api.h:2996
int base_id
Definition: target_api.h:2656
int target_enable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1773
struct target_memmod * _target_insert_sw_breakpoint(struct target *target, tid_t tid, ADDR addr, int is_phys, int nowrite)
Definition: target_api.c:1676
int target_unset_hw_watchpoint(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1858
int(* pause_thread)(struct target *target, tid_t tid, int nowait)
Definition: target_api.h:3000
struct target *(* instantiate_overlay)(struct target *target, struct target_thread *tthread, struct target_spec *spec, struct target_thread **ntthread)
Definition: target_api.h:2913
struct array_list * target_list_threads(struct target *target)
Definition: target_api.c:1233
struct target_memmod *(* insert_sw_breakpoint)(struct target *target, tid_t tid, ADDR addr)
Definition: target_api.h:3085
int(* enable_hw_breakpoint)(struct target *target, tid_t tid, REG dreg)
Definition: target_api.h:3108
int target_write_reg(struct target *target, tid_t tid, REG reg, REGVAL value)
Definition: target_api.c:1141
int(* kill)(struct target *target, int sig)
Definition: target_api.h:2823
#define TID_GLOBAL
Definition: target_api.h:145
target_status_t(* poll)(struct target *target, struct timeval *tv, target_poll_outcome_t *outcome, int *pstatus)
Definition: target_api.h:2941
struct target * t
Definition: dumptarget.c:48
active_probe_flags_t ap_flags
Definition: target_api.h:2220
uint8_t read_only
Definition: target_api.h:2213
GList * regions
Definition: target.h:909
#define OBJSDEAD(obj, type)
Definition: object.h:127
int(* enable_hw_breakpoints)(struct target *target, tid_t tid)
Definition: target_api.h:3106
#define PRIxREGVAL
Definition: common.h:72
struct array_list * target_list_overlays(struct target *target)
Definition: target_api.c:686
uint32_t writeable
Definition: target_api.h:2465
char * personality_lib
Definition: target_api.h:2233