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