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