]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/coredump/stacktrace.c
Merge pull request #7640 from keszybz/tainting-updates
[thirdparty/systemd.git] / src / coredump / stacktrace.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 #include <stdio_ext.h>
24
25 #include "alloc-util.h"
26 #include "fd-util.h"
27 #include "format-util.h"
28 #include "macro.h"
29 #include "stacktrace.h"
30 #include "string-util.h"
31 #include "util.h"
32
33 #define FRAMES_MAX 64
34 #define THREADS_MAX 64
35
36 struct stack_context {
37 FILE *f;
38 Dwfl *dwfl;
39 Elf *elf;
40 unsigned n_thread;
41 unsigned n_frame;
42 };
43
44 static int frame_callback(Dwfl_Frame *frame, void *userdata) {
45 struct stack_context *c = userdata;
46 Dwarf_Addr pc, pc_adjusted, bias = 0;
47 _cleanup_free_ Dwarf_Die *scopes = NULL;
48 const char *fname = NULL, *symbol = NULL;
49 Dwfl_Module *module;
50 bool is_activation;
51
52 assert(frame);
53 assert(c);
54
55 if (c->n_frame >= FRAMES_MAX)
56 return DWARF_CB_ABORT;
57
58 if (!dwfl_frame_pc(frame, &pc, &is_activation))
59 return DWARF_CB_ABORT;
60
61 pc_adjusted = pc - (is_activation ? 0 : 1);
62
63 module = dwfl_addrmodule(c->dwfl, pc_adjusted);
64 if (module) {
65 Dwarf_Die *s, *cudie;
66 int n;
67
68 cudie = dwfl_module_addrdie(module, pc_adjusted, &bias);
69 if (cudie) {
70 n = dwarf_getscopes(cudie, pc_adjusted - bias, &scopes);
71 for (s = scopes; s < scopes + n; s++) {
72 if (IN_SET(dwarf_tag(s), DW_TAG_subprogram, DW_TAG_inlined_subroutine, DW_TAG_entry_point)) {
73 Dwarf_Attribute *a, space;
74
75 a = dwarf_attr_integrate(s, DW_AT_MIPS_linkage_name, &space);
76 if (!a)
77 a = dwarf_attr_integrate(s, DW_AT_linkage_name, &space);
78 if (a)
79 symbol = dwarf_formstring(a);
80 if (!symbol)
81 symbol = dwarf_diename(s);
82
83 if (symbol)
84 break;
85 }
86 }
87 }
88
89 if (!symbol)
90 symbol = dwfl_module_addrname(module, pc_adjusted);
91
92 fname = dwfl_module_info(module, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
93 }
94
95 fprintf(c->f, "#%-2u 0x%016" PRIx64 " %s (%s)\n", c->n_frame, (uint64_t) pc, strna(symbol), strna(fname));
96 c->n_frame++;
97
98 return DWARF_CB_OK;
99 }
100
101 static int thread_callback(Dwfl_Thread *thread, void *userdata) {
102 struct stack_context *c = userdata;
103 pid_t tid;
104
105 assert(thread);
106 assert(c);
107
108 if (c->n_thread >= THREADS_MAX)
109 return DWARF_CB_ABORT;
110
111 if (c->n_thread != 0)
112 fputc('\n', c->f);
113
114 c->n_frame = 0;
115
116 tid = dwfl_thread_tid(thread);
117 fprintf(c->f, "Stack trace of thread " PID_FMT ":\n", tid);
118
119 if (dwfl_thread_getframes(thread, frame_callback, c) < 0)
120 return DWARF_CB_ABORT;
121
122 c->n_thread++;
123
124 return DWARF_CB_OK;
125 }
126
127 int coredump_make_stack_trace(int fd, const char *executable, char **ret) {
128
129 static const Dwfl_Callbacks callbacks = {
130 .find_elf = dwfl_build_id_find_elf,
131 .find_debuginfo = dwfl_standard_find_debuginfo,
132 };
133
134 struct stack_context c = {};
135 char *buf = NULL;
136 size_t sz = 0;
137 int r;
138
139 assert(fd >= 0);
140 assert(ret);
141
142 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
143 return -errno;
144
145 c.f = open_memstream(&buf, &sz);
146 if (!c.f)
147 return -ENOMEM;
148
149 (void) __fsetlocking(c.f, FSETLOCKING_BYCALLER);
150
151 elf_version(EV_CURRENT);
152
153 c.elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
154 if (!c.elf) {
155 r = -EINVAL;
156 goto finish;
157 }
158
159 c.dwfl = dwfl_begin(&callbacks);
160 if (!c.dwfl) {
161 r = -EINVAL;
162 goto finish;
163 }
164
165 if (dwfl_core_file_report(c.dwfl, c.elf, executable) < 0) {
166 r = -EINVAL;
167 goto finish;
168 }
169
170 if (dwfl_report_end(c.dwfl, NULL, NULL) != 0) {
171 r = -EINVAL;
172 goto finish;
173 }
174
175 if (dwfl_core_file_attach(c.dwfl, c.elf) < 0) {
176 r = -EINVAL;
177 goto finish;
178 }
179
180 if (dwfl_getthreads(c.dwfl, thread_callback, &c) < 0) {
181 r = -EINVAL;
182 goto finish;
183 }
184
185 c.f = safe_fclose(c.f);
186
187 *ret = buf;
188 buf = NULL;
189
190 r = 0;
191
192 finish:
193 if (c.dwfl)
194 dwfl_end(c.dwfl);
195
196 if (c.elf)
197 elf_end(c.elf);
198
199 safe_fclose(c.f);
200
201 free(buf);
202
203 return r;
204 }