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/param.h>
32 #include <sys/kernel.h>
36 #include <sys/linker.h>
37 #include <sys/malloc.h>
39 #include <sys/stack.h>
40 #include <sys/systm.h>
41 #include <sys/sysctl.h>
43 FEATURE(stack, "Support for capturing kernel stack");
45 MALLOC_DEFINE(M_STACK, "stack", "Stack Traces");
47 static int stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen,
48 long *offset, int flags);
49 static int stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset);
52 stack_create(int flags)
56 st = malloc(sizeof(*st), M_STACK, flags | M_ZERO);
61 stack_destroy(struct stack *st)
68 stack_put(struct stack *st, vm_offset_t pc)
71 if (st->depth < STACK_MAX) {
72 st->pcs[st->depth++] = pc;
79 stack_copy(const struct stack *src, struct stack *dst)
86 stack_zero(struct stack *st)
89 bzero(st, sizeof *st);
93 stack_print(const struct stack *st)
99 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
100 for (i = 0; i < st->depth; i++) {
101 (void)stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
103 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
109 stack_print_short(const struct stack *st)
115 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
116 for (i = 0; i < st->depth; i++) {
119 if (stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
120 &offset, M_WAITOK) == 0)
121 printf("%s+%#lx", namebuf, offset);
123 printf("%p", (void *)st->pcs[i]);
129 stack_print_ddb(const struct stack *st)
135 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
136 for (i = 0; i < st->depth; i++) {
137 stack_symbol_ddb(st->pcs[i], &name, &offset);
138 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
143 #if defined(DDB) || defined(WITNESS)
145 stack_print_short_ddb(const struct stack *st)
151 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
152 for (i = 0; i < st->depth; i++) {
155 if (stack_symbol_ddb(st->pcs[i], &name, &offset) == 0)
156 printf("%s+%#lx", name, offset);
158 printf("%p", (void *)st->pcs[i]);
165 * Format stack into sbuf from live kernel.
167 * flags - M_WAITOK or M_NOWAIT (EWOULDBLOCK).
170 stack_sbuf_print_flags(struct sbuf *sb, const struct stack *st, int flags,
171 enum stack_sbuf_fmt format)
177 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
178 for (i = 0; i < st->depth; i++) {
179 error = stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
181 if (error == EWOULDBLOCK)
184 case STACK_SBUF_FMT_LONG:
185 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i,
186 (void *)st->pcs[i], namebuf, offset);
188 case STACK_SBUF_FMT_COMPACT:
189 sbuf_printf(sb, "%s+%#lx ", namebuf, offset);
192 __assert_unreachable();
195 sbuf_nl_terminate(sb);
200 stack_sbuf_print(struct sbuf *sb, const struct stack *st)
203 (void)stack_sbuf_print_flags(sb, st, M_WAITOK, STACK_SBUF_FMT_LONG);
206 #if defined(DDB) || defined(WITNESS)
208 stack_sbuf_print_ddb(struct sbuf *sb, const struct stack *st)
214 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
215 for (i = 0; i < st->depth; i++) {
216 (void)stack_symbol_ddb(st->pcs[i], &name, &offset);
217 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
225 stack_ktr(u_int mask, const char *file, int line, const struct stack *st,
234 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
236 if (depth == 0 || st->depth < depth)
238 for (i = 0; i < depth; i++) {
239 (void)stack_symbol_ddb(st->pcs[i], &name, &offset);
240 ktr_tracepoint(mask, file, line, "#%d %p at %s+%#lx",
241 i, st->pcs[i], (u_long)name, offset, 0, 0);
248 * Two variants of stack symbol lookup -- one that uses the DDB interfaces
249 * and bypasses linker locking, and the other that doesn't.
252 stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen, long *offset,
257 error = linker_search_symbol_name_flags((caddr_t)pc, namebuf, buflen,
259 if (error == 0 || error == EWOULDBLOCK)
263 strlcpy(namebuf, "??", buflen);
268 stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset)
270 linker_symval_t symval;
273 if (linker_ddb_search_symbol((caddr_t)pc, &sym, offset) != 0)
275 if (linker_ddb_symbol_values(sym, &symval) != 0)
277 if (symval.name != NULL) {