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