xref: /netbsd-src/common/lib/libc/gmon/mcount.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: mcount.c,v 1.7 2006/10/27 22:14:13 uwe 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.7 2006/10/27 22:14:13 uwe Exp $");
80 #endif
81 #endif
82 
83 #include <sys/param.h>
84 #include <sys/gmon.h>
85 
86 #ifndef _KERNEL
87 #include "reentrant.h"
88 #endif
89 
90 #ifdef _REENTRANT
91 extern thread_key_t _gmonkey;
92 extern struct gmonparam _gmondummy;
93 struct gmonparam *_m_gmon_alloc(void);
94 #endif
95 
96 _MCOUNT_DECL __P((u_long, u_long))
97 #ifdef _KERNEL
98     __attribute__((__no_instrument_function__))
99 #endif
100     __used;
101 
102 /*
103  * mcount is called on entry to each function compiled with the profiling
104  * switch set.  _mcount(), which is declared in a machine-dependent way
105  * with _MCOUNT_DECL, does the actual work and is either inlined into a
106  * C routine or called by an assembly stub.  In any case, this magic is
107  * taken care of by the MCOUNT definition in <machine/profile.h>.
108  *
109  * _mcount updates data structures that represent traversals of the
110  * program's call graph edges.  frompc and selfpc are the return
111  * address and function address that represents the given call graph edge.
112  *
113  * Note: the original BSD code used the same variable (frompcindex) for
114  * both frompcindex and frompc.  Any reasonable, modern compiler will
115  * perform this optimization.
116  */
117 _MCOUNT_DECL(frompc, selfpc)	/* _mcount; may be static, inline, etc */
118 	u_long frompc, selfpc;
119 {
120 	u_short *frompcindex;
121 	struct tostruct *top, *prevtop;
122 	struct gmonparam *p;
123 	long toindex;
124 #ifdef _KERNEL
125 	int s;
126 #endif
127 
128 #if defined(_REENTRANT) && !defined(_KERNEL)
129 	if (__isthreaded) {
130 		p = thr_getspecific(_gmonkey);
131 		if (p == NULL) {
132 			/* Prevent recursive calls while allocating */
133 			thr_setspecific(_gmonkey, &_gmondummy);
134 			p = _m_gmon_alloc();
135 		}
136 	} else
137 #endif
138 		p = &_gmonparam;
139 	/*
140 	 * check that we are profiling
141 	 * and that we aren't recursively invoked.
142 	 */
143 	if (p->state != GMON_PROF_ON)
144 		return;
145 #ifdef _KERNEL
146 	MCOUNT_ENTER;
147 #endif
148 	p->state = GMON_PROF_BUSY;
149 	/*
150 	 * check that frompcindex is a reasonable pc value.
151 	 * for example:	signal catchers get called from the stack,
152 	 *		not from text space.  too bad.
153 	 */
154 	frompc -= p->lowpc;
155 	if (frompc > p->textsize)
156 		goto done;
157 
158 #if (HASHFRACTION & (HASHFRACTION - 1)) == 0
159 	if (p->hashfraction == HASHFRACTION)
160 		frompcindex =
161 		    &p->froms[
162 		    (size_t)(frompc / (HASHFRACTION * sizeof(*p->froms)))];
163 	else
164 #endif
165 		frompcindex =
166 		    &p->froms[
167 		    (size_t)(frompc / (p->hashfraction * sizeof(*p->froms)))];
168 	toindex = *frompcindex;
169 	if (toindex == 0) {
170 		/*
171 		 *	first time traversing this arc
172 		 */
173 		toindex = ++p->tos[0].link;
174 		if (toindex >= p->tolimit)
175 			/* halt further profiling */
176 			goto overflow;
177 
178 		*frompcindex = (u_short)toindex;
179 		top = &p->tos[(size_t)toindex];
180 		top->selfpc = selfpc;
181 		top->count = 1;
182 		top->link = 0;
183 		goto done;
184 	}
185 	top = &p->tos[(size_t)toindex];
186 	if (top->selfpc == selfpc) {
187 		/*
188 		 * arc at front of chain; usual case.
189 		 */
190 		top->count++;
191 		goto done;
192 	}
193 	/*
194 	 * have to go looking down chain for it.
195 	 * top points to what we are looking at,
196 	 * prevtop points to previous top.
197 	 * we know it is not at the head of the chain.
198 	 */
199 	for (; /* goto done */; ) {
200 		if (top->link == 0) {
201 			/*
202 			 * top is end of the chain and none of the chain
203 			 * had top->selfpc == selfpc.
204 			 * so we allocate a new tostruct
205 			 * and link it to the head of the chain.
206 			 */
207 			toindex = ++p->tos[0].link;
208 			if (toindex >= p->tolimit)
209 				goto overflow;
210 
211 			top = &p->tos[(size_t)toindex];
212 			top->selfpc = selfpc;
213 			top->count = 1;
214 			top->link = *frompcindex;
215 			*frompcindex = (u_short)toindex;
216 			goto done;
217 		}
218 		/*
219 		 * otherwise, check the next arc on the chain.
220 		 */
221 		prevtop = top;
222 		top = &p->tos[top->link];
223 		if (top->selfpc == selfpc) {
224 			/*
225 			 * there it is.
226 			 * increment its count
227 			 * move it to the head of the chain.
228 			 */
229 			top->count++;
230 			toindex = prevtop->link;
231 			prevtop->link = top->link;
232 			top->link = *frompcindex;
233 			*frompcindex = (u_short)toindex;
234 			goto done;
235 		}
236 
237 	}
238 done:
239 	p->state = GMON_PROF_ON;
240 #ifdef _KERNEL
241 	MCOUNT_EXIT;
242 #endif
243 	return;
244 overflow:
245 	p->state = GMON_PROF_ERROR;
246 #ifdef _KERNEL
247 	MCOUNT_EXIT;
248 #endif
249 	return;
250 }
251 
252 #ifdef MCOUNT
253 /*
254  * Actual definition of mcount function.  Defined in <machine/profile.h>,
255  * which is included by <sys/gmon.h>.
256  */
257 MCOUNT
258 #endif
259 
260 #endif /* (!_KERNEL || GPROF) && !_STANDALONE */
261