]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/stacktrace.c
tty-ask-password: Split out password sending
[thirdparty/systemd.git] / src / journal / stacktrace.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <dwarf.h>
23 #include <elfutils/libdwfl.h>
24
25 #include "alloc-util.h"
26 #include "fd-util.h"
27 #include "formats-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 elf_version(EV_CURRENT);
150
151 c.elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
152 if (!c.elf) {
153 r = -EINVAL;
154 goto finish;
155 }
156
157 c.dwfl = dwfl_begin(&callbacks);
158 if (!c.dwfl) {
159 r = -EINVAL;
160 goto finish;
161 }
162
163 if (dwfl_core_file_report(c.dwfl, c.elf, executable) < 0) {
164 r = -EINVAL;
165 goto finish;
166 }
167
168 if (dwfl_report_end(c.dwfl, NULL, NULL) != 0) {
169 r = -EINVAL;
170 goto finish;
171 }
172
173 if (dwfl_core_file_attach(c.dwfl, c.elf) < 0) {
174 r = -EINVAL;
175 goto finish;
176 }
177
178 if (dwfl_getthreads(c.dwfl, thread_callback, &c) < 0) {
179 r = -EINVAL;
180 goto finish;
181 }
182
183 c.f = safe_fclose(c.f);
184
185 *ret = buf;
186 buf = NULL;
187
188 r = 0;
189
190 finish:
191 if (c.dwfl)
192 dwfl_end(c.dwfl);
193
194 if (c.elf)
195 elf_end(c.elf);
196
197 safe_fclose(c.f);
198
199 free(buf);
200
201 return r;
202 }