]> git.ipfire.org Git - thirdparty/glibc.git/blob - gmon/mcount.c
Tue Feb 13 00:12:12 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
[thirdparty/glibc.git] / gmon / mcount.c
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. 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.
20 *
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.
32 */
33
34 #if !defined(lint) && !defined(KERNEL) && defined(LIBC_SCCS)
35 static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93";
36 #endif
37
38 #include <sys/param.h>
39 #include <sys/gmon.h>
40
41 /*
42 * mcount is called on entry to each function compiled with the profiling
43 * switch set. _mcount(), which is declared in a machine-dependent way
44 * with _MCOUNT_DECL, does the actual work and is either inlined into a
45 * C routine or called by an assembly stub. In any case, this magic is
46 * taken care of by the MCOUNT definition in <machine/profile.h>.
47 *
48 * _mcount updates data structures that represent traversals of the
49 * program's call graph edges. frompc and selfpc are the return
50 * address and function address that represents the given call graph edge.
51 *
52 * Note: the original BSD code used the same variable (frompcindex) for
53 * both frompcindex and frompc. Any reasonable, modern compiler will
54 * perform this optimization.
55 */
56 #if 0
57 _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */
58 register u_long frompc, selfpc;
59 #endif
60
61 /* GCC version 2 gives us a perfect magical function to get
62 just the information we need:
63 void *__builtin_return_address (unsigned int N)
64 returns the return address of the frame N frames up. */
65
66 #if __GNUC__ < 2
67 #error "This file uses __builtin_return_address, a GCC 2 extension."
68 #endif
69
70 #include <sysdep.h>
71 #ifndef NO_UNDERSCORES
72 /* The asm symbols for C functions are `_function'.
73 The canonical name for the counter function is `mcount', no _. */
74 void _mcount (void) asm ("mcount");
75 #endif
76
77 void
78 _mcount (void)
79 {
80 register u_long selfpc = (u_long) __builtin_return_address (0);
81 register u_long frompc = (u_long) __builtin_return_address (1);
82
83 register u_short *frompcindex;
84 register struct tostruct *top, *prevtop;
85 register struct gmonparam *p;
86 register long toindex;
87 #ifdef KERNEL
88 register int s;
89 #endif
90
91 p = &_gmonparam;
92 /*
93 * check that we are profiling
94 * and that we aren't recursively invoked.
95 */
96 if (p->state != GMON_PROF_ON)
97 return;
98 #ifdef KERNEL
99 MCOUNT_ENTER;
100 #else
101 p->state = GMON_PROF_BUSY;
102 #endif
103 /*
104 * check that frompcindex is a reasonable pc value.
105 * for example: signal catchers get called from the stack,
106 * not from text space. too bad.
107 */
108 frompc -= p->lowpc;
109 if (frompc > p->textsize)
110 goto done;
111
112 frompcindex = &p->froms[frompc / (p->hashfraction * sizeof(*p->froms))];
113 toindex = *frompcindex;
114 if (toindex == 0) {
115 /*
116 * first time traversing this arc
117 */
118 toindex = ++p->tos[0].link;
119 if (toindex >= p->tolimit)
120 /* halt further profiling */
121 goto overflow;
122
123 *frompcindex = toindex;
124 top = &p->tos[toindex];
125 top->selfpc = selfpc;
126 top->count = 1;
127 top->link = 0;
128 goto done;
129 }
130 top = &p->tos[toindex];
131 if (top->selfpc == selfpc) {
132 /*
133 * arc at front of chain; usual case.
134 */
135 top->count++;
136 goto done;
137 }
138 /*
139 * have to go looking down chain for it.
140 * top points to what we are looking at,
141 * prevtop points to previous top.
142 * we know it is not at the head of the chain.
143 */
144 for (; /* goto done */; ) {
145 if (top->link == 0) {
146 /*
147 * top is end of the chain and none of the chain
148 * had top->selfpc == selfpc.
149 * so we allocate a new tostruct
150 * and link it to the head of the chain.
151 */
152 toindex = ++p->tos[0].link;
153 if (toindex >= p->tolimit)
154 goto overflow;
155
156 top = &p->tos[toindex];
157 top->selfpc = selfpc;
158 top->count = 1;
159 top->link = *frompcindex;
160 *frompcindex = toindex;
161 goto done;
162 }
163 /*
164 * otherwise, check the next arc on the chain.
165 */
166 prevtop = top;
167 top = &p->tos[top->link];
168 if (top->selfpc == selfpc) {
169 /*
170 * there it is.
171 * increment its count
172 * move it to the head of the chain.
173 */
174 top->count++;
175 toindex = prevtop->link;
176 prevtop->link = top->link;
177 top->link = *frompcindex;
178 *frompcindex = toindex;
179 goto done;
180 }
181
182 }
183 done:
184 #ifdef KERNEL
185 MCOUNT_EXIT;
186 #else
187 p->state = GMON_PROF_ON;
188 #endif
189 return;
190 overflow:
191 p->state = GMON_PROF_ERROR;
192 #ifdef KERNEL
193 MCOUNT_EXIT;
194 #endif
195 return;
196 }
197
198 #if 0 /* Obsolete with __builtin_return_address. */
199 /*
200 * Actual definition of mcount function. Defined in <machine/profile.h>,
201 * which is included by <sys/gmon.h>.
202 */
203 MCOUNT
204 #endif