1 /* Get Dwarf Frame state for target live PID process.
2 Copyright (C) 2013, 2014, 2015 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
31 #include <sys/types.h>
34 #include <sys/ptrace.h>
37 #include <sys/syscall.h>
41 # define MAX(a, b) ((a) > (b) ? (a) : (b))
47 linux_proc_pid_is_stopped (pid_t pid)
51 bool retval, have_state;
53 snprintf (buffer, sizeof (buffer), "/proc/%ld/status", (long) pid);
54 procfile = fopen (buffer, "r");
59 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
60 if (strncmp (buffer, "State:", 6) == 0)
65 retval = (have_state && strstr (buffer, "T (stopped)") != NULL);
72 __libdwfl_ptrace_attach (pid_t tid, bool *tid_was_stoppedp)
74 if (ptrace (PTRACE_ATTACH, tid, NULL, NULL) != 0)
76 __libdwfl_seterrno (DWFL_E_ERRNO);
79 *tid_was_stoppedp = linux_proc_pid_is_stopped (tid);
80 if (*tid_was_stoppedp)
82 /* Make sure there is a SIGSTOP signal pending even when the process is
83 already State: T (stopped). Older kernels might fail to generate
84 a SIGSTOP notification in that case in response to our PTRACE_ATTACH
85 above. Which would make the waitpid below wait forever. So emulate
86 it. Since there can only be one SIGSTOP notification pending this is
87 safe. See also gdb/linux-nat.c linux_nat_post_attach_wait. */
88 syscall (__NR_tkill, tid, SIGSTOP);
89 ptrace (PTRACE_CONT, tid, NULL, NULL);
94 if (waitpid (tid, &status, __WALL) != tid || !WIFSTOPPED (status))
96 int saved_errno = errno;
97 ptrace (PTRACE_DETACH, tid, NULL, NULL);
99 __libdwfl_seterrno (DWFL_E_ERRNO);
102 if (WSTOPSIG (status) == SIGSTOP)
104 if (ptrace (PTRACE_CONT, tid, NULL,
105 (void *) (uintptr_t) WSTOPSIG (status)) != 0)
107 int saved_errno = errno;
108 ptrace (PTRACE_DETACH, tid, NULL, NULL);
110 __libdwfl_seterrno (DWFL_E_ERRNO);
118 pid_memory_read (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Word *result, void *arg)
120 struct __libdwfl_pid_arg *pid_arg = arg;
121 pid_t tid = pid_arg->tid_attached;
123 Dwfl_Process *process = dwfl->process;
124 if (ebl_get_elfclass (process->ebl) == ELFCLASS64)
128 *result = ptrace (PTRACE_PEEKDATA, tid, (void *) (uintptr_t) addr, NULL);
130 #else /* SIZEOF_LONG != 8 */
131 /* This should not happen. */
133 #endif /* SIZEOF_LONG != 8 */
136 /* We do not care about reads unaliged to 4 bytes boundary.
137 But 0x...ffc read of 8 bytes could overrun a page. */
138 bool lowered = (addr & 4) != 0;
141 #endif /* SIZEOF_LONG == 8 */
143 *result = ptrace (PTRACE_PEEKDATA, tid, (void *) (uintptr_t) addr, NULL);
147 # if BYTE_ORDER == BIG_ENDIAN
154 #endif /* SIZEOF_LONG == 8 */
155 *result &= 0xffffffff;
160 pid_next_thread (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg,
163 struct __libdwfl_pid_arg *pid_arg = dwfl_arg;
164 struct dirent *dirent;
165 /* Start fresh on first traversal. */
166 if (*thread_argp == NULL)
167 rewinddir (pid_arg->dir);
171 dirent = readdir (pid_arg->dir);
176 __libdwfl_seterrno (DWFL_E_ERRNO);
182 while (strcmp (dirent->d_name, ".") == 0
183 || strcmp (dirent->d_name, "..") == 0);
186 long tidl = strtol (dirent->d_name, &end, 10);
189 __libdwfl_seterrno (DWFL_E_ERRNO);
193 if (tidl <= 0 || (end && *end) || tid != tidl)
195 __libdwfl_seterrno (DWFL_E_PARSE_PROC);
198 *thread_argp = dwfl_arg;
202 /* Just checks that the thread id exists. */
204 pid_getthread (Dwfl *dwfl __attribute__ ((unused)), pid_t tid,
205 void *dwfl_arg, void **thread_argp)
207 *thread_argp = dwfl_arg;
208 if (kill (tid, 0) < 0)
210 __libdwfl_seterrno (DWFL_E_ERRNO);
216 /* Implement the ebl_set_initial_registers_tid setfunc callback. */
219 pid_thread_state_registers_cb (int firstreg, unsigned nregs,
220 const Dwarf_Word *regs, void *arg)
222 Dwfl_Thread *thread = (Dwfl_Thread *) arg;
225 assert (firstreg == -1);
227 INTUSE(dwfl_thread_state_register_pc) (thread, *regs);
231 return INTUSE(dwfl_thread_state_registers) (thread, firstreg, nregs, regs);
235 pid_set_initial_registers (Dwfl_Thread *thread, void *thread_arg)
237 struct __libdwfl_pid_arg *pid_arg = thread_arg;
238 assert (pid_arg->tid_attached == 0);
239 pid_t tid = INTUSE(dwfl_thread_tid) (thread);
240 if (! pid_arg->assume_ptrace_stopped
241 && ! __libdwfl_ptrace_attach (tid, &pid_arg->tid_was_stopped))
243 pid_arg->tid_attached = tid;
244 Dwfl_Process *process = thread->process;
245 Ebl *ebl = process->ebl;
246 return ebl_set_initial_registers_tid (ebl, tid,
247 pid_thread_state_registers_cb, thread);
251 pid_detach (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg)
253 struct __libdwfl_pid_arg *pid_arg = dwfl_arg;
254 elf_end (pid_arg->elf);
255 close (pid_arg->elf_fd);
256 closedir (pid_arg->dir);
262 __libdwfl_ptrace_detach (pid_t tid, bool tid_was_stopped)
264 /* This handling is needed only on older Linux kernels such as
265 2.6.32-358.23.2.el6.ppc64. Later kernels such as
266 3.11.7-200.fc19.x86_64 remember the T (stopped) state
267 themselves and no longer need to pass SIGSTOP during
269 ptrace (PTRACE_DETACH, tid, NULL,
270 (void *) (intptr_t) (tid_was_stopped ? SIGSTOP : 0));
274 pid_thread_detach (Dwfl_Thread *thread, void *thread_arg)
276 struct __libdwfl_pid_arg *pid_arg = thread_arg;
277 pid_t tid = INTUSE(dwfl_thread_tid) (thread);
278 assert (pid_arg->tid_attached == tid);
279 pid_arg->tid_attached = 0;
280 if (! pid_arg->assume_ptrace_stopped)
281 __libdwfl_ptrace_detach (tid, pid_arg->tid_was_stopped);
284 static const Dwfl_Thread_Callbacks pid_thread_callbacks =
289 pid_set_initial_registers,
295 dwfl_linux_proc_attach (Dwfl *dwfl, pid_t pid, bool assume_ptrace_stopped)
299 int err = 0; /* The errno to return and set for dwfl->attcherr. */
301 /* Make sure to report the actual PID (thread group leader) to
302 dwfl_attach_state. */
303 snprintf (buffer, sizeof (buffer), "/proc/%ld/status", (long) pid);
304 procfile = fopen (buffer, "r");
305 if (procfile == NULL)
309 if (dwfl->process == NULL && dwfl->attacherr == DWFL_E_NOERROR)
312 dwfl->attacherr = __libdwfl_canon_error (DWFL_E_ERRNO);
319 while (getline (&line, &linelen, procfile) >= 0)
320 if (strncmp (line, "Tgid:", 5) == 0)
324 long val = strtol (&line[5], &endptr, 10);
325 if ((errno == ERANGE && val == LONG_MAX)
326 || *endptr != '\n' || val < 0 || val != (pid_t) val)
342 int i = snprintf (name, sizeof (name), "/proc/%ld/task", (long) pid);
343 assert (i > 0 && i < (ssize_t) sizeof (name) - 1);
344 DIR *dir = opendir (name);
352 i = snprintf (name, sizeof (name), "/proc/%ld/exe", (long) pid);
353 assert (i > 0 && i < (ssize_t) sizeof (name) - 1);
354 int elf_fd = open (name, O_RDONLY);
357 elf = elf_begin (elf_fd, ELF_C_READ_MMAP, NULL);
360 /* Just ignore, dwfl_attach_state will fall back to trying
361 to associate the Dwfl with one of the existing DWfl_Module
362 ELF images (to know the machine/class backend to use). */
369 struct __libdwfl_pid_arg *pid_arg = malloc (sizeof *pid_arg);
380 pid_arg->elf_fd = elf_fd;
381 pid_arg->tid_attached = 0;
382 pid_arg->assume_ptrace_stopped = assume_ptrace_stopped;
383 if (! INTUSE(dwfl_attach_state) (dwfl, elf, pid, &pid_thread_callbacks,
394 INTDEF (dwfl_linux_proc_attach)
396 struct __libdwfl_pid_arg *
398 __libdwfl_get_pid_arg (Dwfl *dwfl)
400 if (dwfl != NULL && dwfl->process != NULL
401 && dwfl->process->callbacks == &pid_thread_callbacks)
402 return (struct __libdwfl_pid_arg *) dwfl->process->callbacks_arg;
407 #else /* __linux__ */
410 pid_next_thread (Dwfl *dwfl __attribute__ ((unused)),
411 void *dwfl_arg __attribute__ ((unused)),
412 void **thread_argp __attribute__ ((unused)))
415 __libdwfl_seterrno (DWFL_E_ERRNO);
420 pid_getthread (Dwfl *dwfl __attribute__ ((unused)),
421 pid_t tid __attribute__ ((unused)),
422 void *dwfl_arg __attribute__ ((unused)),
423 void **thread_argp __attribute__ ((unused)))
426 __libdwfl_seterrno (DWFL_E_ERRNO);
432 __libdwfl_ptrace_attach (pid_t tid __attribute__ ((unused)),
433 bool *tid_was_stoppedp __attribute__ ((unused)))
436 __libdwfl_seterrno (DWFL_E_ERRNO);
441 pid_memory_read (Dwfl *dwfl __attribute__ ((unused)),
442 Dwarf_Addr addr __attribute__ ((unused)),
443 Dwarf_Word *result __attribute__ ((unused)),
444 void *arg __attribute__ ((unused)))
447 __libdwfl_seterrno (DWFL_E_ERRNO);
452 pid_set_initial_registers (Dwfl_Thread *thread __attribute__ ((unused)),
453 void *thread_arg __attribute__ ((unused)))
456 __libdwfl_seterrno (DWFL_E_ERRNO);
461 pid_detach (Dwfl *dwfl __attribute__ ((unused)),
462 void *dwfl_arg __attribute__ ((unused)))
468 __libdwfl_ptrace_detach (pid_t tid __attribute__ ((unused)),
469 bool tid_was_stopped __attribute__ ((unused)))
474 pid_thread_detach (Dwfl_Thread *thread __attribute__ ((unused)),
475 void *thread_arg __attribute__ ((unused)))
479 static const Dwfl_Thread_Callbacks pid_thread_callbacks =
484 pid_set_initial_registers,
490 dwfl_linux_proc_attach (Dwfl *dwfl __attribute__ ((unused)),
491 pid_t pid __attribute__ ((unused)),
492 bool assume_ptrace_stopped __attribute__ ((unused)))
496 INTDEF (dwfl_linux_proc_attach)
498 struct __libdwfl_pid_arg *
500 __libdwfl_get_pid_arg (Dwfl *dwfl __attribute__ ((unused)))
505 #endif /* ! __linux __ */