2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2005 Antoine Brodin
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/kernel.h>
37 #include <sys/linker.h>
38 #include <sys/malloc.h>
40 #include <sys/stack.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
44 FEATURE(stack, "Support for capturing kernel stack");
46 MALLOC_DEFINE(M_STACK, "stack", "Stack Traces");
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);
53 stack_create(int flags)
57 st = malloc(sizeof(*st), M_STACK, flags | M_ZERO);
62 stack_destroy(struct stack *st)
69 stack_put(struct stack *st, vm_offset_t pc)
72 if (st->depth < STACK_MAX) {
73 st->pcs[st->depth++] = pc;
80 stack_copy(const struct stack *src, struct stack *dst)
87 stack_zero(struct stack *st)
90 bzero(st, sizeof *st);
94 stack_print(const struct stack *st)
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),
104 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
110 stack_print_short(const struct stack *st)
116 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
117 for (i = 0; i < st->depth; i++) {
120 if (stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
121 &offset, M_WAITOK) == 0)
122 printf("%s+%#lx", namebuf, offset);
124 printf("%p", (void *)st->pcs[i]);
130 stack_print_ddb(const struct stack *st)
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],
144 #if defined(DDB) || defined(WITNESS)
146 stack_print_short_ddb(const struct stack *st)
152 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
153 for (i = 0; i < st->depth; i++) {
156 if (stack_symbol_ddb(st->pcs[i], &name, &offset) == 0)
157 printf("%s+%#lx", name, offset);
159 printf("%p", (void *)st->pcs[i]);
166 * Format stack into sbuf from live kernel.
168 * flags - M_WAITOK or M_NOWAIT (EWOULDBLOCK).
171 stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags,
172 enum stack_sbuf_fmt format)
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),
182 if (error == EWOULDBLOCK)
185 case STACK_SBUF_FMT_LONG:
186 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i,
187 (void *)st->pcs[i], namebuf, offset);
189 case STACK_SBUF_FMT_COMPACT:
190 sbuf_printf(sb, "%s+%#lx ", namebuf, offset);
193 __assert_unreachable();
196 sbuf_nl_terminate(sb);
201 stack_sbuf_print(struct sbuf *sb, const struct stack *st)
204 (void)stack_sbuf_print_flags(sb, st, M_WAITOK, STACK_SBUF_FMT_LONG);
207 #if defined(DDB) || defined(WITNESS)
209 stack_sbuf_print_ddb(struct sbuf *sb, const struct stack *st)
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],
226 stack_ktr(u_int mask, const char *file, int line, const struct stack *st,
235 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
237 if (depth == 0 || st->depth < 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);
249 * Two variants of stack symbol lookup -- one that uses the DDB interfaces
250 * and bypasses linker locking, and the other that doesn't.
253 stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen, long *offset,
258 error = linker_search_symbol_name_flags((caddr_t)pc, namebuf, buflen,
260 if (error == 0 || error == EWOULDBLOCK)
264 strlcpy(namebuf, "??", buflen);
269 stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset)
271 linker_symval_t symval;
274 if (linker_ddb_search_symbol((caddr_t)pc, &sym, offset) != 0)
276 if (linker_ddb_symbol_values(sym, &symval) != 0)
278 if (symval.name != NULL) {