Branch data Line data Source code
1 : : /* Get Dwarf Frame state for target PID or core file.
2 : : Copyright (C) 2013, 2014, 2024-2025 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : :
5 : : This file is free software; you can redistribute it and/or modify
6 : : it under the terms of either
7 : :
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
11 : :
12 : : or
13 : :
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
17 : :
18 : : or both in parallel, as here.
19 : :
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.
24 : :
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/>. */
28 : :
29 : : #ifdef HAVE_CONFIG_H
30 : : # include <config.h>
31 : : #endif
32 : :
33 : : #include <system.h>
34 : :
35 : : #include "libdwflP.h"
36 : : #include "libdwfl_stacktraceP.h"
37 : :
38 : : /* Set STATE->pc_set from STATE->regs according to the backend. Return true on
39 : : success, false on error. */
40 : : static bool
41 : 44 : state_fetch_pc (Dwfl_Frame *state)
42 : : {
43 [ - + - + ]: 44 : switch (state->pc_state)
44 : : {
45 : : case DWFL_FRAME_STATE_PC_SET:
46 : : return true;
47 : 0 : case DWFL_FRAME_STATE_PC_UNDEFINED:
48 : 0 : abort ();
49 : 29 : case DWFL_FRAME_STATE_ERROR:
50 : : {
51 : 29 : Ebl *ebl = state->thread->process->ebl;
52 : 29 : Dwarf_CIE abi_info;
53 [ - + ]: 29 : if (ebl_abi_cfi (ebl, &abi_info) != 0)
54 : : {
55 : 0 : __libdwfl_seterrno (DWFL_E_LIBEBL);
56 : 0 : return false;
57 : : }
58 : 29 : unsigned ra = abi_info.return_address_register;
59 : : /* dwarf_frame_state_reg_is_set is not applied here. */
60 [ - + ]: 29 : if (ra >= ebl_frame_nregs (ebl))
61 : : {
62 : 0 : __libdwfl_seterrno (DWFL_E_LIBEBL_BAD);
63 : 0 : return false;
64 : : }
65 : 29 : state->pc = state->regs[ra] + ebl_ra_offset (ebl);
66 : 29 : state->pc_state = DWFL_FRAME_STATE_PC_SET;
67 : : }
68 : 29 : return true;
69 : : }
70 : 0 : abort ();
71 : : }
72 : :
73 : : /* Do not call it on your own, to be used by thread_* functions only. */
74 : :
75 : : static void
76 : 0 : free_states (Dwfl_Frame *state)
77 : : {
78 [ - - - - : 75 : while (state)
+ + - + +
+ ]
79 : : {
80 : 32 : Dwfl_Frame *next = state->unwound;
81 : 32 : free(state);
82 : 32 : state = next;
83 : : }
84 : : }
85 : :
86 : : static Dwfl_Frame *
87 : 44 : state_alloc (Dwfl_Thread *thread)
88 : : {
89 [ - + ]: 44 : assert (thread->unwound == NULL);
90 : 44 : Ebl *ebl = thread->process->ebl;
91 : 44 : size_t nregs = ebl_frame_nregs (ebl);
92 [ - + ]: 44 : if (nregs == 0)
93 : : return NULL;
94 [ - + ]: 44 : assert (nregs < sizeof (((Dwfl_Frame *) NULL)->regs_set) * 8);
95 : 44 : Dwfl_Frame *state = malloc (sizeof (*state) + sizeof (*state->regs) * nregs);
96 [ - + ]: 44 : if (state == NULL)
97 : : return NULL;
98 : 44 : state->thread = thread;
99 : 44 : state->signal_frame = false;
100 : 44 : state->initial_frame = true;
101 : 44 : state->pc_state = DWFL_FRAME_STATE_ERROR;
102 : 44 : state->unwound_source = DWFL_UNWOUND_INITIAL_FRAME;
103 : 44 : memset (state->regs_set, 0, sizeof (state->regs_set));
104 : 44 : thread->unwound = state;
105 : 44 : state->unwound = NULL;
106 : 44 : return state;
107 : : }
108 : :
109 : : void
110 : : internal_function
111 : 52 : __libdwfl_process_free (Dwfl_Process *process)
112 : : {
113 : 52 : Dwfl *dwfl = process->dwfl;
114 [ + + ]: 52 : if (process->callbacks->detach != NULL)
115 : 51 : process->callbacks->detach (dwfl, process->callbacks_arg);
116 [ - + ]: 52 : assert (dwfl->process == process);
117 : 52 : dwfl->process = NULL;
118 [ + + ]: 52 : if (process->ebl_close)
119 : 51 : ebl_closebackend (process->ebl);
120 : 52 : free (process);
121 : 52 : dwfl->attacherr = DWFL_E_NOERROR;
122 : 52 : }
123 : :
124 : : /* Allocate new Dwfl_Process for DWFL. */
125 : : static void
126 : 53 : process_alloc (Dwfl *dwfl)
127 : : {
128 : 53 : Dwfl_Process *process = malloc (sizeof (*process));
129 [ + - ]: 53 : if (process == NULL)
130 : : return;
131 : 53 : process->dwfl = dwfl;
132 : 53 : dwfl->process = process;
133 : : }
134 : :
135 : : bool
136 : 53 : dwfl_attach_state (Dwfl *dwfl, Elf *elf, pid_t pid,
137 : : const Dwfl_Thread_Callbacks *thread_callbacks, void *arg)
138 : : {
139 [ - + ]: 53 : if (dwfl->process != NULL)
140 : : {
141 : 0 : __libdwfl_seterrno (DWFL_E_ATTACH_STATE_CONFLICT);
142 : 0 : return false;
143 : : }
144 : :
145 : : /* Reset any previous error, we are just going to try again. */
146 : 53 : dwfl->attacherr = DWFL_E_NOERROR;
147 : : /* thread_callbacks is declared NN */
148 [ + - ]: 53 : if (thread_callbacks->next_thread == NULL
149 [ - + ]: 53 : || thread_callbacks->set_initial_registers == NULL)
150 : : {
151 : 0 : dwfl->attacherr = DWFL_E_INVALID_ARGUMENT;
152 : 0 : fail:
153 : 0 : dwfl->attacherr = __libdwfl_canon_error (dwfl->attacherr);
154 : 0 : __libdwfl_seterrno (dwfl->attacherr);
155 : 0 : return false;
156 : : }
157 : :
158 : 53 : Ebl *ebl;
159 : 53 : bool ebl_close;
160 [ + + ]: 53 : if (elf != NULL)
161 : : {
162 : 52 : ebl = ebl_openbackend (elf);
163 : 52 : ebl_close = true;
164 : : }
165 : : else
166 : : {
167 : 1 : ebl = NULL;
168 [ + - ]: 1 : for (Dwfl_Module *mod = dwfl->modulelist; mod != NULL; mod = mod->next)
169 : : {
170 : : /* Reading of the vDSO or (deleted) modules may fail as
171 : : /proc/PID/mem is unreadable without PTRACE_ATTACH and
172 : : we may not be PTRACE_ATTACH-ed now. MOD would not be
173 : : re-read later to unwind it when we are already
174 : : PTRACE_ATTACH-ed to PID. This happens when this function
175 : : is called from dwfl_linux_proc_attach with elf == NULL.
176 : : __libdwfl_module_getebl will call __libdwfl_getelf which
177 : : will call the find_elf callback. */
178 [ + - ]: 1 : if (startswith (mod->name, "[vdso: ")
179 [ + - - + ]: 2 : || strcmp (strrchr (mod->name, ' ') ?: "",
180 : : " (deleted)") == 0)
181 : 0 : continue;
182 : 1 : Dwfl_Error error = __libdwfl_module_getebl (mod);
183 [ - + ]: 1 : if (error != DWFL_E_NOERROR)
184 : 0 : continue;
185 : 1 : ebl = mod->ebl;
186 : 1 : break;
187 : : }
188 : : ebl_close = false;
189 : : }
190 [ - + ]: 53 : if (ebl == NULL)
191 : : {
192 : : /* Not identified EBL from any of the modules. */
193 : 0 : dwfl->attacherr = DWFL_E_PROCESS_NO_ARCH;
194 : 0 : goto fail;
195 : : }
196 : 53 : process_alloc (dwfl);
197 : 53 : Dwfl_Process *process = dwfl->process;
198 [ - + ]: 53 : if (process == NULL)
199 : : {
200 [ # # ]: 0 : if (ebl_close)
201 : 0 : ebl_closebackend (ebl);
202 : 0 : dwfl->attacherr = DWFL_E_NOMEM;
203 : 0 : goto fail;
204 : : }
205 : 53 : process->ebl = ebl;
206 : 53 : process->ebl_close = ebl_close;
207 : 53 : process->pid = pid;
208 : 53 : process->callbacks = thread_callbacks;
209 : 53 : process->callbacks_arg = arg;
210 : :
211 [ - + ]: 53 : if (dwfl->tracker != NULL)
212 : 0 : __libdwfl_stacktrace_add_dwfl_to_tracker (dwfl);
213 : :
214 : : return true;
215 : : }
216 : : INTDEF(dwfl_attach_state)
217 : :
218 : : pid_t
219 : 71 : dwfl_pid (Dwfl *dwfl)
220 : : {
221 [ - + ]: 71 : if (dwfl->attacherr != DWFL_E_NOERROR)
222 : : {
223 : 0 : __libdwfl_seterrno (dwfl->attacherr);
224 : 0 : return -1;
225 : : }
226 : :
227 [ - + ]: 71 : if (dwfl->process == NULL)
228 : : {
229 : 0 : __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
230 : 0 : return -1;
231 : : }
232 : 71 : return dwfl->process->pid;
233 : : }
234 : : INTDEF(dwfl_pid)
235 : :
236 : : Dwfl *
237 : 166 : dwfl_thread_dwfl (Dwfl_Thread *thread)
238 : : {
239 : 166 : return thread->process->dwfl;
240 : : }
241 : : INTDEF(dwfl_thread_dwfl)
242 : :
243 : : pid_t
244 : 245 : dwfl_thread_tid (Dwfl_Thread *thread)
245 : : {
246 : 245 : return thread->tid;
247 : : }
248 : : INTDEF(dwfl_thread_tid)
249 : :
250 : : Dwfl_Thread *
251 : 165 : dwfl_frame_thread (Dwfl_Frame *state)
252 : : {
253 : 165 : return state->thread;
254 : : }
255 : : INTDEF(dwfl_frame_thread)
256 : :
257 : : Dwfl_Unwound_Source
258 : 57 : dwfl_frame_unwound_source (Dwfl_Frame *state)
259 : : {
260 : 57 : return state->unwound_source;
261 : : }
262 : : INTDEF(dwfl_frame_unwound_source)
263 : :
264 : : const char *
265 : 52 : dwfl_unwound_source_str (Dwfl_Unwound_Source unwound_source)
266 : : {
267 [ + + - - : 52 : switch (unwound_source)
- - ]
268 : : {
269 : : case DWFL_UNWOUND_NONE:
270 : : return "none";
271 : 16 : case DWFL_UNWOUND_INITIAL_FRAME:
272 : 16 : return "initial";
273 : 36 : case DWFL_UNWOUND_EH_CFI:
274 : 36 : return "eh_frame";
275 : 0 : case DWFL_UNWOUND_DWARF_CFI:
276 : 0 : return "dwarf";
277 : 0 : case DWFL_UNWOUND_EBL:
278 : 0 : return "ebl";
279 : 0 : default:
280 : 0 : return "unknown";
281 : : }
282 : : }
283 : : INTDEF(dwfl_unwound_source_str)
284 : :
285 : : int
286 : 32 : dwfl_getthreads (Dwfl *dwfl, int (*callback) (Dwfl_Thread *thread, void *arg),
287 : : void *arg)
288 : : {
289 [ - + ]: 32 : if (dwfl->attacherr != DWFL_E_NOERROR)
290 : : {
291 : 0 : __libdwfl_seterrno (dwfl->attacherr);
292 : 0 : return -1;
293 : : }
294 : :
295 : 32 : Dwfl_Process *process = dwfl->process;
296 [ - + ]: 32 : if (process == NULL)
297 : : {
298 : 0 : __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
299 : 0 : return -1;
300 : : }
301 : :
302 : 32 : Dwfl_Thread thread;
303 : 32 : thread.process = process;
304 : 32 : thread.unwound = NULL;
305 : 32 : thread.callbacks_arg = NULL;
306 : 32 : thread.aarch64.pauth_insn_mask = 0;
307 : :
308 : 78 : for (;;)
309 : : {
310 : 78 : thread.tid = process->callbacks->next_thread (dwfl,
311 : : process->callbacks_arg,
312 : : &thread.callbacks_arg);
313 [ + - ]: 78 : if (thread.tid < 0)
314 : : return -1;
315 [ + + ]: 78 : if (thread.tid == 0)
316 : : {
317 : 31 : __libdwfl_seterrno (DWFL_E_NOERROR);
318 : 31 : return 0;
319 : : }
320 : 47 : int err = callback (&thread, arg);
321 [ - + ]: 46 : if (err != DWARF_CB_OK)
322 : 0 : return err;
323 [ + - ]: 46 : assert (thread.unwound == NULL);
324 : : }
325 : : /* NOTREACHED */
326 : : }
327 : : INTDEF(dwfl_getthreads)
328 : :
329 : : struct one_arg
330 : : {
331 : : pid_t tid;
332 : : bool seen;
333 : : int (*callback) (Dwfl_Thread *thread, void *arg);
334 : : void *arg;
335 : : int ret;
336 : : };
337 : :
338 : : static int
339 : 0 : get_one_thread_cb (Dwfl_Thread *thread, void *arg)
340 : : {
341 : 0 : struct one_arg *oa = (struct one_arg *) arg;
342 [ # # # # ]: 0 : if (! oa->seen && INTUSE(dwfl_thread_tid) (thread) == oa->tid)
343 : : {
344 : 0 : oa->seen = true;
345 : 0 : oa->ret = oa->callback (thread, oa->arg);
346 : 0 : return DWARF_CB_ABORT;
347 : : }
348 : :
349 : : return DWARF_CB_OK;
350 : : }
351 : :
352 : : /* Note not currently exported, will be when there are more Dwfl_Thread
353 : : properties to query. Use dwfl_getthread_frames for now directly. */
354 : : static int
355 : 0 : getthread (Dwfl *dwfl, pid_t tid,
356 : : int (*callback) (Dwfl_Thread *thread, void *arg),
357 : : void *arg)
358 : : {
359 [ # # ]: 0 : if (dwfl->attacherr != DWFL_E_NOERROR)
360 : : {
361 : 0 : __libdwfl_seterrno (dwfl->attacherr);
362 : 0 : return -1;
363 : : }
364 : :
365 : 0 : Dwfl_Process *process = dwfl->process;
366 [ # # ]: 0 : if (process == NULL)
367 : : {
368 : 0 : __libdwfl_seterrno (DWFL_E_NO_ATTACH_STATE);
369 : 0 : return -1;
370 : : }
371 : :
372 [ # # ]: 0 : if (process->callbacks->get_thread != NULL)
373 : : {
374 : 0 : Dwfl_Thread thread;
375 : 0 : thread.process = process;
376 : 0 : thread.unwound = NULL;
377 : 0 : thread.callbacks_arg = NULL;
378 : 0 : thread.aarch64.pauth_insn_mask = 0;
379 : :
380 [ # # ]: 0 : if (process->callbacks->get_thread (dwfl, tid, process->callbacks_arg,
381 : : &thread.callbacks_arg))
382 : : {
383 : 0 : thread.tid = tid;
384 : 0 : return callback (&thread, arg);
385 : : }
386 : :
387 : : return -1;
388 : : }
389 : :
390 : 0 : struct one_arg oa = { .tid = tid, .callback = callback,
391 : : .arg = arg, .seen = false };
392 : 0 : int err = INTUSE(dwfl_getthreads) (dwfl, get_one_thread_cb, &oa);
393 : :
394 [ # # # # ]: 0 : if (err == DWARF_CB_ABORT && oa.seen)
395 : 0 : return oa.ret;
396 : :
397 [ # # # # ]: 0 : if (err == DWARF_CB_OK && ! oa.seen)
398 : : {
399 : 0 : errno = ESRCH;
400 : 0 : __libdwfl_seterrno (DWFL_E_ERRNO);
401 : 0 : return -1;
402 : : }
403 : :
404 : : return err;
405 : : }
406 : :
407 : : struct one_thread
408 : : {
409 : : int (*callback) (Dwfl_Frame *frame, void *arg);
410 : : void *arg;
411 : : };
412 : :
413 : : static int
414 : 0 : get_one_thread_frames_cb (Dwfl_Thread *thread, void *arg)
415 : : {
416 : 0 : struct one_thread *ot = (struct one_thread *) arg;
417 : 0 : return INTUSE(dwfl_thread_getframes) (thread, ot->callback, ot->arg);
418 : : }
419 : :
420 : : int
421 : 0 : dwfl_getthread_frames (Dwfl *dwfl, pid_t tid,
422 : : int (*callback) (Dwfl_Frame *frame, void *arg),
423 : : void *arg)
424 : : {
425 : 0 : struct one_thread ot = { .callback = callback, .arg = arg };
426 : 0 : return getthread (dwfl, tid, get_one_thread_frames_cb, &ot);
427 : : }
428 : : INTDEF(dwfl_getthread_frames)
429 : :
430 : : int
431 : 44 : dwfl_thread_getframes (Dwfl_Thread *thread,
432 : : int (*callback) (Dwfl_Frame *state, void *arg),
433 : : void *arg)
434 : : {
435 : 44 : Ebl *ebl = thread->process->ebl;
436 [ - + ]: 44 : if (ebl_frame_nregs (ebl) == 0)
437 : : {
438 : 0 : __libdwfl_seterrno (DWFL_E_NO_UNWIND);
439 : 0 : return -1;
440 : : }
441 [ - + ]: 44 : if (state_alloc (thread) == NULL)
442 : : {
443 : 0 : __libdwfl_seterrno (DWFL_E_NOMEM);
444 : 0 : return -1;
445 : : }
446 : 44 : Dwfl_Process *process = thread->process;
447 [ - + ]: 44 : if (! process->callbacks->set_initial_registers (thread,
448 : : thread->callbacks_arg))
449 : : {
450 : 0 : free_states (thread->unwound);
451 : 0 : thread->unwound = NULL;
452 : 0 : return -1;
453 : : }
454 : 44 : Dwfl_Frame *state = thread->unwound;
455 : 44 : thread->unwound = NULL;
456 [ - + ]: 44 : if (! state_fetch_pc (state))
457 : : {
458 [ # # ]: 0 : if (process->callbacks->thread_detach)
459 : 0 : process->callbacks->thread_detach (thread, thread->callbacks_arg);
460 : 18 : free_states (state);
461 : : return -1;
462 : : }
463 : 222 : do
464 : : {
465 : 222 : int err = callback (state, arg);
466 [ + + ]: 221 : if (err != DWARF_CB_OK)
467 : : {
468 [ - + ]: 7 : if (process->callbacks->thread_detach)
469 : 0 : process->callbacks->thread_detach (thread, thread->callbacks_arg);
470 : 43 : free_states (state);
471 : : return err;
472 : : }
473 : 214 : __libdwfl_frame_unwind (state);
474 : 214 : Dwfl_Frame *next = state->unwound;
475 : : /* The old frame is no longer needed. */
476 : 214 : free (state);
477 : 214 : state = next;
478 : : }
479 [ + + + + ]: 214 : while (state && state->pc_state == DWFL_FRAME_STATE_PC_SET);
480 : :
481 : 36 : Dwfl_Error err = dwfl_errno ();
482 [ + + ]: 36 : if (process->callbacks->thread_detach)
483 : 4 : process->callbacks->thread_detach (thread, thread->callbacks_arg);
484 [ + + - + ]: 36 : if (state == NULL || state->pc_state == DWFL_FRAME_STATE_ERROR)
485 : : {
486 : 18 : free_states (state);
487 : 18 : __libdwfl_seterrno (err);
488 : 18 : return -1;
489 : : }
490 [ - + ]: 18 : assert (state->pc_state == DWFL_FRAME_STATE_PC_UNDEFINED);
491 : 43 : free_states (state);
492 : : return 0;
493 : : }
494 : : INTDEF(dwfl_thread_getframes)
|