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