1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005 Antoine Brodin
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "opt_ddb.h"
30
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #ifdef KTR
35 #include <sys/ktr.h>
36 #endif
37 #include <sys/linker.h>
38 #include <sys/malloc.h>
39 #include <sys/sbuf.h>
40 #include <sys/stack.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43
44 FEATURE(stack, "Support for capturing kernel stack");
45
46 MALLOC_DEFINE(M_STACK, "stack", "Stack Traces");
47
48 static int stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen,
49 long *offset, int flags);
50 static int stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset);
51
52 struct stack *
stack_create(int flags)53 stack_create(int flags)
54 {
55 struct stack *st;
56
57 st = malloc(sizeof(*st), M_STACK, flags | M_ZERO);
58 return (st);
59 }
60
61 void
stack_destroy(struct stack * st)62 stack_destroy(struct stack *st)
63 {
64
65 free(st, M_STACK);
66 }
67
68 int
stack_put(struct stack * st,vm_offset_t pc)69 stack_put(struct stack *st, vm_offset_t pc)
70 {
71
72 if (st->depth < STACK_MAX) {
73 st->pcs[st->depth++] = pc;
74 return (0);
75 } else
76 return (-1);
77 }
78
79 void
stack_copy(const struct stack * src,struct stack * dst)80 stack_copy(const struct stack *src, struct stack *dst)
81 {
82
83 *dst = *src;
84 }
85
86 void
stack_zero(struct stack * st)87 stack_zero(struct stack *st)
88 {
89
90 bzero(st, sizeof *st);
91 }
92
93 void
stack_print(const struct stack * st)94 stack_print(const struct stack *st)
95 {
96 char namebuf[64];
97 long offset;
98 int i;
99
100 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
101 for (i = 0; i < st->depth; i++) {
102 (void)stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
103 &offset, M_WAITOK);
104 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
105 namebuf, offset);
106 }
107 }
108
109 void
stack_print_short(const struct stack * st)110 stack_print_short(const struct stack *st)
111 {
112 char namebuf[64];
113 long offset;
114 int i;
115
116 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
117 for (i = 0; i < st->depth; i++) {
118 if (i > 0)
119 printf(" ");
120 if (stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
121 &offset, M_WAITOK) == 0)
122 printf("%s+%#lx", namebuf, offset);
123 else
124 printf("%p", (void *)st->pcs[i]);
125 }
126 printf("\n");
127 }
128
129 void
stack_print_ddb(const struct stack * st)130 stack_print_ddb(const struct stack *st)
131 {
132 const char *name;
133 long offset;
134 int i;
135
136 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
137 for (i = 0; i < st->depth; i++) {
138 stack_symbol_ddb(st->pcs[i], &name, &offset);
139 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
140 name, offset);
141 }
142 }
143
144 #if defined(DDB) || defined(WITNESS)
145 void
stack_print_short_ddb(const struct stack * st)146 stack_print_short_ddb(const struct stack *st)
147 {
148 const char *name;
149 long offset;
150 int i;
151
152 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
153 for (i = 0; i < st->depth; i++) {
154 if (i > 0)
155 printf(" ");
156 if (stack_symbol_ddb(st->pcs[i], &name, &offset) == 0)
157 printf("%s+%#lx", name, offset);
158 else
159 printf("%p", (void *)st->pcs[i]);
160 }
161 printf("\n");
162 }
163 #endif
164
165 /*
166 * Format stack into sbuf from live kernel.
167 *
168 * flags - M_WAITOK or M_NOWAIT (EWOULDBLOCK).
169 */
170 int
stack_sbuf_print_flags(struct sbuf * sb,const struct stack * st,int flags,enum stack_sbuf_fmt format)171 stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags,
172 enum stack_sbuf_fmt format)
173 {
174 char namebuf[64];
175 long offset;
176 int i, error;
177
178 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
179 for (i = 0; i < st->depth; i++) {
180 error = stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
181 &offset, flags);
182 if (error == EWOULDBLOCK)
183 return (error);
184 switch (format) {
185 case STACK_SBUF_FMT_LONG:
186 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i,
187 (void *)st->pcs[i], namebuf, offset);
188 break;
189 case STACK_SBUF_FMT_COMPACT:
190 sbuf_printf(sb, "%s+%#lx ", namebuf, offset);
191 break;
192 default:
193 __assert_unreachable();
194 }
195 }
196 sbuf_nl_terminate(sb);
197 return (0);
198 }
199
200 void
stack_sbuf_print(struct sbuf * sb,const struct stack * st)201 stack_sbuf_print(struct sbuf *sb, const struct stack *st)
202 {
203
204 (void)stack_sbuf_print_flags(sb, st, M_WAITOK, STACK_SBUF_FMT_LONG);
205 }
206
207 #if defined(DDB) || defined(WITNESS)
208 void
stack_sbuf_print_ddb(struct sbuf * sb,const struct stack * st)209 stack_sbuf_print_ddb(struct sbuf *sb, const struct stack *st)
210 {
211 const char *name;
212 long offset;
213 int i;
214
215 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
216 for (i = 0; i < st->depth; i++) {
217 (void)stack_symbol_ddb(st->pcs[i], &name, &offset);
218 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
219 name, offset);
220 }
221 }
222 #endif
223
224 #ifdef KTR
225 void
stack_ktr(u_int mask,const char * file,int line,const struct stack * st,u_int depth)226 stack_ktr(u_int mask, const char *file, int line, const struct stack *st,
227 u_int depth)
228 {
229 #ifdef DDB
230 const char *name;
231 long offset;
232 int i;
233 #endif
234
235 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
236 #ifdef DDB
237 if (depth == 0 || st->depth < depth)
238 depth = st->depth;
239 for (i = 0; i < depth; i++) {
240 (void)stack_symbol_ddb(st->pcs[i], &name, &offset);
241 ktr_tracepoint(mask, file, line, "#%d %p at %s+%#lx",
242 i, st->pcs[i], (u_long)name, offset, 0, 0);
243 }
244 #endif
245 }
246 #endif
247
248 /*
249 * Two variants of stack symbol lookup -- one that uses the DDB interfaces
250 * and bypasses linker locking, and the other that doesn't.
251 */
252 static int
stack_symbol(vm_offset_t pc,char * namebuf,u_int buflen,long * offset,int flags)253 stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen, long *offset,
254 int flags)
255 {
256 int error;
257
258 error = linker_search_symbol_name_flags((caddr_t)pc, namebuf, buflen,
259 offset, flags);
260 if (error == 0 || error == EWOULDBLOCK)
261 return (error);
262
263 *offset = 0;
264 strlcpy(namebuf, "??", buflen);
265 return (ENOENT);
266 }
267
268 static int
stack_symbol_ddb(vm_offset_t pc,const char ** name,long * offset)269 stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset)
270 {
271 linker_symval_t symval;
272 c_linker_sym_t sym;
273
274 if (linker_ddb_search_symbol((caddr_t)pc, &sym, offset) != 0)
275 goto out;
276 if (linker_ddb_symbol_values(sym, &symval) != 0)
277 goto out;
278 if (symval.name != NULL) {
279 *name = symval.name;
280 return (0);
281 }
282 out:
283 *offset = 0;
284 *name = "??";
285 return (ENOENT);
286 }
287