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