Update.
[glibc.git] / gmon / mcount.c
blobfe392c0949adcce954fbaab4940eb7723e567792
1 /*-
2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #if !defined(lint) && !defined(KERNEL) && defined(LIBC_SCCS)
35 static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93";
36 #endif
38 #include <sys/param.h>
39 #include <sys/gmon.h>
41 /* This file provides the machine-dependent definitions of the _MCOUNT_DECL
42 and MCOUNT macros. */
43 #include "machine-gmon.h"
45 #include <atomicity.h>
48 * mcount is called on entry to each function compiled with the profiling
49 * switch set. _mcount(), which is declared in a machine-dependent way
50 * with _MCOUNT_DECL, does the actual work and is either inlined into a
51 * C routine or called by an assembly stub. In any case, this magic is
52 * taken care of by the MCOUNT definition in <machine/profile.h>.
54 * _mcount updates data structures that represent traversals of the
55 * program's call graph edges. frompc and selfpc are the return
56 * address and function address that represents the given call graph edge.
58 * Note: the original BSD code used the same variable (frompcindex) for
59 * both frompcindex and frompc. Any reasonable, modern compiler will
60 * perform this optimization.
62 _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */
64 register u_short *frompcindex;
65 register struct tostruct *top, *prevtop;
66 register struct gmonparam *p;
67 register long toindex;
68 int i;
70 p = &_gmonparam;
72 * check that we are profiling
73 * and that we aren't recursively invoked.
75 if (! compare_and_swap (&p->state, GMON_PROF_ON, GMON_PROF_BUSY))
76 return;
79 * check that frompcindex is a reasonable pc value.
80 * for example: signal catchers get called from the stack,
81 * not from text space. too bad.
83 frompc -= p->lowpc;
84 if (frompc > p->textsize)
85 goto done;
87 /* The following test used to be
88 if (p->log_hashfraction >= 0)
89 But we can simplify this if we assume the profiling data
90 is always initialized by the functions in gmon.c. But
91 then it is possible to avoid a runtime check and use the
92 smae `if' as in gmon.c. So keep these tests in sync. */
93 if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) {
94 /* avoid integer divide if possible: */
95 i = frompc >> p->log_hashfraction;
96 } else {
97 i = frompc / (p->hashfraction * sizeof(*p->froms));
99 frompcindex = &p->froms[i];
100 toindex = *frompcindex;
101 if (toindex == 0) {
103 * first time traversing this arc
105 toindex = ++p->tos[0].link;
106 if (toindex >= p->tolimit)
107 /* halt further profiling */
108 goto overflow;
110 *frompcindex = toindex;
111 top = &p->tos[toindex];
112 top->selfpc = selfpc;
113 top->count = 1;
114 top->link = 0;
115 goto done;
117 top = &p->tos[toindex];
118 if (top->selfpc == selfpc) {
120 * arc at front of chain; usual case.
122 top->count++;
123 goto done;
126 * have to go looking down chain for it.
127 * top points to what we are looking at,
128 * prevtop points to previous top.
129 * we know it is not at the head of the chain.
131 for (; /* goto done */; ) {
132 if (top->link == 0) {
134 * top is end of the chain and none of the chain
135 * had top->selfpc == selfpc.
136 * so we allocate a new tostruct
137 * and link it to the head of the chain.
139 toindex = ++p->tos[0].link;
140 if (toindex >= p->tolimit)
141 goto overflow;
143 top = &p->tos[toindex];
144 top->selfpc = selfpc;
145 top->count = 1;
146 top->link = *frompcindex;
147 *frompcindex = toindex;
148 goto done;
151 * otherwise, check the next arc on the chain.
153 prevtop = top;
154 top = &p->tos[top->link];
155 if (top->selfpc == selfpc) {
157 * there it is.
158 * increment its count
159 * move it to the head of the chain.
161 top->count++;
162 toindex = prevtop->link;
163 prevtop->link = top->link;
164 top->link = *frompcindex;
165 *frompcindex = toindex;
166 goto done;
170 done:
171 p->state = GMON_PROF_ON;
172 return;
173 overflow:
174 p->state = GMON_PROF_ERROR;
175 return;
179 * Actual definition of mcount function. Defined in <machine/profile.h>,
180 * which is included by <sys/gmon.h>.
182 MCOUNT