8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libumem / common / getpcstack.c
blobc2dd7f03f1f7363bb11e1fb5d2c3b8d371a7ebbe
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
28 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include "misc.h"
31 #include <ucontext.h>
32 #include <sys/frame.h>
33 #include <sys/stack.h>
34 #include <stdio.h>
36 #if defined(__sparc) || defined(__sparcv9)
37 extern void flush_windows(void);
38 #define UMEM_FRAMESIZE MINFRAME
40 #elif defined(__i386) || defined(__amd64)
42 * On x86, MINFRAME is defined to be 0, but we want to be sure we can
43 * dereference the entire frame structure.
45 #define UMEM_FRAMESIZE (sizeof (struct frame))
47 #else
48 #error needs update for new architecture
49 #endif
52 * Get a pc-only stacktrace. Used for kmem_alloc() buffer ownership tracking.
53 * Returns MIN(current stack depth, pcstack_limit).
55 /*ARGSUSED*/
56 int
57 getpcstack(uintptr_t *pcstack, int pcstack_limit, int check_signal)
59 struct frame *fp;
60 struct frame *nextfp, *minfp;
61 int depth = 0;
62 uintptr_t base = 0;
63 size_t size = 0;
64 #ifndef UMEM_STANDALONE
65 int on_altstack = 0;
66 uintptr_t sigbase = 0;
67 size_t sigsize = 0;
69 stack_t st;
71 if (stack_getbounds(&st) != 0) {
72 if (thr_stksegment(&st) != 0 ||
73 (uintptr_t)st.ss_sp < st.ss_size) {
74 return (0); /* unable to get stack bounds */
77 * thr_stksegment(3C) has a slightly different interface than
78 * stack_getbounds(3C) -- correct it
80 st.ss_sp = (void *)(((uintptr_t)st.ss_sp) - st.ss_size);
81 st.ss_flags = 0; /* can't be on-stack */
83 on_altstack = (st.ss_flags & SS_ONSTACK);
85 if (st.ss_size != 0) {
86 base = (uintptr_t)st.ss_sp;
87 size = st.ss_size;
88 } else {
90 * If size == 0, then ss_sp is the *top* of the stack.
92 * Since we only allow increasing frame pointers, and we
93 * know our caller set its up correctly, we can treat ss_sp
94 * as an upper bound safely.
96 base = 0;
97 size = (uintptr_t)st.ss_sp;
100 if (check_signal != 0) {
101 void (*sigfunc)() = NULL;
102 int sigfuncsize = 0;
103 extern void thr_sighndlrinfo(void (**)(), int *);
105 thr_sighndlrinfo(&sigfunc, &sigfuncsize);
106 sigbase = (uintptr_t)sigfunc;
107 sigsize = sigfuncsize;
109 #else /* UMEM_STANDALONE */
110 base = (uintptr_t)umem_min_stack;
111 size = umem_max_stack - umem_min_stack;
112 #endif
115 * shorten size so that fr_savfp and fr_savpc will be within the stack
116 * bounds.
118 if (size >= UMEM_FRAMESIZE - 1)
119 size -= (UMEM_FRAMESIZE - 1);
120 else
121 size = 0;
123 #if defined(__sparc) || defined(__sparcv9)
124 flush_windows();
125 #endif
127 /* LINTED alignment */
128 fp = (struct frame *)((caddr_t)getfp() + STACK_BIAS);
130 minfp = fp;
132 if (((uintptr_t)fp - base) >= size)
133 return (0); /* the frame pointer isn't in our stack */
135 while (depth < pcstack_limit) {
136 uintptr_t tmp;
138 /* LINTED alignment */
139 nextfp = (struct frame *)((caddr_t)fp->fr_savfp + STACK_BIAS);
140 tmp = (uintptr_t)nextfp;
143 * Check nextfp for validity. It must be properly aligned,
144 * increasing compared to the last %fp (or the top of the
145 * stack we just switched to), and it must be inside
146 * [base, base + size).
148 if (tmp != SA(tmp))
149 break;
150 else if (nextfp <= minfp || (tmp - base) >= size) {
151 #ifndef UMEM_STANDALONE
152 if (tmp == NULL || !on_altstack)
153 break;
155 * If we're on an alternate signal stack, try jumping
156 * to the main thread stack.
158 * If the main thread stack has an unlimited size, we
159 * punt, since we don't know where the frame pointer's
160 * been.
162 * (thr_stksegment() returns the *top of stack*
163 * in ss_sp, not the bottom)
165 if (thr_stksegment(&st) == 0) {
166 if (st.ss_size >= (uintptr_t)st.ss_sp ||
167 st.ss_size < UMEM_FRAMESIZE - 1)
168 break;
170 on_altstack = 0;
171 base = (uintptr_t)st.ss_sp - st.ss_size;
172 size = st.ss_size - (UMEM_FRAMESIZE - 1);
173 minfp = (struct frame *)base;
174 continue; /* try again */
176 #endif
177 break;
180 #ifndef UMEM_STANDALONE
181 if (check_signal && (fp->fr_savpc - sigbase) <= sigsize)
182 umem_panic("called from signal handler");
183 #endif
184 pcstack[depth++] = fp->fr_savpc;
185 fp = nextfp;
186 minfp = fp;
188 return (depth);