2 * Copyright (c) 1991, 1998 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. [rescinded 22 July 1999]
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid
[] = "@(#)gmon.c 5.3 (Berkeley) 5/22/91";
45 extern mcount() asm ("mcount");
46 extern char *minbrk
asm ("minbrk");
53 * froms is actually a bunch of unsigned shorts indexing tos
55 static int profiling
= 3;
56 static unsigned short *froms
;
57 static struct tostruct
*tos
= 0;
58 static long tolimit
= 0;
59 static char *s_lowpc
= 0;
60 static char *s_highpc
= 0;
61 static unsigned long s_textsize
= 0;
66 /* see profil(2) where this is describe (incorrectly) */
67 #define SCALE_1_TO_1 0x10000L
69 #define MSG "No space for profiling buffer(s)\n"
71 monstartup(lowpc
, highpc
)
80 * round lowpc and highpc to multiples of the density we're using
81 * so the rest of the scaling (here and in gprof) stays in ints.
84 ROUNDDOWN((unsigned) lowpc
, HISTFRACTION
*sizeof(HISTCOUNTER
));
87 ROUNDUP((unsigned) highpc
, HISTFRACTION
*sizeof(HISTCOUNTER
));
89 s_textsize
= highpc
- lowpc
;
90 monsize
= (s_textsize
/ HISTFRACTION
) + sizeof(struct phdr
);
91 buffer
= sbrk( monsize
);
92 if ( buffer
== (char *) -1 ) {
93 write( 2 , MSG
, sizeof(MSG
) );
96 froms
= (unsigned short *) sbrk( s_textsize
/ HASHFRACTION
);
97 if ( froms
== (unsigned short *) -1 ) {
98 write( 2 , MSG
, sizeof(MSG
) );
102 tolimit
= s_textsize
* ARCDENSITY
/ 100;
103 if ( tolimit
< MINARCS
) {
105 } else if ( tolimit
> 65534 ) {
108 tos
= (struct tostruct
*) sbrk( tolimit
* sizeof( struct tostruct
) );
109 if ( tos
== (struct tostruct
*) -1 ) {
110 write( 2 , MSG
, sizeof(MSG
) );
119 ( (struct phdr
*) buffer
) -> lpc
= lowpc
;
120 ( (struct phdr
*) buffer
) -> hpc
= highpc
;
121 ( (struct phdr
*) buffer
) -> ncnt
= ssiz
;
122 monsize
-= sizeof(struct phdr
);
128 s_scale
= ( (float) monsize
/ o
) * SCALE_1_TO_1
;
129 #else /* avoid floating point */
131 int quot
= o
/ monsize
;
135 else if (quot
>= 0x100)
136 s_scale
= 0x10000 / quot
;
137 else if (o
>= 0x800000)
138 s_scale
= 0x1000000 / (o
/ (monsize
>> 8));
140 s_scale
= 0x1000000 / ((o
<< 8) / monsize
);
144 s_scale
= SCALE_1_TO_1
;
155 struct rawarc rawarc
;
158 fd
= creat( "gmon.out" , 0666 );
160 perror( "mcount: gmon.out" );
164 fprintf( stderr
, "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf
, ssiz
);
166 write( fd
, sbuf
, ssiz
);
167 endfrom
= s_textsize
/ (HASHFRACTION
* sizeof(*froms
));
168 for ( fromindex
= 0 ; fromindex
< endfrom
; fromindex
++ ) {
169 if ( froms
[fromindex
] == 0 ) {
172 frompc
= s_lowpc
+ (fromindex
* HASHFRACTION
* sizeof(*froms
));
173 for (toindex
=froms
[fromindex
]; toindex
!=0; toindex
=tos
[toindex
].link
) {
176 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
177 frompc
, tos
[toindex
].selfpc
, tos
[toindex
].count
);
179 rawarc
.raw_frompc
= (unsigned long) frompc
;
180 rawarc
.raw_selfpc
= (unsigned long) tos
[toindex
].selfpc
;
181 rawarc
.raw_count
= tos
[toindex
].count
;
182 write( fd
, &rawarc
, sizeof rawarc
);
190 register char *selfpc
;
191 register unsigned short *frompcindex
;
192 register struct tostruct
*top
;
193 register struct tostruct
*prevtop
;
194 register long toindex
;
197 * find the return address for mcount,
198 * and the return address for mcount's caller.
201 /* selfpc = pc pushed by mcount call.
202 This identifies the function that was just entered. */
203 selfpc
= (void *) __builtin_return_address (0);
204 /* frompcindex = pc in preceding frame.
205 This identifies the caller of the function just entered. */
206 frompcindex
= (void *) __builtin_return_address (1);
208 * check that we are profiling
209 * and that we aren't recursively invoked.
216 * check that frompcindex is a reasonable pc value.
217 * for example: signal catchers get called from the stack,
218 * not from text space. too bad.
220 frompcindex
= (unsigned short *) ((long) frompcindex
- (long) s_lowpc
);
221 if ((unsigned long) frompcindex
> s_textsize
) {
225 &froms
[((long) frompcindex
) / (HASHFRACTION
* sizeof(*froms
))];
226 toindex
= *frompcindex
;
229 * first time traversing this arc
231 toindex
= ++tos
[0].link
;
232 if (toindex
>= tolimit
) {
235 *frompcindex
= toindex
;
237 top
->selfpc
= selfpc
;
243 if (top
->selfpc
== selfpc
) {
245 * arc at front of chain; usual case.
251 * have to go looking down chain for it.
252 * top points to what we are looking at,
253 * prevtop points to previous top.
254 * we know it is not at the head of the chain.
256 for (; /* goto done */; ) {
257 if (top
->link
== 0) {
259 * top is end of the chain and none of the chain
260 * had top->selfpc == selfpc.
261 * so we allocate a new tostruct
262 * and link it to the head of the chain.
264 toindex
= ++tos
[0].link
;
265 if (toindex
>= tolimit
) {
269 top
->selfpc
= selfpc
;
271 top
->link
= *frompcindex
;
272 *frompcindex
= toindex
;
276 * otherwise, check the next arc on the chain.
279 top
= &tos
[top
->link
];
280 if (top
->selfpc
== selfpc
) {
283 * increment its count
284 * move it to the head of the chain.
287 toindex
= prevtop
->link
;
288 prevtop
->link
= top
->link
;
289 top
->link
= *frompcindex
;
290 *frompcindex
= toindex
;
297 /* and fall through */
299 return; /* normal return restores saved registers */
302 profiling
++; /* halt further profiling */
303 # define TOLIMIT "mcount: tos overflow\n"
304 write(2, TOLIMIT
, sizeof(TOLIMIT
));
308 /* Control profiling;
309 profiling is what mcount checks to see if
310 all the data structures are ready. */
317 profil(sbuf
+ sizeof(struct phdr
), ssiz
- sizeof(struct phdr
),
318 (int)s_lowpc
, s_scale
);
322 profil((char *) 0, 0, 0, 0);