1 /* Copyright 2007, Red Hat Inc. */
4 #define ps_prochandle attach_state_struct
12 #include <sys/ptrace.h>
13 #include <sys/types.h>
19 #include <linux/ptrace.h>
20 #include <asm/prctl.h>
21 #include <elfutils/libdwfl.h>
22 #include <thread_db.h>
29 #define USLEEP (1000000 / 2)
31 #define TIMEOUT_SECS 20
32 /* LOOPS_MIN is a safety as QEMU clock time sucks.
33 100000 is 4s natively and 53s in QEMU. */
34 #define LOOPS_MIN 500000
37 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
49 static __attribute__((__noreturn__)) void crash (void)
57 fputs (">>> CRASH START\n", stderr);
59 count = backtrace (array, ARRAY_SIZE (array));
60 backtrace_symbols_fd (array, count, STDERR_FILENO);
62 snprintf (command, sizeof (command), "echo -e \"bt\\nquit\""
63 " >/tmp/debugger.%d; gdb --batch -nx --command=/tmp/debugger.%d"
64 " /proc/%d/exe %d </dev/null;rm -f /tmp/debugger.%d",
65 (int) getpid(), (int) getpid(), (int) getpid(), (int) getpid(),
68 fputs (">>> CRASH FINISH\n", stderr);
74 /* Separate the valid `1 << enum state' and `enum state' ranges. */
87 static const char *state_to_name (enum state state)
91 case STATE_INSTABLE: return "STATE_INSTABLE";
92 case STATE_ENOENT: return "STATE_ENOENT";
93 case STATE_SLEEPING: return "STATE_SLEEPING";
94 case STATE_RUNNING: return "STATE_RUNNING";
95 case STATE_STOPPED: return "STATE_STOPPED";
96 case STATE_PTRACED: return "STATE_PTRACED";
97 case STATE_ZOMBIE: return "STATE_ZOMBIE";
98 case STATE_DEAD: return "STATE_DEAD";
105 static enum state state_get (pid_t pid)
107 char status_name[32];
114 snprintf (status_name, sizeof (status_name), "/proc/%d/status", (int) pid);
115 f = fopen (status_name, "r");
116 if (f == NULL && errno == ENOENT)
117 found = STATE_ENOENT;
120 fprintf (stderr, "errno = %d\n", errno);
127 found = STATE_INSTABLE;
128 while (errno = 0, fgets (line, sizeof (line), f) != NULL)
130 const char *const string = "State:\t";
131 const size_t length = sizeof "State:\t" - 1;
133 if (strncmp (line, string, length) != 0)
135 if (strcmp (line + length, "S (sleeping)\n") == 0)
136 found = STATE_SLEEPING;
137 else if (strcmp (line + length, "R (running)\n") == 0)
138 found = STATE_RUNNING;
139 else if (strcmp (line + length, "T (stopped)\n") == 0)
140 found = STATE_STOPPED;
141 else if (strcmp (line + length, "T (tracing stop)\n") == 0)
142 found = STATE_PTRACED;
143 else if (strcmp (line + length, "Z (zombie)\n") == 0)
144 found = STATE_ZOMBIE;
145 /* FIXME: What does it mean? */
146 else if (strcmp (line + length, "X (dead)\n") == 0)
150 fprintf (stderr, "Found an unknown state: %s", line + length);
154 assert (found != STATE_INSTABLE || errno == ESRCH);
161 #define STATE(pid, expect_mask) state ((pid), (expect_mask), #expect_mask )
163 static enum state state (pid_t pid, unsigned expect_mask, const char *expect_mask_string)
166 time_t timeout = time (NULL) + TIMEOUT_SECS;
169 /* Sanity check `1 << enum state' was not misplaced with `enum state'. */
170 assert (1 << (STATE_FIRST + 1) >= STATE_LAST);
171 assert (expect_mask != 0);
172 #define MASK_UNDER_EXCLUSIVE(bit) ((1 << (bit)) - 1)
173 #define MASK_ABOVE_INCLUSIVE(bit) (~MASK_UNDER_EXCLUSIVE (bit))
174 assert ((expect_mask & MASK_UNDER_EXCLUSIVE (STATE_FIRST + 1)) == 0);
175 assert ((expect_mask & MASK_ABOVE_INCLUSIVE (STATE_LAST)) == 0);
176 #undef MASK_ABOVE_INCLUSIVE
177 #undef MASK_UNDER_EXCLUSIVE
181 found = state_get (pid);
183 if (((1 << found) & expect_mask) != 0)
186 while (loops++ < LOOPS_MIN || time (NULL) < timeout);
188 fprintf (stderr, "Found for PID %d state %s but expecting (%s)\n",
189 (int) pid, state_to_name (found), expect_mask_string);
193 struct attach_state_struct
198 td_thragent_t *thread_agent;
199 /* Debugging only: Number of signal needing redelivery on PTRACE_ATTACH. */
204 int attach_state_redelivered_get (struct attach_state_struct *attach_state)
206 assert (attach_state != NULL);
208 return attach_state->redelivered;
211 int attach_state_threads_count_get (struct attach_state_struct *attach_state)
213 assert (attach_state != NULL);
215 return attach_state->threads_count;
218 int attach_state_stopped_get (struct attach_state_struct *attach_state)
220 assert (attach_state != NULL);
222 return attach_state->stopped;
225 pid_t attach_state_pid_get (struct attach_state_struct *attach_state)
227 assert (attach_state != NULL);
229 return attach_state->pid;
232 struct attach_state_struct *attach_single (pid_t pid)
236 struct attach_state_struct *attach_state;
238 attach_state = malloc (sizeof (*attach_state));
239 assert (attach_state != NULL);
240 attach_state->pid = pid;
241 attach_state->dwfl = NULL;
242 attach_state->redelivered = 0;
243 attach_state->threads_count = -1;
247 attach_state->stopped = (STATE (pid, (1 << STATE_SLEEPING)
248 | (1 << STATE_RUNNING)
249 | (1 << STATE_STOPPED))
252 i = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
255 /* FIXME: Why it does not work?
256 Disable also the STATE () call above. */
260 i = ptrace (PTRACE_CONT, pid, (void *) 1, (void *) SIGSTOP);
261 /* `STOPPED == 1' may be false, even if the process was not stopped. */
263 attach_state->stopped = 1;
264 else if (errno == ESRCH)
265 attach_state->stopped = 0;
274 i = waitpid (pid, &status, 0);
276 if (!WIFSTOPPED (status))
278 /* Process may have exited. */
279 fprintf (stderr, "PID %d waitpid(2) status %d\n", (int) pid,
283 if (WSTOPSIG (status) == SIGSTOP)
286 if (attach_state->redelivered == 0)
287 attach_state->redelivered = WSTOPSIG (status);
289 attach_state->redelivered = -1;
293 /* Re-deliver the signal received before SIGSTOP.
294 It happens with about only 1:200000 probability. */
295 i = ptrace (PTRACE_CONT, pid, (void *) 1,
296 (void *) (unsigned long) WSTOPSIG (status));
303 void detach_single (struct attach_state_struct *attach_state)
309 i = ptrace (PTRACE_DETACH, attach_state->pid, NULL,
310 (void *) (unsigned long) (attach_state->stopped ? SIGSTOP : 0));
316 static Dwfl *get_dwfl (struct ps_prochandle *proc_handle)
318 static char *debuginfo_path;
320 static const Dwfl_Callbacks proc_callbacks =
322 .find_debuginfo = dwfl_standard_find_debuginfo,
323 .debuginfo_path = &debuginfo_path,
325 .find_elf = dwfl_linux_proc_find_elf,
328 if (proc_handle->dwfl == NULL)
330 proc_handle->dwfl = dwfl_begin (&proc_callbacks);
331 assert (proc_handle->dwfl != NULL);
334 if (dwfl_linux_proc_report (proc_handle->dwfl, proc_handle->pid) != 0
335 || dwfl_report_end (proc_handle->dwfl, NULL, NULL) != 0)
337 fprintf (stderr, "dwfl reporting: %m\n");
341 return proc_handle->dwfl;
344 /* Functions in this interface return one of these status codes. */
347 PS_OK, /* Generic "call succeeded". */
348 PS_ERR, /* Generic error. */
349 PS_BADPID, /* Bad process handle. */
350 PS_BADLID, /* Bad LWP identifier. */
351 PS_BADADDR, /* Bad address. */
352 PS_NOSYM, /* Could not find given symbol. */
353 PS_NOFREGS /* FPU register set not available for given LWP. */
356 ps_err_e ps_pdread (struct ps_prochandle *proc_handle, psaddr_t addr,
357 void *buffer, size_t length)
359 pid_t pid = proc_handle->pid;
363 if (pid == getpid ())
365 memcpy (buffer, addr, length);
369 /* Already under PTRACE_ATTACH. */
371 if (ptrace (PTRACE_ATTACH, pid, NULL, NULL) != 0)
373 if (waitpid (pid, NULL, 0) != pid)
377 snprintf (filename, sizeof (filename), "/proc/%ld/mem", (long) pid);
378 fd = open (filename, O_RDONLY);
380 if (lseek64 (fd, (off64_t) addr, SEEK_SET) != (off64_t) addr)
382 if (read (fd, buffer, length) != length)
384 /* It occurs for singlethreaded processes. */
385 if ((off64_t) addr == 0)
387 fprintf (stderr, "read() error @0x%lx length %lu: %m\n",
388 (unsigned long) addr, (unsigned long) length);
394 /* Already under PTRACE_ATTACH. */
396 if (ptrace (PTRACE_DETACH, pid, NULL, NULL) != 0)
403 ps_err_e ps_pdwrite (struct ps_prochandle *proc_handle, psaddr_t addr,
404 const void *buffer, size_t length)
409 ps_err_e ps_lgetregs (struct ps_prochandle *proc_handle, lwpid_t lwp,
415 ps_err_e ps_lsetregs (struct ps_prochandle *proc_handle, lwpid_t lwp,
416 const prgregset_t regs)
421 ps_err_e ps_lgetfpregs (struct ps_prochandle *proc_handle, lwpid_t lwp,
422 prfpregset_t *fpregs)
427 ps_err_e ps_lsetfpregs (struct ps_prochandle *proc_handle, lwpid_t lwp,
428 const prfpregset_t *fpregs)
433 /* Return the PID of the process. */
434 pid_t ps_getpid (struct ps_prochandle *proc_handle)
436 return proc_handle->pid;
439 struct getmodules_callback_arg
441 const char *sym_name;
446 static int getmodules_callback (Dwfl_Module *module,
447 void **module_userdata_pointer,
448 const char *module_name,
449 Dwarf_Addr module_low_addr, void *arg)
451 struct getmodules_callback_arg *getmodules_callback_arg = arg;
455 sym_count = dwfl_module_getsymtab (module);
456 /* "/usr/lib/debug/lib64/ld-2.4.so.debug"? */
458 assert (sym_count >= 0);
460 for (ndx = 0; ndx < sym_count; ndx++)
462 const char *name_got;
464 name_got = dwfl_module_getsym (module, ndx, &sym,
466 assert (name_got != NULL);
467 if (strcmp (name_got, getmodules_callback_arg->sym_name) == 0)
470 if (ndx == sym_count)
473 *getmodules_callback_arg->sym_addr = (psaddr_t) sym.st_value;
474 getmodules_callback_arg->retval = PS_OK;
479 /* Look up the named symbol in the named DSO in the symbol tables
480 associated with the process being debugged, filling in *SYM_ADDR
481 with the corresponding run-time address. */
482 ps_err_e ps_pglobal_lookup (struct ps_prochandle *proc_handle,
483 const char *object_name, const char *sym_name,
486 Dwfl *dwfl = get_dwfl (proc_handle);
487 struct getmodules_callback_arg getmodules_callback_arg;
488 ptrdiff_t err_ptrdiff;
490 /* FIXME: `object_name' ignored due to missing unresolving of shared
491 libraries symbolic links. */
492 getmodules_callback_arg.sym_name = sym_name;
493 getmodules_callback_arg.sym_addr = sym_addr;
494 getmodules_callback_arg.retval = PS_NOSYM;
495 err_ptrdiff = dwfl_getmodules (dwfl, getmodules_callback,
496 &getmodules_callback_arg, 0);
497 assert (err_ptrdiff == 0);
498 return getmodules_callback_arg.retval;
502 ps_err_e ps_get_thread_area (const struct ps_prochandle *ph, lwpid_t lwpid,
503 int idx, void **base)
507 assert (idx == FS || idx == GS);
509 if (ptrace (PTRACE_ATTACH, lwpid, NULL, NULL) != 0)
511 if (waitpid (lwpid, NULL, 0) != lwpid)
514 val = ptrace (PTRACE_ARCH_PRCTL, lwpid, base, (idx == FS ? ARCH_GET_FS
517 if (ptrace (PTRACE_DETACH, lwpid, NULL, NULL) != 0)
522 fprintf (stderr, "PTRACE_ARCH_PRCTL (%s): %m\n", (idx == FS ? "FS"
530 #error "Unsupported ps_get_thread_area ()!"
533 static int find_new_threads_callback (const td_thrhandle_t *th_p, void *data)
537 struct attach_state_struct *attach_state = data;
539 err = td_thr_get_info (th_p, &info);
540 assert (err == TD_OK);
541 /* printf ("LWP = %ld TID = 0x%lx\n", (long) info.ti_lid, info.ti_tid); */
543 attach_state->threads_count++;
548 static void attach_multi (struct attach_state_struct *attach_state)
553 assert (err == TD_OK);
555 err = td_ta_new (attach_state, &attach_state->thread_agent);
556 assert (err == TD_OK || err == TD_NOLIBTHREAD);
557 attach_state->threads_count = 0;
558 if (err == TD_NOLIBTHREAD)
560 attach_state->thread_agent = NULL;
563 assert (attach_state->thread_agent != NULL);
564 /* Multithreading test: */
565 err = td_ta_thr_iter (attach_state->thread_agent, find_new_threads_callback,
566 attach_state, TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
567 TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
570 fprintf (stderr, "err = %d\n", err);
573 assert (attach_state->threads_count > 0);
576 struct attach_state_struct *attach (pid_t pid)
578 struct attach_state_struct *attach_state;
580 attach_state = attach_single (pid);
581 attach_multi (attach_state);
586 void detach (struct attach_state_struct *attach_state)
588 if (attach_state->thread_agent != NULL)
592 err = td_ta_delete (attach_state->thread_agent);
593 assert (err == TD_OK);
595 if (attach_state->dwfl != NULL);
596 dwfl_end (attach_state->dwfl);
598 detach_single (attach_state);
603 int main (int argc, char **argv)
606 struct attach_state_struct *attach_state;
610 fprintf (stderr, "Usage: %s <PID>\n", argv[0]);
614 pid = atoi (argv[1]);
616 attach_state = attach (pid);
617 detach (attach_state);
622 #endif /* !LIBRARY */