]>
Commit | Line | Data |
---|---|---|
11c981a9 RM |
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. | |
11c981a9 RM |
13 | * 4. 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 sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93"; | |
32 | #endif | |
33 | ||
34 | #include <sys/param.h> | |
35 | #include <sys/gmon.h> | |
36 | ||
bfc04a9f RM |
37 | /* This file provides the machine-dependent definitions of the _MCOUNT_DECL |
38 | and MCOUNT macros. */ | |
39 | #include "machine-gmon.h" | |
40 | ||
9a0a462c UD |
41 | #include <atomicity.h> |
42 | ||
11c981a9 RM |
43 | /* |
44 | * mcount is called on entry to each function compiled with the profiling | |
45 | * switch set. _mcount(), which is declared in a machine-dependent way | |
46 | * with _MCOUNT_DECL, does the actual work and is either inlined into a | |
47 | * C routine or called by an assembly stub. In any case, this magic is | |
48 | * taken care of by the MCOUNT definition in <machine/profile.h>. | |
49 | * | |
50 | * _mcount updates data structures that represent traversals of the | |
51 | * program's call graph edges. frompc and selfpc are the return | |
52 | * address and function address that represents the given call graph edge. | |
dbdb6189 | 53 | * |
11c981a9 RM |
54 | * Note: the original BSD code used the same variable (frompcindex) for |
55 | * both frompcindex and frompc. Any reasonable, modern compiler will | |
56 | * perform this optimization. | |
57 | */ | |
58 | _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */ | |
11c981a9 RM |
59 | { |
60 | register u_short *frompcindex; | |
61 | register struct tostruct *top, *prevtop; | |
62 | register struct gmonparam *p; | |
63 | register long toindex; | |
b20e47cb | 64 | int i; |
11c981a9 RM |
65 | |
66 | p = &_gmonparam; | |
67 | /* | |
68 | * check that we are profiling | |
69 | * and that we aren't recursively invoked. | |
70 | */ | |
9a0a462c UD |
71 | if (! compare_and_swap (&p->state, GMON_PROF_ON, GMON_PROF_BUSY)) |
72 | return; | |
73 | ||
11c981a9 RM |
74 | /* |
75 | * check that frompcindex is a reasonable pc value. | |
76 | * for example: signal catchers get called from the stack, | |
77 | * not from text space. too bad. | |
78 | */ | |
79 | frompc -= p->lowpc; | |
80 | if (frompc > p->textsize) | |
81 | goto done; | |
82 | ||
9a0a462c UD |
83 | /* The following test used to be |
84 | if (p->log_hashfraction >= 0) | |
85 | But we can simplify this if we assume the profiling data | |
86 | is always initialized by the functions in gmon.c. But | |
87 | then it is possible to avoid a runtime check and use the | |
88 | smae `if' as in gmon.c. So keep these tests in sync. */ | |
89 | if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) { | |
90 | /* avoid integer divide if possible: */ | |
b20e47cb RM |
91 | i = frompc >> p->log_hashfraction; |
92 | } else { | |
93 | i = frompc / (p->hashfraction * sizeof(*p->froms)); | |
94 | } | |
95 | frompcindex = &p->froms[i]; | |
11c981a9 RM |
96 | toindex = *frompcindex; |
97 | if (toindex == 0) { | |
98 | /* | |
99 | * first time traversing this arc | |
100 | */ | |
101 | toindex = ++p->tos[0].link; | |
102 | if (toindex >= p->tolimit) | |
103 | /* halt further profiling */ | |
104 | goto overflow; | |
105 | ||
106 | *frompcindex = toindex; | |
107 | top = &p->tos[toindex]; | |
108 | top->selfpc = selfpc; | |
109 | top->count = 1; | |
110 | top->link = 0; | |
111 | goto done; | |
112 | } | |
113 | top = &p->tos[toindex]; | |
114 | if (top->selfpc == selfpc) { | |
115 | /* | |
116 | * arc at front of chain; usual case. | |
117 | */ | |
118 | top->count++; | |
119 | goto done; | |
120 | } | |
121 | /* | |
122 | * have to go looking down chain for it. | |
123 | * top points to what we are looking at, | |
124 | * prevtop points to previous top. | |
125 | * we know it is not at the head of the chain. | |
126 | */ | |
127 | for (; /* goto done */; ) { | |
128 | if (top->link == 0) { | |
129 | /* | |
130 | * top is end of the chain and none of the chain | |
131 | * had top->selfpc == selfpc. | |
132 | * so we allocate a new tostruct | |
133 | * and link it to the head of the chain. | |
134 | */ | |
135 | toindex = ++p->tos[0].link; | |
136 | if (toindex >= p->tolimit) | |
137 | goto overflow; | |
138 | ||
139 | top = &p->tos[toindex]; | |
140 | top->selfpc = selfpc; | |
141 | top->count = 1; | |
142 | top->link = *frompcindex; | |
143 | *frompcindex = toindex; | |
144 | goto done; | |
145 | } | |
146 | /* | |
147 | * otherwise, check the next arc on the chain. | |
148 | */ | |
149 | prevtop = top; | |
150 | top = &p->tos[top->link]; | |
151 | if (top->selfpc == selfpc) { | |
152 | /* | |
153 | * there it is. | |
154 | * increment its count | |
155 | * move it to the head of the chain. | |
156 | */ | |
157 | top->count++; | |
158 | toindex = prevtop->link; | |
159 | prevtop->link = top->link; | |
160 | top->link = *frompcindex; | |
161 | *frompcindex = toindex; | |
162 | goto done; | |
163 | } | |
dbdb6189 | 164 | |
11c981a9 RM |
165 | } |
166 | done: | |
11c981a9 | 167 | p->state = GMON_PROF_ON; |
11c981a9 RM |
168 | return; |
169 | overflow: | |
170 | p->state = GMON_PROF_ERROR; | |
11c981a9 RM |
171 | return; |
172 | } | |
173 | ||
174 | /* | |
175 | * Actual definition of mcount function. Defined in <machine/profile.h>, | |
176 | * which is included by <sys/gmon.h>. | |
177 | */ | |
178 | MCOUNT |