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