2 * Copyright (c) 2005 Antoine Brodin
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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>
43 static MALLOC_DEFINE(M_STACK, "stack", "Stack Traces");
45 static int stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen,
47 static int stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset);
54 st = malloc(sizeof *st, M_STACK, M_WAITOK | M_ZERO);
59 stack_destroy(struct stack *st)
66 stack_put(struct stack *st, vm_offset_t pc)
69 if (st->depth < STACK_MAX) {
70 st->pcs[st->depth++] = pc;
77 stack_copy(struct stack *src, struct stack *dst)
84 stack_zero(struct stack *st)
87 bzero(st, sizeof *st);
91 stack_print(struct stack *st)
97 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
98 for (i = 0; i < st->depth; i++) {
99 (void)stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
101 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
107 stack_print_short(struct stack *st)
113 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
114 for (i = 0; i < st->depth; i++) {
117 if (stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
119 printf("%s+%#lx", namebuf, offset);
121 printf("%p", (void *)st->pcs[i]);
127 stack_print_ddb(struct stack *st)
133 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
134 for (i = 0; i < st->depth; i++) {
135 stack_symbol_ddb(st->pcs[i], &name, &offset);
136 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
143 stack_print_short_ddb(struct stack *st)
149 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
150 for (i = 0; i < st->depth; i++) {
153 if (stack_symbol_ddb(st->pcs[i], &name, &offset) == 0)
154 printf("%s+%#lx", name, offset);
156 printf("%p", (void *)st->pcs[i]);
163 * Two print routines -- one for use from DDB and DDB-like contexts, the
164 * other for use in the live kernel.
167 stack_sbuf_print(struct sbuf *sb, struct stack *st)
173 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
174 for (i = 0; i < st->depth; i++) {
175 (void)stack_symbol(st->pcs[i], namebuf, sizeof(namebuf),
177 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
184 stack_sbuf_print_ddb(struct sbuf *sb, struct stack *st)
190 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
191 for (i = 0; i < st->depth; i++) {
192 (void)stack_symbol_ddb(st->pcs[i], &name, &offset);
193 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
201 stack_ktr(u_int mask, const char *file, int line, struct stack *st, u_int depth,
210 KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
212 ktr_tracepoint(mask, file, line, "#0 %p %p %p %p %p %p",
213 st->pcs[0], st->pcs[1], st->pcs[2], st->pcs[3],
214 st->pcs[4], st->pcs[5]);
217 ktr_tracepoint(mask, file, line, "#1 %p %p %p %p %p %p",
218 st->pcs[6], st->pcs[7], st->pcs[8], st->pcs[9],
219 st->pcs[10], st->pcs[11]);
222 ktr_tracepoint(mask, file, line, "#2 %p %p %p %p %p %p",
223 st->pcs[12], st->pcs[13], st->pcs[14], st->pcs[15],
224 st->pcs[16], st->pcs[17]);
227 if (depth == 0 || st->depth < depth)
229 for (i = 0; i < depth; i++) {
230 (void)stack_symbol_ddb(st->pcs[i], &name, &offset);
231 ktr_tracepoint(mask, file, line, "#%d %p at %s+%#lx",
232 i, st->pcs[i], (u_long)name, offset, 0, 0);
240 * Two variants of stack symbol lookup -- one that uses the DDB interfaces
241 * and bypasses linker locking, and the other that doesn't.
244 stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen, long *offset)
247 if (linker_search_symbol_name((caddr_t)pc, namebuf, buflen,
250 strlcpy(namebuf, "??", buflen);
257 stack_symbol_ddb(vm_offset_t pc, const char **name, long *offset)
259 linker_symval_t symval;
262 if (linker_ddb_search_symbol((caddr_t)pc, &sym, offset) != 0)
264 if (linker_ddb_symbol_values(sym, &symval) != 0)
266 if (symval.name != NULL) {