2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
39 #include <sys/linker.h>
40 #include <sys/malloc.h>
42 #include <sys/stack.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
46 FEATURE(stack, "Support for capturing kernel stack");
48 static MALLOC_DEFINE(M_STACK, "stack", "Stack Traces");
50 static int stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen,
52 static int stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset);
55 stack_create(int flags)
59 st = malloc(sizeof(*st), M_STACK, flags | M_ZERO);
64 stack_destroy(struct stack *st)
71 stack_put(struct stack *st, vm_offset_t pc)
74 if (st->depth < STACK_MAX) {
75 st->pcs[st->depth++] = pc;
82 stack_copy(const struct stack *src, struct stack *dst)
89 stack_zero(struct stack *st)
92 bzero(st, sizeof *st);
96 stack_print(const struct stack *st)
102 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
103 for (i = 0; i < st->depth; i++) {
104 (void)stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
106 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
112 stack_print_short(const struct stack *st)
118 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
119 for (i = 0; i < st->depth; i++) {
122 if (stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
124 printf("%s+%#lx", namebuf, offset);
126 printf("%p", (void *)st->pcs[i]);
132 stack_print_ddb(const struct stack *st)
138 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
139 for (i = 0; i < st->depth; i++) {
140 stack_symbol_ddb(st->pcs[i], &name, &offset);
141 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
146 #if defined(DDB) || defined(WITNESS)
148 stack_print_short_ddb(const struct stack *st)
154 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
155 for (i = 0; i < st->depth; i++) {
158 if (stack_symbol_ddb(st->pcs[i], &name, &offset) == 0)
159 printf("%s+%#lx", name, offset);
161 printf("%p", (void *)st->pcs[i]);
168 * Two print routines -- one for use from DDB and DDB-like contexts, the
169 * other for use in the live kernel.
172 stack_sbuf_print(struct sbuf *sb, const struct stack *st)
178 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
179 for (i = 0; i < st->depth; i++) {
180 (void)stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
182 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
187 #if defined(DDB) || defined(WITNESS)
189 stack_sbuf_print_ddb(struct sbuf *sb, const struct stack *st)
195 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
196 for (i = 0; i < st->depth; i++) {
197 (void)stack_symbol_ddb(st->pcs[i], &name, &offset);
198 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
206 stack_ktr(u_int mask, const char *file, int line, const struct stack *st,
207 u_int depth, int cheap)
215 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
217 ktr_tracepoint(mask, file, line, "#0 %p %p %p %p %p %p",
218 st->pcs[0], st->pcs[1], st->pcs[2], st->pcs[3],
219 st->pcs[4], st->pcs[5]);
222 ktr_tracepoint(mask, file, line, "#1 %p %p %p %p %p %p",
223 st->pcs[6], st->pcs[7], st->pcs[8], st->pcs[9],
224 st->pcs[10], st->pcs[11]);
227 ktr_tracepoint(mask, file, line, "#2 %p %p %p %p %p %p",
228 st->pcs[12], st->pcs[13], st->pcs[14], st->pcs[15],
229 st->pcs[16], st->pcs[17]);
232 if (depth == 0 || st->depth < depth)
234 for (i = 0; i < depth; i++) {
235 (void)stack_symbol_ddb(st->pcs[i], &name, &offset);
236 ktr_tracepoint(mask, file, line, "#%d %p at %s+%#lx",
237 i, st->pcs[i], (u_long)name, offset, 0, 0);
245 * Two variants of stack symbol lookup -- one that uses the DDB interfaces
246 * and bypasses linker locking, and the other that doesn't.
249 stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen, long *offset)
252 if (linker_search_symbol_name((caddr_t)pc, namebuf, buflen,
255 strlcpy(namebuf, "??", buflen);
262 stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset)
264 linker_symval_t symval;
267 if (linker_ddb_search_symbol((caddr_t)pc, &sym, offset) != 0)
269 if (linker_ddb_symbol_values(sym, &symval) != 0)
271 if (symval.name != NULL) {