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_linux_userproc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "config.h"
20 #include "glib_wrapper.h"
21 
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <sys/ptrace.h>
29 #include <sys/user.h>
30 #include <sys/reg.h>
31 #include <bits/wordsize.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <limits.h>
36 #include <errno.h>
37 #include <dirent.h>
38 #include <sys/time.h>
39 #include <argp.h>
40 #include <gelf.h>
41 #include <elf.h>
42 #include <libelf.h>
43 
44 #include "common.h"
45 #include "object.h"
46 #include "arch.h"
47 #include "arch_x86.h"
48 #include "arch_x86_64.h"
49 #include "waitpipe.h"
50 #include "evloop.h"
51 #include "binfile.h"
52 #include "dwdebug.h"
53 #include "dwdebug_priv.h"
54 #include "target_api.h"
55 #include "target.h"
56 #include "target_linux_userproc.h"
57 
58 #define EF_RF (0x00010000)
59 
60 /*
61  * Prototypes.
62  */
64  struct evloop *evloop);
65 
66 static struct target *linux_userproc_attach(struct target_spec *spec,
67  struct evloop *evloop);
68 static struct target *linux_userproc_launch(struct target_spec *spec,
69  struct evloop *evloop);
70 
71 static int linux_userproc_snprintf(struct target *target,
72  char *buf,int bufsiz);
73 static int linux_userproc_init(struct target *target);
74 static int linux_userproc_postloadinit(struct target *target);
75 static int linux_userproc_attach_internal(struct target *target);
76 static int linux_userproc_detach(struct target *target,int stay_paused);
77 static int linux_userproc_fini(struct target *target);
78 static int linux_userproc_kill(struct target *target,int sig);
79 static int linux_userproc_loadspaces(struct target *target);
80 static int linux_userproc_loadregions(struct target *target,
81  struct addrspace *space);
82 static int linux_userproc_loaddebugfiles(struct target *target,
83  struct addrspace *space,
84  struct memregion *region);
85 
86 static target_status_t linux_userproc_handle_exception(struct target *target,
88  int *again,void *priv);
89 
90 static struct target *
91 linux_userproc_instantiate_overlay(struct target *target,
92  struct target_thread *tthread,
93  struct target_spec *spec,
94  struct target_thread **ntthread);
95 static struct target_thread *
96 linux_userproc_lookup_overlay_thread_by_id(struct target *target,int id);
97 static struct target_thread *
98 linux_userproc_lookup_overlay_thread_by_name(struct target *target,char *name);
99 
100 static target_status_t linux_userproc_status(struct target *target);
101 static int linux_userproc_pause(struct target *target,int nowait);
102 static int linux_userproc_resume(struct target *target);
103 static target_status_t linux_userproc_monitor(struct target *target);
104 static target_status_t linux_userproc_poll(struct target *target,
105  struct timeval *tv,
106  target_poll_outcome_t *outcome,
107  int *pstatus);
110 static unsigned char *linux_userproc_read(struct target *target,
111  ADDR addr,
112  unsigned long length,
113  unsigned char *buf);
114 static unsigned long linux_userproc_write(struct target *target,
115  ADDR addr,
116  unsigned long length,
117  unsigned char *buf);
118 
119 static tid_t linux_userproc_gettid(struct target *target);
120 static void linux_userproc_free_thread_state(struct target *target,void *state);
121 static struct target_thread *linux_userproc_load_thread(struct target *target,
122  tid_t tid,int force);
123 static struct target_thread *linux_userproc_load_current_thread(struct target *target,
124  int force);
125 static int linux_userproc_load_all_threads(struct target *target,int force);
126 static int linux_userproc_pause_thread(struct target *target,tid_t tid,
127  int nowait);
128 static int linux_userproc_flush_thread(struct target *target,tid_t tid);
129 static int linux_userproc_flush_current_thread(struct target *target);
130 static int linux_userproc_flush_all_threads(struct target *target);
131 static int linux_userproc_invalidate_all_threads(struct target *target);
132 static int linux_userproc_thread_snprintf(struct target *target,
133  struct target_thread *tthread,
134  char *buf,int bufsiz,
135  int detail,char *sep,char *kvsep);
136 
137 static REGVAL linux_userproc_read_reg(struct target *target,tid_t tid,REG reg);
138 static int linux_userproc_write_reg(struct target *target,tid_t tid,REG reg,
139  REGVAL value);
140 static GHashTable *linux_userproc_copy_registers(struct target *target,tid_t tid);
141 static REG linux_userproc_get_unused_debug_reg(struct target *target,tid_t tid);
142 static int linux_userproc_set_hw_breakpoint(struct target *target,tid_t tid,
143  REG num,ADDR addr);
144 static int linux_userproc_set_hw_watchpoint(struct target *target,tid_t tid,
145  REG num,ADDR addr,
146  probepoint_whence_t whence,
147  probepoint_watchsize_t watchsize);
148 static int linux_userproc_unset_hw_breakpoint(struct target *target,tid_t tid,
149  REG num);
150 static int linux_userproc_unset_hw_watchpoint(struct target *target,tid_t tid,
151  REG num);
155  REG dreg);
157  REG dreg);
159  int notification);
160 int linux_userproc_singlestep(struct target *target,tid_t tid,int isbp,
161  struct target *overlay);
163  struct target *overlay);
164 
165 
166 static int linux_userproc_evloop_add_tid(struct target *target,int tid);
167 static int linux_userproc_evloop_del_tid(struct target *target,int tid);
168 
169 /*
170  * Set up the target interface for this library.
171  */
173  .snprintf = linux_userproc_snprintf,
174 
175  .init = linux_userproc_init,
176  .fini = linux_userproc_fini,
177  .attach = linux_userproc_attach_internal,
178  .detach = linux_userproc_detach,
179  .kill = linux_userproc_kill,
180  .loadspaces = linux_userproc_loadspaces,
181  .loadregions = linux_userproc_loadregions,
182  .loaddebugfiles = linux_userproc_loaddebugfiles,
183  .postloadinit = linux_userproc_postloadinit,
184 
185  .handle_exception = linux_userproc_handle_exception,
186  .handle_break = probepoint_bp_handler,
187  .handle_step = probepoint_ss_handler,
188  .handle_interrupted_step = NULL,
189 
190  .instantiate_overlay = linux_userproc_instantiate_overlay,
191  .lookup_overlay_thread_by_id = linux_userproc_lookup_overlay_thread_by_id,
192  .lookup_overlay_thread_by_name = linux_userproc_lookup_overlay_thread_by_name,
193 
194  .status = linux_userproc_status,
195  .pause = linux_userproc_pause,
196  .resume = linux_userproc_resume,
197  .monitor = linux_userproc_monitor,
198  .poll = linux_userproc_poll,
199  .read = linux_userproc_read,
200  .write = linux_userproc_write,
201 
202  .gettid = linux_userproc_gettid,
203  .free_thread_state = linux_userproc_free_thread_state,
204  /* There are never any untracked threads in this target. */
205  .list_available_tids = target_list_tids,
206  /* There are never any untracked threads in this target. */
207  .load_available_threads = linux_userproc_load_all_threads,
208  .load_thread = linux_userproc_load_thread,
209  .load_current_thread = linux_userproc_load_current_thread,
210  .load_all_threads = linux_userproc_load_all_threads,
211  .pause_thread = linux_userproc_pause_thread,
212  .flush_thread = linux_userproc_flush_thread,
213  .flush_current_thread = linux_userproc_flush_current_thread,
214  .flush_all_threads = linux_userproc_flush_all_threads,
215  .thread_snprintf = linux_userproc_thread_snprintf,
216 
217  .attach_evloop = linux_userproc_attach_evloop,
218  .detach_evloop = linux_userproc_detach_evloop,
219 
220  .readreg = linux_userproc_read_reg,
221  .writereg = linux_userproc_write_reg,
222  .get_unused_debug_reg = linux_userproc_get_unused_debug_reg,
223  .set_hw_breakpoint = linux_userproc_set_hw_breakpoint,
224  .set_hw_watchpoint = linux_userproc_set_hw_watchpoint,
225  .unset_hw_breakpoint = linux_userproc_unset_hw_breakpoint,
226  .unset_hw_watchpoint = linux_userproc_unset_hw_watchpoint,
227  .disable_hw_breakpoints = linux_userproc_disable_hw_breakpoints,
228  .enable_hw_breakpoints = linux_userproc_enable_hw_breakpoints,
229  .disable_hw_breakpoint = linux_userproc_disable_hw_breakpoint,
230  .enable_hw_breakpoint = linux_userproc_enable_hw_breakpoint,
231  .notify_sw_breakpoint = linux_userproc_notify_sw_breakpoint,
232  .singlestep = linux_userproc_singlestep,
233  .singlestep_end = linux_userproc_singlestep_end,
234 };
235 
236 struct argp_option linux_userproc_argp_opts[] = {
237  /* These options set a flag. */
238  { "pid",'p',"PID",0,"A target process to attach to.",-4 },
239  { "program",'b',"FILE",0,"A program to launch as the target.",-4 },
240  { "args",'a',"LIST",0,"A comma-separated argument list.",-4 },
241  { "envvars",'e',"LIST",0,"A comma-separated envvar list.",-4 },
242  { 0,0,0,0,0,0 },
243 };
244 
245 int linux_userproc_spec_to_argv(struct target_spec *spec,int *argc,char ***argv) {
246  struct linux_userproc_spec *lspec =
247  (struct linux_userproc_spec *)spec->backend_spec;
248  char **av = NULL;
249  int ac = 0;
250  int rc;
251  int i;
252  int envstrlen;
253  int j;
254  char *p;
255 
256  if (!lspec) {
257  if (argv)
258  *argv = NULL;
259  if (argc)
260  *argc = 0;
261  return 0;
262  }
263 
264  if (lspec->program) {
265  /* -- <lspec->program> */
266  ac = 2;
267  if (lspec->argv)
268  for (i = 0; lspec->argv[i] != NULL; ++i,++ac) ;
269  envstrlen = 0;
270  if (lspec->envp) {
271  /* -e */
272  ++ac;
273  for (i = 0; lspec->envp[i] != NULL; ++i,++ac)
274  envstrlen += strlen(lspec->envp[i]) + 1;
275  }
276 
277  av = calloc(ac + 1,sizeof(char *));
278 
279  j = 0;
280 
281  if (lspec->envp) {
282  av[j++] = strdup("-e");
283  envstrlen += 1;
284  av[j] = malloc(envstrlen);
285  i = 0;
286  p = av[j];
287  while (p < (av[j] + envstrlen)) {
288  rc = snprintf(p,(av[j] + envstrlen) - p,"%s",lspec->envp[i]);
289  ++i;
290  /*
291  * Since snprintf returns the num chars that were or
292  * would have been printed, this will still term the loop
293  * even though the final value of p is invalid for
294  * future use.
295  */
296  p += rc;
297  }
298  ++j;
299  }
300 
301  av[j++] = strdup("--");
302  av[j++] = strdup(lspec->program);
303 
304  if (lspec->argv) {
305  for (i = 0; lspec->argv[i] != NULL; ++i) {
306  av[j++] = strdup(lspec->argv[i]);
307  }
308  }
309  av[j] = NULL;
310  ac = j + 1;
311  }
312  else if (lspec->pid > -1) {
313  av = calloc(3,sizeof(char *));
314  av[0] = strdup("-p");
315  av[1] = malloc(11);
316  snprintf(av[1],11,"%d",lspec->pid);
317  ac = 2;
318  }
319 
320  if (argv)
321  *argv = av;
322  if (argc)
323  *argc = ac;
324 
325  return 0;
326 }
327 
328 error_t linux_userproc_argp_parse_opt(int key,char *arg,struct argp_state *state) {
329  struct target_argp_parser_state *tstate = \
330  (struct target_argp_parser_state *)state->input;
331  struct target_spec *spec = NULL;
332  struct linux_userproc_spec *lspec;
333  struct argp_option *opti;
334  int ourkey;
335  int count;
336  int i;
337  int previ;
338  char *argdup;
339 
340  if (key == ARGP_KEY_INIT)
341  return 0;
342  else if (!state->input)
343  return ARGP_ERR_UNKNOWN;
344 
345  if (tstate)
346  spec = tstate->spec;
347 
348  /*
349  * Check to see if this is really one of our keys. If it is, we
350  * need to see if some other backend has already started parsing
351  * args; if it has, we throw an error. Otherwise, we assume we are
352  * using this backend, and process the arg.
353  */
354  if (spec && spec->target_type == TARGET_TYPE_NONE && tstate->quoted_argc)
355  ourkey = 1;
356  else {
357  ourkey = 0;
358  for (opti = &linux_userproc_argp_opts[0]; opti->key != 0; ++opti) {
359  if (key == opti->key) {
360  ourkey = 1;
361  break;
362  }
363  }
364  }
365 
366  if (ourkey) {
367  /* Only claim this as ours if it was one of our keys. */
371  }
372  else if (spec->target_type != TARGET_TYPE_PTRACE) {
373  verror("cannot mix arguments for ptrace target (%c) with non-ptrace"
374  " target!\n",key);
375  return EINVAL;
376  }
377 
378  }
379  /*
380  * Allow ptrace target to swallow quoted args.
381  */
382  else if (spec->target_type == TARGET_TYPE_NONE && tstate->quoted_argc) {
383  ourkey = 1;
384  }
385 
387  lspec = (struct linux_userproc_spec *)spec->backend_spec;
388  else
389  lspec = NULL;
390 
391  switch (key) {
392  case ARGP_KEY_ARG:
393  case ARGP_KEY_ARGS:
394  return ARGP_ERR_UNKNOWN;
395  case ARGP_KEY_INIT:
396  case ARGP_KEY_END:
397  return 0;
398  case ARGP_KEY_NO_ARGS:
399  case ARGP_KEY_SUCCESS:
400  /*
401  * Steal any quoted args here...
402  */
403  if (spec->target_type == TARGET_TYPE_PTRACE && tstate->quoted_argc) {
404  if (lspec->program) {
405  verror("cannot specify both binary to launch and an argv!\n");
406  return EINVAL;
407  }
408  lspec->program = strdup(tstate->quoted_argv[0]);
409  lspec->argv = calloc(tstate->quoted_argc + 1,sizeof(char *));
410  for (i = 0; i < tstate->quoted_argc; ++i)
411  lspec->argv[i] = strdup(tstate->quoted_argv[i]);
412  lspec->argv[tstate->quoted_argc] = NULL;
413 
414  /* Report our theft :). */
415  tstate->quoted_argc = 0;
416  }
417  return 0;
418  case ARGP_KEY_ERROR:
419  case ARGP_KEY_FINI:
420  if (lspec && lspec->pid > -1 && lspec->program) {
421  verror("cannot specify both pid (to attach) and binary (to launch!)\n");
422  return EINVAL;
423  }
424  return 0;
425 
426  case 'p':
427  lspec->pid = atoi(arg);
428  break;
429  case 'b':
430  lspec->program = strdup(arg);
431  if (lspec->argv)
432  lspec->argv[0] = strdup(arg);
433  break;
434  case 'a':
435  count = 1;
436  for (i = 0; arg[i] != '\0'; ++i) {
437  if (arg[i] == ',')
438  ++count;
439  }
440  lspec->argv = calloc(count+2,sizeof(char *));
441  if (lspec->program)
442  lspec->argv[0] = strdup(lspec->program);
443  count = 1;
444  previ = 0;
445  argdup = strdup(arg);
446  for (i = 0; argdup[i] != '\0'; ++i) {
447  if (argdup[i] == ',') {
448  argdup[i] = '\0';
449  lspec->argv[count++] = strdup(&argdup[previ]);
450  previ = i + 1;
451  }
452  }
453  free(argdup);
454  lspec->argv[count+1] = NULL;
455  break;
456  case 'e':
457  count = 1;
458  for (i = 0; arg[i] != '\0'; ++i) {
459  if (arg[i] == ',')
460  ++count;
461  }
462  lspec->envp = calloc(count+1,sizeof(char *));
463  lspec->envp[0] = arg;
464  count = 1;
465  previ = 0;
466  argdup = strdup(arg);
467  for (i = 0; argdup[i] != '\0'; ++i) {
468  if (argdup[i] == ',') {
469  argdup[i] = '\0';
470  lspec->envp[count] = strdup(&argdup[previ]);
471  previ = i + 1;
472  }
473  }
474  free(argdup);
475  lspec->envp[count] = NULL;
476  break;
477 
478  default:
479  return ARGP_ERR_UNKNOWN;
480  }
481 
482  return 0;
483 }
484 
485 struct argp linux_userproc_argp = {
487 };
488 char *linux_userproc_argp_header = "Ptrace Backend Options";
489 
490 /*
491  * If we ever want to support multithreaded targets, we'll have to track
492  * fork/clone/vfork via ptrace too. For now, we just want the bare
493  * minimum so we can tell the user about it.
494  */
495 #define INITIAL_PTRACE_OPTS \
496  PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXIT
497 
503  struct target_thread *tthread;
504 
505  if (!target)
506  return -1;
507 
508  tthread = target_lookup_thread(target,tid);
509  if (!tthread) {
510  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
511  errno = EINVAL;
512  return -1;
513  }
514 
515  return ((struct linux_userproc_thread_state *)tthread->state)->last_signo;
516 }
517 
519  struct target_thread *tthread;
520 
521  if (!target)
522  return -1;
523 
524  tthread = target_lookup_thread(target,tid);
525  if (!tthread) {
526  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
527  errno = EINVAL;
528  return -1;
529  }
530 
531  return ((struct linux_userproc_thread_state *)tthread->state)->last_status;
532 }
533 
535  struct target_thread *tthread;
536 
537  if (!target)
538  return -1;
539 
540  tthread = target_lookup_thread(target,tid);
541  if (!tthread) {
542  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
543  errno = EINVAL;
544  return -1;
545  }
546 
547  return ((struct linux_userproc_thread_state *)tthread->state)->last_status \
548  == (SIGTRAP | 0x80);
549 }
550 
552  struct target_thread *tthread;
553 
554  if (!target)
555  return -1;
556 
557  tthread = target_lookup_thread(target,tid);
558  if (!tthread) {
559  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
560  errno = EINVAL;
561  return -1;
562  }
563 
564  return ((struct linux_userproc_thread_state *)tthread->state)->last_status \
565  == (SIGTRAP | (PTRACE_EVENT_EXEC << 8));
566 }
567 
569  struct target_thread *tthread;
570 
571  if (!target)
572  return -1;
573 
574  tthread = target_lookup_thread(target,tid);
575  if (!tthread) {
576  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
577  errno = EINVAL;
578  return -1;
579  }
580 
581  return ((struct linux_userproc_thread_state *)tthread->state)->last_status \
582  == (SIGTRAP | (PTRACE_EVENT_EXIT<<8));
583 }
584 
586  struct linux_userproc_state *lstate = \
587  (struct linux_userproc_state *)target->state;
588 
589  if (!lstate)
590  return -1;
591 
592  return lstate->pid;
593 }
594 
596  struct evloop *evloop) {
597  struct linux_userproc_spec *lspec = \
598  (struct linux_userproc_spec *)spec->backend_spec;
599 
600  if (lspec->pid > -1) {
601  return linux_userproc_attach(spec,evloop);
602  }
603  else {
604  return linux_userproc_launch(spec,evloop);
605  }
606 }
607 
609  struct linux_userproc_spec *lspec;
610 
611  lspec = calloc(1,sizeof(*lspec));
612  lspec->pid = -1;
613 
614  return lspec;
615 }
616 
618  char **ptr;
619 
620  if (lspec->program)
621  free(lspec->program);
622  if (lspec->argv) {
623  ptr = lspec->argv;
624  while (*ptr) {
625  free(*ptr);
626  ++ptr;
627  }
628  free(lspec->argv);
629  }
630  if (lspec->envp) {
631  ptr = lspec->envp;
632  while (*ptr) {
633  free(*ptr);
634  ++ptr;
635  }
636  free(lspec->envp);
637  }
638 
639  free(lspec);
640 }
641 
642 /*
643  * Attaches to @pid. The caller does all of the normal ptrace
644  * interaction; we just facilitate debuginfo-assisted data operations.
645  */
646 static struct target *linux_userproc_attach(struct target_spec *spec,
647  struct evloop *evloop) {
648  struct linux_userproc_state *lstate;
649  struct target *target;
650  char buf[256];
651  struct stat sbuf;
652  FILE *stfile;
653  char pbuf[PATH_MAX*2];
654  char main_exe[PATH_MAX];
655  int rc;
656  struct binfile *binfile;
657  struct linux_userproc_spec *lspec = \
658  (struct linux_userproc_spec *)spec->backend_spec;
659  int pid = lspec->pid;
660 
661  vdebug(5,LA_TARGET,LF_LUP,"opening pid %d\n",pid);
662 
663  /* This is not strictly true; if they have the right capability they
664  * can trace... but this is easier to check.
665  */
666  if (geteuid() != 0) {
667  verror("must be root!\n");
668  errno = EPERM;
669  return NULL;
670  }
671 
672  snprintf(buf,256,"/proc/%d/stat",pid);
673  if (stat(buf,&sbuf)) {
674  verror("stat %s: %s\n",buf,strerror(errno));
675  errno = ESRCH;
676  return NULL;
677  }
678  else {
679  stfile = fopen(buf,"r");
680  if (!stfile) {
681  verror("fopen %s: %s\n",buf,strerror(errno));
682  return NULL;
683  }
684  else if (!fgets(buf,256,stfile)) {
685  verror("fgets %s: %s\n",buf,strerror(errno));
686  fclose(stfile);
687  return NULL;
688  }
689  if (strlen(buf) && buf[strlen(buf)-1] == '\n')
690  buf[strlen(buf)-1] = '\0';
691  fclose(stfile);
692  }
693 
694  /* Discover the wordsize and endianness of the process, based off
695  * its main executable.
696  */
697  /* first, find the pathname of our main exe */
698  snprintf(pbuf,PATH_MAX*2,"/proc/%d/exe",pid);
699  if ((rc = readlink(pbuf,main_exe,PATH_MAX - 1)) < 1)
700  return NULL;
701  main_exe[rc] = '\0';
702 
703  if (!(binfile = binfile_open__int(pbuf,spec->debugfile_root_prefix,NULL))) {
704  verror("binfile_open %s: %s\n",pbuf,strerror(errno));
705  return NULL;
706  }
707 
708  target = target_create("linux_userspace_process",spec);
709  if (!target)
710  return NULL;
711 
712  target->live = 1;
713  target->writeable = 1;
714 
715  /*
716  * Save off the binfile, and some stuff from it.
717  */
718  target->binfile = binfile;
719  RHOLD(target->binfile,target);
720 
721  target->arch = target->binfile->arch;
722 
723  /* Which register is the fbreg is dependent on host cpu type, not
724  * target cpu type.
725  */
726 #if __WORDSIZE == 64
727  target->fbregno = 6;
728  target->spregno = 7;
729  target->ipregno = 16;
730 #else
731  target->fbregno = 5;
732  target->spregno = 4;
733  target->ipregno = 8;
734 #endif
735 
736  lstate = (struct linux_userproc_state *)malloc(sizeof(*lstate));
737  if (!lstate) {
738  target_finalize(target);
739  errno = ENOMEM;
740  return NULL;
741  }
742  memset(lstate,0,sizeof(*lstate));
743 
744  lstate->pid = pid;
745  lstate->current_tid = 0;
746 
747  target->state = lstate;
748 
749  if (evloop) {
750  target->evloop = evloop;
751  linux_userproc_attach_evloop(target,evloop);
752  }
753 
754  vdebug(5,LA_TARGET,LF_LUP,"opened pid %d\n",pid);
755 
756  return target;
757 }
758 
759 static struct target *linux_userproc_launch(struct target_spec *spec,
760  struct evloop *evloop) {
761  struct linux_userproc_state *lstate;
762  struct target *target;
763  int pid;
764  int newfd;
765  int pstatus;
766  struct linux_userproc_spec *lspec;
767  char *filename;
768  char **argv;
769  char **envp;
770  struct binfile *binfile;
771  REFCNT trefcnt;
772  int inpfd[2] = { -1,-1 };
773  int outpfd[2] = { -1,-1 };
774  int errpfd[2] = { -1,-1 };
775  int infd = -1;
776  int outfd = -1;
777  int errfd = -1;
778 
779 #if __WORDSIZE == 64
780 #define LUP_SC_EXEC 59
781 #define LUP_SC_MPROTECT 10
782 #define LUP_SC_MMAP 9
783 #define LUP_SC_MUNMAP 11
784 #define LUP_SC_MMAP2 9 /* no mmap2 */
785 #define LUP_SC_PRCTL 157
786 #define LUP_SC_ARCH_PRCTL 158
787 #define LUP_SC_SET_THREAD_AREA 205
788 #else
789 #define LUP_SC_EXEC 11
790 #define LUP_SC_MPROTECT 125
791 #define LUP_SC_MMAP 90
792 #define LUP_SC_MUNMAP 91
793 #define LUP_SC_MMAP2 192
794 #define LUP_SC_PRCTL 172
795 #define LUP_SC_ARCH_PRCTL 172 /* no arch_prctl */
796 #define LUP_SC_SET_THREAD_AREA 243
797 #endif
798 
799  struct user_regs_struct uregs;
800 #if __WORDSIZE == 64
801  unsigned long orig_eax;
802 #else
803  long int orig_eax;
804 #endif
805  REGVAL syscall = 0;
806  char *argv_default[2] = { NULL,NULL };
807 
808  lspec = (struct linux_userproc_spec *)spec->backend_spec;
809 
810  filename = lspec->program;
811  argv = lspec->argv;
812  envp = lspec->envp;
813 
814  if (argv == NULL || *argv == NULL) {
815  /*
816  * We cannot have a NULL argv; just handle it here.
817  */
818  argv_default[0] = filename;
819  argv = argv_default;
820  }
821 
822  /*
823  * Read the binary and see if it is a dynamic or statically-linked
824  * executable. If it's dynamic, we look for one sequence of
825  * syscalls to infer when the the fully-linked program is in
826  * memory. If it's static, we look for another (much simpler)
827  * sequence.
828  */
829  binfile = binfile_open__int(filename,spec->debugfile_root_prefix,NULL);
830  if (!binfile) {
831  verror("binfile_open %s: %s\n",filename,strerror(errno));
832  return NULL;
833  }
834 
835  target = target_create("linux_userspace_process",spec);
836  if (!target)
837  goto errout;
838 
839  target->live = 1;
840  target->writeable = 1;
841 
842  /*
843  * Save off the binfile, and some stuff from it.
844  */
845  target->binfile = binfile;
846  RHOLD(target->binfile,target);
847 
848  target->arch = target->binfile->arch;
849 
850  if (binfile->is_dynamic < 0) {
851  verror("could not check if %s is static/dynamic exe; aborting!\n",
852  filename);
853  goto errout;
854  }
855  else if (!binfile->is_dynamic)
856  vdebug(2,LA_TARGET,LF_LUP,"executable %s is static\n",filename);
857  else
858  vdebug(2,LA_TARGET,LF_LUP,"executable %s is dynamic\n",filename);
859 
860  /* Which register is the fbreg is dependent on host cpu type, not
861  * target cpu type.
862  */
863 #if __WORDSIZE == 64
864  target->fbregno = 6;
865  target->spregno = 7;
866  target->ipregno = 16;
867 #else
868  target->fbregno = 5;
869  target->spregno = 4;
870  target->ipregno = 8;
871 #endif
872 
873  lstate = (struct linux_userproc_state *)malloc(sizeof(*lstate));
874  if (!lstate) {
875  errno = ENOMEM;
876  goto errout;
877  }
878  memset(lstate,0,sizeof(*lstate));
879 
880  /* We attach and can't detach, and also can't attach again when the
881  * target API tells us to.
882  */
883  lstate->initdidattach = 1;
884 
885  target->state = lstate;
886 
887  /*
888  * Handle some I/O setup; the rest is handled in parent/child after
889  * fork().
890  */
891  if (spec->in_evh) {
892  /* build a pipe to the child */
893  if (pipe(inpfd)) {
894  verror("pipe(in): %s\n",strerror(errno));
895  goto errout;
896  }
897  }
898  else if (spec->infile && strcmp(spec->infile,"-") != 0) {
899  infd = open(spec->infile,O_RDONLY);
900  if (infd < 0) {
901  verror("open(%s): %s\n",spec->infile,strerror(errno));
902  goto errout;
903  }
904  }
905  if (spec->out_evh) {
906  if (pipe(outpfd)) {
907  verror("pipe(out): %s\n",strerror(errno));
908  goto errout;
909  }
910  }
911  else if (spec->outfile && strcmp(spec->outfile,"-") != 0) {
912  outfd = open(spec->outfile,O_WRONLY | O_CREAT | O_APPEND,
913  S_IRUSR | S_IWUSR | S_IRGRP);
914  if (outfd < 0) {
915  verror("open(%s): %s\n",spec->outfile,strerror(errno));
916  goto errout;
917  }
918  }
919  if (spec->err_evh) {
920  if (pipe(errpfd)) {
921  verror("pipe(err): %s\n",strerror(errno));
922  goto errout;
923  };
924  }
925  else if (spec->errfile && strcmp(spec->errfile,"-") != 0) {
926  errfd = open(spec->errfile,O_WRONLY | O_CREAT | O_APPEND,
927  S_IRUSR | S_IWUSR | S_IRGRP);
928  if (errfd < 0) {
929  verror("open(%s): %s\n",spec->errfile,strerror(errno));
930  goto errout;
931  }
932  }
933 
934  /*
935  * Launch it!
936  */
937 
938  if ((pid = fork()) > 0) {
939  lstate->pid = pid;
940  lstate->current_tid = 0;
941 
942  /*
943  * Handle i/o stuff: close child-only FDs.
944  */
945  if (inpfd[0] > -1) {
946  close(inpfd[0]);
947  inpfd[0] = -1;
948  target->infd = inpfd[1];
949  }
950  else if (infd > -1) {
951  close(infd);
952  infd = -1;
953  }
954  if (outpfd[1] > -1) {
955  close(outpfd[1]);
956  outpfd[1] = -1;
957  target->outfd = outpfd[0];
958  }
959  else if (outfd > -1) {
960  close(outfd);
961  outfd = -1;
962  }
963  if (errpfd[1] > -1) {
964  close(errpfd[1]);
965  errpfd[1] = -1;
966  target->errfd = errpfd[0];
967  }
968  else if (errfd > -1) {
969  close(errfd);
970  errfd = -1;
971  }
972 
973  /* Parent; wait for ptrace to signal us. */
974  vdebug(3,LA_TARGET,LF_LUP,"waiting for ptrace traceme pid %d to exec\n",pid);
975  again:
976  vdebug(9,LA_TARGET,LF_LUP,"waitpid target %d\n",pid);
977  if (waitpid(pid,&pstatus,0) < 0) {
978  if (errno == ECHILD || errno == EINVAL) {
979  verror("waitpid(%d): %s\n",pid,strerror(errno));
980  goto errout;
981  }
982  else {
983  if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
984  verror("ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
985  goto errout;
986  }
987  goto again;
988  }
989  }
990  if (WIFSTOPPED(pstatus)) {
991  /* Ok, this was a ptrace event; if it was a syscall, figure out
992  * which one.
993  */
994  if (WSTOPSIG(pstatus) == SIGTRAP) {
995  vdebug(3,LA_TARGET,LF_LUP,"ptrace traceme: pid %d has exec'd\n",pid);
996  if (ptrace(PTRACE_GETREGS,pid,0,&uregs) < 0) {
997  vwarn("could not read EAX to deciper exec syscall!\n");
998  }
999  else {
1000 #if __WORDSIZE == 64
1001  orig_eax = uregs.orig_rax;
1002 #else
1003  orig_eax = uregs.orig_eax;
1004 #endif
1005  vdebug(5,LA_TARGET,LF_LUP,"exec syscall: %lu\n",orig_eax);
1006  }
1007  }
1008  else {
1009  vdebug(5,LA_TARGET,LF_LUP,"exec hunt sig (no trap)\n");
1010  if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1011  verror("ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1012  goto errout;
1013  }
1014  goto again;
1015  }
1016  }
1017  else {
1018  if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1019  verror("ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1020  goto errout;
1021  }
1022  goto again;
1023  }
1024  }
1025  else if (!pid) {
1026  if (inpfd[0] > 0) {
1027  /* close the write end of the pipe; dup2() the read end to STDIN */
1028  close(inpfd[1]);
1029  dup2(inpfd[0],STDIN_FILENO);
1030  }
1031  else if (infd > 0) {
1032  dup2(infd,STDIN_FILENO);
1033  }
1034  else if (!spec->infile || strcmp("-",spec->infile) != 0) {
1035  close(STDIN_FILENO);
1036  }
1037  else {
1038  /* Take stdin from caller! */
1039  }
1040 
1041  if (outpfd[1] > 0) {
1042  /* close the read end of the pipe; dup2() the write end to STDOUT */
1043  close(outpfd[0]);
1044  dup2(inpfd[1],STDOUT_FILENO);
1045  }
1046  else if (outfd > 0) {
1047  dup2(outfd,STDOUT_FILENO);
1048  }
1049  else if (!spec->outfile || strcmp("-",spec->outfile) != 0) {
1050  newfd = open("/dev/null",O_WRONLY);
1051  dup2(newfd,STDOUT_FILENO);
1052  }
1053  else {
1054  /* Take stdout from caller! */
1055  }
1056 
1057  if (errpfd[1] > 0) {
1058  /* close the read end of the pipe; dup2() the write end to STDERR */
1059  close(errpfd[0]);
1060  dup2(inpfd[1],STDERR_FILENO);
1061  }
1062  else if (errfd > 0) {
1063  dup2(errfd,STDERR_FILENO);
1064  }
1065  else if (!spec->errfile || strcmp("-",spec->errfile) != 0) {
1066  newfd = open("/dev/null",O_WRONLY);
1067  dup2(newfd,STDERR_FILENO);
1068  }
1069  else {
1070  /* Take stderr from caller! */
1071  }
1072 
1073  /* Don't chdir like normal for daemons. */
1074 
1075  ptrace(PTRACE_TRACEME,0,NULL,NULL);
1076  kill(getpid(),SIGINT);
1077 
1078  execve(filename,argv,envp);
1079  exit(-1);
1080  }
1081  else {
1082  verror("fork: %s\n",strerror(errno));
1083  goto errout;
1084  }
1085 
1087 
1088  /*
1089  * Ok, now that we have our child process, more setup!
1090  *
1091  * We let the process spin through its setup; if it's static,
1092  * simply look for prctl or set_thread_area. If it's dynamic, look
1093  * for a sequence like
1094  * mmap|mmap2|mprotect* ; arch_prctl|set_thread_area ; mprotect* ; munmap
1095  */
1096  again2:
1097  /* Look for syscalls! */
1098  if (ptrace(PTRACE_SYSCALL,pid,NULL,NULL) < 0) {
1099  verror("ptrace syscall pid %d failed: %s\n",pid,strerror(errno));
1100  goto errout;
1101  }
1102  vdebug(9,LA_TARGET,LF_LUP,"waitpid target %d (syscall inference)\n",pid);
1103  if (waitpid(pid,&pstatus,0) < 0) {
1104  if (errno == ECHILD || errno == EINVAL) {
1105  verror("waitpid(%d): %s\n",pid,strerror(errno));
1106  goto errout;
1107  }
1108  else {
1109  goto again2;
1110  }
1111  }
1112  if (WIFSTOPPED(pstatus)) {
1113  /* Ok, this was a ptrace event; if it was a syscall, figure out
1114  * which one.
1115  */
1116  if (WSTOPSIG(pstatus) == SIGTRAP) {
1117  if (ptrace(PTRACE_GETREGS,pid,0,&uregs) < 0) {
1118  vwarn("could not read EAX to deciper syscall; skipping inference!\n");
1119  errno = 0;
1120  goto out;
1121  }
1122 #if __WORDSIZE == 64
1123  orig_eax = uregs.orig_rax;
1124 #else
1125  orig_eax = uregs.orig_eax;
1126 #endif
1127 
1128  vdebug(5,LA_TARGET,LF_LUP,"syscall: %ld (%ld)\n",orig_eax,syscall);
1129 
1130  if (binfile->is_dynamic) {
1131  /* syscall state machine for the dynamic case: */
1132  if ((syscall == 0
1133  || ((syscall == LUP_SC_MPROTECT
1134  || syscall == LUP_SC_MMAP
1135  || syscall == LUP_SC_MMAP2)))
1136  && (orig_eax == LUP_SC_MPROTECT
1137  || orig_eax == LUP_SC_MMAP
1138  || orig_eax == LUP_SC_MMAP2)) {
1139  syscall = orig_eax;
1140  }
1141  else if ((syscall == LUP_SC_MPROTECT
1142  || syscall == LUP_SC_MMAP
1143  || syscall == LUP_SC_MMAP2)
1144  && (orig_eax == LUP_SC_PRCTL
1145  || orig_eax == LUP_SC_ARCH_PRCTL
1146  || orig_eax == LUP_SC_SET_THREAD_AREA)) {
1147  syscall = orig_eax;
1148  }
1149  else if ((syscall == LUP_SC_PRCTL
1150  || syscall == LUP_SC_ARCH_PRCTL
1151  || syscall == LUP_SC_SET_THREAD_AREA)
1152  && orig_eax == LUP_SC_MPROTECT) {
1153  syscall = orig_eax;
1154  }
1155  else if (syscall == LUP_SC_MPROTECT
1156  && orig_eax == LUP_SC_MUNMAP) {
1157  syscall = orig_eax;
1158  }
1159  else if (syscall == LUP_SC_MUNMAP
1160  && orig_eax == LUP_SC_MUNMAP) {
1161  syscall = orig_eax;
1162  vdebug(5,LA_TARGET,LF_LUP,"found end of munmap to end dynamic load sequence!\n");
1163  goto out;
1164  }
1165  }
1166  else {
1167  if (orig_eax == LUP_SC_PRCTL) {
1168  vdebug(5,LA_TARGET,LF_LUP,"found prctl to end static load sequence!\n");
1169  goto out;
1170  }
1171  else if (orig_eax == LUP_SC_ARCH_PRCTL) {
1172  vdebug(5,LA_TARGET,LF_LUP,"found arch_prctl to end static load sequence!\n");
1173  goto out;
1174  }
1175  else if (orig_eax == LUP_SC_SET_THREAD_AREA) {
1176  vdebug(5,LA_TARGET,LF_LUP,"found set_thread_area to end static load sequence!\n");
1177  goto out;
1178  }
1179  }
1180  }
1181  goto again2;
1182  }
1183  else if (WIFCONTINUED(pstatus)) {
1184  goto again2;
1185  }
1186  else if (WIFSIGNALED(pstatus)) {
1187  verror("pid %d signaled (%d) in initial tracing!\n",
1188  pid,WTERMSIG(pstatus));
1189  goto errout;
1190  }
1191  else if (WIFEXITED(pstatus)) {
1192  /* yikes, it was sigkill'd out from under us! */
1193  /* XXX: is error good enough? The pid is gone; we should
1194  * probably dump this target.
1195  */
1196  verror("pid %d bailed in initial tracing!\n",pid);
1197  goto errout;
1198  }
1199  else {
1200  vwarn("pid %d: unhandled waitpid condition while waiting for load; trying again!\n",pid);
1201  goto again2;
1202  }
1203 
1204  out:
1205  /*
1206  * We can't detach; that will resume the child. We have to leave it
1207  * paused until the user starts interacting with it. See
1208  * target->initdidattach .
1209  */
1210  /*
1211  if (ptrace(PTRACE_DETACH,pid,NULL,NULL) < 0) {
1212  verror("ptrace temporary detach failed (will try to kill child): %s\n",strerror(errno));
1213  kill(9,pid);
1214  goto errout;
1215  }
1216  */
1217 
1218  /* Set the initial PTRACE opts. */
1219  lstate->ptrace_opts_new = lstate->ptrace_opts = INITIAL_PTRACE_OPTS;
1220  errno = 0;
1221  if (ptrace(PTRACE_SETOPTIONS,pid,NULL,lstate->ptrace_opts) < 0) {
1222  vwarn("ptrace setoptions failed: %s\n",strerror(errno));
1223  }
1224 
1225  /* Clear the status bits right now. */
1226  errno = 0;
1227  if (ptrace(PTRACE_POKEUSER,pid,offsetof(struct user,u_debugreg[6]),0)) {
1228  verror("could not clear status debug reg, continuing anyway: %s!\n",
1229  strerror(errno));
1230  errno = 0;
1231  }
1232  else {
1234  "cleared status debug reg 6 for pid %d\n",pid);
1235  }
1236 
1237  if (evloop) {
1238  target->evloop = evloop;
1239  linux_userproc_attach_evloop(target,evloop);
1240  }
1241 
1242  return target;
1243 
1244  errout:
1245  /*
1246  * Cleanup I/O stuff first! Do it before target_finalize!
1247  */
1248  if (inpfd[0] > -1)
1249  close(inpfd[0]);
1250  if (inpfd[1] > -1) {
1251  if (evloop)
1252  evloop_unset_fd(evloop,target->infd,EVLOOP_FDTYPE_A);
1253  close(inpfd[1]);
1254  target->infd = -1;
1255  }
1256  if (infd > -1)
1257  close(infd);
1258 
1259  if (outpfd[1] > -1)
1260  close(outpfd[1]);
1261  if (outpfd[0] > -1) {
1262  if (evloop)
1263  evloop_unset_fd(evloop,target->outfd,EVLOOP_FDTYPE_A);
1264  close(outpfd[0]);
1265  target->outfd = -1;
1266  }
1267  if (outfd > -1)
1268  close(outfd);
1269 
1270  if (errpfd[1] > -1)
1271  close(errpfd[1]);
1272  if (errpfd[0] > -1) {
1273  if (evloop)
1274  evloop_unset_fd(evloop,target->errfd,EVLOOP_FDTYPE_A);
1275  close(errpfd[0]);
1276  target->errfd = -1;
1277  }
1278  if (errfd > -1)
1279  close(errfd);
1280 
1281  if (target)
1282  target_finalize(target);
1283  else if (binfile)
1284  RPUT(binfile,binfile,target,trefcnt);
1285 
1286  return NULL;
1287 }
1288 
1289 static int __tid_exists(int pid,tid_t tid) {
1290  char buf[256];
1291  struct stat sbuf;
1292 
1293  snprintf(buf,256,"/proc/%d/task/%d",pid,tid);
1294  if (stat(buf,&sbuf)) {
1295  errno = EINVAL;
1296  return 0;
1297  }
1298 
1299  return 1;
1300 }
1301 
1302 int linux_userproc_attach_thread(struct target *target,tid_t parent,tid_t child) {
1303  struct linux_userproc_state *lstate;
1304  struct target_thread *tthread;
1305  int pid;
1306  struct linux_userproc_thread_state *tstate;
1307  gpointer value;
1308  int racy_status;
1309  int pstatus;
1310  int rc;
1311  struct target_event *event;
1312 
1313  lstate = (struct linux_userproc_state *)target->state;
1314 
1315  if (!lstate) {
1316  errno = EFAULT;
1317  return 1;
1318  }
1319  if (!target->opened) {
1320  verror("cannot attach to thread until process is attached to!\n");
1321  errno = EINVAL;
1322  return 1;
1323  }
1324 
1325  pid = lstate->pid;
1326 
1327  if (!__tid_exists(pid,child)) {
1328  verror("thread %d in pid %d does not exist!\n",child,pid);
1329  return 1;
1330  }
1331 
1333  "pid %d parent thread %"PRIiTID" child thread %"PRIiTID"\n",
1334  pid,parent,child);
1335 
1336  /*
1337  * Create the thread.
1338  */
1339  tstate = (struct linux_userproc_thread_state *)calloc(1,sizeof(*tstate));
1340 
1341  tstate->last_status = 0;
1342  tstate->last_signo = 0;
1343  /*
1344  * Don't wait for the child to get the PTRACE-sent SIGSTOP; just
1345  * note that a fake control signal is going to hit the thread; then
1346  * the monitor/poll stuff will wait for it and process it
1347  * correctly. We don't want to deliver that signal.
1348  *
1349  * But, if the thread signaled its SIGSTOP before we were notified
1350  * about the clone() via SIGCHLD, that was recorded in
1351  * lstate->new_racy_threads; in this case, we do not want to wait to
1352  * recv the SIGSTOP!
1353  */
1354  if (g_hash_table_lookup_extended(lstate->new_racy_threads,
1355  (gpointer)(uintptr_t)child,NULL,&value)) {
1356  racy_status = (int)(uintptr_t)value;
1357 
1358  g_hash_table_remove(lstate->new_racy_threads,(gpointer)(uintptr_t)child);
1359 
1360  if (WIFSTOPPED(racy_status) && WSTOPSIG(racy_status) == SIGSTOP) {
1361  vdebug(5,LA_TARGET,LF_LUP,"new racy thread %d already hit sigstop\n",
1362  child);
1363  }
1364  else {
1365  vwarn("new racy thread %d had status %d (but not SIGSTOP);"
1366  " assuming it is stopped though!\n",child,racy_status);
1367  }
1368 
1369  tstate->ctl_sig_sent = 0;
1370  tstate->ctl_sig_recv = 0;
1371 
1372  /*
1373  * Don't reinject this signal!
1374  */
1375  tstate->last_signo = -1;
1376 
1377  /* Set the initial PTRACE opts. */
1378  lstate->ptrace_opts_new = lstate->ptrace_opts = INITIAL_PTRACE_OPTS;
1379  errno = 0;
1380  if (ptrace(PTRACE_SETOPTIONS,pid,NULL,INITIAL_PTRACE_OPTS) < 0) {
1381  vwarn("ptrace setoptions failed: %s\n",strerror(errno));
1382  }
1383 
1384  /* Restart just this thread. */
1385  /*
1386  if (ptrace(lstate->ptrace_type,child,NULL,NULL) < 0) {
1387  verror("ptrace restart of tid %"PRIiTID" failed: %s\n",
1388  child,strerror(errno));
1389  free(tstate);
1390  return 1;
1391  }
1392  */
1393  //kill(child,SIGCONT);
1394 
1395  vdebug(5,LA_TARGET,LF_LUP,"restarted new racy thread %d\n",child);
1396  }
1397  else {
1398  tstate->ctl_sig_sent = 1;
1399  tstate->ctl_sig_recv = 0;
1400 
1401  /*
1402  * Try to handle it right now, WNOHANG.
1403  */
1404  pstatus = 0;
1405  rc = waitpid(child,&pstatus,WNOHANG | __WALL);
1406  if (rc == 0) {
1408  "waitpid returned nothing for new non-racy tid %d!\n",child);
1409  }
1410  else if (rc < 0) {
1411  verror("waitpid(%d): %s\n",child,strerror(errno));
1412  }
1413  else {
1415  "waited for new non-racy tid %d successfully\n",child);
1416 
1417  /*
1418  * Ok, the SIGSTOP is available for us; grab it now and
1419  * set the thread up to get restarted.
1420  */
1421  tstate->ctl_sig_sent = 0;
1422  tstate->ctl_sig_recv = 0;
1423  }
1424  }
1425  tstate->ctl_sig_pause_all = 0;
1426 
1427  tthread = target_create_thread(target,child,tstate,NULL);
1429 
1430  event = target_create_event(target,tthread,T_EVENT_PROCESS_THREAD_CREATED,
1431  tthread);
1432  target_broadcast_event(target,event);
1433 
1435 
1436  if (target->evloop)
1437  linux_userproc_evloop_add_tid(target,child);
1438 
1439  return 0;
1440 }
1441 
1442 
1443 static int __handle_internal_detaching(struct target *target,
1444  struct target_thread *tthread,
1445  int pstatus) {
1446  REG dreg = -1;
1447  struct probepoint *dpp;
1448  REGVAL ipval;
1449 #if __WORDSIZE == 64
1450  unsigned long cdr;
1451 #else
1452  int cdr;
1453 #endif
1454  struct linux_userproc_state *lstate = \
1455  (struct linux_userproc_state *)target->state;
1456  int pid = lstate->pid;
1457  struct linux_userproc_thread_state *tstate = \
1458  (struct linux_userproc_thread_state *)tthread->state;
1459  tid_t tid = tthread->tid;
1460  tid_t newtid;
1461  long newstatus;
1462 
1463  if (!WIFSTOPPED(pstatus)) {
1465  "pid %d thread %"PRIiTID" not stopped; ignoring\n",pid,tid);
1466  return 0;
1467  }
1468 
1469  /*
1470  * Don't handle a just-cloning thread if we are detaching from
1471  * the parent doing the clone().
1472  */
1473  if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_CLONE << 8)) {
1474  ptrace(PTRACE_GETEVENTMSG,tid,NULL,&newstatus);
1475  newtid = (tid_t)newstatus;
1477  "target %d thread %d cloned new thread %d; NOT attaching!\n",
1478  pid,tid,newtid);
1479  return 0;
1480  }
1481 
1482  if (!linux_userproc_load_thread(target,tid,0)) {
1483  verror("could not load thread %"PRIiTID"!\n",tid);
1484  return -1;
1485  }
1486 
1487  tstate->last_status = tstate->last_signo = WSTOPSIG(pstatus);
1488  if (tstate->last_status == (SIGTRAP | 0x80)) {
1490  "thread %"PRIiTID" stopped with syscall trap signo %d, ignoring\n",
1491  tid,tstate->last_status);
1492  tstate->last_signo = -1;
1493  }
1494  else if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_EXIT << 8)) {
1495  vdebug(5,LA_TARGET,LF_LUP,"target %d exiting (%d), ignoring on detach\n",
1496  pid,tstate->last_status);
1497  tstate->last_signo = -1;
1498  }
1499  else if (tstate->last_status == SIGTRAP) {
1500  /* Don't deliver debug traps! */
1502  "thread %"PRIiTID" stopped with trap %d, minimal handling\n",
1503  tid,tstate->last_status);
1504  tstate->last_signo = -1;
1505 
1506  /*
1507  * This is where we handle breakpoint or single step
1508  * events.
1509  */
1510 
1511  /* Check the hw debug status reg first */
1512  errno = 0;
1513  cdr = ptrace(PTRACE_PEEKUSER,tid,
1514  offsetof(struct user,u_debugreg[6]),NULL);
1515  if (errno) {
1516  vwarn("could not read current val of status debug reg;"
1517  " don't know which handler to call; fatal!\n");
1518  return -1;
1519  }
1520 
1521  if (cdr & 0x4000) {
1523  "ignoring single step event pid %d thread %"PRIiTID"\n",
1524  pid,tid);
1525  }
1526  else {
1527  /* Only check the 4 low-order bits */
1528  if (cdr & 15) {
1529  if (cdr & 0x1)
1530  dreg = 0;
1531  else if (cdr & 0x2)
1532  dreg = 1;
1533  else if (cdr & 0x4)
1534  dreg = 2;
1535  else if (cdr & 0x8)
1536  dreg = 3;
1537 
1538  /* If we are relying on the status reg to tell us,
1539  * then also read the actual hw debug reg to get the
1540  * address we broke on.
1541  */
1542  errno = 0;
1543  ipval = ptrace(PTRACE_PEEKUSER,tid,
1544  offsetof(struct user,u_debugreg[dreg]),NULL );
1545  if (errno) {
1546  verror("could not read current val of debug reg %d after up status!\n",dreg);
1547  return -1;
1548  }
1549 
1551  "found hw break (status) in dreg %d on 0x%"PRIxADDR"\n",
1552  dreg,ipval);
1553  }
1554  else {
1555  ipval = linux_userproc_read_reg(target,tid,target->ipregno);
1556  if (errno) {
1557  verror("could not read EIP while finding probepoint: %s\n",
1558  strerror(errno));
1559  return -1;
1560  }
1561 
1562  if (tstate->dr[0] == (ptrace_reg_t)ipval)
1563  dreg = 0;
1564  else if (tstate->dr[1] == (ptrace_reg_t)ipval)
1565  dreg = 1;
1566  else if (tstate->dr[2] == (ptrace_reg_t)ipval)
1567  dreg = 2;
1568  else if (tstate->dr[3] == (ptrace_reg_t)ipval)
1569  dreg = 3;
1570 
1571  if (dreg > -1)
1573  "found hw break (eip) in dreg %d on 0x%"PRIxADDR"\n",
1574  dreg,ipval);
1575  }
1576 
1577  if (dreg > -1) {
1578  /* Found HW breakpoint! */
1579  dpp = (struct probepoint *) \
1580  g_hash_table_lookup(tthread->hard_probepoints,
1581  (gpointer)ipval);
1582 
1583  if (!dpp) {
1584  verror("found hw breakpoint 0x%"PRIxADDR
1585  " in debug reg %d, BUT no probepoint!\n",
1586  ipval,dreg);
1587  return -1;
1588  }
1589 
1591  "hw bp %d pid %d thread %"PRIiTID", not resetting EIP\n",
1592  dreg,pid,tid);
1593  }
1594  /* catch glib bug in hash table init; check for empty hashtable */
1595  else if ((dpp = (struct probepoint *) \
1596  g_hash_table_lookup(target->soft_probepoints,
1597  (gpointer)(ipval - target->arch->breakpoint_instrs_len)))) {
1599  "sw bp pid %d thread %"PRIiTID", resetting EIP\n",
1600  pid,tid);
1601 
1602  ipval -= target->arch->breakpoint_instrs_len;
1603  errno = 0;
1604  target_write_reg(target,tid,target->ipregno,ipval);
1605  if (errno) {
1606  verror("could not reset EIP; thread will crash\n");
1607  return -1;
1608  }
1609  target_flush_thread(target,tid);
1610  }
1611  else {
1612  vwarn("could not find bp and not in sstep; letting thread"
1613  " pid %d thread %"PRIiTID" detach without handling"
1614  " (at 0x%"PRIxADDR"!\n",
1615  pid,tid,ipval);
1616  return 0;
1617  }
1618  }
1619  }
1620  else if (WIFCONTINUED(pstatus)) {
1622  "waitpid CONT event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1623  pid,tid,pstatus);
1624  tstate->last_signo = -1;
1625  tstate->last_status = -1;
1626  }
1627  else if (WIFEXITED(pstatus)) {
1628  /* yikes, it was sigkill'd out from under us! */
1629  /* XXX: is error good enough? The pid is gone; we should
1630  * probably dump this target.
1631  */
1633  "waitpid EXITED event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1634  pid,tid,pstatus);
1635  tstate->last_signo = -1;
1636  tstate->last_status = -1;
1637 
1638  return 1;
1639  }
1640  else if (WIFSIGNALED(pstatus)) {
1642  "waitpid SIGNALED event pid %d thread %"PRIiTID" (status 0x%x); ignoring\n",
1643  pid,tid,pstatus);
1644  tstate->last_signo = -1;
1645  tstate->last_status = -1;
1646 
1647  return 1;
1648  }
1649  else {
1651  "unknown waitpid event pid %d thread %"PRIiTID" (status 0x%x)\n",
1652  pid,tid,pstatus);
1653  return -1;
1654  }
1655 
1656  return 1;
1657 }
1658 
1659 int __poll_and_handle_detaching(struct target *target,
1660  struct target_thread *tthread) {
1661  int status = 0;
1662  int retval;
1663  struct linux_userproc_state *lstate;
1664  int pid;
1665  tid_t tid = tthread->tid;
1666 
1667  lstate = (struct linux_userproc_state *)target->state;
1668  pid = lstate->pid;
1669 
1670  tthread = target_lookup_thread(target,tid);
1671 
1672  retval = waitpid(tid,&status,WNOHANG | __WALL);
1673  if (retval == 0) {
1675  "pid %d thread %"PRIiTID" running; not handling\n",pid,tid);
1676  return 0;
1677  }
1678  else if (retval < 0) {
1679  verror("waitpid pid %d thread %"PRIiTID": %s\n",pid,tid,strerror(errno));
1680  return retval;
1681  }
1682 
1683  /*
1684  * Handle in a basic way so as to remove our presence from the
1685  * thread -- which means if the thread just hit a breakpoint, remove
1686  * the breakpoint and reset EIP, and flush.
1687  *
1688  * If we're at a single step, nothing to worry about!
1689  */
1690 
1691  vdebug(5,LA_TARGET,LF_LUP,"handling pid %d thread %"PRIiTID"\n",pid,tid);
1692  return __handle_internal_detaching(target,tthread,status);
1693 }
1694 
1695 int linux_userproc_detach_thread(struct target *target,tid_t tid,
1696  int detaching_all,int stay_paused) {
1697  struct linux_userproc_state *lstate;
1698  struct target_thread *tthread;
1699  char buf[256];
1700  int pid;
1701  struct stat sbuf;
1702 
1703  lstate = (struct linux_userproc_state *)target->state;
1704  if (!lstate) {
1705  errno = EFAULT;
1706  return 1;
1707  }
1708  pid = lstate->pid;
1709 
1710  vdebug(5,LA_TARGET,LF_LUP,"pid %d thread %"PRIiTID"\n",pid,tid);
1711 
1712  if (!target->opened) {
1713  verror("cannot detach from thread until process is attached to!\n");
1714  errno = EINVAL;
1715  return 1;
1716  }
1717 
1718  if (!(tthread = target_lookup_thread(target,tid))) {
1719  verror("thread %"PRIiTID" does not exist!\n",tid);
1720  errno = EINVAL;
1721  return 1;
1722  }
1723 
1724  if (target->evloop)
1725  linux_userproc_evloop_del_tid(target,tid);
1726 
1727  /*
1728  * If it exists, actually detach. Else, just clean up our state.
1729  */
1730  snprintf(buf,256,"/proc/%d/task/%d",pid,tid);
1731  if (stat(buf,&sbuf) == 0) {
1732 
1733  /* Detach no longer means detach; now it means remove too. */
1734  //target_detach_thread(target,tthread);
1735 
1736  /*
1737  * If the thread is stopped with status, check it and handle it
1738  * in a basic way -- like if it's stopped at a breakpoint, reset
1739  * EIP and replace orig code -- don't handle it more.
1740  */
1741  if (__poll_and_handle_detaching(target,tthread) == 0) {
1742  /*
1743  * Sleep the child first if it's still running; otherwise
1744  * we'll end up sending it a trace trap, which will kill it.
1745  */
1746  kill(tid,SIGSTOP);
1747  }
1748 
1749  ptrace(PTRACE_DETACH,tid,NULL,(void *)(uintptr_t)(stay_paused ? SIGSTOP : 0));
1750 
1751  /*
1752  * Don't CONT it if we're detaching; we'll signal the pid
1753  * globally if necessary.
1754  */
1755  if (!detaching_all && !stay_paused)
1756  kill(tid,SIGCONT);
1757 
1758  errno = 0;
1759  }
1760 
1761  target_detach_thread(target,tthread);
1762 
1763  return 0;
1764 }
1765 
1769 static int linux_userproc_snprintf(struct target *target,
1770  char *buf,int bufsiz) {
1771  struct linux_userproc_spec *lspec = \
1772  (struct linux_userproc_spec *)target->spec->backend_spec;
1773 
1774  if (lspec->program)
1775  return snprintf(buf,bufsiz,"ptrace(%d,%s)",lspec->pid,lspec->program);
1776  else
1777  return snprintf(buf,bufsiz,"ptrace(%d)",lspec->pid);
1778 }
1779 
1780 static tid_t linux_userproc_gettid(struct target *target) {
1781  struct target_thread *tthread;
1782 
1783  /*
1784  * Note: this is really all the same as target->state->current_tid
1785  * -- but we also make sure we load the current thread.
1786  */
1787 
1788  if (target->current_thread && OBJVALID(target->current_thread))
1789  return target->current_thread->tid;
1790 
1791  tthread = linux_userproc_load_current_thread(target,0);
1792  if (!tthread) {
1793  verror("could not load current thread to get TID!\n");
1794  return 0;
1795  }
1796 
1797  return tthread->tid;
1798 }
1799 
1800 static void linux_userproc_free_thread_state(struct target *target,void *state) {
1801  free(state);
1802 }
1803 
1804 static int __linux_userproc_load_thread_status(struct target_thread *tthread,
1805  tid_t tid,int force) {
1806  struct linux_userproc_state *lstate;
1807  int pid;
1808  char buf[64];
1809  FILE *statf;
1810  char pstate;
1811  int rc;
1812  tid_t rtid = 0;
1813 
1814  lstate = (struct linux_userproc_state *)tthread->target->state;
1815  pid = lstate->pid;
1816 
1817  if (OBJVALID(tthread) && !force) {
1819  "pid %d thread %"PRIiTID" already valid\n",pid,tid);
1820  return 0;
1821  }
1822 
1823  vdebug(12,LA_TARGET,LF_LUP,"pid %d thread %"PRIiTID" (%"PRIiTID")\n",
1824  pid,tid,tthread->tid);
1825 
1826  /* Load its status from /proc */
1827  snprintf(buf,64,"/proc/%d/task/%d/stat",pid,tthread->tid);
1828  again:
1829  statf = fopen(buf,"r");
1830  if (!statf) {
1832  "fopen(%s): %s; UNKNOWN!\n",buf,strerror(errno));
1834  return -1;
1835  }
1836 
1837  if ((rc = fscanf(statf,"%d (%s %c",&rtid,buf,&pstate))) {
1838  if (pstate == 'R' || pstate == 'r')
1840  else if (pstate == 'W' || pstate == 'w')
1842  else if (pstate == 'S' || pstate == 's')
1844  else if (pstate == 'D' || pstate == 'd')
1846  else if (pstate == 'Z' || pstate == 'z')
1848  else if (pstate == 'T' || pstate == 't')
1850  else if (pstate == 'X' || pstate == 'x')
1852  else {
1853  verror("fscanf returned %d; read tid %d (%s) %c; UNKNOWN!\n",
1854  rc,rtid,buf,pstate);
1856  goto errout;
1857  }
1858  }
1859  else if (rc < 0 && errno == EINTR) {
1860  fclose(statf);
1861  goto again;
1862  }
1863  fclose(statf);
1864 
1865  vdebug(3,LA_TARGET,LF_LUP,"pid %d tid %"PRIiTID" status %d\n",
1866  pid,tthread->tid,tthread->status);
1867 
1868  return 0;
1869 
1870  errout:
1871  return -1;
1872 }
1873 
1874 static struct target_thread *__linux_userproc_load_thread(struct target *target,
1875  tid_t tid,int force,
1876  int have_status) {
1877  struct target_thread *tthread;
1878  struct linux_userproc_thread_state *tstate;
1879  struct linux_userproc_state *lstate;
1880  int pid;
1881 
1882  lstate = (struct linux_userproc_state *)target->state;
1883  pid = lstate->pid;
1884 
1885  if (!(tthread = target_lookup_thread(target,tid))) {
1886  verror("thread %"PRIiTID" does not exist; forgot to attach?\n",tid);
1887  errno = EINVAL;
1888  return NULL;
1889  }
1890  tstate = (struct linux_userproc_thread_state *)tthread->state;
1891 
1892  if (OBJVALID(tthread) && !force) {
1893  vdebug(9,LA_TARGET,LF_LUP,"pid %d thread %"PRIiTID" already valid\n",
1894  pid,tid);
1895  return tthread;
1896  }
1897 
1898  vdebug(12,LA_TARGET,LF_LUP,"pid %d thread %"PRIiTID" (%"PRIiTID")\n",
1899  pid,tid,tthread->tid);
1900 
1901  if (!have_status)
1902  __linux_userproc_load_thread_status(tthread,tid,force);
1903 
1904  vdebug(3,LA_TARGET,LF_LUP,"pid %d tid %"PRIiTID" status %d\n",
1905  pid,tthread->tid,tthread->status);
1906 
1907  if (tthread->status == THREAD_STATUS_PAUSED) {
1908  errno = 0;
1909  if (ptrace(PTRACE_GETREGS,tthread->tid,NULL,&(tstate->regs)) == -1) {
1910  verror("ptrace(GETREGS): %s\n",strerror(errno));
1911  OBJSINVALID(tthread);
1912  OBJSCLEAN(tthread);
1913  return NULL;
1914  }
1915  OBJSVALID(tthread);
1916  }
1917  else {
1918  memset(&tstate->regs,0,sizeof(tstate->regs));
1919  OBJSINVALID(tthread);
1920  }
1921 
1922  OBJSCLEAN(tthread);
1923 
1924  return tthread;
1925 }
1926 
1927 static struct target_thread *linux_userproc_load_thread(struct target *target,
1928  tid_t tid,int force) {
1929  return __linux_userproc_load_thread(target,tid,force,0);
1930 }
1931 
1932 static struct target_thread *linux_userproc_load_current_thread(struct target *target,
1933  int force) {
1934  struct linux_userproc_state *lstate = \
1935  (struct linux_userproc_state *)target->state;
1936 
1937  return linux_userproc_load_thread(target,lstate->current_tid,force);
1938 }
1939 
1940 static int linux_userproc_load_all_threads(struct target *target,int force) {
1941  int retval = 0;
1942  GHashTableIter iter;
1943  struct target_thread *tthread;
1944  gpointer key;
1945 
1946  g_hash_table_iter_init(&iter,target->threads);
1947  while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
1948  if (key == (gpointer)TID_GLOBAL)
1949  continue;
1950 
1951  if (!linux_userproc_load_thread(target,tthread->tid,force)) {
1952  verror("could not load thread %"PRIiTID"\n",tthread->tid);
1953  ++retval;
1954  }
1955  }
1956 
1957  return retval;
1958 }
1959 
1960 static int linux_userproc_flush_thread(struct target *target,tid_t tid) {
1961  struct target_thread *tthread;
1962  struct linux_userproc_thread_state *tstate;
1963 
1964  vdebug(5,LA_TARGET,LF_LUP | LF_THREAD,"thread %"PRIiTID"\n",tid);
1965 
1966  if ((tthread = target_lookup_thread(target,tid))) {
1967  tstate = (struct linux_userproc_thread_state *)tthread->state;
1968  }
1969  else {
1970  verror("cannot flush unknown thread %"PRIiTID"; forgot to load?\n",tid);
1971  errno = EINVAL;
1972  return -1;
1973  }
1974 
1975  if (!OBJVALID(tthread) || !OBJDIRTY(tthread))
1976  return 0;
1977 
1978  errno = 0;
1979  if (ptrace(PTRACE_SETREGS,tthread->tid,NULL,&(tstate->regs)) == -1) {
1980  verror("ptrace(SETREGS): %s\n",strerror(errno));
1981  return -1;
1982  }
1983  OBJSCLEAN(tthread);
1984 
1985  return 0;
1986 }
1987 
1988 static int linux_userproc_flush_current_thread(struct target *target) {
1989  if (!target->current_thread && linux_userproc_load_current_thread(target,0))
1990  return -1;
1991 
1992  return linux_userproc_flush_thread(target,target->current_thread->tid);
1993 }
1994 
1995 static int linux_userproc_flush_all_threads(struct target *target) {
1996  int rc, retval = 0;
1997  GHashTableIter iter;
1998  struct target_thread *tthread;
1999  gpointer key;
2000 
2001  g_hash_table_iter_init(&iter,target->threads);
2002  while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
2003  if (key == (gpointer)TID_GLOBAL)
2004  continue;
2005 
2006  rc = linux_userproc_flush_thread(target,tthread->tid);
2007  if (rc) {
2008  verror("could not flush thread %"PRIiTID"\n",tthread->tid);
2009  ++retval;
2010  }
2011  }
2012 
2013  return retval;
2014 }
2015 
2016 static int linux_userproc_thread_snprintf(struct target *target,
2017  struct target_thread *tthread,
2018  char *buf,int bufsiz,
2019  int detail,char *sep,char *kvsep) {
2020  struct linux_userproc_thread_state *tstate;
2021  struct user_regs_struct *r;
2022  int rc = 0;
2023 
2024  if (detail < 0)
2025  return 0;
2026 
2027  tstate = (struct linux_userproc_thread_state *)tthread->state;
2028  r = &tstate->regs;
2029 
2030 #if __WORDSIZE == 64
2031 #define __V_LINUX_PTRACE_REG_LARGE_SIZE
2032 #ifdef __V_LINUX_PTRACE_REG_LARGE_SIZE
2033 #define RF "llx"
2034 #define DRF "lx"
2035 #else
2036 #define RF "lx"
2037 #define DRF "lx"
2038 #endif
2039 #else
2040 #define RF "llx"
2041 #define DRF "x"
2042 #endif
2043 
2044  if (detail >= 1)
2045  rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
2046  (rc >= bufsiz) ? 0 :bufsiz - rc,
2047  "%s" "ip%s%"RF "%s" "bp%s%"RF "%s" "sp%s%"RF "%s"
2048  "flags%s%"RF "%s" "ax%s%"RF "%s" "bx%s%"RF "%s"
2049  "cx%s%"RF "%s" "dx%s%"RF "%s" "di%s%"RF "%s"
2050  "si%s%"RF "%s" "cs%s%"RF "%s" "ss%s%"RF "%s"
2051  "ds%s%"RF "%s" "es%s%"RF "%s"
2052  "fs%s%"RF "%s" "gs%s%"RF,
2053 #if __WORDSIZE == 64
2054  sep,kvsep,r->rip,sep,kvsep,r->rbp,sep,kvsep,r->rsp,sep,
2055  kvsep,r->eflags,sep,kvsep,r->rax,sep,kvsep,r->rbx,sep,
2056  kvsep,r->rcx,sep,kvsep,r->rdx,sep,kvsep,r->rdi,sep,
2057  kvsep,r->rsi,sep,kvsep,r->cs,sep,kvsep,r->ss,sep,
2058  kvsep,r->ds,sep,kvsep,r->es,sep,
2059  kvsep,r->fs,sep,kvsep,r->gs
2060 #else
2061  sep,kvsep,r->eip,sep,kvsep,r->ebp,sep,kvsep,r->esp,sep,
2062  kvsep,r->eflags,sep,kvsep,r->eax,sep,kvsep,r->ebx,sep,
2063  kvsep,r->ecx,sep,kvsep,r->edx,sep,kvsep,r->edi,sep,
2064  kvsep,r->esi,sep,kvsep,r->cs,sep,kvsep,r->ss,sep,
2065  kvsep,r->ds,sep,kvsep,r->es,sep,
2066  kvsep,r->fs,sep,kvsep,r->gs
2067 #endif
2068  );
2069  if (detail >= 2)
2070  rc += snprintf((rc >= bufsiz) ? NULL : buf + rc,
2071  (rc >= bufsiz) ? 0 :bufsiz - rc,
2072  "%s" "dr0%s%"DRF "%s" "dr1%s%"DRF
2073  "%s" "dr2%s%"DRF "%s" "dr3%s%"DRF
2074  "%s" "dr6%s%"DRF "%s" "dr7%s%"DRF,
2075  sep,kvsep,tstate->dr[0],sep,kvsep,tstate->dr[1],
2076  sep,kvsep,tstate->dr[1],sep,kvsep,tstate->dr[2],
2077  sep,kvsep,tstate->dr[6],sep,kvsep,tstate->dr[7]);
2078 
2079  return rc;
2080 }
2081 
2082 static int linux_userproc_init(struct target *target) {
2083  struct linux_userproc_state *lstate = \
2084  (struct linux_userproc_state *)target->state;
2085  struct linux_userproc_thread_state *tstate;
2086  struct target_thread *tthread;
2087 
2088  /*
2089  * We must single step hardware breakpoints; we can't set the RF
2090  * flag and thus disable them if we don't technically need a single
2091  * step (i.e., no post handlers nor actions). So we have to disable
2092  * the hw breakpoint, single step it, then reenable it. See
2093  * linux_userproc_singlestep() below.
2094  */
2095  target->nodisablehwbponss = 0;
2096  target->threadctl = 1;
2097 
2098  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
2099 
2100  lstate->memfd = -1;
2101  lstate->ptrace_opts_new = lstate->ptrace_opts = INITIAL_PTRACE_OPTS;
2102  lstate->ptrace_type = PTRACE_CONT;
2103 
2104  lstate->new_racy_threads = g_hash_table_new(g_direct_hash,g_direct_equal);
2105 
2106  /* Create the default thread. */
2107  tstate = (struct linux_userproc_thread_state *)calloc(1,sizeof(*tstate));
2108 
2109  tstate->last_status = -1;
2110  tstate->last_signo = -1;
2111 
2112  tthread = target_create_thread(target,lstate->pid,tstate,NULL);
2114  /* Default thread is always starts paused. */
2116 
2117  /* Reuse the pid's primary thread as the global thread. */
2118  target_reuse_thread_as_global(target,tthread);
2119 
2120  /* Set thread->current_thread to our primary thread! */
2121  target->current_thread = tthread;
2122  lstate->current_tid = tthread->tid;
2123 
2124  target->global_tlctxt =
2125  target_location_ctxt_create(target,TID_GLOBAL,NULL);
2126 
2127  if (target->evloop)
2128  linux_userproc_evloop_add_tid(target,tthread->tid);
2129 
2130  return 0;
2131 }
2132 
2133 static int linux_userproc_postloadinit(struct target *target) {
2134  return 0;
2135 }
2136 
2137 static int linux_userproc_attach_internal(struct target *target) {
2138  struct linux_userproc_state *lstate;
2139  char buf[256];
2140  int pstatus;
2141  int pid;
2142  struct dirent *dirent;
2143  DIR *dirp;
2144  char *endp;
2145  tid_t tid;
2146  int rc = 0;
2147  struct target_thread *tthread;
2148  struct linux_userproc_thread_state *tstate;
2149 
2150  lstate = (struct linux_userproc_state *)target->state;
2151  pid = lstate->pid;
2152 
2153  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",pid);
2154 
2155  if (!lstate) {
2156  errno = EFAULT;
2157  return 1;
2158  }
2159  pid = lstate->pid;
2160  if (target->opened)
2161  return 0;
2162 
2163  errno = 0;
2164  if (!lstate->initdidattach) {
2165  if (ptrace(PTRACE_ATTACH,pid,NULL,NULL) < 0) {
2166  verror("ptrace attach pid %d failed: %s\n",pid,strerror(errno));
2167  return 1;
2168  }
2169  }
2170 
2171  snprintf(buf,256,"/proc/%d/mem",pid);
2172  if ((lstate->memfd = open(buf,O_LARGEFILE,O_RDWR)) < 0) {
2173  verror("open %s failed, detaching: %s!\n",buf,strerror(errno));
2174  ptrace(PTRACE_DETACH,pid,NULL,NULL);
2175  return 1;
2176  }
2177 
2178  if (!lstate->initdidattach) {
2179  /*
2180  * Wait for the child to get the PTRACE-sent SIGSTOP, then make sure
2181  * we *don't* deliver that signal to it when the library user calls
2182  * target_resume!
2183  */
2184 
2185  vdebug(3,LA_TARGET,LF_LUP,"waiting for ptrace attach to hit pid %d\n",pid);
2186  again:
2187  vdebug(5,LA_TARGET,LF_LUP,"initial waitpid target %d\n",pid);
2188  if (waitpid(pid,&pstatus,0) < 0) {
2189  if (errno == ECHILD || errno == EINVAL)
2190  return TSTATUS_ERROR;
2191  else
2192  goto again;
2193  }
2194  vdebug(3,LA_TARGET,LF_LUP,"ptrace attach has hit pid %d\n",pid);
2196  }
2197 
2198  /* Set the initial PTRACE opts. */
2199  errno = 0;
2200  if (ptrace(PTRACE_SETOPTIONS,pid,NULL,lstate->ptrace_opts) < 0) {
2201  vwarn("ptrace setoptions failed: %s\n",strerror(errno));
2202  }
2203 
2204  /*
2205  * Try to attach to any other threads. For a process we spawned,
2206  * there should not be any. But it's harmless to check.
2207  */
2208  snprintf(buf,256,"/proc/%d/task",pid);
2209  if (!(dirp = opendir(buf))) {
2210  verror("could not opendir %s to attach to threads: %s!\n",
2211  buf,strerror(errno));
2212  return TSTATUS_ERROR;
2213  }
2214 
2215  errno = 0;
2216  while ((dirent = readdir(dirp))) {
2217  if (dirent->d_name[0] == '.')
2218  continue;
2219 
2220  tid = (tid_t)strtol(dirent->d_name,&endp,10);
2221  if (endp == dirent->d_name || errno == ERANGE || errno == EINVAL) {
2222  verror("weird error parsing thread id out of '%s': %s; skipping!\n",
2223  dirent->d_name,strerror(errno));
2224  errno = 0;
2225  continue;
2226  }
2227 
2228  if (tid == pid)
2229  continue;
2230 
2231  if (ptrace(PTRACE_ATTACH,tid,NULL,NULL) < 0) {
2232  verror("ptrace attach tid %d failed: %s\n",tid,strerror(errno));
2233  ++rc;
2234  continue;
2235  }
2236 
2237  vdebug(3,LA_TARGET,LF_LUP,"waiting for ptrace attach to hit tid %d\n",tid);
2238  again2:
2239  vdebug(5,LA_TARGET,LF_LUP,"initial waitpid tid %d\n",tid);
2240  if (waitpid(tid,&pstatus,__WALL) < 0) {
2241  if (errno == ECHILD || errno == EINVAL) {
2242  verror("waitpid tid %d failed: %s\n",tid,strerror(errno));
2243  ++rc;
2244  continue;
2245  }
2246  else
2247  goto again2;
2248  }
2249  vdebug(3,LA_TARGET,LF_LUP,"ptrace attach has hit tid %d\n",tid);
2250 
2251  if (ptrace(PTRACE_SETOPTIONS,tid,NULL,lstate->ptrace_opts) < 0) {
2252  vwarn("ptrace setoptions failed, continuing: %s\n",strerror(errno));
2253  errno = 0;
2254  }
2255 
2256  /*
2257  * Create the thread.
2258  */
2259  tstate = (struct linux_userproc_thread_state *)calloc(1,sizeof(*tstate));
2260 
2261  tstate->last_status = 0;
2262  tstate->last_signo = 0;
2263 
2264  tstate->ctl_sig_sent = 0;
2265  tstate->ctl_sig_recv = 0;
2266  tstate->ctl_sig_pause_all = 0;
2267 
2268  tthread = target_create_thread(target,tid,tstate,NULL);
2271 
2272  if (target->evloop)
2273  linux_userproc_evloop_add_tid(target,tid);
2274  }
2275 
2276  closedir(dirp);
2277 
2278  return rc;
2279 }
2280 
2281 static int linux_userproc_detach(struct target *target,int stay_paused) {
2282  struct linux_userproc_state *lstate;
2283  int rc, retval = 0;
2284  GHashTableIter iter;
2285  struct target_thread *tthread;
2286  struct array_list *threadlist;
2287  int i;
2288 
2289  lstate = (struct linux_userproc_state *)(target->state);
2290  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
2291 
2292  if (!lstate) {
2293  errno = EFAULT;
2294  return 1;
2295  }
2296  if (!target->opened)
2297  return 0;
2298 
2299  /*
2300  * Detach from all the threads first. We push them onto a list
2301  * first because we can't trust detach_thread not to delete them
2302  * from the hashtable, which we can't do while we're iterating
2303  * through it.
2304  */
2305  threadlist = array_list_create(g_hash_table_size(target->threads));
2306  g_hash_table_iter_init(&iter,target->threads);
2307  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
2308  if (tthread == target->global_thread)
2309  continue;
2310 
2311  array_list_append(threadlist,tthread);
2312  }
2313  for (i = 0; i < array_list_len(threadlist); ++i) {
2314  tthread = (struct target_thread *)array_list_item(threadlist,i);
2315 
2316  rc = linux_userproc_detach_thread(target,tthread->tid,1,stay_paused);
2317  if (rc) {
2318  verror("could not detach thread %"PRIiTID"\n",tthread->tid);
2319  ++retval;
2320  }
2321  }
2322  array_list_free(threadlist);
2323 
2324  /*
2325  * Now detach from the primary process thread.
2326  */
2327  tthread = target->global_thread;
2328  if (tthread) {
2329  rc = linux_userproc_detach_thread(target,tthread->tid,1,stay_paused);
2330  if (rc) {
2331  verror("could not detach global thread %"PRIiTID"\n",tthread->tid);
2332  ++retval;
2333  }
2334  else
2335  target->global_thread = NULL;
2336  }
2337 
2338  /* Also, remove the global thread from target->threads! */
2339  //g_hash_table_remove(target->threads,(gpointer)(uintptr_t)TID_GLOBAL);
2340 
2341  /*
2342  *
2343  */
2344  /*
2345  errno = 0;
2346  if (ptrace(PTRACE_DETACH,lstate->pid,NULL,NULL) < 0) {
2347  verror("ptrace detach %d failed: %s\n",lstate->pid,strerror(errno));
2348  kill(lstate->pid,SIGCONT);
2349  //return 1;
2350  }
2351  */
2352 
2353  if (!stay_paused)
2354  kill(lstate->pid,SIGCONT);
2355 
2356  if (lstate->memfd > 0)
2357  close(lstate->memfd);
2358 
2359  vdebug(3,LA_TARGET,LF_LUP,"ptrace detach %d done.\n",lstate->pid);
2360 
2361  return 0;
2362 }
2363 
2364 static int linux_userproc_fini(struct target *target) {
2365  struct linux_userproc_state *lstate;
2366 
2367  lstate = (struct linux_userproc_state *)(target->state);
2368 
2369  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
2370 
2371  if (target->spec->outfile) {
2372  unlink(target->spec->outfile);
2373  free(target->spec->outfile);
2374  target->spec->outfile = NULL;
2375  }
2376 
2377  if (target->spec->errfile) {
2378  unlink(target->spec->errfile);
2379  free(target->spec->errfile);
2380  target->spec->errfile = NULL;
2381  }
2382 
2383  free(target->state);
2384 
2385  return 0;
2386 }
2387 
2388 static int linux_userproc_kill(struct target *target,int sig) {
2389  struct linux_userproc_state *lstate;
2390 
2391  lstate = (struct linux_userproc_state *)(target->state);
2392  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
2393 
2394  if (!lstate) {
2395  errno = EFAULT;
2396  return 1;
2397  }
2398 
2399  if (kill(lstate->pid,sig))
2400  return 1;
2401 
2402  return 0;
2403 }
2404 
2405 static int linux_userproc_loadspaces(struct target *target) {
2406  struct linux_userproc_state *lstate = \
2407  (struct linux_userproc_state *)target->state;
2408 
2409  addrspace_create(target,"NULL",lstate->pid);
2410 
2411  return 0;
2412 }
2413 
2414 static int linux_userproc_loadregions(struct target *target,
2415  struct addrspace *space) {
2416  char buf[PATH_MAX*2];
2417  char main_exe[PATH_MAX];
2418  FILE *f;
2419  char p[4];
2420  struct memregion *region;
2421  struct memrange *range;
2422  unsigned long long start,end,offset;
2423  region_type_t rtype;
2424  int rc;
2425  char *ret;
2426  struct linux_userproc_state *lstate = \
2427  (struct linux_userproc_state *)target->state;
2428 
2429  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
2430 
2431  /* first, find the pathname of our main exe */
2432  snprintf(buf,PATH_MAX*2,"/proc/%d/exe",lstate->pid);
2433  if ((rc = readlink(buf,main_exe,PATH_MAX - 1)) < 1)
2434  return -1;
2435  main_exe[rc] = '\0';
2436 
2437  snprintf(buf,PATH_MAX,"/proc/%d/maps",lstate->pid);
2438  f = fopen(buf,"r");
2439  if (!f)
2440  return 1;
2441 
2442  while (1) {
2443  errno = 0;
2444  if (!(ret = fgets(buf,PATH_MAX*2,f)) && !errno)
2445  break;
2446  else if (!ret && errno) {
2447  verror("fgets: %s",strerror(errno));
2448  break;
2449  }
2450 
2451  vdebug(8,LA_TARGET,LF_LUP,"scanning mmap line %s",buf);
2452 
2453  rc = sscanf(buf,"%Lx-%Lx %c%c%c%c %Lx %*x:%*x %*d %s",&start,&end,
2454  &p[0],&p[1],&p[2],&p[3],&offset,buf);
2455  if (rc == 8 || rc == 7) {
2456  if (rc == 8) {
2457  /* we got the whole thing, including a path */
2458  if (strncmp(main_exe,buf,PATH_MAX) == 0)
2459  rtype = REGION_TYPE_MAIN;
2460  else if (strcmp(buf,"[heap]") == 0)
2461  rtype = REGION_TYPE_HEAP;
2462  else if (strcmp(buf,"[stack]") == 0)
2463  rtype = REGION_TYPE_STACK;
2464  else if (strcmp(buf,"[vdso]") == 0)
2465  rtype = REGION_TYPE_VDSO;
2466  else if (strcmp(buf,"[vsyscall]") == 0)
2467  rtype = REGION_TYPE_VSYSCALL;
2468  else
2469  rtype = REGION_TYPE_LIB;
2470  }
2471  else {
2472  rtype = REGION_TYPE_ANON;
2473  buf[0] = '\0';
2474  }
2475 
2476  /* Create a region for this map entry if it doesn't already
2477  * exist.
2478  */
2479  if (rtype == REGION_TYPE_ANON
2480  && addrspace_find_range_real(space,start - 1,&region,NULL))
2481  ;
2482  else if (rtype == REGION_TYPE_ANON
2483  || !(region = addrspace_find_region(space,buf))) {
2484  if (!(region = memregion_create(space,rtype,buf)))
2485  goto err;
2486  }
2487 
2488  if (!(range = memrange_create(region,start,end,offset,0))) {
2489  goto err;
2490  }
2491 
2492  if (p[0] == 'r')
2493  range->prot_flags |= PROT_READ;
2494  if (p[1] == 'w')
2495  range->prot_flags |= PROT_WRITE;
2496  if (p[2] == 'x')
2497  range->prot_flags |= PROT_EXEC;
2498  if (p[3] == 's')
2499  range->prot_flags |= PROT_SHARED;
2500 
2501  range = NULL;
2502  region = NULL;
2503  }
2504  /*
2505  else if (rc == EOF && !errno) {
2506  break;
2507  else if (rc == EOF && errno) {
2508  verror("fscanf error: %s\n",strerror(errno));
2509  goto err;
2510  }
2511  */
2512  else if (rc > 0 && !errno) {
2513  vwarn("weird content in /proc/pid/maps (%d)!\n",rc);
2514  }
2515  else if (rc > 0 && errno) {
2516  vwarn("weird content in /proc/pid/maps (%d): %s!\n",rc,strerror(errno));
2517  }
2518  }
2519 
2520  fclose(f);
2521  return 0;
2522 
2523  err:
2524  fclose(f);
2525  // XXX cleanup the regions we added??
2526  return -1;
2527 }
2528 
2529 static int linux_userproc_updateregions(struct target *target,
2530  struct addrspace *space) {
2531  char buf[PATH_MAX*2];
2532  char main_exe[PATH_MAX];
2533  FILE *f;
2534  char p[4];
2535  struct memregion *region;
2536  struct memrange *range;
2537  GList *t1,*t2,*t3,*t4;
2538  unsigned long long start,end,offset;
2539  region_type_t rtype;
2540  int rc;
2541  char *ret;
2542  struct linux_userproc_state *lstate = \
2543  (struct linux_userproc_state *)target->state;
2544  uint32_t prot_flags;
2545  int exists;
2546  int updated;
2547  struct target_event *event;
2548 
2549  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
2550 
2551  /* first, find the pathname of our main exe */
2552  snprintf(buf,PATH_MAX*2,"/proc/%d/exe",lstate->pid);
2553  if ((rc = readlink(buf,main_exe,PATH_MAX - 1)) < 1)
2554  return -1;
2555  main_exe[rc] = '\0';
2556 
2557  snprintf(buf,PATH_MAX,"/proc/%d/maps",lstate->pid);
2558  f = fopen(buf,"r");
2559  if (!f)
2560  return 1;
2561 
2562  /*
2563  * Mark all existing regions as dead; then liven up the others.
2564  */
2565  v_g_list_foreach(space->regions,t1,region) {
2566  OBJSDEAD(region,memregion);
2567  }
2568 
2569  while (1) {
2570  errno = 0;
2571  if (!(ret = fgets(buf,PATH_MAX*2,f)) && !errno)
2572  break;
2573  else if (!ret && errno) {
2574  verror("fgets: %s",strerror(errno));
2575  break;
2576  }
2577 
2578  vdebug(9,LA_TARGET,LF_LUP,"scanning mmap line %s",buf);
2579 
2580  rc = sscanf(buf,"%Lx-%Lx %c%c%c%c %Lx %*x:%*x %*d %s",&start,&end,
2581  &p[0],&p[1],&p[2],&p[3],&offset,buf);
2582  if (rc == 8 || rc == 7) {
2583  if (rc == 8) {
2584  /* we got the whole thing, including a path */
2585  if (strncmp(main_exe,buf,PATH_MAX) == 0)
2586  rtype = REGION_TYPE_MAIN;
2587  else if (strcmp(buf,"[heap]") == 0)
2588  rtype = REGION_TYPE_HEAP;
2589  else if (strcmp(buf,"[stack]") == 0)
2590  rtype = REGION_TYPE_STACK;
2591  else if (strcmp(buf,"[vdso]") == 0)
2592  rtype = REGION_TYPE_VDSO;
2593  else if (strcmp(buf,"[vsyscall]") == 0)
2594  rtype = REGION_TYPE_VSYSCALL;
2595  else
2596  rtype = REGION_TYPE_LIB;
2597  }
2598  else {
2599  rtype = REGION_TYPE_ANON;
2600  buf[0] = '\0';
2601  }
2602 
2603  /* Create a region for this map entry if it doesn't already
2604  * exist.
2605  */
2606  if ((rtype != REGION_TYPE_ANON
2607  && !(region = addrspace_match_region_name(space,rtype,buf)))
2608  || (rtype == REGION_TYPE_ANON
2609  && !addrspace_find_range_real(space,start - 1,&region,NULL)
2610  && !(region = addrspace_match_region_start(space,rtype,start)))) {
2611  if (!(region = memregion_create(space,rtype,buf)))
2612  goto err;
2613  OBJSNEW(region);
2614  }
2615 
2616  prot_flags = 0;
2617  if (p[0] == 'r')
2618  prot_flags |= PROT_READ;
2619  if (p[1] == 'w')
2620  prot_flags |= PROT_WRITE;
2621  if (p[2] == 'x')
2622  prot_flags |= PROT_EXEC;
2623  if (p[3] == 's')
2624  prot_flags |= PROT_SHARED;
2625 
2626  if (!(range = memregion_match_range(region,start))) {
2627  if (!(range = memrange_create(region,start,end,offset,0))) {
2628  goto err;
2629  }
2630  OBJSNEW(range);
2631  }
2632  else {
2633  if (range->end == end
2634  && range->offset == offset
2635  && range->prot_flags == prot_flags) {
2636  OBJSLIVE(range,memrange);
2637  }
2638  else {
2639  range->end = end;
2640  range->offset = offset;
2641  range->prot_flags = prot_flags;
2642  OBJSMOD(range);
2643 
2644  if (start < region->base_load_addr)
2645  region->base_load_addr = start;
2646  }
2647  }
2648 
2649  range = NULL;
2650  region = NULL;
2651  }
2652  else if (rc > 0 && !errno) {
2653  vwarn("weird content in /proc/pid/maps (%d)!\n",rc);
2654  }
2655  else if (rc > 0 && errno) {
2656  vwarn("weird content in /proc/pid/maps (%d): %s!\n",rc,strerror(errno));
2657  }
2658  }
2659  fclose(f);
2660 
2661  /*
2662  * Now, for all the regions/ranges, check if they were newly added
2663  * or modified or still exist; if none of those, then they vanished
2664  * and we have to purge them.
2665  */
2666 
2667  v_g_list_foreach_safe(space->regions,t1,t2,region) {
2668  exists = 0;
2669  updated = 0;
2670  v_g_list_foreach_safe(region->ranges,t3,t4,range) {
2671  if (OBJNEW(range)) {
2673  "new range 0x%"PRIxADDR"-0x%"PRIxADDR":%"PRIiOFFSET"\n",
2674  range->start,range->end,range->offset);
2675  exists = 1;
2676 
2677  event = target_create_event(target,NULL,
2679  target_broadcast_event(target,event);
2680  }
2681  else if (OBJLIVE(range)) {
2683  "same range 0x%"PRIxADDR"-0x%"PRIxADDR":%"PRIiOFFSET"\n",
2684  range->start,range->end,range->offset);
2685  exists = 1;
2686  }
2687  else if (OBJMOD(range)) {
2689  "updated range 0x%"PRIxADDR"-0x%"PRIxADDR":%"PRIiOFFSET"\n",
2690  range->start,range->end,range->offset);
2691  exists = 1;
2692  updated = 1;
2693 
2694  event = target_create_event(target,NULL,
2696  target_broadcast_event(target,event);
2697  }
2698  else {
2700  "removing stale range 0x%"PRIxADDR"-0x%"PRIxADDR":%"PRIiOFFSET"\n",
2701  range->start,range->end,range->offset);
2702 
2703  event = target_create_event(target,NULL,
2705  target_broadcast_event(target,event);
2706 
2707  memregion_detach_range(region,range);
2708  range = NULL;
2709  }
2710  }
2711 
2712  if (!exists || !region->ranges) {
2713  vdebug(3,LA_TARGET,LF_LUP,"removing stale region (%s:%s:%s)\n",
2714  region->space->name,region->name,REGION_TYPE(region->type));
2715 
2716  event = target_create_event(target,NULL,
2718  target_broadcast_event(target,event);
2719 
2720  addrspace_detach_region(space,region);
2721  }
2722  else if (updated) {
2723  event = target_create_event(target,NULL,
2725  target_broadcast_event(target,event);
2726  }
2727  else if (OBJNEW(region)) {
2728  event = target_create_event(target,NULL,
2730  target_broadcast_event(target,event);
2731 
2732  /*
2733  * Add debugfiles for the region!
2734  */
2735  if (linux_userproc_loaddebugfiles(target,space,region)) {
2736  vwarn("could not load debugfile for new region (%s:%s:%s)\n",
2737  region->space->name,region->name,
2738  REGION_TYPE(region->type));
2739  }
2740  }
2741  else {
2742  //region->exists = region->new = 0;
2743  }
2744  }
2745 
2746  return 0;
2747 
2748  err:
2749  fclose(f);
2750  // XXX cleanup the regions we added/modified??
2751  return -1;
2752 }
2753 
2754 static int linux_userproc_loaddebugfiles(struct target *target,
2755  struct addrspace *space,
2756  struct memregion *region) {
2757  int retval = -1;
2758  struct debugfile *debugfile = NULL;
2759  struct linux_userproc_state *lstate = \
2760  (struct linux_userproc_state *)target->state;
2761  int bfn = 0;
2762  int bfpn = 0;
2763 
2764  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
2765 
2766  if (!(region->type == REGION_TYPE_MAIN
2767  || region->type == REGION_TYPE_LIB)) {
2768  vdebug(4,LA_TARGET,LF_LUP,"region %s is not MAIN nor LIB; skipping!\n",
2769  region->name);
2770  return 0;
2771  }
2772 
2773  if (!region->name || strlen(region->name) == 0)
2774  return -1;
2775 
2776  debugfile = debugfile_from_file(region->name,
2777  target->spec->debugfile_root_prefix,
2778  target->spec->debugfile_load_opts_list);
2779  if (!debugfile)
2780  goto out;
2781 
2782  if (target_associate_debugfile(target,region,debugfile))
2783  goto out;
2784 
2785  /*
2786  * Try to figure out which binfile has the info we need. On
2787  * different distros, they're stripped different ways.
2788  */
2789  if (0 && debugfile->binfile_pointing) {
2790  binfile_get_root_scope_sizes(debugfile->binfile,&bfn,NULL,NULL,NULL);
2792  NULL,NULL,NULL);
2793  if (bfpn > bfn) {
2794  RHOLD(debugfile->binfile_pointing,region);
2795  region->binfile = debugfile->binfile_pointing;
2796  }
2797  }
2798 
2799  if (!region->binfile) {
2800  RHOLD(debugfile->binfile,region);
2801  region->binfile = debugfile->binfile;
2802  }
2803 
2804  /*
2805  * Propagate some binfile info...
2806  */
2807  region->base_phys_addr = region->binfile->base_phys_addr;
2808  region->base_virt_addr = region->binfile->base_virt_addr;
2809 
2810  retval = 0;
2811 
2812  out:
2813  return retval;
2814 }
2815 
2816 static struct target *
2817 linux_userproc_instantiate_overlay(struct target *target,
2818  struct target_thread *tthread,
2819  struct target_spec *spec,
2820  struct target_thread **ntthread) {
2821  struct target *overlay;
2822 
2823  if (spec->target_type != TARGET_TYPE_PHP) {
2824  errno = EINVAL;
2825  return NULL;
2826  }
2827 
2828  /*
2829  * All we want to do here is create the overlay target.
2830  */
2831  overlay = target_create("php",spec);
2832 
2833  return overlay;
2834 }
2835 
2836 static struct target_thread *
2837 linux_userproc_lookup_overlay_thread_by_id(struct target *target,int id) {
2838  struct target_thread *retval;
2839 
2840  if (id < 0)
2841  id = TID_GLOBAL;
2842 
2843  retval = linux_userproc_load_thread(target,id,0);
2844  if (!retval) {
2845  if (!errno)
2846  errno = ESRCH;
2847  return NULL;
2848  }
2849 
2851  "found overlay thread %d\n",id);
2852 
2853  return retval;
2854 }
2855 
2856 static struct target_thread *
2857 linux_userproc_lookup_overlay_thread_by_name(struct target *target,char *name) {
2858  struct target_thread *retval = NULL;
2859  struct target_thread *tthread;
2860  int rc;
2861  GHashTableIter iter;
2862 
2863  if ((rc = linux_userproc_load_all_threads(target,0)))
2864  vwarn("could not load %d threads; continuing anyway!\n",-rc);
2865 
2866  g_hash_table_iter_init(&iter,target->threads);
2867  while (g_hash_table_iter_next(&iter,NULL,(gpointer)&tthread)) {
2868  if (tthread == target->global_thread)
2869  continue;
2870  else if (tthread->name && strcmp(tthread->name,name) == 0) {
2871  retval = tthread;
2872  break;
2873  }
2874  }
2875 
2876  if (retval) {
2878  "found overlay thread %"PRIiTID"\n",retval->tid);
2879  return tthread;
2880  }
2881  else {
2882  errno = ESRCH;
2883  return NULL;
2884  }
2885 }
2886 
2887 static target_status_t linux_userproc_status(struct target *target) {
2888  char buf[256];
2889  FILE *statf;
2890  char pstate;
2891  target_status_t retval = TSTATUS_ERROR;
2892  int rc;
2893  struct linux_userproc_state *lstate = \
2894  (struct linux_userproc_state *)target->state;
2895  int pid = lstate->pid;
2896 
2897  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",pid);
2898 
2899  again:
2900  snprintf(buf,256,"/proc/%d/stat",pid);
2901  statf = fopen(buf,"r");
2902  if (!statf) {
2903  verror("statf(%s): %s\n",buf,strerror(errno));
2904  return TSTATUS_ERROR;
2905  }
2906 
2907  if ((rc = fscanf(statf,"%d (%s %c",&pid,buf,&pstate))) {
2908  if (pstate == 'R' || pstate == 'r' || pstate == 'W' || pstate == 'w')
2909  retval = TSTATUS_RUNNING;
2910  else if (pstate == 'S' || pstate == 's' || pstate == 'D' || pstate == 'd')
2911  retval = TSTATUS_STOPPED;
2912  else if (pstate == 'Z' || pstate == 'z')
2913  retval = TSTATUS_DEAD;
2914  else if (pstate == 'T' || pstate == 't')
2915  retval = TSTATUS_PAUSED;
2916  else {
2917  vwarn("fscanf returned %d; read %d (%s) %c; returning TSTATUS_UNKNOWN!\n",
2918  rc,pid,buf,pstate);
2919  retval = TSTATUS_UNKNOWN;
2920  }
2921  }
2922  else if (rc < 0 && errno == EINTR) {
2923  fclose(statf);
2924  goto again;
2925  }
2926 
2927  vdebug(3,LA_TARGET,LF_LUP,"pid %d status %d\n",lstate->pid,retval);
2928 
2929  fclose(statf);
2930  return retval;
2931 }
2932 
2933 static int linux_userproc_pause(struct target *target,int nowait) {
2934  GHashTableIter iter;
2935  struct target_thread *tthread;
2936  gpointer key;
2937  int pstatus;
2938  struct linux_userproc_state *lstate = \
2939  (struct linux_userproc_state *)target->state;
2940  int rc = 0;
2941  struct linux_userproc_thread_state *tstate;
2942  siginfo_t sinfo;
2943 
2944  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
2945 
2946  /*
2947  * We send a stop to each traced tid, and wait until it is delivered
2948  * to us! We do not save it for redelivery to the child!
2949  *
2950  * Only do this if the target is not currently paused, because it
2951  * might need to be restarted with whatever last_signo state it had
2952  * previously been paused with.
2953  */
2954 
2955  g_hash_table_iter_init(&iter,target->threads);
2956  while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
2957  if (key == (gpointer)TID_GLOBAL)
2958  continue;
2959 
2960  if (!linux_userproc_load_thread(target,tthread->tid,0)) {
2961  verror("could not load thread %"PRIiTID"; pausing anyway!\n",
2962  tthread->tid);
2963  }
2964  else if (tthread->status == THREAD_STATUS_PAUSED) {
2965  vdebug(3,LA_TARGET,LF_LUP,"tid %d already paused\n",tthread->tid);
2966  continue;
2967  }
2968  /*
2969  * Since thread load is slow (goes through libc and /proc to
2970  * check status, we try a last-ditch means to avoid stomping on
2971  * pending signal info that we need to handle to detect a ptrace
2972  * stop event.
2973  */
2974  else if (waitid(P_PID,tthread->tid,&sinfo,WNOHANG | WNOWAIT) == 0
2975  && sinfo.si_pid == tthread->tid) {
2977  "tid %d has pending siginfo to waitpid on; not pausing here\n",
2978  tthread->tid);
2979  continue;
2980  }
2981 
2982  tstate = (struct linux_userproc_thread_state *)tthread->state;
2983 
2984  tstate->ctl_sig_sent = 1;
2985  /* If we have an evloop attached, we might get interrupted
2986  * before the synchronous waitpid() below is run.
2987  *
2988  * Of course, this is only an issue for multithread uses of this
2989  * library, and we technically would have a race by setting the
2990  * signal state bits like we do here :).
2991  */
2992  if (!nowait)
2993  tstate->ctl_sig_synch = 1;
2994  if (kill(tthread->tid,SIGSTOP) < 0) {
2995  verror("kill(%d,SIGSTOP): %s\n",tthread->tid,strerror(errno));
2996  --rc;
2997  continue;
2998  }
2999 
3000  if (nowait) {
3001  vdebug(3,LA_TARGET,LF_LUP,"not waiting for pause to hit tid %d\n",
3002  tthread->tid);
3003  continue;
3004  }
3005 
3006  vdebug(3,LA_TARGET,LF_LUP,"waiting for pause SIGSTOP to hit tid %d\n",
3007  tthread->tid);
3008  again:
3009  if (waitpid(tthread->tid,&pstatus,__WALL) < 0) {
3010  if (errno == ECHILD || errno == EINVAL) {
3011  verror("waitpid(%"PRIiTID"): %s\n",tthread->tid,strerror(errno));
3012  --rc;
3013  continue;
3014  }
3015  else
3016  goto again;
3017  }
3018  vdebug(3,LA_TARGET,LF_LUP,"pause SIGSTOP has hit tid %d\n",tthread->tid);
3020  tstate->ctl_sig_sent = 0;
3021  tstate->ctl_sig_synch = 0;
3022  }
3023 
3025 
3026  return rc;
3027 }
3028 
3029 static int linux_userproc_pause_thread(struct target *target,tid_t tid,
3030  int nowait) {
3031  int pstatus;
3032  struct linux_userproc_state *lstate;
3033  struct target_thread *tthread;
3034  struct linux_userproc_thread_state *tstate;
3035 
3036  /*
3037  * The global thread doesn't really exist, so we can't pause it.
3038  */
3039  if (tid == TID_GLOBAL)
3040  return 0;
3041 
3042  lstate = (struct linux_userproc_state *)(target->state);
3043  if (!(tthread = target_lookup_thread(target,tid))) {
3044  verror("thread %"PRIiTID" does not exist!\n",tid);
3045  errno = EINVAL;
3046  return 1;
3047  }
3048  tstate = (struct linux_userproc_thread_state *)tthread->state;
3049 
3050  vdebug(5,LA_TARGET,LF_LUP,"pid %d thread %"PRIiTID"\n",lstate->pid,tid);
3051 
3052  /*
3053  * We send a stop to each traced tid, and wait until it is delivered
3054  * to us! We do not save it for redelivery to the child!
3055  *
3056  * Only do this if the target is not currently paused, because it
3057  * might need to be restarted with whatever last_signo state it had
3058  * previously been paused with.
3059  */
3060 
3061  if (!linux_userproc_load_thread(target,tthread->tid,0)) {
3062  verror("could not load thread %"PRIiTID"; pausing anyway!\n",
3063  tthread->tid);
3064  }
3065  else if (tthread->status == THREAD_STATUS_PAUSED) {
3066  vdebug(3,LA_TARGET,LF_LUP,"tid %d already paused\n",tthread->tid);
3067  return 0;
3068  }
3069 
3070  if (kill(tthread->tid,SIGSTOP) < 0) {
3071  verror("kill(%d,SIGSTOP): %s\n",tthread->tid,strerror(errno));
3072  return 1;
3073  }
3074  tstate->ctl_sig_sent = 1;
3075 
3076  if (nowait) {
3077  vdebug(3,LA_TARGET,LF_LUP,"not waiting for pause to hit tid %d\n",tthread->tid);
3078  return 0;
3079  }
3080 
3081  vdebug(3,LA_TARGET,LF_LUP,"waiting for pause SIGSTOP to hit tid %d\n",tthread->tid);
3082  again:
3083  if (waitpid(tthread->tid,&pstatus,__WALL) < 0) {
3084  if (errno == ECHILD || errno == EINVAL) {
3085  verror("waitpid(%"PRIiTID"): %s\n",tthread->tid,strerror(errno));
3086  return 1;
3087  }
3088  else
3089  goto again;
3090  }
3091  vdebug(3,LA_TARGET,LF_LUP,"pause SIGSTOP has hit tid %d\n",tthread->tid);
3093  tstate->ctl_sig_sent = 0;
3094 
3095  return 0;
3096 }
3097 
3098 static int linux_userproc_resume(struct target *target) {
3099  struct linux_userproc_state *lstate;
3100  GHashTableIter iter;
3101  struct target_thread *tthread;
3102  struct linux_userproc_thread_state *tstate;
3103  gpointer key;
3104  int rc = 0;
3105 
3106  lstate = (struct linux_userproc_state *)(target->state);
3107 
3108  vdebug(9,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
3109 
3110  /*
3111  * First, on resume, try to keep handling any threads that were
3112  * "paused" (by the probe layer not restarting them) at some point
3113  * in the breakpoint handling process -- but only if the probepoint
3114  * they needed has freed up.
3115  */
3116 
3117  /* Flush back registers if they're dirty, for paused threads. */
3118  target_flush_all_threads(target);
3119  /* Always invalidate all threads so their status (and state) is
3120  * re-read each time we waitpid.
3121  */
3123 
3124  /*
3125  * Then, go through all the threads and figure out which ones we can
3126  * restart, and which ones we can't. Invalidate only the ones we
3127  * are restarting.
3128  */
3129  g_hash_table_iter_init(&iter,target->threads);
3130  while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
3131  if (key == (gpointer)TID_GLOBAL)
3132  continue;
3133 
3134  /*
3135  * We have several cases. First, we might have tried to pause
3136  * all threads except target->blocking_thread; blocking_thread
3137  * always gets restarted, and all others are ignored. (Well, if
3138  * the blocking thread has a single step scheduled, it may
3139  * already be running -- this is target specific). Second, if a
3140  * thread is not paused, don't restart it (doh). Otherwise, all
3141  * other paused threads get restarted.
3142  */
3143  if (target->blocking_thread && tthread != target->blocking_thread)
3144  continue;
3145 
3146  if (tthread->status != THREAD_STATUS_PAUSED
3147  && tthread->status != THREAD_STATUS_EXITING)
3148  continue;
3149  else if (tthread->resumeat != THREAD_RESUMEAT_NONE)
3150  continue;
3151 
3152  /*
3153  * Do the restart.
3154  */
3155 
3156  if (lstate->ptrace_opts != lstate->ptrace_opts_new) {
3157  errno = 0;
3158  if (ptrace(PTRACE_SETOPTIONS,tthread->tid,NULL,
3159  lstate->ptrace_opts_new) < 0) {
3160  vwarn("ptrace setoptions on tid %"PRIiTID" failed: %s\n",
3161  tthread->tid,strerror(errno));
3162  }
3163  }
3164  tstate = (struct linux_userproc_thread_state *)tthread->state;
3165 
3166  if (tstate->last_signo > -1) {
3167  if (ptrace(lstate->ptrace_type,tthread->tid,NULL,
3168  tstate->last_signo) < 0) {
3169  verror("ptrace signo %d restart of tid %"PRIiTID" failed: %s\n",
3170  tstate->last_signo,tthread->tid,strerror(errno));
3171  --rc;
3172  continue;
3173  }
3174  }
3175  else {
3176  if (ptrace(lstate->ptrace_type,tthread->tid,NULL,NULL) < 0) {
3177  verror("ptrace restart of tid %"PRIiTID" (status %d) failed: %s\n",
3178  tthread->tid,tthread->status,strerror(errno));
3179  --rc;
3180  continue;
3181  }
3182  }
3183 
3184  vdebug(8,LA_TARGET,LF_LUP,"ptrace restart pid %d tid %"PRIiTID" succeeded\n",
3185  lstate->pid,tthread->tid);
3187 
3188  tstate->last_signo = -1;
3189  tstate->last_status = -1;
3190  }
3191 
3192  if (lstate->ptrace_opts != lstate->ptrace_opts_new)
3193  lstate->ptrace_opts = lstate->ptrace_opts_new;
3194 
3195  /*
3196  * If there's a blocking thread, we can't do anything here, yet.
3197  */
3198  if (!target->blocking_thread) {
3199  g_hash_table_iter_init(&iter,target->threads);
3200  while (g_hash_table_iter_next(&iter,&key,(gpointer)&tthread)) {
3201  if (key == (gpointer)TID_GLOBAL)
3202  continue;
3203 
3204  /*
3205  * If the thread was needing to be resumed because it could not
3206  * own the probepoint, and the probepoint it needed is now free,
3207  * then let it go! Also, as soon as one of these happens
3208  * successfully and sets target->blocking, break out of the
3209  * loop, because once a resumeat thread calls target_pause,
3210  * we can't try to do anything else that would call
3211  * target_pause and thus set target->blocking_thread.
3212  */
3213  if (tthread->status == THREAD_STATUS_PAUSED
3214  && tthread->resumeat != THREAD_RESUMEAT_NONE) {
3215  probepoint_resumeat_handler(target,tthread);
3216  }
3217 
3218  if (target->blocking_thread)
3219  break;
3220  }
3221  }
3222 
3224  return 0;
3225 }
3226 
3227 static target_status_t linux_userproc_handle_exception(struct target *target,
3229  int *again,void *priv) {
3230  struct linux_userproc_state *lstate;
3231  REG dreg = -1;
3232  struct probepoint *dpp;
3233  REGVAL ipval;
3234 #if __WORDSIZE == 64
3235  unsigned long cdr;
3236 #else
3237  int cdr;
3238 #endif
3239  struct target_thread *tthread = NULL;
3240  struct linux_userproc_thread_state *tstate;
3241  tid_t newtid;
3242  int pid;
3243  long int newstatus;
3244  struct addrspace *space;
3246  tid_t tid;
3247  int pstatus;
3248  GList *t1;
3249  struct target_event *event;
3250 
3251  exst = (struct linux_userproc_exception_handler_state *)priv;
3252  tid = exst->tid;
3253  pstatus = exst->pstatus;
3254 
3255  lstate = (struct linux_userproc_state *)(target->state);
3256  pid = lstate->pid;
3257  if (!(tthread = target_lookup_thread(target,tid))) {
3258  if (__tid_exists(pid,tid)) {
3260  "thread %d does not YET exist; might be new!\n",tid);
3261  g_hash_table_insert(lstate->new_racy_threads,
3262  (gpointer)(uintptr_t)tid,
3263  (gpointer)(uintptr_t)pstatus);
3264  goto out_again;
3265  }
3266  verror("thread %"PRIiTID" does not exist!\n",tid);
3267  errno = EINVAL;
3268  goto out_err;
3269  }
3270  tstate = (struct linux_userproc_thread_state *)tthread->state;
3271 
3272  if (WIFSTOPPED(pstatus)) {
3273  /* Ok, this was a ptrace event; figure out which sig (or if it
3274  * was a syscall), and redeliver the sig if it was a sig;
3275  * otherwise, don't deliver a sig, and just continue the child,
3276  * on resume.
3277  */
3278 
3279  /*
3280  * We know the thread is at least "paused", so set that; change
3281  * it later depending on *which* ptrace event *if necessary*.
3282  *
3283  * (This avoids expensive polling of /proc/pid/task/tid/stat in
3284  * linux_userproc_load_thread, and we can just call
3285  * __linux_userproc_load_thread.)
3286  */
3289 
3290  /*
3291  * Handle clone before loading the current thread; we don't need
3292  * the extra overhead of loading the current thread in this
3293  * case; we just want to attach to the new thread right away.
3294  */
3295  if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_CLONE << 8)) {
3296  ptrace(PTRACE_GETEVENTMSG,tid,NULL,&newstatus);
3297  newtid = (tid_t)newstatus;
3299  "target %d thread %d cloned new thread %d; attaching now.\n",
3300  pid,tid,newtid);
3301 
3302  linux_userproc_attach_thread(target,tid,newtid);
3303  /*
3304  * Flush, invalidate, and restart the parent.
3305  */
3306  /*
3307  if (ptrace(lstate->ptrace_type,tid,NULL,NULL) < 0) {
3308  vwarn("ptrace parent restart failed: %s\n",strerror(errno));
3309  }
3310  */
3311  //target_thread_set_status(tthread,THREAD_STATUS_RUNNING);
3312  goto out_again;
3313  }
3314 
3315  //if (!lstate->live_syscall_maps_tracking) {
3316  v_g_list_foreach(target->spaces,t1,space) {
3317  linux_userproc_updateregions(target,space);
3318  }
3319  //}
3320 
3321  if (!__linux_userproc_load_thread(target,tid,0,1)) {
3322  verror("could not load thread %"PRIiTID"!\n",tid);
3323  goto out_err;
3324  }
3325 
3326  target->current_thread = tthread;
3327  lstate->current_tid = tthread->tid;
3328 
3329  tstate->last_status = tstate->last_signo = WSTOPSIG(pstatus);
3330  if (tstate->last_status == (SIGTRAP | 0x80)) {
3332  "thread %"PRIiTID" stopped with syscall trap signo %d\n",
3333  tid,tstate->last_status);
3334  tstate->last_signo = -1;
3335  }
3336  else if (pstatus >> 8 == (SIGTRAP | PTRACE_EVENT_EXIT << 8)) {
3338  "target %d tid %d exiting (%d)! will detach at next resume.\n",
3339  pid,tid,tstate->last_status);
3340  tstate->last_signo = -1;
3341  //linux_userproc_detach(target);
3342  //target_set_status(target,TSTATUS_EXITING);
3344 
3345  event = target_create_event(target,NULL,T_EVENT_EXITING,target);
3346  target_broadcast_event(target,event);
3347 
3348  return THREAD_STATUS_EXITING;
3349  }
3350  else if (tstate->last_status == SIGTRAP) {
3351  /* Don't deliver debug traps! */
3352  vdebug(5,LA_TARGET,LF_LUP,"thread %"PRIiTID" stopped with trap signo %d\n",
3353  tid,tstate->last_status);
3354  tstate->last_signo = -1;
3355 
3356  /*
3357  * This is where we handle breakpoint or single step
3358  * events.
3359  *
3360  * If this was a single step event, notify the ss handler
3361  * (we always assume that if target->sstep_probepoint was
3362  * set, we are single stepping with hardware breakpoints
3363  * disabled, since the target code does this for us!).
3364  *
3365  * Otherwise, if the address matches one of our hardware
3366  * breakpoints, we pass that addr to the handler.
3367  *
3368  * Otherwise, if it doesn't match, we (locally, not in the
3369  * CPU's register state -- the generic bp handler does
3370  * this!) decrement EIP by the breakpoint instruction length
3371  * and search for that address. If we find one, we notify
3372  * that BP handler.
3373  *
3374  * Otherwise, if we haven't found a SW probepoint that
3375  * matches, return to the user, and let THEM handle it!
3376  */
3377 
3378  /* Check the hw debug status reg first */
3379  errno = 0;
3380  cdr = ptrace(PTRACE_PEEKUSER,tid,
3381  offsetof(struct user,u_debugreg[6]),NULL);
3382  if (errno) {
3383  vwarn("could not read current val of status debug reg;"
3384  " don't know which handler to call; fatal!\n");
3385  return THREAD_STATUS_ERROR;
3386  }
3387 
3388  /*
3389  * Check the breakpoints first; starting with the debug
3390  * status register.
3391  */
3392  if (cdr & 15) {
3393  if (cdr & 0x1)
3394  dreg = 0;
3395  else if (cdr & 0x2)
3396  dreg = 1;
3397  else if (cdr & 0x4)
3398  dreg = 2;
3399  else if (cdr & 0x8)
3400  dreg = 3;
3401 
3402  /* If we are relying on the status reg to tell us,
3403  * then also read the actual hw debug reg to get the
3404  * address we broke on.
3405  */
3406  errno = 0;
3407  ipval = ptrace(PTRACE_PEEKUSER,tid,
3408  offsetof(struct user,u_debugreg[dreg]),NULL );
3409  if (errno) {
3410  verror("could not read current val of debug reg %d after up status!\n",dreg);
3411  return THREAD_STATUS_ERROR;
3412  }
3413 
3415  "found hw break (status) in dreg %d on 0x%"PRIxADDR"\n",
3416  dreg,ipval);
3417  }
3418  else {
3419  ipval = linux_userproc_read_reg(target,tid,target->ipregno);
3420  if (errno) {
3421  verror("could not read EIP while finding probepoint: %s\n",
3422  strerror(errno));
3423  return THREAD_STATUS_ERROR;
3424  }
3425 
3426  if (tstate->dr[0] == (ptrace_reg_t)ipval)
3427  dreg = 0;
3428  else if (tstate->dr[1] == (ptrace_reg_t)ipval)
3429  dreg = 1;
3430  else if (tstate->dr[2] == (ptrace_reg_t)ipval)
3431  dreg = 2;
3432  else if (tstate->dr[3] == (ptrace_reg_t)ipval)
3433  dreg = 3;
3434 
3435  if (dreg > -1)
3437  "found hw break (eip) in dreg %d on 0x%"PRIxADDR"\n",
3438  dreg,ipval);
3439  else
3441  "checking for SS or sw break on 0x%"PRIxADDR"\n",
3442  ipval - target->arch->breakpoint_instrs_len);
3443  }
3444 
3445  /*
3446  * Handle the hardware breakpoint if we found one.
3447  */
3448  if (dreg > -1) {
3449  /* Found HW breakpoint! */
3450  /* Clear the status bits right now. */
3451  errno = 0;
3452  if (ptrace(PTRACE_POKEUSER,tid,
3453  offsetof(struct user,u_debugreg[6]),0)) {
3454  verror("could not clear status debug reg, continuing"
3455  " anyway: %s!\n",strerror(errno));
3456  errno = 0;
3457  }
3458  else {
3459  vdebug(5,LA_TARGET,LF_LUP,"cleared status debug reg 6\n");
3460  }
3461 
3462  dpp = (struct probepoint *) \
3463  g_hash_table_lookup(tthread->hard_probepoints,
3464  (gpointer)ipval);
3465 
3466  if (!dpp) {
3467  verror("found hw breakpoint 0x%"PRIxADDR
3468  " in debug reg %d, BUT no probepoint!\n",
3469  ipval,dreg);
3470  return THREAD_STATUS_ERROR;
3471  }
3472 
3473  if (target->ops->handle_break(target,tthread,dpp,cdr & 0x4000)
3474  != RESULT_SUCCESS)
3475  return THREAD_STATUS_ERROR;
3476  goto out_again;
3477  }
3478  /*
3479  * Try to handle a single step.
3480  *
3481  * NOTE NOTE NOTE: we must do this before checking the
3482  * software breakpoint. Suppose we are single stepping
3483  * something at the breakpoint location; if we take the
3484  * single step interrupt, but check the software probepoint
3485  * below first, we'll think we're at a breakpoint, but we're
3486  * at a single step.
3487  *
3488  * Similarly, but differently, we cannot check single step
3489  * before hardware brekapoint, because we don't catch
3490  * the case where we single step into a hardware breakpoint
3491  * -- we miss the single step because the hw bp seems to
3492  * dominate the single step exception. handle_break handles
3493  * this...
3494  */
3495  else if (cdr & 0x4000) {
3496  if (tthread->tpc) {
3497  if (target->ops->handle_step(target,tthread,tthread->tpc->probepoint)
3498  != RESULT_SUCCESS)
3499  return THREAD_STATUS_ERROR;
3500  goto out_again;
3501  }
3502  else {
3503  if (target->ops->handle_step(target,tthread,NULL)
3504  != RESULT_SUCCESS)
3505  return THREAD_STATUS_ERROR;
3506  goto out_again;
3507  }
3508  }
3509  /* Try to handle a software breakpoint. */
3510  else if ((dpp = (struct probepoint *) \
3511  g_hash_table_lookup(target->soft_probepoints,
3512  (gpointer)(ipval - target->arch->breakpoint_instrs_len)))) {
3513  if (target->ops->handle_break(target,tthread,dpp,cdr & 0x4000)
3514  != RESULT_SUCCESS)
3515  return THREAD_STATUS_ERROR;
3516  goto out_again;
3517  }
3518  else {
3519  vwarn("could not find hardware bp and not sstep'ing;"
3520  " letting user handle fault at 0x%"PRIxADDR"!\n",
3521  ipval);
3522  }
3523  }
3524  else {
3525  if (tstate->ctl_sig_sent) {
3527  "thread %"PRIiTID" stopped with (our) signo %d\n",
3528  tid,tstate->last_status);
3529  /*
3530  * Don't reinject this signal!
3531  */
3532  tstate->last_signo = -1;
3533  tstate->ctl_sig_sent = 0;
3534 
3535  /*
3536  * If we were trying to sigstop all threads, let all the
3537  * stop sigs get recv'd...
3538  */
3539  if (lstate->ctl_sig_pausing_all) {
3540  tstate->ctl_sig_recv = 1;
3541  goto out_again;
3542  }
3543  else {
3544  tstate->ctl_sig_recv = 0;
3545  /* Restart just this thread. */
3546  if (ptrace(lstate->ptrace_type,tid,NULL,NULL) < 0) {
3547  verror("ptrace restart of tid %"PRIiTID" failed: %s\n",
3548  tid,strerror(errno));
3549  }
3551  goto out_again;
3552  }
3553  }
3554  else
3556  "thread %"PRIiTID" stopped with (ext) signo %d\n",
3557  tid,tstate->last_status);
3558  }
3559 
3560  return THREAD_STATUS_PAUSED;
3561  }
3562  else if (WIFCONTINUED(pstatus)) {
3563 
3565 
3566  tstate->last_signo = -1;
3567  tstate->last_status = -1;
3568 
3569  goto out_again;
3570  }
3571  else if (WIFSIGNALED(pstatus) || WIFEXITED(pstatus)) {
3572 
3574 
3575  /* yikes, it was sigkill'd out from under us! */
3576  /* XXX: is error good enough? The pid is gone; we should
3577  * probably dump this target.
3578  */
3580 
3581  event = target_create_event_2(target,tthread,
3583  tthread,(void *)(uintptr_t)pstatus);
3584  target_broadcast_event(target,event);
3585 
3586  /*
3587  * If we're out of threads (besides the global thread); detach
3588  * the target now. Otherwise, just detach from the thread.
3589  */
3590  if (g_hash_table_size(target->threads) == 2) {
3592 
3593  event = target_create_event(target,tthread,T_EVENT_EXITED,NULL);
3594  target_broadcast_event(target,event);
3595 
3596  linux_userproc_detach(target,0);
3597  }
3598  else
3599  linux_userproc_detach_thread(target,tid,0,0);
3600 
3601  return THREAD_STATUS_DONE;
3602  }
3603  else {
3605 
3606  vwarn("unexpected child process status event: %08x; bailing!\n",
3607  pstatus);
3608  return THREAD_STATUS_ERROR;
3609  }
3610 
3611  out_err:
3612  return THREAD_STATUS_ERROR;
3613 
3614  out_again:
3615  /*
3616  * If this is not returning to the user (i.e., not going back
3617  * through target_resume()), we must flush and invalidate our
3618  * threads ourself!
3619  */
3620  //target_flush_all_threads(target);
3621  //target_invalidate_all_threads(target);
3622  //target_set_status(target,TSTATUS_RUNNING);
3623  target_resume(target);
3624  if (again)
3625  *again = 1;
3626  return THREAD_STATUS_RUNNING;
3627 }
3628 
3629 int linux_userproc_evloop_handler(int readfd,int fdtype,void *state) {
3630  int tid;
3631  int retval;
3632  int status;
3633  int again = 0;
3634  struct target *target = (struct target *)state;
3635  struct target_thread *tthread;
3636  struct linux_userproc_thread_state *tstate;
3638 
3639  if ((tid = waitpipe_get_pid(readfd)) < 0) {
3640  verror("could not find thread tid for readfd %d!\n",readfd);
3641  errno = ESRCH;
3642  return EVLOOP_HRET_BADERROR;
3643  }
3644 
3645  if ((waitpipe_drain(tid)) < 0) {
3646  verror("waitpipe_drain: %s\n",strerror(errno));
3647  return EVLOOP_HRET_BADERROR;
3648  }
3649 
3650  if (!(tthread = target_lookup_thread(target,tid))) {
3651  verror("cound not find thread %d!\n",tid);
3652  errno = ESRCH;
3653  return EVLOOP_HRET_BADERROR;
3654  }
3655 
3656  tstate = (struct linux_userproc_thread_state *)tthread->state;
3657 
3658  if (tstate->ctl_sig_sent && tstate->ctl_sig_synch) {
3660  "synchronous ctl sig sent to tid %d; not calling waitpid; ignoring"
3661  " (probable multithread bug!)\n",
3662  tid);
3663  return EVLOOP_HRET_SUCCESS;
3664  }
3665 
3666  /* Need to waitpid(tid) to grab status, then can call handle_exception(). */
3667  again:
3668  retval = waitpid(tid,&status,WNOHANG | __WALL);
3669  if (retval < 0) {
3670  if (errno == ECHILD || errno == EINVAL) {
3671  verror("waitpid(%"PRIiTID"): %s\n",tid,strerror(errno));
3672  return EVLOOP_HRET_ERROR;
3673  }
3674  else
3675  goto again;
3676  }
3677  else if (retval == 0) {
3679  "tid %"PRIiTID" running; waitpid has nothing to report; ignoring!\n",
3680  tid);
3681  return EVLOOP_HRET_SUCCESS;
3682  }
3683 
3685  "tid %"PRIiTID" running; handling evloop sig\n",tid);
3686 
3687  /*
3688  * Ok, handle whatever happened. If we can't handle it, pass
3689  * control to the user, just like monitor() would.
3690  */
3691  exst.tid = tid;
3692  exst.pstatus = status;
3693  retval = linux_userproc_handle_exception(target,0,&again,&exst);
3694 
3695  if (THREAD_SPECIFIC_STATUS(retval)) {
3696  verror("thread-specific status %d tid %d; bad!\n",retval,tid);
3697  return EVLOOP_HRET_BADERROR;
3698  }
3699  else if (retval == TSTATUS_ERROR) {
3700  /* invoke user error handler... ? */
3701  verror("unexpected error on thread %d; bad!\n",tid);
3702  return EVLOOP_HRET_ERROR;
3703  }
3704  else if (retval == TSTATUS_UNKNOWN) {
3705  /* invoke user error handler... ? */
3706  verror("unexpected unknown on thread %d; bad!\n",tid);
3707  return EVLOOP_HRET_ERROR;
3708  }
3709  else if (retval == TSTATUS_RUNNING) {
3710  /* target_resume() has been called, so return success */
3711  return EVLOOP_HRET_SUCCESS;
3712  }
3713  else if (retval == TSTATUS_EXITING) {
3714  /*
3715  * NB:
3716  *
3717  * target_resume() has NOT been called; we need to call it since
3718  * we don't want to intercept it.
3719  */
3720  target_resume(target);
3721  return EVLOOP_HRET_SUCCESS;
3722  }
3723  else if (retval == TSTATUS_DONE) {
3724  /* remove FD from set */
3726  "tid %"PRIiTID" done; removing its fd (%d) from evloop\n",
3727  tid,readfd);
3728  /*
3729  * NB:
3730  *
3731  * If the whole target is not finished, but only this thread,
3732  * then resume!
3733  */
3734  if (target->status != TSTATUS_DONE)
3735  target_resume(target);
3736  /* now remove FD from set */
3738  }
3739  else if (retval == TSTATUS_PAUSED
3740  && tstate->last_signo > -1) {
3741  /*
3742  * NB:
3743  *
3744  * target_resume() has NOT been called; we need to call it since
3745  * we don't want to intercept it.
3746  *
3747  * It was signaled; we don't expose this to the user when evloop
3748  * is handling!
3749  */
3751  "tid %"PRIiTID" signaled with %d; resuming; signal will hit tid\n",
3752  tid,tstate->last_signo);
3753  target_resume(target);
3754  return EVLOOP_HRET_SUCCESS;
3755  }
3756  else if (retval == TSTATUS_PAUSED) {
3757  /* user must handle fault; invoke error handler */
3758  vwarn("unexpected pause on thread %d; bad!\n",tid);
3759  return EVLOOP_HRET_ERROR;
3760  }
3761  else {
3762  /* user must handle unexpected fault; invoke error handler */
3763  verror("unexpected error on thread %d; bad!\n",tid);
3764  return EVLOOP_HRET_BADERROR;
3765  }
3766 }
3767 
3768 /*
3769  * We need to add waitpipe FDs for any tids we are monitoring. Also,
3770  * once we setup an evloop, we need to keep tracking tid
3771  * addition/subtraction and add/remove waitpipes as the tids come and
3772  * go.
3773  */
3774 static int linux_userproc_evloop_add_tid(struct target *target,int tid) {
3775  int readfd;
3776 
3777  if (!target->evloop) {
3778  verror("no evloop attached!\n");
3779  return -1;
3780  }
3781 
3782  if ((readfd = waitpipe_get(tid)) > 0) {
3784  "not adding waitpipe readfd %d for tid %d\n",readfd,tid);
3785  }
3786  else {
3787  readfd = waitpipe_add(tid);
3788  if (readfd < 0) {
3789  verror("could not add tid %d to waitpipe!\n",tid);
3790  return -1;
3791  }
3792 
3793  evloop_set_fd(target->evloop,readfd,EVLOOP_FDTYPE_R,
3795 
3797  "added waitpipe/evloop readfd %d for tid %d\n",readfd,tid);
3798  }
3799 
3800  return 0;
3801 }
3802 
3803 /*
3804  * We need to remove the waitpipe FDs for each tid we are monitoring!
3805  */
3806 static int linux_userproc_evloop_del_tid(struct target *target,int tid) {
3807  int readfd;
3808 
3809  if (!target->evloop) {
3810  verror("no evloop attached!\n");
3811  return -1;
3812  }
3813 
3814  if ((readfd = waitpipe_get(tid)) > 0) {
3815  evloop_unset_fd(target->evloop,readfd,EVLOOP_FDTYPE_A);
3816  waitpipe_remove(tid);
3817 
3819  "removed waitpipe/evloop readfd %d for tid %d\n",
3820  readfd,tid);
3821  }
3822  else {
3824  "did not find valid readfd (%d) to remove tid %d from evloop!\n",
3825  readfd,tid);
3826  }
3827 
3828  if (target->infd > -1)
3829  evloop_unset_fd(target->evloop,target->infd,EVLOOP_FDTYPE_A);
3830  if (target->outfd > -1)
3831  evloop_unset_fd(target->evloop,target->outfd,EVLOOP_FDTYPE_A);
3832  if (target->errfd > -1)
3833  evloop_unset_fd(target->evloop,target->errfd,EVLOOP_FDTYPE_A);
3834 
3835  return 0;
3836 }
3837 
3838 int linux_userproc_attach_evloop(struct target *target,struct evloop *evloop) {
3839  struct array_list *tids;
3840  int tid;
3841  int i;
3842 
3843  if (!waitpipe_is_initialized())
3844  waitpipe_init_auto(NULL);
3845 
3846  if (target->infd > -1)
3847  evloop_set_fd(target->evloop,target->infd,EVLOOP_FDTYPE_W,
3848  target->spec->in_evh,target);
3849  if (target->outfd > -1)
3850  evloop_set_fd(target->evloop,target->outfd,EVLOOP_FDTYPE_R,
3851  target->spec->out_evh,target);
3852  if (target->errfd > -1)
3853  evloop_set_fd(target->evloop,target->errfd,EVLOOP_FDTYPE_R,
3854  target->spec->err_evh,target);
3855 
3856  tids = target_list_tids(target);
3857  if (!tids)
3858  return 0;
3859 
3860  array_list_foreach_fakeptr_t(tids,i,tid,uintptr_t) {
3861  linux_userproc_evloop_add_tid(target,tid);
3862  }
3863 
3864  array_list_free(tids);
3865 
3866  return 0;
3867 }
3868 
3869 int linux_userproc_detach_evloop(struct target *target) {
3870  struct array_list *tids;
3871  int tid;
3872  int i;
3873 
3874  if (!waitpipe_is_initialized())
3875  waitpipe_init_auto(NULL);
3876 
3877  tids = target_list_tids(target);
3878  if (!tids)
3879  return 0;
3880 
3881  array_list_foreach_fakeptr_t(tids,i,tid,uintptr_t) {
3882  linux_userproc_evloop_del_tid(target,tid);
3883  }
3884 
3885  array_list_free(tids);
3886 
3887  return 0;
3888 }
3889 
3890 static target_status_t linux_userproc_poll(struct target *target,
3891  struct timeval *tv,
3892  target_poll_outcome_t *outcome,
3893  int *pstatus) {
3894  int tid;
3895  int status;
3896  target_status_t retval;
3897  struct linux_userproc_state *lstate = \
3898  (struct linux_userproc_state *)target->state;
3899  struct timespec req, rem;
3900  unsigned int usec_thresh = 100; // 100 us is the least we'll sleep
3901  uint64_t total_us;
3902  uint64_t total_ns = 0;
3904 
3905  if (tv) {
3906  total_us = tv->tv_sec * 1000000 + tv->tv_usec;
3907  total_ns = total_us * 1000;
3908 
3909  if (total_us < usec_thresh) {
3910  req.tv_sec = 0;
3911  req.tv_nsec = total_ns;
3912  }
3913  else {
3914  req.tv_sec = 0;
3915  req.tv_nsec = usec_thresh * 1000;
3916  }
3917  }
3918 
3919  vdebug(9,LA_TARGET,LF_LUP,"waitpid target %d\n",lstate->pid);
3920  again:
3921  tid = waitpid(-lstate->pid,&status,WNOHANG | __WALL);
3922  if (tid < 0) {
3923  /* We always do this on error; these two errnos are the only
3924  * ones we should see, though.
3925  */
3926  if (1 || errno == ECHILD || errno == EINVAL) {
3927  if (outcome)
3928  *outcome = POLL_ERROR;
3929  return TSTATUS_ERROR;
3930  }
3931  }
3932  else if (tid == 0) {
3933  if (!tv) {
3934  if (outcome)
3935  *outcome = POLL_NOTHING;
3936  /* Assume it is running! Is this right? */
3937  return TSTATUS_RUNNING;
3938  }
3939  else {
3940  /* Try to sleep for a bit. */
3941  rem.tv_sec = 0;
3942  rem.tv_nsec = 0;
3943  nanosleep(&req,&rem);
3944  if (rem.tv_nsec)
3945  total_ns -= req.tv_nsec - rem.tv_nsec;
3946  else
3947  total_ns -= req.tv_nsec;
3948 
3949  if (total_ns > 0) {
3950  if (total_ns > usec_thresh * 1000)
3951  req.tv_nsec = usec_thresh * 1000;
3952  else
3953  req.tv_nsec = total_ns;
3954 
3955  goto again;
3956  }
3957  else {
3958  if (outcome)
3959  *outcome = POLL_NOTHING;
3960  /* Assume it is running! Is this right? */
3961  return TSTATUS_RUNNING;
3962  }
3963  }
3964  }
3965  else if (target_lookup_thread(target,tid)) {
3966  if (outcome)
3967  *outcome = POLL_SUCCESS;
3968  if (pstatus)
3969  *pstatus = status;
3970 
3971  /*
3972  * Ok, handle whatever happened. If we can't handle it, pass
3973  * control to the user, just like monitor() would.
3974  */
3975  exst.tid = tid;
3976  exst.pstatus = status;
3977  retval = linux_userproc_handle_exception(target,0,NULL,&exst);
3978 
3979  if (THREAD_SPECIFIC_STATUS(retval)) {
3980  vwarn("unhandled thread-specific status %d!\n",retval);
3981  }
3982 
3983  return retval;
3984  }
3985  else {
3986  if (outcome)
3987  *outcome = POLL_UNKNOWN;
3988  return TSTATUS_ERROR;
3989  }
3990 }
3991 
3992 static target_status_t linux_userproc_monitor(struct target *target) {
3993  int tid;
3994  int pstatus;
3995  int again = 0;
3996  thread_status_t retval;
3997  struct linux_userproc_state *lstate = \
3998  (struct linux_userproc_state *)target->state;
4000 
4001  vdebug(9,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
4002 
4003  /* do the whole ptrace waitpid dance */
4004 
4005  again:
4006  vdebug(9,LA_TARGET,LF_LUP,"monitor pid %d (again %d)\n",lstate->pid,again);
4007  again = 0;
4008  tid = waitpid(-1,&pstatus,__WALL);
4009  if (tid < 0) {
4010  if (errno == ECHILD || errno == EINVAL)
4011  return TSTATUS_ERROR;
4012  else
4013  goto again;
4014  }
4015 
4016  exst.tid = tid;
4017  exst.pstatus = pstatus;
4018  retval = linux_userproc_handle_exception(target,0,&again,&exst);
4019  if (again)
4020  goto again;
4021 
4022  // xxx write! then write generic target functions, clean up the
4023  // headers and makefile, and get it to compile. then add debug code
4024  // and try to actually load regions and monitor a process!
4025 
4026  if (THREAD_SPECIFIC_STATUS(retval)) {
4027  vwarn("unhandled thread-specific status %d!\n",retval);
4028  }
4029 
4030  return retval;
4031 }
4032 
4033 static unsigned char *linux_userproc_read(struct target *target,
4034  ADDR addr,
4035  unsigned long length,
4036  unsigned char *buf) {
4037  struct linux_userproc_state *lstate = \
4038  (struct linux_userproc_state *)target->state;
4039 
4040  vdebug(5,LA_TARGET,LF_LUP,"pid %d\n",lstate->pid);
4041 
4042  /* Don't bother checking if process is stopped! We can't send it a
4043  * STOP without interfering with its execution, so we don't!
4044  */
4045  return target_generic_fd_read(lstate->memfd,addr,length,buf);
4046 }
4047 
4048 unsigned long linux_userproc_write(struct target *target,
4049  ADDR addr,
4050  unsigned long length,
4051  unsigned char *buf) {
4052  struct linux_userproc_state *lstate;
4053  lstate = (struct linux_userproc_state *)(target->state);
4054 #if __WORDSIZE == 64
4055  int64_t word;
4056 #else
4057  int32_t word;
4058 #endif
4059  struct memrange *range = NULL;;
4060  unsigned int i = 0;
4061  unsigned int j;
4062 
4063  vdebug(5,LA_TARGET,LF_LUP,"pid %d length %lu ",lstate->pid,length);
4064  for (j = 0; j < length && j < 16; ++j)
4065  vdebugc(5,LA_TARGET,LF_LUP,"%02hhx ",buf[j]);
4066  vdebugc(5,LA_TARGET,LF_LUP,"\n");
4067 
4068  target_find_memory_real(target,addr,NULL,NULL,&range);
4069 
4070  /* Don't bother checking if process is stopped! We can't send it a
4071  * STOP without interfering with its execution, so we don't!
4072  */
4073 
4074  /*
4075  * We cannot just write to text/executable ranges via the memory
4076  * device. BUT, if we can't resolve the address to a range, we just
4077  * try it anyway.
4078  */
4079  if (!range || range->prot_flags & PROT_WRITE) {
4080  return target_generic_fd_write(lstate->memfd,addr,length,buf);
4081  }
4082 
4083  /*
4084  * If we're writing to a write-protected range, we have to use
4085  * ptrace, word by word! So if our write doesn't end on a word
4086  * boundary, first read the word containing the last byte we're
4087  * going to write, and fill it with our last byte. Then write all
4088  * the preceding words, and finally the special last word.
4089  */
4090  if (length % (__WORDSIZE / 8)) {
4091  errno = 0;
4092  word = ptrace(PTRACE_PEEKTEXT,lstate->current_tid,
4093  (addr + length) - (length % (__WORDSIZE / 8)),
4094  NULL);
4095  if (errno) {
4096  verror("ptrace(PEEKTEXT) last word: %s\n",strerror(errno));
4097  return 0;
4098  }
4099 
4100  vdebug(9,LA_TARGET,LF_LUP,"last word was ");
4101  for (j = 0; j < __WORDSIZE / 8; ++j)
4102  vdebugc(9,LA_TARGET,LF_LUP,"%02hhx ",*(((char *)&word) + j));
4103  vdebugc(9,LA_TARGET,LF_LUP,"\n");
4104 
4105  memcpy(&word,(buf + length) - (length % (__WORDSIZE / 8)),
4106  length % (__WORDSIZE / 8));
4107 
4108  vdebug(9,LA_TARGET,LF_LUP,"new last word is ");
4109  for (j = 0; j < __WORDSIZE / 8; ++j)
4110  vdebugc(9,LA_TARGET,LF_LUP,"%02hhx ",*(((char *)&word) + j));
4111  vdebugc(9,LA_TARGET,LF_LUP,"\n");
4112  }
4113 
4114  if (length / (__WORDSIZE / 8)) {
4115  for (i = 0; i < length; i += (__WORDSIZE / 8)) {
4116  errno = 0;
4117  if (ptrace(PTRACE_POKETEXT,lstate->current_tid,
4118 #if __WORDSIZE == 64
4119  addr + i,*(uint64_t *)(buf + i)) == -1) {
4120 #else
4121  addr + i,*(uint32_t *)(buf + i)) == -1) {
4122 #endif
4123  verror("ptrace(POKETEXT): %s\n",strerror(errno));
4124  return 0;
4125  }
4126  }
4127  }
4128 
4129  if (length % (__WORDSIZE / 8)) {
4130  errno = 0;
4131  if (ptrace(PTRACE_POKETEXT,lstate->current_tid,
4132  (i) ? addr + i - (__WORDSIZE / 8) : addr,
4133  word) == -1) {
4134  verror("ptrace(POKETEXT) last word: %s\n",strerror(errno));
4135  return 0;
4136  }
4137  }
4138 
4139  return length;
4140 }
4141 
4142 /*
4143  * The register mapping between x86_64 registers is defined by AMD in
4144  * http://www.x86-64.org/documentation/abi-0.99.pdf :
4145  *
4146  *
4147  * Figure 3.36: DWARF Register Number Mapping
4148  * Register Name Number Abbreviation
4149  * General Purpose Register RAX 0 %rax
4150  * General Purpose Register RDX 1 %rdx
4151  * General Purpose Register RCX 2 %rcx
4152  * General Purpose Register RBX 3 %rbx
4153  * General Purpose Register RSI 4 %rsi
4154  * General Purpose Register RDI 5 %rdi
4155  * Frame Pointer Register RBP 6 %rbp
4156  * Stack Pointer Register RSP 7 %rsp
4157  * Extended Integer Registers 8-15 8-15 %r8-%r15
4158  * Return Address RA 16
4159  * Vector Registers 0-7 17-24 %xmm0-%xmm7
4160  * Extended Vector Registers 8-15 25-32 %xmm8-%xmm15
4161  * Floating Point Registers 0-7 33-40 %st0-%st7
4162  * MMX Registers 0-7 41-48 %mm0-%mm7
4163  * Flag Register 49 %rFLAGS
4164  * Segment Register ES 50 %es
4165  * Segment Register CS 51 %cs
4166  * Segment Register SS 52 %ss
4167  * Segment Register DS 53 %ds
4168  * Segment Register FS 54 %fs
4169  * Segment Register GS 55 %gs
4170  * Reserved 56-57
4171  * FS Base address 58 %fs.base
4172  * GS Base address 59 %gs.base
4173  * Reserved 60-61
4174  * Task Register 62 %tr
4175  * LDT Register 63 %ldtr
4176  * 128-bit Media Control and Status 64 %mxcsr
4177  * x87 Control Word 65 %fcw
4178  * x87 Status Word 66 %fsw
4179  */
4180 
4181 /* Register mapping.
4182  *
4183  * First, be aware that our host bit size (64/32) *does* influence which
4184  * registers we can access -- i.e., ptrace on 64-bit host tracing a
4185  * 32-bit process still gets the 64-bit registers -- but even then, we
4186  * want the 32-bit mapping for DWARF reg num to i386 reg.
4187  *
4188  * Second, the mappings below are defined in sys/reg.h, but since the
4189  * macros there are defined according to compile-time __WORDSIZE, we
4190  * don't use them, and just encode the indexes manually.
4191  * regmapNN[x] = y provides, for DWARF register x, an offset y into the
4192  * register structs returned by ptrace.
4193  *
4194  * XXX XXX XXX
4195  * If structs in sys/user.h change, ever, these mappings will be wrong.
4196  * It is unfortunate that sys/user.h conditions the macros on __WORDSIZE.
4197  */
4198 #if __WORDSIZE == 64
4199 static int dreg_to_ptrace_idx64[ARCH_X86_64_REG_COUNT] = {
4200  10, 12, 11, 5, 13, 14, 4, 19,
4201  9, 8, 7, 6, 3, 2, 1, 0,
4202  16,
4203  -1, -1, -1, -1, -1, -1, -1, -1,
4204  -1, -1, -1, -1, -1, -1, -1, -1,
4205  -1, -1, -1, -1, -1, -1, -1, -1,
4206  -1, -1, -1, -1, -1, -1, -1, -1,
4207  18, 24, 17, 20, 23, 25, 26,
4208  -1, -1,
4209  21, 22,
4210  -1, -1,
4211  -1, -1, -1, -1, -1,
4212  -1,
4213  -1, -1, -1, -1,-1, -1, -1, -1,-1, -1,
4214  -1, -1,-1, -1, -1, -1,-1, -1, -1, -1,
4215  -1,
4216 };
4217 #endif
4218 static int dreg_to_ptrace_idx32[ARCH_X86_REG_COUNT] = {
4219  6, 1, 2, 0, 15, 5, 3, 4,
4220  12, 14,
4221  -1,
4222  -1, -1, -1, -1, -1, -1, -1, -1,
4223  -1, -1,
4224  -1, -1, -1, -1, -1, -1, -1, -1,
4225  -1, -1, -1, -1, -1, -1, -1, -1,
4226  -1,-1,-1,
4227  /* These are "fake" DWARF regs. */
4228  8, 13, 16, 7, 9, 10,
4229  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
4230  -1,
4231 };
4232 
4233 /*
4234  * Register functions.
4235  */
4236 REGVAL linux_userproc_read_reg(struct target *target,tid_t tid,REG reg) {
4237  int ptrace_idx;
4238  struct target_thread *tthread;
4239  struct linux_userproc_thread_state *tstate;
4240 
4241  tthread = linux_userproc_load_thread(target,tid,0);
4242  if (!tthread) {
4243  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4244  errno = EINVAL;
4245  return 0;
4246  }
4247  tstate = (struct linux_userproc_thread_state *)tthread->state;
4248 
4249  vdebug(5,LA_TARGET,LF_LUP,"reading reg %s\n",
4250  target_regname(target,reg));
4251 
4252  if (reg >= arch_regcount(target->arch)) {
4253  verror("regnum %d does not have a target mapping!\n",reg);
4254  errno = EINVAL;
4255  return 0;
4256  }
4257 
4258  if (target->arch->type == ARCH_X86_64) {
4259  ptrace_idx = dreg_to_ptrace_idx64[reg];
4260  return (REGVAL)(((unsigned long *)&(tstate->regs))[ptrace_idx]);
4261  }
4262  else {
4263  ptrace_idx = dreg_to_ptrace_idx32[reg];
4264  return (REGVAL)(((long int *)&(tstate->regs))[ptrace_idx]);
4265  }
4266 }
4267 
4268 int linux_userproc_write_reg(struct target *target,tid_t tid,REG reg,
4269  REGVAL value) {
4270  int ptrace_idx;
4271  struct target_thread *tthread;
4272  struct linux_userproc_thread_state *tstate;
4273 
4274  tthread = linux_userproc_load_thread(target,tid,0);
4275  if (!tthread) {
4276  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4277  errno = EINVAL;
4278  return 0;
4279  }
4280  tstate = (struct linux_userproc_thread_state *)tthread->state;
4281 
4282  vdebug(5,LA_TARGET,LF_LUP,"writing reg %s 0x%"PRIxREGVAL"\n",
4283  target_regname(target,reg),value);
4284 
4285  if (reg >= arch_regcount(target->arch)) {
4286  verror("regnum %d does not have a target mapping!\n",reg);
4287  errno = EINVAL;
4288  return 0;
4289  }
4290  if (target->arch->type == ARCH_X86_64) {
4291  ptrace_idx = dreg_to_ptrace_idx64[reg];
4292  ((unsigned long *)&(tstate->regs))[ptrace_idx] = (unsigned long)value;
4293  }
4294  else {
4295  ptrace_idx = dreg_to_ptrace_idx32[reg];
4296  ((long int*)&(tstate->regs))[ptrace_idx] = (long int)value;
4297  }
4298 
4299  /* Flush the registers in target_resume! */
4300  OBJSDIRTY(tthread);
4301 
4302  return 0;
4303 }
4304 
4305 /*
4306  * Hardware breakpoint support.
4307  */
4308 static REG linux_userproc_get_unused_debug_reg(struct target *target,tid_t tid) {
4309  REG retval = -1;
4310  struct target_thread *tthread;
4311  struct linux_userproc_thread_state *tstate;
4312 
4313  tthread = linux_userproc_load_thread(target,tid,0);
4314  if (!tthread) {
4315  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4316  errno = EINVAL;
4317  return 0;
4318  }
4319  tstate = (struct linux_userproc_thread_state *)tthread->state;
4320 
4321  if (!tstate->dr[0]) { retval = 0; }
4322  else if (!tstate->dr[1]) { retval = 1; }
4323  else if (!tstate->dr[2]) { retval = 2; }
4324  else if (!tstate->dr[3]) { retval = 3; }
4325 
4326  vdebug(5,LA_TARGET,LF_LUP,"returning unused debug reg %d\n",retval);
4327 
4328  return retval;
4329 }
4330 
4331 #define VWORDBYTESIZE __WORDSIZE / 8
4332 
4333 #if __WORDSIZE == 64
4334 static int read_ptrace_debug_reg(int pid,unsigned long *array) {
4335 #else
4336 static int read_ptrace_debug_reg(int pid,int *array) {
4337 #endif
4338  int i = 0;
4339 
4340  errno = 0;
4341  for ( ; i < 8; ++i) {
4342 #if __WORDSIZE == 64
4343  array[i] = \
4344  (unsigned long)ptrace(PTRACE_PEEKUSER,pid,
4345  offsetof(struct user,u_debugreg[i]),NULL);
4346 #else
4347  array[i] = \
4348  (int)ptrace(PTRACE_PEEKUSER,pid,
4349  offsetof(struct user,u_debugreg[i]),NULL);
4350 #endif
4351  if (errno) {
4352  verror("ptrace(PEEKUSER): %s\n",strerror(errno));
4353  return -1;
4354  }
4355  }
4356 
4357  return 0;
4358 }
4359 
4360 /*
4361  * struct x86_dr_format {
4362  * int dr0_l:1;
4363  * int dr0_g:1;
4364  * int dr1_l:1;
4365  * int dr1_g:1;
4366  * int dr2_l:1;
4367  * int dr2_g:1;
4368  * int dr3_l:1;
4369  * int dr3_g:1;
4370  * int exact_l:1;
4371  * int exact_g:1;
4372  * int reserved:6;
4373  * probepoint_whence_t dr0_break:2;
4374  * probepoint_watchsize_t dr0_len:2;
4375  * probepoint_whence_t dr1_break:2;
4376  * probepoint_watchsize_t dr1_len:2;
4377  * probepoint_whence_t dr2_break:2;
4378  * probepoint_watchsize_t dr2_len:2;
4379  * probepoint_whence_t dr3_break:2;
4380  * probepoint_watchsize_t dr3_len:2;
4381  * };
4382  */
4383 
4384 static int linux_userproc_set_hw_breakpoint(struct target *target,tid_t tid,
4385  REG reg,ADDR addr) {
4386 #if __WORDSIZE == 64
4387  unsigned long cdr;
4388 #else
4389  int cdr = 0;
4390 #endif
4391  struct target_thread *tthread;
4392  struct linux_userproc_thread_state *tstate;
4393 
4394  tthread = linux_userproc_load_thread(target,tid,0);
4395  if (!tthread) {
4396  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4397  errno = EINVAL;
4398  return 0;
4399  }
4400  tstate = (struct linux_userproc_thread_state *)tthread->state;
4401 
4402  if (reg < 0 || reg > 3) {
4403  errno = EINVAL;
4404  return -1;
4405  }
4406 
4407  errno = 0;
4408  ptrace(PTRACE_PEEKUSER,tid,
4409  offsetof(struct user,u_debugreg[reg]),(void *)&cdr);
4410  if (errno) {
4411  vwarn("could not read current val of debug reg %"PRIiREG": %s!\n",
4412  reg,strerror(errno));
4413  }
4414  else if (cdr != 0) {
4415  vwarn("debug reg %"PRIiREG" already has an address, overwriting (0x%"PRIxADDR")!\n",
4416  reg,cdr);
4417  //errno = EBUSY;
4418  //return -1;
4419  }
4420 
4421  /* Set the address, then the control bits. */
4422  tstate->dr[reg] = addr;
4423 
4424  /* Clear the status bits */
4425  tstate->dr[6] = 0; //&= ~(1 << reg);
4426 
4427  /* Set the local control bit, and unset the global bit. */
4428  tstate->dr[7] |= (1 << (reg * 2));
4429  tstate->dr[7] &= ~(1 << (reg * 2 + 1));
4430  /* Set the break to be on execution (00b). */
4431  tstate->dr[7] &= ~(3 << (16 + (reg * 4)));
4432 
4433  /*
4434  if (reg == 0) {
4435  dr7->dr0_l = 1;
4436  dr7->dr0_g = 0;
4437  dr7->dr0_break = PROBEPOINT_EXEC;
4438  dr7->dr0_len = 0;
4439  }
4440  */
4441 
4442  /* Now write these values! */
4443  errno = 0;
4444  ptrace(PTRACE_POKEUSER,tid,
4445  offsetof(struct user,u_debugreg[reg]),(void *)(tstate->dr[reg]));
4446  if (errno) {
4447  verror("could not update debug reg %"PRIiREG", aborting: %s!\n",
4448  reg,strerror(errno));
4449  goto errout;
4450  }
4451 
4452  ptrace(PTRACE_POKEUSER,tid,
4453  offsetof(struct user,u_debugreg[6]),(void *)(tstate->dr[6]));
4454  if (errno) {
4455  verror("could not update status debug reg, aborting: %s!\n",
4456  strerror(errno));
4457  goto errout;
4458  }
4459  ptrace(PTRACE_POKEUSER,tid,
4460  offsetof(struct user,u_debugreg[7]),(void *)(tstate->dr[7]));
4461  if (errno) {
4462  verror("could not update control debug reg, aborting: %s!\n",
4463  strerror(errno));
4464  goto errout;
4465  }
4466 
4467  return 0;
4468 
4469  errout:
4470  tstate->dr[reg] = 0;
4471 
4472  return -1;
4473 }
4474 
4475  static int linux_userproc_set_hw_watchpoint(struct target *target,tid_t tid,
4476  REG reg,ADDR addr,
4477  probepoint_whence_t whence,
4478  probepoint_watchsize_t watchsize) {
4479 #if __WORDSIZE == 64
4480  unsigned long cdr;
4481 #else
4482  int cdr;
4483 #endif
4484  struct target_thread *tthread;
4485  struct linux_userproc_thread_state *tstate;
4486 
4487  tthread = linux_userproc_load_thread(target,tid,0);
4488  if (!tthread) {
4489  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4490  errno = EINVAL;
4491  return 0;
4492  }
4493  tstate = (struct linux_userproc_thread_state *)tthread->state;
4494 
4495  if (reg < 0 || reg > 3) {
4496  errno = EINVAL;
4497  return -1;
4498  }
4499 
4500  errno = 0;
4501  ptrace(PTRACE_PEEKUSER,tid,
4502  offsetof(struct user,u_debugreg[reg]),(void *)&cdr);
4503  if (errno) {
4504  vwarn("could not read current val of debug reg %"PRIiREG"!\n",reg);
4505  }
4506  else if (cdr != 0) {
4507  vwarn("debug reg %"PRIiREG" already has an address, overwriting (0x%"PRIxADDR")!\n",
4508  reg,cdr);
4509  //errno = EBUSY;
4510  //return -1;
4511  }
4512 
4513  /* Set the address, then the control bits. */
4514  tstate->dr[reg] = addr;
4515 
4516  /* Clear the status bits */
4517  tstate->dr[6] = 0; //&= ~(1 << reg);
4518 
4519  /* Set the local control bit, and unset the global bit. */
4520  tstate->dr[7] |= (1 << (reg * 2));
4521  tstate->dr[7] &= ~(1 << (reg * 2 + 1));
4522  /* Set the break to be on whatever whence was) (clear the bits first!). */
4523  tstate->dr[7] &= ~(3 << (16 + (reg * 4)));
4524  tstate->dr[7] |= (whence << (16 + (reg * 4)));
4525  /* Set the watchsize to be whatever watchsize was). */
4526  tstate->dr[7] &= ~(3 << (18 + (reg * 4)));
4527  tstate->dr[7] |= (watchsize << (18 + (reg * 4)));
4528 
4529  /* Enable the LE bit to slow the processor! */
4530  tstate->dr[7] |= (1 << 8);
4531  /* Enable the GE bit to slow the processor! */
4532  /* tstate->dr[7] |= (1 << 9); */
4533 
4534  vdebug(4,LA_TARGET,LF_LUP,"dreg6 = 0x%"PRIxADDR"; dreg7 = 0x%"PRIxADDR", w = %d, ws = 0x%x\n",
4535  tstate->dr[6],tstate->dr[7],whence,watchsize);
4536 
4537  /* Now write these values! */
4538  errno = 0;
4539  ptrace(PTRACE_POKEUSER,tid,
4540  offsetof(struct user,u_debugreg[reg]),(void *)(tstate->dr[reg]));
4541  if (errno) {
4542  verror("could not update debug reg %"PRIiREG" (%p), aborting: %s!\n",reg,
4543  (void *)(tstate->dr[reg]),strerror(errno));
4544  goto errout;
4545  }
4546 
4547  ptrace(PTRACE_POKEUSER,tid,
4548  offsetof(struct user,u_debugreg[6]),(void *)(tstate->dr[6]));
4549  if (errno) {
4550  verror("could not update status debug reg (%p), aborting: %s!\n",
4551  (void *)(tstate->dr[6]),strerror(errno));
4552  goto errout;
4553  }
4554  ptrace(PTRACE_POKEUSER,tid,
4555  offsetof(struct user,u_debugreg[7]),(void *)(tstate->dr[7]));
4556  if (errno) {
4557  verror("could not update control debug reg (%p), aborting: %s!\n",
4558  (void *)(tstate->dr[7]),strerror(errno));
4559  goto errout;
4560  }
4561 
4562  return 0;
4563 
4564  errout:
4565  tstate->dr[reg] = 0;
4566 
4567  return -1;
4568 }
4569 
4570 static int linux_userproc_unset_hw_breakpoint(struct target *target,tid_t tid,
4571  REG reg) {
4572  struct target_thread *tthread;
4573  struct linux_userproc_thread_state *tstate;
4574 
4575  tthread = linux_userproc_load_thread(target,tid,0);
4576  if (!tthread) {
4577  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4578  errno = EINVAL;
4579  return 0;
4580  }
4581  tstate = (struct linux_userproc_thread_state *)tthread->state;
4582 
4583  if (reg < 0 || reg > 3) {
4584  errno = EINVAL;
4585  return -1;
4586  }
4587 
4588  /* Set the address, then the control bits. */
4589  tstate->dr[reg] = 0;
4590 
4591  /* Clear the status bits */
4592  tstate->dr[6] = 0; //&= ~(1 << reg);
4593 
4594  /* Unset the local control bit, and unset the global bit. */
4595  tstate->dr[7] &= ~(3 << (reg * 2));
4596 
4597  errno = 0;
4598  /* Now write these values! */
4599  ptrace(PTRACE_POKEUSER,tid,
4600  offsetof(struct user,u_debugreg[reg]),(void *)(tstate->dr[reg]));
4601  if (errno) {
4602  verror("could not update debug reg %"PRIiREG", aborting: %s!\n",
4603  reg,strerror(errno));
4604  goto errout;
4605  }
4606 
4607  ptrace(PTRACE_POKEUSER,tid,
4608  offsetof(struct user,u_debugreg[6]),(void *)(tstate->dr[6]));
4609  if (errno) {
4610  verror("could not update status debug reg, aborting: %s!\n",
4611  strerror(errno));
4612  goto errout;
4613  }
4614  ptrace(PTRACE_POKEUSER,tid,
4615  offsetof(struct user,u_debugreg[7]),(void *)(tstate->dr[7]));
4616  if (errno) {
4617  verror("could not update control debug reg,aborting: %s!\n",
4618  strerror(errno));
4619  goto errout;
4620  }
4621 
4622  return 0;
4623 
4624  errout:
4625  return -1;
4626 }
4627 
4628 static int linux_userproc_unset_hw_watchpoint(struct target *target,tid_t tid,
4629  REG reg) {
4630  /* It's the exact same thing, yay! */
4631  return linux_userproc_unset_hw_breakpoint(target,tid,reg);
4632 }
4633 
4634 int linux_userproc_disable_hw_breakpoints(struct target *target,tid_t tid) {
4635  ptrace(PTRACE_POKEUSER,tid,
4636  offsetof(struct user,u_debugreg[7]),(void *)0);
4637  if (errno) {
4638  verror("could not update control debug reg, aborting: %s!\n",
4639  strerror(errno));
4640  return -1;
4641  }
4642  return 0;
4643 }
4644 
4645 int linux_userproc_enable_hw_breakpoints(struct target *target,tid_t tid) {
4646  struct target_thread *tthread;
4647  struct linux_userproc_thread_state *tstate;
4648 
4649  tthread = linux_userproc_load_thread(target,tid,0);
4650  if (!tthread) {
4651  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4652  errno = EINVAL;
4653  return 0;
4654  }
4655  tstate = (struct linux_userproc_thread_state *)tthread->state;
4656 
4657  ptrace(PTRACE_POKEUSER,tid,
4658  offsetof(struct user,u_debugreg[7]),(void *)tstate->dr[7]);
4659  if (errno) {
4660  verror("could not update control debug reg, aborting: %s!\n",
4661  strerror(errno));
4662  return -1;
4663  }
4664  return 0;
4665 }
4666 
4667 int linux_userproc_disable_hw_breakpoint(struct target *target,tid_t tid,
4668  REG dreg) {
4669  struct target_thread *tthread;
4670  struct linux_userproc_thread_state *tstate;
4671 
4672  tthread = linux_userproc_load_thread(target,tid,0);
4673  if (!tthread) {
4674  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4675  errno = EINVAL;
4676  return 0;
4677  }
4678  tstate = (struct linux_userproc_thread_state *)tthread->state;
4679 
4680  if (dreg < 0 || dreg > 3) {
4681  errno = EINVAL;
4682  return -1;
4683  }
4684 
4685  /* Clear the status bits */
4686  tstate->dr[6] = 0; //&= ~(1 << reg);
4687 
4688  /* Unset the local control bit, and unset the global bit. */
4689  tstate->dr[7] &= ~(3 << (dreg * 2));
4690 
4691  errno = 0;
4692  /* Now write these values! */
4693  ptrace(PTRACE_POKEUSER,tid,
4694  offsetof(struct user,u_debugreg[6]),(void *)(tstate->dr[6]));
4695  if (errno) {
4696  verror("could not update status debug reg, aborting: %s!\n",
4697  strerror(errno));
4698  goto errout;
4699  }
4700  ptrace(PTRACE_POKEUSER,tid,
4701  offsetof(struct user,u_debugreg[7]),(void *)(tstate->dr[7]));
4702  if (errno) {
4703  verror("could not update control debug reg,aborting: %s!\n",
4704  strerror(errno));
4705  goto errout;
4706  }
4707 
4708  return 0;
4709 
4710  errout:
4711  return -1;
4712 }
4713 
4714 int linux_userproc_enable_hw_breakpoint(struct target *target,tid_t tid,
4715  REG dreg) {
4716  struct target_thread *tthread;
4717  struct linux_userproc_thread_state *tstate;
4718 
4719  tthread = linux_userproc_load_thread(target,tid,0);
4720  if (!tthread) {
4721  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4722  errno = EINVAL;
4723  return 0;
4724  }
4725  tstate = (struct linux_userproc_thread_state *)tthread->state;
4726 
4727  if (dreg < 0 || dreg > 3) {
4728  errno = EINVAL;
4729  return -1;
4730  }
4731 
4732  /* Clear the status bits */
4733  tstate->dr[6] = 0; //&= ~(1 << reg);
4734 
4735  /* Set the local control bit, and unset the global bit. */
4736  tstate->dr[7] |= (1 << (dreg * 2));
4737  tstate->dr[7] &= ~(1 << (dreg * 2 + 1));
4738 
4739  /* Now write these values! */
4740  errno = 0;
4741  ptrace(PTRACE_POKEUSER,tid,
4742  offsetof(struct user,u_debugreg[6]),(void *)(tstate->dr[6]));
4743  if (errno) {
4744  verror("could not update status debug reg, aborting: %s!\n",
4745  strerror(errno));
4746  goto errout;
4747  }
4748  ptrace(PTRACE_POKEUSER,tid,
4749  offsetof(struct user,u_debugreg[7]),(void *)(tstate->dr[7]));
4750  if (errno) {
4751  verror("could not update control debug reg, aborting: %s!\n",
4752  strerror(errno));
4753  goto errout;
4754  }
4755 
4756  return 0;
4757 
4758  errout:
4759  return -1;
4760 }
4761 
4762 int linux_userproc_notify_sw_breakpoint(struct target *target,ADDR addr,
4763  int notification) {
4764  return 0;
4765 }
4766 
4767 int linux_userproc_singlestep(struct target *target,tid_t tid,int isbp,
4768  struct target *overlay) {
4769  struct target_thread *tthread;
4770  struct linux_userproc_thread_state *tstate;
4771 
4772  tthread = linux_userproc_load_thread(target,tid,0);
4773  if (!tthread) {
4774  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4775  errno = EINVAL;
4776  return -1;
4777  }
4778  tstate = (struct linux_userproc_thread_state *)tthread->state;
4779 
4780  /* Clear the status bits */
4781  tstate->dr[6] = 0; //&= ~(1 << reg);
4782 
4783  /* Now write these values! */
4784  errno = 0;
4785  ptrace(PTRACE_POKEUSER,tid,
4786  offsetof(struct user,u_debugreg[6]),(void *)(tstate->dr[6]));
4787  if (errno) {
4788  verror("could not update status debug reg, aborting: %s!\n",
4789  strerror(errno));
4790  return -1;
4791  }
4792 
4793  /*
4794  * If this is a single step of an instruction for which a breakpoint
4795  * is set, set the RF flag. Why? Because then we don't have to
4796  * disable the hw breakpoint at this instruction if there is one.
4797  * The x86 clears it after one instruction anyway, so it's safe.
4798  *
4799  * Actually (and leaving this in so nobody else tries it), with
4800  * ptrace, we can't set the RF flag... it's masked out.
4801  */
4802  /*
4803  if (isbp) {
4804  flagsregno = linux_userproc_dw_reg_no(target,CREG_FLAGS);
4805  flags = linux_userproc_read_reg(target,tid,flagsregno);
4806  flags |= EF_RF;
4807  if (linux_userproc_write_reg(target,tid,flagsregno,flags))
4808  verror("could not set RF flag to single step breakpoint'd instr!\n");
4809  }
4810  */
4811 
4812  if (linux_userproc_flush_thread(target,tid) < 0) {
4813  verror("could not flush thread; not single stepping!\n");
4814  return -1;
4815  }
4816 
4817  ptrace(PTRACE_SINGLESTEP,tid,NULL,NULL);
4818  if (errno) {
4819  verror("could not ptrace single step thread %"PRIiTID": %s\n",
4820  tid,strerror(errno));
4821  return -1;
4822  }
4823 
4824  /*
4825  * PTRACE_SINGLESTEP runs the thread right away, so we have make
4826  * sure to do all the things _resume() would have done to it.
4827  */
4828  target_invalidate_thread(target,tthread);
4830 
4831  return 0;
4832 }
4833 
4834 int linux_userproc_singlestep_end(struct target *target,tid_t tid,
4835  struct target *overlay) {
4836  struct target_thread *tthread;
4837  struct linux_userproc_thread_state *tstate;
4838 
4839  tthread = linux_userproc_load_thread(target,tid,0);
4840  if (!tthread) {
4841  verror("thread %"PRIiTID" does not exist; forgot to load?\n",tid);
4842  errno = EINVAL;
4843  return 0;
4844  }
4845  tstate = (struct linux_userproc_thread_state *)tthread->state;
4846 
4847  /* Clear the status bits */
4848  tstate->dr[6] = 0; //&= ~(1 << reg);
4849 
4850  /* Now write these values! */
4851  errno = 0;
4852  ptrace(PTRACE_POKEUSER,tid,
4853  offsetof(struct user,u_debugreg[6]),(void *)(tstate->dr[6]));
4854  if (errno) {
4855  verror("could not update status debug reg, aborting: %s!\n",
4856  strerror(errno));
4857  return -1;
4858  }
4859 
4860  //target_thread_set_status(tthread,THREAD_STATUS_PAUSED);
4861 
4862  return 0;
4863 }
arch_type_t type
Definition: arch.h:117
#define OBJSCLEAN(obj)
Definition: object.h:116
#define OBJMOD(obj)
Definition: object.h:92
#define OBJNEW(obj)
Definition: object.h:88
int infd
Definition: target_api.h:2573
int waitpipe_add(int pid)
Definition: waitpipe.c:184
unsigned long target_generic_fd_write(int fd, ADDR addr, unsigned long length, unsigned char *buf)
int target_flush_all_threads(struct target *target)
Definition: target_api.c:1275
int addrspace_detach_region(struct addrspace *space, struct memregion *region)
Definition: memory.c:89
int linux_userproc_detach_evloop(struct target *target)
ADDR base_virt_addr
Definition: binfile.h:286
#define EVLOOP_HRET_REMOVEALLTYPES
Definition: evloop.h:38
#define vwarnopt(level, area, flags, format,...)
Definition: log.h:37
error_t linux_userproc_argp_parse_opt(int key, char *arg, struct argp_state *state)
#define LUP_SC_PRCTL
#define PRIiOFFSET
Definition: common.h:70
void * state
Definition: target_api.h:2488
char * linux_userproc_argp_header
#define INITIAL_PTRACE_OPTS
int waitpipe_init_auto(void(*alt_handler)(int, siginfo_t *, void *))
Definition: waitpipe.c:140
void target_broadcast_event(struct target *target, struct target_event *event)
Definition: target_event.c:56
void * backend_spec
Definition: target_api.h:2252
int evloop_unset_fd(struct evloop *evloop, int fd, int fdtype)
Definition: evloop.c:165
struct debugfile * debugfile_from_file(char *filename, char *root_prefix, struct array_list *debugfile_load_opts_list)
Definition: debug.c:1584
int32_t tid_t
Definition: common.h:36
#define OBJSVALID(obj)
Definition: object.h:101
#define LUP_SC_MMAP2
target_status_t
Definition: target_api.h:197
int waitpipe_get(int readfd)
Definition: waitpipe.c:310
struct target_event * target_create_event_2(struct target *target, struct target_thread *thread, target_event_t type, void *priv, void *priv2)
Definition: target_event.c:47
result_t probepoint_ss_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: probe.c:3021
int linux_userproc_singlestep_end(struct target *target, tid_t tid, struct target *overlay)
int addrspace_find_range_real(struct addrspace *space, ADDR addr, struct memregion **region_saveptr, struct memrange **range_saveptr)
Definition: memory.c:141
int linux_userproc_at_exit(struct target *target, tid_t tid)
int linux_userproc_disable_hw_breakpoints(struct target *target, tid_t tid)
#define PROT_SHARED
Definition: common.h:109
struct linux_userproc_spec * linux_userproc_build_spec(void)
void * p
GHashTable * soft_probepoints
Definition: target_api.h:2695
#define PRIiREG
Definition: common.h:94
#define OBJSNEW(obj)
Definition: object.h:133
probepoint_state_t state
Definition: probe.h:220
Definition: log.h:171
evloop_handler_t in_evh
Definition: target_api.h:2244
int linux_userproc_enable_hw_breakpoint(struct target *target, tid_t tid, REG dreg)
int memregion_detach_range(struct memregion *region, struct memrange *range)
Definition: memory.c:293
int target_resume(struct target *target)
Definition: target_api.c:973
ADDR end
Definition: target.h:984
#define v_g_list_foreach(glhead, glcur, elm)
Definition: glib_wrapper.h:34
int waitpipe_is_initialized(void)
Definition: waitpipe.c:117
probepoint_whence_t
Definition: probe_api.h:234
#define OBJSLIVE(obj, type)
Definition: object.h:121
target_debug_bp_handler_t handle_break
Definition: target_api.h:2829
evloop_handler_t out_evh
Definition: target_api.h:2245
struct target_thread * global_thread
Definition: target_api.h:2645
struct target * target_create(char *type, struct target_spec *spec)
Definition: target.c:1849
char * name
Definition: target.h:928
int target_flush_thread(struct target *target, tid_t tid)
Definition: target_api.c:1269
evloop_handler_t err_evh
Definition: target_api.h:2246
int target_associate_debugfile(struct target *target, struct memregion *region, struct debugfile *debugfile)
Definition: target.c:1963
ADDR start
Definition: target.h:983
struct target_ops linux_userspace_process_ops
#define verror(format,...)
Definition: log.h:30
#define LUP_SC_SET_THREAD_AREA
int target_find_memory_real(struct target *target, ADDR addr, struct addrspace **space_saveptr, struct memregion **region_saveptr, struct memrange **range_saveptr)
Definition: target.c:3536
void linux_userproc_free_spec(struct linux_userproc_spec *lspec)
int linux_userproc_spec_to_argv(struct target_spec *spec, int *argc, char ***argv)
void target_detach_thread(struct target *target, struct target_thread *tthread)
Definition: target.c:4073
int waitpipe_get_pid(int readfd)
Definition: waitpipe.c:330
Definition: evloop.h:66
#define OBJSINVALID(obj)
Definition: object.h:106
struct memregion * memregion_create(struct addrspace *space, region_type_t type, char *name)
Definition: memory.c:242
int target_invalidate_thread(struct target *target, struct target_thread *tthread)
Definition: target.c:4366
ADDR base_phys_addr
Definition: target.h:966
thread_status_t status
Definition: target_api.h:2047
#define vwarn(format,...)
Definition: log.h:33
uint32_t threadctl
Definition: target_api.h:2427
struct evloop * evloop
Definition: target_api.h:2598
#define RF
uint8_t is_dynamic
Definition: binfile.h:202
result_t probepoint_bp_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint, int was_stepping)
Definition: probe.c:2593
int linux_userproc_enable_hw_breakpoints(struct target *target, tid_t tid)
#define LUP_SC_MUNMAP
#define LUP_SC_ARCH_PRCTL
#define OBJLIVE(obj)
Definition: object.h:84
enum __ptrace_request ptrace_type
struct argp_option linux_userproc_argp_opts[]
struct arch * arch
Definition: binfile.h:216
int outfd
Definition: target_api.h:2574
struct memrange * memrange_create(struct memregion *region, ADDR start, ADDR end, OFFSET offset, unsigned int prot_flags)
Definition: memory.c:538
struct target_thread * current_thread
Definition: target_api.h:2640
struct binfile * binfile_open__int(char *filename, char *root_prefix, struct binfile_instance *bfinst)
Definition: binfile.c:254
#define OBJDIRTY(obj)
Definition: object.h:80
int linux_userproc_attach_thread(struct target *target, tid_t parent, tid_t child)
struct memregion * addrspace_match_region_start(struct addrspace *space, region_type_t rtype, ADDR start)
Definition: memory.c:126
region_type_t
struct array_list * target_list_tids(struct target *target)
Definition: target_api.c:1145
int linux_userproc_last_status(struct target *target, tid_t tid)
int waitpipe_remove(int pid)
Definition: waitpipe.c:284
probepoint_watchsize_t
Definition: probe_api.h:241
struct addrspace * space
Definition: target.h:926
#define THREAD_SPECIFIC_STATUS(status)
Definition: target_api.h:284
struct target * linux_userproc_instantiate(struct target_spec *spec, struct evloop *evloop)
struct target_location_ctxt * global_tlctxt
Definition: target_api.h:2667
uint32_t live
Definition: target_api.h:2427
const char * target_regname(struct target *target, REG reg)
Definition: target_api.c:1065
struct memregion * addrspace_match_region_name(struct addrspace *space, region_type_t rtype, char *name)
Definition: memory.c:111
GHashTable * threads
Definition: target_api.h:2632
struct memrange * memregion_match_range(struct memregion *region, ADDR start)
Definition: memory.c:315
#define LUP_SC_MPROTECT
int linux_userproc_notify_sw_breakpoint(struct target *target, ADDR addr, int notification)
struct target_spec * spec
Definition: target_api.h:2258
#define PROT_EXEC
Definition: common.h:108
#define RHOLD(x, hx)
Definition: common.h:622
#define PROT_WRITE
Definition: common.h:107
#define EVLOOP_FDTYPE_A
Definition: evloop.h:29
target_poll_outcome_t
Definition: target_api.h:394
target_type_t target_type
Definition: target_api.h:2166
GList * spaces
Definition: target_api.h:2603
unsigned char * target_generic_fd_read(int fd, ADDR addr, unsigned long length, unsigned char *buf)
struct binfile * binfile
Definition: target_api.h:2609
int linux_userproc_detach_thread(struct target *target, tid_t tid, int detaching_all, int stay_paused)
#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 evloop_set_fd(struct evloop *evloop, int fd, int fdtype, evloop_handler_t handler, void *state)
Definition: evloop.c:48
struct thread_probepoint_context * tpc
Definition: target_api.h:2131
#define OBJSMOD(obj)
Definition: object.h:137
#define EVLOOP_HRET_ERROR
Definition: evloop.h:35
Definition: log.h:172
int linux_userproc_singlestep(struct target *target, tid_t tid, int isbp, struct target *overlay)
#define OBJVALID(obj)
Definition: object.h:76
#define vdebugc(devel, areas, flags, format,...)
Definition: log.h:303
int __poll_and_handle_detaching(struct target *target, struct target_thread *tthread)
struct arch * arch
Definition: target_api.h:2563
#define OBJSDIRTY(obj)
Definition: object.h:111
ADDR offset
Definition: target.h:985
unsigned int prot_flags
Definition: target.h:986
result_t probepoint_resumeat_handler(struct target *target, struct target_thread *tthread)
Definition: probe.c:3502
#define ARCH_X86_64_REG_COUNT
Definition: arch_x86_64.h:33
int unlink(const char *pathname)
Definition: qemuhacks.c:132
struct array_list * debugfile_load_opts_list
Definition: target_api.h:2199
#define ARCH_X86_REG_COUNT
Definition: arch_x86.h:34
struct binfile * binfile
Definition: dwdebug.h:808
void target_thread_set_status(struct target_thread *tthread, thread_status_t status)
Definition: target.c:3999
struct target * target
Definition: target_api.h:2041
#define REGION_TYPE(n)
Definition: target_api.h:384
Definition: log.h:70
GList * ranges
Definition: target.h:936
uint32_t REGVAL
Definition: common.h:66
int ptrace_reg_t
#define EVLOOP_HRET_SUCCESS
Definition: evloop.h:36
int target_invalidate_all_threads(struct target *target)
Definition: target.c:4426
#define PRIiTID
Definition: common.h:37
int linux_userproc_last_signo(struct target *target, tid_t tid)
struct binfile * binfile_pointing
Definition: dwdebug.h:830
void target_reuse_thread_as_global(struct target *target, struct target_thread *thread)
Definition: target.c:4063
int8_t REG
Definition: common.h:93
#define LUP_SC_MMAP
uint32_t opened
Definition: target_api.h:2427
struct target_location_ctxt * target_location_ctxt_create(struct target *target, tid_t tid, struct memregion *region)
Definition: target.c:5248
int target_finalize(struct target *target)
Definition: target.c:1925
unsigned int breakpoint_instrs_len
Definition: arch.h:150
GHashTable * hard_probepoints
Definition: target_api.h:2080
uint32_t ADDR
Definition: common.h:64
struct target_ops * ops
Definition: target_api.h:2510
char * infile
Definition: target_api.h:2248
target_exception_flags_t
Definition: target_api.h:386
char * name
Definition: target.h:887
REG spregno
Definition: target_api.h:2469
int linux_userproc_pid(struct target *target)
thread_status_t
Definition: target_api.h:254
target_status_t status
Definition: target_api.h:2465
#define EVLOOP_FDTYPE_W
Definition: evloop.h:31
int linux_userproc_disable_hw_breakpoint(struct target *target, tid_t tid, REG dreg)
#define PROT_READ
Definition: common.h:106
REG fbregno
Definition: target_api.h:2468
uint32_t REFCNT
Definition: common.h:124
int waitpipe_drain(int pid)
Definition: waitpipe.c:350
#define PRIxADDR
Definition: common.h:67
#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
int(* snprintf)(struct target *target, char *buf, int bufsiz)
Definition: target_api.h:2758
void target_tid_set_status(struct target *target, tid_t tid, thread_status_t status)
Definition: target.c:4007
struct target_spec * spec
Definition: target_api.h:2565
int linux_userproc_at_syscall(struct target *target, tid_t tid)
#define EVLOOP_HRET_BADERROR
Definition: evloop.h:34
struct user_regs_struct regs
uint32_t nodisablehwbponss
Definition: target_api.h:2427
#define RPUT(x, objtype, hx, rc)
Definition: common.h:624
ADDR base_virt_addr
Definition: target.h:967
char * debugfile_root_prefix
Definition: target_api.h:2197
int linux_userproc_evloop_handler(int readfd, int fdtype, void *state)
#define DRF
region_type_t type
Definition: target.h:929
struct target_thread * target_lookup_thread(struct target *target, tid_t tid)
Definition: target.c:3981
void target_set_status(struct target *target, target_status_t status)
Definition: target.c:3993
ADDR base_load_addr
Definition: target.h:957
struct target_event * target_create_event(struct target *target, struct target_thread *thread, target_event_t type, void *priv)
Definition: target_event.c:26
char * errfile
Definition: target_api.h:2250
struct target_thread * blocking_thread
Definition: target_api.h:2653
target_debug_handler_t handle_step
Definition: target_api.h:2830
#define array_list_foreach_fakeptr_t(alist, lpc, placeholder, intertype)
Definition: alist.h:381
int linux_userproc_attach_evloop(struct target *target, struct evloop *evloop)
Definition: log.h:177
struct memregion * addrspace_find_region(struct addrspace *space, char *name)
Definition: memory.c:69
char * outfile
Definition: target_api.h:2249
ADDR base_phys_addr
Definition: binfile.h:285
struct argp linux_userproc_argp
struct binfile * binfile
Definition: target.h:950
struct target_thread * target_create_thread(struct target *target, tid_t tid, void *tstate, void *tpstate)
Definition: target.c:4021
int errfd
Definition: target_api.h:2575
REG ipregno
Definition: target_api.h:2470
int target_write_reg(struct target *target, tid_t tid, REG reg, REGVAL value)
Definition: target_api.c:1092
#define TID_GLOBAL
Definition: target_api.h:145
GList * regions
Definition: target.h:898
struct probepoint * probepoint
Definition: probe.h:207
#define OBJSDEAD(obj, type)
Definition: object.h:127
#define PRIxREGVAL
Definition: common.h:72
struct addrspace * addrspace_create(struct target *target, char *name, ADDR tag)
Definition: memory.c:41
uint32_t writeable
Definition: target_api.h:2427
target_type_t supported_overlay_types
Definition: target_api.h:2048
int linux_userproc_at_exec(struct target *target, tid_t tid)