Added some support the new err(3) routines need
[unix-history] / lib / csu.i386 / gmon.c
CommitLineData
15637ed4
RG
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * 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(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)gmon.c 5.3 (Berkeley) 5/22/91";
36#endif /* LIBC_SCCS and not lint */
37
38
39#include <unistd.h>
40
41#ifdef DEBUG
42#include <stdio.h>
43#endif
44
45#include "gmon.h"
46
47extern mcount() asm ("mcount");
48extern char *minbrk asm ("minbrk");
49
50 /*
51 * froms is actually a bunch of unsigned shorts indexing tos
52 */
53static int profiling = 3;
54static unsigned short *froms;
55static struct tostruct *tos = 0;
56static long tolimit = 0;
57static char *s_lowpc = 0;
58static char *s_highpc = 0;
59static unsigned long s_textsize = 0;
60
61static int ssiz;
62static char *sbuf;
63static int s_scale;
64 /* see profil(2) where this is describe (incorrectly) */
65#define SCALE_1_TO_1 0x10000L
66
67#define MSG "No space for profiling buffer(s)\n"
68
69monstartup(lowpc, highpc)
70 char *lowpc;
71 char *highpc;
72{
73 int monsize;
74 char *buffer;
75 register int o;
76
77 /*
78 * round lowpc and highpc to multiples of the density we're using
79 * so the rest of the scaling (here and in gprof) stays in ints.
80 */
81 lowpc = (char *)
82 ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
83 s_lowpc = lowpc;
84 highpc = (char *)
85 ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
86 s_highpc = highpc;
87 s_textsize = highpc - lowpc;
88 monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
89 buffer = sbrk( monsize );
90 if ( buffer == (char *) -1 ) {
91 write( 2 , MSG , sizeof(MSG) );
92 return;
93 }
94 froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
95 if ( froms == (unsigned short *) -1 ) {
96 write( 2 , MSG , sizeof(MSG) );
97 froms = 0;
98 return;
99 }
100 tolimit = s_textsize * ARCDENSITY / 100;
101 if ( tolimit < MINARCS ) {
102 tolimit = MINARCS;
103 } else if ( tolimit > 65534 ) {
104 tolimit = 65534;
105 }
106 tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
107 if ( tos == (struct tostruct *) -1 ) {
108 write( 2 , MSG , sizeof(MSG) );
109 froms = 0;
110 tos = 0;
111 return;
112 }
113 minbrk = sbrk(0);
114 tos[0].link = 0;
115 sbuf = buffer;
116 ssiz = monsize;
117 ( (struct phdr *) buffer ) -> lpc = lowpc;
118 ( (struct phdr *) buffer ) -> hpc = highpc;
119 ( (struct phdr *) buffer ) -> ncnt = ssiz;
120 monsize -= sizeof(struct phdr);
121 if ( monsize <= 0 )
122 return;
123 o = highpc - lowpc;
124 if( monsize < o )
125#ifndef hp300
126 s_scale = ( (float) monsize / o ) * SCALE_1_TO_1;
127#else /* avoid floating point */
128 {
129 int quot = o / monsize;
130
131 if (quot >= 0x10000)
132 s_scale = 1;
133 else if (quot >= 0x100)
134 s_scale = 0x10000 / quot;
135 else if (o >= 0x800000)
136 s_scale = 0x1000000 / (o / (monsize >> 8));
137 else
138 s_scale = 0x1000000 / ((o << 8) / monsize);
139 }
140#endif
141 else
142 s_scale = SCALE_1_TO_1;
143 moncontrol(1);
144}
145
146_mcleanup()
147{
148 int fd;
149 int fromindex;
150 int endfrom;
151 char *frompc;
152 int toindex;
153 struct rawarc rawarc;
154
155 moncontrol(0);
156 fd = creat( "gmon.out" , 0666 );
157 if ( fd < 0 ) {
158 perror( "mcount: gmon.out" );
159 return;
160 }
161# ifdef DEBUG
162 fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
163# endif DEBUG
164 write( fd , sbuf , ssiz );
165 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
166 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
167 if ( froms[fromindex] == 0 ) {
168 continue;
169 }
170 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
171 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
172# ifdef DEBUG
173 fprintf( stderr ,
174 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
175 frompc , tos[toindex].selfpc , tos[toindex].count );
176# endif DEBUG
177 rawarc.raw_frompc = (unsigned long) frompc;
178 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
179 rawarc.raw_count = tos[toindex].count;
180 write( fd , &rawarc , sizeof rawarc );
181 }
182 }
183 close( fd );
184}
185
186mcount()
187{
188 register char *selfpc;
189 register unsigned short *frompcindex;
190 register struct tostruct *top;
191 register struct tostruct *prevtop;
192 register long toindex;
193
194 /*
195 * find the return address for mcount,
196 * and the return address for mcount's caller.
197 */
198 asm(".text"); /* make sure we're in text space */
199 /*
200 * selfpc = pc pushed by mcount call
201 */
202 asm("movl 4(%%ebp),%0" : "=r" (selfpc));
203 /*
204 * frompcindex = pc pushed by jsr into self.
205 * In GCC the caller's stack frame has already been built so we
206 * have to chase a6 to find caller's raddr.
207 */
208 asm("movl (%%ebp),%0" : "=r" (frompcindex));
209 frompcindex = ((unsigned short **)frompcindex)[1];
210 /*
211 * check that we are profiling
212 * and that we aren't recursively invoked.
213 */
214 if (profiling) {
215 goto out;
216 }
217 profiling++;
218 /*
219 * check that frompcindex is a reasonable pc value.
220 * for example: signal catchers get called from the stack,
221 * not from text space. too bad.
222 */
223 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
224 if ((unsigned long)frompcindex > s_textsize) {
225 goto done;
226 }
227 frompcindex =
228 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
229 toindex = *frompcindex;
230 if (toindex == 0) {
231 /*
232 * first time traversing this arc
233 */
234 toindex = ++tos[0].link;
235 if (toindex >= tolimit) {
236 goto overflow;
237 }
238 *frompcindex = toindex;
239 top = &tos[toindex];
240 top->selfpc = selfpc;
241 top->count = 1;
242 top->link = 0;
243 goto done;
244 }
245 top = &tos[toindex];
246 if (top->selfpc == selfpc) {
247 /*
248 * arc at front of chain; usual case.
249 */
250 top->count++;
251 goto done;
252 }
253 /*
254 * have to go looking down chain for it.
255 * top points to what we are looking at,
256 * prevtop points to previous top.
257 * we know it is not at the head of the chain.
258 */
259 for (; /* goto done */; ) {
260 if (top->link == 0) {
261 /*
262 * top is end of the chain and none of the chain
263 * had top->selfpc == selfpc.
264 * so we allocate a new tostruct
265 * and link it to the head of the chain.
266 */
267 toindex = ++tos[0].link;
268 if (toindex >= tolimit) {
269 goto overflow;
270 }
271 top = &tos[toindex];
272 top->selfpc = selfpc;
273 top->count = 1;
274 top->link = *frompcindex;
275 *frompcindex = toindex;
276 goto done;
277 }
278 /*
279 * otherwise, check the next arc on the chain.
280 */
281 prevtop = top;
282 top = &tos[top->link];
283 if (top->selfpc == selfpc) {
284 /*
285 * there it is.
286 * increment its count
287 * move it to the head of the chain.
288 */
289 top->count++;
290 toindex = prevtop->link;
291 prevtop->link = top->link;
292 top->link = *frompcindex;
293 *frompcindex = toindex;
294 goto done;
295 }
296
297 }
298done:
299 profiling--;
300 /* and fall through */
301out:
302 return; /* normal return restores saved registers */
303
304overflow:
305 profiling++; /* halt further profiling */
306# define TOLIMIT "mcount: tos overflow\n"
307 write(2, TOLIMIT, sizeof(TOLIMIT));
308 goto out;
309}
310
311/*
312 * Control profiling
313 * profiling is what mcount checks to see if
314 * all the data structures are ready.
315 */
316moncontrol(mode)
317 int mode;
318{
319 if (mode) {
320 /* start */
321 profil(sbuf + sizeof(struct phdr), ssiz - sizeof(struct phdr),
322 (int)s_lowpc, s_scale);
323 profiling = 0;
324 } else {
325 /* stop */
326 profil((char *)0, 0, 0, 0);
327 profiling = 3;
328 }
329}