1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <elfutils/libdwfl.h>
24 #include "alloc-util.h"
26 #include "format-util.h"
28 #include "stacktrace.h"
29 #include "string-util.h"
33 #define THREADS_MAX 64
35 struct stack_context
{
43 static int frame_callback(Dwfl_Frame
*frame
, void *userdata
) {
44 struct stack_context
*c
= userdata
;
45 Dwarf_Addr pc
, pc_adjusted
, bias
= 0;
46 _cleanup_free_ Dwarf_Die
*scopes
= NULL
;
47 const char *fname
= NULL
, *symbol
= NULL
;
54 if (c
->n_frame
>= FRAMES_MAX
)
55 return DWARF_CB_ABORT
;
57 if (!dwfl_frame_pc(frame
, &pc
, &is_activation
))
58 return DWARF_CB_ABORT
;
60 pc_adjusted
= pc
- (is_activation
? 0 : 1);
62 module
= dwfl_addrmodule(c
->dwfl
, pc_adjusted
);
67 cudie
= dwfl_module_addrdie(module
, pc_adjusted
, &bias
);
69 n
= dwarf_getscopes(cudie
, pc_adjusted
- bias
, &scopes
);
70 for (s
= scopes
; s
< scopes
+ n
; s
++) {
71 if (IN_SET(dwarf_tag(s
), DW_TAG_subprogram
, DW_TAG_inlined_subroutine
, DW_TAG_entry_point
)) {
72 Dwarf_Attribute
*a
, space
;
74 a
= dwarf_attr_integrate(s
, DW_AT_MIPS_linkage_name
, &space
);
76 a
= dwarf_attr_integrate(s
, DW_AT_linkage_name
, &space
);
78 symbol
= dwarf_formstring(a
);
80 symbol
= dwarf_diename(s
);
89 symbol
= dwfl_module_addrname(module
, pc_adjusted
);
91 fname
= dwfl_module_info(module
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
94 fprintf(c
->f
, "#%-2u 0x%016" PRIx64
" %s (%s)\n", c
->n_frame
, (uint64_t) pc
, strna(symbol
), strna(fname
));
100 static int thread_callback(Dwfl_Thread
*thread
, void *userdata
) {
101 struct stack_context
*c
= userdata
;
107 if (c
->n_thread
>= THREADS_MAX
)
108 return DWARF_CB_ABORT
;
110 if (c
->n_thread
!= 0)
111 fputc_unlocked('\n', c
->f
);
115 tid
= dwfl_thread_tid(thread
);
116 fprintf(c
->f
, "Stack trace of thread " PID_FMT
":\n", tid
);
118 if (dwfl_thread_getframes(thread
, frame_callback
, c
) < 0)
119 return DWARF_CB_ABORT
;
126 int coredump_make_stack_trace(int fd
, const char *executable
, char **ret
) {
128 static const Dwfl_Callbacks callbacks
= {
129 .find_elf
= dwfl_build_id_find_elf
,
130 .find_debuginfo
= dwfl_standard_find_debuginfo
,
133 struct stack_context c
= {};
141 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
144 c
.f
= open_memstream(&buf
, &sz
);
148 elf_version(EV_CURRENT
);
150 c
.elf
= elf_begin(fd
, ELF_C_READ_MMAP
, NULL
);
156 c
.dwfl
= dwfl_begin(&callbacks
);
162 if (dwfl_core_file_report(c
.dwfl
, c
.elf
, executable
) < 0) {
167 if (dwfl_report_end(c
.dwfl
, NULL
, NULL
) != 0) {
172 if (dwfl_core_file_attach(c
.dwfl
, c
.elf
) < 0) {
177 if (dwfl_getthreads(c
.dwfl
, thread_callback
, &c
) < 0) {
182 c
.f
= safe_fclose(c
.f
);