xref: /freebsd/sys/kern/subr_stack.c (revision db612abe8df3355d1eb23bb3b50fdd97bc21e979)
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 "opt_ddb.h"
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #ifdef KTR
35 #include <sys/ktr.h>
36 #endif
37 #include <sys/linker.h>
38 #include <sys/malloc.h>
39 #include <sys/sbuf.h>
40 #include <sys/stack.h>
41 #include <sys/systm.h>
42 
43 MALLOC_DEFINE(M_STACK, "stack", "Stack Traces");
44 
45 static void stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen,
46 	    long *offset);
47 #ifdef DDB
48 static void stack_symbol_ddb(vm_offset_t pc, char *namebuf, u_int buflen,
49 	    long *offset);
50 #endif
51 
52 struct stack *
53 stack_create(void)
54 {
55 	struct stack *st;
56 
57 	st = malloc(sizeof *st, M_STACK, M_WAITOK | M_ZERO);
58 	return (st);
59 }
60 
61 void
62 stack_destroy(struct stack *st)
63 {
64 
65 	free(st, M_STACK);
66 }
67 
68 int
69 stack_put(struct stack *st, vm_offset_t pc)
70 {
71 
72 	if (st->depth < STACK_MAX) {
73 		st->pcs[st->depth++] = pc;
74 		return (0);
75 	} else
76 		return (-1);
77 }
78 
79 void
80 stack_copy(struct stack *src, struct stack *dst)
81 {
82 
83 	*dst = *src;
84 }
85 
86 void
87 stack_zero(struct stack *st)
88 {
89 
90 	bzero(st, sizeof *st);
91 }
92 
93 void
94 stack_print(struct stack *st)
95 {
96 	char namebuf[64];
97 	long offset;
98 	int i;
99 
100 	KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
101 	for (i = 0; i < st->depth; i++) {
102 		stack_symbol(st->pcs[i], namebuf, sizeof(namebuf), &offset);
103 		printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
104 		    namebuf, offset);
105 	}
106 }
107 
108 #ifdef DDB
109 void
110 stack_print_ddb(struct stack *st)
111 {
112 	char namebuf[64];
113 	long offset;
114 	int i;
115 
116 	KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
117 	for (i = 0; i < st->depth; i++) {
118 		stack_symbol_ddb(st->pcs[i], namebuf, sizeof(namebuf),
119 		    &offset);
120 		printf("#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
121 		    namebuf, offset);
122 	}
123 }
124 #endif
125 
126 /*
127  * Two print routines -- one for use from DDB and DDB-like contexts, the
128  * other for use in the live kernel.
129  */
130 void
131 stack_sbuf_print(struct sbuf *sb, struct stack *st)
132 {
133 	char namebuf[64];
134 	long offset;
135 	int i;
136 
137 	KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
138 	for (i = 0; i < st->depth; i++) {
139 		stack_symbol(st->pcs[i], namebuf, sizeof(namebuf), &offset);
140 		sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
141 		    namebuf, offset);
142 	}
143 }
144 
145 #ifdef DDB
146 void
147 stack_sbuf_print_ddb(struct sbuf *sb, struct stack *st)
148 {
149 	char namebuf[64];
150 	long offset;
151 	int i;
152 
153 	KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
154 	for (i = 0; i < st->depth; i++) {
155 		stack_symbol_ddb(st->pcs[i], namebuf, sizeof(namebuf),
156 		    &offset);
157 		sbuf_printf(sb, "#%d %p at %s+%#lx\n", i, (void *)st->pcs[i],
158 		    namebuf, offset);
159 	}
160 }
161 
162 #ifdef KTR
163 void
164 stack_ktr(u_int mask, const char *file, int line, struct stack *st, u_int depth,
165     int cheap)
166 {
167 	char namebuf[64];
168 	long offset;
169 	int i;
170 
171 	KASSERT(st->depth <= STACK_MAX, ("bogus stack"));
172 	if (cheap) {
173 		ktr_tracepoint(mask, file, line, "#0 %p %p %p %p %p %p",
174 		    st->pcs[0], st->pcs[1], st->pcs[2], st->pcs[3],
175 		    st->pcs[4], st->pcs[5]);
176 		if (st->depth <= 6)
177 			return;
178 		ktr_tracepoint(mask, file, line, "#1 %p %p %p %p %p %p",
179 		    st->pcs[6], st->pcs[7], st->pcs[8], st->pcs[9],
180 		    st->pcs[10], st->pcs[11]);
181 		if (st->depth <= 12)
182 			return;
183 		ktr_tracepoint(mask, file, line, "#2 %p %p %p %p %p %p",
184 		    st->pcs[12], st->pcs[13], st->pcs[14], st->pcs[15],
185 		    st->pcs[16], st->pcs[17]);
186 	} else {
187 		if (depth == 0 || st->depth < depth)
188 			depth = st->depth;
189 		for (i = 0; i < depth; i++) {
190 			stack_symbol_ddb(st->pcs[i], namebuf,
191 			    sizeof(namebuf), &offset);
192 			ktr_tracepoint(mask, file, line, "#%d %p at %s+%#lx",
193 			    i, st->pcs[i], (u_long)namebuf, offset, 0, 0);
194 		}
195 	}
196 }
197 #endif
198 #endif
199 
200 /*
201  * Two variants of stack symbol lookup -- one that uses the DDB interfaces
202  * and bypasses linker locking, and the other that doesn't.
203  */
204 static void
205 stack_symbol(vm_offset_t pc, char *namebuf, u_int buflen, long *offset)
206 {
207 
208 	if (linker_search_symbol_name((caddr_t)pc, namebuf, buflen,
209 	    offset) != 0) {
210 		*offset = 0;
211 		strlcpy(namebuf, "??", buflen);
212 	}
213 }
214 
215 #ifdef DDB
216 static void
217 stack_symbol_ddb(vm_offset_t pc, char *namebuf, u_int buflen, long *offset)
218 {
219 
220 	if (linker_ddb_search_symbol_name((caddr_t)pc, namebuf, buflen,
221 	    offset) != 0) {
222 		*offset = 0;
223 		strlcpy(namebuf, "??", buflen);
224 	};
225 }
226 #endif
227