1 /* Get Dwarf Frame state for target PID or core file.
2 Copyright (C) 2013, 2014 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/>. */
30 #include <sys/ptrace.h>
33 /* Set STATE->pc_set from STATE->regs according to the backend. Return true on
34 success, false on error. */
36 state_fetch_pc (Dwfl_Frame *state)
38 switch (state->pc_state)
40 case DWFL_FRAME_STATE_PC_SET:
42 case DWFL_FRAME_STATE_PC_UNDEFINED:
44 case DWFL_FRAME_STATE_ERROR:
46 Ebl *ebl = state->thread->process->ebl;
48 if (ebl_abi_cfi (ebl, &abi_info) != 0)
50 __libdwfl_seterrno (DWFL_E_LIBEBL);
53 unsigned ra = abi_info.return_address_register;
54 /* dwarf_frame_state_reg_is_set is not applied here. */
55 if (ra >= ebl_frame_nregs (ebl))
57 __libdwfl_seterrno (DWFL_E_LIBEBL_BAD);
60 state->pc = state->regs[ra] + ebl_ra_offset (ebl);
61 state->pc_state = DWFL_FRAME_STATE_PC_SET;
68 /* Do not call it on your own, to be used by thread_* functions only. */
71 state_free (Dwfl_Frame *state)
73 Dwfl_Thread *thread = state->thread;
74 assert (thread->unwound == state);
75 thread->unwound = state->unwound;
80 thread_free_all_states (Dwfl_Thread *thread)
82 while (thread->unwound)
83 state_free (thread->unwound);
87 state_alloc (Dwfl_Thread *thread)
89 assert (thread->unwound == NULL);
90 Ebl *ebl = thread->process->ebl;
91 size_t nregs = ebl_frame_nregs (ebl);
94 assert (nregs < sizeof (((Dwfl_Frame *) NULL)->regs_set) * 8);
95 Dwfl_Frame *state = malloc (sizeof (*state) + sizeof (*state->regs) * nregs);
98 state->thread = thread;
99 state->signal_frame = false;
100 state->initial_frame = true;
101 state->pc_state = DWFL_FRAME_STATE_ERROR;
102 memset (state->regs_set, 0, sizeof (state->regs_set));
103 thread->unwound = state;
104 state->unwound = NULL;
110 __libdwfl_process_free (Dwfl_Process *process)
112 Dwfl *dwfl = process->dwfl;
113 if (process->callbacks->detach != NULL)
114 process->callbacks->detach (dwfl, process->callbacks_arg);
115 assert (dwfl->process == process);
116 dwfl->process = NULL;
117 if (process->ebl_close)
118 ebl_closebackend (process->ebl);
120 dwfl->attacherr = DWFL_E_NOERROR;
123 /* Allocate new Dwfl_Process for DWFL. */
125 process_alloc (Dwfl *dwfl)
127 Dwfl_Process *process = malloc (sizeof (*process));
130 process->dwfl = dwfl;
131 dwfl->process = process;
135 dwfl_attach_state (Dwfl *dwfl, Elf *elf, pid_t pid,
136 const Dwfl_Thread_Callbacks *thread_callbacks, void *arg)
138 if (dwfl->process != NULL)
140 __libdwfl_seterrno (DWFL_E_ATTACH_STATE_CONFLICT);
144 /* Reset any previous error, we are just going to try again. */
145 dwfl->attacherr = DWFL_E_NOERROR;
146 /* thread_callbacks is declared NN */
147 if (thread_callbacks->next_thread == NULL
148 || thread_callbacks->set_initial_registers == NULL)
150 dwfl->attacherr = DWFL_E_INVALID_ARGUMENT;
152 dwfl->attacherr = __libdwfl_canon_error (dwfl->attacherr);
153 __libdwfl_seterrno (dwfl->attacherr);
161 ebl = ebl_openbackend (elf);
167 for (Dwfl_Module *mod = dwfl->modulelist; mod != NULL; mod = mod->next)
169 /* Reading of the vDSO or (deleted) modules may fail as
170 /proc/PID/mem is unreadable without PTRACE_ATTACH and
171 we may not be PTRACE_ATTACH-ed now. MOD would not be
172 re-read later to unwind it when we are already
173 PTRACE_ATTACH-ed to PID. This happens when this function
174 is called from dwfl_linux_proc_attach with elf == NULL.
175 __libdwfl_module_getebl will call __libdwfl_getelf which
176 will call the find_elf callback. */
177 if (strncmp (mod->name, "[vdso: ", 7) == 0
178 || strcmp (strrchr (mod->name, ' ') ?: "",
181 Dwfl_Error error = __libdwfl_module_getebl (mod);
182 if (error != DWFL_E_NOERROR)
191 /* Not identified EBL from any of the modules. */
192 dwfl->attacherr = DWFL_E_PROCESS_NO_ARCH;
195 process_alloc (dwfl);
196 Dwfl_Process *process = dwfl->process;
200 ebl_closebackend (ebl);
201 dwfl->attacherr = DWFL_E_NOMEM;
205 process->ebl_close = ebl_close;
207 process->callbacks = thread_callbacks;
208 process->callbacks_arg = arg;
211 INTDEF(dwfl_attach_state)
214 dwfl_pid (Dwfl *dwfl)
216 if (dwfl->attacherr != DWFL_E_NOERROR)
218 __libdwfl_seterrno (dwfl->attacherr);
222 if (dwfl->process == NULL)
224 __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
227 return dwfl->process->pid;
232 dwfl_thread_dwfl (Dwfl_Thread *thread)
234 return thread->process->dwfl;
236 INTDEF(dwfl_thread_dwfl)
239 dwfl_thread_tid (Dwfl_Thread *thread)
243 INTDEF(dwfl_thread_tid)
246 dwfl_frame_thread (Dwfl_Frame *state)
248 return state->thread;
250 INTDEF(dwfl_frame_thread)
253 dwfl_getthreads (Dwfl *dwfl, int (*callback) (Dwfl_Thread *thread, void *arg),
256 if (dwfl->attacherr != DWFL_E_NOERROR)
258 __libdwfl_seterrno (dwfl->attacherr);
262 Dwfl_Process *process = dwfl->process;
265 __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
270 thread.process = process;
271 thread.unwound = NULL;
272 thread.callbacks_arg = NULL;
275 thread.tid = process->callbacks->next_thread (dwfl,
276 process->callbacks_arg,
277 &thread.callbacks_arg);
280 Dwfl_Error saved_errno = dwfl_errno ();
281 thread_free_all_states (&thread);
282 __libdwfl_seterrno (saved_errno);
287 thread_free_all_states (&thread);
288 __libdwfl_seterrno (DWFL_E_NOERROR);
291 int err = callback (&thread, arg);
292 if (err != DWARF_CB_OK)
294 thread_free_all_states (&thread);
297 assert (thread.unwound == NULL);
301 INTDEF(dwfl_getthreads)
307 int (*callback) (Dwfl_Thread *thread, void *arg);
313 get_one_thread_cb (Dwfl_Thread *thread, void *arg)
315 struct one_arg *oa = (struct one_arg *) arg;
316 if (! oa->seen && INTUSE(dwfl_thread_tid) (thread) == oa->tid)
319 oa->ret = oa->callback (thread, oa->arg);
320 return DWARF_CB_ABORT;
326 /* Note not currently exported, will be when there are more Dwfl_Thread
327 properties to query. Use dwfl_getthread_frames for now directly. */
329 getthread (Dwfl *dwfl, pid_t tid,
330 int (*callback) (Dwfl_Thread *thread, void *arg),
333 if (dwfl->attacherr != DWFL_E_NOERROR)
335 __libdwfl_seterrno (dwfl->attacherr);
339 Dwfl_Process *process = dwfl->process;
342 __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
346 if (process->callbacks->get_thread != NULL)
349 thread.process = process;
350 thread.unwound = NULL;
351 thread.callbacks_arg = NULL;
353 if (process->callbacks->get_thread (dwfl, tid, process->callbacks_arg,
354 &thread.callbacks_arg))
358 err = callback (&thread, arg);
359 thread_free_all_states (&thread);
366 struct one_arg oa = { .tid = tid, .callback = callback,
367 .arg = arg, .seen = false };
368 int err = INTUSE(dwfl_getthreads) (dwfl, get_one_thread_cb, &oa);
370 if (err == DWARF_CB_ABORT && oa.seen)
373 if (err == DWARF_CB_OK && ! oa.seen)
376 __libdwfl_seterrno (DWFL_E_ERRNO);
385 int (*callback) (Dwfl_Frame *frame, void *arg);
390 get_one_thread_frames_cb (Dwfl_Thread *thread, void *arg)
392 struct one_thread *ot = (struct one_thread *) arg;
393 return INTUSE(dwfl_thread_getframes) (thread, ot->callback, ot->arg);
397 dwfl_getthread_frames (Dwfl *dwfl, pid_t tid,
398 int (*callback) (Dwfl_Frame *frame, void *arg),
401 struct one_thread ot = { .callback = callback, .arg = arg };
402 return getthread (dwfl, tid, get_one_thread_frames_cb, &ot);
404 INTDEF(dwfl_getthread_frames)
407 dwfl_thread_getframes (Dwfl_Thread *thread,
408 int (*callback) (Dwfl_Frame *state, void *arg),
411 if (thread->unwound != NULL)
413 /* We had to be called from inside CALLBACK. */
414 __libdwfl_seterrno (DWFL_E_ATTACH_STATE_CONFLICT);
417 Ebl *ebl = thread->process->ebl;
418 if (ebl_frame_nregs (ebl) == 0)
420 __libdwfl_seterrno (DWFL_E_NO_UNWIND);
423 if (state_alloc (thread) == NULL)
425 __libdwfl_seterrno (DWFL_E_NOMEM);
428 Dwfl_Process *process = thread->process;
429 if (! process->callbacks->set_initial_registers (thread,
430 thread->callbacks_arg))
432 thread_free_all_states (thread);
435 if (! state_fetch_pc (thread->unwound))
437 if (process->callbacks->thread_detach)
438 process->callbacks->thread_detach (thread, thread->callbacks_arg);
439 thread_free_all_states (thread);
446 state = thread->unwound;
447 int err = callback (state, arg);
448 if (err != DWARF_CB_OK)
450 if (process->callbacks->thread_detach)
451 process->callbacks->thread_detach (thread, thread->callbacks_arg);
452 thread_free_all_states (thread);
455 __libdwfl_frame_unwind (state);
456 /* The old frame is no longer needed. */
457 state_free (thread->unwound);
458 state = thread->unwound;
460 while (state && state->pc_state == DWFL_FRAME_STATE_PC_SET);
462 Dwfl_Error err = dwfl_errno ();
463 if (process->callbacks->thread_detach)
464 process->callbacks->thread_detach (thread, thread->callbacks_arg);
465 if (state == NULL || state->pc_state == DWFL_FRAME_STATE_ERROR)
467 thread_free_all_states (thread);
468 __libdwfl_seterrno (err);
471 assert (state->pc_state == DWFL_FRAME_STATE_PC_UNDEFINED);
472 thread_free_all_states (thread);
475 INTDEF(dwfl_thread_getframes)