mcount.c revision 1.12 1 /* $NetBSD: mcount.c,v 1.12 2016/01/11 01:57:12 christos Exp $ */
2
3 /*
4 * Copyright (c) 2003, 2004 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Nathan J. Williams for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*-
39 * Copyright (c) 1983, 1992, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 */
66
67 /* If building a standalone libkern, don't include mcount. */
68 #if (!defined(_KERNEL) || defined(GPROF)) && !defined(_STANDALONE)
69
70 #ifdef _KERNEL_OPT
71 #include "opt_multiprocessor.h"
72 #endif
73
74 #include <sys/cdefs.h>
75 #if !defined(lint) && !defined(_KERNEL) && defined(LIBC_SCCS)
76 #if 0
77 static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93";
78 #else
79 __RCSID("$NetBSD: mcount.c,v 1.12 2016/01/11 01:57:12 christos Exp $");
80 #endif
81 #endif
82
83 #include <sys/param.h>
84 #include <sys/gmon.h>
85 #include <sys/lock.h>
86
87 #ifndef _KERNEL
88 #include "reentrant.h"
89 #endif
90
91 #ifdef _REENTRANT
92 extern thread_key_t _gmonkey;
93 extern struct gmonparam _gmondummy;
94 struct gmonparam *_m_gmon_alloc(void);
95 #endif
96
97 #if defined(_KERNEL) && !defined(_RUMPKERNEL) && defined(MULTIPROCESSOR)
98 __cpu_simple_lock_t __mcount_lock;
99 #endif
100
101 #ifndef __LINT__
102 _MCOUNT_DECL(u_long, u_long)
103 #ifdef _KERNEL
104 __attribute__((__no_instrument_function__))
105 #endif
106 __used;
107 #endif
108
109 /* XXX: make these interfaces */
110 #ifdef _RUMPKERNEL
111 #undef MCOUNT_ENTER
112 #define MCOUNT_ENTER
113 #undef MCOUNT_EXIT
114 #define MCOUNT_EXIT
115 #undef MCOUNT
116 #define MCOUNT
117 #endif
118
119 /*
120 * mcount is called on entry to each function compiled with the profiling
121 * switch set. _mcount(), which is declared in a machine-dependent way
122 * with _MCOUNT_DECL, does the actual work and is either inlined into a
123 * C routine or called by an assembly stub. In any case, this magic is
124 * taken care of by the MCOUNT definition in <machine/profile.h>.
125 *
126 * _mcount updates data structures that represent traversals of the
127 * program's call graph edges. frompc and selfpc are the return
128 * address and function address that represents the given call graph edge.
129 *
130 * Note: the original BSD code used the same variable (frompcindex) for
131 * both frompcindex and frompc. Any reasonable, modern compiler will
132 * perform this optimization.
133 */
134 #ifndef __LINT__
135 /* _mcount; may be static, inline, etc */
136 _MCOUNT_DECL(u_long frompc, u_long selfpc)
137 {
138 u_short *frompcindex;
139 struct tostruct *top, *prevtop;
140 struct gmonparam *p;
141 long toindex;
142 #if defined(_KERNEL) && !defined(_RUMPKERNEL)
143 int s;
144 #endif
145
146 #if defined(_REENTRANT) && !defined(_KERNEL)
147 if (__isthreaded) {
148 p = thr_getspecific(_gmonkey);
149 if (p == NULL) {
150 /* Prevent recursive calls while allocating */
151 thr_setspecific(_gmonkey, &_gmondummy);
152 p = _m_gmon_alloc();
153 }
154 } else
155 #endif
156 p = &_gmonparam;
157 /*
158 * check that we are profiling
159 * and that we aren't recursively invoked.
160 */
161 if (p->state != GMON_PROF_ON)
162 return;
163 #if defined(_KERNEL) && !defined(_RUMPKERNEL)
164 MCOUNT_ENTER;
165 #ifdef MULTIPROCESSOR
166 __cpu_simple_lock(&__mcount_lock);
167 __insn_barrier();
168 #endif
169 #endif
170 p->state = GMON_PROF_BUSY;
171 /*
172 * check that frompcindex is a reasonable pc value.
173 * for example: signal catchers get called from the stack,
174 * not from text space. too bad.
175 */
176 frompc -= p->lowpc;
177 if (frompc > p->textsize)
178 goto done;
179
180 #if (HASHFRACTION & (HASHFRACTION - 1)) == 0
181 if (p->hashfraction == HASHFRACTION)
182 frompcindex =
183 &p->froms[
184 (size_t)(frompc / (HASHFRACTION * sizeof(*p->froms)))];
185 else
186 #endif
187 frompcindex =
188 &p->froms[
189 (size_t)(frompc / (p->hashfraction * sizeof(*p->froms)))];
190 toindex = *frompcindex;
191 if (toindex == 0) {
192 /*
193 * first time traversing this arc
194 */
195 toindex = ++p->tos[0].link;
196 if (toindex >= p->tolimit)
197 /* halt further profiling */
198 goto overflow;
199
200 *frompcindex = (u_short)toindex;
201 top = &p->tos[(size_t)toindex];
202 top->selfpc = selfpc;
203 top->count = 1;
204 top->link = 0;
205 goto done;
206 }
207 top = &p->tos[(size_t)toindex];
208 if (top->selfpc == selfpc) {
209 /*
210 * arc at front of chain; usual case.
211 */
212 top->count++;
213 goto done;
214 }
215 /*
216 * have to go looking down chain for it.
217 * top points to what we are looking at,
218 * prevtop points to previous top.
219 * we know it is not at the head of the chain.
220 */
221 for (; /* goto done */; ) {
222 if (top->link == 0) {
223 /*
224 * top is end of the chain and none of the chain
225 * had top->selfpc == selfpc.
226 * so we allocate a new tostruct
227 * and link it to the head of the chain.
228 */
229 toindex = ++p->tos[0].link;
230 if (toindex >= p->tolimit)
231 goto overflow;
232
233 top = &p->tos[(size_t)toindex];
234 top->selfpc = selfpc;
235 top->count = 1;
236 top->link = *frompcindex;
237 *frompcindex = (u_short)toindex;
238 goto done;
239 }
240 /*
241 * otherwise, check the next arc on the chain.
242 */
243 prevtop = top;
244 top = &p->tos[top->link];
245 if (top->selfpc == selfpc) {
246 /*
247 * there it is.
248 * increment its count
249 * move it to the head of the chain.
250 */
251 top->count++;
252 toindex = prevtop->link;
253 prevtop->link = top->link;
254 top->link = *frompcindex;
255 *frompcindex = (u_short)toindex;
256 goto done;
257 }
258 }
259 done:
260 p->state = GMON_PROF_ON;
261 #if defined(_KERNEL) && !defined(_RUMPKERNEL)
262 #ifdef MULTIPROCESSOR
263 __insn_barrier();
264 __cpu_simple_unlock(&__mcount_lock);
265 #endif
266 MCOUNT_EXIT;
267 #endif
268 return;
269
270 overflow:
271 p->state = GMON_PROF_ERROR;
272 #if defined(_KERNEL) && !defined(_RUMPKERNEL)
273 #ifdef MULTIPROCESSOR
274 __insn_barrier();
275 __cpu_simple_unlock(&__mcount_lock);
276 #endif
277 MCOUNT_EXIT;
278 #endif
279 return;
280 }
281 #endif
282
283 #ifdef MCOUNT
284 /*
285 * Actual definition of mcount function. Defined in <machine/profile.h>,
286 * which is included by <sys/gmon.h>.
287 */
288 MCOUNT
289 #endif
290
291 #endif /* (!_KERNEL || GPROF) && !_STANDALONE */
292