1 /* Get Dwarf Frame state for target live PID process.
2 Copyright (C) 2013, 2014, 2015, 2018 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/>. */
37 #include <sys/types.h>
46 #include <sys/ptrace.h>
47 #include <sys/syscall.h>
51 linux_proc_pid_is_stopped (pid_t pid)
55 bool retval, have_state;
57 snprintf (buffer, sizeof (buffer), "/proc/%ld/status", (long) pid);
58 procfile = fopen (buffer, "r");
63 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
64 if (startswith (buffer, "State:"))
69 retval = (have_state && strstr (buffer, "T (stopped)") != NULL);
76 __libdwfl_ptrace_attach (pid_t tid, bool *tid_was_stoppedp)
78 if (ptrace (PTRACE_ATTACH, tid, NULL, NULL) != 0)
80 __libdwfl_seterrno (DWFL_E_ERRNO);
83 *tid_was_stoppedp = linux_proc_pid_is_stopped (tid);
84 if (*tid_was_stoppedp)
86 /* Make sure there is a SIGSTOP signal pending even when the process is
87 already State: T (stopped). Older kernels might fail to generate
88 a SIGSTOP notification in that case in response to our PTRACE_ATTACH
89 above. Which would make the waitpid below wait forever. So emulate
90 it. Since there can only be one SIGSTOP notification pending this is
91 safe. See also gdb/linux-nat.c linux_nat_post_attach_wait. */
92 syscall (__NR_tkill, tid, SIGSTOP);
93 ptrace (PTRACE_CONT, tid, NULL, NULL);
98 if (waitpid (tid, &status, __WALL) != tid || !WIFSTOPPED (status))
100 int saved_errno = errno;
101 ptrace (PTRACE_DETACH, tid, NULL, NULL);
103 __libdwfl_seterrno (DWFL_E_ERRNO);
106 if (WSTOPSIG (status) == SIGSTOP)
108 if (ptrace (PTRACE_CONT, tid, NULL,
109 (void *) (uintptr_t) WSTOPSIG (status)) != 0)
111 int saved_errno = errno;
112 ptrace (PTRACE_DETACH, tid, NULL, NULL);
114 __libdwfl_seterrno (DWFL_E_ERRNO);
121 #ifdef HAVE_PROCESS_VM_READV
122 /* Note that the result word size depends on the architecture word size.
123 That is sizeof long. */
125 read_cached_memory (struct __libdwfl_pid_arg *pid_arg,
126 Dwarf_Addr addr, Dwarf_Word *result)
128 /* Let the ptrace fallback deal with the corner case of the address
129 possibly crossing a page boundary. */
130 if ((addr & ((Dwarf_Addr)__LIBDWFL_REMOTE_MEM_CACHE_SIZE - 1))
131 > (Dwarf_Addr)__LIBDWFL_REMOTE_MEM_CACHE_SIZE - sizeof (unsigned long))
134 struct __libdwfl_remote_mem_cache *mem_cache = pid_arg->mem_cache;
135 if (mem_cache == NULL)
137 size_t mem_cache_size = sizeof (struct __libdwfl_remote_mem_cache);
138 mem_cache = malloc (mem_cache_size);
139 if (mem_cache == NULL)
144 pid_arg->mem_cache = mem_cache;
148 if (addr >= mem_cache->addr && addr - mem_cache->addr < mem_cache->len)
150 d = &mem_cache->buf[addr - mem_cache->addr];
151 if ((((uintptr_t) d) & (sizeof (unsigned long) - 1)) == 0)
152 *result = *(unsigned long *) d;
154 memcpy (result, d, sizeof (unsigned long));
158 struct iovec local, remote;
159 mem_cache->addr = addr & ~((Dwarf_Addr)__LIBDWFL_REMOTE_MEM_CACHE_SIZE - 1);
160 local.iov_base = mem_cache->buf;
161 local.iov_len = __LIBDWFL_REMOTE_MEM_CACHE_SIZE;
162 remote.iov_base = (void *) (uintptr_t) mem_cache->addr;
163 remote.iov_len = __LIBDWFL_REMOTE_MEM_CACHE_SIZE;
165 ssize_t res = process_vm_readv (pid_arg->tid_attached,
166 &local, 1, &remote, 1, 0);
167 if (res != __LIBDWFL_REMOTE_MEM_CACHE_SIZE)
173 mem_cache->len = res;
174 d = &mem_cache->buf[addr - mem_cache->addr];
175 if ((((uintptr_t) d) & (sizeof (unsigned long) - 1)) == 0)
176 *result = *(unsigned long *) d;
178 memcpy (result, d, sizeof (unsigned long));
181 #endif /* HAVE_PROCESS_VM_READV */
184 clear_cached_memory (struct __libdwfl_pid_arg *pid_arg)
186 struct __libdwfl_remote_mem_cache *mem_cache = pid_arg->mem_cache;
187 if (mem_cache != NULL)
191 /* Note that the result word size depends on the architecture word size.
192 That is sizeof long. */
194 pid_memory_read (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Word *result, void *arg)
196 struct __libdwfl_pid_arg *pid_arg = arg;
197 pid_t tid = pid_arg->tid_attached;
198 Dwfl_Process *process = dwfl->process;
201 #ifdef HAVE_PROCESS_VM_READV
202 if (read_cached_memory (pid_arg, addr, result))
205 # if BYTE_ORDER == BIG_ENDIAN
206 if (ebl_get_elfclass (process->ebl) == ELFCLASS32)
214 if (ebl_get_elfclass (process->ebl) == ELFCLASS64)
218 *result = ptrace (PTRACE_PEEKDATA, tid, (void *) (uintptr_t) addr, NULL);
220 #else /* SIZEOF_LONG != 8 */
221 /* This should not happen. */
223 #endif /* SIZEOF_LONG != 8 */
226 /* We do not care about reads unaliged to 4 bytes boundary.
227 But 0x...ffc read of 8 bytes could overrun a page. */
228 bool lowered = (addr & 4) != 0;
231 #endif /* SIZEOF_LONG == 8 */
233 *result = ptrace (PTRACE_PEEKDATA, tid, (void *) (uintptr_t) addr, NULL);
237 # if BYTE_ORDER == BIG_ENDIAN
244 #endif /* SIZEOF_LONG == 8 */
245 *result &= 0xffffffff;
250 pid_next_thread (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg,
253 struct __libdwfl_pid_arg *pid_arg = dwfl_arg;
254 struct dirent *dirent;
255 /* Start fresh on first traversal. */
256 if (*thread_argp == NULL)
257 rewinddir (pid_arg->dir);
261 dirent = readdir (pid_arg->dir);
266 __libdwfl_seterrno (DWFL_E_ERRNO);
272 while (strcmp (dirent->d_name, ".") == 0
273 || strcmp (dirent->d_name, "..") == 0);
276 long tidl = strtol (dirent->d_name, &end, 10);
279 __libdwfl_seterrno (DWFL_E_ERRNO);
283 if (tidl <= 0 || (end && *end) || tid != tidl)
285 __libdwfl_seterrno (DWFL_E_PARSE_PROC);
288 *thread_argp = dwfl_arg;
292 /* Just checks that the thread id exists. */
294 pid_getthread (Dwfl *dwfl __attribute__ ((unused)), pid_t tid,
295 void *dwfl_arg, void **thread_argp)
297 *thread_argp = dwfl_arg;
298 if (kill (tid, 0) < 0)
300 __libdwfl_seterrno (DWFL_E_ERRNO);
306 /* Implement the ebl_set_initial_registers_tid setfunc callback. */
309 pid_thread_state_registers_cb (int firstreg, unsigned nregs,
310 const Dwarf_Word *regs, void *arg)
312 Dwfl_Thread *thread = (Dwfl_Thread *) arg;
315 assert (firstreg == -1);
317 INTUSE(dwfl_thread_state_register_pc) (thread, *regs);
321 return INTUSE(dwfl_thread_state_registers) (thread, firstreg, nregs, regs);
325 pid_set_initial_registers (Dwfl_Thread *thread, void *thread_arg)
327 struct __libdwfl_pid_arg *pid_arg = thread_arg;
328 assert (pid_arg->tid_attached == 0);
329 pid_t tid = INTUSE(dwfl_thread_tid) (thread);
330 if (! pid_arg->assume_ptrace_stopped
331 && ! __libdwfl_ptrace_attach (tid, &pid_arg->tid_was_stopped))
333 pid_arg->tid_attached = tid;
334 Dwfl_Process *process = thread->process;
335 Ebl *ebl = process->ebl;
336 return ebl_set_initial_registers_tid (ebl, tid,
337 pid_thread_state_registers_cb, thread);
341 pid_detach (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg)
343 struct __libdwfl_pid_arg *pid_arg = dwfl_arg;
344 elf_end (pid_arg->elf);
345 free (pid_arg->mem_cache);
346 close (pid_arg->elf_fd);
347 closedir (pid_arg->dir);
353 __libdwfl_ptrace_detach (pid_t tid, bool tid_was_stopped)
355 /* This handling is needed only on older Linux kernels such as
356 2.6.32-358.23.2.el6.ppc64. Later kernels such as
357 3.11.7-200.fc19.x86_64 remember the T (stopped) state
358 themselves and no longer need to pass SIGSTOP during
360 ptrace (PTRACE_DETACH, tid, NULL,
361 (void *) (intptr_t) (tid_was_stopped ? SIGSTOP : 0));
365 pid_thread_detach (Dwfl_Thread *thread, void *thread_arg)
367 struct __libdwfl_pid_arg *pid_arg = thread_arg;
368 pid_t tid = INTUSE(dwfl_thread_tid) (thread);
369 assert (pid_arg->tid_attached == tid);
370 pid_arg->tid_attached = 0;
371 clear_cached_memory (pid_arg);
372 if (! pid_arg->assume_ptrace_stopped)
373 __libdwfl_ptrace_detach (tid, pid_arg->tid_was_stopped);
376 static const Dwfl_Thread_Callbacks pid_thread_callbacks =
381 pid_set_initial_registers,
387 dwfl_linux_proc_attach (Dwfl *dwfl, pid_t pid, bool assume_ptrace_stopped)
391 int err = 0; /* The errno to return and set for dwfl->attcherr. */
393 /* Make sure to report the actual PID (thread group leader) to
394 dwfl_attach_state. */
395 snprintf (buffer, sizeof (buffer), "/proc/%ld/status", (long) pid);
396 procfile = fopen (buffer, "r");
397 if (procfile == NULL)
401 if (dwfl->process == NULL && dwfl->attacherr == DWFL_E_NOERROR)
404 dwfl->attacherr = __libdwfl_canon_error (DWFL_E_ERRNO);
411 while (getline (&line, &linelen, procfile) >= 0)
412 if (startswith (line, "Tgid:"))
416 long val = strtol (&line[5], &endptr, 10);
417 if ((errno == ERANGE && val == LONG_MAX)
418 || *endptr != '\n' || val < 0 || val != (pid_t) val)
434 int i = snprintf (name, sizeof (name), "/proc/%ld/task", (long) pid);
435 if (i <= 0 || i >= (ssize_t) sizeof (name) - 1)
440 DIR *dir = opendir (name);
448 i = snprintf (name, sizeof (name), "/proc/%ld/exe", (long) pid);
449 assert (i > 0 && i < (ssize_t) sizeof (name) - 1);
450 int elf_fd = open (name, O_RDONLY);
453 elf = elf_begin (elf_fd, ELF_C_READ_MMAP, NULL);
456 /* Just ignore, dwfl_attach_state will fall back to trying
457 to associate the Dwfl with one of the existing DWfl_Module
458 ELF images (to know the machine/class backend to use). */
465 struct __libdwfl_pid_arg *pid_arg = malloc (sizeof *pid_arg);
476 pid_arg->elf_fd = elf_fd;
477 pid_arg->mem_cache = NULL;
478 pid_arg->tid_attached = 0;
479 pid_arg->assume_ptrace_stopped = assume_ptrace_stopped;
480 if (! INTUSE(dwfl_attach_state) (dwfl, elf, pid, &pid_thread_callbacks,
491 INTDEF (dwfl_linux_proc_attach)
493 struct __libdwfl_pid_arg *
495 __libdwfl_get_pid_arg (Dwfl *dwfl)
497 if (dwfl != NULL && dwfl->process != NULL
498 && dwfl->process->callbacks == &pid_thread_callbacks)
499 return (struct __libdwfl_pid_arg *) dwfl->process->callbacks_arg;
504 #else /* __linux__ */
508 __libdwfl_ptrace_attach (pid_t tid __attribute__ ((unused)),
509 bool *tid_was_stoppedp __attribute__ ((unused)))
512 __libdwfl_seterrno (DWFL_E_ERRNO);
518 __libdwfl_ptrace_detach (pid_t tid __attribute__ ((unused)),
519 bool tid_was_stopped __attribute__ ((unused)))
524 dwfl_linux_proc_attach (Dwfl *dwfl __attribute__ ((unused)),
525 pid_t pid __attribute__ ((unused)),
526 bool assume_ptrace_stopped __attribute__ ((unused)))
530 INTDEF (dwfl_linux_proc_attach)
532 struct __libdwfl_pid_arg *
534 __libdwfl_get_pid_arg (Dwfl *dwfl __attribute__ ((unused)))
539 #endif /* ! __linux __ */