]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_stack.c
This commit was generated by cvs2svn to compensate for changes in r171831,
[FreeBSD/FreeBSD.git] / sys / kern / subr_stack.c
1 /*-
2  * Copyright (c) 2005 Antoine Brodin
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #ifdef KTR
33 #include <sys/ktr.h>
34 #endif
35 #include <sys/linker.h>
36 #include <sys/malloc.h>
37 #include <sys/sbuf.h>
38 #include <sys/stack.h>
39 #include <sys/systm.h>
40
41 MALLOC_DEFINE(M_STACK, "stack", "Stack Traces");
42
43 static void stack_symbol(vm_offset_t pc, const char **name, long *offset);
44
45 struct stack *
46 stack_create(void)
47 {
48         struct stack *st;
49
50         st = malloc(sizeof *st, M_STACK, M_WAITOK | M_ZERO);
51         return (st);
52 }
53
54 void
55 stack_destroy(struct stack *st)
56 {
57
58         free(st, M_STACK);
59 }
60
61 int
62 stack_put(struct stack *st, vm_offset_t pc)
63 {
64
65         if (st->depth < STACK_MAX) {
66                 st->pcs[st->depth++] = pc;
67                 return (0);
68         } else
69                 return (-1);
70 }
71
72 void
73 stack_copy(struct stack *src, struct stack *dst)
74 {
75
76         *dst = *src;
77 }
78
79 void
80 stack_zero(struct stack *st)
81 {
82
83         bzero(st, sizeof *st);
84 }
85
86 void
87 stack_print(struct stack *st)
88 {
89         const char *name;
90         long offset;
91         int i;
92
93         KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
94         for (i = 0; i < st->depth; i++) {
95                 stack_symbol(st->pcs[i], &name, &offset);
96                 printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
97                     name, offset);
98         }
99 }
100
101 void
102 stack_sbuf_print(struct sbuf *sb, struct stack *st)
103 {
104         const char *name;
105         long offset;
106         int i;
107
108         KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
109         for (i = 0; i < st->depth; i++) {
110                 stack_symbol(st->pcs[i], &name, &offset);
111                 sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
112                     name, offset);
113         }
114 }
115
116 #ifdef KTR
117 void
118 stack_ktr(u_int mask, const char *file, int line, struct stack *st, u_int depth,
119     int cheap)
120 {
121         const char *name;
122         long offset;
123         int i;
124
125         KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
126         if (cheap) {
127                 ktr_tracepoint(mask, file, line, "#0 %p %p %p %p %p %p",
128                     st->pcs[0], st->pcs[1], st->pcs[2], st->pcs[3],
129                     st->pcs[4], st->pcs[5]);
130                 if (st->depth <= 6)
131                         return;
132                 ktr_tracepoint(mask, file, line, "#1 %p %p %p %p %p %p",
133                     st->pcs[6], st->pcs[7], st->pcs[8], st->pcs[9],
134                     st->pcs[10], st->pcs[11]);
135                 if (st->depth <= 12)
136                         return;
137                 ktr_tracepoint(mask, file, line, "#2 %p %p %p %p %p %p",
138                     st->pcs[12], st->pcs[13], st->pcs[14], st->pcs[15],
139                     st->pcs[16], st->pcs[17]);
140         } else {
141                 if (depth == 0 || st->depth < depth)
142                         depth = st->depth;
143                 for (i = 0; i < depth; i++) {
144                         stack_symbol(st->pcs[i], &name, &offset);
145                         ktr_tracepoint(mask, file, line, "#%d %p at %s+%#lx",
146                             i, st->pcs[i], (u_long)name, offset, 0, 0);
147                 }
148         }
149 }
150 #endif
151
152 static void
153 stack_symbol(vm_offset_t pc, const char **name, long *offset)
154 {
155         linker_symval_t symval;
156         c_linker_sym_t sym;
157
158         if (linker_ddb_search_symbol((caddr_t)pc, &sym, offset) != 0)
159                 goto out;
160         if (linker_ddb_symbol_values(sym, &symval) != 0)
161                 goto out;
162         if (symval.name != NULL) {
163                 *name = symval.name;
164                 return;
165         }
166 out:
167         *offset = 0;
168         *name = "Unknown func";
169 }