Restructure autoconf around ./config directory
[zfs.git] / zfs / lib / libumem / getpcstack.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Portions Copyright 2006 OmniTI, Inc.
28  */
29
30 /* #pragma ident        "@(#)getpcstack.c       1.5     05/06/08 SMI" */
31
32 #include "config.h"
33 #include "misc.h"
34
35 #if HAVE_UCONTEXT_H
36 #include <ucontext.h>
37 #endif
38
39 #if HAVE_SYS_FRAME_H
40 #include <sys/frame.h>
41 #endif
42 #if HAVE_SYS_STACK_H
43 #include <sys/stack.h>
44 #endif
45
46 #include <stdio.h>
47
48 #if defined(__MACH__)
49 /*
50  * Darwin doesn't have any exposed frame info, so give it some space.
51  */
52 #define UMEM_FRAMESIZE (2 * sizeof(long long))
53
54 #elif defined(__sparc) || defined(__sparcv9)
55 extern void flush_windows(void);
56 #define UMEM_FRAMESIZE  MINFRAME
57
58 #elif defined(__i386) || defined(__amd64)
59 /*
60  * On x86, MINFRAME is defined to be 0, but we want to be sure we can
61  * dereference the entire frame structure.
62  */
63 #define UMEM_FRAMESIZE  (sizeof (struct frame))
64
65 #elif !defined(EC_UMEM_DUMMY_PCSTACK)
66 #error needs update for new architecture
67 #endif
68
69 /*
70  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
71  * Returns MIN(current stack depth, pcstack_limit).
72  */
73 /*ARGSUSED*/
74 int
75 getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal)
76 {
77 #ifdef EC_UMEM_DUMMY_PCSTACK
78   return 0;
79 #else
80         struct frame *fp;
81         struct frame *nextfp, *minfp;
82         int depth = 0;
83         uintptr_t base = 0;
84         size_t size = 0;
85 #ifndef UMEM_STANDALONE
86         int on_altstack = 0;
87         uintptr_t sigbase = 0;
88         size_t sigsize = 0;
89
90         stack_t st;
91
92         if (stack_getbounds(&st) != 0) {
93                 if (thr_stksegment(&st) != 0 ||
94                     (uintptr_t)st.ss_sp < st.ss_size) {
95                         return (0);             /* unable to get stack bounds */
96                 }
97                 /*
98                  * thr_stksegment(3C) has a slightly different interface than
99                  * stack_getbounds(3C) -- correct it
100                  */
101                 st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size);
102                 st.ss_flags = 0;                /* can't be on-stack */
103         }
104         on_altstack = (st.ss_flags & SS_ONSTACK);
105
106         if (st.ss_size != 0) {
107                 base = (uintptr_t)st.ss_sp;
108                 size = st.ss_size;
109         } else {
110                 /*
111                  * If size == 0, then ss_sp is the *top* of the stack.
112                  *
113                  * Since we only allow increasing frame pointers, and we
114                  * know our caller set his up correctly, we can treat ss_sp
115                  * as an upper bound safely.
116                  */
117                 base = 0;
118                 size = (uintptr_t)st.ss_sp;
119         }
120
121         if (check_signal != 0) {
122                 void (*sigfunc)() = NULL;
123                 int sigfuncsize = 0;
124                 extern void thr_sighndlrinfo(void (**)(), int *);
125
126                 thr_sighndlrinfo(&sigfunc, &sigfuncsize);
127                 sigbase = (uintptr_t)sigfunc;
128                 sigsize = sigfuncsize;
129         }
130 #else /* UMEM_STANDALONE */
131         base = (uintptr_t)umem_min_stack;
132         size = umem_max_stack - umem_min_stack;
133 #endif
134
135         /*
136          * shorten size so that fr_savfp and fr_savpc will be within the stack
137          * bounds.
138          */
139         if (size >= UMEM_FRAMESIZE - 1)
140                 size -= (UMEM_FRAMESIZE - 1);
141         else
142                 size = 0;
143
144 #if defined(__sparc) || defined(__sparcv9)
145         flush_windows();
146 #endif
147
148         /* LINTED alignment */
149         fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS);
150
151         minfp = fp;
152
153         if (((uintptr_t)fp - base) >= size)
154                 return (0);     /* the frame pointer isn't in our stack */
155
156         while (depth < pcstack_limit) {
157                 uintptr_t tmp;
158
159                 /* LINTED alignment */
160                 nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
161                 tmp = (uintptr_t)nextfp;
162
163                 /*
164                  * Check nextfp for validity.  It must be properly aligned,
165                  * increasing compared to the last %fp (or the top of the
166                  * stack we just switched to), and it must be inside
167                  * [base, base + size).
168                  */
169                 if (tmp != SA(tmp))
170                         break;
171                 else if (nextfp <= minfp || (tmp - base) >= size) {
172 #ifndef UMEM_STANDALONE
173                         if (tmp == NULL || !on_altstack)
174                                 break;
175                         /*
176                          * If we're on an alternate signal stack, try jumping
177                          * to the main thread stack.
178                          *
179                          * If the main thread stack has an unlimited size, we
180                          * punt, since we don't know where the frame pointer's
181                          * been.
182                          *
183                          * (thr_stksegment() returns the *top of stack*
184                          * in ss_sp, not the bottom)
185                          */
186                         if (thr_stksegment(&st) == 0) {
187                                 if (st.ss_size >= (uintptr_t)st.ss_sp ||
188                                     st.ss_size < UMEM_FRAMESIZE - 1)
189                                         break;
190
191                                 on_altstack = 0;
192                                 base = (uintptr_t)st.ss_sp - st.ss_size;
193                                 size = st.ss_size - (UMEM_FRAMESIZE - 1);
194                                 minfp = (struct frame *)base;
195                                 continue;               /* try again */
196                         }
197 #endif
198                         break;
199                 }
200
201 #ifndef UMEM_STANDALONE
202                 if (check_signal && (fp->fr_savpc - sigbase) <= sigsize)
203                         umem_panic("called from signal handler");
204 #endif
205                 pcstack[depth++] = fp->fr_savpc;
206                 fp = nextfp;
207                 minfp = fp;
208         }
209         return (depth);
210 #endif
211 }