Branch data Line data Source code
1 : : /* Get Dwarf Frame state for perf stack sample data.
2 : : Copyright (C) 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 "libdwfl_stacktraceP.h"
34 : :
35 : : /* Various functions providing arch-specific info: */
36 : :
37 : : Ebl *default_ebl = NULL;
38 : : GElf_Half default_ebl_machine = EM_NONE;
39 : :
40 : : uint64_t
41 : 0 : dwflst_perf_sample_preferred_regs_mask (GElf_Half machine)
42 : : {
43 : : /* XXX The most likely case is that this will only be called once,
44 : : for the current architecture. So we keep one Ebl* around for
45 : : answering this query and replace it in the unlikely case of
46 : : getting called with different architectures. */
47 [ # # # # ]: 0 : if (default_ebl != NULL && default_ebl_machine != machine)
48 : : {
49 : 0 : ebl_closebackend(default_ebl);
50 : 0 : default_ebl = NULL;
51 : : }
52 [ # # ]: 0 : if (default_ebl == NULL)
53 : : {
54 : 0 : default_ebl = ebl_openbackend_machine(machine);
55 : 0 : default_ebl_machine = machine;
56 : : }
57 [ # # ]: 0 : if (default_ebl != NULL)
58 : 0 : return ebl_perf_frame_regs_mask (default_ebl);
59 : : return 0;
60 : : }
61 : :
62 : : GElf_Half
63 : 14 : dwflst_arch_from_uname (const char *umachine)
64 : : {
65 [ - + ]: 14 : if (strncmp(umachine, "x86_64", 6) == 0)
66 : : return EM_X86_64;
67 [ # # ]: 0 : else if (strncmp(umachine, "i686", 4) == 0
68 [ # # ]: 0 : || strncmp(umachine, "i386", 4) == 0)
69 : : return EM_386;
70 [ # # ]: 0 : else if (strncmp(umachine, "aarch64", 7) == 0)
71 : : return EM_AARCH64;
72 [ # # ]: 0 : else if (strncmp(umachine, "armv7l", 6) == 0)
73 : : return EM_ARM;
74 [ # # ]: 0 : else if (strncmp(umachine, "alpha", 5) == 0)
75 : 0 : return EM_ALPHA;
76 : : /* XXX Other architectures not yet supported. */
77 : : return EM_NONE;
78 : : }
79 : :
80 : : /* XXX The per-machine switches suggest the following implementations
81 : : could be folded into backends/, but we would need to create a Ebl
82 : : to handle the dispatch: */
83 : :
84 : : uint32_t
85 : 11826 : dwflst_arch_expected_frame_nregs (GElf_Half machine)
86 : : {
87 : : /* For aarch64, we actually use fewer than ebl->frame_nregs to unwind: */
88 [ + - ]: 11826 : if (machine == EM_AARCH64)
89 : : return 14;
90 [ + - ]: 11826 : if (machine == EM_ARM)
91 : : return 16;
92 : : /* On alpha, expect everything the kernel can sample: */
93 [ + - ]: 11826 : if (machine == EM_ALPHA)
94 : : return 25;
95 : : /* On x86, expect everything except FLAGS: */
96 [ + - ]: 11826 : if (machine == EM_X86_64 || machine == EM_386)
97 : : /* XXX An external user of the library can't access the Ebl, hence
98 : : can't conveniently provide it to us it here. We provide the
99 : : constant directly rather than initializing a new Ebl. */
100 [ - + ]: 11826 : return machine == EM_X86_64 ? 17 : 9;
101 : : /* return ebl_frame_nregs(ebl); */
102 : : /* XXX Other architectures are not supported yet.
103 : : In general, it's fine to be on the permissive side here
104 : : for a minimum expected number of registers. */
105 : : return 0;
106 : : }
107 : :
108 : : int
109 : 65594 : dwflst_arch_sp_dwarf_reg (GElf_Half machine, bool force_abi32)
110 : : {
111 [ + - - - : 65594 : switch (machine) {
- - ]
112 : 65594 : case EM_X86_64:
113 [ + - ]: 65594 : return force_abi32 ? 4 : 7;
114 : : case EM_386:
115 : : return 4;
116 : 0 : case EM_ARM:
117 : 0 : return 13;
118 : 0 : case EM_AARCH64:
119 [ # # ]: 0 : return force_abi32 ? 13 : 31;
120 : 0 : case EM_ALPHA:
121 : 0 : return 30;
122 : 0 : default:
123 : : /* XXX Other architectures are not supported yet. */
124 : 0 : return -1;
125 : : }
126 : : }
127 : :
128 : : int
129 : 11826 : dwflst_arch_sp_perf_reg (GElf_Half machine,
130 : : uint64_t perf_regs_mask, bool is_abi32)
131 : : {
132 : 11826 : int sp_perf_index;
133 [ - + ]: 11826 : if (machine == EM_X86_64 || machine == EM_386)
134 : : sp_perf_index = 7; /* uniform on 32/64-bit abi */
135 : : /* compare backends/x86_initreg_sample.c;
136 : : for dwarf_index, would be (is_abi32 ? 4 : 7) */
137 [ # # ]: 0 : else if (machine == EM_ARM || machine == EM_AARCH64)
138 [ # # ]: 0 : sp_perf_index = (is_abi32 ? 13 : 31);
139 : : /* basic linear mapping of perf_regs<->dwarf_regs */
140 [ # # ]: 0 : else if (machine == EM_ALPHA)
141 : : sp_perf_index = 30;
142 : : /* PERF_REG_ALPHA_SP */
143 : : else
144 : : /* XXX Other architectures are not supported yet. */
145 : : return -1;
146 : :
147 [ + - ]: 11826 : if (perf_regs_mask == 0)
148 : : /* Assume all registers present: */
149 : : return sp_perf_index;
150 : :
151 : : /* Iterate perf_regs_mask to find
152 : : k == index of sp in perf_regs_mask
153 : : j == index of sp in regs[] */
154 : : int j, k; uint64_t bit;
155 : : for (k = 0, j = -1, bit = 1;
156 [ + - ]: 94608 : k <= sp_perf_index; k++, bit <<= 1)
157 : : {
158 [ + - ]: 94608 : if ((bit & perf_regs_mask))
159 : 94608 : j++;
160 : : else
161 : 0 : continue; /* sp may not be present */
162 [ + + ]: 94608 : if (k == sp_perf_index)
163 : : return j;
164 : : }
165 : : return -2;
166 : : }
167 : :
168 : : /* Stack sample handling: */
169 : :
170 : : struct sample_info {
171 : : pid_t pid;
172 : : pid_t tid;
173 : : Dwarf_Addr base_addr;
174 : : const uint8_t *stack;
175 : : size_t stack_size;
176 : : const Dwarf_Word *regs;
177 : : uint n_regs;
178 : : const int *regs_mapping;
179 : : size_t n_regs_mapping;
180 : : int elfclass;
181 : : Dwarf_Addr pc;
182 : : };
183 : :
184 : : /* The next few functions imitate the corefile interface for a single
185 : : stack sample, with very restricted access to registers and memory. */
186 : :
187 : : /* Just yield the single thread id matching the sample. */
188 : : static pid_t
189 : 0 : sample_next_thread (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg,
190 : : void **thread_argp)
191 : : {
192 : 0 : struct sample_info *sample_arg =
193 : : (struct sample_info *)dwfl_arg;
194 [ # # ]: 0 : if (*thread_argp == NULL)
195 : : {
196 : 0 : *thread_argp = (void *)0xea7b3375;
197 : 0 : return sample_arg->tid;
198 : : }
199 : : else
200 : : return 0;
201 : : }
202 : :
203 : : /* Just check that the thread id matches the sample. */
204 : : static bool
205 : 11826 : sample_getthread (Dwfl *dwfl __attribute__ ((unused)), pid_t tid,
206 : : void *dwfl_arg, void **thread_argp)
207 : : {
208 : 11826 : struct sample_info *sample_arg =
209 : : (struct sample_info *)dwfl_arg;
210 : 11826 : *thread_argp = (void *)sample_arg;
211 [ - + ]: 11826 : if (sample_arg->tid != tid)
212 : : {
213 : 0 : __libdwfl_seterrno(DWFL_E_INVALID_ARGUMENT);
214 : 0 : return false;
215 : : }
216 : : return true;
217 : : }
218 : :
219 : : #define copy_word_64(result, d) \
220 : : if ((((uintptr_t) (d)) & (sizeof (uint64_t) - 1)) == 0) \
221 : : *(result) = *(uint64_t *)(d); \
222 : : else \
223 : : memcpy ((result), (d), sizeof (uint64_t));
224 : :
225 : : #define copy_word_32(result, d) \
226 : : if ((((uintptr_t) (d)) & (sizeof (uint32_t) - 1)) == 0) \
227 : : *(result) = *(uint32_t *)(d); \
228 : : else \
229 : : memcpy ((result), (d), sizeof (uint32_t));
230 : :
231 : : #define copy_word(result, d, elfclass) \
232 : : if ((elfclass) == ELFCLASS64) \
233 : : { copy_word_64((result), (d)); } \
234 : : else if ((elfclass) == ELFCLASS32) \
235 : : { copy_word_32((result), (d)); } \
236 : : else \
237 : : *(result) = 0;
238 : :
239 : : static bool
240 : 773 : elf_memory_read (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Word *result, void *arg)
241 : : {
242 : 773 : struct sample_info *sample_arg =
243 : : (struct sample_info *)arg;
244 : 773 : Dwfl_Module *mod = INTUSE(dwfl_addrmodule) (dwfl, addr);
245 : 773 : Dwarf_Addr bias;
246 : 773 : Elf_Scn *section = INTUSE(dwfl_module_address_section) (mod, &addr, &bias);
247 : :
248 [ + - ]: 773 : if (!section)
249 : : {
250 : 773 : __libdwfl_seterrno(DWFL_E_ADDR_OUTOFRANGE);
251 : 773 : return false;
252 : : }
253 : :
254 : 0 : Elf_Data *data = elf_getdata(section, NULL);
255 [ # # # # : 0 : if (data && data->d_buf && data->d_size > addr) {
# # ]
256 : 0 : uint8_t *d = ((uint8_t *)data->d_buf) + addr;
257 [ # # # # : 0 : copy_word(result, d, sample_arg->elfclass);
# # # # ]
258 : : return true;
259 : : }
260 : 0 : __libdwfl_seterrno(DWFL_E_ADDR_OUTOFRANGE);
261 : 0 : return false;
262 : : }
263 : :
264 : : static bool
265 : 322604 : sample_memory_read (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Word *result, void *arg)
266 : : {
267 : 322604 : struct sample_info *sample_arg =
268 : : (struct sample_info *)arg;
269 : : /* Imitate read_cached_memory() with the stack sample data as the cache. */
270 [ + + ]: 322604 : if (addr < sample_arg->base_addr ||
271 [ - + ]: 321831 : addr - sample_arg->base_addr >= sample_arg->stack_size)
272 : 773 : return elf_memory_read(dwfl, addr, result, arg);
273 : 321831 : const uint8_t *d = &sample_arg->stack[addr - sample_arg->base_addr];
274 [ + - + - : 321831 : copy_word(result, d, sample_arg->elfclass);
- - - - ]
275 : : return true;
276 : : }
277 : :
278 : :
279 : : static bool
280 : 11826 : sample_set_initial_registers (Dwfl_Thread *thread, void *arg)
281 : : {
282 : 11826 : struct sample_info *sample_arg =
283 : : (struct sample_info *)arg;
284 : 11826 : INTUSE(dwfl_thread_state_register_pc) (thread, sample_arg->pc);
285 : 11826 : Dwfl_Process *process = thread->process;
286 : 11826 : Ebl *ebl = process->ebl;
287 : 23652 : return ebl_set_initial_registers_sample
288 : 11826 : (ebl, sample_arg->regs, sample_arg->n_regs,
289 : : sample_arg->regs_mapping, sample_arg->n_regs_mapping,
290 : : __libdwfl_set_initial_registers_thread, thread);
291 : : }
292 : :
293 : : static void
294 : 6 : sample_detach (Dwfl *dwfl __attribute__ ((unused)), void *dwfl_arg)
295 : : {
296 : 6 : struct sample_info *sample_arg =
297 : : (struct sample_info *)dwfl_arg;
298 : 6 : free (sample_arg);
299 : 6 : }
300 : :
301 : : static const Dwfl_Thread_Callbacks sample_thread_callbacks =
302 : : {
303 : : sample_next_thread,
304 : : sample_getthread,
305 : : sample_memory_read,
306 : : sample_set_initial_registers,
307 : : sample_detach,
308 : : NULL, /* sample_thread_detach */
309 : : };
310 : :
311 : : int
312 : 0 : dwflst_sample_getframes (Dwfl *dwfl, Elf *elf,
313 : : pid_t pid, pid_t tid,
314 : : const void *stack, size_t stack_size,
315 : : const Dwarf_Word *regs, uint n_regs,
316 : : const int *regs_mapping, size_t n_regs_mapping,
317 : : int (*callback) (Dwfl_Frame *state, void *arg),
318 : : void *arg)
319 : : {
320 : : /* TODO: Lock the dwfl to ensure attach_state does not interfere
321 : : with other dwfl_perf_sample_getframes calls. */
322 : :
323 : 0 : struct sample_info *sample_arg;
324 : 0 : bool attached = false;
325 [ # # ]: 0 : if (dwfl->process != NULL)
326 : : {
327 : 0 : sample_arg = dwfl->process->callbacks_arg;
328 : 0 : attached = true;
329 : : }
330 : : else
331 : : {
332 : 0 : sample_arg = malloc (sizeof *sample_arg);
333 [ # # ]: 0 : if (sample_arg == NULL)
334 : : {
335 : 0 : __libdwfl_seterrno(DWFL_E_NOMEM);
336 : 0 : return -1;
337 : : }
338 : : }
339 : :
340 : 0 : sample_arg->pid = pid;
341 : 0 : sample_arg->tid = tid;
342 : 0 : sample_arg->stack = (const uint8_t *)stack;
343 : 0 : sample_arg->stack_size = stack_size;
344 : 0 : sample_arg->regs = regs;
345 : 0 : sample_arg->n_regs = n_regs;
346 : 0 : sample_arg->regs_mapping = regs_mapping;
347 : 0 : sample_arg->n_regs_mapping = n_regs_mapping;
348 : :
349 [ # # ]: 0 : if (! attached
350 [ # # ]: 0 : && ! INTUSE(dwfl_attach_state) (dwfl, elf, pid,
351 : : &sample_thread_callbacks, sample_arg))
352 : : {
353 : 0 : free(sample_arg);
354 : 0 : return -1;
355 : : }
356 : :
357 : 0 : Dwfl_Process *process = dwfl->process;
358 : 0 : Ebl *ebl = process->ebl;
359 : 0 : sample_arg->elfclass = ebl_get_elfclass(ebl);
360 : 0 : ebl_sample_sp_pc(ebl, regs, n_regs,
361 : : regs_mapping, n_regs_mapping,
362 : 0 : &sample_arg->base_addr, &sample_arg->pc);
363 : :
364 : 0 : return INTUSE(dwfl_getthread_frames) (dwfl, tid, callback, arg);
365 : : }
366 : :
367 : : int
368 : 11826 : dwflst_perf_sample_getframes (Dwfl *dwfl, Elf *elf,
369 : : pid_t pid, pid_t tid,
370 : : const void *stack, size_t stack_size,
371 : : const Dwarf_Word *regs, uint32_t n_regs,
372 : : uint64_t perf_regs_mask, uint32_t abi,
373 : : int (*callback) (Dwfl_Frame *state, void *arg),
374 : : void *arg)
375 : : {
376 : : /* TODO: Lock the dwfl to ensure attach_state does not interfere
377 : : with other dwfl_perf_sample_getframes calls. */
378 : :
379 : 11826 : struct sample_info *sample_arg;
380 : 11826 : bool attached = false;
381 [ + + ]: 11826 : if (dwfl->process != NULL)
382 : : {
383 : 11820 : sample_arg = dwfl->process->callbacks_arg;
384 : 11820 : attached = true;
385 : : }
386 : : else
387 : : {
388 : 6 : sample_arg = malloc (sizeof *sample_arg);
389 [ - + ]: 6 : if (sample_arg == NULL)
390 : : {
391 : 0 : __libdwfl_seterrno(DWFL_E_NOMEM);
392 : 0 : return -1;
393 : : }
394 : : }
395 : :
396 : 11826 : sample_arg->pid = pid;
397 : 11826 : sample_arg->tid = tid;
398 : 11826 : sample_arg->stack = (const uint8_t *)stack;
399 : 11826 : sample_arg->stack_size = stack_size;
400 : 11826 : sample_arg->regs = regs;
401 : 11826 : sample_arg->n_regs = n_regs;
402 : :
403 [ + + ]: 11826 : if (! attached
404 [ - + ]: 6 : && ! INTUSE(dwfl_attach_state) (dwfl, elf, pid,
405 : : &sample_thread_callbacks, sample_arg))
406 : : {
407 : 0 : free(sample_arg);
408 : 0 : return -1;
409 : : }
410 : :
411 : : /* Select the regs_mapping based on architecture. This will be
412 : : cached in ebl to avoid having to recompute the regs_mapping array
413 : : when perf_regs_mask is consistent for the entire session: */
414 : 11826 : Dwfl_Process *process = dwfl->process;
415 : 11826 : Ebl *ebl = process->ebl;
416 [ - + ]: 11826 : if (!ebl_sample_perf_regs_mapping(ebl,
417 : : perf_regs_mask, abi,
418 : : &sample_arg->regs_mapping, &sample_arg->n_regs_mapping))
419 : : {
420 : 0 : __libdwfl_seterrno(DWFL_E_LIBEBL_BAD);
421 : 0 : return -1;
422 : : }
423 : 11826 : sample_arg->elfclass = ebl_get_elfclass(ebl);
424 : 11826 : ebl_sample_sp_pc(ebl, regs, n_regs,
425 : : sample_arg->regs_mapping, sample_arg->n_regs_mapping,
426 : 11826 : &sample_arg->base_addr, &sample_arg->pc);
427 : :
428 : : /* XXX May want to check if abi matches ebl_get_elfclass(ebl). */
429 : 11826 : return INTUSE(dwfl_getthread_frames) (dwfl, tid, callback, arg);
430 : : }
|