xref: /openbsd-src/sys/lib/libkern/mcount.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: mcount.c,v 1.4 1997/11/07 15:56:45 niklas Exp $	*/
2 /*	$NetBSD: mcount.c,v 1.3.6.1 1996/06/12 04:23:01 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1983, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if !defined(lint) && !defined(_KERNEL) && defined(LIBC_SCCS)
38 #if 0
39 static char sccsid[] = "@(#)mcount.c	8.1 (Berkeley) 6/4/93";
40 #else
41 static char rcsid[] = "$OpenBSD: mcount.c,v 1.4 1997/11/07 15:56:45 niklas Exp $";
42 #endif
43 #endif
44 
45 #include <sys/param.h>
46 #include <sys/gmon.h>
47 
48 /*
49  * mcount is called on entry to each function compiled with the profiling
50  * switch set.  _mcount(), which is declared in a machine-dependent way
51  * with _MCOUNT_DECL, does the actual work and is either inlined into a
52  * C routine or called by an assembly stub.  In any case, this magic is
53  * taken care of by the MCOUNT definition in <machine/profile.h>.
54  *
55  * _mcount updates data structures that represent traversals of the
56  * program's call graph edges.  frompc and selfpc are the return
57  * address and function address that represents the given call graph edge.
58  *
59  * Note: the original BSD code used the same variable (frompcindex) for
60  * both frompcindex and frompc.  Any reasonable, modern compiler will
61  * perform this optimization.
62  */
63 _MCOUNT_DECL(u_long frompc, u_long selfpc);
64 _MCOUNT_DECL(frompc, selfpc)	/* _mcount; may be static, inline, etc */
65 	register u_long frompc, selfpc;
66 {
67 	register u_short *frompcindex;
68 	register struct tostruct *top, *prevtop;
69 	register struct gmonparam *p;
70 	register long toindex;
71 #ifdef _KERNEL
72 	register int s;
73 #endif
74 
75 	p = &_gmonparam;
76 	/*
77 	 * check that we are profiling
78 	 * and that we aren't recursively invoked.
79 	 */
80 	if (p->state != GMON_PROF_ON)
81 		return;
82 #ifdef _KERNEL
83 	MCOUNT_ENTER;
84 #else
85 	p->state = GMON_PROF_BUSY;
86 #endif
87 	/*
88 	 * check that frompcindex is a reasonable pc value.
89 	 * for example:	signal catchers get called from the stack,
90 	 *		not from text space.  too bad.
91 	 */
92 	frompc -= p->lowpc;
93 	if (frompc > p->textsize)
94 		goto done;
95 
96 #if (HASHFRACTION & (HASHFRACTION - 1)) == 0
97 	if (p->hashfraction == HASHFRACTION)
98 		frompcindex =
99 		    &p->froms[frompc / (HASHFRACTION * sizeof(*p->froms))];
100 	else
101 #endif
102 		frompcindex =
103 		    &p->froms[frompc / (p->hashfraction * sizeof(*p->froms))];
104 	toindex = *frompcindex;
105 	if (toindex == 0) {
106 		/*
107 		 *	first time traversing this arc
108 		 */
109 		toindex = ++p->tos[0].link;
110 		if (toindex >= p->tolimit)
111 			/* halt further profiling */
112 			goto overflow;
113 
114 		*frompcindex = toindex;
115 		top = &p->tos[toindex];
116 		top->selfpc = selfpc;
117 		top->count = 1;
118 		top->link = 0;
119 		goto done;
120 	}
121 	top = &p->tos[toindex];
122 	if (top->selfpc == selfpc) {
123 		/*
124 		 * arc at front of chain; usual case.
125 		 */
126 		top->count++;
127 		goto done;
128 	}
129 	/*
130 	 * have to go looking down chain for it.
131 	 * top points to what we are looking at,
132 	 * prevtop points to previous top.
133 	 * we know it is not at the head of the chain.
134 	 */
135 	for (; /* goto done */; ) {
136 		if (top->link == 0) {
137 			/*
138 			 * top is end of the chain and none of the chain
139 			 * had top->selfpc == selfpc.
140 			 * so we allocate a new tostruct
141 			 * and link it to the head of the chain.
142 			 */
143 			toindex = ++p->tos[0].link;
144 			if (toindex >= p->tolimit)
145 				goto overflow;
146 
147 			top = &p->tos[toindex];
148 			top->selfpc = selfpc;
149 			top->count = 1;
150 			top->link = *frompcindex;
151 			*frompcindex = toindex;
152 			goto done;
153 		}
154 		/*
155 		 * otherwise, check the next arc on the chain.
156 		 */
157 		prevtop = top;
158 		top = &p->tos[top->link];
159 		if (top->selfpc == selfpc) {
160 			/*
161 			 * there it is.
162 			 * increment its count
163 			 * move it to the head of the chain.
164 			 */
165 			top->count++;
166 			toindex = prevtop->link;
167 			prevtop->link = top->link;
168 			top->link = *frompcindex;
169 			*frompcindex = toindex;
170 			goto done;
171 		}
172 
173 	}
174 done:
175 #ifdef _KERNEL
176 	MCOUNT_EXIT;
177 #else
178 	p->state = GMON_PROF_ON;
179 #endif
180 	return;
181 overflow:
182 	p->state = GMON_PROF_ERROR;
183 #ifdef _KERNEL
184 	MCOUNT_EXIT;
185 #endif
186 	return;
187 }
188 
189 /*
190  * Actual definition of mcount function.  Defined in <machine/profile.h>,
191  * which is included by <sys/gmon.h>.
192  */
193 MCOUNT
194