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