]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8d4e028f LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2014 Lennart Poettering | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. | |
19 | ***/ | |
20 | ||
21 | #include <dwarf.h> | |
22 | #include <elfutils/libdwfl.h> | |
23 | ||
b5efdb8a | 24 | #include "alloc-util.h" |
3ffd4af2 | 25 | #include "fd-util.h" |
f97b34a6 | 26 | #include "format-util.h" |
8d4e028f | 27 | #include "macro.h" |
3ffd4af2 | 28 | #include "stacktrace.h" |
07630cea LP |
29 | #include "string-util.h" |
30 | #include "util.h" | |
8d4e028f LP |
31 | |
32 | #define FRAMES_MAX 64 | |
33 | #define THREADS_MAX 64 | |
34 | ||
35 | struct stack_context { | |
36 | FILE *f; | |
37 | Dwfl *dwfl; | |
38 | Elf *elf; | |
39 | unsigned n_thread; | |
40 | unsigned n_frame; | |
41 | }; | |
42 | ||
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; | |
48 | Dwfl_Module *module; | |
49 | bool is_activation; | |
50 | ||
51 | assert(frame); | |
52 | assert(c); | |
53 | ||
54 | if (c->n_frame >= FRAMES_MAX) | |
55 | return DWARF_CB_ABORT; | |
56 | ||
57 | if (!dwfl_frame_pc(frame, &pc, &is_activation)) | |
58 | return DWARF_CB_ABORT; | |
59 | ||
60 | pc_adjusted = pc - (is_activation ? 0 : 1); | |
61 | ||
62 | module = dwfl_addrmodule(c->dwfl, pc_adjusted); | |
63 | if (module) { | |
64 | Dwarf_Die *s, *cudie; | |
65 | int n; | |
66 | ||
67 | cudie = dwfl_module_addrdie(module, pc_adjusted, &bias); | |
68 | if (cudie) { | |
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; | |
73 | ||
74 | a = dwarf_attr_integrate(s, DW_AT_MIPS_linkage_name, &space); | |
75 | if (!a) | |
76 | a = dwarf_attr_integrate(s, DW_AT_linkage_name, &space); | |
77 | if (a) | |
78 | symbol = dwarf_formstring(a); | |
79 | if (!symbol) | |
80 | symbol = dwarf_diename(s); | |
81 | ||
82 | if (symbol) | |
83 | break; | |
84 | } | |
85 | } | |
86 | } | |
87 | ||
88 | if (!symbol) | |
89 | symbol = dwfl_module_addrname(module, pc_adjusted); | |
90 | ||
91 | fname = dwfl_module_info(module, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | |
92 | } | |
93 | ||
94 | fprintf(c->f, "#%-2u 0x%016" PRIx64 " %s (%s)\n", c->n_frame, (uint64_t) pc, strna(symbol), strna(fname)); | |
313cefa1 | 95 | c->n_frame++; |
8d4e028f LP |
96 | |
97 | return DWARF_CB_OK; | |
98 | } | |
99 | ||
100 | static int thread_callback(Dwfl_Thread *thread, void *userdata) { | |
101 | struct stack_context *c = userdata; | |
102 | pid_t tid; | |
103 | ||
104 | assert(thread); | |
105 | assert(c); | |
106 | ||
107 | if (c->n_thread >= THREADS_MAX) | |
108 | return DWARF_CB_ABORT; | |
109 | ||
110 | if (c->n_thread != 0) | |
4b61c875 | 111 | fputc_unlocked('\n', c->f); |
8d4e028f LP |
112 | |
113 | c->n_frame = 0; | |
114 | ||
115 | tid = dwfl_thread_tid(thread); | |
116 | fprintf(c->f, "Stack trace of thread " PID_FMT ":\n", tid); | |
117 | ||
118 | if (dwfl_thread_getframes(thread, frame_callback, c) < 0) | |
119 | return DWARF_CB_ABORT; | |
120 | ||
313cefa1 | 121 | c->n_thread++; |
8d4e028f LP |
122 | |
123 | return DWARF_CB_OK; | |
124 | } | |
125 | ||
126 | int coredump_make_stack_trace(int fd, const char *executable, char **ret) { | |
127 | ||
128 | static const Dwfl_Callbacks callbacks = { | |
129 | .find_elf = dwfl_build_id_find_elf, | |
130 | .find_debuginfo = dwfl_standard_find_debuginfo, | |
131 | }; | |
132 | ||
133 | struct stack_context c = {}; | |
134 | char *buf = NULL; | |
135 | size_t sz = 0; | |
136 | int r; | |
137 | ||
138 | assert(fd >= 0); | |
139 | assert(ret); | |
140 | ||
141 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) | |
142 | return -errno; | |
143 | ||
144 | c.f = open_memstream(&buf, &sz); | |
145 | if (!c.f) | |
146 | return -ENOMEM; | |
147 | ||
148 | elf_version(EV_CURRENT); | |
149 | ||
150 | c.elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); | |
151 | if (!c.elf) { | |
152 | r = -EINVAL; | |
153 | goto finish; | |
154 | } | |
155 | ||
156 | c.dwfl = dwfl_begin(&callbacks); | |
157 | if (!c.dwfl) { | |
158 | r = -EINVAL; | |
159 | goto finish; | |
160 | } | |
161 | ||
162 | if (dwfl_core_file_report(c.dwfl, c.elf, executable) < 0) { | |
163 | r = -EINVAL; | |
164 | goto finish; | |
165 | } | |
166 | ||
167 | if (dwfl_report_end(c.dwfl, NULL, NULL) != 0) { | |
168 | r = -EINVAL; | |
169 | goto finish; | |
170 | } | |
171 | ||
172 | if (dwfl_core_file_attach(c.dwfl, c.elf) < 0) { | |
173 | r = -EINVAL; | |
174 | goto finish; | |
175 | } | |
176 | ||
177 | if (dwfl_getthreads(c.dwfl, thread_callback, &c) < 0) { | |
178 | r = -EINVAL; | |
179 | goto finish; | |
180 | } | |
181 | ||
74ca738f | 182 | c.f = safe_fclose(c.f); |
8d4e028f LP |
183 | |
184 | *ret = buf; | |
185 | buf = NULL; | |
186 | ||
187 | r = 0; | |
188 | ||
189 | finish: | |
190 | if (c.dwfl) | |
191 | dwfl_end(c.dwfl); | |
192 | ||
193 | if (c.elf) | |
194 | elf_end(c.elf); | |
195 | ||
74ca738f | 196 | safe_fclose(c.f); |
8d4e028f LP |
197 | |
198 | free(buf); | |
199 | ||
200 | return r; | |
201 | } |